main.c 470 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213
  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 10
  81. #define E4U_STANDBY_TIME 1800
  82. #define WIRING_INFO_DELAY 30
  83. #define WAIT_REASSIGN_TIME 40 // unit: second
  84. #define MAX_REQUEST_CHARGING_TIME 15 // unit: second
  85. #define MIN_WAIT_PREPARNING_TIME 1 // unit: second
  86. #define FORCE_CHARGING_CMD_DELAY 3 // unit: second
  87. #define FORCE_CHARGING_WAIT_EVSE 10 // unit: second
  88. #define TRY_RESET_4G_WIFI_TIME 300 // unit: second
  89. #define FAST_RESET_4G_WIFI_TIME 60 // unit: second
  90. #define ENERAGY_INTERVAL 1 // unit: second
  91. #define FAULT_RELEASE_TIME 2 // unit: second
  92. #define MAINTAIN_RECOVERY_TIME 1 // unit: second
  93. #define PARALLEL_WAIT_TIME 3 // unit: second
  94. #define START_UPGRADE_TIME 60 // unit: second
  95. #define PCABINET_UPGRADE_TIME 300 // unit: second
  96. #define MCABINET_UPGRADE_TIME 60 // unit: second
  97. #define CONNECTION_TIME_OFFSET 5 // unit: second
  98. #define PLUG_IN_HOLD_TIME 1 // unit: second
  99. #define FINAL_COST_TIMEOUT 3 // unit: second
  100. #define FINAL_COST_UPDATE_TIME 10 // unit: second
  101. #define MAX_FINAL_COST_MISC_COUNT 3
  102. #define PARKING_STATUS_TIMEOUT 3 // unit: second
  103. #define OCCUPANCY_FEE_TIMEOUT 10 // unit: second
  104. #define MIN_AVAILABLE_MEMORY 200 // unit: MB
  105. #define AUTO_START_CHARGING "AutoStartCharging"
  106. #define CONTACTOR_AUTO_ON_OFF 0
  107. #define CONTACTOR_AUTO_ON_TIME 60
  108. #define CONTACTOR_AUTO_OFF_TIME 300
  109. #define WAIT_DISPENSER_STOP_TIMEOUT 3 // unit: second
  110. #define STARTTRANSATION_TIME 1
  111. #define STOP_TARTTRANSATION_DELAY 2
  112. #define WAIT_DIAGNOSTICS_CONFIRM 10 // unit: second
  113. #define WAIT_DIAGNOSTICS_TIMEOUT 120 // unit: second
  114. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  115. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  116. int whileLoopTime = 10000; // 10 ms
  117. int wtdFd = -1;
  118. byte _authorizeIndex = NO_DEFINE;
  119. BOOL _UpgradeNeedReboot = FALSE;
  120. bool _isBridgeModeConfig = false;
  121. #define GUN_DERATING_COUNT 2
  122. const int GunDeratingTemp[GUN_DERATING_COUNT] = {80, 85};
  123. const byte Group_to_Location_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  124. const byte Location_to_Group_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  125. const byte GroupParallelRelay_2_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  126. {
  127. {0, 1, 0, 0},
  128. {1, 0, 0, 0},
  129. {0, 0, 0, 0},
  130. {0, 0, 0, 0}
  131. };
  132. //Location: 1 2 3 4
  133. // Gun: 1 3 4 2
  134. //Parallel: 1 2 3
  135. //const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 1, 2};
  136. //const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 2, 3, 1};
  137. //const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  138. //{
  139. // {0, 3, 1, 2}, // group 0
  140. // {1, 0, 2, 3}, // group 1
  141. // {1, 3, 0, 2}, // group 2
  142. // {1, 3, 2, 0} // group 3
  143. //};
  144. //Location: 1 2 3 4
  145. // Gun: 1 4 3 2
  146. //Parallel: 1 2 3
  147. const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  148. const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  149. const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  150. {
  151. {0, 3, 2, 1}, // group 0
  152. {1, 0, 3, 2}, // group 1
  153. {1, 3, 0, 2}, // group 2
  154. {1, 3, 2, 0} // group 3
  155. };
  156. const byte GroupParallelRelay_4_Gun_6Parallel[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  157. {
  158. {0, 4, 6, 1}, // group 0
  159. {4, 0, 3, 5}, // group 1
  160. {6, 3, 0, 2}, // group 2
  161. {1, 5, 2, 0} // group 3
  162. };
  163. long long DiffTimebWithNow(struct timeb ST);
  164. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  165. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  166. void ChargingTerminalProcess(byte gunIndex);
  167. void ChkPrimaryStatus();
  168. void ChargingAlarmProcess(byte gunIndex);
  169. void InformOcppErrOccur(byte codeType);
  170. void SetOcppErrorCode(unsigned char _index, char *errCode);
  171. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode);
  172. void ReleaseAlarmCode(byte gunIndex);
  173. int DB_Open(sqlite3 *db);
  174. int DB_Insert_Record(sqlite3 *db, int gun_index);
  175. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  176. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  177. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value);
  178. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value);
  179. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index);
  180. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index);
  181. float Get_PowerConsumption(uint8_t gun_index);
  182. void Update_PowerConsumption(uint8_t gun_index);
  183. int DB_Insert_FinalCost(sqlite3 *db, uint8_t gun_index, int txId, char *description);
  184. int DB_Update_FinalCost_Status(sqlite3 *db, int txId, int status);
  185. int DB_Get_Undisposed_FinalCost(sqlite3 *db, int gunIndex, char *costString);
  186. int DB_Reboot_Record(sqlite3 *db);
  187. int DB_Insert_Ocmf(sqlite3 *db, int gun_index, char *txId, char *ocmfKey, char *ocmfInfo);
  188. int DB_Update_Ocmf_Status(sqlite3 *db, char *txId, int status);
  189. int DB_Get_Undisposed_Ocmf(sqlite3 *db, int *gunIndex, char *txId, char *ocmfKey, char *ocmfInfo);
  190. void InitialDHCP();
  191. void InitialDispenserDhcpServerConfig(void);
  192. void StartDispenserDhcpServer(void);
  193. int GetStartScheduleTime(unsigned char *time);
  194. void CheckConnectionTimeout(void);
  195. bool IsParallelCabinetIdle(void);
  196. bool IsStandbyEnable(void);
  197. bool IsGunIdle(int gunIndex, int idleType);
  198. bool IsDispenserIdle(int dispenserIndex);
  199. bool IsConnectorWholeIdle(void);
  200. void SetAcContactor(unsigned char OnOff);
  201. void UpdateErrorCodeToOcpp(byte index);
  202. bool CheckConnectorTypeStatus(void);
  203. void CreateWatchdog(void);
  204. void TryCloseWatchdog(void);
  205. void TryFeedWatchdog(void);
  206. bool IsReadyParalleling(void);
  207. void SetParallelingStart(void);
  208. void SetParallelingDone(void);
  209. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff);
  210. void SetParallelCabinetAcContactor(unsigned char OnOff);
  211. unsigned short GetTotalParallelCabinetPower(void);
  212. void MCabinetParallelingProcess(void);
  213. void SCabinetControllProcess(void);
  214. bool Is_WebServer_Alive(void);
  215. void Run_WebServer_Service(void);
  216. bool Get_Ocpp_ConnectionStatus(void);
  217. void Run_Ocpp_Service(void);
  218. bool Is_Ocpp_Task_Alive(void);
  219. void Kill_Ocpp_Service(void);
  220. void Run_MaintainOcpp_Service(void);
  221. bool Is_MaintainOcpp_Task_Alive(void);
  222. void Kill_MaintainOcpp_Service(void);
  223. long int Get_Ocpp_procDogTime(void);
  224. void Clean_Ocpp_procDogTime(void);
  225. bool Is_Ocpp_BootNotification(void);
  226. void Clean_Ocpp_BootNotification(void);
  227. bool Is_Ocpp_ResetReq(void);
  228. bool Chk_Ocpp_Reset_Type(char *type);
  229. void Clean_Ocpp_ResetReq(void);
  230. void Set_Ocpp_AuthorizeReq(void);
  231. bool Is_Ocpp_AuthorizeConf(void);
  232. bool Is_Ocpp_Authorize_Status(char *status);
  233. bool Is_Ocpp_UpdateFirmwareReq(void);
  234. void Clean_Ocpp_UpdateFirmwareReq(void);
  235. bool Is_Ocpp_UpdateFirmwareStatus(char *status);
  236. void Set_Ocpp_UpdateFirmwareStatus(char *status);
  237. void Clean_Ocpp_UpdateFirmwareStatus(char *status);
  238. void Set_Ocpp_FirmwareStatusNotificationReq(void);
  239. bool Is_Ocpp_StopOnInvalidId_Enable(void);
  240. bool Is_Ocpp_AuthorizeRemoteTxRequests(void);
  241. int Get_Ocpp_ConnectionTimeOut(void);
  242. void Get_Ocpp_DefaultPrice(char *defaultPrice);
  243. void Get_Ocpp_UserPrice(char * userId, char *userPrice);
  244. void Clean_Ocpp_UserPrice(void);
  245. void Get_Ocpp_RunningCost(int gun_index, char *cost);
  246. void Clean_Ocpp_RunningCost(int gun_index);
  247. void Get_Ocpp_FinalCost(int gun_index, char *cost);
  248. void Clean_Ocpp_FinalCost(int gun_index);
  249. int Get_Ocpp_FinalCostTxId(int gun_index);
  250. void Get_Ocpp_TimeOffset(char *timeOffset);
  251. void Get_Ocpp_PreAuthAmount(char *amount);
  252. void Get_Ocpp_PowerSharingServerIP(char *ip);
  253. void Get_Ocpp_Refunding(char *Refunding);
  254. void Clean_Ocpp_Refunding(void);
  255. bool Is_Ocpp_LcdOverideReq(int *gun, int *page);
  256. void Clean_Ocpp_LcdOverideReq(void);
  257. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name);
  258. bool Is_Ocpp_ChargerStationReq(void);
  259. void Clean_Ocpp_ChargerStationReq(void);
  260. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature);
  261. bool Is_Ocpp_WeatherReq(void);
  262. void Clean_Ocpp_WeatherReq(void);
  263. void Set_Ocpp_DeductTransaction(int transaction);
  264. void Set_Ocpp_DeductCreditNo(char *userId);
  265. void Set_Ocpp_DeductVemData(char *vemData);
  266. void Set_Ocpp_DeductAmount(double value);
  267. void Set_Ocpp_DeductResult(unsigned char result);
  268. void Set_Ocpp_DonateInvoice(unsigned char donate);
  269. bool Is_Ocpp_ReportCreditDeductReq(void);
  270. void Set_Ocpp_ReportCreditDeductReq(void);
  271. void Clean_Ocpp_TcciSerialNo(int gun_index);
  272. bool Set_Ocpp_TcciReaderStatus(int gun_index, DeductResultInfoData *ReaderStatus);
  273. void Clean_Ocpp_TcciCreditInfo(int gun_index);
  274. void Set_Ocpp_TcciReaderStatusReq(int gun_index);
  275. bool Is_Ocpp_TcciReaderStatusReq(int gun_index);
  276. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id);
  277. void Set_Ocpp_StartTransactionReq(int gun_index);
  278. bool Is_Ocpp_StartTransactionConf(int gun_index);
  279. void Clean_Ocpp_StartTransactionConf(int gun_index);
  280. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status);
  281. bool Is_Ocpp_StartTransaction_Accepted(int gun_index);
  282. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status);
  283. void Get_Ocpp_TransactionId(int gun_index, char *transactionId);
  284. bool Is_Invalid_StartTransactionId(int gun_index);
  285. void Set_Ocpp_StopTransactionId(int gun_index, char *id);
  286. void Set_Ocpp_StopTransactionReq(int gun_index);
  287. void Chk_Ocpp_Backup_ErrorCode(int gun_index);
  288. bool Is_Ocpp_ReserveNowReq(int gun_index);
  289. void Clean_Ocpp_ReserveNowReq(int gun_index);
  290. int Get_Ocpp_ReserveNowId(int gun_index);
  291. void Set_Ocpp_ReserveNowConf(int gun_index);
  292. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date);
  293. bool Is_Ocpp_ReserveNowExpired(int gun_index);
  294. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag);
  295. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag);
  296. bool Is_Ocpp_CancelReservationReq(int gun_index);
  297. void Clean_Ocpp_CancelReservationReq(int gun_index);
  298. void Set_Ocpp_CancelReservationConf(int gun_index);
  299. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index);
  300. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index);
  301. bool Chk_Ocpp_Availability_Type(int gun_index, char *type);
  302. bool Is_Ocpp_UnlockConnectorReq(int gun_index);
  303. void Clean_Ocpp_UnlockConnectorReq(int gun_index);
  304. void Set_Ocpp_UnlockConnectorConf(int gun_index);
  305. bool Is_Ocpp_RemoteStartReq(int gun_index);
  306. void Clean_Ocpp_RemoteStartReq(int gun_index);
  307. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag);
  308. bool Is_Ocpp_RemoteStopReq(int gun_index);
  309. void Clean_Ocpp_RemoteStopReq(int gun_index);
  310. void Set_Ocpp_StopReason(int gun_index, char *reason);
  311. void Get_Ocpp_StopReason(int gun_index, char *reason);
  312. void Clean_Ocpp_StopReason(int gun_index);
  313. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  314. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  315. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  316. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  317. bool Is_Ocpp_ChargingProfileConf(int gun_index);
  318. void Clean_Ocpp_ChargingProfileConf(int gun_index);
  319. bool Is_Ocpp_ChargingProfileReq(int gun_index);
  320. void Set_Ocpp_ChargingProfileReq(int gun_index);
  321. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind);
  322. bool Is_Ocpp_ChargingProfileId(int gun_index);
  323. int Get_Ocpp_ChargingProfileStartTime(int gun_index);
  324. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index);
  325. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index);
  326. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index);
  327. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index);
  328. int Get_Ocpp_ChargingProfileStartIndex(int gun_index);
  329. bool Is_Ocpp_MaxChargingProfileKind(char *kind);
  330. bool Is_Ocpp_MaxChargingProfileId(void);
  331. int Get_Ocpp_MaxProfileStartTime(void);
  332. int Get_Ocpp_MaxProfileScheduleCount(void);
  333. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index);
  334. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index);
  335. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index);
  336. int Get_Ocpp_MaxProfileStartIndex(void);
  337. int Is_Ocpp_RemoteStartWaitReq(void);
  338. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget);
  339. int Get_Ocpp_OffLineMaxChargingPower(void);
  340. bool Is_Ocpp_SendOcmfDataReq(int gunIndex);
  341. void Set_Ocpp_SendOcmfDataReq(int gunIndex);
  342. void Clean_Ocpp_SendOcmfDataReq(int gunIndex);
  343. void Set_Ocpp_OcmfData(int gunIndex, char *key, char *ocmfInfo);
  344. void Set_Deduct_UserId(int gunIndex, char *userId);
  345. void Set_Deduct_TransactionId(int gunIndex, int transaction);
  346. void Clean_DeductInfo(int gunIndex);
  347. bool Is_DeductResultReq(int gunIndex);
  348. bool Is_ReaderStatusReq(int gunIndex);
  349. void Clean_ReaderStatusInfo(int gunIndex);
  350. void Set_UserTransactionId(int gunIndex, int transaction);
  351. void Set_UserPrice(int gunIndex, float price);
  352. void Set_UserTotalCost(int gunIndex, float cost);
  353. void Set_UserAccountBalance(int gunIndex, float balance);
  354. void Set_UserDiscount(int gunIndex, float discount);
  355. void Clean_UserTransaction(int gunIndex);
  356. bool Is_OccupancyDeductReq(int gunIndex);
  357. void Clean_OccupancyDeductReq(int gunIndex);
  358. bool Is_OccupancyKeepCountingReq(int gunIndex);
  359. void Set_OccupancyKeepCountingReq(int gunIndex);
  360. void Clean_OccupancyKeepCountingReq(int gunIndex);
  361. bool Is_OccupancyFeeForceCancelReq(int gunIndex);
  362. void Set_OccupancyFeeForceCancelReq(int gunIndex);
  363. void Clean_OccupancyFeeForceCancelReq(int gunIndex);
  364. bool Is_Ocpp_GetDiagnosticsReq(void);
  365. void Clean_Ocpp_GetDiagnosticsReq(void);
  366. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  367. struct StatusCodeData *ShmStatusCodeData;
  368. struct PsuData *ShmPsuData;
  369. struct CHAdeMOData *ShmCHAdeMOData;
  370. struct GBTData *ShmGBTData;
  371. struct CcsData *ShmCcsData;
  372. struct PrimaryMcuData *ShmPrimaryMcuData;
  373. struct FanModuleData *ShmFanModuleData;
  374. struct RelayModuleData *ShmRelayModuleData[2];
  375. struct LedModuleData *ShmLedModuleData;
  376. struct OCPP16Data *ShmOCPP16Data;
  377. struct OCPP20Data *ShmOCPP20Data;
  378. struct OCPP16Data *ShmOCPP16DataPH;
  379. struct MeterInformation *ShmCsuMeterData;
  380. ChargerInfoData *ShmChargerInfo;
  381. PsuPositionInfoData *ShmPsuPosition;
  382. PsuGroupingInfoData *ShmPsuGrouping;
  383. struct ChargingInfoData *chargingInfo[CONNECTOR_QUANTITY];
  384. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  385. struct timespec _cmdMainPriority_time;
  386. struct timespec _cmdSubPriority_time;
  387. struct timespec _ConnectorAuthorizing_Time[CONNECTOR_QUANTITY];
  388. struct timespec _ConnectorAuthorizeFail_Time[CONNECTOR_QUANTITY];
  389. struct timespec _DispenserAuthorizing_Time[CONNECTOR_QUANTITY];
  390. struct timespec _DispenserUpgrade_time;
  391. struct timespec _Standby_time;
  392. struct timespec _SystemStatus_Time[CONNECTOR_QUANTITY];
  393. struct timespec _ChargedEnergy_Time[CONNECTOR_QUANTITY];
  394. struct timespec _Parallel_Time;
  395. struct timespec _RemoteStartNoId_Time;
  396. float gunOutputVol[CONNECTOR_QUANTITY];
  397. unsigned char _ocppProfileChkFlag[CONNECTOR_QUANTITY];
  398. float _lastProfilePower[CONNECTOR_QUANTITY];
  399. float _lastProfileCurrent[CONNECTOR_QUANTITY];
  400. float _lastMaxProfilePower;
  401. float _lsatGunMeterValue[CONNECTOR_QUANTITY];
  402. struct StructStatusNotification _BackupStatusNotification[CONNECTOR_QUANTITY];
  403. bool _useGunMeterValue[MAX_GUN_QUANTITY];
  404. bool _triggerTransaction[MAX_GUN_QUANTITY];
  405. bool _plugInHold[MAX_GUN_QUANTITY];
  406. struct timespec _PlugInHold_Time[MAX_GUN_QUANTITY];
  407. struct timespec _DispenserBill_Time[MAX_DISPENSER_QUANTITY];
  408. struct timespec _ConnectorBill_Time[MAX_GUN_QUANTITY];
  409. struct timespec _ParkingStatus_Time[MAX_GUN_QUANTITY];
  410. struct timespec _GetOccupancyFee_Time[MAX_GUN_QUANTITY];
  411. struct timespec _Diagnostics_Time;
  412. bool _canUpdateFinalCost[MAX_DISPENSER_QUANTITY];
  413. bool _isUndisposedFinalCost[MAX_DISPENSER_QUANTITY];
  414. int _notifyOccupancyFeeAct[MAX_GUN_QUANTITY];
  415. bool _NeedReset4gWifi;
  416. struct timespec _4gWifiReset_time;
  417. int _psuLostQuantity = 0;
  418. bool _isStandbyEnable = true;
  419. bool isModelNameMatch = true;
  420. bool _NeedDiagnostics = false;
  421. //int rfidFd = -1;
  422. //char* rfidPortName = "/dev/ttyS2";
  423. #if ENABLE_PCBA_TEST == 0
  424. char* fwVersion = "V2.05.00.0000.00";
  425. char* subVersion = "03";
  426. #else
  427. char* fwVersion = "PCBA.00.04";
  428. char* subVersion = "00";
  429. #endif
  430. sqlite3 *localDb;
  431. bool isDb_ready;
  432. sqlite3 *networkDb;
  433. //================================================
  434. // initial can-bus
  435. //================================================
  436. int InitCanBus()
  437. {
  438. int s0,nbytes;
  439. struct timeval tv;
  440. struct ifreq ifr0;
  441. struct sockaddr_can addr0;
  442. system("/sbin/ip link set can0 down");
  443. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  444. system("/sbin/ip link set can0 up");
  445. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  446. tv.tv_sec = 0;
  447. tv.tv_usec = 10000;
  448. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  449. {
  450. #ifdef SystemLogMessage
  451. LOG_ERROR("Set SO_RCVTIMEO NG");
  452. #endif
  453. }
  454. nbytes=40960;
  455. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  456. {
  457. #ifdef SystemLogMessage
  458. LOG_ERROR("Set SO_RCVBUF NG");
  459. #endif
  460. }
  461. nbytes=40960;
  462. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  463. {
  464. #ifdef SystemLogMessage
  465. LOG_ERROR("Set SO_SNDBUF NG");
  466. #endif
  467. }
  468. strcpy(ifr0.ifr_name, "can0" );
  469. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  470. addr0.can_family = AF_CAN;
  471. addr0.can_ifindex = ifr0.ifr_ifindex;
  472. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  473. return s0;
  474. }
  475. //================================================
  476. // initial uart port
  477. //================================================
  478. char *_priPortName = "/dev/ttyS1";
  479. char *_485PortName = "/dev/ttyS5";
  480. int InitComPort(byte target)
  481. {
  482. int fd;
  483. struct termios tios;
  484. if(target == UPGRADE_PRI)
  485. fd = open(_priPortName, O_RDWR);
  486. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  487. fd = open(_485PortName, O_RDWR);
  488. if(fd<=0)
  489. {
  490. #ifdef SystemLogMessage
  491. LOG_ERROR("open 407 Communication port NG");
  492. #endif
  493. return -1;
  494. }
  495. ioctl (fd, TCGETS, &tios);
  496. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  497. tios.c_lflag = 0;
  498. tios.c_iflag = 0;
  499. tios.c_oflag = 0;
  500. tios.c_cc[VMIN]=0;
  501. tios.c_cc[VTIME]=(unsigned char)1;
  502. tios.c_lflag=0;
  503. tcflush(fd, TCIFLUSH);
  504. ioctl (fd, TCSETS, &tios);
  505. return fd;
  506. }
  507. //=================================
  508. // Common routine
  509. //=================================
  510. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  511. {
  512. strncpy(dest, src + start, cnt);
  513. dest[cnt] = 0;
  514. }
  515. int InitWatchDog()
  516. {
  517. int fd;
  518. int timeout = 180;
  519. system("/usr/bin/fuser -k /dev/watchdog");
  520. sleep(1);
  521. system("echo V > /dev/watchdog");
  522. sleep(1);
  523. fd=open("/dev/watchdog", O_RDWR);
  524. if(fd<=0)
  525. {
  526. LOG_ERROR("System watch dog initial fail.\r\n");
  527. }
  528. ioctl(fd, _IOWR('W', 6, int), &timeout);
  529. return fd;
  530. }
  531. void setChargerMode(byte gun_index, byte mode)
  532. {
  533. chargingInfo[gun_index]->SystemStatus = mode;
  534. }
  535. long long DiffTimebWithNow(struct timeb ST)
  536. {
  537. //return milli-second
  538. struct timeb ET;
  539. long long StartTime,StopTime;
  540. ftime(&ET);
  541. StartTime=(long long)ST.time;
  542. StopTime=(long long)ET.time;
  543. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  544. }
  545. //=================================
  546. // Create all share memory
  547. //=================================
  548. int CreateShareMemory()
  549. {
  550. int MeterSMId;
  551. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  552. {
  553. #ifdef SystemLogMessage
  554. LOG_ERROR("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG");
  555. #endif
  556. return 0;
  557. }
  558. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  559. {
  560. #ifdef SystemLogMessage
  561. LOG_ERROR("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG");
  562. #endif
  563. return 0;
  564. }
  565. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  566. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  567. {
  568. #ifdef SystemLogMessage
  569. LOG_ERROR("[main]CreatShareMemory:shmget ShmStatusCodeData NG");
  570. #endif
  571. return 0;
  572. }
  573. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  574. {
  575. #ifdef SystemLogMessage
  576. LOG_ERROR("[main]CreatShareMemory:shmat ShmStatusCodeData NG");
  577. #endif
  578. return 0;
  579. }
  580. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  581. //creat ShmPsuData
  582. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  583. {
  584. #ifdef SystemLogMessage
  585. LOG_ERROR("[main]CreatShareMemory:shmget ShmPsuData NG");
  586. #endif
  587. return 0;
  588. }
  589. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  590. {
  591. #ifdef SystemLogMessage
  592. LOG_ERROR("[main]CreatShareMemory:shmat ShmPsuData NG");
  593. #endif
  594. return 0;
  595. }
  596. memset(ShmPsuData, 0, sizeof(struct PsuData));
  597. /*
  598. if(CHAdeMO_QUANTITY > 0)
  599. {
  600. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  601. {
  602. #ifdef SystemLogMessage
  603. LOG_ERROR("[main]CreatShareMemory:shmget ShmCHAdeMOData NG");
  604. #endif
  605. return 0;
  606. }
  607. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  608. {
  609. #ifdef SystemLogMessage
  610. LOG_ERROR("[main]CreatShareMemory:shmat ShmCHAdeMOData NG");
  611. #endif
  612. return 0;
  613. }
  614. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  615. }
  616. if(GB_QUANTITY > 0)
  617. {
  618. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  619. {
  620. #ifdef SystemLogMessage
  621. LOG_ERROR("[main]CreatShareMemory:shmget ShmGBTData NG");
  622. #endif
  623. return 0;
  624. }
  625. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  626. {
  627. #ifdef SystemLogMessage
  628. LOG_ERROR("[main]CreatShareMemory:shmat ShmGBTData NG");
  629. #endif
  630. return 0;
  631. }
  632. memset(ShmGBTData, 0, sizeof(struct GBTData));
  633. }
  634. //creat ShmCcsData
  635. if(CCS_QUANTITY > 0)
  636. {
  637. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  638. {
  639. #ifdef SystemLogMessage
  640. LOG_ERROR("[main]CreatShareMemory:shmget ShmCcsData NG");
  641. #endif
  642. return 0;
  643. }
  644. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  645. {
  646. #ifdef SystemLogMessage
  647. LOG_ERROR("[main]CreatShareMemory:shmat ShmCcsData NG");
  648. #endif
  649. return 0;
  650. }
  651. memset(ShmCcsData, 0, sizeof(struct CcsData));
  652. }
  653. */
  654. //creat ShmPrimaryMcuData
  655. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  656. {
  657. #ifdef SystemLogMessage
  658. LOG_ERROR("[main]CreatShareMemory:shmget ShmPrimaryMcuData NGn");
  659. #endif
  660. return 0;
  661. }
  662. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  663. {
  664. #ifdef SystemLogMessage
  665. LOG_ERROR("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG");
  666. #endif
  667. return 0;
  668. }
  669. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  670. //creat ShmFanModuleData
  671. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  672. {
  673. #ifdef SystemLogMessage
  674. LOG_ERROR("[main]CreatShareMemory:shmget ShmFanModuleData NG");
  675. #endif
  676. return 0;
  677. }
  678. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  679. {
  680. #ifdef SystemLogMessage
  681. LOG_ERROR("[main]CreatShareMemory:shmat ShmFanModuleData NG");
  682. #endif
  683. return 0;
  684. }
  685. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  686. //creat ShmRelayModuleData
  687. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  688. {
  689. #ifdef SystemLogMessage
  690. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[0] NG");
  691. #endif
  692. return 0;
  693. }
  694. else if ((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  695. {
  696. #ifdef SystemLogMessage
  697. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[0] NG");
  698. #endif
  699. return 0;
  700. }
  701. memset(ShmRelayModuleData[0], 0, sizeof(struct RelayModuleData));
  702. // DO360 RC2
  703. if ((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  704. {
  705. #ifdef SystemLogMessage
  706. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[1] NG");
  707. #endif
  708. return 0;
  709. }
  710. else if ((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  711. {
  712. #ifdef SystemLogMessage
  713. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[1] NG");
  714. #endif
  715. return 0;
  716. }
  717. memset(ShmRelayModuleData[1], 0, sizeof(struct RelayModuleData));
  718. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  719. {
  720. #ifdef SystemLogMessage
  721. LOG_ERROR("[main]CreatShareMemory:shmget ShmLedModuleData NG");
  722. #endif
  723. return 0;
  724. }
  725. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  726. {
  727. #ifdef SystemLogMessage
  728. LOG_ERROR("[main]CreatShareMemory:shmat ShmLedModuleData NG");
  729. #endif
  730. return 0;
  731. }
  732. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  733. //creat ShmOCPP16Data
  734. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  735. {
  736. #ifdef SystemLogMessage
  737. LOG_ERROR("[main]CreatShareMemory:shmget ShmOCPP16Data NG");
  738. #endif
  739. return 0;
  740. }
  741. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  742. {
  743. #ifdef SystemLogMessage
  744. LOG_ERROR("[main]CreatShareMemory:shmat ShmOCPP16Data NG");
  745. #endif
  746. return 0;
  747. }
  748. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  749. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  750. {
  751. LOG_ERROR("[main]CreatShareMemory:shmget OCPP20Data NG");
  752. return 0;
  753. }
  754. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  755. {
  756. LOG_ERROR("[main]CreatShareMemory:shmat OCPP20Data NG");
  757. return 0;
  758. }
  759. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  760. //creat ShmOCPP16DataPH
  761. if ((MeterSMId = shmget(ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  762. {
  763. LOG_ERROR("shmget ShmOCPP16DataPH NG");
  764. return 0;
  765. }
  766. else if ((ShmOCPP16DataPH = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  767. {
  768. LOG_ERROR("shmat ShmOCPP16DataPH NG");
  769. return 0;
  770. }
  771. memset(ShmOCPP16DataPH,0,sizeof(struct OCPP16Data));
  772. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), IPC_CREAT | 0777)) < 0)
  773. {
  774. #ifdef SystemLogMessage
  775. LOG_ERROR("[main]CreatShareMemory:shmget ChargerInfoData NG");
  776. #endif
  777. return 0;
  778. }
  779. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  780. {
  781. #ifdef SystemLogMessage
  782. LOG_ERROR("[main]CreatShareMemory:shmat ChargerInfoData NG");
  783. #endif
  784. return 0;
  785. }
  786. memset(ShmChargerInfo, 0, sizeof(ChargerInfoData));
  787. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  788. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  789. return 1;
  790. }
  791. //======================================================
  792. // Peripheral initial
  793. //======================================================
  794. void InitGPIO()
  795. {
  796. /*****************0~3, 4 bank, bank x 32+ num*********************/
  797. /***************************************************************/
  798. /*************** GPIO 0 ***************************************/
  799. /***************************************************************/
  800. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/ // no use
  801. system("echo 22 > /sys/class/gpio/export");
  802. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  803. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/ // no use
  804. system("echo 23 > /sys/class/gpio/export");
  805. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  806. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/ // no use
  807. system("echo 26 > /sys/class/gpio/export");
  808. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  809. system("echo 1 > /sys/class/gpio/gpio26/value");
  810. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/ // no use
  811. system("echo 27 > /sys/class/gpio/export");
  812. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  813. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  814. system("echo 29 > /sys/class/gpio/export");
  815. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  816. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/ // no use
  817. system("echo 19 > /sys/class/gpio/export");
  818. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  819. system("echo 1 > /sys/class/gpio/gpio19/value");
  820. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/ // no use
  821. system("echo 20 > /sys/class/gpio/export");
  822. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  823. /***************************************************************/
  824. /*************** GPIO 1 ***************************************/
  825. /***************************************************************/
  826. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/ // no use
  827. system("echo 44 > /sys/class/gpio/export");
  828. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  829. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/ // no use
  830. system("echo 45 > /sys/class/gpio/export");
  831. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  832. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/ // no use
  833. system("echo 46 > /sys/class/gpio/export");
  834. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  835. system("echo 0 > /sys/class/gpio/gpio46/value");
  836. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/ // no use
  837. system("echo 47 > /sys/class/gpio/export");
  838. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  839. /***************************************************************/
  840. /*************** GPIO 2 ***************************************/
  841. /***************************************************************/
  842. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  843. system("echo 89 > /sys/class/gpio/export");
  844. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  845. system("echo 1 > /sys/class/gpio/gpio89/value");
  846. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  847. system("echo 87 > /sys/class/gpio/export");
  848. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  849. system("echo 0 > /sys/class/gpio/gpio87/value");
  850. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/ // no use
  851. system("echo 88 > /sys/class/gpio/export");
  852. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  853. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/ // no use
  854. system("echo 86 > /sys/class/gpio/export");
  855. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  856. /***************************************************************/
  857. /*************** GPIO 3 ***************************************/
  858. /***************************************************************/
  859. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  860. system("echo 111 > /sys/class/gpio/export");
  861. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  862. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  863. system("echo 114 > /sys/class/gpio/export");
  864. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  865. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  866. system("echo 113 > /sys/class/gpio/export");
  867. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  868. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  869. system("echo 110 > /sys/class/gpio/export");
  870. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  871. system("echo 0 > /sys/class/gpio/gpio110/value");
  872. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/ // no use
  873. system("echo 115 > /sys/class/gpio/export");
  874. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  875. system("echo 0 > /sys/class/gpio/gpio115/value");
  876. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  877. system("echo 112 > /sys/class/gpio/export");
  878. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  879. system("echo 1 > /sys/class/gpio/gpio112/value");
  880. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/ // no use
  881. system("echo 116 > /sys/class/gpio/export");
  882. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  883. system("echo 0 > /sys/class/gpio/gpio116/value");
  884. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  885. system("echo 103 > /sys/class/gpio/export");
  886. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  887. system("echo 0 > /sys/class/gpio/gpio103/value");
  888. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  889. system("echo 104 > /sys/class/gpio/export");
  890. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  891. system("echo 0 > /sys/class/gpio/gpio104/value");
  892. LOG_INFO("[main]InitGPIO: Initial GPIO OK");
  893. }
  894. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  895. {
  896. int fd, wrd;
  897. unsigned char *buf;
  898. unsigned int ChkSum, ChkSumOrg;
  899. if((buf = malloc(MtdBlockSize)) == NULL)
  900. {
  901. LOG_ERROR("malloc buffer NG, rebooting..\n");
  902. if(ShmStatusCodeData!=NULL)
  903. {
  904. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  905. }
  906. sleep(5);
  907. system("reboot -f");
  908. sleep(5);
  909. system("reboot -f");
  910. }
  911. memset(buf, 0, MtdBlockSize);
  912. //================================================
  913. // Load configuration from mtdblock10
  914. //================================================
  915. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  916. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  917. if(fd < 0)
  918. {
  919. free(buf);
  920. LOG_ERROR("open mtdblock10 NG, rebooting..\n");
  921. if(ShmStatusCodeData != NULL)
  922. {
  923. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  924. }
  925. sleep(5);
  926. system("reboot -f");
  927. sleep(5);
  928. system("reboot -f");
  929. }
  930. wrd = read(fd, buf, MtdBlockSize);
  931. close(fd);
  932. if(wrd < MtdBlockSize)
  933. {
  934. free(buf);
  935. LOG_ERROR("read SysConfigData data NG, rebooting..\n");
  936. if(ShmStatusCodeData != NULL)
  937. {
  938. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  939. }
  940. sleep(5);
  941. system("reboot -f");
  942. sleep(5);
  943. system("reboot -f");
  944. }
  945. ChkSum = 0;
  946. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  947. {
  948. ChkSum += buf[wrd];
  949. }
  950. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  951. memcpy(&ptr->ModelName, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), ARRAY_SIZE(ptr->ModelName));
  952. memcpy(&ptr->SerialNumber, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), ARRAY_SIZE(ptr->SerialNumber));
  953. //================================================
  954. // Load configuration from mtdblock11
  955. //================================================
  956. if(ChkSum != ChkSumOrg)
  957. {
  958. LOG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  959. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  960. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  961. if(fd < 0)
  962. {
  963. free(buf);
  964. LOG_ERROR("open mtdblock11 (backup) NG, rebooting..\n");
  965. if(ShmStatusCodeData != NULL)
  966. {
  967. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  968. }
  969. sleep(5);
  970. system("reboot -f");
  971. sleep(5);
  972. system("reboot -f");
  973. }
  974. memset(buf, 0, MtdBlockSize);
  975. wrd = read(fd, buf, MtdBlockSize);
  976. close(fd);
  977. if(wrd < MtdBlockSize)
  978. {
  979. free(buf);
  980. LOG_ERROR("read backup SysConfigData data NG, rebooting..\n");
  981. if(ShmStatusCodeData != NULL)
  982. {
  983. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  984. }
  985. sleep(5);
  986. system("reboot -f");
  987. sleep(5);
  988. system("reboot -f");
  989. }
  990. ChkSum = 0;
  991. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  992. {
  993. ChkSum += buf[wrd];
  994. }
  995. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  996. //================================================
  997. // Load configuration from mtdblock12 (Factory default)
  998. //================================================
  999. if(ChkSum != ChkSumOrg)
  1000. {
  1001. LOG_ERROR("backup SysConfigData checksum NG, read Factory default\n");
  1002. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1003. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1004. if(fd < 0)
  1005. {
  1006. free(buf);
  1007. LOG_ERROR("open mtdblock12 (Factory default) NG, rebooting..\n");
  1008. if(ShmStatusCodeData != NULL)
  1009. {
  1010. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1011. }
  1012. sleep(5);
  1013. system("reboot -f");
  1014. sleep(5);
  1015. system("reboot -f");
  1016. }
  1017. memset(buf, 0, MtdBlockSize);
  1018. wrd = read(fd, buf, MtdBlockSize);
  1019. close(fd);
  1020. if(wrd < MtdBlockSize)
  1021. {
  1022. free(buf);
  1023. LOG_ERROR("read factory default SysConfigData data NG, rebooting..\n");
  1024. if(ShmStatusCodeData != NULL)
  1025. {
  1026. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1027. }
  1028. sleep(5);
  1029. system("reboot -f");
  1030. sleep(5);
  1031. system("reboot -f");
  1032. }
  1033. ChkSum = 0;
  1034. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  1035. {
  1036. ChkSum += buf[wrd];
  1037. }
  1038. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  1039. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1040. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1041. if(ChkSum != ChkSumOrg)
  1042. {
  1043. LOG_ERROR("factory default SysConfigData checksum NG, restore factory default\n");
  1044. free(buf);
  1045. system("cd /root;./FactoryConfig -m");
  1046. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1047. system("sync");
  1048. sleep(5);
  1049. system("reboot -f");
  1050. sleep(5);
  1051. system("reboot -f");
  1052. return FAIL;
  1053. }
  1054. }
  1055. }
  1056. //load OK
  1057. memcpy((struct SysConfigData *)ptr, buf, sizeof(struct SysConfigData));
  1058. free(buf);
  1059. system("rm -f /mnt/EvseConfig.bin");
  1060. // SysConfig in flash is empty (0xffffffff)
  1061. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1062. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1063. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1064. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1065. {
  1066. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1067. {
  1068. LOG_INFO("Model name over length.");
  1069. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1070. }
  1071. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1072. {
  1073. LOG_INFO("Model serial number over length.");
  1074. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1075. }
  1076. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1077. {
  1078. LOG_INFO("System id over length.");
  1079. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1080. }
  1081. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1082. {
  1083. LOG_INFO("Ethernet dhcp config is null.");
  1084. }
  1085. system("cd /root;./Module_FactoryConfig -m");
  1086. sleep(3);
  1087. system("/usr/bin/run_evse_restart.sh");
  1088. }
  1089. LOG_INFO("Load SysConfigData OK");
  1090. return PASS;
  1091. }
  1092. int isEthernetCablePlugIn(int ethernet)
  1093. {
  1094. int fd, PlugInStatus = 0;
  1095. char ch[2];
  1096. char buffer[64];
  1097. snprintf(buffer, sizeof(buffer), "/sys/class/net/eth%d/carrier", ethernet);
  1098. fd = open(buffer, O_RDONLY);
  1099. if(fd < 0)
  1100. {
  1101. return -1;
  1102. }
  1103. if(read(fd, ch, 1) < 0)
  1104. {
  1105. return -1;
  1106. }
  1107. PlugInStatus = atoi(ch);
  1108. close(fd);
  1109. return PlugInStatus;
  1110. }
  1111. int isRouteFail()
  1112. {
  1113. int result = YES;
  1114. FILE *fp;
  1115. char buf[512];
  1116. fp = popen("route -n", "r");
  1117. if(fp != NULL)
  1118. {
  1119. while(fgets(buf, sizeof(buf), fp) != NULL)
  1120. {
  1121. if(strstr(buf, "eth0") != NULL)
  1122. result = NO;
  1123. }
  1124. }
  1125. pclose(fp);
  1126. return result;
  1127. }
  1128. int isReachableInternet()
  1129. {
  1130. int result = FAIL;
  1131. FILE *fp;
  1132. char cmd[256];
  1133. char buf[512];
  1134. char tmp[512];
  1135. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1136. // {
  1137. // result = PASS;
  1138. // }
  1139. // else
  1140. {
  1141. strcpy(cmd, "ifconfig eth0");
  1142. fp = popen(cmd, "r");
  1143. if (fp != NULL)
  1144. {
  1145. while(fgets(buf, sizeof(buf), fp) != NULL)
  1146. {
  1147. if (strstr(buf, "inet addr:") > 0)
  1148. {
  1149. sscanf(buf, "%*s%s", tmp);
  1150. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
  1151. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  1152. {
  1153. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  1154. }
  1155. }
  1156. }
  1157. }
  1158. pclose(fp);
  1159. memset(buf, 0x00, sizeof(buf));
  1160. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1161. {
  1162. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1163. fp = popen(cmd, "r");
  1164. if(fp != NULL)
  1165. {
  1166. while(fgets(buf, sizeof(buf), fp) != NULL)
  1167. {
  1168. if(strstr(buf, "transmitted") > 0)
  1169. {
  1170. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1171. if(strstr(buf,"100%") != NULL)
  1172. {
  1173. }
  1174. else
  1175. {
  1176. result = PASS;
  1177. }
  1178. //DEBUG_INFO("%s",buf);
  1179. //DEBUG_INFO("%s\n",tmp);
  1180. }
  1181. }
  1182. }
  1183. pclose(fp);
  1184. }
  1185. }
  1186. return result;
  1187. }
  1188. int isUap0up(void)
  1189. {
  1190. int result = FAIL;
  1191. FILE *fp;
  1192. char cmd[256];
  1193. char buf[512];
  1194. strcpy(cmd, "ifconfig uap0");;
  1195. fp = popen(cmd, "r");
  1196. if(fp != NULL)
  1197. {
  1198. while(fgets(buf, sizeof(buf), fp) != NULL)
  1199. {
  1200. if(strstr(buf, "uap0") > 0)
  1201. {
  1202. result = PASS;
  1203. }
  1204. }
  1205. }
  1206. pclose(fp);
  1207. return result;
  1208. }
  1209. void InitialApModeDhcpServerConfig(void)
  1210. {
  1211. system("echo 'start 192.168.10.100' > /etc/udhcpd.conf");
  1212. system("echo 'end 192.168.10.150' >> /etc/udhcpd.conf");
  1213. system("echo 'interface uap0' >> /etc/udhcpd.conf");
  1214. system("echo 'lease_file /var/log/udhcpd.leases'>> /etc/udhcpd.conf");
  1215. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd.conf");
  1216. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd.conf");
  1217. system("echo 'opt router 192.168.10.10' >> /etc/udhcpd.conf");
  1218. system("echo 'option domain local' >> /etc/udhcpd.conf");
  1219. system("echo 'option lease 86400' >> /etc/udhcpd.conf");
  1220. usleep(1000);
  1221. }
  1222. void InitialBridgeModeDhcpServerConfig(void)
  1223. {
  1224. system("echo 'start 192.168.10.100' > /etc/udhcpd.conf");
  1225. system("echo 'end 192.168.10.150' >> /etc/udhcpd.conf");
  1226. system("echo 'interface br0' >> /etc/udhcpd.conf");
  1227. system("echo 'lease_file /var/log/udhcpd.leases'>> /etc/udhcpd.conf");
  1228. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd.conf");
  1229. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd.conf");
  1230. system("echo 'opt router 192.168.10.10' >> /etc/udhcpd.conf");
  1231. system("echo 'option domain local' >> /etc/udhcpd.conf");
  1232. system("echo 'option lease 86400' >> /etc/udhcpd.conf");
  1233. usleep(1000);
  1234. }
  1235. bool Is_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1236. {
  1237. return (ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue & misc_cmd) > 0 ? true : false;
  1238. }
  1239. void Set_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1240. {
  1241. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue |= misc_cmd;
  1242. }
  1243. void Clean_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1244. {
  1245. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue &= ~misc_cmd;
  1246. }
  1247. bool Is_Connector_MiscCommand(int connector, int misc_cmd)
  1248. {
  1249. return (ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue & misc_cmd) > 0 ? true : false;
  1250. }
  1251. void Set_Connector_MiscCommand(int connector, int misc_cmd)
  1252. {
  1253. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue |= misc_cmd;
  1254. }
  1255. void Clean_Connector_MiscCommand(int connector, int misc_cmd)
  1256. {
  1257. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue &= ~misc_cmd;
  1258. }
  1259. void AnnounceEthernetStatus(void)
  1260. {
  1261. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1262. {
  1263. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1264. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1265. {
  1266. Set_Dispenser_MiscCommand(index, MISC_DISP_ETHERNET_STATUS);
  1267. }
  1268. }
  1269. }
  1270. void AnnounceWiFiStatus(void)
  1271. {
  1272. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1273. {
  1274. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1275. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1276. {
  1277. Set_Dispenser_MiscCommand(index, MISC_DISP_WIFI_STATUS);
  1278. }
  1279. }
  1280. }
  1281. void AnnounceTelcomModemStatus(void)
  1282. {
  1283. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1284. {
  1285. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1286. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1287. {
  1288. Set_Dispenser_MiscCommand(index, MISC_DISP_TELCOM_MODEM_STATUS);
  1289. }
  1290. }
  1291. }
  1292. void AnnounceBackendStatus(void)
  1293. {
  1294. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1295. {
  1296. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1297. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1298. {
  1299. Set_Dispenser_MiscCommand(index, MISC_DISP_BACKEND_STATUS);
  1300. }
  1301. }
  1302. }
  1303. void AnnounceBillingStatus(void)
  1304. {
  1305. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1306. {
  1307. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1308. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1309. {
  1310. Set_Dispenser_MiscCommand(index, MISC_DISP_BILLING_STATUS);
  1311. }
  1312. }
  1313. }
  1314. void AnnounceStopChargingButtonStatus(void)
  1315. {
  1316. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1317. {
  1318. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1319. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1320. {
  1321. Set_Dispenser_MiscCommand(index, MISC_DISP_STOP_BUTTON_STATUS);
  1322. }
  1323. }
  1324. }
  1325. void AnnounceAuthModeConfig(void)
  1326. {
  1327. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1328. {
  1329. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1330. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1331. {
  1332. Set_Dispenser_MiscCommand(index, MISC_DISP_AUTH_MODE_CONFIG);
  1333. }
  1334. }
  1335. }
  1336. void AnnounceEVCCIDConfig(void)
  1337. {
  1338. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1339. {
  1340. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1341. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1342. {
  1343. Set_Dispenser_MiscCommand(index, MISC_DISP_EVCCID_CONFIG);
  1344. }
  1345. }
  1346. }
  1347. void AnnounceLEDIntensity(void)
  1348. {
  1349. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1350. {
  1351. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1352. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1353. {
  1354. Set_Dispenser_MiscCommand(index, MISC_DISP_LED_INTENSITY);
  1355. }
  1356. }
  1357. }
  1358. void AnnounceCurrency(void)
  1359. {
  1360. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1361. {
  1362. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1363. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1364. {
  1365. Set_Dispenser_MiscCommand(index, MISC_DISP_CURRENCY);
  1366. }
  1367. }
  1368. }
  1369. void AnnounceRfidEndian(void)
  1370. {
  1371. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1372. {
  1373. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1374. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1375. {
  1376. Set_Dispenser_MiscCommand(index, MISC_DISP_RFID_CARD_ENDIAN);
  1377. }
  1378. }
  1379. }
  1380. void AnnounceRfidReader(void)
  1381. {
  1382. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1383. {
  1384. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1385. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1386. {
  1387. Set_Dispenser_MiscCommand(index, MISC_DISP_RFID_ENABLE);
  1388. }
  1389. }
  1390. }
  1391. void AnnounceRemoteStartNoID(void)
  1392. {
  1393. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  1394. {
  1395. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1396. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1397. {
  1398. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  1399. {
  1400. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  1401. if(chargingInfo[gun]->SystemStatus == S_IDLE)
  1402. {
  1403. ShmChargerInfo->ConnectorPageReq[gun].bits.RemoteStartNoID = true;
  1404. break;
  1405. }
  1406. }
  1407. }
  1408. }
  1409. }
  1410. void AnnounceLineStatusCode(int gun, int status)
  1411. {
  1412. int dispenser = 0;
  1413. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].ParentDispensetIndex;
  1414. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_None &&
  1415. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  1416. {
  1417. ShmChargerInfo->LineApi.StatusCode[gun] = status;
  1418. Set_Connector_MiscCommand(gun, MISC_CONN_LINE_STATUS);
  1419. }
  1420. }
  1421. void AnnounceStationInfo(void)
  1422. {
  1423. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1424. {
  1425. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1426. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1427. {
  1428. Set_Dispenser_MiscCommand(index, MISC_DISP_STATION_INFO);
  1429. }
  1430. }
  1431. }
  1432. void AnnounceOccupancyPrice(void)
  1433. {
  1434. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1435. {
  1436. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1437. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1438. {
  1439. Set_Dispenser_MiscCommand(index, MISC_DISP_PARKING_PRICE);
  1440. }
  1441. }
  1442. }
  1443. void UpdateConfigAndIcon(void)
  1444. {
  1445. unsigned char status = _Connnection_Disable;
  1446. // update ethernet connection status
  1447. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1448. if(ShmChargerInfo->CabinetMiscValue.EthernetStatus != status)
  1449. {
  1450. ShmChargerInfo->CabinetMiscValue.EthernetStatus = status;
  1451. AnnounceEthernetStatus();
  1452. }
  1453. // update wifi connection status
  1454. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1455. {
  1456. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1457. }
  1458. else
  1459. {
  1460. status = _Connnection_Disable;
  1461. }
  1462. if(ShmChargerInfo->CabinetMiscValue.WiFiStatus != status)
  1463. {
  1464. ShmChargerInfo->CabinetMiscValue.WiFiStatus = status;
  1465. AnnounceWiFiStatus();
  1466. }
  1467. // update 4g connection status
  1468. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1469. {
  1470. status = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1471. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? _Connnection_Connected : _Connnection_Disconnected;
  1472. }
  1473. else
  1474. {
  1475. status = _Connnection_Disable;
  1476. }
  1477. if(ShmChargerInfo->CabinetMiscValue.TelcomModemStatus != status)
  1478. {
  1479. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = status;
  1480. AnnounceTelcomModemStatus();
  1481. }
  1482. // update backend connection status
  1483. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1484. if(ShmChargerInfo->CabinetMiscValue.BackendStatus != status)
  1485. {
  1486. ShmChargerInfo->CabinetMiscValue.BackendStatus = status;
  1487. AnnounceBackendStatus();
  1488. }
  1489. // update billing status
  1490. status = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1491. if(ShmChargerInfo->CabinetMiscValue.BillingStatus != status)
  1492. {
  1493. ShmChargerInfo->CabinetMiscValue.BillingStatus = status;
  1494. AnnounceBillingStatus();
  1495. }
  1496. // update stop charging button
  1497. status = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1498. if(ShmChargerInfo->CabinetMiscValue.StopChargingButton != status)
  1499. {
  1500. ShmChargerInfo->CabinetMiscValue.StopChargingButton = status;
  1501. AnnounceStopChargingButtonStatus();
  1502. }
  1503. // update AuthMode config
  1504. status = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  1505. if(ShmChargerInfo->CabinetMiscValue.AuthModeConfig != status)
  1506. {
  1507. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = status;
  1508. AnnounceAuthModeConfig();
  1509. AnnounceEVCCIDConfig();
  1510. }
  1511. // update EVCCID config
  1512. status = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  1513. if(ShmChargerInfo->CabinetMiscValue.EVCCIDConfig != status)
  1514. {
  1515. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = status;
  1516. AnnounceEVCCIDConfig();
  1517. }
  1518. // update LED Intensity
  1519. status = ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity;
  1520. if(ShmChargerInfo->CabinetMiscValue.LEDIntensity != status)
  1521. {
  1522. ShmChargerInfo->CabinetMiscValue.LEDIntensity = status;
  1523. AnnounceLEDIntensity();
  1524. }
  1525. // update currency
  1526. status = ShmSysConfigAndInfo->SysConfig.BillingData.Currency;
  1527. if(ShmChargerInfo->CabinetMiscValue.Currency != status)
  1528. {
  1529. ShmChargerInfo->CabinetMiscValue.Currency = status;
  1530. AnnounceCurrency();
  1531. }
  1532. // update RfidCardNumEndian
  1533. status = ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian;
  1534. if(ShmChargerInfo->CabinetMiscValue.RfidEndian != status)
  1535. {
  1536. ShmChargerInfo->CabinetMiscValue.RfidEndian = status;
  1537. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Phihong ||
  1538. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  1539. {
  1540. AnnounceRfidEndian();
  1541. }
  1542. }
  1543. // update AuthMode config
  1544. status = ShmSysConfigAndInfo->SysConfig.isRFID;
  1545. if(ShmChargerInfo->AuthInfo.AuthMode.bits.RFIDEnable != status)
  1546. {
  1547. ShmChargerInfo->AuthInfo.AuthMode.bits.RFIDEnable = status;
  1548. ShmChargerInfo->CabinetMiscValue.RfidReaderEnable = status;
  1549. AnnounceRfidReader();
  1550. }
  1551. }
  1552. void Check4gWifiReset(void)
  1553. {
  1554. int timeDelay = 0;
  1555. if(ShmChargerInfo->Control.SysCtrl.bits.BackendEnable)
  1556. {
  1557. if((!ShmSysConfigAndInfo->SysInfo.InternetConn && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  1558. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE ||
  1559. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)) ||
  1560. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES)
  1561. {
  1562. if(!_NeedReset4gWifi)
  1563. {
  1564. GetClockTime(&_4gWifiReset_time);
  1565. LOG_INFO("Backend Enable But Internet Not Available!");
  1566. }
  1567. _NeedReset4gWifi = true;
  1568. }
  1569. else
  1570. {
  1571. _NeedReset4gWifi = false;
  1572. }
  1573. if(_NeedReset4gWifi)
  1574. {
  1575. timeDelay = ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES ?
  1576. FAST_RESET_4G_WIFI_TIME : TRY_RESET_4G_WIFI_TIME;
  1577. if(GetTimeoutValue(_4gWifiReset_time) / uSEC_VAL >= timeDelay)
  1578. {
  1579. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = true;
  1580. usleep(100000);
  1581. // before reset 4g io, kill Module_4g & Module_Wifi task
  1582. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1583. {
  1584. system("killall Module_4g &");
  1585. }
  1586. usleep(100000);
  1587. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1588. {
  1589. system("killall Module_Wifi &");
  1590. }
  1591. LOG_INFO("Reset 4g_Wifi Module");
  1592. system("echo 1 > /sys/class/gpio/gpio104/value");
  1593. sleep(1);
  1594. system("echo 0 > /sys/class/gpio/gpio104/value");
  1595. _NeedReset4gWifi = false;
  1596. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = false;
  1597. }
  1598. }
  1599. }
  1600. else
  1601. {
  1602. _NeedReset4gWifi = false;
  1603. }
  1604. }
  1605. void MasterCabinetDhcpClientConfig(void)
  1606. {
  1607. char tmpbuf[256];
  1608. //Run DHCP client if enabled
  1609. system("killall udhcpc");
  1610. system("rm -rf /etc/resolv.conf");
  1611. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1612. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1613. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1614. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1615. {
  1616. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1617. system(tmpbuf);
  1618. }
  1619. //Upgrade system id to /etc/hostname
  1620. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1621. system(tmpbuf);
  1622. }
  1623. void InitMasterCabinetEthernet(void)
  1624. {
  1625. char tmpbuf[256];
  1626. //Init Eth0 for internet
  1627. memset(tmpbuf,0,256);
  1628. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1629. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1630. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1631. system(tmpbuf);
  1632. memset(tmpbuf,0,256);
  1633. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1634. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1635. system(tmpbuf);
  1636. //system("ifconfig lo up");
  1637. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1638. //Init Eth1 for administrator tool
  1639. memset(tmpbuf,0,256);
  1640. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", "192.168.100.1", "255.255.255.0");
  1641. system(tmpbuf);
  1642. }
  1643. void InitSlaveCabinetEthernet(void)
  1644. {
  1645. char tmpbuf[256];
  1646. system("/sbin/ifconfig eth0 down");
  1647. system("/sbin/ifconfig eth1 down");
  1648. system("killall udhcpc");
  1649. system("rm -rf /etc/resolv.conf");
  1650. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  1651. //Init Eth0 for internet
  1652. memset(tmpbuf,0,256);
  1653. sprintf(tmpbuf,"/sbin/ifconfig eth0 192.168.100.3%d netmask %s up",
  1654. (ShmChargerInfo->Control.CabinetSwitch - 1),
  1655. "255.255.255.0");
  1656. system(tmpbuf);
  1657. system("/sbin/route add default gw 192.168.100.1 eth0");
  1658. //Init Eth1 for internet
  1659. memset(tmpbuf,0,256);
  1660. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", SLAVE_ETH1_IP, SLAVE_ETH1_SUBMASK);
  1661. system(tmpbuf);
  1662. }
  1663. void InitEthernet(void)
  1664. {
  1665. system("ifconfig eth0 down");
  1666. system("ifconfig eth1 down");
  1667. sleep(2);
  1668. char tmpbuf[256];
  1669. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1670. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1671. sleep(2);
  1672. InitMasterCabinetEthernet();
  1673. MasterCabinetDhcpClientConfig();
  1674. // DO360 DHCP Server
  1675. InitialDispenserDhcpServerConfig();
  1676. StartDispenserDhcpServer();
  1677. pid_t pid = fork();
  1678. uint8_t cnt_pingDNS_Fail;
  1679. if(pid == 0)
  1680. {
  1681. ShmChargerInfo->CabinetMiscValue.BackendStatus = _Connnection_Disconnected;
  1682. ShmChargerInfo->CabinetMiscValue.EthernetStatus = _Connnection_Disconnected;
  1683. ShmChargerInfo->CabinetMiscValue.WiFiStatus = _Connnection_Disable;
  1684. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = _Connnection_Disable;
  1685. LOG_INFO("Internet Fork PID = %d", getpid());
  1686. for(;;)
  1687. {
  1688. if(isEthernetCablePlugIn(0) == YES)
  1689. {
  1690. if(isRouteFail())
  1691. {
  1692. LOG_INFO("eth0 not in route, restart eht0.");
  1693. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1694. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1695. {
  1696. InitialDHCP();
  1697. }
  1698. else
  1699. {
  1700. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1701. memset(tmpbuf,0,256);
  1702. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1703. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1704. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1705. system(tmpbuf);
  1706. memset(tmpbuf,0,256);
  1707. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1708. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1709. system(tmpbuf);
  1710. }
  1711. }
  1712. }
  1713. if(isReachableInternet() == PASS)
  1714. {
  1715. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != NO)
  1716. {
  1717. LOG_INFO("Internet is Reachable");
  1718. }
  1719. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = NO;
  1720. ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1721. cnt_pingDNS_Fail = 0;
  1722. }
  1723. else
  1724. {
  1725. if(++cnt_pingDNS_Fail > 3)
  1726. {
  1727. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != YES)
  1728. {
  1729. LOG_INFO("Internet is Disconnect Via Ethernet");
  1730. }
  1731. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1732. ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1733. }
  1734. }
  1735. bool ethResult = ShmSysConfigAndInfo->SysInfo.ethInternetConn;
  1736. if(ethResult == YES)
  1737. {
  1738. system("/sbin/ifmetric eth0 0");
  1739. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1740. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1741. {
  1742. system("/sbin/ifmetric mlan0 1");
  1743. }
  1744. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1745. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1746. {
  1747. system("/sbin/ifmetric ppp0 2");
  1748. }
  1749. }
  1750. if (!ethResult &&
  1751. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1752. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1753. {
  1754. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1755. if (ethResult)
  1756. {
  1757. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1758. {
  1759. system("/sbin/ifmetric eth0 1");
  1760. system("/sbin/ifmetric mlan0 0");
  1761. }
  1762. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1763. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1764. {
  1765. system("/sbin/ifmetric ppp0 2");
  1766. }
  1767. }
  1768. }
  1769. if (!ethResult &&
  1770. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1771. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1772. {
  1773. ethResult = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1774. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? YES : NO;
  1775. if (ethResult)
  1776. {
  1777. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1778. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1779. {
  1780. system("/sbin/ifmetric mlan0 2");
  1781. }
  1782. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1783. {
  1784. system("/sbin/ifmetric eth0 1");
  1785. system("/sbin/ifmetric ppp0 0");
  1786. }
  1787. }
  1788. }
  1789. // Bridge ethernet to uap0 to get dynamic ip address
  1790. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE &&
  1791. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP))
  1792. {
  1793. if((isUap0up() == PASS) && access("/sys/class/net/br0/address", F_OK) == -1)
  1794. {
  1795. system("/usr/sbin/brctl addbr br0");
  1796. system("/usr/sbin/brctl addif br0 eth0");
  1797. system("/usr/sbin/brctl addif br0 uap0");
  1798. system("/sbin/ifconfig br0 192.168.10.200 up");
  1799. InitialBridgeModeDhcpServerConfig();
  1800. //system("/bin/sed -i '/interface/d' /etc/udhcpd.conf");
  1801. //system("/bin/echo 'interface br0' >> /etc/udhcpd.conf");
  1802. system("pgrep -f \"udhcpd /etc/udhcpd.conf\" | xargs kill");
  1803. system("/usr/sbin/udhcpd /etc/udhcpd.conf");
  1804. LOG_INFO("Bridge uap0 & eth0 for local power sharing by ethernet.");
  1805. _isBridgeModeConfig = true;
  1806. }
  1807. }
  1808. else
  1809. {
  1810. if(access("/sys/class/net/br0/address", F_OK) != -1)
  1811. {
  1812. system("/sbin/ifconfig br0 down");
  1813. system("/usr/sbin/brctl delbr br0");
  1814. LOG_INFO("Disable uap0 & eth0 Bridge.");
  1815. }
  1816. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP && _isBridgeModeConfig)
  1817. {
  1818. InitialApModeDhcpServerConfig();
  1819. system("pgrep -f \"udhcpd /etc/udhcpd.conf\" | xargs kill");
  1820. system("/usr/sbin/udhcpd /etc/udhcpd.conf");
  1821. LOG_INFO("Restart uap0 udhcpd.");
  1822. _isBridgeModeConfig = false;
  1823. }
  1824. }
  1825. // Change uap0 to low priority
  1826. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE &&
  1827. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP) &&
  1828. (isUap0up() == PASS))
  1829. {
  1830. system("/sbin/ifmetric uap0 1");
  1831. }
  1832. //==============================================
  1833. // Check Local Power Sharing Communication
  1834. //==============================================
  1835. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing == _SYS_POWER_SHARING_MODE_SLAVE)
  1836. {
  1837. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  1838. {
  1839. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == ON)
  1840. {
  1841. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
  1842. }
  1843. }
  1844. else
  1845. {
  1846. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == OFF)
  1847. {
  1848. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = ON;
  1849. }
  1850. }
  1851. }
  1852. else
  1853. {
  1854. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
  1855. }
  1856. if(!Is_Ocpp_Task_Alive())
  1857. {
  1858. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = false;
  1859. }
  1860. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1861. ShmStatusCodeData->InfoCode.InfoEvents.bits.WiFiDisable = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_DISABLE ? true : false;
  1862. ShmStatusCodeData->InfoCode.InfoEvents.bits.Telocom4GModuleDisable = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == false ? true : false;
  1863. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet = ShmSysConfigAndInfo->SysInfo.OcppConnStatus == false ? true : false;
  1864. UpdateConfigAndIcon();
  1865. Check4gWifiReset();
  1866. sleep(5);
  1867. }
  1868. }
  1869. #ifdef SystemLogMessage
  1870. LOG_INFO("[main]InitEthernet: Initial Ethernet OK.");
  1871. #endif
  1872. }
  1873. void GetMacAddress()
  1874. {
  1875. for (byte index = 0; index < 2; index++)
  1876. {
  1877. int fd;
  1878. struct ifreq ifr;
  1879. char tarEth[5];
  1880. char Mac[18];
  1881. sprintf(tarEth,"eth%d",index);
  1882. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1883. ifr.ifr_addr.sa_family = AF_INET;
  1884. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1885. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1886. close(fd);
  1887. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1888. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1889. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1890. if (index == 0)
  1891. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1892. else
  1893. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1894. }
  1895. }
  1896. void GetFirmwareVersion()
  1897. {
  1898. // Get CSU root file system version
  1899. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1900. sprintf(ShmChargerInfo->SysMisc.MainVersion, fwVersion);
  1901. sprintf(ShmChargerInfo->SysMisc.SubVersion, subVersion);
  1902. #if ENABLE_PCBA_TEST == 0
  1903. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1904. for(uint8_t idx=0;idx<3;idx++)
  1905. {
  1906. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J')
  1907. {
  1908. chademo++;
  1909. count++;
  1910. }
  1911. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1912. {
  1913. gb++;
  1914. count++;
  1915. }
  1916. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  1917. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E')
  1918. {
  1919. ccs++;
  1920. count++;
  1921. }
  1922. }
  1923. if (count == 1)
  1924. {
  1925. if (chademo > 0)
  1926. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1927. else if (ccs > 0)
  1928. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1929. else if (gb > 0)
  1930. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1931. }
  1932. else
  1933. {
  1934. if (chademo > 0 && ccs > 0)
  1935. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1936. else if (chademo > 0 && gb > 0)
  1937. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1938. else if (ccs > 0 && gb > 0)
  1939. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1940. }
  1941. // Get network option from model name
  1942. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1943. {
  1944. case 'B':
  1945. case 'U':
  1946. //Blue tooth
  1947. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1948. break;
  1949. case 'W':
  1950. // WIFI
  1951. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1952. break;
  1953. case 'T':
  1954. // 3G/4G
  1955. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1956. break;
  1957. case 'D':
  1958. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1959. break;
  1960. default:
  1961. // LAN
  1962. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1963. break;
  1964. }
  1965. // Get rating power from model name
  1966. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1967. // Get IEC or UL
  1968. char _buf[3] = {0};
  1969. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  1970. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL)
  1971. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1972. else if (strcmp(_buf, "WU") == EQUAL)
  1973. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1974. #endif
  1975. }
  1976. void InitialShareMemoryInfo()
  1977. {
  1978. FILE *fp;
  1979. char cmd[512];
  1980. char buf[512];
  1981. _UpgradeNeedReboot = FALSE;
  1982. _psuLostQuantity = 0;
  1983. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1984. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1985. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1986. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1987. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1988. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1989. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1990. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1991. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1992. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1993. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1994. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1995. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1996. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1997. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1998. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1999. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  2000. sprintf(cmd, "/bin/uname -r");
  2001. fp = popen(cmd, "r");
  2002. if(fp == NULL)
  2003. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  2004. else
  2005. {
  2006. while(fgets(buf, sizeof(buf), fp) != NULL)
  2007. {
  2008. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  2009. }
  2010. }
  2011. // 雙槍 CCS + Chademo
  2012. GetFirmwareVersion();
  2013. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  2014. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  2015. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  2016. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  2017. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  2018. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  2019. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  2020. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  2021. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  2022. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  2023. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  2024. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  2025. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  2026. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleHwRev, " "); //DO360 RC2
  2027. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, " "); //DO360 RC2
  2028. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  2029. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, " ");
  2030. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector1FwRev, " ");
  2031. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector2FwRev, " ");
  2032. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  2033. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  2034. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  2035. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  2036. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  2037. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  2038. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2039. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  2040. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  2041. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  2042. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  2043. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  2044. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  2045. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  2046. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  2047. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  2048. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  2049. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  2050. memset(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev)); //DO360 RC2
  2051. ShmPrimaryMcuData->SelfTest_Comp = NO;
  2052. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  2053. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  2054. ShmFanModuleData->SelfTest_Comp = NO;
  2055. ShmLedModuleData->SelfTest_Comp = NO;
  2056. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2057. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  2058. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  2059. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  2060. ShmFanModuleData->TestFanSpeed = 0;
  2061. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  2062. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  2063. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  2064. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  2065. LOG_INFO("DispenserSequence = %d, MaxConnectorQuantity = %d",
  2066. ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence,
  2067. ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity);
  2068. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  2069. {
  2070. LOG_INFO("Dispenser[%d] <=> %d", i, ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[i]);
  2071. }
  2072. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2073. {
  2074. memset((char *)&_BackupStatusNotification[i], 0, sizeof(struct StructStatusNotification));
  2075. _useGunMeterValue[i] = false;
  2076. _notifyOccupancyFeeAct[i] = _Notify_None;
  2077. }
  2078. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  2079. {
  2080. _canUpdateFinalCost[i] = false;
  2081. _isUndisposedFinalCost[i] = true;
  2082. }
  2083. }
  2084. int Initialization(void)
  2085. {
  2086. // int pinOut[2] = { 116, 115 };
  2087. for (byte count = 0; count < CONNECTOR_QUANTITY; count++)
  2088. {
  2089. chargingInfo[count]->RemoteStartFlag = NO;
  2090. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStartConfirm = false;
  2091. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStopConfirm = false;
  2092. SetOcppErrorCode(count, "NoError");
  2093. }
  2094. LOG_INFO("Initialization OK");
  2095. return PASS;
  2096. }
  2097. void InitialPsuGroupingAndLocation(void)
  2098. {
  2099. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  2100. {
  2101. memcpy(ShmPsuGrouping->Location, Group_to_Location_4_Gun, sizeof(ShmPsuGrouping->Location));
  2102. memcpy(ShmPsuGrouping->Layout, Location_to_Group_4_Gun, sizeof(ShmPsuGrouping->Layout));
  2103. }
  2104. else
  2105. {
  2106. memcpy(ShmPsuGrouping->Location, Group_to_Location_2_Gun, sizeof(ShmPsuGrouping->Location));
  2107. memcpy(ShmPsuGrouping->Layout, Location_to_Group_2_Gun, sizeof(ShmPsuGrouping->Layout));
  2108. }
  2109. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2110. {
  2111. ShmPsuGrouping->GroupCollection[i].Index = i;
  2112. ShmPsuGrouping->GroupCollection[i].Location = ShmPsuGrouping->Location[i];
  2113. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  2114. {
  2115. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2116. {
  2117. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun[i][0], GENERAL_GUN_QUANTITY);
  2118. }
  2119. else
  2120. {
  2121. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun_6Parallel[i][0], GENERAL_GUN_QUANTITY);
  2122. }
  2123. }
  2124. else
  2125. {
  2126. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_2_Gun[i][0], GENERAL_GUN_QUANTITY);
  2127. }
  2128. ShmChargerInfo->Control.GunAvailable[i] = NO;
  2129. }
  2130. }
  2131. void InitialCustomerCode(void)
  2132. {
  2133. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  2134. {
  2135. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Audi;
  2136. }
  2137. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'E' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == '4')
  2138. {
  2139. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_E4U;
  2140. }
  2141. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'U' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'U')
  2142. {
  2143. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_UCar;
  2144. }
  2145. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'T' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'C')
  2146. {
  2147. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_TCC;
  2148. }
  2149. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'O' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'L')
  2150. {
  2151. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Shell;
  2152. }
  2153. else
  2154. {
  2155. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Phihong;
  2156. }
  2157. }
  2158. void InitialCustomizedSetting(void)
  2159. {
  2160. ShmChargerInfo->Control.CustomizedInfo.StandbyTime = STANDBY_TIME;
  2161. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  2162. {
  2163. ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyTimeDisableForTcc = true;
  2164. }
  2165. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_E4U)
  2166. {
  2167. ShmChargerInfo->Control.CustomizedInfo.Flag.bits.CustomizedStandbyTime = true;
  2168. ShmChargerInfo->Control.CustomizedInfo.StandbyTime = E4U_STANDBY_TIME;
  2169. }
  2170. }
  2171. void InitialMaxDispenserConnectorQuantity(void)
  2172. {
  2173. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable == false)
  2174. {
  2175. ShmChargerInfo->Control.MaxDispenser = 2;
  2176. ShmChargerInfo->Control.MaxConnector = 2;
  2177. }
  2178. else
  2179. {
  2180. ShmChargerInfo->Control.MaxDispenser = GENERAL_GUN_QUANTITY;
  2181. ShmChargerInfo->Control.MaxConnector = GENERAL_GUN_QUANTITY;
  2182. }
  2183. }
  2184. void InitialAuthMode(void)
  2185. {
  2186. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  2187. {
  2188. }
  2189. }
  2190. void InitialQRCodeMode(void)
  2191. {
  2192. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi ||
  2193. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  2194. {
  2195. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_ChargeBoxId;
  2196. }
  2197. else
  2198. {
  2199. // customized
  2200. if(ShmSysConfigAndInfo->SysConfig.QRCodeMadeMode)
  2201. {
  2202. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Customized;
  2203. }
  2204. else
  2205. {
  2206. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Default;
  2207. }
  2208. }
  2209. }
  2210. void InitialPowerCabinetSetting(void)
  2211. {
  2212. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  2213. {
  2214. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = true;
  2215. }
  2216. ShmChargerInfo->CabinetMiscValue.BillingStatus = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  2217. ShmChargerInfo->CabinetMiscValue.StopChargingButton = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  2218. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  2219. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  2220. }
  2221. void Initial6ParallelSetting(void)
  2222. {
  2223. if(ShmSysConfigAndInfo->SysConfig.ModelName[11] == '3')
  2224. {
  2225. ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay = 1;
  2226. }
  2227. }
  2228. void PowerModuleLibSelection(void)
  2229. {
  2230. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'L')
  2231. {
  2232. ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib = true;
  2233. }
  2234. else
  2235. {
  2236. ShmChargerInfo->Control.LibCtrl.bits.InfyPwrLib = true;
  2237. }
  2238. }
  2239. bool InitialChargerSetting(void)
  2240. {
  2241. bool result = true;
  2242. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' &&
  2243. (ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'O' || ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'L'))
  2244. {
  2245. ShmChargerInfo->Control.PrimaryCtrl.bits.AcContactorReverse = false;
  2246. ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse = false;
  2247. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2248. ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable = true;
  2249. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  2250. }
  2251. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  2252. {
  2253. if(!(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'P' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'K'))
  2254. {
  2255. ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  2256. }
  2257. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2258. ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  2259. }
  2260. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'B')
  2261. {
  2262. //ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  2263. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2264. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  2265. //ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  2266. ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable = true;
  2267. ShmChargerInfo->Control.RelayCtrl.bits.DcInputEnable = true;
  2268. }
  2269. else
  2270. {
  2271. result = false;
  2272. }
  2273. LOG_INFO("door sensor reverse = %d", ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse);
  2274. LOG_INFO("ac contactor reverse = %d",ShmChargerInfo->Control.PrimaryCtrl.bits.AcContactorReverse);
  2275. InitialCustomerCode();
  2276. InitialCustomizedSetting();
  2277. InitialMaxDispenserConnectorQuantity();
  2278. InitialAuthMode();
  2279. InitialQRCodeMode();
  2280. Initial6ParallelSetting();
  2281. InitialPsuGroupingAndLocation();
  2282. InitialPowerCabinetSetting();
  2283. PowerModuleLibSelection();
  2284. if(!CheckConnectorTypeStatus())
  2285. {
  2286. result = false;
  2287. }
  2288. return result;
  2289. }
  2290. bool InitialSystemDefaultConfig()
  2291. {
  2292. bool result = true;
  2293. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2294. InitGPIO();
  2295. InitEthernet();
  2296. GetMacAddress();
  2297. return result;
  2298. }
  2299. void SelfTestRun(void)
  2300. {
  2301. LOG_INFO("Self test.");
  2302. system("/root/Module_ChargerSelfTest &");
  2303. }
  2304. void SpawnPowerModuleTask(void)
  2305. {
  2306. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  2307. {
  2308. system("/root/Module_PsuComm_PH &");
  2309. }
  2310. else
  2311. {
  2312. system("/root/Module_PsuComm &");
  2313. }
  2314. }
  2315. void KillPowerModuleTask(void)
  2316. {
  2317. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  2318. {
  2319. system("killall Module_PsuComm_PH");
  2320. }
  2321. else
  2322. {
  2323. system("killall Module_PsuComm");
  2324. }
  2325. }
  2326. void SpawnNormalService(void)
  2327. {
  2328. system("/root/Module_EventLogging &");
  2329. system("/root/Module_PrimaryComm &");
  2330. system("/root/Module_EvComm &");
  2331. //system("/root/Module_LcmControl &");
  2332. system("/root/Module_InternalComm &");
  2333. SpawnPowerModuleTask();
  2334. system("/root/Module_ProduceUtils &");
  2335. system("/root/Module_CabinetParallel &");
  2336. system("/root/Module_LedIndication &");
  2337. system("/root/Module_Authorize &");
  2338. }
  2339. void SpawnOcppService(void)
  2340. {
  2341. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  2342. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  2343. {
  2344. Run_Ocpp_Service();
  2345. }
  2346. }
  2347. void SpawnMaintainOcppService(void)
  2348. {
  2349. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  2350. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  2351. {
  2352. Run_MaintainOcpp_Service();
  2353. }
  2354. }
  2355. void SpawnInternetService(void)
  2356. {
  2357. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  2358. {
  2359. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2360. {
  2361. system("/root/Module_4g &");
  2362. }
  2363. }
  2364. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2365. {
  2366. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2367. {
  2368. system("/root/Module_Wifi &");
  2369. }
  2370. }
  2371. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  2372. {
  2373. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2374. {
  2375. system("/root/Module_4g &");
  2376. }
  2377. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2378. {
  2379. system("/root/Module_Wifi &");
  2380. }
  2381. }
  2382. }
  2383. int SpawnTask(void)
  2384. {
  2385. sleep(2);
  2386. SpawnNormalService();
  2387. SpawnInternetService();
  2388. return PASS;
  2389. }
  2390. int runFlashShellCmd(const char *cmd)
  2391. {
  2392. int result = FAIL;
  2393. char buf[256];
  2394. FILE *fp;
  2395. fp = popen(cmd, "r");
  2396. if(fp != NULL)
  2397. {
  2398. while(fgets(buf, sizeof(buf), fp) != NULL)
  2399. {
  2400. LOG_INFO("%s\n", buf);
  2401. }
  2402. result = PASS;
  2403. }
  2404. pclose(fp);
  2405. return result;
  2406. }
  2407. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2408. {
  2409. int result = PASS;
  2410. int fd, wrd;
  2411. unsigned int i, Chk;
  2412. unsigned char *ptr, *BufTmp;
  2413. Chk = 0;
  2414. ptr = (unsigned char *)UsrData;
  2415. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2416. {
  2417. memset(BufTmp, 0, MtdBlockSize);
  2418. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2419. for(i = ARRAY_SIZE(UsrData->CsuBootLoadFwRev); i < MtdBlockSize - 4; i++)
  2420. {
  2421. Chk += *(BufTmp+i);
  2422. }
  2423. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  2424. // Output configuration to file.
  2425. fd = open("/mnt/EvseTempConfig.bin", O_RDWR | O_CREAT | O_TRUNC);
  2426. if(fd < 0)
  2427. {
  2428. LOG_ERROR("open /mnt/EvseTempConfig.bin NG");
  2429. free(BufTmp);
  2430. return 0;
  2431. }
  2432. wrd = write(fd, BufTmp, MtdBlockSize);
  2433. close(fd);
  2434. if(wrd < MtdBlockSize)
  2435. {
  2436. LOG_ERROR("write /mnt/EvseTempConfig.bin NG");
  2437. free(BufTmp);
  2438. return 0;
  2439. }
  2440. LOG_INFO("EvseTempConfig write to file in /mnt OK.");
  2441. LOG_INFO("Erase /dev/mtd10.");
  2442. runFlashShellCmd("flash_erase /dev/mtd10 0 0");
  2443. LOG_INFO("Write /dev/mtd10.");
  2444. runFlashShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseTempConfig.bin");
  2445. LOG_INFO("Erase /dev/mtd11.");
  2446. runFlashShellCmd("flash_erase /dev/mtd11 0 0");
  2447. LOG_INFO("Write /dev/mtd11.");
  2448. runFlashShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseTempConfig.bin");
  2449. system("rm -f /mnt/EvseTempConfig.bin");
  2450. LOG_INFO("EvseTempConfig write to flash OK");
  2451. }
  2452. else
  2453. {
  2454. LOG_ERROR("alloc BlockSize NG");
  2455. result = FAIL;
  2456. }
  2457. if(BufTmp != NULL)
  2458. {
  2459. free(BufTmp);
  2460. }
  2461. return result;
  2462. }
  2463. //===============================================
  2464. // Common Detect Chk - Stop Charging ?
  2465. //===============================================
  2466. bool isEvBoardStopChargeFlag(byte gunIndex)
  2467. {
  2468. return chargingInfo[gunIndex]->ChargingStopFlag.bits.AlarmStop;
  2469. }
  2470. bool isNormalStopChargeFlag(byte gunIndex)
  2471. {
  2472. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.NormalStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.ManualStop);
  2473. }
  2474. bool isResetStopChargeFlag(byte gunIndex)
  2475. {
  2476. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.HardResetStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.SoftResetStop);
  2477. }
  2478. bool isInvalidIdStopFlag(byte gunIndex)
  2479. {
  2480. return chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop;
  2481. }
  2482. bool isSessionTargetStopFlag(byte gunIndex)
  2483. {
  2484. return chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop;
  2485. }
  2486. bool isBackendStopFlag(byte gunIndex)
  2487. {
  2488. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.BackendStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop);
  2489. }
  2490. void ReleaseAlarmCode(byte gunIndex)
  2491. {
  2492. if(!Is_Ocpp_StatusNotificationErrorCode(gunIndex, "NoError"))
  2493. {
  2494. SetOcppErrorCode(gunIndex, "NoError");
  2495. }
  2496. if(!Is_Ocpp_StatusNotificationVendorCode(gunIndex, ""))
  2497. {
  2498. SetOcppVendorErrorCode(gunIndex, "");
  2499. }
  2500. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  2501. {
  2502. strcpy((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "");
  2503. }
  2504. if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  2505. {
  2506. strcpy((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "");
  2507. }
  2508. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "") != EQUAL)
  2509. {
  2510. strcpy((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "");
  2511. }
  2512. if (strcmp((char *)ShmChargerInfo->GunError[gunIndex].GunAlarmCode, "") != EQUAL)
  2513. {
  2514. strcpy((char *)ShmChargerInfo->GunError[gunIndex].GunAlarmCode, "");
  2515. }
  2516. }
  2517. //===============================================
  2518. // EmergencyStop and Charging Stop
  2519. //===============================================
  2520. void ChargingTerminalProcess(byte gunIndex)
  2521. {
  2522. setChargerMode(gunIndex, MODE_TERMINATING);
  2523. }
  2524. void ChargingAlarmProcess(byte gunIndex)
  2525. {
  2526. UpdateErrorCodeToOcpp(gunIndex);
  2527. setChargerMode(gunIndex, MODE_ALARM);
  2528. }
  2529. void AcChargingTerminalProcess()
  2530. {
  2531. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2532. }
  2533. void StopChargingProcessByString(byte level)
  2534. {
  2535. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2536. {
  2537. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2538. }
  2539. }
  2540. void ReleaseChargingProcessByString(byte level)
  2541. {
  2542. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2543. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2544. }
  2545. // 一般錯誤停止充電處理函式
  2546. void BoardErrOccurByString(byte index, char *code)
  2547. {
  2548. byte level = 1;
  2549. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2550. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2551. {
  2552. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2553. {
  2554. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2555. }
  2556. ChargingTerminalProcess(index);
  2557. }
  2558. StopChargingProcessByString(level);
  2559. }
  2560. // 急停狀況的停止充電處理函式
  2561. void EmcOccureByString(char *code)
  2562. {
  2563. byte level = 2;
  2564. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  2565. {
  2566. return;
  2567. }
  2568. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2569. // 其錯誤等級為 2
  2570. if (strncmp(code, "042251", 6) == EQUAL || strncmp(code, "042252", 6) == EQUAL ||
  2571. strncmp(code, "042238", 6) == EQUAL || strncmp(code, "042267", 6) == EQUAL ||
  2572. strncmp(code, "042328", 6) == EQUAL)
  2573. {
  2574. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2575. {
  2576. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2577. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2578. {
  2579. if (strncmp((char *)chargingInfo[gun]->ConnectorAlarmCode, "", 6) == EQUAL)
  2580. {
  2581. memcpy(chargingInfo[gun]->ConnectorAlarmCode, code, 6);
  2582. }
  2583. InformOcppErrOccur(4);
  2584. ChargingAlarmProcess(gun);
  2585. }
  2586. }
  2587. StopChargingProcessByString(level);
  2588. }
  2589. }
  2590. void ReleaseEmsOccureByString(byte index, char *code)
  2591. {
  2592. bool isTrigger = false;
  2593. byte level = 2;
  2594. if (strncmp(code, "042251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2595. {
  2596. isTrigger = true;
  2597. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2598. }
  2599. else if (strncmp(code, "042252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2600. {
  2601. isTrigger = true;
  2602. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2603. }
  2604. else if (strncmp(code, "042237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2605. {
  2606. isTrigger = true;
  2607. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2608. }
  2609. else if (strncmp(code, "042238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2610. {
  2611. isTrigger = true;
  2612. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2613. }
  2614. else if (strncmp(code, "042267", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm == YES)
  2615. {
  2616. isTrigger = true;
  2617. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  2618. }
  2619. if (isTrigger)
  2620. {
  2621. ReleaseChargingProcessByString(level);
  2622. }
  2623. }
  2624. //===============================================
  2625. // 確認硬體 (按鈕) 狀態
  2626. //===============================================
  2627. void ChkPrimaryStatus()
  2628. {
  2629. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  2630. {
  2631. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2632. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2633. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2634. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2635. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2636. return;
  2637. }
  2638. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2639. {
  2640. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2641. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = YES;
  2642. EmcOccureByString("042251");
  2643. }
  2644. else
  2645. {
  2646. ReleaseEmsOccureByString(0, "042251");
  2647. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2648. }
  2649. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2650. {
  2651. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2652. //EmcOccureByString("042238");
  2653. }
  2654. else
  2655. {
  2656. //ReleaseEmsOccureByString(0, "042238");
  2657. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2658. }
  2659. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2660. {
  2661. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2662. }
  2663. else
  2664. {
  2665. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2666. }
  2667. if(ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL &&
  2668. (ShmChargerInfo->Control.CustomizedInfo.TiltSensorStep == _TILT_SENSOR_NONE ||
  2669. ShmChargerInfo->Control.CustomizedInfo.TiltSensorStep == _TILT_SENSOR_FINISH))
  2670. {
  2671. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2672. EmcOccureByString("042252");
  2673. }
  2674. else
  2675. {
  2676. ReleaseEmsOccureByString(0, "042252");
  2677. }
  2678. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2679. {
  2680. EmcOccureByString("042328");
  2681. SetAcContactor(OFF);
  2682. }
  2683. if(ShmChargerInfo->Control.CustomizedInfo.TiltSensorFail)
  2684. {
  2685. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.TiltSensorStestFail = YES;
  2686. }
  2687. else
  2688. {
  2689. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.TiltSensorStestFail = NO;
  2690. }
  2691. }
  2692. struct timespec _AcContactReOpen_Time;
  2693. void ChkPsuStatus(void)
  2694. {
  2695. if(!ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2696. {
  2697. if(IsConnectorWholeIdle())
  2698. {
  2699. for(int psuGroup = 0; psuGroup < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; psuGroup++)
  2700. {
  2701. //if(ShmPsuData->PsuGroup[psuGroup].GroupErrorFlag.PsuGroupErrorValue != 0)
  2702. //{
  2703. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2704. //}
  2705. if(ShmChargerInfo->PsuGrouping.GroupPsuStatus_OR[psuGroup].StatusVal & PSU_STATUS_FLAG_FAULT)
  2706. {
  2707. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2708. }
  2709. }
  2710. }
  2711. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2712. {
  2713. EmcOccureByString("042267");
  2714. }
  2715. }
  2716. else
  2717. {
  2718. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor &&
  2719. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume == NO)
  2720. {
  2721. bool allFaultStatus = true;
  2722. for(int gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2723. {
  2724. if(chargingInfo[gunIndex]->SystemStatus != S_FAULT && chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  2725. {
  2726. allFaultStatus = false;
  2727. }
  2728. }
  2729. if(allFaultStatus)
  2730. {
  2731. LOG_INFO("PSU Failure, Need Resume");
  2732. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = YES;
  2733. }
  2734. }
  2735. }
  2736. }
  2737. int AcContactorReOpenCount = 0;
  2738. void PsuFailureResume(void)
  2739. {
  2740. if(ShmChargerInfo->Control.PsuCtrl.bits.FailureResume)
  2741. {
  2742. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO)
  2743. {
  2744. LOG_INFO("Ac Contactor Off By Psu Failure");
  2745. // update _AcContactReOpen_Time
  2746. GetClockTime(&_AcContactReOpen_Time);
  2747. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = YES;
  2748. AcContactorReOpenCount++;
  2749. }
  2750. else
  2751. {
  2752. if(AcContactorReOpenCount < MAX_AC_CONTACTOR_COUNT)
  2753. {
  2754. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= AC_CONTACTOR_REOPEN_TIME)
  2755. {
  2756. ReleaseEmsOccureByString(0, "042267");
  2757. // update _AcContactReOpen_Time
  2758. GetClockTime(&_AcContactReOpen_Time);
  2759. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = NO;
  2760. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = NO;
  2761. LOG_INFO("Psu Failure Resume");
  2762. }
  2763. }
  2764. }
  2765. }
  2766. else
  2767. {
  2768. if(AcContactorReOpenCount > 0)
  2769. {
  2770. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= PSU_FAILURE_RESUME_TIME)
  2771. {
  2772. AcContactorReOpenCount = 0;
  2773. }
  2774. }
  2775. }
  2776. }
  2777. void SetAcContactor(unsigned char OnOff)
  2778. {
  2779. OnOff = OnOff ? ON : OFF;
  2780. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor != OnOff)
  2781. {
  2782. if(OnOff)
  2783. {
  2784. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2785. }
  2786. LOG_INFO("Set Ac Contactor %s", OnOff == ON ? "On" : "Off");
  2787. }
  2788. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = OnOff == ON ? ON : OFF;
  2789. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER && ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  2790. {
  2791. SetParallelCabinetAcContactor(OnOff);
  2792. }
  2793. }
  2794. int GetStandbyTime(void)
  2795. {
  2796. int time = STANDBY_TIME;
  2797. if(ShmChargerInfo->Control.TestCtrl.bits.FastStandbyTime)
  2798. {
  2799. time = FAST_STANDBY_TIME;
  2800. }
  2801. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.CustomizedStandbyTime)
  2802. {
  2803. time = ShmChargerInfo->Control.CustomizedInfo.StandbyTime;
  2804. }
  2805. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyImmediately)
  2806. {
  2807. time = FAST_STANDBY_TIME;
  2808. }
  2809. return time;
  2810. }
  2811. void StandbyCheck(void)
  2812. {
  2813. bool enable = IsStandbyEnable();
  2814. if(IsConnectorWholeIdle() && IsParallelCabinetIdle())
  2815. {
  2816. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2817. {
  2818. int time = GetStandbyTime();
  2819. #if CONTACTOR_AUTO_ON_OFF == 1
  2820. time = CONTACTOR_AUTO_ON_TIME;
  2821. #endif
  2822. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor)
  2823. {
  2824. if(GetSecTimeoutValue(_Standby_time) >= time && enable)
  2825. {
  2826. SetAcContactor(OFF);
  2827. }
  2828. }
  2829. #if CONTACTOR_AUTO_ON_OFF == 1
  2830. time = CONTACTOR_AUTO_ON_TIME + CONTACTOR_AUTO_OFF_TIME;
  2831. if((GetTimeoutValue(_Standby_time) / uSEC_VAL) >= time)
  2832. {
  2833. SetAcContactor(ON);
  2834. }
  2835. #endif
  2836. }
  2837. else
  2838. {
  2839. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = YES;
  2840. GetClockTime(&_Standby_time);
  2841. }
  2842. }
  2843. else
  2844. {
  2845. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2846. {
  2847. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2848. }
  2849. }
  2850. }
  2851. void CheckDisconnectionStatusRecovery(void)
  2852. {
  2853. bool recovery = true;
  2854. // check all connector
  2855. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2856. {
  2857. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection)
  2858. {
  2859. recovery = false;
  2860. }
  2861. }
  2862. if(recovery)
  2863. {
  2864. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = false;
  2865. }
  2866. }
  2867. //===============================================
  2868. // 確認各小板偵測的錯誤狀況
  2869. //===============================================
  2870. void CheckErrorOccurStatus(byte index)
  2871. {
  2872. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.NormalStopRequest)
  2873. {
  2874. chargingInfo[index]->ChargingStopFlag.bits.NormalStop = true;
  2875. }
  2876. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AlarmStopRequest)
  2877. {
  2878. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2879. {
  2880. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemotenAlarmCode, 6);
  2881. }
  2882. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2883. {
  2884. LOG_INFO("Connector %d Error Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2885. }
  2886. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2887. }
  2888. if(ShmChargerInfo->GunError[index].ErrFlag.ErrorValue != 0)
  2889. {
  2890. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2891. {
  2892. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmChargerInfo->GunError[index].GunAlarmCode, 6);
  2893. }
  2894. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2895. {
  2896. LOG_INFO("Connector %d GunError Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2897. }
  2898. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2899. }
  2900. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest)
  2901. {
  2902. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest = false;
  2903. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.Disconnection = true;
  2904. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = true;
  2905. if(chargingInfo[index]->SystemStatus == S_AUTHORIZING ||
  2906. chargingInfo[index]->SystemStatus == S_PREPARNING ||
  2907. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EV ||
  2908. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EVSE ||
  2909. chargingInfo[index]->SystemStatus == S_CHARGING)
  2910. {
  2911. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2912. {
  2913. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042304", 6);
  2914. }
  2915. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2916. {
  2917. LOG_INFO("Connector %d Disconnect Alarm Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2918. }
  2919. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2920. }
  2921. else if(chargingInfo[index]->SystemStatus == S_REASSIGN_CHECK ||
  2922. chargingInfo[index]->SystemStatus == S_REASSIGN)
  2923. {
  2924. setChargerMode(index, MODE_IDLE);
  2925. }
  2926. }
  2927. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo)
  2928. {
  2929. CheckDisconnectionStatusRecovery();
  2930. }
  2931. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES ||
  2932. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2933. {
  2934. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2935. {
  2936. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_PRIMARY;
  2937. StopChargingProcessByString(2);
  2938. }
  2939. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2940. {
  2941. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2942. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042251", 6);
  2943. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2944. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042252", 6);
  2945. }
  2946. }
  2947. else
  2948. {
  2949. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_PRIMARY)
  2950. {
  2951. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2952. ReleaseChargingProcessByString(2);
  2953. }
  2954. }
  2955. // RB
  2956. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2957. {
  2958. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2959. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2960. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2961. {
  2962. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2963. {
  2964. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2965. StopChargingProcessByString(2);
  2966. }
  2967. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2968. {
  2969. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2970. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042203", 6);
  2971. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2972. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042204", 6);
  2973. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2974. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042205", 6);
  2975. }
  2976. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2977. {
  2978. LOG_INFO("Connector %d RC Board Input UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2979. }
  2980. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2981. }
  2982. else
  2983. {
  2984. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2985. {
  2986. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2987. ReleaseChargingProcessByString(2);
  2988. }
  2989. }
  2990. }
  2991. else
  2992. {
  2993. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2994. {
  2995. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2996. ReleaseChargingProcessByString(2);
  2997. }
  2998. }
  2999. if(ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable == NO)
  3000. {
  3001. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  3002. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  3003. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  3004. {
  3005. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  3006. {
  3007. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  3008. StopChargingProcessByString(2);
  3009. }
  3010. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  3011. {
  3012. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  3013. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042200", 6);
  3014. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  3015. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042201", 6);
  3016. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  3017. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042202", 6);
  3018. }
  3019. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  3020. {
  3021. LOG_INFO("Connector %d RC Board Input OVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  3022. }
  3023. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  3024. }
  3025. else
  3026. {
  3027. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  3028. {
  3029. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3030. ReleaseChargingProcessByString(2);
  3031. }
  3032. }
  3033. }
  3034. else
  3035. {
  3036. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES ||
  3037. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  3038. {
  3039. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  3040. {
  3041. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  3042. StopChargingProcessByString(2);
  3043. }
  3044. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  3045. {
  3046. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES)
  3047. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042327", 6);
  3048. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  3049. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042328", 6);
  3050. }
  3051. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  3052. {
  3053. LOG_INFO("Gun %d DC Input OVP/UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  3054. }
  3055. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  3056. }
  3057. else
  3058. {
  3059. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  3060. {
  3061. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3062. ReleaseChargingProcessByString(2);
  3063. }
  3064. }
  3065. }
  3066. }
  3067. int DiffTimebWithNowSec(struct timeb ST)
  3068. {
  3069. //return milli-second
  3070. struct timeb ET;
  3071. unsigned int StartTime,StopTime;
  3072. ftime(&ET);
  3073. StartTime=(unsigned int)ST.time;
  3074. StopTime=(unsigned int)ET.time;
  3075. return (StopTime-StartTime);
  3076. }
  3077. int CheckTimeOut(char *start)
  3078. {
  3079. int result = YES;
  3080. struct ParsingResult
  3081. {
  3082. int result;
  3083. int scanedElement;
  3084. int year;
  3085. int month;
  3086. int mday;
  3087. int hour;
  3088. int min;
  3089. int sec;
  3090. int tz_hour;
  3091. int tz_min;
  3092. float minSec;
  3093. }parsingResult;
  3094. struct tm tmStart;
  3095. struct timeb tbStart;
  3096. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  3097. if(strstr((char*)start, ".") != NULL)
  3098. {
  3099. // Original data with mini second
  3100. if(strstr((char*)start, "Z") != NULL)
  3101. {
  3102. // Original data with Z
  3103. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  3104. &parsingResult.year,
  3105. &parsingResult.month,
  3106. &parsingResult.mday,
  3107. &parsingResult.hour,
  3108. &parsingResult.min,
  3109. &parsingResult.sec,
  3110. &parsingResult.minSec);
  3111. }
  3112. else
  3113. {
  3114. // Original data without Z
  3115. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  3116. &parsingResult.year,
  3117. &parsingResult.month,
  3118. &parsingResult.mday,
  3119. &parsingResult.hour,
  3120. &parsingResult.min,
  3121. &parsingResult.sec,
  3122. &parsingResult.minSec,
  3123. &parsingResult.tz_hour,
  3124. &parsingResult.tz_min);
  3125. }
  3126. }
  3127. else
  3128. {
  3129. // Original data without mini second
  3130. if(strstr((char*)start, "Z") != NULL)
  3131. {
  3132. // Original data with Z
  3133. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  3134. &parsingResult.year,
  3135. &parsingResult.month,
  3136. &parsingResult.mday,
  3137. &parsingResult.hour,
  3138. &parsingResult.min,
  3139. &parsingResult.sec);
  3140. }
  3141. else
  3142. {
  3143. // Original data without Z
  3144. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  3145. &parsingResult.year,
  3146. &parsingResult.month,
  3147. &parsingResult.mday,
  3148. &parsingResult.hour,
  3149. &parsingResult.min,
  3150. &parsingResult.sec,
  3151. &parsingResult.tz_hour,
  3152. &parsingResult.tz_min);
  3153. }
  3154. }
  3155. if(parsingResult.scanedElement >= 6)
  3156. {
  3157. tmStart.tm_year = parsingResult.year - 1900;
  3158. tmStart.tm_mon = parsingResult.month - 1;
  3159. tmStart.tm_mday = parsingResult.mday;
  3160. tmStart.tm_hour = parsingResult.hour;
  3161. tmStart.tm_min = parsingResult.min;
  3162. tmStart.tm_sec = parsingResult.sec;
  3163. tmStart.tm_gmtoff = 0;
  3164. tbStart.time = mktime(&tmStart);
  3165. tbStart.timezone = 0;
  3166. tbStart.millitm = 0;
  3167. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  3168. if(DiffTimebWithNowSec(tbStart) <= 0)
  3169. result = NO;
  3170. else
  3171. result = YES;
  3172. }
  3173. else
  3174. {
  3175. LOG_INFO("Start date parsing error");
  3176. }
  3177. return result;
  3178. }
  3179. bool Is_WebServer_Alive(void)
  3180. {
  3181. if(system("pidof -s lighttpd > /dev/null") != 0)
  3182. {
  3183. return false;
  3184. }
  3185. return true;
  3186. }
  3187. void Run_WebServer_Service(void)
  3188. {
  3189. system("/root/web.sh");
  3190. }
  3191. bool Is_LocalPowerSharing_Alive(void)
  3192. {
  3193. if(system("pidof -s Module_PowerSharing > /dev/null") != 0)
  3194. {
  3195. return false;
  3196. }
  3197. return true;
  3198. }
  3199. void Run_LocalPowerSharing_Service(void)
  3200. {
  3201. system ("/root/Module_PowerSharing &");
  3202. }
  3203. //===============================================
  3204. // Ocpp_Service
  3205. //===============================================
  3206. void Run_Ocpp_Service(void)
  3207. {
  3208. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3209. {
  3210. system("/root/OcppBackend &");
  3211. return;
  3212. }
  3213. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3214. {
  3215. system("/root/OcppBackend20 &");
  3216. return;
  3217. }
  3218. }
  3219. bool Is_Ocpp_Task_Alive(void)
  3220. {
  3221. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3222. {
  3223. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3224. {
  3225. return false;
  3226. }
  3227. return true;
  3228. }
  3229. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3230. {
  3231. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  3232. {
  3233. return false;
  3234. }
  3235. return true;
  3236. }
  3237. return false;
  3238. }
  3239. void Kill_Ocpp_Service(void)
  3240. {
  3241. system("killall OcppBackend");
  3242. system("killall OcppBackend20");
  3243. }
  3244. //===============================================
  3245. // Maintain Ocpp_Service
  3246. //===============================================
  3247. void Run_MaintainOcpp_Service(void)
  3248. {
  3249. system("/root/OcppBackendPH &");
  3250. }
  3251. bool Is_MaintainOcpp_Task_Alive(void)
  3252. {
  3253. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  3254. {
  3255. return false;
  3256. }
  3257. return true;
  3258. }
  3259. void Kill_MaintainOcpp_Service(void)
  3260. {
  3261. system("killall OcppBackendPH");
  3262. }
  3263. //===============================================
  3264. // Ocpp OcppConnStatus
  3265. //===============================================
  3266. bool Get_Ocpp_ConnectionStatus(void)
  3267. {
  3268. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3269. {
  3270. return ShmOCPP16Data->OcppConnStatus > 0 ? true : false;
  3271. }
  3272. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3273. {
  3274. return ShmOCPP20Data->OcppConnStatus > 0 ? true : false;
  3275. }
  3276. return false;
  3277. }
  3278. //===============================================
  3279. // Ocpp procDogTime
  3280. //===============================================
  3281. long int Get_Ocpp_procDogTime(void)
  3282. {
  3283. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3284. {
  3285. return ShmOCPP16Data->procDogTime;
  3286. }
  3287. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3288. {
  3289. return ShmOCPP20Data->procDogTime;
  3290. }
  3291. return 0;
  3292. }
  3293. void Clean_Ocpp_procDogTime(void)
  3294. {
  3295. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3296. {
  3297. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  3298. return;
  3299. }
  3300. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3301. {
  3302. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  3303. return;
  3304. }
  3305. }
  3306. //===============================================
  3307. // Ocpp Boot Notification
  3308. //===============================================
  3309. bool Is_Ocpp_BootNotification(void)
  3310. {
  3311. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3312. {
  3313. return ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  3314. }
  3315. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3316. {
  3317. return ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  3318. }
  3319. return false;
  3320. }
  3321. void Clean_Ocpp_BootNotification(void)
  3322. {
  3323. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3324. {
  3325. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = false;
  3326. return;
  3327. }
  3328. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3329. {
  3330. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = false;
  3331. return;
  3332. }
  3333. }
  3334. //===============================================
  3335. // Ocpp Rest
  3336. //===============================================
  3337. bool Is_Ocpp_ResetReq(void)
  3338. {
  3339. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3340. {
  3341. return ShmOCPP16Data->MsMsg.bits.ResetReq == YES ? true : false;
  3342. }
  3343. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3344. {
  3345. return ShmOCPP20Data->MsMsg.bits.ResetReq == YES ? true : false;
  3346. }
  3347. return false;
  3348. }
  3349. bool Chk_Ocpp_Reset_Type(char *type)
  3350. {
  3351. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3352. {
  3353. return strcmp((char *)ShmOCPP16Data->Reset.Type, type) == EQUAL ? true : false;
  3354. }
  3355. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3356. {
  3357. return strcmp((char *)ShmOCPP20Data->Reset.type, type) == EQUAL ? true : false;
  3358. }
  3359. return false;
  3360. }
  3361. void Clean_Ocpp_ResetReq(void)
  3362. {
  3363. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3364. {
  3365. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3366. ShmOCPP16Data->MsMsg.bits.ResetReq = false;
  3367. ShmOCPP16Data->MsMsg.bits.ResetConf = true;
  3368. return;
  3369. }
  3370. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3371. {
  3372. sprintf ( (char*) ShmOCPP20Data->Reset.Response_status, "Accepted" );
  3373. ShmOCPP20Data->MsMsg.bits.ResetReq = false;
  3374. ShmOCPP20Data->MsMsg.bits.ResetConf = true;
  3375. return;
  3376. }
  3377. }
  3378. //===============================================
  3379. // Ocpp AuthorizeReq
  3380. //===============================================
  3381. void Set_Ocpp_AuthorizeReq(void)
  3382. {
  3383. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3384. {
  3385. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  3386. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  3387. return;
  3388. }
  3389. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3390. {
  3391. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = false;
  3392. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = true;
  3393. return;
  3394. }
  3395. }
  3396. bool Is_Ocpp_AuthorizeConf(void)
  3397. {
  3398. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3399. {
  3400. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3401. }
  3402. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3403. {
  3404. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3405. }
  3406. return false;
  3407. }
  3408. bool Is_Ocpp_Authorize_Status(char *status)
  3409. {
  3410. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3411. {
  3412. return strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  3413. }
  3414. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3415. {
  3416. return strcmp((char *)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, status) == EQUAL ? true : false;
  3417. }
  3418. return false;
  3419. }
  3420. //===============================================
  3421. // Ocpp UpdateFirmwareReq
  3422. //===============================================
  3423. bool Is_Ocpp_UpdateFirmwareReq(void)
  3424. {
  3425. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3426. {
  3427. return ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3428. }
  3429. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3430. {
  3431. return ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3432. }
  3433. return false;
  3434. }
  3435. void Clean_Ocpp_UpdateFirmwareReq(void)
  3436. {
  3437. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3438. {
  3439. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = false;
  3440. return;
  3441. }
  3442. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3443. {
  3444. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = false;
  3445. return;
  3446. }
  3447. }
  3448. bool Is_Ocpp_UpdateFirmwareStatus(char *status)
  3449. {
  3450. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3451. {
  3452. return strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status) == EQUAL ? true : false;
  3453. }
  3454. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3455. {
  3456. return strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status) == EQUAL ? true : false;
  3457. }
  3458. return false;
  3459. }
  3460. void Set_Ocpp_UpdateFirmwareStatus(char *status)
  3461. {
  3462. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3463. {
  3464. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status);
  3465. return;
  3466. }
  3467. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3468. {
  3469. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status);
  3470. return;
  3471. }
  3472. }
  3473. void Clean_Ocpp_UpdateFirmwareStatus(char *status)
  3474. {
  3475. Set_Ocpp_UpdateFirmwareStatus("");
  3476. }
  3477. void Set_Ocpp_FirmwareStatusNotificationReq(void)
  3478. {
  3479. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3480. {
  3481. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3482. return;
  3483. }
  3484. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3485. {
  3486. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3487. return;
  3488. }
  3489. }
  3490. //===============================================
  3491. // Ocpp StopTransactionOnInvalidId
  3492. //===============================================
  3493. bool Is_Ocpp_StopOnInvalidId_Enable(void)
  3494. {
  3495. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3496. {
  3497. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE") > 0 ? true : false;
  3498. }
  3499. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3500. {
  3501. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3502. }
  3503. return false;
  3504. }
  3505. //===============================================
  3506. // Ocpp AuthorizeRemoteTxRequests
  3507. //===============================================
  3508. bool Is_Ocpp_AuthorizeRemoteTxRequests(void)
  3509. {
  3510. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3511. {
  3512. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE") > 0 ? true : false;
  3513. }
  3514. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3515. {
  3516. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3517. }
  3518. return false;
  3519. }
  3520. //===============================================
  3521. // Ocpp ConnectionTimeOut
  3522. //===============================================
  3523. int Get_Ocpp_ConnectionTimeOut(void)
  3524. {
  3525. int timeout = 0;
  3526. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3527. {
  3528. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData, "") != 0)
  3529. {
  3530. timeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3531. }
  3532. }
  3533. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3534. {
  3535. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value, "" ) != 0)
  3536. {
  3537. timeout = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  3538. }
  3539. }
  3540. return timeout > 0 ? timeout : 0;
  3541. }
  3542. //===============================================
  3543. // Ocpp DefaultPrice
  3544. //===============================================
  3545. void Get_Ocpp_DefaultPrice(char *defaultPrice)
  3546. {
  3547. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3548. {
  3549. strcpy(defaultPrice, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  3550. return;
  3551. }
  3552. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3553. {
  3554. strcpy(defaultPrice, "");
  3555. return;
  3556. }
  3557. strcpy(defaultPrice, "");
  3558. }
  3559. //===============================================
  3560. // Ocpp UserPrice
  3561. //===============================================
  3562. void Get_Ocpp_UserPrice(char *userId, char *userPrice)
  3563. {
  3564. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3565. {
  3566. strcpy(userId, (char *)ShmOCPP16Data->Cost.SetUserPrice.idToken);
  3567. strcpy(userPrice, (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  3568. return;
  3569. }
  3570. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3571. {
  3572. strcpy(userId, "");
  3573. strcpy(userPrice, "");
  3574. return;
  3575. }
  3576. strcpy(userId, "");
  3577. strcpy(userPrice, "");
  3578. }
  3579. void Clean_Ocpp_UserPrice(void)
  3580. {
  3581. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3582. {
  3583. memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  3584. return;
  3585. }
  3586. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3587. {
  3588. return;
  3589. }
  3590. }
  3591. //===============================================
  3592. // Ocpp RunningCost
  3593. //===============================================
  3594. void Get_Ocpp_RunningCost(int gun_index, char *cost)
  3595. {
  3596. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3597. {
  3598. strcpy(cost, (char *)ShmOCPP16Data->Cost.RunningCost[gun_index].description);
  3599. return;
  3600. }
  3601. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3602. {
  3603. strcpy(cost, (char *)"");
  3604. return;
  3605. }
  3606. }
  3607. void Clean_Ocpp_RunningCost(int gun_index)
  3608. {
  3609. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3610. {
  3611. memset(&ShmOCPP16Data->Cost.RunningCost[gun_index], 0x00, sizeof(struct StrcutRunningCost));
  3612. return;
  3613. }
  3614. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3615. {
  3616. return;
  3617. }
  3618. }
  3619. //===============================================
  3620. // Ocpp FinalCost
  3621. //===============================================
  3622. void Get_Ocpp_FinalCost(int gun_index, char *cost)
  3623. {
  3624. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3625. {
  3626. strcpy(cost, (char *)ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  3627. return;
  3628. }
  3629. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3630. {
  3631. strcpy(cost, (char *)"");
  3632. return;
  3633. }
  3634. }
  3635. void Clean_Ocpp_FinalCost(int gun_index)
  3636. {
  3637. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3638. {
  3639. memset(&ShmOCPP16Data->Cost.FinalCost[gun_index], 0x00, sizeof(struct StrcutFinalCost));
  3640. return;
  3641. }
  3642. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3643. {
  3644. return;
  3645. }
  3646. }
  3647. int Get_Ocpp_FinalCostTxId(int gun_index)
  3648. {
  3649. int txId = 0;
  3650. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3651. {
  3652. txId = ShmOCPP16Data->Cost.FinalCost[gun_index].txId;
  3653. }
  3654. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3655. {
  3656. }
  3657. return txId;
  3658. }
  3659. //===============================================
  3660. // Ocpp TimeOffset
  3661. //===============================================
  3662. void Get_Ocpp_TimeOffset(char *timeOffset)
  3663. {
  3664. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3665. {
  3666. strcpy(timeOffset, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[TimeOffset].ItemData);
  3667. return;
  3668. }
  3669. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3670. {
  3671. strcpy(timeOffset, "");
  3672. return;
  3673. }
  3674. strcpy(timeOffset, "");
  3675. }
  3676. //===============================================
  3677. // Ocpp PreAuthAmount
  3678. //===============================================
  3679. void Get_Ocpp_PreAuthAmount(char *amount)
  3680. {
  3681. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3682. {
  3683. strcpy(amount, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PreAuthAmount].ItemData);
  3684. return;
  3685. }
  3686. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3687. {
  3688. strcpy(amount, "");
  3689. return;
  3690. }
  3691. strcpy(amount, "");
  3692. }
  3693. //===============================================
  3694. // Ocpp PowerSharingServerIP
  3695. //===============================================
  3696. void Get_Ocpp_PowerSharingServerIP(char *ip)
  3697. {
  3698. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3699. {
  3700. strcpy(ip, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PowerSharingServerIP].ItemData);
  3701. return;
  3702. }
  3703. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3704. {
  3705. strcpy(ip, "");
  3706. return;
  3707. }
  3708. strcpy(ip, "");
  3709. }
  3710. //===============================================
  3711. // Ocpp Refunding
  3712. //===============================================
  3713. void Get_Ocpp_Refunding(char *Refunding)
  3714. {
  3715. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3716. {
  3717. return;
  3718. }
  3719. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3720. {
  3721. return;
  3722. }
  3723. }
  3724. void Clean_Ocpp_Refunding(void)
  3725. {
  3726. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3727. {
  3728. return;
  3729. }
  3730. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3731. {
  3732. return;
  3733. }
  3734. }
  3735. //===============================================
  3736. // Ocpp LcdOveride
  3737. //===============================================
  3738. bool Is_Ocpp_LcdOverideReq(int *gun, int *page)
  3739. {
  3740. *gun = ShmSysConfigAndInfo->SysInfo.LcdOveride.connectorId;
  3741. *page = ShmSysConfigAndInfo->SysInfo.LcdOveride.page_index;
  3742. return ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq ? true : false;
  3743. }
  3744. void Clean_Ocpp_LcdOverideReq(void)
  3745. {
  3746. ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq = false;
  3747. }
  3748. //===============================================
  3749. // Ocpp StationInfo
  3750. //===============================================
  3751. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name)
  3752. {
  3753. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3754. {
  3755. strcpy(name, (char *)ShmOCPP16Data->TcciCustomData.ChargerInfo.station_name);
  3756. }
  3757. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3758. {
  3759. }
  3760. }
  3761. bool Is_Ocpp_ChargerStationReq(void)
  3762. {
  3763. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3764. {
  3765. return ShmOCPP16Data->TcciCustomData.ChargerInfoReq ? true : false;
  3766. }
  3767. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3768. {
  3769. return false;
  3770. }
  3771. return false;
  3772. }
  3773. void Clean_Ocpp_ChargerStationReq(void)
  3774. {
  3775. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3776. {
  3777. ShmOCPP16Data->TcciCustomData.ChargerInfoReq = false;
  3778. }
  3779. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3780. {
  3781. }
  3782. }
  3783. //===============================================
  3784. // Ocpp WeatherInfo
  3785. //===============================================
  3786. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature)
  3787. {
  3788. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3789. {
  3790. *weatherID = ShmOCPP16Data->TcciCustomData.WeatherInfo.weatherId;
  3791. *temperature = ShmOCPP16Data->TcciCustomData.WeatherInfo.temperature;
  3792. }
  3793. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3794. {
  3795. }
  3796. }
  3797. bool Is_Ocpp_WeatherReq(void)
  3798. {
  3799. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3800. {
  3801. return ShmOCPP16Data->TcciCustomData.WeatherInfoReq ? true : false;
  3802. }
  3803. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3804. {
  3805. return false;
  3806. }
  3807. return false;
  3808. }
  3809. void Clean_Ocpp_WeatherReq(void)
  3810. {
  3811. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3812. {
  3813. ShmOCPP16Data->TcciCustomData.WeatherInfoReq = false;
  3814. }
  3815. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3816. {
  3817. }
  3818. }
  3819. //===============================================
  3820. // Ocpp DeductInfo
  3821. //===============================================
  3822. void Set_Ocpp_DeductTransaction(int transaction)
  3823. {
  3824. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3825. {
  3826. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = transaction;
  3827. }
  3828. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3829. {
  3830. }
  3831. }
  3832. void Set_Ocpp_DeductCreditNo(char *userId)
  3833. {
  3834. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3835. {
  3836. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, userId, strlen(userId));
  3837. }
  3838. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3839. {
  3840. }
  3841. }
  3842. void Set_Ocpp_DeductVemData(char *vemData)
  3843. {
  3844. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3845. {
  3846. strcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, vemData);
  3847. }
  3848. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3849. {
  3850. }
  3851. }
  3852. void Set_Ocpp_DeductAmount(double value)
  3853. {
  3854. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3855. {
  3856. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = value;
  3857. }
  3858. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3859. {
  3860. }
  3861. }
  3862. void Set_Ocpp_DeductResult(unsigned char result)
  3863. {
  3864. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3865. {
  3866. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = result;
  3867. }
  3868. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3869. {
  3870. }
  3871. }
  3872. void Set_Ocpp_DonateInvoice(unsigned char donate)
  3873. {
  3874. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3875. {
  3876. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = donate ? YES : NO;
  3877. }
  3878. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3879. {
  3880. }
  3881. }
  3882. bool Set_Ocpp_DeductInfo(DeductResultInfoData *deductInfo)
  3883. {
  3884. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3885. {
  3886. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = deductInfo->TransactionId;
  3887. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, (char *)deductInfo->creditNo, sizeof(deductInfo->creditNo));
  3888. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, deductInfo->VemData, sizeof(deductInfo->VemData));
  3889. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = ((double)deductInfo->DeductAmount / 100);
  3890. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.approvalNo, deductInfo->ApprovalNumber, sizeof(deductInfo->ApprovalNumber));
  3891. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = deductInfo->DeductResult;
  3892. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = deductInfo->IsDonateInvoice ? YES : NO;
  3893. return true;
  3894. }
  3895. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3896. {
  3897. }
  3898. return false;
  3899. }
  3900. bool Is_Ocpp_ReportCreditDeductReq(void)
  3901. {
  3902. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3903. {
  3904. return ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq ? true : false;
  3905. }
  3906. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3907. {
  3908. return false;
  3909. }
  3910. return false;
  3911. }
  3912. void Set_Ocpp_ReportCreditDeductReq(void)
  3913. {
  3914. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3915. {
  3916. ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq = YES;
  3917. }
  3918. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3919. {
  3920. }
  3921. }
  3922. //===============================================
  3923. // Ocpp Tcci SerialNo
  3924. //===============================================
  3925. void Clean_Ocpp_TcciSerialNo(int gun_index)
  3926. {
  3927. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3928. {
  3929. memset((char *)&ShmOCPP16Data->TcciCustomData.SerialNo[gun_index], 0x00, sizeof(37));
  3930. }
  3931. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3932. {
  3933. }
  3934. }
  3935. //===============================================
  3936. // Ocpp Tcci ReaderStatus
  3937. //===============================================
  3938. bool Set_Ocpp_TcciReaderStatus(int gun_index, DeductResultInfoData *ReaderStatus)
  3939. {
  3940. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3941. {
  3942. if(ReaderStatus->DeductResult == _Deduct_PreAuth_Cancel || ReaderStatus->DeductResult == _Deduct_PreAuth || ReaderStatus->DeductResult == _Deduct_PreAuth_Pass)
  3943. {
  3944. switch(ReaderStatus->DeductResult)
  3945. {
  3946. case _Deduct_PreAuth_Pass:
  3947. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 1;
  3948. break;
  3949. case _Deduct_PreAuth_Cancel:
  3950. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 2;
  3951. break;
  3952. case _Deduct_PreAuth:
  3953. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 0;
  3954. break;
  3955. }
  3956. memcpy((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, ReaderStatus->creditNo, sizeof(ReaderStatus->creditNo));
  3957. memcpy((char *)&ShmOCPP16Data->TcciCustomData.VEMData[gun_index], ReaderStatus->VemData, sizeof(ReaderStatus->VemData));
  3958. return true;
  3959. }
  3960. }
  3961. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3962. {
  3963. }
  3964. return false;
  3965. }
  3966. void Clean_Ocpp_TcciCreditInfo(int gun_index)
  3967. {
  3968. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3969. {
  3970. memset((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo));
  3971. memset((char *)&ShmOCPP16Data->TcciCustomData.VEMData[gun_index], 0x00, sizeof(ShmOCPP16Data->TcciCustomData.VEMData[gun_index]));
  3972. }
  3973. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3974. {
  3975. }
  3976. }
  3977. void Set_Ocpp_TcciReaderStatusReq(int gun_index)
  3978. {
  3979. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3980. {
  3981. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq = true;
  3982. }
  3983. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3984. {
  3985. }
  3986. }
  3987. bool Is_Ocpp_TcciReaderStatusReq(int gun_index)
  3988. {
  3989. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3990. {
  3991. return ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq ? true : false;
  3992. }
  3993. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3994. {
  3995. return false;
  3996. }
  3997. return false;
  3998. }
  3999. //===============================================
  4000. // Ocpp Tcci OccupancyFeeDisplay
  4001. //===============================================
  4002. bool Is_Ocpp_TcciOccupancyDisplayReq(int gun_index)
  4003. {
  4004. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4005. {
  4006. return ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].OccupancyFeeDisplayReq ? true : false;
  4007. }
  4008. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4009. {
  4010. return false;
  4011. }
  4012. return false;
  4013. }
  4014. void Clean_Ocpp_TcciOccupancyDisplayReq(int gun_index)
  4015. {
  4016. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4017. {
  4018. ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].OccupancyFeeDisplayReq = false;
  4019. }
  4020. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4021. {
  4022. }
  4023. }
  4024. // input: gun_index, sn_len
  4025. // output: sn
  4026. // return: action
  4027. int Get_Ocpp_TcciOccupancyAction(int gun_index, int sn_len, char *sn)
  4028. {
  4029. int action = 0;
  4030. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4031. {
  4032. action = ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].actionId;
  4033. if(action == _ParkingAction_DisplayButton)
  4034. {
  4035. memcpy(sn, ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN, sn_len);
  4036. }
  4037. }
  4038. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4039. {
  4040. }
  4041. return action;
  4042. }
  4043. void Clean_Ocpp_TcciOccupancyActionAndSn(int gun_index)
  4044. {
  4045. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4046. {
  4047. ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].actionId = 0;
  4048. memset(ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN));
  4049. }
  4050. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4051. {
  4052. }
  4053. }
  4054. //===============================================
  4055. // Ocpp Tcci SetOccupancyPrice
  4056. //===============================================
  4057. bool Is_Ocpp_TcciSetOccupancyPriceReq(void)
  4058. {
  4059. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4060. {
  4061. return ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.SetOccupancyPriceReq ? true : false;
  4062. }
  4063. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4064. {
  4065. return false;
  4066. }
  4067. return false;
  4068. }
  4069. void Clean_Ocpp_TcciSetOccupancyPriceReq(void)
  4070. {
  4071. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4072. {
  4073. ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.SetOccupancyPriceReq = false;
  4074. }
  4075. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4076. {
  4077. }
  4078. }
  4079. float Get_Ocpp_TcciOccupancyPrice(void)
  4080. {
  4081. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4082. {
  4083. return ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.price;
  4084. }
  4085. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4086. {
  4087. }
  4088. return 0;
  4089. }
  4090. //===============================================
  4091. // Ocpp Tcci GetOccupancyFee
  4092. //===============================================
  4093. bool Is_Ocpp_TcciOccupancyFeeActReq(int gun_index)
  4094. {
  4095. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4096. {
  4097. return ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq ? true : false;
  4098. }
  4099. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4100. {
  4101. return false;
  4102. }
  4103. return false;
  4104. }
  4105. void Set_Ocpp_TcciOccupancyFeeAct(int gun_index, OccupancyBillInfo *bill, int action)
  4106. {
  4107. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4108. {
  4109. memset(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN));
  4110. memcpy(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN, bill->Sn, sizeof(bill->Sn));
  4111. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].action = action;
  4112. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq = true;
  4113. }
  4114. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4115. {
  4116. }
  4117. }
  4118. void Clean_Ocpp_TcciNotifyOccupancyFeeActReq(int gun_index)
  4119. {
  4120. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4121. {
  4122. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].action = _Notify_None;
  4123. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq = false;
  4124. }
  4125. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4126. {
  4127. }
  4128. }
  4129. void Get_Ocpp_TcciGetOccupancyFeeResponse(int gun_index, OccupancyBillInfo *bill)
  4130. {
  4131. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4132. {
  4133. bill->Duration = ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_duration;
  4134. bill->Fee = ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_occupancyFee;
  4135. memcpy(bill->StartTime, ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_startTime, sizeof(bill->StartTime));
  4136. }
  4137. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4138. {
  4139. }
  4140. }
  4141. bool Is_Ocpp_TcciOccupancyFeeActStatus(int gun_index, char *status)
  4142. {
  4143. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4144. {
  4145. return strcmp((char *)ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_status, status) == EQUAL ? true : false;
  4146. }
  4147. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4148. {
  4149. }
  4150. return false;
  4151. }
  4152. //===============================================
  4153. // Ocpp Tcci ReportOccupancyDeductReq
  4154. //===============================================
  4155. bool Is_Ocpp_TcciReportOccupancyDeductReq(void)
  4156. {
  4157. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4158. {
  4159. return ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq ? true : false;
  4160. }
  4161. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4162. {
  4163. return false;
  4164. }
  4165. return false;
  4166. }
  4167. void Set_Ocpp_TcciReportOccupancyDeductReq(void)
  4168. {
  4169. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4170. {
  4171. ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq = true;
  4172. }
  4173. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4174. {
  4175. }
  4176. }
  4177. //===============================================
  4178. // Ocpp Tcci OccupancyDeductResult
  4179. //===============================================
  4180. bool Set_Ocpp_OccupancyDeductResult(OccupancyDeductInfo *deduct, bool pass_fail)
  4181. {
  4182. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4183. {
  4184. memset(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult, 0x00, sizeof(struct StructOccupancyDeductResult));
  4185. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.deductResult = pass_fail;
  4186. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.amount = deduct->Fee;
  4187. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.occupancySN[0], &deduct->Sn[0], sizeof(deduct->Sn));
  4188. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.creditNo[0], &deduct->creditNo[0], sizeof(deduct->creditNo));
  4189. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.approvalNo[0], &deduct->ApprovalNumber[0], sizeof(deduct->ApprovalNumber));
  4190. return true;
  4191. }
  4192. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4193. {
  4194. }
  4195. return false;
  4196. }
  4197. //===============================================
  4198. // Ocpp Start Transaction
  4199. //===============================================
  4200. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id)
  4201. {
  4202. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4203. {
  4204. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag, id);
  4205. }
  4206. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4207. {
  4208. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4209. }
  4210. // MaintainServer
  4211. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4212. {
  4213. strcpy((char *)ShmOCPP16DataPH->StartTransaction[gun_index].IdTag, id);
  4214. }
  4215. }
  4216. void Set_Ocpp_StartTransactionReq(int gun_index)
  4217. {
  4218. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4219. {
  4220. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = true;
  4221. }
  4222. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4223. {
  4224. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4225. }
  4226. // MaintainServer
  4227. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4228. {
  4229. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq = true;
  4230. }
  4231. }
  4232. bool Is_Ocpp_StartTransactionConf(int gun_index)
  4233. {
  4234. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4235. {
  4236. return ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == YES ? true : false;
  4237. }
  4238. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4239. {
  4240. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf == YES)
  4241. {
  4242. if(strstr((char*)ShmOCPP20Data->TransactionEvent[gun_index].eventType, "Started") != NULL)
  4243. {
  4244. return true;
  4245. }
  4246. else
  4247. {
  4248. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  4249. return false;
  4250. }
  4251. }
  4252. else
  4253. {
  4254. return false;
  4255. }
  4256. }
  4257. return false;
  4258. }
  4259. void Clean_Ocpp_StartTransactionConf(int gun_index)
  4260. {
  4261. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4262. {
  4263. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = false;
  4264. }
  4265. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4266. {
  4267. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  4268. }
  4269. // MaintainServer
  4270. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4271. {
  4272. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf = false;
  4273. }
  4274. }
  4275. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4276. {
  4277. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4278. {
  4279. return strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  4280. }
  4281. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4282. {
  4283. return strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, status) == EQUAL ? true : false;
  4284. }
  4285. return false;
  4286. }
  4287. bool Is_Ocpp_StartTransaction_Accepted(int gun_index)
  4288. {
  4289. return Is_Ocpp_StartTransaction_Status(gun_index, "Accepted");
  4290. }
  4291. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4292. {
  4293. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4294. {
  4295. strcpy(status, (char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status);
  4296. }
  4297. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4298. {
  4299. strcpy(status, (char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status);
  4300. }
  4301. }
  4302. void Get_Ocpp_TransactionId(int gun_index, char *transactionId)
  4303. {
  4304. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4305. {
  4306. sprintf(transactionId, "%d", ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId);
  4307. return;
  4308. }
  4309. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4310. {
  4311. strcpy(transactionId, (char *)&ShmOCPP20Data->TransactionEvent [gun_index].transactionInfo.transactionId[0]);
  4312. return;
  4313. }
  4314. sprintf(transactionId, "%d", 0);
  4315. }
  4316. bool Is_Invalid_StartTransactionId(int gun_index)
  4317. {
  4318. if(Is_Ocpp_StartTransaction_Status(gun_index, "Blocked") ||
  4319. Is_Ocpp_StartTransaction_Status(gun_index, "Expired") ||
  4320. Is_Ocpp_StartTransaction_Status(gun_index, "Invalid") ||
  4321. Is_Ocpp_StartTransaction_Status(gun_index, "NoCredit") ||
  4322. Is_Ocpp_StartTransaction_Status(gun_index, "NotAllowedTypeEVSE") ||
  4323. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisLocation") ||
  4324. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisTime") ||
  4325. Is_Ocpp_StartTransaction_Status(gun_index, "Unknown"))
  4326. {
  4327. return true;
  4328. }
  4329. return false;
  4330. }
  4331. //===============================================
  4332. // Ocpp Stop Transaction
  4333. //===============================================
  4334. void Set_Ocpp_StopTransactionId(int gun_index, char *id)
  4335. {
  4336. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4337. {
  4338. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].IdTag, id);
  4339. return;
  4340. }
  4341. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4342. {
  4343. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4344. return;
  4345. }
  4346. }
  4347. void Set_Ocpp_StopTransactionReq(int gun_index)
  4348. {
  4349. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4350. {
  4351. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = true;
  4352. }
  4353. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4354. {
  4355. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4356. }
  4357. // MaintainServer
  4358. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4359. {
  4360. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = true;
  4361. }
  4362. }
  4363. //===============================================
  4364. // Ocpp Check Backup Error Code
  4365. //===============================================
  4366. void Chk_Ocpp_Backup_ErrorCode(int gun_index)
  4367. {
  4368. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4369. {
  4370. if(strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, "") == EQUAL)
  4371. {
  4372. if(strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, "") != EQUAL)
  4373. {
  4374. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, (char *)_BackupStatusNotification[gun_index].ErrorCode);
  4375. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, (char *)_BackupStatusNotification[gun_index].VendorErrorCode);
  4376. }
  4377. }
  4378. }
  4379. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4380. {
  4381. }
  4382. }
  4383. //===============================================
  4384. // Ocpp Reserve Now
  4385. //===============================================
  4386. bool Is_Ocpp_ReserveNowReq(int gun_index)
  4387. {
  4388. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4389. {
  4390. return ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4391. }
  4392. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4393. {
  4394. return ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4395. }
  4396. return false;
  4397. }
  4398. void Clean_Ocpp_ReserveNowReq(int gun_index)
  4399. {
  4400. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4401. {
  4402. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4403. return;
  4404. }
  4405. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4406. {
  4407. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4408. return;
  4409. }
  4410. }
  4411. int Get_Ocpp_ReserveNowId(int gun_index)
  4412. {
  4413. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4414. {
  4415. return ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  4416. }
  4417. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4418. {
  4419. return ShmOCPP20Data->ReserveNow[gun_index].id;
  4420. }
  4421. return 0;
  4422. }
  4423. void Set_Ocpp_ReserveNowConf(int gun_index)
  4424. {
  4425. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4426. {
  4427. ShmOCPP16Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4428. return;
  4429. }
  4430. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4431. {
  4432. ShmOCPP20Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4433. return;
  4434. }
  4435. }
  4436. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date)
  4437. {
  4438. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4439. {
  4440. strcpy(date, (char *)ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate);
  4441. return;
  4442. }
  4443. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4444. {
  4445. strcpy(date, (char *)ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime);
  4446. return;
  4447. }
  4448. strcpy(date, "");
  4449. return;
  4450. }
  4451. bool Is_Ocpp_ReserveNowExpired(int gun_index)
  4452. {
  4453. bool _expired = true;
  4454. char _expiryDate[128];
  4455. Get_Ocpp_ReserveNowExpiryDate(gun_index, _expiryDate);
  4456. _expired = CheckTimeOut(_expiryDate);
  4457. return _expired;
  4458. }
  4459. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag)
  4460. {
  4461. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4462. {
  4463. strcpy(idTag, (char *)ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  4464. return;
  4465. }
  4466. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4467. {
  4468. strcpy(idTag, (char *)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken);
  4469. return;
  4470. }
  4471. strcpy(idTag, "");
  4472. return;
  4473. }
  4474. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag)
  4475. {
  4476. char ReserveIdTag[32];
  4477. Get_Ocpp_ReserveNowIdTag(gun_index, ReserveIdTag);
  4478. return strcmp(ReserveIdTag, authIdTag) == EQUAL ? true : false;
  4479. }
  4480. //===============================================
  4481. // Ocpp Cancel Reservation
  4482. //===============================================
  4483. bool Is_Ocpp_CancelReservationReq(int gun_index)
  4484. {
  4485. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4486. {
  4487. return ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4488. }
  4489. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4490. {
  4491. return ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4492. }
  4493. return false;
  4494. }
  4495. void Clean_Ocpp_CancelReservationReq(int gun_index)
  4496. {
  4497. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4498. {
  4499. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4500. return;
  4501. }
  4502. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4503. {
  4504. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4505. return;
  4506. }
  4507. }
  4508. void Set_Ocpp_CancelReservationConf(int gun_index)
  4509. {
  4510. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4511. {
  4512. ShmOCPP16Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4513. return;
  4514. }
  4515. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4516. {
  4517. ShmOCPP20Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4518. return;
  4519. }
  4520. }
  4521. //===============================================
  4522. // Ocpp Change Availability
  4523. //===============================================
  4524. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index)
  4525. {
  4526. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4527. {
  4528. return ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4529. }
  4530. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4531. {
  4532. return ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4533. }
  4534. return false;
  4535. }
  4536. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index)
  4537. {
  4538. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4539. {
  4540. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4541. return;
  4542. }
  4543. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4544. {
  4545. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4546. return;
  4547. }
  4548. }
  4549. bool Chk_Ocpp_Availability_Type(int gun_index, char *type)
  4550. {
  4551. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4552. {
  4553. return strcmp((char *)ShmOCPP16Data->ChangeAvailability[gun_index].Type, type) == EQUAL ? true : false;
  4554. }
  4555. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4556. {
  4557. return strcmp((char *)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, type) == EQUAL ? true : false;
  4558. }
  4559. return false;
  4560. }
  4561. //===============================================
  4562. // Ocpp Unlock Connector
  4563. //===============================================
  4564. bool Is_Ocpp_UnlockConnectorReq(int gun_index)
  4565. {
  4566. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4567. {
  4568. return ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4569. }
  4570. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4571. {
  4572. return ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4573. }
  4574. return false;
  4575. }
  4576. void Clean_Ocpp_UnlockConnectorReq(int gun_index)
  4577. {
  4578. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4579. {
  4580. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4581. return;
  4582. }
  4583. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4584. {
  4585. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4586. return;
  4587. }
  4588. }
  4589. void Set_Ocpp_UnlockConnectorConf(int gun_index)
  4590. {
  4591. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4592. {
  4593. strcpy((char *)ShmOCPP16Data->UnlockConnector[gun_index].ResponseStatus, "Unlocked");
  4594. ShmOCPP16Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4595. return;
  4596. }
  4597. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4598. {
  4599. strcpy((char *)ShmOCPP20Data->UnlockConnector[gun_index].Response_status, "Unlocked");
  4600. ShmOCPP20Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4601. return;
  4602. }
  4603. }
  4604. //===============================================
  4605. // Ocpp Remote Start
  4606. //===============================================
  4607. bool Is_Ocpp_RemoteStartReq(int gun_index)
  4608. {
  4609. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4610. {
  4611. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES ? true : false;
  4612. }
  4613. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4614. {
  4615. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq == YES ? true : false;
  4616. }
  4617. return false;
  4618. }
  4619. void Clean_Ocpp_RemoteStartReq(int gun_index)
  4620. {
  4621. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4622. {
  4623. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = false;
  4624. return;
  4625. }
  4626. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4627. {
  4628. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = false;
  4629. return;
  4630. }
  4631. }
  4632. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag)
  4633. {
  4634. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4635. {
  4636. strcpy(idTag, (char *)&ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag[0]);
  4637. return;
  4638. }
  4639. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4640. {
  4641. strcpy(idTag, (char *)&ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken[0]);
  4642. return;
  4643. }
  4644. strcpy(idTag, "");
  4645. }
  4646. //===============================================
  4647. // Ocpp Remote Stop
  4648. //===============================================
  4649. bool Is_Ocpp_RemoteStopReq(int gun_index)
  4650. {
  4651. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4652. {
  4653. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == YES ? true : false;
  4654. }
  4655. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4656. {
  4657. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq == YES ? true : false;
  4658. }
  4659. return false;
  4660. }
  4661. void Clean_Ocpp_RemoteStopReq(int gun_index)
  4662. {
  4663. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4664. {
  4665. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = false;
  4666. return;
  4667. }
  4668. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4669. {
  4670. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = false;
  4671. return;
  4672. }
  4673. }
  4674. //===============================================
  4675. // Ocpp StopReason
  4676. //===============================================
  4677. void Set_Ocpp_StopReason(int gun_index, char *reason)
  4678. {
  4679. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4680. {
  4681. if(strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  4682. {
  4683. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, reason);
  4684. }
  4685. }
  4686. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4687. {
  4688. if(strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "" ) == EQUAL)
  4689. {
  4690. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, reason);
  4691. }
  4692. }
  4693. // Maintain server
  4694. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4695. {
  4696. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, ShmOCPP16Data->StopTransaction[gun_index].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  4697. }
  4698. }
  4699. void Get_Ocpp_StopReason(int gun_index, char *reason)
  4700. {
  4701. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4702. {
  4703. strcpy(reason, (char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4704. return;
  4705. }
  4706. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4707. {
  4708. strcpy(reason, (char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4709. return;
  4710. }
  4711. strcpy(reason, "");
  4712. }
  4713. void Clean_Ocpp_StopReason(int gun_index)
  4714. {
  4715. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4716. {
  4717. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  4718. }
  4719. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4720. {
  4721. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "");
  4722. }
  4723. // Maintain server
  4724. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4725. {
  4726. strcpy((char *)ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, "");
  4727. }
  4728. }
  4729. //===============================================
  4730. // Ocpp StatusNotification ErrorCode
  4731. //===============================================
  4732. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4733. {
  4734. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4735. {
  4736. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode);
  4737. return;
  4738. }
  4739. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4740. {
  4741. return;
  4742. }
  4743. }
  4744. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4745. {
  4746. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4747. {
  4748. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4749. }
  4750. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4751. {
  4752. return strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4753. }
  4754. return false;
  4755. }
  4756. //===============================================
  4757. // Ocpp StatusNotification VendorErrorCode
  4758. //===============================================
  4759. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4760. {
  4761. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4762. {
  4763. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode);
  4764. return;
  4765. }
  4766. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4767. {
  4768. return;
  4769. }
  4770. }
  4771. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4772. {
  4773. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4774. {
  4775. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4776. }
  4777. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4778. {
  4779. return strcmp((char *)_BackupStatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4780. }
  4781. return false;
  4782. }
  4783. //===============================================
  4784. // Ocpp ChargingProfileConf
  4785. //===============================================
  4786. bool Is_Ocpp_ChargingProfileConf(int gun_index)
  4787. {
  4788. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4789. {
  4790. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4791. }
  4792. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4793. {
  4794. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4795. }
  4796. return false;
  4797. }
  4798. void Clean_Ocpp_ChargingProfileConf(int gun_index)
  4799. {
  4800. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4801. {
  4802. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4803. return;
  4804. }
  4805. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4806. {
  4807. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4808. return;
  4809. }
  4810. }
  4811. //===============================================
  4812. // Ocpp ChargingProfileReq
  4813. //===============================================
  4814. bool Is_Ocpp_ChargingProfileReq(int gun_index)
  4815. {
  4816. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4817. {
  4818. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4819. }
  4820. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4821. {
  4822. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4823. }
  4824. return false;
  4825. }
  4826. void Set_Ocpp_ChargingProfileReq(int gun_index)
  4827. {
  4828. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4829. {
  4830. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4831. return;
  4832. }
  4833. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4834. {
  4835. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4836. return;
  4837. }
  4838. }
  4839. //===============================================
  4840. // Ocpp SmartChargingProfile ChargingProfileKind
  4841. //===============================================
  4842. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind)
  4843. {
  4844. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4845. {
  4846. return strcmp((char *)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, kind) == EQUAL ? true : false;
  4847. }
  4848. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4849. {
  4850. return strcmp((char *)ShmOCPP20Data->SmartChargingProfile [gun_index].chargingProfileKind, kind) == EQUAL ? true : false;
  4851. }
  4852. return false;
  4853. }
  4854. bool Is_Ocpp_ChargingProfileId(int gun_index)
  4855. {
  4856. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4857. {
  4858. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == YES ? true : false;
  4859. }
  4860. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4861. {
  4862. return ShmOCPP20Data->SmartChargingProfile[gun_index].id == YES ? true : false;
  4863. }
  4864. return false;
  4865. }
  4866. int Get_Ocpp_ChargingProfileStartTime(int gun_index)
  4867. {
  4868. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4869. {
  4870. return GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  4871. }
  4872. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4873. {
  4874. return GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  4875. }
  4876. return 0;
  4877. }
  4878. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index)
  4879. {
  4880. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4881. {
  4882. return ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);
  4883. }
  4884. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4885. {
  4886. return ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);
  4887. }
  4888. return 0;
  4889. }
  4890. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index)
  4891. {
  4892. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4893. {
  4894. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4895. }
  4896. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4897. {
  4898. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4899. }
  4900. return 0;
  4901. }
  4902. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index)
  4903. {
  4904. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4905. {
  4906. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4907. }
  4908. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4909. {
  4910. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4911. }
  4912. return 0;
  4913. }
  4914. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index)
  4915. {
  4916. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4917. {
  4918. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4919. }
  4920. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4921. {
  4922. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].numberPhases;
  4923. }
  4924. return 0;
  4925. }
  4926. int Get_Ocpp_ChargingProfileStartIndex(int gun_index)
  4927. {
  4928. int _time = 0, _scheduleTime = 0;
  4929. int _maxCount = 0;
  4930. float _limit = 0;
  4931. int _startCount = NO_DEFINE;
  4932. _time = Get_Ocpp_ChargingProfileStartTime(gun_index);
  4933. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(gun_index);
  4934. for(int i = 0; i < _maxCount; i++)
  4935. {
  4936. _scheduleTime = Get_Ocpp_ChargingProfileScheduleTime(gun_index, i);
  4937. if(_time >= _scheduleTime)
  4938. {
  4939. _limit = Get_Ocpp_ChargingProfileScheduleLimit(gun_index, i);
  4940. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4941. {
  4942. _startCount = i;
  4943. }
  4944. }
  4945. }
  4946. return _startCount;
  4947. }
  4948. //===============================================
  4949. // Ocpp MaxChargingProfile ChargingProfileKind
  4950. //===============================================
  4951. bool Is_Ocpp_MaxChargingProfileKind(char *kind)
  4952. {
  4953. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4954. {
  4955. return strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, kind) == EQUAL ? true : false;
  4956. }
  4957. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4958. {
  4959. return strcmp((char *)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, kind) == EQUAL ? true : false;
  4960. }
  4961. return false;
  4962. }
  4963. bool Is_Ocpp_MaxChargingProfileId(void)
  4964. {
  4965. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4966. {
  4967. return ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES ? true : false;
  4968. }
  4969. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4970. {
  4971. return ShmOCPP20Data->MaxChargingProfile.id == YES ? true : false;
  4972. }
  4973. return false;
  4974. }
  4975. int Get_Ocpp_MaxProfileStartTime(void)
  4976. {
  4977. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4978. {
  4979. return GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  4980. }
  4981. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4982. {
  4983. return GetStartScheduleTime(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule);
  4984. }
  4985. return 0;
  4986. }
  4987. int Get_Ocpp_MaxProfileScheduleCount(void)
  4988. {
  4989. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4990. {
  4991. return ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  4992. }
  4993. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4994. {
  4995. return ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);
  4996. }
  4997. return 0;
  4998. }
  4999. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index)
  5000. {
  5001. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5002. {
  5003. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  5004. }
  5005. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5006. {
  5007. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  5008. }
  5009. return 0;
  5010. }
  5011. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index)
  5012. {
  5013. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5014. {
  5015. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  5016. }
  5017. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5018. {
  5019. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  5020. }
  5021. return 0;
  5022. }
  5023. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index)
  5024. {
  5025. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5026. {
  5027. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  5028. }
  5029. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5030. {
  5031. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  5032. }
  5033. return 0;
  5034. }
  5035. int Get_Ocpp_MaxProfileStartIndex(void)
  5036. {
  5037. int _time = 0, _scheduleTime = 0;
  5038. int _maxCount = 0;
  5039. float _limit = 0;
  5040. int _startCount = NO_DEFINE;
  5041. _time = Get_Ocpp_MaxProfileStartTime();
  5042. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  5043. for(int i = 0; i < _maxCount; i++)
  5044. {
  5045. _scheduleTime = Get_Ocpp_MaxProfileScheduleTime(i);
  5046. if(_time >= _scheduleTime)
  5047. {
  5048. _limit = Get_Ocpp_MaxProfileScheduleLimit(i);
  5049. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  5050. {
  5051. _startCount = i;
  5052. }
  5053. }
  5054. }
  5055. return _startCount;
  5056. }
  5057. //===============================================
  5058. // Ocpp isRemoteStartWaitReq
  5059. //===============================================
  5060. int Is_Ocpp_RemoteStartWaitReq(void)
  5061. {
  5062. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5063. {
  5064. return ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  5065. }
  5066. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5067. {
  5068. return ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  5069. }
  5070. return 0;
  5071. }
  5072. //===============================================
  5073. // Ocpp SessionTarget
  5074. //===============================================
  5075. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget)
  5076. {
  5077. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5078. {
  5079. memcpy(sessionTarget, &ShmOCPP16Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5080. }
  5081. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5082. {
  5083. memcpy(sessionTarget, &ShmOCPP20Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5084. }
  5085. }
  5086. //===============================================
  5087. // Ocpp OffLineMaxChargingPower
  5088. //===============================================
  5089. int Get_Ocpp_OffLineMaxChargingPower(void)
  5090. {
  5091. // unit: W
  5092. int maxChargingPower = 0;
  5093. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5094. {
  5095. maxChargingPower = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[OffLineMaxChargingPower].ItemData);
  5096. }
  5097. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5098. {
  5099. maxChargingPower = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[OffLineMaxChargingPower].variableAttribute[0].value);
  5100. }
  5101. return maxChargingPower > 0 ? maxChargingPower : 0;
  5102. }
  5103. //===============================================
  5104. // Ocpp OcmfData
  5105. //===============================================
  5106. bool Is_Ocpp_SendOcmfDataReq(int gunIndex)
  5107. {
  5108. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5109. {
  5110. return ShmOCPP16Data->OcmfData[gunIndex].SendOcmfDataReq == YES ? true : false;
  5111. }
  5112. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5113. {
  5114. }
  5115. return false;
  5116. }
  5117. void Set_Ocpp_SendOcmfDataReq(int gunIndex)
  5118. {
  5119. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5120. {
  5121. ShmOCPP16Data->OcmfData[gunIndex].SendOcmfDataReq = YES;
  5122. }
  5123. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5124. {
  5125. }
  5126. }
  5127. void Clean_Ocpp_SendOcmfDataReq(int gunIndex)
  5128. {
  5129. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5130. {
  5131. ShmOCPP16Data->OcmfData[gunIndex].SendOcmfDataReq = NO;
  5132. }
  5133. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5134. {
  5135. }
  5136. }
  5137. void Set_Ocpp_OcmfData(int gunIndex, char *key, char *ocmfInfo)
  5138. {
  5139. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5140. {
  5141. strncpy((char *)ShmOCPP16Data->OcmfData[gunIndex].PublicKey, key, sizeof(ShmOCPP16Data->OcmfData[gunIndex].PublicKey));
  5142. strncpy((char *)ShmOCPP16Data->OcmfData[gunIndex].DataString, ocmfInfo, sizeof(ShmOCPP16Data->OcmfData[gunIndex].DataString));
  5143. }
  5144. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5145. {
  5146. }
  5147. }
  5148. //===============================================
  5149. // Set_StartIdType
  5150. //===============================================
  5151. void Set_StartIdType(int gunIndex, int type)
  5152. {
  5153. chargingInfo[gunIndex]->StartIdType = type;
  5154. }
  5155. //===============================================
  5156. // DeductResult
  5157. //===============================================
  5158. void Set_Deduct_UserId(int gunIndex, char *userId)
  5159. {
  5160. memcpy((char *)&ShmChargerInfo->DeductResultReq[gunIndex].creditNo, userId, strlen(userId));
  5161. }
  5162. void Set_Deduct_TransactionId(int gunIndex, int transaction)
  5163. {
  5164. ShmChargerInfo->DeductResultReq[gunIndex].TransactionId = transaction;
  5165. }
  5166. void Clean_DeductInfo(int gunIndex)
  5167. {
  5168. memset(&ShmChargerInfo->DeductResultReq[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5169. }
  5170. bool Is_DeductResultReq(int gunIndex)
  5171. {
  5172. return ShmChargerInfo->DeductResultReq[gunIndex].DeductReq ? true : false;
  5173. }
  5174. //===============================================
  5175. // ReaderStatus
  5176. //===============================================
  5177. bool Is_ReaderStatusReq(int gunIndex)
  5178. {
  5179. return ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq ? true : false;
  5180. }
  5181. void Clean_ReaderStatusInfo(int gunIndex)
  5182. {
  5183. memset(&ShmChargerInfo->ReaderStatus[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5184. }
  5185. //===============================================
  5186. // TransactionInfoData
  5187. //===============================================
  5188. void Set_UserTransactionId(int gunIndex, int transaction)
  5189. {
  5190. ShmChargerInfo->UserTransaction[gunIndex].TransactionId = transaction;
  5191. }
  5192. void Set_UserPrice(int gunIndex, float price)
  5193. {
  5194. ShmChargerInfo->UserTransaction[gunIndex].UserPrice = price;
  5195. }
  5196. void Set_UserTotalCost(int gunIndex, float cost)
  5197. {
  5198. ShmChargerInfo->UserTransaction[gunIndex].TotalCost = cost;
  5199. }
  5200. void Set_UserAccountBalance(int gunIndex, float balance)
  5201. {
  5202. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = balance;
  5203. }
  5204. void Set_UserDiscount(int gunIndex, float discount)
  5205. {
  5206. ShmChargerInfo->UserTransaction[gunIndex].CostDiscount = discount;
  5207. }
  5208. void Clean_UserTransaction(int gunIndex)
  5209. {
  5210. memset(&ShmChargerInfo->UserTransaction[gunIndex], 0x00, sizeof(TransactionInfoData));
  5211. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = -65535;
  5212. }
  5213. void Clean_UserPriceAndReceipt(int gunIndex)
  5214. {
  5215. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex]));
  5216. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex]));
  5217. }
  5218. //===============================================
  5219. // OccupancyDeductReq
  5220. //===============================================
  5221. bool Is_OccupancyDeductReq(int gunIndex)
  5222. {
  5223. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq ? true : false;
  5224. }
  5225. void Clean_OccupancyDeductReq(int gunIndex)
  5226. {
  5227. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq = false;
  5228. }
  5229. bool Is_OccupancyKeepCountingReq(int gunIndex)
  5230. {
  5231. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting ? true : false;
  5232. }
  5233. void Set_OccupancyKeepCountingReq(int gunIndex)
  5234. {
  5235. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = true;
  5236. }
  5237. void Clean_OccupancyKeepCountingReq(int gunIndex)
  5238. {
  5239. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = false;
  5240. }
  5241. bool Is_OccupancyFeeForceCancelReq(int gunIndex)
  5242. {
  5243. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.ForceCancel ? true : false;
  5244. }
  5245. void Set_OccupancyFeeForceCancelReq(int gunIndex)
  5246. {
  5247. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.ForceCancel = true;
  5248. }
  5249. void Clean_OccupancyFeeForceCancelReq(int gunIndex)
  5250. {
  5251. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.ForceCancel = false;
  5252. }
  5253. //===============================================
  5254. // Ocpp GetDiagnosticsReq
  5255. //===============================================
  5256. bool Is_Ocpp_GetDiagnosticsReq(void)
  5257. {
  5258. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5259. {
  5260. return ShmOCPP16Data->MsMsg.bits.GetDiagnosticsReq == YES ? true : false;
  5261. }
  5262. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5263. {
  5264. }
  5265. return false;
  5266. }
  5267. void Clean_Ocpp_GetDiagnosticsReq(void)
  5268. {
  5269. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5270. {
  5271. ShmOCPP16Data->MsMsg.bits.GetDiagnosticsReq = false;
  5272. }
  5273. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5274. {
  5275. }
  5276. }
  5277. //===============================================
  5278. // Main process
  5279. //===============================================
  5280. // 檢查 Byte 中某個 Bit 的值
  5281. // _byte : 欲改變的 byte
  5282. // _bit : 該 byte 的第幾個 bit
  5283. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  5284. {
  5285. return ( _byte & mask_table[_bit] ) != 0x00;
  5286. }
  5287. // 設定 Byte 中某個 Bit的值
  5288. // _byte : 欲改變的 byte
  5289. // _bit : 該 byte 的第幾個 bit
  5290. // value : 修改的值為 0 or 1
  5291. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  5292. {
  5293. if(value == 1)
  5294. *_byte |= (1 << _bit);
  5295. else if (value == 0)
  5296. *_byte ^= (1 << _bit);
  5297. }
  5298. void UserScanFunction()
  5299. {
  5300. }
  5301. unsigned char isModeChange(unsigned char gun_index)
  5302. {
  5303. unsigned char result = NO;
  5304. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  5305. {
  5306. result = YES;
  5307. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  5308. }
  5309. return result;
  5310. }
  5311. void AnnounceAccountBalance(unsigned char index, float balance)
  5312. {
  5313. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  5314. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = balance;
  5315. if(!ShmChargerInfo->Control.TestCtrl.bits.DisableBalance)
  5316. {
  5317. Set_Connector_MiscCommand(index, MISC_CONN_ACCOUNT_BALANCE);
  5318. }
  5319. LOG_INFO("%sAnnounce Account Balance %4.2f To Connector %d",
  5320. ShmChargerInfo->Control.TestCtrl.bits.DisableBalance ? "Do Not " : "", balance, index + 1);
  5321. }
  5322. void FouceAnnounceAccountBalance(unsigned char index)
  5323. {
  5324. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  5325. {
  5326. AnnounceAccountBalance(index, 0);
  5327. }
  5328. }
  5329. void CleanConnectorUserIdAndPrice(unsigned char index)
  5330. {
  5331. if(chargingInfo[index]->SystemStatus == S_IDLE)
  5332. {
  5333. LOG_INFO("*********** Connector %d Clean UserId & UserPrice ***********", index + 1);
  5334. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  5335. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  5336. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -65535;
  5337. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  5338. }
  5339. }
  5340. void DispenserAuthInitial(unsigned char dispenser)
  5341. {
  5342. LOG_INFO("*********** Dispenser %d Authorizing Initial ***********", dispenser + 1);
  5343. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5344. // clean dispenser price
  5345. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser]));
  5346. }
  5347. void GunAuthInitial(unsigned char connector)
  5348. {
  5349. LOG_INFO("*********** Gun %d Authorizing Initial ***********", connector + 1);
  5350. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5351. }
  5352. unsigned char _preGunAuthResult[GENERAL_GUN_QUANTITY];
  5353. unsigned char _preDispenserAuthResult[GENERAL_GUN_QUANTITY];
  5354. struct timespec _GunAuth_Time[GENERAL_GUN_QUANTITY];
  5355. struct timespec _DispenserAuth_Time[GENERAL_GUN_QUANTITY];
  5356. void AuthorizeTimeoutProcess(void)
  5357. {
  5358. int CompletedTime = 0;
  5359. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5360. {
  5361. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult != _AuthResult_None)
  5362. {
  5363. if(_preGunAuthResult[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult)
  5364. {
  5365. GetClockTime(&_GunAuth_Time[index]);
  5366. }
  5367. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5368. {
  5369. FouceAnnounceAccountBalance(index);
  5370. }
  5371. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5372. {
  5373. if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= AUTHORIZE_COMPLETED_TIME)
  5374. {
  5375. LOG_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
  5376. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5377. CleanConnectorUserIdAndPrice(index);
  5378. }
  5379. }
  5380. }
  5381. _preGunAuthResult[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult;
  5382. }
  5383. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5384. {
  5385. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult != _AuthResult_None)
  5386. {
  5387. if(_preDispenserAuthResult[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult)
  5388. {
  5389. GetClockTime(&_DispenserAuth_Time[index]);
  5390. }
  5391. CompletedTime = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
  5392. (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + AUTHORIZE_COMPLETED_TIME) : AUTHORIZE_COMPLETED_TIME;
  5393. if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= CompletedTime)
  5394. {
  5395. DispenserAuthInitial(index);
  5396. }
  5397. }
  5398. _preDispenserAuthResult[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult;
  5399. }
  5400. }
  5401. BOOL IsConnectorAuthorizeSuccess(unsigned char connector)
  5402. {
  5403. BOOL success = false;
  5404. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult == _AuthResult_Valid)
  5405. {
  5406. success = true;
  5407. }
  5408. return success;
  5409. }
  5410. BOOL IsAutoGunSelectionAuthorizeSuccess(unsigned char connector)
  5411. {
  5412. BOOL success = false;
  5413. unsigned char dispenser = 0;
  5414. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  5415. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo.AuthResult == _AuthResult_Valid)
  5416. {
  5417. success = true;
  5418. }
  5419. return success;
  5420. }
  5421. bool CheckConnectorTypeStatus(void)
  5422. {
  5423. bool result = true;
  5424. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 14)
  5425. {
  5426. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = ShmChargerInfo->Control.MaxConnector;
  5427. ShmChargerInfo->Control.ChargerRatingPower = ParsingRatingPower((char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5428. ShmChargerInfo->Control.PsuQuantityByModel = ShmChargerInfo->Control.ChargerRatingPower / (INFY_SINGLE_PSU_POWER / 100);
  5429. LOG_INFO("DC Gun Count = %d, RatingPower = %d.%d kW",
  5430. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  5431. (ShmChargerInfo->Control.ChargerRatingPower / 10),
  5432. (ShmChargerInfo->Control.ChargerRatingPower % 10));
  5433. if(ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  5434. {
  5435. result = false;
  5436. }
  5437. //DO360 charging info default setting
  5438. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5439. {
  5440. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  5441. chargingInfo[i]->Index = (i < ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity || i == 0) ? i : 0xFF;
  5442. chargingInfo[i]->Evboard_id = 0x00;
  5443. chargingInfo[i]->ReservationId = -1;
  5444. chargingInfo[i]->slotsIndex = 0;
  5445. chargingInfo[i]->SystemStatus = S_BOOTING;
  5446. chargingInfo[i]->Type = _Type_Unknown;
  5447. chargingInfo[i]->type_index = 0;
  5448. chargingInfo[i]->IsAvailable = YES;
  5449. }
  5450. }
  5451. else
  5452. {
  5453. // Module Name 不正確 - 告警
  5454. result = false;
  5455. }
  5456. return result;
  5457. }
  5458. void KillTaskExceptPrimary()
  5459. {
  5460. system("killall Module_Authorize");
  5461. system("killall Module_CabinetParallel");
  5462. system("killall Module_ChargerSelfTest");
  5463. system("killall Module_LedIndication");
  5464. system("killall Module_EvComm");
  5465. system("killall Module_InternalComm");
  5466. KillPowerModuleTask();
  5467. system("killall Module_4g &");
  5468. system("killall Module_Wifi &");
  5469. }
  5470. void KillNtpd(void)
  5471. {
  5472. system("killall ntpd");
  5473. }
  5474. void KillAllTask()
  5475. {
  5476. system("killall Module_Authorize");
  5477. system("killall Module_CabinetParallel");
  5478. system("killall Module_ChargerSelfTest");
  5479. system("killall Module_LedIndication");
  5480. system("killall Module_EventLogging");
  5481. system("killall Module_PrimaryComm");
  5482. system("killall Module_EvComm");
  5483. system("killall Module_LcmControl");
  5484. system("killall Module_InternalComm");
  5485. KillPowerModuleTask();
  5486. Kill_Ocpp_Service();
  5487. Kill_MaintainOcpp_Service();
  5488. system("killall Module_4g &");
  5489. system("killall Module_Wifi &");
  5490. }
  5491. int TransferImageViaFTP(char *IpAddress, char *SourcePath)
  5492. {
  5493. int isPass = PASS;
  5494. unsigned char ftpcmdbuf[256];
  5495. //Using ftpput command to transfer upgrade image,
  5496. //User name : root
  5497. //User password : y42j/4cj84
  5498. //Destination : as same as source path
  5499. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s \"%s\" \"%s\"", IpAddress, SourcePath, SourcePath);
  5500. if(system((char*)ftpcmdbuf) != 0)
  5501. {
  5502. isPass = FAIL;
  5503. }
  5504. return isPass;
  5505. }
  5506. void TriggerDispenserUpgrade(unsigned char index, char *FileName)
  5507. {
  5508. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, 0x00, 128);
  5509. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, FileName, strlen(FileName));
  5510. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.FirmwareUpdateRequest = true;
  5511. LOG_INFO("Trigger dispenser %d to update image [%s]", index + 1, FileName);
  5512. }
  5513. BOOL PutImageToAllDispenser(char *SourcePath, char *FileName, char *model_name)
  5514. {
  5515. int isPass = FAIL;
  5516. int isImageOK[GENERAL_GUN_QUANTITY];
  5517. int dispenser_index = 0;
  5518. BOOL transferred = FALSE;
  5519. memset(isImageOK, 0x00, sizeof(isImageOK));
  5520. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5521. {
  5522. isPass = FAIL;
  5523. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  5524. {
  5525. dispenser_index = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex;
  5526. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName, (char *)model_name) != EQUAL)
  5527. {
  5528. LOG_INFO("Dispenser %d model name [%s] update ignore",
  5529. dispenser_index + 1,
  5530. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName);
  5531. continue;
  5532. }
  5533. char DispenserIpAddress[16];
  5534. memset((char*)DispenserIpAddress, 0x00, 16);
  5535. sprintf((char*)DispenserIpAddress, "%d.%d.%d.%d",
  5536. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  5537. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  5538. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  5539. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  5540. LOG_INFO("Transfer Image(%s) to Dispenser %d (IP: %s)", SourcePath, dispenser_index + 1, DispenserIpAddress);
  5541. isPass = TransferImageViaFTP(DispenserIpAddress, SourcePath);
  5542. isImageOK[dispenser_index] = isPass;
  5543. LOG_INFO("Dispenser %d Put Image %s", dispenser_index + 1, isPass == PASS ? "OK" : "Fail");
  5544. }
  5545. }
  5546. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5547. {
  5548. if(isImageOK[i] > 0)
  5549. {
  5550. TriggerDispenserUpgrade(i, FileName);
  5551. transferred = true;
  5552. }
  5553. }
  5554. return transferred;
  5555. }
  5556. BOOL WaitAllDispenserUpgradeCompleted(void)
  5557. {
  5558. BOOL completed = TRUE;
  5559. GetClockTime(&_DispenserUpgrade_time);
  5560. do
  5561. {
  5562. completed = TRUE;
  5563. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5564. {
  5565. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest ||
  5566. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm)
  5567. {
  5568. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted)
  5569. {
  5570. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest = false;
  5571. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm = false;
  5572. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted = false;
  5573. LOG_INFO("Dispenser %d Upgrade Success", i + 1);
  5574. }
  5575. else
  5576. {
  5577. completed = FALSE;
  5578. }
  5579. }
  5580. }
  5581. if(GetTimeoutValue(_DispenserUpgrade_time) / uSEC_VAL >= DISPENSER_UPGRADE_TIME)
  5582. {
  5583. LOG_INFO("Wait Upgrade Timeout");
  5584. completed = FALSE;
  5585. break;
  5586. }
  5587. if(completed == FALSE)
  5588. {
  5589. sleep(1);
  5590. }
  5591. }while(!completed);
  5592. return completed;
  5593. }
  5594. void UpgradePrepare(void)
  5595. {
  5596. int count = 0;
  5597. SetAcContactor(OFF);
  5598. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5599. {
  5600. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5601. {
  5602. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5603. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5604. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5605. {
  5606. count++;
  5607. }
  5608. }
  5609. }
  5610. if(count > 0)
  5611. {
  5612. sleep(3);
  5613. }
  5614. else
  5615. {
  5616. sleep(1);
  5617. }
  5618. }
  5619. void TriggerPCabinetUpgrade(unsigned char index, char *FileName)
  5620. {
  5621. memset(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, 0x00, 128);
  5622. memcpy(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, FileName, strlen(FileName));
  5623. ShmChargerInfo->ParallelCabinet.PCabinet[index].UpdateRequest = YES;
  5624. LOG_INFO("Trigger PCabinet %d to update image [%s]", index + 1, FileName);
  5625. }
  5626. BOOL PutImageToAllParallelCabinet(char *SourcePath, char *FileName)
  5627. {
  5628. int isPass = FAIL;
  5629. BOOL transferred = FALSE;
  5630. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5631. {
  5632. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5633. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5634. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5635. {
  5636. int channel = ShmChargerInfo->ParallelCabinet.PCabinet[i].SocketChannel;
  5637. if(ShmChargerInfo->CabinetConnInfo[channel].Status == _SCONN_MATCHED &&
  5638. ShmChargerInfo->ParallelCabinet.PCUpdateState[i] == Not_Update)
  5639. {
  5640. char PCabinetIpAddress[16];
  5641. memset((char*)PCabinetIpAddress, 0x00, 16);
  5642. sprintf((char*)PCabinetIpAddress, "%d.%d.%d.%d",
  5643. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 0) & 0xFF),
  5644. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 8) & 0xFF),
  5645. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 16) & 0xFF),
  5646. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 24) & 0xFF));
  5647. isPass = TransferImageViaFTP(PCabinetIpAddress, SourcePath);
  5648. if(isPass == PASS)
  5649. {
  5650. TriggerPCabinetUpgrade(i, FileName);
  5651. }
  5652. transferred = true;
  5653. }
  5654. }
  5655. }
  5656. return transferred;
  5657. }
  5658. BOOL WaitAllPCabinetStartUpgrade(void)
  5659. {
  5660. BOOL isStart = true;
  5661. int count = 0;
  5662. struct timespec _waitStart;
  5663. GetClockTime(&_waitStart);
  5664. do
  5665. {
  5666. isStart = true;
  5667. count = 0;
  5668. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5669. {
  5670. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5671. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5672. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5673. {
  5674. count++;
  5675. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updating)
  5676. {
  5677. isStart = false;
  5678. }
  5679. }
  5680. }
  5681. if((GetTimeoutValue(_waitStart) / uSEC_VAL) >= START_UPGRADE_TIME)
  5682. {
  5683. isStart = false;
  5684. break;
  5685. }
  5686. usleep(10000);
  5687. }while(!isStart);
  5688. if(isStart && count > 0)
  5689. {
  5690. LOG_INFO("Total %d PCabinet upgrade start...", count);
  5691. }
  5692. else
  5693. {
  5694. if(count > 0)
  5695. {
  5696. LOG_INFO("Wait PCabinet upgrade timeout");
  5697. }
  5698. else
  5699. {
  5700. LOG_INFO("No PCabinet need to upgrade");
  5701. }
  5702. }
  5703. return isStart;
  5704. }
  5705. BOOL WaitAllPCabinetUpgradeCompleted(void)
  5706. {
  5707. BOOL completed = true;
  5708. int count = 0;
  5709. struct timespec _waitCompleted;
  5710. GetClockTime(&_waitCompleted);
  5711. do
  5712. {
  5713. completed = true;
  5714. count = 0;
  5715. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5716. {
  5717. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5718. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5719. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5720. {
  5721. count++;
  5722. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updated)
  5723. {
  5724. completed = false;
  5725. }
  5726. }
  5727. }
  5728. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= PCABINET_UPGRADE_TIME)
  5729. {
  5730. completed = false;
  5731. break;
  5732. }
  5733. usleep(10000);
  5734. }while(!completed);
  5735. if(completed && count > 0)
  5736. {
  5737. LOG_INFO("Total %d PCabinet upgrade completed!", count);
  5738. }
  5739. else
  5740. {
  5741. if(count > 0)
  5742. {
  5743. LOG_INFO("Wait PCabinet upgrade completed timeout");
  5744. }
  5745. else
  5746. {
  5747. LOG_INFO("Need not to wait PCabinet completed");
  5748. }
  5749. }
  5750. return completed;
  5751. }
  5752. BOOL WaitMCabinetUpgradeCompleted(void)
  5753. {
  5754. BOOL completed = true;
  5755. struct timespec _waitCompleted;
  5756. GetClockTime(&_waitCompleted);
  5757. do
  5758. {
  5759. completed = false;
  5760. if(ShmChargerInfo->SCabinetControl.NeedUpgrade == _R_NoUpgrade)
  5761. {
  5762. completed = true;
  5763. }
  5764. if(ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Idle &&
  5765. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Alarm &&
  5766. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Charging)
  5767. {
  5768. completed = false;
  5769. break;
  5770. }
  5771. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= MCABINET_UPGRADE_TIME)
  5772. {
  5773. completed = false;
  5774. break;
  5775. }
  5776. usleep(10000);
  5777. }while(!completed);
  5778. if(completed)
  5779. {
  5780. LOG_INFO("MCabinet upgrade completed!");
  5781. }
  5782. else
  5783. {
  5784. LOG_INFO("Wait MCabinet upgrade timeout or disconnected...");
  5785. }
  5786. return completed;
  5787. }
  5788. bool IsModelNameMatch(char *model)
  5789. {
  5790. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == model[0] &&
  5791. ShmSysConfigAndInfo->SysConfig.ModelName[1] == model[1] &&
  5792. ShmSysConfigAndInfo->SysConfig.ModelName[7] == model[7] &&
  5793. ShmSysConfigAndInfo->SysConfig.ModelName[8] == model[8] &&
  5794. ShmSysConfigAndInfo->SysConfig.ModelName[9] == model[9] &&
  5795. ShmSysConfigAndInfo->SysConfig.ModelName[11] == model[11] &&
  5796. ShmSysConfigAndInfo->SysConfig.ModelName[12] == model[12] &&
  5797. ShmSysConfigAndInfo->SysConfig.ModelName[13] == model[13])
  5798. {
  5799. return true;
  5800. }
  5801. return false;
  5802. }
  5803. unsigned long long getAvailableMemory(void)
  5804. {
  5805. long pages = sysconf(_SC_AVPHYS_PAGES);
  5806. long page_size = sysconf(_SC_PAGE_SIZE);
  5807. return pages * page_size;
  5808. }
  5809. void ImageTypeParsing(unsigned int type, char *type_str)
  5810. {
  5811. switch(type)
  5812. {
  5813. case 0x10000001:
  5814. sprintf(type_str, "Image_uboot");
  5815. break;
  5816. case 0x10000002:
  5817. sprintf(type_str, "Image_dtb");
  5818. break;
  5819. case 0x10000003:
  5820. sprintf(type_str, "Image_zImage");
  5821. break;
  5822. case 0x10000004:
  5823. sprintf(type_str, "Image_ramdisk");
  5824. break;
  5825. case 0x10000005:
  5826. sprintf(type_str, "Image_Factory");
  5827. break;
  5828. case 0x10000006:
  5829. sprintf(type_str, "Image_Primary");
  5830. break;
  5831. case 0x10000007:
  5832. sprintf(type_str, "Image_CCS_uboot");
  5833. break;
  5834. case 0x10000008:
  5835. sprintf(type_str, "Image_CCS_dtb");
  5836. break;
  5837. case 0x10000009:
  5838. sprintf(type_str, "Image_CCS_zImage");
  5839. break;
  5840. case 0x1000000A:
  5841. sprintf(type_str, "Image_CCS_ramdisk");
  5842. break;
  5843. case 0x1000000B:
  5844. sprintf(type_str, "Image_CAHdeMO");
  5845. break;
  5846. case 0x1000000C:
  5847. sprintf(type_str, "Image_GBT");
  5848. break;
  5849. case 0x1000000D:
  5850. sprintf(type_str, "Image_Relay");
  5851. break;
  5852. case 0x1000000E:
  5853. sprintf(type_str, "Image_Fan");
  5854. break;
  5855. case 0x1000000F:
  5856. sprintf(type_str, "Image_LCM");
  5857. break;
  5858. case 0x10000010:
  5859. sprintf(type_str, "Image_Infy_PFC");
  5860. break;
  5861. case 0x10000011:
  5862. sprintf(type_str, "Image_Infy_DD");
  5863. break;
  5864. case 0x10000014:
  5865. sprintf(type_str, "Image_LED");
  5866. break;
  5867. case 0x10000015:
  5868. sprintf(type_str, "Image_LCM_APK");
  5869. break;
  5870. default:
  5871. sprintf(type_str, "Image_Unknown");
  5872. break;
  5873. }
  5874. }
  5875. char CheckUpdateProcess()
  5876. {
  5877. char _model[32];
  5878. char _type[32];
  5879. bool support = false;
  5880. bool OneImageOnly = false;
  5881. int isPass = true;
  5882. uint8_t retSucc = 0;
  5883. uint8_t retFail = 0;
  5884. byte target = 0;
  5885. char *new_str = NULL;
  5886. unsigned char *ptr = NULL;
  5887. int fd = 0;
  5888. int uartFd = 0;
  5889. unsigned int Type = 0;
  5890. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  5891. DIR *d;
  5892. struct dirent *dir;
  5893. char cmdBuf[128];
  5894. int availableMem = 0;
  5895. memset(_model, 0x00, sizeof(_model));
  5896. memset(_type, 0x00, sizeof(_type));
  5897. availableMem = getAvailableMemory() / (1024 * 1024);
  5898. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5899. // If available memory too small, free memory cache first
  5900. if(availableMem < MIN_AVAILABLE_MEMORY)
  5901. {
  5902. LOG_INFO("Available memory less than %d MBytes, free cache & kill ntpd.", MIN_AVAILABLE_MEMORY);
  5903. system("echo 3 > /proc/sys/vm/drop_caches");
  5904. KillNtpd();
  5905. availableMem = getAvailableMemory() / (1024 * 1024);
  5906. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5907. }
  5908. d = opendir("/mnt/");
  5909. if(d)
  5910. {
  5911. while((dir = readdir(d)) != NULL && !OneImageOnly)
  5912. {
  5913. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  5914. continue;
  5915. }
  5916. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  5917. strcat(new_str, "/mnt/");
  5918. strcat(new_str, dir->d_name);
  5919. fd = open(new_str, O_RDONLY);
  5920. if(fd < 0)
  5921. {
  5922. close(fd);
  5923. free(new_str);
  5924. continue;
  5925. }
  5926. LOG_INFO("%s\n", new_str);
  5927. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  5928. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  5929. //get the image length
  5930. ImageLen = read(fd, ptr, MaxLen);
  5931. if(ImageLen > 20)
  5932. {
  5933. memcpy(_model, ptr, 16);
  5934. Type = (((unsigned int)ptr[16]) << 24 | ((unsigned int)ptr[17]) << 16 | ((unsigned int)ptr[18]) << 8 | ((unsigned int)ptr[19]));
  5935. ImageTypeParsing(Type, _type);
  5936. LOG_INFO("Model: %s, Type: %s", _model, _type);
  5937. }
  5938. free(ptr);
  5939. if (ImageLen > 20)
  5940. {
  5941. support = false;
  5942. availableMem = getAvailableMemory() / (1024 * 1024);
  5943. LOG_INFO("AvailableMemory: %dMByte", availableMem);
  5944. //if(strcmp(model, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == EQUAL)
  5945. if(IsModelNameMatch(_model))
  5946. {
  5947. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5948. {
  5949. PutImageToAllParallelCabinet(new_str, dir->d_name);
  5950. WaitAllPCabinetStartUpgrade();
  5951. }
  5952. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.NeedUpgrade)
  5953. {
  5954. ShmChargerInfo->SCabinetControl.UpgradeState = Updating;
  5955. sleep(1);
  5956. }
  5957. switch (Type)
  5958. {
  5959. case 0x10000001:
  5960. case 0x10000002:
  5961. case 0x10000003:
  5962. case 0x10000004:
  5963. case 0x10000005:
  5964. support = true;
  5965. _UpgradeNeedReboot = TRUE;
  5966. LOG_INFO("Upgrade Flash Start...", Type);
  5967. isPass = Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5968. LOG_INFO("Upgrade Flash End, %s", isPass ? "Pass" : "Fail");
  5969. break;
  5970. case 0x10000006: // primary mcu
  5971. case 0x1000000D: // relay board
  5972. case 0x1000000E: // fan board
  5973. case 0x10000014: // led board
  5974. support = true;
  5975. target = 0x00;
  5976. if (Type == 0x10000006)
  5977. {
  5978. target = UPGRADE_PRI;
  5979. }
  5980. else if (Type == 0x1000000D)
  5981. {
  5982. target = UPGRADE_RB;
  5983. }
  5984. else if (Type == 0x1000000E)
  5985. {
  5986. target = UPGRADE_FAN;
  5987. }
  5988. else if (Type == 0x10000014)
  5989. {
  5990. target = UPGRADE_LED;
  5991. }
  5992. uartFd = InitComPort(target);
  5993. if(target == UPGRADE_RB)
  5994. {
  5995. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC1, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5996. if(isPass == PASS)
  5997. {
  5998. retSucc++;
  5999. }
  6000. else
  6001. {
  6002. retFail++;
  6003. }
  6004. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  6005. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  6006. {
  6007. sleep(3);
  6008. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC2, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  6009. }
  6010. }
  6011. else
  6012. {
  6013. isPass = Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  6014. }
  6015. close(uartFd);
  6016. break;
  6017. }
  6018. if(support)
  6019. {
  6020. if(isPass == PASS)
  6021. {
  6022. retSucc++;
  6023. }
  6024. else
  6025. {
  6026. retFail++;
  6027. }
  6028. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  6029. }
  6030. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  6031. {
  6032. LOG_INFO("MCabinet upgrade completed");
  6033. WaitAllPCabinetUpgradeCompleted();
  6034. }
  6035. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.UpgradeState == Updating)
  6036. {
  6037. ShmChargerInfo->SCabinetControl.UpgradeState = Updated;
  6038. LOG_INFO("SCabinet upgrade completed");
  6039. WaitMCabinetUpgradeCompleted();
  6040. }
  6041. }
  6042. else
  6043. {
  6044. if(_model[0] == 'D' && (_model[1] == 'D' || _model[1] == 'X'))
  6045. {
  6046. OneImageOnly = true;
  6047. _UpgradeNeedReboot = TRUE;
  6048. LOG_INFO("Start Put Image To Dispenser");
  6049. PutImageToAllDispenser(new_str, dir->d_name, _model);
  6050. WaitAllDispenserUpgradeCompleted();
  6051. LOG_INFO("All Dispenser Upgrade Completed");
  6052. }
  6053. else
  6054. {
  6055. retFail++;
  6056. LOG_INFO("Model Name Is Not Match: %s", _model);
  6057. }
  6058. }
  6059. }
  6060. else
  6061. {
  6062. retFail++;
  6063. LOG_INFO("Image length is illegal");
  6064. }
  6065. sprintf(cmdBuf, "rm -f %s", new_str);
  6066. system(cmdBuf);
  6067. close(fd);
  6068. free(new_str);
  6069. }
  6070. }
  6071. closedir(d);
  6072. if(retFail != 0)
  6073. {
  6074. return FAIL;
  6075. }
  6076. return PASS;
  6077. }
  6078. void TriggerDispenserConnectionTimeoutSetting(void)
  6079. {
  6080. BOOL trigger = FALSE;
  6081. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6082. {
  6083. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0 &&
  6084. !Is_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT))
  6085. {
  6086. trigger = TRUE;
  6087. Set_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT);
  6088. }
  6089. }
  6090. if(trigger)
  6091. {
  6092. LOG_INFO("********** Trigger Dispenser Connector Timeout Value: %d **********", ShmChargerInfo->CabinetMiscValue.ConnectionTimeout);
  6093. }
  6094. }
  6095. void CheckConnectionTimeout(void)
  6096. {
  6097. if(!Is_Ocpp_Task_Alive())
  6098. {
  6099. _connectionTimeout = CONN_PLUG_TIME_OUT;
  6100. }
  6101. else
  6102. {
  6103. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  6104. {
  6105. _connectionTimeout = Get_Ocpp_ConnectionTimeOut();
  6106. if(_connectionTimeout <= 0)
  6107. {
  6108. _connectionTimeout = CONN_PLUG_TIME_OUT;
  6109. }
  6110. }
  6111. else
  6112. {
  6113. _connectionTimeout = CONN_PLUG_TIME_OUT;
  6114. }
  6115. }
  6116. if(_connectionTimeout != ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  6117. {
  6118. ShmChargerInfo->CabinetMiscValue.ConnectionTimeout = _connectionTimeout;
  6119. TriggerDispenserConnectionTimeoutSetting();
  6120. }
  6121. }
  6122. void CreateTimeoutFork()
  6123. {
  6124. pid_t timeoutPid;
  6125. timeoutPid = fork();
  6126. if (timeoutPid == 0)
  6127. {
  6128. GetClockTime(&_cmdSubPriority_time);
  6129. CheckConnectionTimeout();
  6130. LOG_INFO("Timeout Fork PID = %d", getpid());
  6131. while(true)
  6132. {
  6133. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  6134. {
  6135. CheckConnectionTimeout();
  6136. GetClockTime(&_cmdSubPriority_time);
  6137. }
  6138. sleep(1);
  6139. }
  6140. }
  6141. }
  6142. void CheckFactoryConfigFunction(void)
  6143. {
  6144. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  6145. {
  6146. char Buf[256];
  6147. sprintf(Buf, "cd /root;./FactoryConfig -m");
  6148. system(Buf);
  6149. system("rm -f /Storage/OCPP/OCPPConfiguration");
  6150. system("sync");
  6151. sleep(5);
  6152. system("reboot -f");
  6153. sleep(5);
  6154. system("reboot -f");
  6155. }
  6156. }
  6157. void CheckFwUpdateFunction(void)
  6158. {
  6159. char UpdateResult = PASS;
  6160. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  6161. {
  6162. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  6163. UpgradePrepare();
  6164. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  6165. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  6166. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  6167. LOG_INFO("ftp : update start.");
  6168. TryCloseWatchdog();
  6169. //KillTask();
  6170. system("killall Module_PrimaryComm");
  6171. system("killall Module_InternalComm");
  6172. UpdateResult = CheckUpdateProcess();
  6173. if (UpdateResult == PASS)
  6174. {
  6175. LOG_INFO("ftp : update complete.");
  6176. }
  6177. else
  6178. {
  6179. LOG_INFO("ftp : update fail.");
  6180. }
  6181. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  6182. sleep(10);
  6183. if(_UpgradeNeedReboot)
  6184. {
  6185. system("reboot -f");
  6186. }
  6187. else
  6188. {
  6189. KillAllTask();
  6190. system("/usr/bin/run_evse_restart.sh");
  6191. }
  6192. }
  6193. else if(Is_Ocpp_UpdateFirmwareReq())
  6194. {
  6195. Clean_Ocpp_UpdateFirmwareReq();
  6196. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  6197. UpgradePrepare();
  6198. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  6199. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  6200. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  6201. if(Is_Ocpp_UpdateFirmwareStatus("Downloaded"))
  6202. {
  6203. LOG_INFO("Backend : update start.");
  6204. TryCloseWatchdog();
  6205. Set_Ocpp_UpdateFirmwareStatus("Installing");
  6206. Set_Ocpp_FirmwareStatusNotificationReq();
  6207. //KillTask();
  6208. system("killall Module_PrimaryComm");
  6209. system("killall Module_InternalComm");
  6210. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6211. {
  6212. setChargerMode(_index, MODE_UPDATE);
  6213. }
  6214. UpdateResult = CheckUpdateProcess();
  6215. if (UpdateResult == PASS)
  6216. {
  6217. LOG_INFO("Backend : update complete.");
  6218. Set_Ocpp_UpdateFirmwareStatus("Installed");
  6219. }
  6220. else
  6221. {
  6222. LOG_INFO("Backend : update fail.");
  6223. Set_Ocpp_UpdateFirmwareStatus("InstallationFailed");
  6224. }
  6225. Set_Ocpp_FirmwareStatusNotificationReq();
  6226. sleep(10);
  6227. if(_UpgradeNeedReboot)
  6228. {
  6229. system("reboot -f");
  6230. }
  6231. else
  6232. {
  6233. KillAllTask();
  6234. system("/usr/bin/run_evse_restart.sh");
  6235. }
  6236. }
  6237. }
  6238. }
  6239. void ExecuteWriteFlash(void)
  6240. {
  6241. struct SysConfigData config;
  6242. memcpy(&config, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  6243. config.SwitchDebugFlag = NO;
  6244. strcpy((char *) config.UserId, "");
  6245. int result = StoreUsrConfigData(&config);
  6246. if(result != 1)
  6247. {
  6248. LOG_INFO("Store User Config NG");
  6249. }
  6250. else
  6251. {
  6252. LOG_INFO("Store User Config OK");
  6253. }
  6254. }
  6255. struct timespec _WiringInfo_time;
  6256. int CheckFlashUpdate(void)
  6257. {
  6258. if(ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged)
  6259. {
  6260. ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged = false;
  6261. LOG_INFO("Flash Config Changed");
  6262. GetClockTime(&_WiringInfo_time);
  6263. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = true;
  6264. }
  6265. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable)
  6266. {
  6267. // wait for 30s to write flash or write flash immediately
  6268. if((GetTimeoutValue(_WiringInfo_time) / uSEC_VAL) >= WIRING_INFO_DELAY ||
  6269. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashImmediately)
  6270. {
  6271. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = false;
  6272. LOG_INFO("Start Writing Flash");
  6273. pid_t writePid = fork();
  6274. if(writePid == 0)
  6275. {
  6276. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = true;
  6277. ExecuteWriteFlash();
  6278. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = false;
  6279. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset)
  6280. {
  6281. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset = false;
  6282. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  6283. LOG_INFO("Write Flash OK And Need Soft Reset");
  6284. }
  6285. return NO;
  6286. }
  6287. LOG_INFO("Write Flash Conpleted");
  6288. }
  6289. }
  6290. return YES;
  6291. }
  6292. void CheckSoftHardReset(void)
  6293. {
  6294. if(ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset)
  6295. {
  6296. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = false;
  6297. LOG_INFO("********** Need Soft Reset **********");
  6298. sleep(5);
  6299. Kill_Ocpp_Service();
  6300. KillAllTask();
  6301. TryCloseWatchdog();
  6302. system("/usr/bin/run_evse_restart.sh");
  6303. }
  6304. if(ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset)
  6305. {
  6306. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = false;
  6307. LOG_INFO("********** Need Hard Reset **********");
  6308. sleep(5);
  6309. system("reboot -f");
  6310. }
  6311. }
  6312. struct timespec _Reboot_time;
  6313. void TriggerDispenserHardwareReboot(void)
  6314. {
  6315. BOOL trigger = FALSE;
  6316. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6317. {
  6318. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6319. {
  6320. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm == 0 &&
  6321. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  6322. {
  6323. trigger = TRUE;
  6324. Set_Dispenser_MiscCommand(i, MISC_DISP_HARDWARE_REBOOT);
  6325. }
  6326. }
  6327. }
  6328. if(trigger)
  6329. {
  6330. GetClockTime(&_Reboot_time);
  6331. LOG_INFO("********** Trigger Dispenser Hardware Reboot **********");
  6332. }
  6333. }
  6334. BOOL IsDispenserHardwareRebootResponsed(void)
  6335. {
  6336. BOOL response_ok = TRUE;
  6337. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6338. {
  6339. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6340. {
  6341. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  6342. {
  6343. response_ok = FALSE;
  6344. // 10s for reboot request timeout
  6345. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  6346. {
  6347. response_ok = TRUE;
  6348. LOG_INFO("Reboot request timeout");
  6349. }
  6350. }
  6351. }
  6352. }
  6353. return response_ok;
  6354. }
  6355. struct timespec _Reset_time;
  6356. void TriggerDispenserSoftwareReset(void)
  6357. {
  6358. BOOL trigger = FALSE;
  6359. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6360. {
  6361. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6362. {
  6363. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  6364. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  6365. {
  6366. trigger = TRUE;
  6367. Set_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET);
  6368. }
  6369. }
  6370. }
  6371. if(trigger)
  6372. {
  6373. GetClockTime(&_Reset_time);
  6374. LOG_INFO("********** Trigger Dispenser Software Reset **********");
  6375. }
  6376. }
  6377. BOOL IsDispenserSoftwareResetResponsed(void)
  6378. {
  6379. BOOL response_ok = TRUE;
  6380. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6381. {
  6382. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6383. {
  6384. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  6385. {
  6386. response_ok = FALSE;
  6387. // 10s for reset request timeout
  6388. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  6389. {
  6390. response_ok = TRUE;
  6391. LOG_INFO("Reset request timeout");
  6392. }
  6393. }
  6394. }
  6395. }
  6396. return response_ok;
  6397. }
  6398. // return time offset
  6399. int TimeOffsetKeyParsing(char *StringItem)
  6400. {
  6401. int offset = 0;
  6402. int hour = 0, minute = 0, sign = 1;
  6403. char *ptrSave, *ptrToken;
  6404. char strSource[501];
  6405. memset(strSource, 0x00, sizeof(strSource));
  6406. strcpy(strSource, StringItem);
  6407. if(strSource[0] != '+' && strSource[0] != '-')
  6408. {
  6409. return offset;
  6410. }
  6411. sign = strSource[0] == '+' ? (1) : (-1);
  6412. ptrToken = strtok_r(&strSource[1], ":", &ptrSave);
  6413. if(ptrToken != NULL)
  6414. {
  6415. hour = atoi(ptrToken);
  6416. ptrToken = strtok_r(NULL, ":", &ptrSave);
  6417. if(ptrToken != NULL)
  6418. {
  6419. minute = atoi(ptrToken);
  6420. offset = ((hour * 60) + minute) * sign;
  6421. }
  6422. }
  6423. return offset;
  6424. }
  6425. char MyDefaultPriceString[501];
  6426. void DefaultPriceHandler(void)
  6427. {
  6428. char price[501];
  6429. char defaultPrice[512];
  6430. memset(defaultPrice, 0x00, sizeof(defaultPrice));
  6431. Get_Ocpp_DefaultPrice((char *)price);
  6432. if(strcmp(MyDefaultPriceString, (char *)price) != EQUAL)
  6433. {
  6434. float ConnectionFee = 0, OccupancyFee = 0;
  6435. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6436. {
  6437. ConnectionFee = PriceParsing(price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6438. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = PriceParsing(price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6439. OccupancyFee = PriceParsing(price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6440. LOG_INFO("******** Customer Audi Default Price *******");
  6441. LOG_INFO("********** Connection Fee: %4.2f **********", ConnectionFee);
  6442. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6443. LOG_INFO("********** Occupancy Fee: %4.2f **********", OccupancyFee);
  6444. }
  6445. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6446. {
  6447. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6448. ShellPriceParsing(price, defaultPrice);
  6449. LOG_INFO("******* Customer Shell Default Price *******");
  6450. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6451. }
  6452. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6453. {
  6454. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = TccDefaultPriceParsing(price, &ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6455. LOG_INFO("******* Customer Tcc Default Price *******");
  6456. LOG_INFO("*** TimeOfDayPricing: %4.2f", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6457. }
  6458. else
  6459. {
  6460. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6461. strcpy(defaultPrice, price);
  6462. LOG_INFO("********** Standard Default Price **********");
  6463. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6464. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6465. }
  6466. strcpy(MyDefaultPriceString, price);
  6467. }
  6468. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6469. {
  6470. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = GetNowTimePricing(&ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6471. }
  6472. if(ShmChargerInfo->CabinetMiscValue.DefaultPrice != ShmChargerInfo->PriceAndReceiptInfo.CurrentRate)
  6473. {
  6474. ShmChargerInfo->CabinetMiscValue.DefaultPrice = ShmChargerInfo->PriceAndReceiptInfo.CurrentRate;
  6475. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6476. {
  6477. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6478. {
  6479. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE))
  6480. {
  6481. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE);
  6482. }
  6483. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY))
  6484. {
  6485. Set_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY);
  6486. }
  6487. }
  6488. }
  6489. LOG_INFO("********** Trigger Dispenser Default Price Value **********");
  6490. }
  6491. if(strlen(defaultPrice) > 0)
  6492. {
  6493. strcpy(ShmChargerInfo->PriceAndReceiptInfo.DefaultPriceString, defaultPrice);
  6494. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6495. {
  6496. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6497. {
  6498. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING))
  6499. {
  6500. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING);
  6501. }
  6502. }
  6503. }
  6504. LOG_INFO("********** Trigger Dispenser Default Price Value String **********");
  6505. }
  6506. }
  6507. void UserPriceHandler(void)
  6508. {
  6509. char UserId[32];
  6510. char price[500];
  6511. char userPrice[512];
  6512. memset(userPrice, 0x00, sizeof(userPrice));
  6513. Get_Ocpp_UserPrice(UserId, price);
  6514. if(strlen((char *)UserId) > 0 && strlen((char *)price) > 0)
  6515. {
  6516. float ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  6517. float AccountBalance = 0;
  6518. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6519. {
  6520. ConnectionFee = PriceParsing((char *)price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6521. CurrentRate = PriceParsing((char *)price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6522. OccupancyFee = PriceParsing((char *)price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6523. AccountBalance = PriceParsing((char *)price, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6524. LOG_INFO("********* Customer Audi User Price *********");
  6525. LOG_INFO("********* User Id: [%s]", UserId);
  6526. LOG_INFO("********* Connection Fee: [%7.2f]", ConnectionFee);
  6527. LOG_INFO("********* Current Rate: [%7.2f]", CurrentRate);
  6528. LOG_INFO("********* Occupancy Fee: [%7.2f]", OccupancyFee);
  6529. LOG_INFO("********* Account Balance: [%7.2f]", AccountBalance);
  6530. }
  6531. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6532. {
  6533. ShellPriceParsing(price, userPrice);
  6534. LOG_INFO("******** Customer Shell User Price *********");
  6535. LOG_INFO("******** User Id: [%s]", UserId);
  6536. LOG_INFO("******** User Price: [%s]", userPrice);
  6537. }
  6538. else
  6539. {
  6540. CurrentRate = PriceParsing((char *)price, "chargineFee", _PHIHONG_PRICE_SPLIT_KEY);
  6541. OccupancyFee = PriceParsing((char *)price, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6542. strcpy(userPrice, price);
  6543. LOG_INFO("*********** Standard User Price ************");
  6544. LOG_INFO("*********** User Id: [%s]", UserId);
  6545. LOG_INFO("*********** Current Rate: [%7.2f]", CurrentRate);
  6546. LOG_INFO("*********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6547. LOG_INFO("*********** User Price: [%s]", userPrice);
  6548. }
  6549. #if 0
  6550. LOG_INFO("********** User Id: %s **********", UserId);
  6551. LOG_INFO("********** User Price: %s", price);
  6552. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6553. LOG_INFO("********** Current Rate: %7.2f **********", CurrentRate);
  6554. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6555. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6556. #endif
  6557. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6558. {
  6559. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  6560. {
  6561. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  6562. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  6563. {
  6564. LOG_INFO("********** Connector Id %d User Price %4.2f, Account Balance %7.2f **********",
  6565. (i + 1), CurrentRate, AccountBalance);
  6566. }
  6567. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  6568. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  6569. if(strlen(userPrice) > 0)
  6570. {
  6571. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[i][0], userPrice);
  6572. Set_Connector_MiscCommand(i, MISC_CONN_USER_PRICE_STRING);
  6573. }
  6574. if(chargingInfo[i]->SystemStatus == S_AUTHORIZING)
  6575. {
  6576. AnnounceAccountBalance(i, AccountBalance);
  6577. }
  6578. }
  6579. }
  6580. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6581. {
  6582. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthResult == _AuthResult_Valid)
  6583. {
  6584. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthId, UserId) == EQUAL)
  6585. {
  6586. if(strlen(userPrice) > 0)
  6587. {
  6588. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[i][0], userPrice);
  6589. }
  6590. }
  6591. }
  6592. }
  6593. Clean_Ocpp_UserPrice();
  6594. }
  6595. }
  6596. void RunningCostParsing(int gunIndex, char *strCost)
  6597. {
  6598. float ConnectionFee = 0, SessionFee = 0, OccupancyFee = -1, TotalCost = 0, EnergyCost = -1;
  6599. float AccountBalance = 0;
  6600. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6601. {
  6602. ConnectionFee = PriceParsing((char *)strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6603. SessionFee = PriceParsing((char *)strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6604. OccupancyFee = PriceParsing((char *)strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6605. TotalCost = PriceParsing((char *)strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6606. AccountBalance = PriceParsing((char *)strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6607. LOG_INFO("******** Customer Audi Gun %d Running Cost ********", (gunIndex + 1));
  6608. LOG_INFO("******** Connection Fee: [%7.2f]", ConnectionFee);
  6609. LOG_INFO("******** Session Fee: [%7.2f]", SessionFee);
  6610. LOG_INFO("******** Occupancy Fee: [%7.2f]", OccupancyFee);
  6611. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6612. LOG_INFO("******** Account Balance: [%7.2f]", AccountBalance);
  6613. }
  6614. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6615. {
  6616. TotalCost = ShellRunningCostParsing(strCost);
  6617. LOG_INFO("******** Customer Shell Gun %d Running Cost *******", (gunIndex + 1));
  6618. LOG_INFO("******** Gun %d Running Cost", (gunIndex + 1));
  6619. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6620. }
  6621. else
  6622. {
  6623. EnergyCost = PriceParsing((char *)strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6624. TotalCost = EnergyCost;
  6625. OccupancyFee = PriceParsing((char *)strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6626. LOG_INFO("********** Standard Gun %d Running Cost ***********", (gunIndex + 1));
  6627. LOG_INFO("********** Energy Cost: [%7.2f]", EnergyCost);
  6628. LOG_INFO("********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6629. LOG_INFO("********** Total Cost: [%7.2f]", TotalCost);
  6630. }
  6631. #if 0
  6632. LOG_INFO("********** Connector Id: %d Running Cost **********", (gunIndex + 1));
  6633. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6634. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6635. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6636. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6637. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6638. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6639. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance)
  6640. {
  6641. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance %7.2f **********",
  6642. (gunIndex + 1), TotalCost, AccountBalance);
  6643. }
  6644. #endif
  6645. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = TotalCost;
  6646. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = AccountBalance;
  6647. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = EnergyCost;
  6648. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = OccupancyFee;
  6649. }
  6650. void RunningCostHandler(void)
  6651. {
  6652. char strCost[256];
  6653. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6654. {
  6655. Get_Ocpp_RunningCost(i, strCost);
  6656. if(strlen((char *)strCost) > 0)
  6657. {
  6658. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  6659. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_CHARGING)
  6660. {
  6661. RunningCostParsing(i, strCost);
  6662. }
  6663. Clean_Ocpp_RunningCost(i);
  6664. }
  6665. }
  6666. }
  6667. void EnableConnectorBillRequest(int gunIndex, ChargingBillInfoData *bill)
  6668. {
  6669. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6670. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6671. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = bill->TxId;
  6672. memcpy(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, &bill->FeeInfo, sizeof(FeeInfoData));
  6673. }
  6674. void DisableConnectorBill(int gunIndex)
  6675. {
  6676. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Idle;
  6677. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6678. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = 0;
  6679. memset(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, 0x00, sizeof(FeeInfoData));
  6680. }
  6681. void FinalCostParsing(int txId, char *strCost, ChargingBillInfoData *_bill)
  6682. {
  6683. memset(_bill, 0x00, sizeof(ChargingBillInfoData));
  6684. _bill->TxId = txId;
  6685. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6686. {
  6687. _bill->FeeInfo.ConnectionFee = PriceParsing(strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6688. _bill->FeeInfo.SessionFee = PriceParsing(strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6689. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6690. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6691. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6692. }
  6693. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6694. {
  6695. _bill->FeeInfo.TotalCost = ShellFinalCostParsing(strCost);
  6696. GetShellReceiptInfo(strCost, (char *)_bill->Receipt);
  6697. }
  6698. else
  6699. {
  6700. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6701. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6702. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "accountBalance", _PHIHONG_PRICE_SPLIT_KEY);
  6703. _bill->FeeInfo.CostDiscount = PriceParsing(strCost, "couponPoint", _PHIHONG_PRICE_SPLIT_KEY);
  6704. _bill->FeeInfo.RemainAmount = PriceParsing(strCost, "farewellMessage", _PHIHONG_PRICE_SPLIT_KEY);
  6705. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0)
  6706. {
  6707. sprintf(_bill->Receipt, "%s%d",
  6708. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0],
  6709. txId);
  6710. }
  6711. }
  6712. }
  6713. void UpdateCurrentFinalCost(int gunIndex, ChargingBillInfoData *_bill)
  6714. {
  6715. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6716. {
  6717. LOG_INFO("********* Customer Audi Gun %d Final Cost *********", (gunIndex + 1));
  6718. LOG_INFO("********* Connection Fee: [%7.2f]", _bill->FeeInfo.ConnectionFee);
  6719. LOG_INFO("********* Session Fee: [%7.2f]", _bill->FeeInfo.SessionFee);
  6720. LOG_INFO("********* Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6721. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6722. LOG_INFO("********* Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6723. }
  6724. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6725. {
  6726. LOG_INFO("********* Customer Shell Gun %d Final Cost ********", (gunIndex + 1));
  6727. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6728. LOG_INFO("********* Receipt: [%s]", _bill->Receipt);
  6729. }
  6730. else
  6731. {
  6732. LOG_INFO("*********** Standard Gun %d Final Cost ************", (gunIndex + 1));
  6733. LOG_INFO("*********** Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6734. LOG_INFO("*********** Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6735. LOG_INFO("*********** Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6736. LOG_INFO("*********** Coupon Point: [%7.2f]", _bill->FeeInfo.CostDiscount);
  6737. LOG_INFO("*********** Remain Amount: [%7.2f]", _bill->FeeInfo.RemainAmount);
  6738. LOG_INFO("*********** Receipt: [%s]", _bill->Receipt);
  6739. }
  6740. #if 0
  6741. LOG_INFO("********** Connector Id: %d Final Cost **********", (gunIndex + 1));
  6742. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6743. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6744. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6745. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6746. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6747. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6748. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance ||
  6749. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount != CostDiscount)
  6750. {
  6751. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance: %7.2f, Discount: %d.%02d **********",
  6752. (gunIndex + 1), TotalCost, AccountBalance, (CostDiscount / 100), (CostDiscount % 100));
  6753. }
  6754. #endif
  6755. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = _bill->FeeInfo.TotalCost;
  6756. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = _bill->FeeInfo.AccountBalance;
  6757. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount = _bill->FeeInfo.CostDiscount;
  6758. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = _bill->FeeInfo.EnergyCost;
  6759. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = _bill->FeeInfo.ParkingFee;
  6760. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemainAmount = _bill->FeeInfo.RemainAmount;
  6761. Set_Connector_MiscCommand(gunIndex, MISC_CONN_FINAL_COST);
  6762. if(strlen(_bill->Receipt) > 0)
  6763. {
  6764. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], _bill->Receipt);
  6765. Set_Connector_MiscCommand(gunIndex, MISC_CONN_RECEIPT);
  6766. }
  6767. DB_Update_FinalCost_Status(localDb, _bill->TxId, BILL_SATAUS_Bypass_OK);
  6768. }
  6769. bool IsBillStatusChanged(int gunIndex)
  6770. {
  6771. return ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus == ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus ?
  6772. false : true;
  6773. }
  6774. void SyncBillStatus(int gunIndex)
  6775. {
  6776. ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6777. }
  6778. void EnableUndisposedBill(int dispenserIndex)
  6779. {
  6780. LOG_INFO("Enable Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6781. _isUndisposedFinalCost[dispenserIndex] = true;
  6782. }
  6783. void FindUndisposedAndTrigger(int dispenserIndex)
  6784. {
  6785. int txId = 0;
  6786. char strCost[256];
  6787. ChargingBillInfoData _bill;
  6788. bool find = false;
  6789. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6790. {
  6791. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6792. txId = DB_Get_Undisposed_FinalCost(localDb, gun, strCost);
  6793. if(txId >= 0)
  6794. {
  6795. LOG_INFO("Find Gun %d Undisposed ChargingBill, txId: [%d]", gun + 1, txId);
  6796. FinalCostParsing(txId, (char *)strCost, &_bill);
  6797. EnableConnectorBillRequest(gun, &_bill);
  6798. find = true;
  6799. break;
  6800. }
  6801. }
  6802. if(!find)
  6803. {
  6804. //LOG_INFO("Dispenser %d Undisposed ChargingBill Not Found", dispenserIndex + 1);
  6805. _isUndisposedFinalCost[dispenserIndex] = false;
  6806. }
  6807. }
  6808. bool IsDispenserBillStatusBusy(int dispenserIndex)
  6809. {
  6810. bool busy = false;
  6811. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6812. {
  6813. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6814. if(ShmChargerInfo->AllBill.GunBill[gun].BillStatus != _BillStatus_Idle)
  6815. {
  6816. busy = true;
  6817. break;
  6818. }
  6819. }
  6820. return busy;
  6821. }
  6822. void DispenserBillRequestProcess(int dispenserIndex)
  6823. {
  6824. if(IsDispenserIdle(dispenserIndex) && !IsDispenserBillStatusBusy(dispenserIndex))
  6825. {
  6826. if(!_canUpdateFinalCost[dispenserIndex])
  6827. {
  6828. _canUpdateFinalCost[dispenserIndex] = true;
  6829. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6830. }
  6831. if(_canUpdateFinalCost[dispenserIndex] && _isUndisposedFinalCost[dispenserIndex])
  6832. {
  6833. if(GetSecTimeoutValue(_DispenserBill_Time[dispenserIndex]) >= FINAL_COST_UPDATE_TIME)
  6834. {
  6835. //LOG_INFO("Try Find Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6836. FindUndisposedAndTrigger(dispenserIndex);
  6837. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6838. }
  6839. }
  6840. }
  6841. else
  6842. {
  6843. _canUpdateFinalCost[dispenserIndex] = false;
  6844. }
  6845. }
  6846. void GunBillRequestProcess(int gunIndex)
  6847. {
  6848. unsigned char _status = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6849. switch(_status)
  6850. {
  6851. case _BillStatus_Idle:
  6852. if(IsBillStatusChanged(gunIndex))
  6853. {
  6854. SyncBillStatus(gunIndex);
  6855. }
  6856. break;
  6857. case _BillStatus_MiscReq:
  6858. if(IsBillStatusChanged(gunIndex))
  6859. {
  6860. Database_INFO("Gun %d ChargingBill MiscRequest", gunIndex + 1);
  6861. SyncBillStatus(gunIndex);
  6862. }
  6863. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_WaitMiscClean;
  6864. Set_Connector_MiscCommand(gunIndex, MISC_CONN_CHARGING_BILL);
  6865. break;
  6866. case _BillStatus_WaitMiscClean:
  6867. if(IsBillStatusChanged(gunIndex))
  6868. {
  6869. SyncBillStatus(gunIndex);
  6870. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6871. }
  6872. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6873. {
  6874. Database_INFO("Gun %d ChargingBill WaitMisc Timeout", gunIndex + 1);
  6875. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6876. }
  6877. break;
  6878. case _BillStatus_MiscClean:
  6879. if(IsBillStatusChanged(gunIndex))
  6880. {
  6881. Database_INFO("Gun %d ChargingBill MiscClean", gunIndex + 1);
  6882. SyncBillStatus(gunIndex);
  6883. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6884. }
  6885. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6886. {
  6887. Database_INFO("Gun %d ChargingBill Wait Dispenser Timeout", gunIndex + 1);
  6888. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt++;
  6889. if(ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt >= MAX_FINAL_COST_MISC_COUNT)
  6890. {
  6891. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6892. }
  6893. else
  6894. {
  6895. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6896. }
  6897. }
  6898. break;
  6899. case _BillStatus_UpdateDone:
  6900. if(IsBillStatusChanged(gunIndex))
  6901. {
  6902. Database_INFO("Gun %d ChargingBill UpdateDone", gunIndex + 1);
  6903. SyncBillStatus(gunIndex);
  6904. }
  6905. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Completed;
  6906. break;
  6907. case _BillStatus_Completed:
  6908. if(IsBillStatusChanged(gunIndex))
  6909. {
  6910. SyncBillStatus(gunIndex);
  6911. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6912. }
  6913. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6914. {
  6915. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_End;
  6916. }
  6917. break;
  6918. case _BillStatus_Fail:
  6919. if(IsBillStatusChanged(gunIndex))
  6920. {
  6921. SyncBillStatus(gunIndex);
  6922. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_FAIL);
  6923. }
  6924. DisableConnectorBill(gunIndex);
  6925. break;
  6926. case _BillStatus_End:
  6927. if(IsBillStatusChanged(gunIndex))
  6928. {
  6929. SyncBillStatus(gunIndex);
  6930. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_OK);
  6931. }
  6932. DisableConnectorBill(gunIndex);
  6933. break;
  6934. default:
  6935. DisableConnectorBill(gunIndex);
  6936. break;
  6937. }
  6938. }
  6939. void FinalCostHandler(void)
  6940. {
  6941. int txId = 0;
  6942. char strCost[256];
  6943. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6944. {
  6945. txId = Get_Ocpp_FinalCostTxId(i);
  6946. Get_Ocpp_FinalCost(i, strCost);
  6947. if(strlen((char *)strCost) > 0)
  6948. {
  6949. DB_Insert_FinalCost(localDb, i, txId, strCost);
  6950. Database_INFO("Insert FinalCost: Gun %d TxId[%d]", i + 1, txId);
  6951. if(ShmChargerInfo->UserTransaction[i].TransactionId == txId)
  6952. {
  6953. ChargingBillInfoData _bill;
  6954. FinalCostParsing(txId, (char *)strCost, &_bill);
  6955. UpdateCurrentFinalCost(i, &_bill);
  6956. }
  6957. else
  6958. {
  6959. EnableUndisposedBill(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex);
  6960. }
  6961. Clean_Ocpp_FinalCost(i);
  6962. }
  6963. }
  6964. if(ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill)
  6965. {
  6966. ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill = false;
  6967. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6968. {
  6969. _isUndisposedFinalCost[i] = true;
  6970. }
  6971. }
  6972. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6973. {
  6974. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  6975. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  6976. {
  6977. DispenserBillRequestProcess(i);
  6978. }
  6979. else
  6980. {
  6981. _canUpdateFinalCost[i] = false;
  6982. }
  6983. }
  6984. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6985. {
  6986. GunBillRequestProcess(i);
  6987. }
  6988. }
  6989. char MyTimeOffsetString[501];
  6990. void TimeOffsetHandler(void)
  6991. {
  6992. char timeOffset[501];
  6993. Get_Ocpp_TimeOffset((char *)timeOffset);
  6994. if(strcmp(MyTimeOffsetString, (char *)timeOffset) != EQUAL)
  6995. {
  6996. BOOL trigger = FALSE;
  6997. int offset = 0;
  6998. offset = TimeOffsetKeyParsing(timeOffset);
  6999. LOG_INFO("********** Time Offset String [%s] -> Value [%d] -> Time [%s%02d:%02d]",
  7000. timeOffset, offset, offset >= 0 ? "+" : "-", (abs(offset) / 60), (abs(offset) % 60));
  7001. ShmChargerInfo->CabinetMiscValue.TimeOffset = offset;
  7002. #if 0
  7003. time_t timep;
  7004. struct tm *tm;
  7005. time(&timep);
  7006. tm = localtime(&timep);
  7007. LOG_INFO(" NowTime: %d/%02d/%02d %02d:%02d:%02d",
  7008. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  7009. timep += (offset * 60);
  7010. tm = localtime(&timep);
  7011. LOG_INFO("OffsetTime: %d/%02d/%02d %02d:%02d:%02d",
  7012. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  7013. #endif
  7014. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7015. {
  7016. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  7017. {
  7018. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET))
  7019. {
  7020. trigger = TRUE;
  7021. Set_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET);
  7022. }
  7023. }
  7024. }
  7025. if(trigger)
  7026. {
  7027. LOG_INFO("********** Trigger Dispenser Time Offset **********");
  7028. }
  7029. strcpy(MyTimeOffsetString, timeOffset);
  7030. }
  7031. }
  7032. struct timespec _Cost_Price_time;
  7033. void CheckOcppCostAndPrice(void)
  7034. {
  7035. // 5s for reboot request timeout
  7036. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  7037. {
  7038. GetClockTime(&_Cost_Price_time);
  7039. DefaultPriceHandler();
  7040. UserPriceHandler();
  7041. RunningCostHandler();
  7042. TimeOffsetHandler();
  7043. }
  7044. FinalCostHandler();
  7045. }
  7046. void CheckRemoteStartNoIdRequest(void)
  7047. {
  7048. if(Is_Ocpp_RemoteStartWaitReq())
  7049. {
  7050. if(!ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  7051. {
  7052. LOG_INFO("Ocpp remote start without connector id is trigger");
  7053. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = true;
  7054. AnnounceRemoteStartNoID();
  7055. GetClockTime(&_RemoteStartNoId_Time);
  7056. }
  7057. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  7058. {
  7059. // to avoid keep RemoteStartNoId state when ocpp is disconnected
  7060. if((GetTimeoutValue(_RemoteStartNoId_Time) / uSEC_VAL) >= (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + CONNECTION_TIME_OFFSET))
  7061. {
  7062. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  7063. }
  7064. }
  7065. }
  7066. else
  7067. {
  7068. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  7069. {
  7070. LOG_INFO("Ocpp remote start without connector id is expired");
  7071. }
  7072. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  7073. }
  7074. }
  7075. void CheckOcppPageChangeRequest(void)
  7076. {
  7077. int gunId = 0, pageIndex = 0;
  7078. if(Is_Ocpp_LcdOverideReq(&gunId, &pageIndex))
  7079. {
  7080. Clean_Ocpp_LcdOverideReq();
  7081. LOG_INFO("Gun %d Ocpp Line Status [%d] is trigger", gunId, pageIndex);
  7082. AnnounceLineStatusCode(gunId - 1, pageIndex);
  7083. }
  7084. }
  7085. void CheckLcmPageChangeRequest(void)
  7086. {
  7087. bool isTrigger = false;
  7088. CheckRemoteStartNoIdRequest();
  7089. CheckOcppPageChangeRequest();
  7090. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  7091. {
  7092. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  7093. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  7094. {
  7095. isTrigger = false;
  7096. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  7097. {
  7098. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  7099. if(ShmChargerInfo->ConnectorPageReq[gun].CtrlValue != 0)
  7100. {
  7101. isTrigger = true;
  7102. break;
  7103. }
  7104. }
  7105. if(isTrigger)
  7106. {
  7107. Set_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  7108. }
  7109. else
  7110. {
  7111. Clean_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  7112. }
  7113. }
  7114. }
  7115. }
  7116. void CheckStationInfo(void)
  7117. {
  7118. int station_id = 0;
  7119. char station_name[64];
  7120. if(Is_Ocpp_ChargerStationReq())
  7121. {
  7122. memset(station_name, 0x00, sizeof(station_name));
  7123. Get_Ocpp_ChargerStationInfo(&station_id, station_name);
  7124. if(strcmp(station_name, ShmChargerInfo->StationInfo.StationName) != EQUAL ||
  7125. ShmChargerInfo->StationInfo.StationID != station_id)
  7126. {
  7127. strcpy(ShmChargerInfo->StationInfo.StationName, station_name);
  7128. ShmChargerInfo->StationInfo.StationID = station_id;
  7129. LOG_INFO("Station ID: %d, Name: %s", ShmChargerInfo->StationInfo.StationID, ShmChargerInfo->StationInfo.StationName);
  7130. AnnounceStationInfo();
  7131. }
  7132. Clean_Ocpp_ChargerStationReq();
  7133. }
  7134. }
  7135. void CheckWeatherInfo(void)
  7136. {
  7137. int weatherID = 0;
  7138. float temperature = 0;
  7139. char strWeather[16];
  7140. 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};
  7141. if(Is_Ocpp_WeatherReq())
  7142. {
  7143. Get_Ocpp_WeatherInfo(&weatherID, &temperature);
  7144. if(weatherID != ShmChargerInfo->StationInfo.WeatherID ||
  7145. temperature != ShmChargerInfo->StationInfo.Temperature)
  7146. {
  7147. ShmChargerInfo->StationInfo.WeatherID = weatherID;
  7148. ShmChargerInfo->StationInfo.Temperature = temperature;
  7149. if(ShmChargerInfo->StationInfo.WeatherID <= _WEATHER_Mist)
  7150. {
  7151. strcpy(strWeather, str_weather_type[ShmChargerInfo->StationInfo.WeatherID]);
  7152. }
  7153. else
  7154. {
  7155. sprintf(strWeather, "%d", ShmChargerInfo->StationInfo.WeatherID);
  7156. }
  7157. LOG_INFO("Weather: [%s][%d], Temperature: %4.1f", strWeather, ShmChargerInfo->StationInfo.WeatherID, ShmChargerInfo->StationInfo.Temperature);
  7158. AnnounceStationInfo();
  7159. }
  7160. Clean_Ocpp_WeatherReq();
  7161. }
  7162. }
  7163. bool _WriteDeductBusy[CONNECTOR_QUANTITY] = {0};
  7164. void CheckDeductResultRequest(void)
  7165. {
  7166. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7167. char cardNo[21];
  7168. char appNo[10];
  7169. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7170. {
  7171. if(Is_DeductResultReq(i))
  7172. {
  7173. if(!_WriteDeductBusy[i])
  7174. {
  7175. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7176. {
  7177. _WriteDeductBusy[i] = true;
  7178. //Set_Ocpp_DeductTransaction(_BackupDeductResult[i].TransactionId);
  7179. //Set_Ocpp_DeductCreditNo((char *)&_BackupDeductResult[i].creditNo);
  7180. //Set_Ocpp_DeductVemData("");
  7181. //Set_Ocpp_DeductAmount(((double)_BackupDeductResult[i].DeductAmount / 100));
  7182. //Set_Ocpp_DeductResult(_BackupDeductResult[i].DeductResult);
  7183. //Set_Ocpp_DonateInvoice(_BackupDeductResult[i].IsDonateInvoice);
  7184. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->DeductResultReq[i]))
  7185. {
  7186. memset(cardNo, 0x00, sizeof(cardNo));
  7187. memcpy(cardNo, (char *)&ShmChargerInfo->DeductResultReq[i].creditNo[0], sizeof(ShmChargerInfo->DeductResultReq[i].creditNo));
  7188. memset(appNo, 0x00, sizeof(appNo));
  7189. memcpy(appNo, (char *)&ShmChargerInfo->DeductResultReq[i].ApprovalNumber[0], sizeof(ShmChargerInfo->DeductResultReq[i].ApprovalNumber));
  7190. Set_Ocpp_ReportCreditDeductReq();
  7191. LOG_INFO("DeductReport Gun %d TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7192. i + 1,
  7193. ShmChargerInfo->DeductResultReq[i].TransactionId,
  7194. cardNo,
  7195. appNo,
  7196. (ShmChargerInfo->DeductResultReq[i].DeductAmount / 100),
  7197. (ShmChargerInfo->DeductResultReq[i].DeductAmount % 100),
  7198. str_ReaderStatus[ShmChargerInfo->DeductResultReq[i].DeductResult],
  7199. ShmChargerInfo->DeductResultReq[i].IsDonateInvoice ? "Yes" : "No");
  7200. if(ShmChargerInfo->DeductResultReq[i].DeductResult == _Deduct_PreAuth_Pass)
  7201. {
  7202. memcpy(&ShmChargerInfo->ReaderStatus[i], &ShmChargerInfo->DeductResultReq[i], sizeof(DeductResultInfoData));
  7203. ShmChargerInfo->ReaderStatus[i].ReaderStatusReq = YES;
  7204. }
  7205. }
  7206. }
  7207. }
  7208. else
  7209. {
  7210. if(!Is_Ocpp_ReportCreditDeductReq())
  7211. {
  7212. Clean_DeductInfo(i);
  7213. _WriteDeductBusy[i] = false;
  7214. }
  7215. }
  7216. }
  7217. else
  7218. {
  7219. _WriteDeductBusy[i] = false;
  7220. }
  7221. }
  7222. }
  7223. bool _WriteReDeductBusy = false;
  7224. void CheckReDeductRequest(void)
  7225. {
  7226. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7227. char cardNo[21];
  7228. char appNo[10];
  7229. if(ShmChargerInfo->ReDeductReq.DeductReq)
  7230. {
  7231. if(!_WriteReDeductBusy)
  7232. {
  7233. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7234. {
  7235. _WriteReDeductBusy = true;
  7236. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->ReDeductReq))
  7237. {
  7238. memset(cardNo, 0x00, sizeof(cardNo));
  7239. memcpy(cardNo, (char *)&ShmChargerInfo->ReDeductReq.creditNo[0], sizeof(ShmChargerInfo->ReDeductReq.creditNo));
  7240. memset(appNo, 0x00, sizeof(appNo));
  7241. memcpy(appNo, (char *)&ShmChargerInfo->ReDeductReq.ApprovalNumber[0], sizeof(ShmChargerInfo->ReDeductReq.ApprovalNumber));
  7242. Set_Ocpp_ReportCreditDeductReq();
  7243. LOG_INFO("ReDeductReport TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7244. ShmChargerInfo->ReDeductReq.TransactionId,
  7245. cardNo,
  7246. appNo,
  7247. (ShmChargerInfo->ReDeductReq.DeductAmount / 100),
  7248. (ShmChargerInfo->ReDeductReq.DeductAmount % 100),
  7249. str_ReaderStatus[ShmChargerInfo->ReDeductReq.DeductResult],
  7250. ShmChargerInfo->ReDeductReq.IsDonateInvoice ? "Yes" : "No");
  7251. if(ShmChargerInfo->ReDeductReq.DeductResult == _Deduct_PreAuth_Pass && ShmChargerInfo->ReDeductReq.ConnectorId != 0xFF)
  7252. {
  7253. int gunIndex = ShmChargerInfo->ReDeductReq.ConnectorId - 1;
  7254. memcpy(&ShmChargerInfo->ReaderStatus[gunIndex], &ShmChargerInfo->ReDeductReq, sizeof(DeductResultInfoData));
  7255. ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq = YES;
  7256. }
  7257. }
  7258. }
  7259. }
  7260. else
  7261. {
  7262. if(!Is_Ocpp_ReportCreditDeductReq())
  7263. {
  7264. memset(&ShmChargerInfo->ReDeductReq, 0x00, sizeof(DeductResultInfoData));
  7265. _WriteReDeductBusy = false;
  7266. }
  7267. }
  7268. }
  7269. else
  7270. {
  7271. _WriteReDeductBusy = false;
  7272. }
  7273. }
  7274. bool _WriteReaderStatusBusy[CONNECTOR_QUANTITY] = {0};
  7275. void CheckReaderStatusRequest(void)
  7276. {
  7277. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7278. char cardNo[21];
  7279. char appNo[10];
  7280. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7281. {
  7282. if(Is_ReaderStatusReq(i))
  7283. {
  7284. if(!_WriteReaderStatusBusy[i])
  7285. {
  7286. if(!Is_Ocpp_TcciReaderStatusReq(i) && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7287. {
  7288. _WriteReaderStatusBusy[i] = true;
  7289. if(Set_Ocpp_TcciReaderStatus(i, &ShmChargerInfo->ReaderStatus[i]))
  7290. {
  7291. memset(cardNo, 0x00, sizeof(cardNo));
  7292. memcpy(cardNo, (char *)&ShmChargerInfo->ReaderStatus[i].creditNo[0], sizeof(ShmChargerInfo->ReaderStatus[i].creditNo));
  7293. memset(appNo, 0x00, sizeof(appNo));
  7294. memcpy(appNo, (char *)&ShmChargerInfo->ReaderStatus[i].ApprovalNumber[0], sizeof(ShmChargerInfo->ReaderStatus[i].ApprovalNumber));
  7295. Set_Ocpp_TcciReaderStatusReq(i);
  7296. LOG_INFO("StatusReport Gun %d: [%s], CreditNo: [%s], ApprovalNo: [%s]",
  7297. i + 1, str_ReaderStatus[ShmChargerInfo->ReaderStatus[i].DeductResult], cardNo, appNo);
  7298. }
  7299. }
  7300. }
  7301. else
  7302. {
  7303. if(!Is_Ocpp_TcciReaderStatusReq(i))
  7304. {
  7305. Clean_ReaderStatusInfo(i);
  7306. _WriteReaderStatusBusy[i] = false;
  7307. }
  7308. }
  7309. }
  7310. else
  7311. {
  7312. _WriteReaderStatusBusy[i] = false;
  7313. }
  7314. }
  7315. }
  7316. void CheckOccupancyPrice(void)
  7317. {
  7318. float _price = 0;
  7319. if(Is_Ocpp_TcciSetOccupancyPriceReq())
  7320. {
  7321. _price = Get_Ocpp_TcciOccupancyPrice();
  7322. if(ShmChargerInfo->CabinetMiscValue.ParkingPrice != _price)
  7323. {
  7324. ShmChargerInfo->CabinetMiscValue.ParkingPrice = _price;
  7325. LOG_INFO("Set OccupancyPrice: [%6.2f]", ShmChargerInfo->CabinetMiscValue.ParkingPrice);
  7326. AnnounceOccupancyPrice();
  7327. }
  7328. Clean_Ocpp_TcciSetOccupancyPriceReq();
  7329. }
  7330. }
  7331. bool IsParkingButtonStatusChanged(int gunIndex)
  7332. {
  7333. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus ?
  7334. false : true;
  7335. }
  7336. void SyncParkingButtonStatus(int gunIndex)
  7337. {
  7338. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7339. }
  7340. int OccupancyButtonSync(int gunIndex)
  7341. {
  7342. int _pass = FAIL;
  7343. unsigned char _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7344. switch(_status)
  7345. {
  7346. case _BillStatus_Idle:
  7347. if(IsParkingButtonStatusChanged(gunIndex))
  7348. {
  7349. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7350. SyncParkingButtonStatus(gunIndex);
  7351. }
  7352. break;
  7353. case _BillStatus_MiscReq:
  7354. if(IsParkingButtonStatusChanged(gunIndex))
  7355. {
  7356. Occupancy_INFO("Gun %d OccupancyButton [MiscRequest]", gunIndex + 1);
  7357. SyncParkingButtonStatus(gunIndex);
  7358. }
  7359. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_WaitMiscClean;
  7360. Set_Connector_MiscCommand(gunIndex, MISC_CONN_PARKING_STATUS);
  7361. break;
  7362. case _BillStatus_WaitMiscClean:
  7363. if(IsParkingButtonStatusChanged(gunIndex))
  7364. {
  7365. SyncParkingButtonStatus(gunIndex);
  7366. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7367. }
  7368. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7369. {
  7370. Occupancy_INFO("Gun %d OccupancyButton [WaitMisc Timeout]", gunIndex + 1);
  7371. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7372. }
  7373. break;
  7374. case _BillStatus_MiscClean:
  7375. if(IsParkingButtonStatusChanged(gunIndex))
  7376. {
  7377. Occupancy_INFO("Gun %d OccupancyButton [MiscClean]", gunIndex + 1);
  7378. SyncParkingButtonStatus(gunIndex);
  7379. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7380. }
  7381. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7382. {
  7383. LOG_INFO("Gun %d OccupancyButton [Wait Timeout]", gunIndex + 1);
  7384. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7385. }
  7386. break;
  7387. case _BillStatus_UpdateDone:
  7388. if(IsParkingButtonStatusChanged(gunIndex))
  7389. {
  7390. Occupancy_INFO("Gun %d OccupancyButton [UpdateDone]", gunIndex + 1);
  7391. SyncParkingButtonStatus(gunIndex);
  7392. }
  7393. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_Idle;
  7394. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7395. _pass = PASS;
  7396. break;
  7397. default:
  7398. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7399. break;
  7400. }
  7401. return _pass;
  7402. }
  7403. int NotifyOccupancyFeeActRequest(int gunIndex, int action)
  7404. {
  7405. char *str_notify_occupancy_act[] = {STR_NOTIFY_NONE, STR_GET_OCCUPANCY_FEE, STR_CANCEL_OCCUPANCY_DEDUCT, STR_CANCEL_OCCUPANCY_FEE};
  7406. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_None)
  7407. {
  7408. if(!Is_Ocpp_TcciOccupancyFeeActReq(gunIndex))
  7409. {
  7410. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7411. Set_Ocpp_TcciOccupancyFeeAct(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill, action);
  7412. _notifyOccupancyFeeAct[gunIndex] = action;
  7413. LOG_INFO("Gun %d Set OccupancyFee Action [%s] Request", gunIndex + 1, str_notify_occupancy_act[action]);
  7414. }
  7415. }
  7416. else
  7417. {
  7418. if(!Is_Ocpp_TcciOccupancyFeeActReq(gunIndex))
  7419. {
  7420. if(Is_Ocpp_TcciOccupancyFeeActStatus(gunIndex, "Accepted"))
  7421. {
  7422. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_GetOccupancyFee)
  7423. {
  7424. Get_Ocpp_TcciGetOccupancyFeeResponse(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill);
  7425. LOG_INFO("Gun %d Get Occupancy Fee: [%.2f], Duration: [%d], StartTime: [%s]",
  7426. gunIndex + 1,
  7427. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Fee,
  7428. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Duration,
  7429. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.StartTime);
  7430. }
  7431. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_CancelLocalDeduction)
  7432. {
  7433. LOG_INFO("Gun %d Occupancy Deduction Stopped", gunIndex + 1);
  7434. }
  7435. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_CancelOccupancyFee)
  7436. {
  7437. LOG_INFO("Gun %d Occupancy Fee Force Clean", gunIndex + 1);
  7438. }
  7439. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7440. return PASS;
  7441. }
  7442. else
  7443. {
  7444. LOG_INFO("Gun %d OccupancyFee Action [%s] Rejected", gunIndex + 1, str_notify_occupancy_act[action]);
  7445. return FAIL;
  7446. }
  7447. }
  7448. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7449. {
  7450. // shall not reach here
  7451. Clean_Ocpp_TcciNotifyOccupancyFeeActReq(gunIndex);
  7452. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7453. LOG_INFO("Gun %d OccupancyFee Action [%s] Request Timeout", gunIndex + 1, str_notify_occupancy_act[action]);
  7454. return FAIL;
  7455. }
  7456. }
  7457. return WAIT;
  7458. }
  7459. bool IsParkingStatusChanged(int gunIndex)
  7460. {
  7461. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus ?
  7462. false : true;
  7463. }
  7464. void SyncParkingStatus(int gunIndex)
  7465. {
  7466. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7467. }
  7468. bool Is_Occupancy_Cancel(int gunIndex)
  7469. {
  7470. unsigned char dispenser = 0;
  7471. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParentDispensetIndex;
  7472. if((!ShmSysConfigAndInfo->SysInfo.OcppConnStatus && !ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq) ||
  7473. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_None ||
  7474. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_Timeout)
  7475. {
  7476. return true;
  7477. }
  7478. return false;
  7479. }
  7480. void OccupancyRequest(int gunIndex)
  7481. {
  7482. int _action = 0;
  7483. bool feeRequest = false;
  7484. bool keepCounting = false;
  7485. int res = FAIL;
  7486. char _sn[37];
  7487. unsigned char _status = 0;
  7488. char *str_action[] = {STR_OCCUPANCY_ACT_NONE, STR_OCCUPANCY_ACT_DISPLAY, STR_OCCUPANCY_ACT_CANCEL, STR_OCCUPANCY_ACT_PAID_OK, STR_OCCUPANCY_ACT_PAID_NG};
  7489. _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7490. if(Is_Ocpp_TcciOccupancyDisplayReq(gunIndex))
  7491. {
  7492. memset(_sn, 0x00, sizeof(_sn));
  7493. _action = Get_Ocpp_TcciOccupancyAction(gunIndex, sizeof(_sn) - 1, _sn);
  7494. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ActionId = _action;
  7495. LOG_INFO("Gun %d OccupancyAction is [%s]", gunIndex + 1, str_action[_action]);
  7496. Clean_Ocpp_TcciOccupancyDisplayReq(gunIndex);
  7497. }
  7498. if(ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest)
  7499. {
  7500. feeRequest = true;
  7501. ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest =false;
  7502. }
  7503. if(Is_OccupancyKeepCountingReq(gunIndex))
  7504. {
  7505. if(_status == _Parking_WaitToPay)
  7506. {
  7507. keepCounting = true;
  7508. }
  7509. else
  7510. {
  7511. Clean_OccupancyKeepCountingReq(gunIndex);
  7512. }
  7513. }
  7514. if(_status != _Parking_NoOccupancy && chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7515. {
  7516. if(!Is_OccupancyFeeForceCancelReq(gunIndex))
  7517. {
  7518. Set_OccupancyFeeForceCancelReq(gunIndex);
  7519. Occupancy_INFO("Gun %d Need Force Cancel OccupancyStatus", gunIndex + 1);
  7520. }
  7521. }
  7522. if(Is_OccupancyFeeForceCancelReq(gunIndex))
  7523. {
  7524. res = NotifyOccupancyFeeActRequest(gunIndex, _Notify_CancelOccupancyFee);
  7525. if(res == PASS || res == FAIL)
  7526. {
  7527. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7528. Clean_OccupancyFeeForceCancelReq(gunIndex);
  7529. return;
  7530. }
  7531. else
  7532. {
  7533. // wait response
  7534. }
  7535. }
  7536. switch(_status)
  7537. {
  7538. case _Parking_NoOccupancy:
  7539. if(IsParkingStatusChanged(gunIndex))
  7540. {
  7541. Occupancy_INFO("Gun %d OccupancyStatus [Clean]", gunIndex + 1);
  7542. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex], 0x00, sizeof(OccupancyInfoData));
  7543. SyncParkingStatus(gunIndex);
  7544. }
  7545. if(_action == _ParkingAction_DisplayButton)
  7546. {
  7547. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7548. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_SyncOccupancy;
  7549. }
  7550. break;
  7551. case _Parking_SyncOccupancy:
  7552. if(IsParkingStatusChanged(gunIndex))
  7553. {
  7554. Occupancy_INFO("Gun %d OccupancyStatus [Sync]", gunIndex + 1);
  7555. SyncParkingStatus(gunIndex);
  7556. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7557. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7558. }
  7559. if(_action == _ParkingAction_DisplayButton)
  7560. {
  7561. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7562. }
  7563. else if(_action == _ParkingAction_CancelOccupancy ||
  7564. Is_Occupancy_Cancel(gunIndex))
  7565. {
  7566. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7567. }
  7568. else if(_action == _ParkingAction_PaidOnlineOK)
  7569. {
  7570. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7571. }
  7572. else
  7573. {
  7574. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  7575. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  7576. {
  7577. if(OccupancyButtonSync(gunIndex) == PASS)
  7578. {
  7579. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7580. }
  7581. }
  7582. }
  7583. break;
  7584. case _Parking_Occupied:
  7585. if(IsParkingStatusChanged(gunIndex))
  7586. {
  7587. Occupancy_INFO("Gun %d OccupancyStatus [Start]", gunIndex + 1);
  7588. SyncParkingStatus(gunIndex);
  7589. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7590. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7591. }
  7592. if(_action == _ParkingAction_DisplayButton)
  7593. {
  7594. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7595. }
  7596. else if(_action == _ParkingAction_CancelOccupancy ||
  7597. Is_Occupancy_Cancel(gunIndex))
  7598. {
  7599. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7600. }
  7601. else if(_action == _ParkingAction_PaidOnlineOK)
  7602. {
  7603. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7604. }
  7605. else if(_action == _ParkingAction_PaidOnlineFail)
  7606. {
  7607. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7608. }
  7609. else
  7610. {
  7611. if(feeRequest)
  7612. {
  7613. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_RequestFee;
  7614. }
  7615. }
  7616. break;
  7617. case _Parking_RequestFee:
  7618. if(IsParkingStatusChanged(gunIndex))
  7619. {
  7620. Occupancy_INFO("Gun %d OccupancyStatus [RequestFee]", gunIndex + 1);
  7621. SyncParkingStatus(gunIndex);
  7622. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7623. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7624. }
  7625. if(_action == _ParkingAction_CancelOccupancy ||
  7626. Is_Occupancy_Cancel(gunIndex))
  7627. {
  7628. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7629. }
  7630. else if(_action == _ParkingAction_PaidOnlineOK)
  7631. {
  7632. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7633. }
  7634. else if(_action == _ParkingAction_PaidOnlineFail)
  7635. {
  7636. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7637. }
  7638. else
  7639. {
  7640. if(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq)
  7641. {
  7642. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7643. }
  7644. else
  7645. {
  7646. res = NotifyOccupancyFeeActRequest(gunIndex, _Notify_GetOccupancyFee);
  7647. if(res == PASS)
  7648. {
  7649. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7650. }
  7651. else if(res == FAIL)
  7652. {
  7653. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7654. }
  7655. else
  7656. {
  7657. // wait response
  7658. }
  7659. }
  7660. }
  7661. break;
  7662. case _Parking_WaitToPay:
  7663. if(IsParkingStatusChanged(gunIndex))
  7664. {
  7665. Occupancy_INFO("Gun %d OccupancyStatus [WaitToPay]", gunIndex + 1);
  7666. SyncParkingStatus(gunIndex);
  7667. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].BillReqStatus, 0x00, sizeof(MiscRequestStatus));
  7668. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7669. }
  7670. if(_action == _ParkingAction_CancelOccupancy ||
  7671. Is_Occupancy_Cancel(gunIndex))
  7672. {
  7673. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7674. }
  7675. else if(_action == _ParkingAction_PaidOnlineOK)
  7676. {
  7677. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7678. }
  7679. else if(_action == _ParkingAction_PaidOnlineFail)
  7680. {
  7681. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7682. }
  7683. else if(keepCounting)
  7684. {
  7685. res = NotifyOccupancyFeeActRequest(gunIndex, _Notify_CancelLocalDeduction);
  7686. if(res == PASS)
  7687. {
  7688. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7689. Clean_OccupancyKeepCountingReq(gunIndex);
  7690. }
  7691. else if(res == FAIL)
  7692. {
  7693. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7694. Clean_OccupancyKeepCountingReq(gunIndex);
  7695. }
  7696. else
  7697. {
  7698. // wait response
  7699. }
  7700. }
  7701. break;
  7702. case _Parking_PaidOnlineOK:
  7703. if(IsParkingStatusChanged(gunIndex))
  7704. {
  7705. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineOK]", gunIndex + 1);
  7706. SyncParkingStatus(gunIndex);
  7707. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7708. }
  7709. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7710. {
  7711. LOG_INFO("Gun %d Reset OccupancyStatus via PaidOnlineOK", gunIndex + 1);
  7712. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7713. }
  7714. break;
  7715. case _Parking_PaidOnlineFail:
  7716. if(IsParkingStatusChanged(gunIndex))
  7717. {
  7718. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineFail]", gunIndex + 1);
  7719. SyncParkingStatus(gunIndex);
  7720. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7721. }
  7722. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7723. {
  7724. LOG_INFO("Gun %d Return to Occupied via PaidOnlineFail", gunIndex + 1);
  7725. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7726. }
  7727. break;
  7728. default:
  7729. DisableConnectorBill(gunIndex);
  7730. break;
  7731. }
  7732. }
  7733. void CheckOccupancyFee(void)
  7734. {
  7735. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7736. {
  7737. OccupancyRequest(i);
  7738. }
  7739. }
  7740. #define OCCUPANCY_DEDUCT_IDLE 0
  7741. #define OCCUPANCY_DEDUCT_REQUEST 1
  7742. unsigned char _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7743. int _OccupancyReportGunId = 0;
  7744. void CheckOccupancyDeduct(void)
  7745. {
  7746. char cardNo[21];
  7747. char appNo[10];
  7748. if(_OccupancyDeductStatus == OCCUPANCY_DEDUCT_IDLE)
  7749. {
  7750. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7751. {
  7752. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7753. {
  7754. if(Is_OccupancyDeductReq(i))
  7755. {
  7756. if(ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyReq.bits.SelfReq &&
  7757. ShmChargerInfo->AllBill.OccupancyInfo[i].Status == _Parking_WaitToPay)
  7758. {
  7759. ShmChargerInfo->AllBill.OccupancyInfo[i].Status = _Parking_NoOccupancy;
  7760. LOG_INFO("Gun %d Occupancy Concluded!", i + 1);
  7761. break;
  7762. }
  7763. Set_Ocpp_OccupancyDeductResult(&ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyDeduct, YES);
  7764. Set_Ocpp_TcciReportOccupancyDeductReq();
  7765. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_REQUEST;
  7766. _OccupancyReportGunId = i + 1;
  7767. Clean_OccupancyDeductReq(i);
  7768. break;
  7769. }
  7770. }
  7771. if(_OccupancyDeductStatus != OCCUPANCY_DEDUCT_IDLE)
  7772. {
  7773. memset(cardNo, 0x00, sizeof(cardNo));
  7774. memset(appNo, 0x00, sizeof(appNo));
  7775. memcpy(cardNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo));
  7776. memcpy(appNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber));
  7777. LOG_INFO("Gun %d Occupancy Deduct [Request] Sn: [%s], Fee: [%.2f], CreditNo: [%s], ApprovalNo: [%s]",
  7778. _OccupancyReportGunId,
  7779. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Sn,
  7780. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Fee,
  7781. cardNo,
  7782. appNo);
  7783. }
  7784. }
  7785. }
  7786. else
  7787. {
  7788. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7789. {
  7790. if(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status == _Parking_WaitToPay)
  7791. {
  7792. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status = _Parking_NoOccupancy;
  7793. LOG_INFO("Gun %d OccupancyDeduct Request Completed!", _OccupancyReportGunId);
  7794. }
  7795. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7796. }
  7797. }
  7798. }
  7799. void CheckDiagnosticsReq(void)
  7800. {
  7801. bool _DiagnosticsCompleted = true;
  7802. int _DispenserId = 0;
  7803. char _IpAddress[32];
  7804. char cmdBuf[256] = {0};
  7805. if(Is_Ocpp_GetDiagnosticsReq() || ShmChargerInfo->Control.Diagnostics.DiagnosticsType != DIAGNOSTICS_TYPE_NONE)
  7806. {
  7807. // check dispenser status and set diagnostics request
  7808. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  7809. {
  7810. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  7811. {
  7812. _DispenserId = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex + 1;
  7813. if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_DispenserId - 1].bits.Request)
  7814. {
  7815. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_DispenserId - 1].CtrlValue = 0;
  7816. sprintf((char*)_IpAddress, "%d.%d.%d.%d",
  7817. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  7818. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  7819. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  7820. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  7821. sprintf(cmdBuf, "/root/Module_Diagnostics %d %s &", _DispenserId, _IpAddress);
  7822. system(cmdBuf);
  7823. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_DispenserId - 1].bits.Request = true;
  7824. }
  7825. if(!_NeedDiagnostics)
  7826. {
  7827. GetClockTime(&_Diagnostics_Time);
  7828. LOG_INFO("Dispenser Need Diagnostics!");
  7829. }
  7830. _NeedDiagnostics = true;
  7831. }
  7832. }
  7833. if(_NeedDiagnostics)
  7834. {
  7835. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  7836. {
  7837. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  7838. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  7839. {
  7840. if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Stop)
  7841. {
  7842. if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Confirm)
  7843. {
  7844. if(GetSecTimeoutValue(_Diagnostics_Time) >= WAIT_DIAGNOSTICS_CONFIRM)
  7845. {
  7846. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Stop = true;
  7847. LOG_INFO("Wait Dispenser %d diagnostics confirm timeout!", i + 1);
  7848. }
  7849. _DiagnosticsCompleted = false;
  7850. }
  7851. else if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Completed)
  7852. {
  7853. if(GetSecTimeoutValue(_Diagnostics_Time) >= WAIT_DIAGNOSTICS_TIMEOUT)
  7854. {
  7855. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Stop = true;
  7856. LOG_INFO("Wait Dispenser %d diagnostics completed timeout!", i + 1);
  7857. }
  7858. _DiagnosticsCompleted = false;
  7859. }
  7860. }
  7861. }
  7862. }
  7863. }
  7864. if(_DiagnosticsCompleted)
  7865. {
  7866. // diagnostics completed, clean ocpp GetDiagnosticsReq
  7867. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  7868. {
  7869. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].CtrlValue = 0;
  7870. }
  7871. if(Is_Ocpp_GetDiagnosticsReq())
  7872. {
  7873. Clean_Ocpp_GetDiagnosticsReq();
  7874. }
  7875. if(ShmChargerInfo->Control.Diagnostics.DiagnosticsType != DIAGNOSTICS_TYPE_NONE)
  7876. {
  7877. ShmChargerInfo->Control.Diagnostics.DiagnosticsType = DIAGNOSTICS_TYPE_NONE;
  7878. }
  7879. LOG_INFO("Dispenser Diagnostics Completed!");
  7880. }
  7881. }
  7882. else
  7883. {
  7884. _NeedDiagnostics = false;
  7885. }
  7886. }
  7887. //===============================================
  7888. // OCPP
  7889. //===============================================
  7890. void CheckOcppStatus()
  7891. {
  7892. if (Is_Ocpp_BootNotification())
  7893. {
  7894. Clean_Ocpp_BootNotification();
  7895. LOG_INFO("*********************OCPP Boot Notification****************************");
  7896. }
  7897. if (Is_Ocpp_ResetReq())
  7898. {
  7899. bool canReset = true;
  7900. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  7901. {
  7902. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7903. {
  7904. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  7905. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  7906. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  7907. {
  7908. canReset = false;
  7909. //if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  7910. //{
  7911. // ChargingTerminalProcess(_index);
  7912. //}
  7913. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7914. {
  7915. chargingInfo[_index]->ChargingStopFlag.bits.HardResetStop = true;
  7916. }
  7917. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7918. {
  7919. chargingInfo[_index]->ChargingStopFlag.bits.SoftResetStop = true;
  7920. }
  7921. }
  7922. }
  7923. }
  7924. if(canReset)
  7925. {
  7926. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7927. {
  7928. //trigger dispenser to hardware reboot
  7929. TriggerDispenserHardwareReboot();
  7930. if(IsDispenserHardwareRebootResponsed())
  7931. {
  7932. Clean_Ocpp_ResetReq();
  7933. LOG_INFO("****** Hard Reboot ******");
  7934. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7935. {
  7936. setChargerMode(i, MODE_MAINTAIN);
  7937. }
  7938. sleep(3);
  7939. system("reboot -f");
  7940. }
  7941. }
  7942. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7943. {
  7944. //trigger dispenser to software reset
  7945. TriggerDispenserSoftwareReset();
  7946. if(IsDispenserSoftwareResetResponsed())
  7947. {
  7948. Clean_Ocpp_ResetReq();
  7949. LOG_INFO("****** Soft Reboot ******");
  7950. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7951. {
  7952. setChargerMode(i, MODE_MAINTAIN);
  7953. }
  7954. sleep(3);
  7955. Kill_Ocpp_Service();
  7956. KillAllTask();
  7957. TryCloseWatchdog();
  7958. system("/usr/bin/run_evse_restart.sh");
  7959. }
  7960. }
  7961. }
  7962. }
  7963. CheckOcppCostAndPrice();
  7964. CheckLcmPageChangeRequest();
  7965. CheckStationInfo();
  7966. CheckWeatherInfo();
  7967. CheckDeductResultRequest();
  7968. CheckReDeductRequest();
  7969. CheckReaderStatusRequest();
  7970. CheckOccupancyPrice();
  7971. CheckOccupancyFee();
  7972. CheckOccupancyDeduct();
  7973. CheckDiagnosticsReq();
  7974. }
  7975. void CheckoutStartChargingSoc(byte gunIndex, bool force)
  7976. {
  7977. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc)
  7978. {
  7979. if(chargingInfo[gunIndex]->EvBatterySoc > 0 || force)
  7980. {
  7981. chargingInfo[gunIndex]->EvBatteryStartSoc = chargingInfo[gunIndex]->EvBatterySoc;
  7982. LOG_INFO("Gun %d StartSoc = %d", chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->EvBatteryStartSoc);
  7983. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc = true;
  7984. }
  7985. }
  7986. }
  7987. void UpdateChargingTimestamp(char *timestamp)
  7988. {
  7989. struct timeb SeqEndTime;
  7990. struct tm *tm;
  7991. ftime(&SeqEndTime);
  7992. SeqEndTime.time = time(NULL);
  7993. tm = localtime(&SeqEndTime.time);
  7994. 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);
  7995. }
  7996. void OcppStartTransaction(byte gunIndex)
  7997. {
  7998. if(_triggerTransaction[gunIndex])
  7999. {
  8000. return;
  8001. }
  8002. // clean deduct info
  8003. Clean_DeductInfo(gunIndex);
  8004. // set deduct user id
  8005. //Set_Deduct_UserId(gunIndex, (char *)&chargingInfo[gunIndex]->StartUserId);
  8006. // initial start & stop datetime
  8007. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00");
  8008. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, "00:00:00");
  8009. Update_PowerConsumption(gunIndex);
  8010. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  8011. Set_Ocpp_StartTransactionIdTag(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  8012. LOG_INFO("Gun %d Start Transaction IdTag = %s, MeterValue: %.4f",
  8013. chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->StartUserId, chargingInfo[gunIndex]->PowerConsumption);
  8014. Clean_Ocpp_StartTransactionConf(gunIndex);
  8015. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StartCharging);
  8016. Set_Ocpp_StartTransactionReq(gunIndex);
  8017. // set _triggerTransaction
  8018. _triggerTransaction[gunIndex] = true;
  8019. }
  8020. void OcppStopTransaction(byte gunIndex)
  8021. {
  8022. if(!_triggerTransaction[gunIndex])
  8023. {
  8024. return;
  8025. }
  8026. Update_PowerConsumption(gunIndex);
  8027. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  8028. Set_Ocpp_StopTransactionId(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  8029. LOG_INFO("Gun %d Stop Transaction IdTag = %s, StopSoc = %d, MeterValue: %.4f",
  8030. chargingInfo[gunIndex]->Index + 1,
  8031. chargingInfo[gunIndex]->StartUserId,
  8032. chargingInfo[gunIndex]->EvBatterySoc,
  8033. chargingInfo[gunIndex]->PowerConsumption);
  8034. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StopCharging);
  8035. Set_Ocpp_StopTransactionReq(gunIndex);
  8036. // clean _triggerTransaction
  8037. _triggerTransaction[gunIndex] = false;
  8038. }
  8039. bool Is_DispenserStop(byte gunIndex)
  8040. {
  8041. bool _isStop = false;
  8042. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  8043. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  8044. {
  8045. _isStop = true;
  8046. }
  8047. return _isStop;
  8048. }
  8049. void ChkOcppStatus(byte gunIndex)
  8050. {
  8051. Chk_Ocpp_Backup_ErrorCode(gunIndex);
  8052. if(Is_Ocpp_ReserveNowReq(gunIndex))
  8053. {
  8054. Clean_Ocpp_ReserveNowReq(gunIndex);
  8055. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE && !Is_Ocpp_ReserveNowExpired(gunIndex))
  8056. {
  8057. LOG_INFO("***************ChkOcppStatus: Gun %d OcppReservedStatus********************", gunIndex + 1);
  8058. chargingInfo[gunIndex]->ReservationId = Get_Ocpp_ReserveNowId(gunIndex);
  8059. setChargerMode(gunIndex, MODE_RESERVATION);
  8060. }
  8061. Set_Ocpp_ReserveNowConf(gunIndex);
  8062. }
  8063. if(Is_Ocpp_CancelReservationReq(gunIndex))
  8064. {
  8065. Clean_Ocpp_CancelReservationReq(gunIndex);
  8066. if(chargingInfo[gunIndex]->SystemStatus == S_RESERVATION)
  8067. {
  8068. LOG_INFO("***************ChkOcppStatus: Gun %d Cancel OcppReservedStatus********************", gunIndex + 1);
  8069. chargingInfo[gunIndex]->ReservationId = -1;
  8070. setChargerMode(gunIndex, MODE_IDLE);
  8071. }
  8072. Set_Ocpp_CancelReservationConf(gunIndex);
  8073. }
  8074. if (Is_Ocpp_ChangeAvailabilityReq(gunIndex) == YES)
  8075. {
  8076. LOG_INFO("***************ChkOcppStatus: Gun %d OcppChangeAvailability********************", gunIndex + 1);
  8077. Clean_Ocpp_ChangeAvailabilityReq(gunIndex);
  8078. if(Chk_Ocpp_Availability_Type(gunIndex, "Operative"))
  8079. {
  8080. if (isDb_ready)
  8081. DB_Update_Operactive(localDb, gunIndex, true);
  8082. chargingInfo[gunIndex]->IsAvailable = YES;
  8083. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  8084. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  8085. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  8086. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  8087. {
  8088. setChargerMode(gunIndex, MODE_IDLE);
  8089. }
  8090. }
  8091. else if(Chk_Ocpp_Availability_Type(gunIndex, "Inoperative"))
  8092. {
  8093. if (isDb_ready)
  8094. DB_Update_Operactive(localDb, gunIndex, false);
  8095. chargingInfo[gunIndex]->IsAvailable = NO;
  8096. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  8097. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  8098. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  8099. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  8100. {
  8101. setChargerMode(gunIndex, MODE_MAINTAIN);
  8102. }
  8103. }
  8104. }
  8105. if (Is_Ocpp_UnlockConnectorReq(gunIndex))
  8106. {
  8107. Clean_Ocpp_UnlockConnectorReq(gunIndex);
  8108. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  8109. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  8110. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  8111. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  8112. {
  8113. // 充電中,需停止充電
  8114. Set_Ocpp_StopReason(gunIndex, "UnlockCommand");
  8115. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_UNLOCK);
  8116. chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop = true;
  8117. LOG_INFO("********** OcppUnlockStop Gun %d **********", gunIndex + 1);
  8118. }
  8119. Set_Ocpp_UnlockConnectorConf(gunIndex);
  8120. }
  8121. if (Is_Ocpp_RemoteStopReq(gunIndex))
  8122. {
  8123. Clean_Ocpp_RemoteStopReq(gunIndex);
  8124. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  8125. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  8126. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  8127. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  8128. {
  8129. Set_Ocpp_StopReason(gunIndex, "Remote");
  8130. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_REMOTE_STOP);
  8131. chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop = true;
  8132. LOG_INFO("********** OcppRemoteStop Gun %d **********", gunIndex + 1);
  8133. }
  8134. }
  8135. if(Is_Ocpp_StartTransactionConf(gunIndex))
  8136. {
  8137. char acceptId[128] = {0};
  8138. char status[32] = {0};
  8139. Get_Ocpp_StartTransaction_Status(gunIndex, status);
  8140. Get_Ocpp_TransactionId(gunIndex, acceptId);
  8141. Set_Deduct_TransactionId(gunIndex, atoi(acceptId));
  8142. Set_UserTransactionId(gunIndex, atoi(acceptId));
  8143. LOG_INFO("Gun %d OCPP Start Transaction %s, TransactionId [%s]", gunIndex + 1, status, acceptId);
  8144. if(!Is_Ocpp_StartTransaction_Accepted(gunIndex))
  8145. {
  8146. if(Is_Ocpp_StopOnInvalidId_Enable())
  8147. {
  8148. if(Is_Invalid_StartTransactionId(gunIndex))
  8149. {
  8150. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop = true;
  8151. LOG_INFO("Gun %d OCPP StopOnInvalidId", gunIndex + 1);
  8152. }
  8153. }
  8154. LOG_INFO("Gun %d Transaction [%s] Not Accepted, %s",
  8155. gunIndex + 1, acceptId,
  8156. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop > 0 ? "And Stop Charging" : "But StopOnInvalidId is Disable");
  8157. }
  8158. Clean_Ocpp_StartTransactionConf(gunIndex);
  8159. }
  8160. }
  8161. bool CheckBackendChargingTimeout(byte gunIndex)
  8162. {
  8163. bool result = false;
  8164. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  8165. {
  8166. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  8167. {
  8168. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  8169. result = true;
  8170. }
  8171. }
  8172. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  8173. {
  8174. // 隨插即充電的要看 offline
  8175. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  8176. {
  8177. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  8178. result = true;
  8179. }
  8180. }
  8181. return result;
  8182. }
  8183. bool CheckBackendChargingEnergy(byte gunIndex)
  8184. {
  8185. bool result = false;
  8186. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  8187. {
  8188. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  8189. {
  8190. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  8191. result = true;
  8192. }
  8193. }
  8194. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  8195. {
  8196. // 隨插即充電的要看 offline
  8197. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  8198. {
  8199. if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  8200. result = true;
  8201. }
  8202. }
  8203. return result;
  8204. }
  8205. bool IsReachConfigMaxChargingSoc(byte gunIndex)
  8206. {
  8207. bool result = false;
  8208. if(ShmSysConfigAndInfo->SysConfig.MaxChargingSoc > 0)
  8209. {
  8210. if(chargingInfo[gunIndex]->EvBatterySoc >= ShmSysConfigAndInfo->SysConfig.MaxChargingSoc)
  8211. {
  8212. LOG_INFO("Gun %d MaxChargingSoc: [%d%%] Stop", gunIndex + 1, ShmSysConfigAndInfo->SysConfig.MaxChargingSoc);
  8213. result = true;
  8214. }
  8215. }
  8216. return result;
  8217. }
  8218. bool CheckSessionTargetStop(byte gunIndex)
  8219. {
  8220. bool stop = false;
  8221. struct StructSessionTarget target;
  8222. Get_Ocpp_SessionTarget(gunIndex, &target);
  8223. if(target.targetDuration > 0)
  8224. {
  8225. if(chargingInfo[gunIndex]->PresentChargedDuration >= (target.targetDuration * 60))
  8226. {
  8227. LOG_INFO("Gun %d SessionTarget(Duration) Stop, Present: %d (sec), Target: %d (min)",
  8228. gunIndex + 1,
  8229. chargingInfo[gunIndex]->PresentChargedDuration,
  8230. target.targetDuration);
  8231. stop = true;
  8232. }
  8233. }
  8234. if(target.targetSoc > 0)
  8235. {
  8236. if(chargingInfo[gunIndex]->EvBatterySoc >= target.targetSoc)
  8237. {
  8238. LOG_INFO("Gun %d SessionTarget(SOC) Stop, Present: %d (%%), Target: %d (%%)",
  8239. gunIndex + 1,
  8240. chargingInfo[gunIndex]->EvBatterySoc,
  8241. target.targetSoc);
  8242. stop = true;
  8243. }
  8244. }
  8245. if(target.targetEnergy > 0)
  8246. {
  8247. if(chargingInfo[gunIndex]->PresentChargedEnergy >= target.targetEnergy)
  8248. {
  8249. LOG_INFO("Gun %d SessionTarget(Energy) Stop, Present: %8.3f (kWh), Target: %4d (kWh)",
  8250. gunIndex + 1,
  8251. chargingInfo[gunIndex]->PresentChargedEnergy,
  8252. target.targetEnergy);
  8253. stop = true;
  8254. }
  8255. }
  8256. if(stop)
  8257. {
  8258. chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop = true;
  8259. }
  8260. return stop;
  8261. }
  8262. void InformOcppErrOccur(byte codeType)
  8263. {
  8264. char _error[25];
  8265. switch(codeType)
  8266. {
  8267. case 4: strcpy(_error, "InternalError"); break;
  8268. case 6: strcpy(_error, "NoError"); break;
  8269. case 7: strcpy(_error, "OtherError"); break;
  8270. case 13: strcpy(_error, "UnderVoltage"); break;
  8271. case 14: strcpy(_error, "OverVoltage"); break;
  8272. }
  8273. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8274. {
  8275. SetOcppErrorCode(gun_index, _error);
  8276. }
  8277. }
  8278. void SetOcppErrorCode(unsigned char _index, char *errCode)
  8279. {
  8280. Set_Ocpp_StatusNotificationErrorCode(_index, errCode);
  8281. strcpy((char *)_BackupStatusNotification[_index].ErrorCode, errCode);
  8282. }
  8283. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode)
  8284. {
  8285. Set_Ocpp_StatusNotificationVendorCode(_index, vendorCode);
  8286. strcpy((char *)_BackupStatusNotification[_index].VendorErrorCode, vendorCode);
  8287. }
  8288. //===============================================
  8289. // SQLite3 related routine
  8290. //===============================================
  8291. int NetworkDB_Open(sqlite3 *db)
  8292. {
  8293. int result = PASS;
  8294. char* errMsg = NULL;
  8295. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  8296. "idx integer primary key AUTOINCREMENT, "
  8297. "occurDatetime text NOT NULL, "
  8298. "isInternet text NOT NULL, "
  8299. "isOcppConnected text NOT NULL, "
  8300. "isEth0Internet text NOT NULL, "
  8301. "isMlan0Internet text NOT NULL, "
  8302. "isPpp0Internet text NOT NULL, "
  8303. "rssiMlan0 text NOT NULL, "
  8304. "rssiPpp0 text NOT NULL"
  8305. ");";
  8306. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  8307. if(sqlite3_open(NETWORK_DB_FILE, &db))
  8308. {
  8309. result = FAIL;
  8310. LOG_ERROR("Can't open database (Net) : %s", sqlite3_errmsg(db));
  8311. sqlite3_close(db);
  8312. }
  8313. else
  8314. {
  8315. LOG_INFO("Local network status record database open successfully.");
  8316. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8317. {
  8318. result = FAIL;
  8319. LOG_ERROR("Create local network status record table error message: %s", errMsg);
  8320. }
  8321. else
  8322. {
  8323. LOG_INFO("Opened local network status record table successfully");
  8324. }
  8325. sqlite3_close(db);
  8326. }
  8327. return result;
  8328. }
  8329. int DB_Open(sqlite3 *db)
  8330. {
  8331. int result = PASS;
  8332. char* errMsg = NULL;
  8333. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  8334. "idx integer primary key AUTOINCREMENT, "
  8335. "reservationId text, "
  8336. "transactionId text, "
  8337. "startMethod text, "
  8338. "userId text, "
  8339. "dateTimeStart text, "
  8340. "dateTimeStop text,"
  8341. "socStart text, "
  8342. "socStop text, "
  8343. "chargeEnergy text, "
  8344. "stopReason text, "
  8345. "connector INTEGER"
  8346. ");";
  8347. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  8348. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8349. "`IsAvailable` TEXT NOT NULL, "
  8350. "`connector` INTEGER NOT NULL, "
  8351. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  8352. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  8353. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8354. "`occurDatetime` TEXT NOT NULL, "
  8355. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  8356. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  8357. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8358. "`rebootDatetime` TEXT NOT NULL, "
  8359. "`model_name` TEXT, "
  8360. "`serial_number` TEXT, unique(rebootDatetime) on conflict replace);";
  8361. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  8362. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8363. "`connector` INTEGER NOT NULL, "
  8364. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8365. char *createGunConsumptionSql = "CREATE TABLE IF NOT EXISTS `gun_consumption` ( "
  8366. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8367. "`connector` INTEGER NOT NULL, "
  8368. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8369. char *createFinalCostSql = "CREATE TABLE IF NOT EXISTS `final_cost` ( "
  8370. "idx integer primary key AUTOINCREMENT, "
  8371. "gun text, "
  8372. "transactionId text, "
  8373. "occurDatetime text, "
  8374. "updateDatetime text, "
  8375. "description text, "
  8376. "updateStatus integer"
  8377. ");";
  8378. char *createOcmfRecordSql = "CREATE TABLE IF NOT EXISTS `ocmf_record` ( "
  8379. "idx integer primary key AUTOINCREMENT, "
  8380. "gun text, "
  8381. "transactionId text, "
  8382. "publicKey text, "
  8383. "ocmfInfo text, "
  8384. "updateStatus integer, "
  8385. "occurDatetime text, "
  8386. "updateDatetime text"
  8387. ");";
  8388. if(sqlite3_open(DB_FILE, &db))
  8389. {
  8390. result = FAIL;
  8391. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8392. sqlite3_close(db);
  8393. }
  8394. else
  8395. {
  8396. LOG_INFO( "Local charging record database open successfully.");
  8397. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8398. {
  8399. result = FAIL;
  8400. LOG_ERROR( "Create local charging record table error message: %s", errMsg);
  8401. }
  8402. else
  8403. {
  8404. LOG_INFO( "Opened local charging record table successfully");
  8405. }
  8406. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  8407. {
  8408. result = FAIL;
  8409. LOG_ERROR( "Create local config table error message: %s", errMsg);
  8410. }
  8411. else
  8412. {
  8413. LOG_INFO( "Opened local config table successfully");
  8414. }
  8415. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8416. {
  8417. result = FAIL;
  8418. LOG_ERROR( "Create local record table error message: %s", errMsg);
  8419. }
  8420. else
  8421. {
  8422. LOG_INFO( "Opened local record table successfully");
  8423. }
  8424. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  8425. {
  8426. result = FAIL;
  8427. LOG_ERROR( "Create reboot record table error message: %s", errMsg);
  8428. }
  8429. else
  8430. {
  8431. LOG_INFO( "Opened reboot record table successfully");
  8432. }
  8433. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8434. {
  8435. result = FAIL;
  8436. LOG_ERROR( "Create power consumption record table error message: %s", errMsg);
  8437. }
  8438. else
  8439. {
  8440. LOG_INFO( "Opened power consumption record table successfully");
  8441. }
  8442. if (sqlite3_exec(db, createGunConsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8443. {
  8444. result = FAIL;
  8445. LOG_ERROR( "Create gun consumption record table error message: %s", errMsg);
  8446. }
  8447. else
  8448. {
  8449. LOG_INFO( "Opened gun consumption record table successfully");
  8450. }
  8451. if (sqlite3_exec(db, createFinalCostSql, 0, 0, &errMsg) != SQLITE_OK)
  8452. {
  8453. result = FAIL;
  8454. LOG_ERROR( "Create final cost table error message: %s", errMsg);
  8455. }
  8456. else
  8457. {
  8458. LOG_INFO( "Opened final cost table successfully");
  8459. }
  8460. if(sqlite3_exec(db, createOcmfRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8461. {
  8462. result = FAIL;
  8463. LOG_ERROR("Create OCMF record table error message: %s", errMsg);
  8464. }
  8465. else
  8466. {
  8467. LOG_INFO("Opened OCMF record table successfully");
  8468. }
  8469. sqlite3_close(db);
  8470. }
  8471. return result;
  8472. }
  8473. int DB_Insert_Record(sqlite3 *db, int gun_index)
  8474. {
  8475. int result = PASS;
  8476. char* errMsg = NULL;
  8477. char insertSql[1024];
  8478. char transactionId[128];
  8479. char reason[32];
  8480. Get_Ocpp_TransactionId(gun_index, transactionId);
  8481. Get_Ocpp_StopReason(gun_index, reason);
  8482. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, connector) "
  8483. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', %d);",
  8484. chargingInfo[gun_index]->ReservationId,
  8485. transactionId,
  8486. chargingInfo[gun_index]->StartMethod,
  8487. chargingInfo[gun_index]->StartUserId,
  8488. chargingInfo[gun_index]->StartDateTime,
  8489. chargingInfo[gun_index]->StopDateTime,
  8490. chargingInfo[gun_index]->EvBatteryStartSoc,
  8491. chargingInfo[gun_index]->EvBatterySoc,
  8492. chargingInfo[gun_index]->PresentChargedEnergy,
  8493. reason,
  8494. gun_index + 1);
  8495. if(sqlite3_open(DB_FILE, &db))
  8496. {
  8497. result = FAIL;
  8498. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8499. sqlite3_close(db);
  8500. }
  8501. else
  8502. {
  8503. //LOG_INFO( "Local charging record database open successfully.");
  8504. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8505. {
  8506. result = FAIL;
  8507. LOG_ERROR("Insert local charging record error message: %s", errMsg);
  8508. }
  8509. else
  8510. {
  8511. //LOG_INFO("Insert local charging record successfully");
  8512. }
  8513. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  8514. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8515. {
  8516. result = FAIL;
  8517. LOG_ERROR("delete local charging error message: %s", errMsg);
  8518. }
  8519. else
  8520. {
  8521. //LOG_INFO("delete local charging record successfully");
  8522. }
  8523. sqlite3_close(db);
  8524. }
  8525. return result;
  8526. }
  8527. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  8528. {
  8529. uint8_t result = false;
  8530. char* errMsg = NULL;
  8531. char sqlStr[1024];
  8532. srand(time(NULL));
  8533. if(sqlite3_open(DB_FILE, &db))
  8534. {
  8535. result = FAIL;
  8536. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8537. sqlite3_close(db);
  8538. }
  8539. else
  8540. {
  8541. LOG_INFO( "Local charging record database open successfully (%d).", IsAvailable);
  8542. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  8543. LOG_INFO("sqlStr= %s", sqlStr);
  8544. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8545. {
  8546. result = FAIL;
  8547. LOG_INFO( "update config error message: %s", errMsg);
  8548. }
  8549. else
  8550. {
  8551. LOG_INFO("update connector-%d config item isOperactive to %d", gun_index, IsAvailable);
  8552. }
  8553. sqlite3_close(db);
  8554. }
  8555. return result;
  8556. }
  8557. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  8558. {
  8559. uint8_t result = true;
  8560. char* errMsg = NULL;
  8561. char sqlStr[1024];
  8562. char **rs;
  8563. int rows, cols;
  8564. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  8565. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  8566. if(sqlite3_open(DB_FILE, &db))
  8567. {
  8568. result = FAIL;
  8569. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8570. sqlite3_close(db);
  8571. }
  8572. else
  8573. {
  8574. LOG_INFO( "Local config query database open successfully.");
  8575. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8576. if(rows>0)
  8577. {
  8578. for(int idxRow=1;idxRow<=rows;idxRow++)
  8579. {
  8580. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  8581. {
  8582. result = false;
  8583. }
  8584. LOG_INFO("Query connector-%d isOperactive: %s", gun_index, rs[(idxRow*cols)+3]);
  8585. }
  8586. }
  8587. else
  8588. {
  8589. LOG_INFO("Query connector-%d fail, set default value to operactive.", gun_index);
  8590. }
  8591. sqlite3_free_table(rs);
  8592. sqlite3_close(db);
  8593. }
  8594. return result;
  8595. }
  8596. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8597. {
  8598. char* errMsg = NULL;
  8599. char sqlStr[1024];
  8600. srand(time(NULL));
  8601. if(sqlite3_open(DB_FILE, &db))
  8602. {
  8603. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8604. sqlite3_close(db);
  8605. }
  8606. else
  8607. {
  8608. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  8609. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8610. {
  8611. LOG_ERROR("update power_consumption error message: %s", errMsg);
  8612. }
  8613. else
  8614. {
  8615. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8616. }
  8617. sqlite3_close(db);
  8618. }
  8619. }
  8620. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8621. {
  8622. char* errMsg = NULL;
  8623. char sqlStr[1024];
  8624. srand(time(NULL));
  8625. if(sqlite3_open(DB_FILE, &db))
  8626. {
  8627. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8628. sqlite3_close(db);
  8629. }
  8630. else
  8631. {
  8632. sprintf(sqlStr, "insert or replace into gun_consumption (connector, val) values(%d, %f);", gun_index, value);
  8633. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8634. {
  8635. LOG_ERROR("update gun_consumption error message: %s", errMsg);
  8636. }
  8637. else
  8638. {
  8639. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8640. }
  8641. sqlite3_close(db);
  8642. }
  8643. }
  8644. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index)
  8645. {
  8646. float result = 0;
  8647. char* errMsg = NULL;
  8648. char sqlStr[1024];
  8649. char **rs;
  8650. int rows, cols;
  8651. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  8652. if (sqlite3_open(DB_FILE, &db))
  8653. {
  8654. result = FAIL;
  8655. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8656. sqlite3_close(db);
  8657. }
  8658. else
  8659. {
  8660. //LOG_INFO("Local config query database open successfully.");
  8661. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8662. if (rows > 0)
  8663. {
  8664. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8665. {
  8666. result = atof(rs[(idxRow * cols) + 2]);
  8667. }
  8668. }
  8669. else
  8670. {
  8671. LOG_INFO("Query connector-%d fail, set default value to consumption.", gun_index);
  8672. }
  8673. sqlite3_free_table(rs);
  8674. sqlite3_close(db);
  8675. }
  8676. return result;
  8677. }
  8678. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index)
  8679. {
  8680. float result = 0;
  8681. char* errMsg = NULL;
  8682. char sqlStr[1024];
  8683. char **rs;
  8684. int rows, cols;
  8685. sprintf(sqlStr, "select * from gun_consumption where connector=%d;", gun_index);
  8686. if (sqlite3_open(DB_FILE, &db))
  8687. {
  8688. result = FAIL;
  8689. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8690. sqlite3_close(db);
  8691. }
  8692. else
  8693. {
  8694. //LOG_INFO("Local config query database open successfully.");
  8695. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8696. if (rows > 0)
  8697. {
  8698. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8699. {
  8700. result = atof(rs[(idxRow * cols) + 2]);
  8701. }
  8702. }
  8703. else
  8704. {
  8705. LOG_INFO("Query gun %d fail, set -1 to gun consumption.", gun_index + 1);
  8706. result = -1;
  8707. }
  8708. sqlite3_free_table(rs);
  8709. sqlite3_close(db);
  8710. }
  8711. return result;
  8712. }
  8713. float Get_PowerConsumption(uint8_t gun_index)
  8714. {
  8715. float value = 0;
  8716. //value = ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0 ?
  8717. // ShmChargerInfo->MeterValue[gun_index].GunConsumption : ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8718. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  8719. {
  8720. if(!_useGunMeterValue[gun_index])
  8721. {
  8722. LOG_INFO("***** Gun %d Use GunMeterValue *****", gun_index + 1);
  8723. }
  8724. _useGunMeterValue[gun_index] = true;
  8725. value = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  8726. }
  8727. else
  8728. {
  8729. value = ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8730. }
  8731. return value;
  8732. }
  8733. void Update_PowerConsumption(uint8_t gun_index)
  8734. {
  8735. chargingInfo[gun_index]->PowerConsumption = Get_PowerConsumption(gun_index);
  8736. }
  8737. int DB_Insert_FinalCost(sqlite3 *db, uint8_t gun_index, int txId, char *description)
  8738. {
  8739. char timeBuf[32];
  8740. int result = PASS;
  8741. char* errMsg = NULL;
  8742. char insertSql[1024];
  8743. getNowDatetime((unsigned char *)timeBuf);
  8744. sprintf(insertSql, "insert into final_cost(gun, transactionId, occurDatetime, updateDatetime, description, updateStatus) "
  8745. "values('%d', '%d', '%s', '%s', '%s', '%d');",
  8746. gun_index + 1,
  8747. txId,
  8748. timeBuf,
  8749. "",
  8750. description,
  8751. BILL_SATAUS_UNDISPOSED);
  8752. if(sqlite3_open(DB_FILE, &db))
  8753. {
  8754. result = FAIL;
  8755. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8756. sqlite3_close(db);
  8757. }
  8758. else
  8759. {
  8760. //LOG_INFO("FinalCost database open successfully.");
  8761. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8762. {
  8763. result = FAIL;
  8764. LOG_ERROR("Insert final_cost error message: %s", errMsg);
  8765. }
  8766. else
  8767. {
  8768. //LOG_INFO("Insert final_cost successfully");
  8769. }
  8770. sprintf(insertSql, "delete from final_cost where idx < (select idx from final_cost order by idx desc limit 1)-2000;");
  8771. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8772. {
  8773. result = FAIL;
  8774. LOG_ERROR("delete final_cost error message: %s", errMsg);
  8775. }
  8776. else
  8777. {
  8778. //LOG_INFO("delete final_cost successfully");
  8779. }
  8780. sqlite3_close(db);
  8781. }
  8782. return result;
  8783. }
  8784. int DB_Update_FinalCost_Status(sqlite3 *db, int txId, int status)
  8785. {
  8786. char timeBuf[32];
  8787. int result = PASS;
  8788. char* errMsg = NULL;
  8789. char updateSql[1024];
  8790. getNowDatetime((unsigned char *)timeBuf);
  8791. sprintf(updateSql, "update final_cost set updateStatus = %d, updateDatetime = '%s' where transactionId = '%d';",
  8792. status,
  8793. timeBuf,
  8794. txId);
  8795. if(sqlite3_open(DB_FILE, &db))
  8796. {
  8797. result = FAIL;
  8798. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8799. sqlite3_close(db);
  8800. }
  8801. else
  8802. {
  8803. if(sqlite3_exec(db, updateSql, 0, 0, &errMsg) != SQLITE_OK)
  8804. {
  8805. LOG_ERROR("update final_cost status error message: %s", errMsg);
  8806. result = FAIL;
  8807. }
  8808. else
  8809. {
  8810. //LOG_INFO("update final_cost txId[%d] status %d ok", txId, status);
  8811. }
  8812. sqlite3_close(db);
  8813. }
  8814. return result;
  8815. }
  8816. int DB_Get_Undisposed_FinalCost(sqlite3 *db, int gunIndex, char *costString)
  8817. {
  8818. int txId = -1;
  8819. char **rs;
  8820. int rows, cols;
  8821. char* errMsg = NULL;
  8822. char sqlString[1024];
  8823. sprintf(sqlString, "select * from final_cost where updateStatus = -1 and gun = '%d' order by idx asc limit 1;",
  8824. gunIndex + 1);
  8825. if(sqlite3_open(DB_FILE, &db))
  8826. {
  8827. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8828. sqlite3_close(db);
  8829. }
  8830. else
  8831. {
  8832. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  8833. if(rows > 0)
  8834. {
  8835. for(int idxRow = 1; idxRow <= rows; idxRow++)
  8836. {
  8837. txId = atoi(rs[(idxRow * cols) + 2]);
  8838. strcpy(costString, rs[(idxRow * cols) + 5]);
  8839. }
  8840. }
  8841. sqlite3_free_table(rs);
  8842. sqlite3_close(db);
  8843. }
  8844. return txId;
  8845. }
  8846. void CheckInProgress(uint8_t gun_index)
  8847. {
  8848. if((ShmChargerInfo->Connector[gun_index].Status > S_IDLE && ShmChargerInfo->Connector[gun_index].Status <= S_FAULT) ||
  8849. (ShmChargerInfo->Connector[gun_index].Status >= S_CCS_PRECHARGE_ST0 && ShmChargerInfo->Connector[gun_index].Status <= S_CCS_PRECHARGE_ST1))
  8850. {
  8851. if(chargingInfo[gun_index]->InProgress == NO)
  8852. {
  8853. LOG_INFO("Gun %d In Progress", gun_index + 1);
  8854. }
  8855. chargingInfo[gun_index]->InProgress = YES;
  8856. }
  8857. else
  8858. {
  8859. if(chargingInfo[gun_index]->InProgress == YES)
  8860. {
  8861. LOG_INFO("Gun %d Real Idle", gun_index + 1);
  8862. }
  8863. chargingInfo[gun_index]->InProgress = NO;
  8864. }
  8865. }
  8866. void CheckDebugMode(uint8_t gun_index)
  8867. {
  8868. if(ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay)
  8869. {
  8870. if(chargingInfo[gun_index]->SystemStatus != S_DEBUG)
  8871. {
  8872. setChargerMode(gun_index, MODE_DEBUG);
  8873. }
  8874. }
  8875. }
  8876. int DB_Reboot_Record(sqlite3 *db)
  8877. {
  8878. int result = PASS;
  8879. char* errMsg = NULL;
  8880. char insertSql[256];
  8881. sprintf(insertSql, "insert into reboot_record(rebootDatetime, model_name, serial_number) values(CURRENT_TIMESTAMP, '%s', '%s');",
  8882. (char *)ShmSysConfigAndInfo->SysConfig.ModelName,
  8883. (char *)ShmSysConfigAndInfo->SysConfig.SerialNumber);
  8884. if(sqlite3_open(DB_FILE, &db))
  8885. {
  8886. result = FAIL;
  8887. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8888. sqlite3_close(db);
  8889. }
  8890. else
  8891. {
  8892. LOG_INFO("Local charging record database open successfully.");
  8893. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8894. {
  8895. result = FAIL;
  8896. LOG_ERROR("Insert reboot record error message: %s", errMsg);
  8897. }
  8898. else
  8899. {
  8900. //LOG_INFO("Insert reboot record successfully");
  8901. }
  8902. sprintf(insertSql, "delete from reboot_record where idx < (select idx from reboot_record order by idx desc limit 1)-2000;");
  8903. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8904. {
  8905. result = FAIL;
  8906. LOG_ERROR("delete reboot record error message: %s", errMsg);
  8907. }
  8908. else
  8909. {
  8910. //LOG_INFO("delete reboot record successfully");
  8911. }
  8912. sqlite3_close(db);
  8913. }
  8914. return result;
  8915. }
  8916. int DB_Insert_Ocmf(sqlite3 *db, int gun_index, char *txId, char *ocmfKey, char *ocmfInfo)
  8917. {
  8918. char timeBuf[32];
  8919. int result = PASS;
  8920. char* errMsg = NULL;
  8921. char insertSql[3072];
  8922. getNowDatetime((unsigned char *)timeBuf);
  8923. sprintf(insertSql, "insert into ocmf_record(gun, transactionId, publicKey, ocmfInfo, updateStatus, occurDatetime, updateDatetime) "
  8924. "values('%d', '%s', '%s', '%s', '%d', '%s', '%s');",
  8925. gun_index + 1,
  8926. txId,
  8927. ocmfKey,
  8928. ocmfInfo,
  8929. OCMF_SATAUS_INSERTED,
  8930. timeBuf,
  8931. "");
  8932. if(sqlite3_open(DB_FILE, &db))
  8933. {
  8934. result = FAIL;
  8935. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8936. sqlite3_close(db);
  8937. }
  8938. else
  8939. {
  8940. //LOG_INFO("OCMF database open successfully.");
  8941. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8942. {
  8943. result = FAIL;
  8944. LOG_ERROR("Insert ocmf_record error message: %s", errMsg);
  8945. }
  8946. else
  8947. {
  8948. //LOG_INFO("Insert ocmf_record successfully");
  8949. }
  8950. sprintf(insertSql, "delete from ocmf_record where idx < (select idx from ocmf_record order by idx desc limit 1)-2000;");
  8951. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8952. {
  8953. result = FAIL;
  8954. LOG_ERROR("delete ocmf_record error message: %s", errMsg);
  8955. }
  8956. else
  8957. {
  8958. //LOG_INFO("delete ocmf_record successfully");
  8959. }
  8960. sqlite3_close(db);
  8961. }
  8962. return result;
  8963. }
  8964. int DB_Update_Ocmf_Status(sqlite3 *db, char *txId, int status)
  8965. {
  8966. char timeBuf[32];
  8967. int result = PASS;
  8968. char* errMsg = NULL;
  8969. char updateSql[1024];
  8970. getNowDatetime((unsigned char *)timeBuf);
  8971. sprintf(updateSql, "update ocmf_record set updateStatus = %d, updateDatetime = '%s' where transactionId = '%s';",
  8972. status,
  8973. timeBuf,
  8974. txId);
  8975. if(sqlite3_open(DB_FILE, &db))
  8976. {
  8977. result = FAIL;
  8978. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8979. sqlite3_close(db);
  8980. }
  8981. else
  8982. {
  8983. if(sqlite3_exec(db, updateSql, 0, 0, &errMsg) != SQLITE_OK)
  8984. {
  8985. LOG_ERROR("update updateStatus status error message: %s", errMsg);
  8986. result = FAIL;
  8987. }
  8988. else
  8989. {
  8990. //LOG_INFO("update updateStatus txId[%s] status %d ok", txId, status);
  8991. }
  8992. sqlite3_close(db);
  8993. }
  8994. return result;
  8995. }
  8996. // return YES if exist undisposed ocmf
  8997. int DB_Get_Undisposed_Ocmf(sqlite3 *db, int *gunIndex, char *txId, char *ocmfKey, char *ocmfInfo)
  8998. {
  8999. char **rs;
  9000. int rows, cols;
  9001. char* errMsg = NULL;
  9002. char sqlString[1024];
  9003. int result = NO;
  9004. sprintf(sqlString, "select * from ocmf_record where updateStatus = 0 order by idx asc limit 1;");
  9005. if(sqlite3_open(DB_FILE, &db))
  9006. {
  9007. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  9008. sqlite3_close(db);
  9009. }
  9010. else
  9011. {
  9012. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  9013. if(rows > 0)
  9014. {
  9015. for(int idxRow = 1; idxRow <= rows; idxRow++)
  9016. {
  9017. *gunIndex = atoi(rs[(idxRow * cols + 1)]) - 1;
  9018. strncpy(txId, rs[(idxRow * cols) + 2], 36);
  9019. strncpy(ocmfKey, rs[(idxRow * cols) + 3], 200);
  9020. strcpy(ocmfInfo, rs[(idxRow * cols) + 4]);
  9021. }
  9022. result = YES;
  9023. }
  9024. sqlite3_free_table(rs);
  9025. sqlite3_close(db);
  9026. }
  9027. return result;
  9028. }
  9029. void DB_TryAddRebootRecordColumn(sqlite3 *db)
  9030. {
  9031. char **rs;
  9032. int rows, cols;
  9033. char* errMsg = NULL;
  9034. char sqlString[1024];
  9035. int result = 0;
  9036. strcpy(sqlString, "select count(*) from pragma_table_info('reboot_record') where name = 'model_name';");
  9037. if(sqlite3_open(DB_FILE, &db))
  9038. {
  9039. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  9040. sqlite3_close(db);
  9041. }
  9042. else
  9043. {
  9044. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  9045. if(rows > 0)
  9046. {
  9047. for(int idxRow = 1; idxRow <= rows; idxRow++)
  9048. {
  9049. result = atoi(rs[(idxRow * cols) + 0]);
  9050. }
  9051. }
  9052. sqlite3_free_table(rs);
  9053. if(result == 0)
  9054. {
  9055. strcpy(sqlString, "ALTER TABLE 'reboot_record' ADD COLUMN 'model_name' TEXT;");
  9056. if(sqlite3_exec(db, sqlString, 0, 0, &errMsg) != SQLITE_OK)
  9057. {
  9058. result = FAIL;
  9059. LOG_ERROR("reboot_record add column 'model_name' error message: %s", errMsg);
  9060. }
  9061. else
  9062. {
  9063. LOG_INFO("reboot_record ADD COLUMN 'model_name' successfully");
  9064. }
  9065. strcpy(sqlString, "ALTER TABLE 'reboot_record' ADD COLUMN 'serial_number' TEXT;");
  9066. if(sqlite3_exec(db, sqlString, 0, 0, &errMsg) != SQLITE_OK)
  9067. {
  9068. result = FAIL;
  9069. LOG_ERROR("reboot_record add column 'serial_number' error message: %s", errMsg);
  9070. }
  9071. else
  9072. {
  9073. LOG_INFO("reboot_record ADD COLUMN 'serial_number' successfully");
  9074. }
  9075. }
  9076. sqlite3_close(db);
  9077. }
  9078. }
  9079. void DB_TryAddChargingRecordColumn(sqlite3 *db)
  9080. {
  9081. char **rs;
  9082. int rows, cols;
  9083. char* errMsg = NULL;
  9084. char sqlString[1024];
  9085. int result = 0;
  9086. strcpy(sqlString, "select count(*) from pragma_table_info('charging_record') where name = 'connector';");
  9087. if(sqlite3_open(DB_FILE, &db))
  9088. {
  9089. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  9090. sqlite3_close(db);
  9091. }
  9092. else
  9093. {
  9094. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  9095. if(rows > 0)
  9096. {
  9097. for(int idxRow = 1; idxRow <= rows; idxRow++)
  9098. {
  9099. result = atoi(rs[(idxRow * cols) + 0]);
  9100. }
  9101. }
  9102. sqlite3_free_table(rs);
  9103. if(result == 0)
  9104. {
  9105. strcpy(sqlString, "ALTER TABLE 'charging_record' ADD COLUMN 'connector' INTEGER;");
  9106. if(sqlite3_exec(db, sqlString, 0, 0, &errMsg) != SQLITE_OK)
  9107. {
  9108. result = FAIL;
  9109. LOG_ERROR("charging_record add column connector error message: %s", errMsg);
  9110. }
  9111. else
  9112. {
  9113. LOG_INFO("charging_record ADD COLUMN connector successfully");
  9114. }
  9115. }
  9116. sqlite3_close(db);
  9117. }
  9118. }
  9119. //===============================================
  9120. // Config process
  9121. //===============================================
  9122. void AddPlugInTimes(byte gunIndex)
  9123. {
  9124. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  9125. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  9126. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  9127. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  9128. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  9129. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  9130. }
  9131. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  9132. {
  9133. char cmdBuf[32];
  9134. struct timeb csuTime;
  9135. struct tm *tmCSU;
  9136. ftime(&csuTime);
  9137. tmCSU = localtime(&csuTime.time);
  9138. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  9139. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  9140. tmCSU->tm_sec);
  9141. if (isStart)
  9142. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  9143. else
  9144. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  9145. }
  9146. void zipLogFiles()
  9147. {
  9148. const char* logPath = "/Storage/SystemLog";
  9149. // 獲取目錄
  9150. DIR* pDir = opendir(logPath);
  9151. if (pDir != NULL)
  9152. {
  9153. struct timeb csuTime;
  9154. struct tm *tmCSU;
  9155. ftime(&csuTime);
  9156. tmCSU = localtime(&csuTime.time);
  9157. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  9158. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  9159. // tmCSU->tm_sec);
  9160. // Read items inside the folder
  9161. struct dirent* pEntry = NULL;
  9162. while ((pEntry = readdir(pDir)) != NULL)
  9163. {
  9164. if (strcmp(pEntry->d_name, ".") != 0 &&
  9165. strcmp(pEntry->d_name, "..") != 0 &&
  9166. strncmp(pEntry->d_name, "[", 1) == 0 &&
  9167. strstr(pEntry->d_name, "tar") < 0)
  9168. {
  9169. char yearC[5];
  9170. unsigned short year = 0;
  9171. char monthC[3];
  9172. unsigned short month = 0;
  9173. yearC[4] = '\0';
  9174. strncpy(yearC, pEntry->d_name + 1, 4);
  9175. monthC[2] = '\0';
  9176. strncpy(monthC, pEntry->d_name + 6, 2);
  9177. year = atoi(yearC);
  9178. month = atoi(monthC);
  9179. if (year != 0)
  9180. {
  9181. if (year < tmCSU->tm_year + 1900 ||
  9182. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  9183. {
  9184. LOG_INFO("tar file name : %s", pEntry->d_name);
  9185. char file[256];
  9186. memset(file, 0x00, sizeof(file));
  9187. strcat(file, "tar zcvf ");
  9188. strcat(file, logPath);
  9189. strncat(file, "/", 1);
  9190. strcat(file, pEntry->d_name);
  9191. strcat(file, ".tar");
  9192. strncat(file, " ", 1);
  9193. strcat(file, logPath);
  9194. strncat(file, "/", 1);
  9195. strcat(file, pEntry->d_name);
  9196. LOG_INFO("zip = %s \n", file);
  9197. system(file);
  9198. }
  9199. }
  9200. }
  9201. }
  9202. }
  9203. // Close folder
  9204. closedir(pDir);
  9205. }
  9206. void StopProcessingLoop()
  9207. {
  9208. for (;;)
  9209. {
  9210. CheckFactoryConfigFunction();
  9211. CheckFwUpdateFunction();
  9212. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  9213. {
  9214. ChkPrimaryStatus();
  9215. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0)
  9216. {
  9217. LOG_INFO("Soft reboot for retry self-tets (Primary).");
  9218. KillAllTask();
  9219. sleep(3);
  9220. TryCloseWatchdog();
  9221. system("/usr/bin/run_evse_restart.sh");
  9222. return;
  9223. }
  9224. }
  9225. if(Is_Ocpp_ResetReq())
  9226. {
  9227. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  9228. {
  9229. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = true;
  9230. }
  9231. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  9232. {
  9233. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  9234. }
  9235. }
  9236. CheckSoftHardReset();
  9237. sleep(1);
  9238. }
  9239. }
  9240. void CreateWatchdog(void)
  9241. {
  9242. wtdFd = InitWatchDog();
  9243. if(wtdFd < 0)
  9244. {
  9245. LOG_INFO("Watchdog Initial Fail");
  9246. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  9247. }
  9248. else
  9249. {
  9250. LOG_INFO("Watchdog Initial Success");
  9251. }
  9252. }
  9253. void TryCloseWatchdog(void)
  9254. {
  9255. if(wtdFd > 0)
  9256. {
  9257. write(wtdFd, "V", 1);
  9258. close(wtdFd);
  9259. }
  9260. }
  9261. void TryFeedWatchdog(void)
  9262. {
  9263. if(wtdFd > 0)
  9264. {
  9265. write(wtdFd, "a", 1);
  9266. }
  9267. }
  9268. bool IsParallelCabinetIdle(void)
  9269. {
  9270. bool result = true;
  9271. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  9272. {
  9273. result = false;
  9274. }
  9275. return result;
  9276. }
  9277. bool IsStandbyEnable(void)
  9278. {
  9279. bool result = true;
  9280. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyTimeDisableForTcc)
  9281. {
  9282. time_t timep;
  9283. struct tm *tm;
  9284. time(&timep);
  9285. timep += (ShmChargerInfo->CabinetMiscValue.TimeOffset * 60);
  9286. tm = localtime(&timep);
  9287. if(tm->tm_hour >= 0 && tm->tm_hour < 8)
  9288. {
  9289. if(!_isStandbyEnable)
  9290. {
  9291. LOG_INFO("[TCC]Customer Enable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  9292. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  9293. }
  9294. _isStandbyEnable = true;
  9295. result = true;
  9296. }
  9297. else
  9298. {
  9299. if(_isStandbyEnable)
  9300. {
  9301. LOG_INFO("[TCC]Customer Disable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  9302. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  9303. }
  9304. _isStandbyEnable = false;
  9305. result = false;
  9306. }
  9307. }
  9308. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyTimeDisable)
  9309. {
  9310. result = false;
  9311. }
  9312. return result;
  9313. }
  9314. bool IsGunIdle(int gunIndex, int idleType)
  9315. {
  9316. bool idle = true;
  9317. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  9318. chargingInfo[gunIndex]->ConnectorPlugIn &&
  9319. idleType == GUN_IDLE_TYPE_WHOLE)
  9320. {
  9321. idle = false;
  9322. }
  9323. else if (chargingInfo[gunIndex]->SystemStatus != S_IDLE &&
  9324. chargingInfo[gunIndex]->SystemStatus != S_RESERVATION &&
  9325. chargingInfo[gunIndex]->SystemStatus != S_FAULT &&
  9326. chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  9327. {
  9328. idle = false;
  9329. }
  9330. return idle;
  9331. }
  9332. bool IsDispenserIdle(int dispenserIndex)
  9333. {
  9334. bool dispenserIdle = true;
  9335. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  9336. {
  9337. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  9338. if(!IsGunIdle(gun, GUN_IDLE_TYPE_NORMAL))
  9339. {
  9340. dispenserIdle = false;
  9341. break;
  9342. }
  9343. }
  9344. return dispenserIdle;
  9345. }
  9346. bool IsConnectorWholeIdle(void)
  9347. {
  9348. bool result = true;
  9349. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  9350. {
  9351. if(!IsGunIdle(count, GUN_IDLE_TYPE_WHOLE))
  9352. {
  9353. result = false;
  9354. break;
  9355. }
  9356. }
  9357. return result;
  9358. }
  9359. bool IsUpgradeAvailable(void)
  9360. {
  9361. bool result = true;
  9362. for(int count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  9363. {
  9364. if(!IsGunIdle(count, GUN_IDLE_TYPE_NORMAL))
  9365. {
  9366. result = false;
  9367. break;
  9368. }
  9369. }
  9370. return result;
  9371. }
  9372. void ClearAlarmCodeWhenAcOff()
  9373. {
  9374. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  9375. {
  9376. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  9377. }
  9378. }
  9379. //==========================================
  9380. // Check task processing
  9381. //==========================================
  9382. void CheckTask()
  9383. {
  9384. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  9385. {
  9386. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  9387. {
  9388. if(system("pidof -s Module_4g > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  9389. {
  9390. LOG_ERROR("Module_4g not running, restart it.");
  9391. system("/root/Module_4g &");
  9392. }
  9393. }
  9394. }
  9395. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  9396. {
  9397. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  9398. {
  9399. if(system("pidof -s Module_Wifi > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  9400. {
  9401. LOG_ERROR("Module_Wifi not running, restart it.");
  9402. system("/root/Module_Wifi &");
  9403. }
  9404. }
  9405. }
  9406. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  9407. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  9408. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  9409. {
  9410. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = true;
  9411. if((time((time_t*)NULL) - Get_Ocpp_procDogTime()) > 180)
  9412. {
  9413. LOG_ERROR("OcppBackend watch dog timeout task restart.");
  9414. Clean_Ocpp_procDogTime();
  9415. Kill_Ocpp_Service();
  9416. }
  9417. if(!Is_Ocpp_Task_Alive())
  9418. {
  9419. LOG_ERROR("OcppBackend not running, restart it.");
  9420. Run_Ocpp_Service();
  9421. }
  9422. }
  9423. else
  9424. {
  9425. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = false;
  9426. }
  9427. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  9428. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  9429. {
  9430. if(!Is_MaintainOcpp_Task_Alive())
  9431. {
  9432. LOG_ERROR("Maintain Backend not running, restart it.");
  9433. Run_MaintainOcpp_Service();
  9434. }
  9435. }
  9436. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  9437. {
  9438. LOG_ERROR("Module_ProduceUtils not running, restart it.");
  9439. system ("/root/Module_ProduceUtils &");
  9440. }
  9441. if(system("pidof -s Module_EvComm > /dev/null") != 0)
  9442. {
  9443. LOG_ERROR("Module_EvComm not running, restart it.");
  9444. system ("/root/Module_EvComm &");
  9445. }
  9446. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  9447. {
  9448. LOG_ERROR("Module_InternalComm not running, restart it.");
  9449. system ("/root/Module_InternalComm &");
  9450. //ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  9451. }
  9452. if(!Is_WebServer_Alive())
  9453. {
  9454. LOG_ERROR("lighttpd not running, restart it.");
  9455. Run_WebServer_Service();
  9456. }
  9457. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9458. {
  9459. if(!Is_LocalPowerSharing_Alive())
  9460. {
  9461. LOG_ERROR("Module_PowerSharing not running, restart it.");
  9462. Run_LocalPowerSharing_Service();
  9463. }
  9464. }
  9465. }
  9466. void InitialDHCP()
  9467. {
  9468. char tmpbuf[256];
  9469. memset(tmpbuf,0,256);
  9470. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  9471. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  9472. system(tmpbuf);
  9473. }
  9474. void InitialDispenserDhcpServerConfig(void)
  9475. {
  9476. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  9477. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  9478. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  9479. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  9480. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  9481. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  9482. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  9483. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  9484. usleep(1000);
  9485. }
  9486. void StartDispenserDhcpServer(void)
  9487. {
  9488. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  9489. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  9490. }
  9491. //==========================================
  9492. // Check Smart Charging Profile
  9493. //==========================================
  9494. void Ocpp_ProfileReq_Cmd(byte gunIndex)
  9495. {
  9496. if(!Is_Ocpp_ChargingProfileConf(gunIndex))
  9497. {
  9498. if(!Is_Ocpp_ChargingProfileReq(gunIndex))
  9499. {
  9500. Set_Ocpp_ChargingProfileReq(gunIndex);
  9501. }
  9502. }
  9503. }
  9504. int GetStartScheduleTime(unsigned char *time)
  9505. {
  9506. int result = -1;
  9507. struct tm tmScheduleStart;
  9508. struct timeb tbScheduleStart;
  9509. 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))
  9510. {
  9511. tmScheduleStart.tm_year -= 1900;
  9512. tmScheduleStart.tm_mon -= 1;
  9513. tbScheduleStart.time = mktime(&tmScheduleStart);
  9514. tbScheduleStart.millitm = 0;
  9515. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  9516. }
  9517. return result;
  9518. }
  9519. void Ocpp_ChargingProfile_Process(byte _index)
  9520. {
  9521. int _startCount = NO_DEFINE;
  9522. int _maxCount = 0;
  9523. float _limit = 0;
  9524. int _numberPhases = 0;
  9525. if(Is_Ocpp_ChargingProfileKind(_index, "Absolute") && Is_Ocpp_ChargingProfileId(_index))
  9526. {
  9527. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(_index);
  9528. _startCount = Get_Ocpp_ChargingProfileStartIndex(_index);
  9529. if(_startCount < _maxCount)
  9530. {
  9531. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9532. _numberPhases = Get_Ocpp_ChargingProfileScheduleNumberPhases(_index, _startCount);
  9533. chargingInfo[_index]->ChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9534. if(chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  9535. (int)chargingInfo[_index]->PresentChargingVoltage > 0)
  9536. {
  9537. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  9538. }
  9539. else
  9540. {
  9541. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9542. }
  9543. }
  9544. else
  9545. {
  9546. chargingInfo[_index]->ChargingProfilePower = -1;
  9547. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9548. }
  9549. }
  9550. else
  9551. {
  9552. chargingInfo[_index]->ChargingProfilePower = -1;
  9553. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9554. }
  9555. if((int)_lastProfilePower[_index] != (int)chargingInfo[_index]->ChargingProfilePower ||
  9556. (int)_lastProfileCurrent[_index] != (int)chargingInfo[_index]->ChargingProfileCurrent)
  9557. {
  9558. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9559. LOG_INFO("Gun %d Get Profile - Index = %d, Limit = %f",
  9560. _index + 1, _startCount, _limit);
  9561. LOG_INFO("Max: %d kW, Gun %d ChargingProfile Power = %d, Current = %d",
  9562. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower == -1 ? (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower : ((int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000),
  9563. _index + 1, (int)chargingInfo[_index]->ChargingProfilePower, (int)chargingInfo[_index]->ChargingProfileCurrent);
  9564. }
  9565. _lastProfilePower[_index] = chargingInfo[_index]->ChargingProfilePower;
  9566. _lastProfileCurrent[_index] = chargingInfo[_index]->ChargingProfileCurrent;
  9567. }
  9568. void Ocpp_MaxChargingProfile_Process(void)
  9569. {
  9570. int _startCount = NO_DEFINE;
  9571. int _maxCount = 0;
  9572. float _limit = 0;
  9573. int _numberPhases = 0;
  9574. if(Is_Ocpp_MaxChargingProfileKind("Absolute") && Is_Ocpp_MaxChargingProfileId())
  9575. {
  9576. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  9577. _startCount = Get_Ocpp_MaxProfileStartIndex();
  9578. if(_startCount < _maxCount)
  9579. {
  9580. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9581. _numberPhases = Get_Ocpp_MaxProfileScheduleNumberPhases(_startCount);
  9582. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9583. }
  9584. else
  9585. {
  9586. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9587. }
  9588. }
  9589. else
  9590. {
  9591. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9592. }
  9593. if((int)_lastMaxProfilePower != (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower)
  9594. {
  9595. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9596. LOG_INFO("Charger Get Max Profile - Index = %d, Limit = %f",
  9597. _startCount, _limit);
  9598. LOG_INFO("Charger Max Profile Power = %d", (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower);
  9599. }
  9600. _lastMaxProfilePower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower;
  9601. }
  9602. void CheckSmartChargeProfile(byte _index)
  9603. {
  9604. if(Is_Ocpp_ChargingProfileConf(_index))
  9605. {
  9606. Clean_Ocpp_ChargingProfileConf(_index);
  9607. Ocpp_MaxChargingProfile_Process();
  9608. Ocpp_ChargingProfile_Process(_index);
  9609. }
  9610. }
  9611. void GetLocalPowerLimit(byte gunIndex)
  9612. {
  9613. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9614. {
  9615. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  9616. {
  9617. float _localLimitPower = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gunIndex] * AC_OUTPUT_VOL;
  9618. if(chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 && (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  9619. {
  9620. chargingInfo[gunIndex]->LocalPowerLimitCurrent = _localLimitPower / chargingInfo[gunIndex]->PresentChargingVoltage;
  9621. }
  9622. else
  9623. {
  9624. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9625. }
  9626. }
  9627. else
  9628. {
  9629. chargingInfo[gunIndex]->LocalPowerLimitCurrent = 0;
  9630. }
  9631. }
  9632. else
  9633. {
  9634. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9635. }
  9636. }
  9637. void TheEndCharging(byte gun_index)
  9638. {
  9639. chargingInfo[gun_index]->isRemoteStart = NO;
  9640. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0 && ShmChargerInfo->UserTransaction[gun_index].TransactionId > 0)
  9641. {
  9642. char receiptString[512];
  9643. sprintf(receiptString, "%s%d",
  9644. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0], ShmChargerInfo->UserTransaction[gun_index].TransactionId);
  9645. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gun_index][0], receiptString);
  9646. Set_Connector_MiscCommand(gun_index, MISC_CONN_RECEIPT);
  9647. LOG_INFO("Gun %d ReceiptrURL: [%s]", gun_index + 1, receiptString);
  9648. }
  9649. ChangeStartOrStopDateTime(NO, gun_index);
  9650. DB_Insert_Record(localDb, gun_index);
  9651. }
  9652. void UpdateErrorCodeToOcpp(byte index)
  9653. {
  9654. //printf("ConnectorAlarmCode = %s \n", chargingInfo[index]->ConnectorAlarmCode);
  9655. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL)
  9656. {
  9657. SetOcppErrorCode(index, "InternalError");
  9658. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->ConnectorAlarmCode);
  9659. }
  9660. else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL)
  9661. {
  9662. SetOcppErrorCode(index, "OtherError");
  9663. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->EvConnAlarmCode);
  9664. }
  9665. }
  9666. void CheckMiscCommandRequirement(void)
  9667. {
  9668. BOOL NeedAnnouncement = FALSE;
  9669. unsigned char connector = 0;
  9670. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate != YES)
  9671. {
  9672. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9673. {
  9674. NeedAnnouncement = FALSE;
  9675. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  9676. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  9677. {
  9678. if(ShmChargerInfo->DispenserMiscReq[i].CtrlValue != 0)
  9679. {
  9680. NeedAnnouncement = TRUE;
  9681. }
  9682. for(int j = 0; j < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity; j++)
  9683. {
  9684. connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorID[j] - 1;
  9685. if(ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue != 0)
  9686. {
  9687. NeedAnnouncement = TRUE;
  9688. }
  9689. }
  9690. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement != NeedAnnouncement)
  9691. {
  9692. if(NeedAnnouncement)
  9693. {
  9694. //LOG_INFO("********** Dispenser %d Misc Command Need Announcement **********", i + 1);
  9695. }
  9696. else
  9697. {
  9698. //LOG_INFO("********** Dispenser %d Misc Command Announced **********", i + 1);
  9699. }
  9700. }
  9701. }
  9702. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  9703. }
  9704. }
  9705. }
  9706. void CheckDispenserVersionUpdateRequirement(void)
  9707. {
  9708. }
  9709. bool IsReadyParalleling(void)
  9710. {
  9711. bool ready = true;
  9712. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  9713. {
  9714. if(chargingInfo[i]->SystemStatus != S_IDLE && chargingInfo[i]->SystemStatus != S_MAINTAIN)
  9715. {
  9716. ready = false;
  9717. break;
  9718. }
  9719. }
  9720. return ready;
  9721. }
  9722. void SetParallelingStart(void)
  9723. {
  9724. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9725. {
  9726. if(chargingInfo[i]->SystemStatus == S_IDLE)
  9727. {
  9728. setChargerMode(i, MODE_MAINTAIN);
  9729. }
  9730. }
  9731. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Wait;
  9732. }
  9733. void SetParallelingDone(void)
  9734. {
  9735. unsigned short totalPow = 0;
  9736. totalPow = GetTotalParallelCabinetPower();
  9737. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9738. {
  9739. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9740. }
  9741. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9742. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_None;
  9743. }
  9744. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff)
  9745. {
  9746. unsigned char relay = 0;
  9747. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9748. {
  9749. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9750. {
  9751. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9752. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9753. {
  9754. relay = OnOff == YES ? YES : NO;
  9755. if(ShmChargerInfo->ParallelCabinet.PCabinet[index].OutputRelaySetting[index] != relay)
  9756. {
  9757. LOG_INFO("Set Parallel Cabinet Gun %d Output Relay %s", index + 1, relay == YES ? "ON" : "OFF");
  9758. }
  9759. ShmChargerInfo->ParallelCabinet.PCabinet[i].OutputRelaySetting[index] = relay;
  9760. }
  9761. }
  9762. }
  9763. }
  9764. void SetParallelCabinetAcContactor(unsigned char OnOff)
  9765. {
  9766. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9767. {
  9768. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9769. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9770. {
  9771. ShmChargerInfo->ParallelCabinet.PCabinet[i].AcContactorSetting = OnOff == YES ? YES : NO;
  9772. }
  9773. }
  9774. }
  9775. unsigned short GetTotalParallelCabinetPower(void)
  9776. {
  9777. unsigned short power = 0, slavePow = 0;
  9778. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9779. {
  9780. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  9781. {
  9782. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  9783. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  9784. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  9785. {
  9786. slavePow = ParsingRatingPower((char *)ShmChargerInfo->ParallelCabinet.PCabinet[i].ModelName);
  9787. power += slavePow;
  9788. }
  9789. }
  9790. }
  9791. return power;
  9792. }
  9793. unsigned char preParallelStatus = 0;
  9794. void MCabinetParallelingProcess(void)
  9795. {
  9796. bool PsuReInitial = false;
  9797. unsigned char status = 0;
  9798. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9799. {
  9800. if(IsReadyParalleling())
  9801. {
  9802. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9803. {
  9804. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest)
  9805. {
  9806. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest = false;
  9807. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm = true;
  9808. PsuReInitial = true;
  9809. }
  9810. }
  9811. if(PsuReInitial)
  9812. {
  9813. SetParallelingStart();
  9814. }
  9815. }
  9816. status = ShmChargerInfo->ParallelCabinet.ParallelStatus;
  9817. switch(status)
  9818. {
  9819. case _Parallel_None:
  9820. if(preParallelStatus != status)
  9821. {
  9822. LOG_INFO("MCabinet Parallel Status [None]");
  9823. GetClockTime(&_Parallel_Time);
  9824. }
  9825. unsigned short totalPow = 0;
  9826. totalPow = GetTotalParallelCabinetPower();
  9827. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9828. {
  9829. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9830. }
  9831. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9832. break;
  9833. case _Parallel_Wait:
  9834. if(preParallelStatus != status)
  9835. {
  9836. LOG_INFO("MCabinet Parallel Status [Wait]");
  9837. SetAcContactor(OFF);
  9838. SetParallelCabinetAcContactor(OFF);
  9839. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9840. {
  9841. SetPCabinetOutputRelay(i, OFF);
  9842. }
  9843. GetClockTime(&_Parallel_Time);
  9844. }
  9845. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9846. {
  9847. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Working;
  9848. }
  9849. break;
  9850. case _Parallel_Working:
  9851. if(preParallelStatus != status)
  9852. {
  9853. LOG_INFO("MCabinet Parallel Status [Working]");
  9854. SetAcContactor(ON);
  9855. SetParallelCabinetAcContactor(ON);
  9856. GetClockTime(&_Parallel_Time);
  9857. }
  9858. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  9859. {
  9860. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Done;
  9861. }
  9862. break;
  9863. case _Parallel_Done:
  9864. if(preParallelStatus != status)
  9865. {
  9866. LOG_INFO("MCabinet Parallel Status [Done]");
  9867. GetClockTime(&_Parallel_Time);
  9868. }
  9869. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9870. {
  9871. SetParallelingDone();
  9872. }
  9873. break;
  9874. }
  9875. preParallelStatus = status;
  9876. }
  9877. }
  9878. void SCabinetControllProcess(void)
  9879. {
  9880. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  9881. {
  9882. if(ShmChargerInfo->SCabinetControl.SAcContactor)
  9883. {
  9884. SetAcContactor(ON);
  9885. }
  9886. else
  9887. {
  9888. SetAcContactor(OFF);
  9889. }
  9890. unsigned short _parallelRelay = 0;
  9891. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9892. {
  9893. _parallelRelay = ShmChargerInfo->SCabinetControl.SParallelRelay[i] > 0 ? (1 < i) : 0;
  9894. }
  9895. if(_parallelRelay != ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue)
  9896. {
  9897. LOG_INFO("SCabinet Set Parallel Relay %X -> %X", ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue, _parallelRelay);
  9898. }
  9899. ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue = _parallelRelay;
  9900. }
  9901. }
  9902. void CheckConnectorDisconnectionRecovery(unsigned char connector)
  9903. {
  9904. unsigned char dispenser = 0;
  9905. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  9906. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  9907. {
  9908. LOG_INFO("Connector %d Disconnection Recovery", connector + 1);
  9909. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.Disconnection = false;
  9910. }
  9911. }
  9912. bool ConnectorRequestToCharging(unsigned char connector)
  9913. {
  9914. unsigned char role = 0;
  9915. bool done = false;
  9916. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9917. switch(role)
  9918. {
  9919. case _GROLE_IDLE:
  9920. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest)
  9921. {
  9922. LOG_INFO("Request Gun %d To Start Charging", connector + 1);
  9923. }
  9924. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest = true;
  9925. break;
  9926. case _GROLE_MASTER:
  9927. done = true;
  9928. break;
  9929. case _GROLE_SLAVE:
  9930. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest)
  9931. {
  9932. LOG_INFO("Gun %d Wait For Re-Grouping", connector + 1);
  9933. }
  9934. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest = true;
  9935. break;
  9936. case _GROLE_REQUEST_TO_CHARGING:
  9937. done = true;
  9938. break;
  9939. default:
  9940. break;
  9941. }
  9942. return done;
  9943. }
  9944. bool IsConnectorGroupingCompleted(unsigned char connector)
  9945. {
  9946. unsigned char role = 0;
  9947. bool completed = false;
  9948. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9949. switch(role)
  9950. {
  9951. case _GROLE_MASTER:
  9952. completed = true;
  9953. break;
  9954. default:
  9955. break;
  9956. }
  9957. return completed;
  9958. }
  9959. void SelfTestFailReason(void)
  9960. {
  9961. char reason[50];
  9962. memset(reason, 0x00, sizeof(reason));
  9963. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  9964. {
  9965. StatusCodeCompose(reason, "042251");
  9966. }
  9967. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail ||
  9968. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail ||
  9969. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9970. {
  9971. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail)
  9972. {
  9973. StatusCodeCompose(reason, "042280");
  9974. }
  9975. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9976. {
  9977. StatusCodeCompose(reason, "042281");
  9978. }
  9979. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail)
  9980. {
  9981. StatusCodeCompose(reason, "042282");
  9982. }
  9983. }
  9984. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail ||
  9985. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  9986. {
  9987. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail)
  9988. {
  9989. StatusCodeCompose(reason, "042285");
  9990. }
  9991. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  9992. {
  9993. StatusCodeCompose(reason, "042286");
  9994. }
  9995. }
  9996. else
  9997. {
  9998. StatusCodeCompose(reason, "Self Test Fail");
  9999. }
  10000. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10001. {
  10002. SetOcppErrorCode(gun_index, "InternalError");
  10003. SetOcppVendorErrorCode(gun_index, reason);
  10004. setChargerMode(gun_index, MODE_MAINTAIN);
  10005. }
  10006. }
  10007. bool IsAvailableBackToIdle(int gun_index)
  10008. {
  10009. if(_plugInHold[gun_index] == false &&
  10010. (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false || chargingInfo[gun_index]->ConnectorPlugIn == false))
  10011. {
  10012. _plugInHold[gun_index] = true;
  10013. GetClockTime(&_PlugInHold_Time[gun_index]);
  10014. LOG_INFO ("Gun %d Plug In Hold Delay", gun_index + 1);
  10015. }
  10016. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle ||
  10017. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false ||
  10018. (!chargingInfo[gun_index]->ConnectorPlugIn && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm) ||
  10019. isResetStopChargeFlag(gun_index) == YES)
  10020. {
  10021. if(!_plugInHold[gun_index])
  10022. {
  10023. _plugInHold[gun_index] = true;
  10024. GetClockTime(&_PlugInHold_Time[gun_index]);
  10025. LOG_INFO ("Force Gun %d Plug In Hold Delay", gun_index + 1);
  10026. }
  10027. if((GetTimeoutValue(_PlugInHold_Time[gun_index]) / uSEC_VAL) >= PLUG_IN_HOLD_TIME)
  10028. {
  10029. return true;
  10030. }
  10031. }
  10032. return false;
  10033. }
  10034. void CheckGunAvailable(void)
  10035. {
  10036. if(!ShmChargerInfo->Control.SysCtrl.bits.PsuInit)
  10037. {
  10038. if(ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  10039. {
  10040. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  10041. {
  10042. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  10043. {
  10044. ShmChargerInfo->Control.GunAvailable[i] = YES;
  10045. }
  10046. }
  10047. ShmChargerInfo->Control.SysCtrl.bits.PsuInit = true;
  10048. }
  10049. }
  10050. }
  10051. int presentChargedEnergyClear(unsigned char gun_index)
  10052. {
  10053. int result = FAIL;
  10054. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  10055. memset(chargingInfo[gun_index]->presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod) * sizeof(float));
  10056. result = PASS;
  10057. return result;
  10058. }
  10059. float presentChargedEnergyTotal(unsigned char gun_index)
  10060. {
  10061. float result = 0.0f;
  10062. for(int idx = 0; idx < ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod); idx++)
  10063. {
  10064. result += chargingInfo[gun_index]->presentChargedEnergyPeriod[idx];
  10065. }
  10066. return result;
  10067. }
  10068. int presentChargedEnergyUpdate(unsigned char gun_index, float kwh)
  10069. {
  10070. int result = FAIL;
  10071. time_t CurrentTime;
  10072. struct tm *tm;
  10073. CurrentTime = time(NULL);
  10074. tm=localtime(&CurrentTime);
  10075. chargingInfo[gun_index]->presentChargedEnergyPeriod[tm->tm_hour] += kwh;
  10076. return result;
  10077. }
  10078. void ChargedEnergySummation(unsigned char gun_index)
  10079. {
  10080. unsigned long time = 0;
  10081. time = GetSecTimeoutValue(_ChargedEnergy_Time[gun_index]);
  10082. if(time >= (chargingInfo[gun_index]->PresentChargedDuration + ENERAGY_INTERVAL))
  10083. {
  10084. chargingInfo[gun_index]->PresentChargedDuration++;
  10085. float changedEnergy = 0;
  10086. float _meterEnergy = 0;
  10087. float _calChangingEnergy = (chargingInfo[gun_index]->PresentChargingPower) / 3600;
  10088. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  10089. {
  10090. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= _lsatGunMeterValue[gun_index])
  10091. {
  10092. _meterEnergy = ShmChargerInfo->MeterValue[gun_index].GunConsumption - _lsatGunMeterValue[gun_index];
  10093. }
  10094. else
  10095. {
  10096. _meterEnergy = 0;
  10097. }
  10098. changedEnergy = _meterEnergy;
  10099. _lsatGunMeterValue[gun_index] = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  10100. }
  10101. else
  10102. {
  10103. changedEnergy = _calChangingEnergy;
  10104. }
  10105. if(ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  10106. {
  10107. chargingInfo[gun_index]->ChargingFee += changedEnergy * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  10108. }
  10109. presentChargedEnergyUpdate(gun_index, changedEnergy);
  10110. chargingInfo[gun_index]->PresentChargedEnergy += changedEnergy;
  10111. ShmChargerInfo->MeterValue[gun_index].LocalConsumption += changedEnergy;
  10112. }
  10113. }
  10114. void ChkConnectorAction(void)
  10115. {
  10116. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  10117. {
  10118. if(ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel)
  10119. {
  10120. if(ShmChargerInfo->AllBill.OccupancyInfo[i].Status != _Parking_WaitToPay)
  10121. {
  10122. LOG_INFO("Gun %d Charging Canceled", i + 1);
  10123. Clean_Ocpp_TcciSerialNo(i);
  10124. }
  10125. else
  10126. {
  10127. LOG_INFO("Gun %d Occupancy Keep Counting", i + 1);
  10128. Set_OccupancyKeepCountingReq(i);
  10129. }
  10130. ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel = false;
  10131. }
  10132. }
  10133. }
  10134. void Update_DispenserAvailability(int dispenserIndex)
  10135. {
  10136. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  10137. {
  10138. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  10139. Set_Connector_MiscCommand(gun, MISC_CONN_AVAILABILITY);
  10140. }
  10141. }
  10142. void InitialDataBaseValue(void)
  10143. {
  10144. // Local DB
  10145. if(DB_Open(localDb) != PASS)
  10146. {
  10147. LOG_INFO("DB_Open fail.");
  10148. isDb_ready = false;
  10149. }
  10150. else
  10151. {
  10152. isDb_ready = true;
  10153. DB_TryAddRebootRecordColumn(localDb);
  10154. DB_TryAddChargingRecordColumn(localDb);
  10155. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10156. {
  10157. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  10158. LOG_INFO("Gun %d IsAvailable Initial: [%s]", _index + 1, chargingInfo[_index]->IsAvailable == YES ? "Enable" : "Disable");
  10159. }
  10160. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10161. {
  10162. ShmChargerInfo->MeterValue[_index].LocalConsumption = DB_Get_LocalConsumption(localDb, _index);
  10163. LOG_INFO("Local Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  10164. }
  10165. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10166. {
  10167. ShmChargerInfo->MeterValue[_index].GunConsumption = DB_Get_GunConsumption(localDb, _index);
  10168. LOG_INFO("Remote Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].GunConsumption);
  10169. }
  10170. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10171. {
  10172. Update_PowerConsumption(_index);
  10173. LOG_INFO("Gun %d Power Consumption Initial: [%.4f kWh]", _index + 1, chargingInfo[_index]->PowerConsumption);
  10174. }
  10175. DB_Reboot_Record(localDb);
  10176. }
  10177. if (NetworkDB_Open(networkDb) != PASS)
  10178. {
  10179. LOG_ERROR("NetworkDB_Open fail.");
  10180. }
  10181. }
  10182. float CalculateMaxOutputRatio(int gunIndex)
  10183. {
  10184. float ratio = 0;
  10185. int inUsingCnt = 0;
  10186. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  10187. {
  10188. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  10189. {
  10190. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  10191. }
  10192. }
  10193. if(inUsingCnt != 0)
  10194. {
  10195. ratio = ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].Role == _GROLE_MASTER ?
  10196. ((float)ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].GunPsuQuantity / inUsingCnt) : 0;
  10197. }
  10198. ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio = ratio;
  10199. return ratio;
  10200. }
  10201. // return unit: 0.1kW
  10202. unsigned short GetModelNameLimitPower(int gunIndex)
  10203. {
  10204. unsigned short limitPower = 0;
  10205. float fPower = 0;
  10206. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10207. ShmChargerInfo->Control.ChargerRatingPower != 0)
  10208. {
  10209. fPower = (float)ShmChargerInfo->Control.ChargerRatingPower / 10;
  10210. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  10211. {
  10212. fPower += (float)ShmChargerInfo->ParallelCabinet.TotalParallelPower / 10;
  10213. }
  10214. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10215. limitPower = (unsigned short)(fPower * 10);
  10216. }
  10217. return limitPower;
  10218. }
  10219. void GetWebPageConfigPowerLimit(int gunIndex)
  10220. {
  10221. unsigned short limitPower = 0;
  10222. float fPower = 0;
  10223. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10224. ShmSysConfigAndInfo->SysConfig.MaxChargingPower != 0)
  10225. {
  10226. fPower = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingPower;
  10227. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10228. limitPower = (unsigned short)(fPower * 10);
  10229. }
  10230. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower = limitPower;
  10231. }
  10232. void GetWebPageConfigCurrentLimit(int gunIndex)
  10233. {
  10234. unsigned short limitCurrent = 0;
  10235. float fCurrent = 0;
  10236. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10237. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent != 0)
  10238. {
  10239. fCurrent = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent;
  10240. fCurrent = fCurrent * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10241. limitCurrent = (unsigned short)(fCurrent * 10);
  10242. }
  10243. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent = limitCurrent;
  10244. }
  10245. void GetOffLineMaxChargingPowerLimit(int gunIndex)
  10246. {
  10247. unsigned short limitPower = 0;
  10248. float fPower = 0;
  10249. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10250. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower != 0 &&
  10251. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES)
  10252. {
  10253. fPower = ShmChargerInfo->OutputLimit.Offline_MaxChargingPower / 1000;
  10254. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10255. limitPower = (unsigned short)(fPower * 10);
  10256. }
  10257. ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] = limitPower * 100;
  10258. }
  10259. void GetMaxChargingProfileLimit(int gunIndex)
  10260. {
  10261. unsigned short limitPower = 0, limitCurrent = 0;
  10262. float fPower = 0;
  10263. // max charging profile
  10264. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10265. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower >= 0)
  10266. {
  10267. fPower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000;
  10268. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10269. limitPower = (unsigned short)(fPower * 10);
  10270. if (chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 &&
  10271. (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  10272. {
  10273. limitCurrent = (limitPower * 100 / chargingInfo[gunIndex]->PresentChargingVoltage) * 10;
  10274. }
  10275. }
  10276. ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] = limitPower * 100;
  10277. ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] = limitCurrent;
  10278. }
  10279. void GetMaxChargingLimit(int gunIndex)
  10280. {
  10281. GetWebPageConfigPowerLimit(gunIndex);
  10282. GetWebPageConfigCurrentLimit(gunIndex);
  10283. GetOffLineMaxChargingPowerLimit(gunIndex);
  10284. GetMaxChargingProfileLimit(gunIndex);
  10285. }
  10286. void GetGunOtpDerating(int gunIndex)
  10287. {
  10288. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp.isNeedDerating &&
  10289. chargingInfo[gunIndex]->SystemStatus > S_IDLE &&
  10290. chargingInfo[gunIndex]->SystemStatus <= S_CHARGING)
  10291. {
  10292. struct DERATING_BY_OTP *_derating;
  10293. _derating = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp;
  10294. if(_derating->deratingIndex < GUN_DERATING_COUNT)
  10295. {
  10296. int _connectorTemp = (int)chargingInfo[gunIndex]->ConnectorTemp - 60;
  10297. int _chillerTemp = (int)chargingInfo[gunIndex]->ChillerTemp - 60;
  10298. if(_connectorTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  10299. chargingInfo[gunIndex]->ConnectorTemp != 0xFF &&
  10300. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  10301. {
  10302. LOG_INFO("Gun %d GunTemp %d is Derating(%d) to %d A",
  10303. gunIndex + 1,
  10304. _connectorTemp,
  10305. GunDeratingTemp[_derating->deratingIndex],
  10306. ((int)_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  10307. _derating->deratingIndex++;
  10308. }
  10309. else if(_chillerTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  10310. chargingInfo[gunIndex]->ChillerTemp != 0xFF &&
  10311. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  10312. {
  10313. LOG_INFO("Gun %d ChillerTemp %d is Derating(%d) to %d A",
  10314. gunIndex + 1,
  10315. _chillerTemp,
  10316. GunDeratingTemp[_derating->deratingIndex],
  10317. ((int)_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  10318. _derating->deratingIndex++;
  10319. }
  10320. }
  10321. if(chargingInfo[gunIndex]->ConnectorTemp != 0xFF || chargingInfo[gunIndex]->ChillerTemp != 0xFF)
  10322. {
  10323. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = (int)_derating->deratingTargetCurrent[_derating->deratingIndex];
  10324. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  10325. }
  10326. else
  10327. {
  10328. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = 0;
  10329. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  10330. }
  10331. }
  10332. else
  10333. {
  10334. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = 0;
  10335. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  10336. }
  10337. }
  10338. void CalculateGunAbsMaxPower(int gunIndex)
  10339. {
  10340. int maxValue = 0;
  10341. maxValue = (int)GetModelNameLimitPower(gunIndex);
  10342. ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] = maxValue != 0 ? (maxValue * 100) : 0;
  10343. }
  10344. void CalculateGunAbsMaxVoltage(int gunIndex)
  10345. {
  10346. int maxValue = 0;
  10347. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalVoltage;
  10348. ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] = maxValue;
  10349. }
  10350. void CalculateGunAbsMaxCurrent(int gunIndex)
  10351. {
  10352. int maxValue = 0;
  10353. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalCurrent;
  10354. ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] = maxValue;
  10355. }
  10356. void RestrictChargingCapability(int gunIndex, unsigned short *power, unsigned short *voltage, unsigned short *current)
  10357. {
  10358. unsigned short limitPower = 0;
  10359. int _limitPowerResult = 0;
  10360. int _limitVoltageResult = 0;
  10361. int _limitCurrentResult = 0;
  10362. _limitPowerResult = ShmPsuData->SystemAvailablePower;
  10363. _limitVoltageResult = MAX_OUTPUT_VOLTAGE;
  10364. _limitCurrentResult = ShmPsuData->SystemAvailableCurrent;
  10365. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] != 0)
  10366. {
  10367. limitPower = ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] / 100;
  10368. if(limitPower != 0 && _limitPowerResult > limitPower)
  10369. {
  10370. _limitPowerResult = limitPower;
  10371. }
  10372. }
  10373. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] != 0 &&
  10374. _limitVoltageResult > ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex])
  10375. {
  10376. _limitVoltageResult = ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex];
  10377. }
  10378. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] != 0 &&
  10379. _limitCurrentResult > ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex])
  10380. {
  10381. _limitCurrentResult = ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex];
  10382. }
  10383. // restrict from WebPageConfig power
  10384. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower != 0)
  10385. {
  10386. if(_limitPowerResult > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower)
  10387. {
  10388. _limitPowerResult = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower;
  10389. }
  10390. }
  10391. // restrict from WebPageConfig current
  10392. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent != 0)
  10393. {
  10394. if(_limitCurrentResult > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent)
  10395. {
  10396. _limitCurrentResult = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent;
  10397. }
  10398. }
  10399. // restrict from Offline_MaxChargingPower
  10400. if(ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] != 0)
  10401. {
  10402. limitPower = ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] / 100;
  10403. if(limitPower != 0 && _limitPowerResult > limitPower)
  10404. {
  10405. _limitPowerResult = limitPower;
  10406. }
  10407. }
  10408. // restrict from MaxChargingProfile
  10409. if(ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] != 0)
  10410. {
  10411. limitPower = ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] / 100;
  10412. if(limitPower != 0 && _limitPowerResult > limitPower)
  10413. {
  10414. _limitPowerResult = limitPower;
  10415. }
  10416. }
  10417. // restrict from MaxChargingProfile
  10418. if(ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] != 0)
  10419. {
  10420. if(_limitCurrentResult > ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex])
  10421. {
  10422. _limitCurrentResult = ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex];
  10423. }
  10424. }
  10425. // restrict from ChargingProfile
  10426. if((chargingInfo[gunIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gunIndex]->SystemStatus <= S_CHARGING) ||
  10427. (chargingInfo[gunIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gunIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10428. {
  10429. if(chargingInfo[gunIndex]->ChargingProfilePower >= 0 && _limitPowerResult > ((int)chargingInfo[gunIndex]->ChargingProfilePower / 100))
  10430. {
  10431. _limitPowerResult = (int)chargingInfo[gunIndex]->ChargingProfilePower / 100;
  10432. }
  10433. if(chargingInfo[gunIndex]->ChargingProfileCurrent >= 0 && _limitCurrentResult > (int)chargingInfo[gunIndex]->ChargingProfileCurrent)
  10434. {
  10435. _limitCurrentResult = (int)chargingInfo[gunIndex]->ChargingProfileCurrent;
  10436. }
  10437. }
  10438. // restrict from local power limit
  10439. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  10440. {
  10441. if(chargingInfo[gunIndex]->LocalPowerLimitCurrent >= 0 && _limitCurrentResult > (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10))
  10442. {
  10443. _limitCurrentResult = (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10);
  10444. }
  10445. }
  10446. // restrict after otp
  10447. if(ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] != 0)
  10448. {
  10449. limitPower = ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] / 100;
  10450. if(limitPower != 0 && _limitPowerResult > limitPower)
  10451. {
  10452. _limitPowerResult = limitPower;
  10453. }
  10454. }
  10455. // restrict after otp
  10456. if(ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] != 0 &&
  10457. _limitCurrentResult > ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex])
  10458. {
  10459. _limitCurrentResult = ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex];
  10460. }
  10461. ShmChargerInfo->OutputLimit.GunLimitPower[gunIndex] = _limitPowerResult;
  10462. ShmChargerInfo->OutputLimit.GunLimitVoltage[gunIndex] = _limitVoltageResult;
  10463. ShmChargerInfo->OutputLimit.GunLimitCurrent[gunIndex] = _limitCurrentResult;
  10464. if(_limitPowerResult != 0 && *power > _limitPowerResult)
  10465. {
  10466. *power = _limitPowerResult;
  10467. }
  10468. if(_limitVoltageResult != 0 && *voltage > _limitVoltageResult)
  10469. {
  10470. *voltage = _limitVoltageResult;
  10471. }
  10472. if(_limitCurrentResult != 0 && *current > _limitCurrentResult)
  10473. {
  10474. *current = _limitCurrentResult;
  10475. }
  10476. }
  10477. void UpdateChargingCapability(void)
  10478. {
  10479. unsigned short voltage = 0, current = 0, power = 0;
  10480. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower = Get_Ocpp_OffLineMaxChargingPower();
  10481. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  10482. {
  10483. voltage = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.MaximumChargingVoltage;
  10484. current = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingCurrent;
  10485. power = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingPower;
  10486. CalculateGunAbsMaxPower(i);
  10487. CalculateGunAbsMaxVoltage(i);
  10488. CalculateGunAbsMaxCurrent(i);
  10489. CalculateMaxOutputRatio(i);
  10490. GetMaxChargingLimit(i);
  10491. GetLocalPowerLimit(i);
  10492. GetGunOtpDerating(i);
  10493. RestrictChargingCapability(i, &power, &voltage, &current);
  10494. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityVoltage = voltage;
  10495. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityCurrent = current;
  10496. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityPower = power;
  10497. }
  10498. }
  10499. void GunErrorCollection(void)
  10500. {
  10501. bool _PsuNoResource = false;
  10502. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  10503. {
  10504. if(ShmPsuData->Work_Step == INITIAL_START)
  10505. {
  10506. ShmChargerInfo->GunError[i].ErrFlag.bits.PsuGroupNoResource = false;
  10507. }
  10508. else
  10509. {
  10510. if(ShmChargerInfo->GunError[i].ErrFlag.bits.PsuGroupNoResource)
  10511. {
  10512. _PsuNoResource = true;
  10513. }
  10514. }
  10515. }
  10516. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = _PsuNoResource;
  10517. }
  10518. void UpdateGunPresentChargingPower(int gunIndex)
  10519. {
  10520. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Enable)
  10521. {
  10522. chargingInfo[gunIndex]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingCurrent))) / 1000 / 100);
  10523. }
  10524. else
  10525. {
  10526. chargingInfo[gunIndex]->PresentChargingPower = ((float)((chargingInfo[gunIndex]->PresentChargingVoltage) * (chargingInfo[gunIndex]->PresentChargingCurrent)) / 1000);
  10527. }
  10528. }
  10529. void CheckPsuLostQuantity(void)
  10530. {
  10531. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  10532. {
  10533. if(ShmChargerInfo->Control.PsuQuantityByModel == ShmPsuData->SystemPresentPsuQuantity)
  10534. {
  10535. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = false;
  10536. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = 0;
  10537. }
  10538. else
  10539. {
  10540. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = ShmChargerInfo->Control.PsuQuantityByModel > ShmPsuData->SystemPresentPsuQuantity ?
  10541. (ShmChargerInfo->Control.PsuQuantityByModel - ShmPsuData->SystemPresentPsuQuantity) : 0;
  10542. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch ||
  10543. _psuLostQuantity != ShmSysConfigAndInfo->SysInfo.PsuLostQuantity)
  10544. {
  10545. LOG_INFO("Psu Quantity is %d, it should be %d, Psu Lost: %d",
  10546. ShmPsuData->SystemPresentPsuQuantity, ShmChargerInfo->Control.PsuQuantityByModel,
  10547. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity);
  10548. }
  10549. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = true;
  10550. _psuLostQuantity = ShmSysConfigAndInfo->SysInfo.PsuLostQuantity;
  10551. }
  10552. }
  10553. }
  10554. void CheckOcmfHandler(void)
  10555. {
  10556. bool _newOcmf = false;
  10557. int _ocmfLength = 0;
  10558. int _gunIndex = 0;
  10559. char _txId[37];
  10560. char _ocmfKey[201];
  10561. char _ocmfInfo[2048];
  10562. // check new ocmf info
  10563. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  10564. {
  10565. if(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfFlag.bits.OcmfReceived)
  10566. {
  10567. _newOcmf = false;
  10568. char fileName[256] = {0};
  10569. sprintf(fileName, "/mnt/Gun_%d_%s_OCMF", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId);
  10570. if(access(fileName, F_OK) != -1)
  10571. {
  10572. _newOcmf = true;
  10573. }
  10574. if(_newOcmf)
  10575. {
  10576. int fd = 0;
  10577. fd = open(fileName, O_RDONLY);
  10578. if(fd < 0)
  10579. {
  10580. LOG_INFO("Gun %d TxId[%s] OCMF file [%s] open fail", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, fileName);
  10581. }
  10582. else
  10583. {
  10584. memset(_txId, 0x00, sizeof(_txId));
  10585. memset(_ocmfKey, 0x00, sizeof(_ocmfKey));
  10586. memset(_ocmfInfo, 0x00, sizeof(_ocmfInfo)); //-48 is take out the header
  10587. _ocmfLength = read(fd, _ocmfInfo, sizeof(_ocmfInfo) - 1);
  10588. close(fd);
  10589. memcpy(_txId, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId));
  10590. memcpy(_ocmfKey, ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey));
  10591. DB_Insert_Ocmf(localDb, i, _txId, _ocmfKey, _ocmfInfo);
  10592. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfPending = true;
  10593. char cmdBuf[256] = {0};
  10594. sprintf(cmdBuf, "rm -f %s", fileName);
  10595. system(cmdBuf);
  10596. LOG_INFO("Gun %d TxId[%s] insert OCMF info ok, length: %d", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, _ocmfLength);
  10597. }
  10598. }
  10599. else
  10600. {
  10601. LOG_INFO("Gun %d TxId[%s] OCMF info [%s] access fail", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, fileName);
  10602. }
  10603. memset(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, 0x00, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId));
  10604. memset(ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey, 0x00, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey));
  10605. ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfFlag.bits.OcmfReceived = false;
  10606. }
  10607. }
  10608. // try to find ocmf pending in database
  10609. if(ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfPending && !ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload)
  10610. {
  10611. _gunIndex = 0;
  10612. memset(_txId, 0x00, sizeof(_txId));
  10613. memset(_ocmfKey, 0x00, sizeof(_ocmfKey));
  10614. memset(_ocmfInfo, 0x00, sizeof(_ocmfInfo));
  10615. ShmChargerInfo->OcmfInfo.SysOcmfIndex = 0;
  10616. memset(ShmChargerInfo->OcmfInfo.SysOcmfTxId, 0x00, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfTxId));
  10617. memset(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey, 0x00, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey));
  10618. memset(ShmChargerInfo->OcmfInfo.SysOcmfInfo, 0x00, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfInfo));
  10619. if(DB_Get_Undisposed_Ocmf(localDb, &_gunIndex, _txId, _ocmfKey, _ocmfInfo))
  10620. {
  10621. ShmChargerInfo->OcmfInfo.SysOcmfIndex = _gunIndex;
  10622. memcpy(ShmChargerInfo->OcmfInfo.SysOcmfTxId, _txId, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfTxId));
  10623. memcpy(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey, _ocmfKey, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey));
  10624. memcpy(ShmChargerInfo->OcmfInfo.SysOcmfInfo, _ocmfInfo, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfInfo));
  10625. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload = true;
  10626. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = false;
  10627. LOG_INFO("Gun %d TxId[%s] OCMF info need upload", _gunIndex + 1, _txId);
  10628. }
  10629. else
  10630. {
  10631. LOG_INFO("There is no ocmf info pending");
  10632. ShmChargerInfo->OcmfInfo.SysOcmfFlag.CtrlValue = 0;
  10633. }
  10634. }
  10635. // try to upload ocmf to backend
  10636. if(ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload)
  10637. {
  10638. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  10639. {
  10640. if(!ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading)
  10641. {
  10642. if(!Is_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex))
  10643. {
  10644. Set_Ocpp_OcmfData(ShmChargerInfo->OcmfInfo.SysOcmfIndex, ShmChargerInfo->OcmfInfo.SysOcmfPublicKey, ShmChargerInfo->OcmfInfo.SysOcmfInfo);
  10645. Set_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex);
  10646. LOG_INFO("Set Gun %d TxId[%s] update OCMF request", ShmChargerInfo->OcmfInfo.SysOcmfIndex + 1, ShmChargerInfo->OcmfInfo.SysOcmfTxId);
  10647. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = true;
  10648. }
  10649. }
  10650. else
  10651. {
  10652. if(!Is_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex))
  10653. {
  10654. DB_Update_Ocmf_Status(localDb, ShmChargerInfo->OcmfInfo.SysOcmfTxId, OCMF_SATAUS_UPDATED_OK);
  10655. LOG_INFO("Gun %d TxId[%s] update OCMF info to backend ok", ShmChargerInfo->OcmfInfo.SysOcmfIndex + 1, ShmChargerInfo->OcmfInfo.SysOcmfTxId);
  10656. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload = false;
  10657. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = false;
  10658. }
  10659. }
  10660. }
  10661. else
  10662. {
  10663. if(ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading)
  10664. {
  10665. Clean_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex);
  10666. LOG_INFO("Backend disconnected, Gun %d TxId[%s] ocmf will update later", ShmChargerInfo->OcmfInfo.SysOcmfIndex + 1, ShmChargerInfo->OcmfInfo.SysOcmfTxId);
  10667. }
  10668. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = false;
  10669. }
  10670. }
  10671. }
  10672. int main(void)
  10673. {
  10674. unsigned long time = 0;
  10675. int _dispenser = 0;
  10676. _isStandbyEnable = true;
  10677. if(CreateShareMemory() == 0)
  10678. {
  10679. #ifdef SystemLogMessage
  10680. LOG_ERROR("CreatShareMemory NG");
  10681. #endif
  10682. if(ShmStatusCodeData!=NULL)
  10683. {
  10684. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  10685. }
  10686. sleep(5);
  10687. system("reboot -f");
  10688. sleep(5);
  10689. system("reboot -f");
  10690. }
  10691. LOG_INFO("\n");
  10692. LOG_INFO("===== Boot and Initialization start =====");
  10693. LOG_INFO("Main Fork PID = %d", getpid());
  10694. if (!InitialSystemDefaultConfig())
  10695. {
  10696. LOG_INFO("InitialSystemDefaultConfig NG");
  10697. StopProcessingLoop();
  10698. }
  10699. LOG_INFO("ModelName: [%s], SN: [%s], SW Version: [%s][%s]",
  10700. ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber, fwVersion, subVersion);
  10701. LOG_INFO("InitialShareMemoryInfo");
  10702. InitialShareMemoryInfo();
  10703. LOG_INFO("ChargerType (IEC or UL) = %d", ShmSysConfigAndInfo->SysInfo.ChargerType);
  10704. if(!InitialChargerSetting())
  10705. {
  10706. isModelNameMatch = false;
  10707. }
  10708. Initialization();
  10709. InitialDataBaseValue();
  10710. LOG_INFO("Spawn all Task.");
  10711. SpawnTask();
  10712. if (!isModelNameMatch)
  10713. {
  10714. LOG_INFO("Module Name & HW info none match.");
  10715. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  10716. // Module Name 與硬體對應不正確
  10717. LOG_ERROR("Module Name & HW info none match.");
  10718. sleep(3);
  10719. KillAllTask();
  10720. StopProcessingLoop();
  10721. }
  10722. LOG_INFO("Module Name & HW info correct. Initialize.......");
  10723. CreateTimeoutFork();
  10724. SelfTestRun();
  10725. while(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  10726. {
  10727. sleep(1);
  10728. }
  10729. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  10730. {
  10731. sleep(1);
  10732. SelfTestFailReason();
  10733. }
  10734. else
  10735. {
  10736. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10737. {
  10738. setChargerMode(gun_index, MODE_IDLE);
  10739. if(ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 ||
  10740. !ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  10741. {
  10742. ShmChargerInfo->Control.GunAvailable[gun_index] = YES;
  10743. }
  10744. }
  10745. }
  10746. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE)
  10747. {
  10748. SetAcContactor(ON);
  10749. SpawnOcppService();
  10750. SpawnMaintainOcppService();
  10751. }
  10752. else
  10753. {
  10754. InitSlaveCabinetEthernet();
  10755. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10756. {
  10757. setChargerMode(gun_index, MODE_MAINTAIN);
  10758. }
  10759. }
  10760. sleep(1);
  10761. CreateWatchdog();
  10762. // Main loop
  10763. LOG_INFO("%s \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_COMPLETE ?
  10764. "****************************Main Loop**********************************" :
  10765. "********************** SelfTest Fail **********************");
  10766. GetClockTime(&_cmdMainPriority_time);
  10767. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfPending = true;
  10768. for (;;)
  10769. {
  10770. CheckGunAvailable();
  10771. CheckOcppStatus();
  10772. ChkPrimaryStatus();
  10773. ChkPsuStatus();
  10774. PsuFailureResume();
  10775. ChkConnectorAction();
  10776. if(IsUpgradeAvailable())
  10777. {
  10778. CheckFactoryConfigFunction();
  10779. CheckFwUpdateFunction();
  10780. }
  10781. if(CheckFlashUpdate() != YES)
  10782. {
  10783. // return fork process
  10784. return 0;
  10785. }
  10786. CheckSoftHardReset();
  10787. StandbyCheck();
  10788. AuthorizeTimeoutProcess();
  10789. // check ocmf request
  10790. CheckOcmfHandler();
  10791. // 當 AC 沒有搭上時,清除一些錯誤碼
  10792. //ClearAlarmCodeWhenAcOff();
  10793. if ((GetTimeoutValue(_cmdMainPriority_time) / uSEC_VAL) > MAIN_PRIORITY_INTERVAL)
  10794. {
  10795. CheckTask();
  10796. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10797. {
  10798. if((chargingInfo[_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[_index]->SystemStatus <= S_CHARGING) ||
  10799. (chargingInfo[_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10800. {
  10801. if (chargingInfo[_index]->SystemStatus == S_CHARGING && _ocppProfileChkFlag[_index] == 12)
  10802. {
  10803. Ocpp_ProfileReq_Cmd(_index);
  10804. gunOutputVol[_index] = chargingInfo[_index]->PresentChargingVoltage;
  10805. _ocppProfileChkFlag[_index] = 0;
  10806. }
  10807. else if (chargingInfo[_index]->SystemStatus != S_CHARGING)
  10808. {
  10809. Ocpp_ProfileReq_Cmd(_index);
  10810. _ocppProfileChkFlag[_index] = 0;
  10811. }
  10812. else
  10813. {
  10814. _ocppProfileChkFlag[_index]++;
  10815. }
  10816. }
  10817. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  10818. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  10819. {
  10820. DB_Update_LocalConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  10821. if(ShmChargerInfo->MeterValue[_index].GunConsumption >= 0)
  10822. {
  10823. DB_Update_GunConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].GunConsumption);
  10824. }
  10825. }
  10826. }
  10827. CheckPsuLostQuantity();
  10828. GetClockTime(&_cmdMainPriority_time);
  10829. }
  10830. CheckMiscCommandRequirement();
  10831. CheckDispenserVersionUpdateRequirement();
  10832. MCabinetParallelingProcess();
  10833. SCabinetControllProcess();
  10834. UpdateChargingCapability();
  10835. GunErrorCollection();
  10836. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10837. {
  10838. //CheckGpioInStatus();
  10839. CheckErrorOccurStatus(gun_index);
  10840. ChkOcppStatus(gun_index);
  10841. if((chargingInfo[gun_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= S_CHARGING) ||
  10842. (chargingInfo[gun_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10843. {
  10844. if(chargingInfo[gun_index]->SystemStatus == S_CHARGING)
  10845. {
  10846. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  10847. if (chargingInfo[gun_index]->PresentChargingVoltage >= gunOutputVol[gun_index] + 10 ||
  10848. chargingInfo[gun_index]->PresentChargingVoltage <= gunOutputVol[gun_index] - 10)
  10849. {
  10850. _ocppProfileChkFlag[gun_index] = 12;
  10851. gunOutputVol[gun_index] = chargingInfo[gun_index]->PresentChargingVoltage;
  10852. }
  10853. }
  10854. }
  10855. CheckSmartChargeProfile(gun_index);
  10856. Update_PowerConsumption(gun_index);
  10857. CheckInProgress(gun_index);
  10858. CheckDebugMode(gun_index);
  10859. //LOG_INFO("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  10860. switch(chargingInfo[gun_index]->SystemStatus)
  10861. {
  10862. case S_MAINTAIN:
  10863. if(isModeChange(gun_index))
  10864. {
  10865. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10866. {
  10867. LOG_INFO("================== Slave Cabinet %d ==================", ShmChargerInfo->Control.CabinetSwitch - 1);
  10868. }
  10869. LOG_INFO("================== S_MAINTAIN (%x) ================== \n", gun_index + 1);
  10870. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10871. GetClockTime(&_SystemStatus_Time[gun_index]);
  10872. }
  10873. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  10874. if(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE ||
  10875. ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  10876. ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10877. {
  10878. break;
  10879. }
  10880. if(chargingInfo[gun_index]->IsAvailable == YES &&
  10881. ShmChargerInfo->Control.GunAvailable[gun_index] == YES &&
  10882. ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  10883. {
  10884. if(time >= MAINTAIN_RECOVERY_TIME)
  10885. {
  10886. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10887. setChargerMode(gun_index, MODE_IDLE);
  10888. }
  10889. }
  10890. break;
  10891. case S_RESERVATION:
  10892. if(isModeChange(gun_index))
  10893. {
  10894. LOG_INFO("================== S_RESERVATION (%x) ================== \n", gun_index + 1);
  10895. char expiryDate[128];
  10896. Get_Ocpp_ReserveNowExpiryDate(gun_index, expiryDate);
  10897. Get_Ocpp_ReserveNowIdTag(gun_index, &ShmChargerInfo->Control.ResevedIdTag[gun_index][0]);
  10898. Set_Connector_MiscCommand(gun_index, MISC_CONN_RESERVATION);
  10899. LOG_INFO("****** Reservation IdTag: [%s], Expiry: [%s]", &ShmChargerInfo->Control.ResevedIdTag[gun_index][0], expiryDate);
  10900. GetClockTime(&_SystemStatus_Time[gun_index]);
  10901. }
  10902. if(chargingInfo[gun_index]->ConnectorPlugIn)
  10903. {
  10904. SetAcContactor(ON);
  10905. }
  10906. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  10907. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  10908. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  10909. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10910. {
  10911. UpdateErrorCodeToOcpp(gun_index);
  10912. setChargerMode(gun_index, MODE_FAULT);
  10913. continue;
  10914. }
  10915. if(Is_Ocpp_ReserveNowExpired(gun_index))
  10916. {
  10917. chargingInfo[gun_index]->ReservationId = -1;
  10918. LOG_INFO("Gun %d reservation is expired...", gun_index + 1);
  10919. setChargerMode(gun_index, MODE_IDLE);
  10920. }
  10921. else
  10922. {
  10923. if(IsConnectorAuthorizeSuccess(gun_index))
  10924. {
  10925. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10926. {
  10927. LOG_INFO("Gun %d remote start reserve charging", gun_index + 1);
  10928. }
  10929. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  10930. {
  10931. LOG_INFO("Gun %d swipe rfid start reserve charging", gun_index + 1);
  10932. }
  10933. setChargerMode(gun_index, MODE_AUTHORIZING);
  10934. }
  10935. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  10936. {
  10937. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  10938. if(Is_Ocpp_ReserveNowIdTag(gun_index, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId))
  10939. {
  10940. SetAcContactor(ON);
  10941. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  10942. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10943. {
  10944. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  10945. LOG_INFO("Gun %d reserve Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  10946. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  10947. {
  10948. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  10949. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  10950. }
  10951. DispenserAuthInitial(_dispenser);
  10952. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10953. }
  10954. }
  10955. }
  10956. else
  10957. {
  10958. if(chargingInfo[gun_index]->ReservationId == -1)
  10959. {
  10960. LOG_INFO("Gun %d reservation is clean", gun_index + 1);
  10961. setChargerMode(gun_index, MODE_IDLE);
  10962. }
  10963. }
  10964. }
  10965. break;
  10966. case S_IDLE:
  10967. case S_FAULT:
  10968. {
  10969. // clean PermissionRequest flag
  10970. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  10971. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  10972. if(isModeChange(gun_index))
  10973. {
  10974. if (chargingInfo[gun_index]->SystemStatus == S_IDLE)
  10975. {
  10976. LOG_INFO("================== S_IDLE (%x) ================== \n", gun_index + 1);
  10977. chargingInfo[gun_index]->ReservationId = -1;
  10978. _triggerTransaction[gun_index] = false;
  10979. _plugInHold[gun_index] = false;
  10980. chargingInfo[gun_index]->PresentChargedDuration = 0;
  10981. chargingInfo[gun_index]->RemainChargingDuration = 0;
  10982. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  10983. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  10984. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  10985. memset(ShmChargerInfo->Timestamp[gun_index].StartCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StartCharging));
  10986. memset(ShmChargerInfo->Timestamp[gun_index].StopCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StopCharging));
  10987. Clean_Ocpp_StopReason(gun_index);
  10988. ReleaseAlarmCode(gun_index);
  10989. // clean connector information
  10990. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  10991. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  10992. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  10993. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  10994. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  10995. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  10996. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -65535;
  10997. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].CostDiscount = 0;
  10998. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemainAmount = 0;
  10999. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].EnergyCost = -1;
  11000. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParkingFee = -1;
  11001. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  11002. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.GetStartChargingSoc = false;
  11003. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  11004. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  11005. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  11006. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.OutputLimitEnable = false;
  11007. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].GeneralChargingData.deratingByConnOtp.deratingIndex = 0;
  11008. chargingInfo[gun_index]->EvBatterytargetVoltage = 0;
  11009. chargingInfo[gun_index]->EvBatterytargetCurrent = 0;
  11010. chargingInfo[gun_index]->EvBatteryMaxVoltage = 0;
  11011. chargingInfo[gun_index]->EvBatterySoc = 0;
  11012. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetVoltage = 0;
  11013. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetCurrent = 0;
  11014. // clean RemoteStart & RemoteStop flag
  11015. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_START);
  11016. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_STOP);
  11017. Clean_Connector_MiscCommand(gun_index, MISC_CONN_UNLOCK);
  11018. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  11019. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopConfirm = false;
  11020. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopConfirm = false;
  11021. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NormalStopRequest = false;
  11022. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AlarmStopRequest = false;
  11023. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  11024. ShmChargerInfo->GunError[gun_index].ErrFlag.ErrorValue = 0;
  11025. memset(chargingInfo[gun_index]->ConnectorAlarmCode, 0x00, sizeof(chargingInfo[gun_index]->ConnectorAlarmCode));
  11026. Clean_Ocpp_RunningCost(gun_index);
  11027. Clean_Ocpp_FinalCost(gun_index);
  11028. Clean_DeductInfo(gun_index);
  11029. Clean_Ocpp_TcciSerialNo(gun_index);
  11030. Clean_UserTransaction(gun_index);
  11031. Clean_UserPriceAndReceipt(gun_index);
  11032. presentChargedEnergyClear(gun_index);
  11033. chargingInfo[gun_index]->PresentChargingPower = 0;
  11034. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower = 0;
  11035. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent = 0;
  11036. chargingInfo[gun_index]->ChargingProfilePower = -1;
  11037. chargingInfo[gun_index]->ChargingProfileCurrent = -1;
  11038. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  11039. {
  11040. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging = false;
  11041. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger = false;
  11042. // reset dispenser availability
  11043. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  11044. }
  11045. // clean force charging info
  11046. memset(&ShmChargerInfo->Control.FCharging[gun_index], 0x00, sizeof(ForceCharging));
  11047. }
  11048. else if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  11049. {
  11050. LOG_INFO("================== S_FAULT (%x) ================== \n", gun_index + 1);
  11051. }
  11052. GetClockTime(&_SystemStatus_Time[gun_index]);
  11053. }
  11054. if (chargingInfo[gun_index]->IsAvailable == NO ||
  11055. ShmChargerInfo->Control.GunAvailable[gun_index] == NO)
  11056. {
  11057. setChargerMode(gun_index, MODE_MAINTAIN);
  11058. continue;
  11059. }
  11060. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  11061. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  11062. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  11063. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  11064. {
  11065. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest)
  11066. {
  11067. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11068. {
  11069. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemotenAlarmCode, 6);
  11070. }
  11071. }
  11072. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  11073. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  11074. {
  11075. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11076. {
  11077. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042304", 6);
  11078. LOG_INFO("Connector %d Disconnect Occur", gun_index + 1);
  11079. }
  11080. }
  11081. if(chargingInfo[gun_index]->SystemStatus != S_FAULT)
  11082. {
  11083. UpdateErrorCodeToOcpp(gun_index);
  11084. setChargerMode(gun_index, MODE_FAULT);
  11085. }
  11086. else
  11087. {
  11088. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  11089. {
  11090. CheckConnectorDisconnectionRecovery(gun_index);
  11091. }
  11092. }
  11093. }
  11094. else
  11095. {
  11096. if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  11097. {
  11098. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  11099. if(time >= FAULT_RELEASE_TIME)
  11100. {
  11101. ReleaseAlarmCode(gun_index);
  11102. setChargerMode(gun_index, MODE_IDLE);
  11103. }
  11104. break;
  11105. }
  11106. // clean stop charge flag
  11107. chargingInfo[gun_index]->StopChargeFlag = false;
  11108. chargingInfo[gun_index]->ChargingStopFlag.ChargingStopValue = 0;
  11109. if(chargingInfo[gun_index]->ConnectorPlugIn || ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyTimeDisable)
  11110. {
  11111. SetAcContactor(ON);
  11112. }
  11113. { // Idle 正常程序起點
  11114. // 判斷是否有啟用檢查插槍
  11115. if(IsConnectorAuthorizeSuccess(gun_index))
  11116. {
  11117. if(chargingInfo[gun_index]->IsAvailable &&
  11118. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  11119. {
  11120. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  11121. {
  11122. LOG_INFO("Gun %d remote start charging", gun_index + 1);
  11123. Clean_Ocpp_TcciCreditInfo(gun_index);
  11124. }
  11125. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  11126. {
  11127. LOG_INFO("Gun %d swipe rfid start charging", gun_index + 1);
  11128. }
  11129. Set_StartIdType(gun_index, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthIdType);
  11130. setChargerMode(gun_index, MODE_AUTHORIZING);
  11131. continue;
  11132. }
  11133. }
  11134. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  11135. {
  11136. SetAcContactor(ON);
  11137. if(chargingInfo[gun_index]->IsAvailable &&
  11138. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  11139. chargingInfo[gun_index]->ConnectorPlugIn &&
  11140. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  11141. {
  11142. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  11143. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  11144. LOG_INFO("Gun %d Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  11145. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  11146. {
  11147. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  11148. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  11149. }
  11150. Set_StartIdType(gun_index, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthIdType);
  11151. DispenserAuthInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  11152. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  11153. continue;
  11154. }
  11155. }
  11156. // only for test & debug purpose
  11157. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging &&
  11158. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging)
  11159. {
  11160. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging = false;
  11161. strcpy((char *)chargingInfo[gun_index]->StartUserId, "ChargingTest");
  11162. LOG_INFO("Gun %d Start Force Charging%s",
  11163. gun_index + 1,
  11164. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger > 0 ? ", Triggered by WebAPI" : "");
  11165. if(ShmChargerInfo->Control.TestCtrl.bits.FChargingReleaseExtend)
  11166. {
  11167. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableReleaseAndExtend = true;
  11168. LOG_INFO("Gun %d Enable ReleaseAndExtend At Force Charging", gun_index + 1);
  11169. }
  11170. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  11171. {
  11172. LOG_INFO("Set Dispenser %d Inoperative",
  11173. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex + 1);
  11174. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  11175. }
  11176. Set_StartIdType(gun_index, _Type_Local);
  11177. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  11178. continue;
  11179. }
  11180. else
  11181. {
  11182. }
  11183. } // Idle 正常程序終點
  11184. }
  11185. }
  11186. break;
  11187. case S_AUTHORIZING:
  11188. if(isModeChange(gun_index))
  11189. {
  11190. LOG_INFO("================== S_AUTHORIZING (%x) ================== \n", gun_index + 1);
  11191. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  11192. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  11193. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  11194. //StopSystemTimeoutDet();
  11195. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  11196. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  11197. SetAcContactor(ON);
  11198. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  11199. }
  11200. if(!_triggerTransaction[gun_index] &&
  11201. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC &&
  11202. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  11203. {
  11204. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= STARTTRANSATION_TIME)
  11205. {
  11206. OcppStartTransaction(gun_index);
  11207. }
  11208. }
  11209. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  11210. {
  11211. FouceAnnounceAccountBalance(gun_index);
  11212. }
  11213. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm)
  11214. {
  11215. // sync with dispenser
  11216. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  11217. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  11218. LOG_INFO("Sync with Gun %d through remote start", gun_index + 1);
  11219. FouceAnnounceAccountBalance(gun_index);
  11220. }
  11221. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm)
  11222. {
  11223. // sync with dispenser
  11224. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm = false;
  11225. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  11226. LOG_INFO("Sync with Connector %d through swipe rfid", gun_index + 1);
  11227. }
  11228. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  11229. {
  11230. if(!chargingInfo[gun_index]->ConnectorPlugIn)
  11231. {
  11232. LOG_INFO("*********** Gun %d Plug In Timeout ***********", gun_index + 1);
  11233. }
  11234. else
  11235. {
  11236. LOG_INFO("*********** Gun %d Preparing State Timeout ***********", gun_index + 1);
  11237. }
  11238. OcppStopTransaction(gun_index);
  11239. setChargerMode(gun_index, MODE_IDLE);
  11240. }
  11241. else
  11242. {
  11243. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  11244. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  11245. {
  11246. AddPlugInTimes(gun_index);
  11247. LOG_INFO("Gun %d CardNumber = [%s]", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  11248. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  11249. GunAuthInitial(gun_index);
  11250. }
  11251. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  11252. {
  11253. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  11254. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  11255. LOG_INFO("*********** Gun %d User Stop Plug In ***********", gun_index + 1);
  11256. OcppStopTransaction(gun_index);
  11257. setChargerMode(gun_index, MODE_IDLE);
  11258. }
  11259. else if(isEvBoardStopChargeFlag(gun_index) == YES)
  11260. {
  11261. LOG_INFO("********** Gun %d Alarm Stop (S_AUTHORIZING) **********", gun_index + 1);
  11262. Set_Ocpp_StopReason(gun_index, "Local");
  11263. ChargingAlarmProcess(gun_index);
  11264. }
  11265. else if(isNormalStopChargeFlag(gun_index) == YES)
  11266. {
  11267. LOG_INFO("********** Gun %d Normal Stop (S_AUTHORIZING) **********", gun_index + 1);
  11268. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11269. ChargingTerminalProcess(gun_index);
  11270. }
  11271. }
  11272. break;
  11273. case S_REASSIGN_CHECK:
  11274. {
  11275. if (isModeChange(gun_index))
  11276. {
  11277. LOG_INFO("================== S_REASSIGN_CHECK (%x) ================== \n", gun_index + 1);
  11278. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  11279. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  11280. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  11281. //StopSystemTimeoutDet();
  11282. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  11283. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy = ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy;
  11284. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration = ShmSysConfigAndInfo->SysConfig.MaxChargingDuration;
  11285. LOG_INFO("Max Total Current %d A, Max Total Power %d kW, Total Energy %d kWh, Total Duration %d",
  11286. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent,
  11287. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  11288. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy,
  11289. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration);
  11290. SetAcContactor(ON);
  11291. SetPCabinetOutputRelay(gun_index, ON);
  11292. GetClockTime(&_SystemStatus_Time[gun_index]);
  11293. }
  11294. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11295. if(ConnectorRequestToCharging(gun_index))
  11296. {
  11297. setChargerMode(gun_index, MODE_REASSIGN);
  11298. }
  11299. else if(time >= WAIT_REASSIGN_TIME)
  11300. {
  11301. if(ShmPsuData->Work_Step != _WORK_CHARGING)
  11302. {
  11303. LOG_INFO("Gun %d Wait PSU Work Step Timeout", gun_index + 1);
  11304. }
  11305. else
  11306. {
  11307. LOG_INFO("Gun %d Request Charging Timeout", gun_index + 1);
  11308. }
  11309. ShmChargerInfo->GunError[gun_index].ErrFlag.bits.PsuGroupNoResource = true;
  11310. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11311. {
  11312. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042279", 6);
  11313. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  11314. }
  11315. Set_Ocpp_StopReason(gun_index, "Local");
  11316. ChargingAlarmProcess(gun_index);
  11317. //setChargerMode(gun_index, MODE_IDLE);
  11318. }
  11319. }
  11320. break;
  11321. case S_REASSIGN:
  11322. {
  11323. if (isModeChange(gun_index))
  11324. {
  11325. LOG_INFO("================== S_REASSIGN (%x) ================== \n", gun_index + 1);
  11326. //gettimeofday(&_toAverage_time, NULL);
  11327. GetClockTime(&_SystemStatus_Time[gun_index]);
  11328. }
  11329. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11330. if(IsConnectorGroupingCompleted(gun_index))
  11331. {
  11332. setChargerMode(gun_index, MODE_PRECHARGE);
  11333. }
  11334. else if(time >= MAX_REQUEST_CHARGING_TIME)
  11335. {
  11336. LOG_INFO("Gun %d Grouping Timeout", gun_index + 1);
  11337. setChargerMode(gun_index, MODE_IDLE);
  11338. }
  11339. }
  11340. break;
  11341. case S_PREPARNING:
  11342. {
  11343. if (isModeChange(gun_index))
  11344. {
  11345. LOG_INFO("================== S_PREPARNING (%x) ================== \n", gun_index + 1);
  11346. //StopGunInfoTimeoutDet(gun_index);
  11347. //StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  11348. SetAcContactor(ON);
  11349. GetClockTime(&_SystemStatus_Time[gun_index]);
  11350. }
  11351. UpdateGunPresentChargingPower(gun_index);
  11352. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11353. if ((chargingInfo[gun_index]->AvailableChargingPower > 10) && time >= MIN_WAIT_PREPARNING_TIME)
  11354. {
  11355. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  11356. break;;
  11357. }
  11358. if (isEvBoardStopChargeFlag(gun_index) == YES)
  11359. {
  11360. // 板端或後臺要求停止
  11361. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARNING) **********", gun_index + 1);
  11362. Set_Ocpp_StopReason(gun_index, "Local");
  11363. ChargingAlarmProcess(gun_index);
  11364. }
  11365. else if(isNormalStopChargeFlag(gun_index) == YES)
  11366. {
  11367. LOG_INFO("********** Gun %d Normal Stop (S_PREPARNING) %s**********", gun_index + 1,
  11368. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11369. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11370. ChargingTerminalProcess(gun_index);
  11371. }
  11372. else if(isResetStopChargeFlag(gun_index) == YES)
  11373. {
  11374. LOG_INFO("********** Gun %d Reset Stop (S_PREPARNING) %s**********", gun_index + 1,
  11375. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11376. char *reason[16];
  11377. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11378. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11379. ChargingTerminalProcess(gun_index);
  11380. }
  11381. else if(isBackendStopFlag(gun_index))
  11382. {
  11383. LOG_INFO("********** Gun %d Backend Stop (S_PREPARNING) **********", gun_index + 1);
  11384. Set_Ocpp_StopReason(gun_index, "Other");
  11385. ChargingTerminalProcess(gun_index);
  11386. }
  11387. if(time > GUN_PREPARE_TIMEOUT)
  11388. {
  11389. LOG_INFO("********** Gun %d PrepareTimeout **********", gun_index + 1);
  11390. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  11391. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11392. {
  11393. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "012279", 6);
  11394. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  11395. }
  11396. Set_Ocpp_StopReason(gun_index, "Local");
  11397. ChargingAlarmProcess(gun_index);
  11398. }
  11399. }
  11400. break;
  11401. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  11402. {
  11403. if (isModeChange(gun_index))
  11404. {
  11405. LOG_INFO("================== S_PREPARING_FOR_EV (%x) ================== \n", gun_index + 1);
  11406. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  11407. //StopGunInfoTimeoutDet(gun_index);
  11408. //StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  11409. GetClockTime(&_SystemStatus_Time[gun_index]);
  11410. }
  11411. UpdateGunPresentChargingPower(gun_index);
  11412. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11413. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  11414. {
  11415. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  11416. }
  11417. // only for test & debug purpose
  11418. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11419. {
  11420. LOG_INFO("Gun %d [Force Charging] S_PREPARING_FOR_EV OK", gun_index + 1);
  11421. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = true;
  11422. }
  11423. if (isEvBoardStopChargeFlag(gun_index) == YES)
  11424. {
  11425. // 板端或後臺要求停止
  11426. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  11427. Set_Ocpp_StopReason(gun_index, "Local");
  11428. ChargingAlarmProcess(gun_index);
  11429. }
  11430. else if(isNormalStopChargeFlag(gun_index) == YES)
  11431. {
  11432. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  11433. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11434. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11435. ChargingTerminalProcess(gun_index);
  11436. }
  11437. else if(isResetStopChargeFlag(gun_index) == YES)
  11438. {
  11439. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  11440. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11441. char *reason[16];
  11442. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11443. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11444. ChargingTerminalProcess(gun_index);
  11445. }
  11446. else if(isBackendStopFlag(gun_index))
  11447. {
  11448. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  11449. Set_Ocpp_StopReason(gun_index, "Other");
  11450. ChargingTerminalProcess(gun_index);
  11451. }
  11452. if(time >= GUN_EV_WAIT_TIMEOUT)
  11453. {
  11454. LOG_INFO("********** Gun %d Wait Dispenser Timeout **********", gun_index + 1);
  11455. ChargingTerminalProcess(gun_index);
  11456. }
  11457. }
  11458. break;
  11459. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  11460. {
  11461. if (isModeChange(gun_index))
  11462. {
  11463. LOG_INFO("================== S_PREPARING_FOR_EVSE (%x) ================== \n", gun_index + 1);
  11464. GetClockTime(&_SystemStatus_Time[gun_index]);
  11465. #ifdef STARTTRANSATION_IMMEDIATELY
  11466. OcppStartTransaction(gun_index);
  11467. #endif
  11468. }
  11469. CheckoutStartChargingSoc(gun_index, NO);
  11470. UpdateGunPresentChargingPower(gun_index);
  11471. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11472. // only for test & debug purpose
  11473. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11474. {
  11475. if(time >= FORCE_CHARGING_CMD_DELAY)
  11476. {
  11477. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  11478. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  11479. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  11480. {
  11481. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  11482. }
  11483. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  11484. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  11485. }
  11486. }
  11487. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Charging)
  11488. {
  11489. setChargerMode(gun_index, MODE_CHARGING);
  11490. }
  11491. // only for test & debug purpose
  11492. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11493. {
  11494. if(time >= FORCE_CHARGING_WAIT_EVSE)
  11495. {
  11496. LOG_INFO("Gun %d [Charging Simulation] S_PREPARING_FOR_EVSE OK", gun_index + 1);
  11497. setChargerMode(gun_index, MODE_CHARGING);
  11498. }
  11499. }
  11500. if (isEvBoardStopChargeFlag(gun_index) == YES)
  11501. {
  11502. // 板端或後臺要求停止
  11503. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  11504. Set_Ocpp_StopReason(gun_index, "Local");
  11505. ChargingAlarmProcess(gun_index);
  11506. }
  11507. else if(isNormalStopChargeFlag(gun_index) == YES)
  11508. {
  11509. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  11510. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11511. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11512. ChargingTerminalProcess(gun_index);
  11513. }
  11514. else if(isResetStopChargeFlag(gun_index) == YES)
  11515. {
  11516. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  11517. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11518. char *reason[16];
  11519. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11520. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11521. ChargingTerminalProcess(gun_index);
  11522. }
  11523. else if(isInvalidIdStopFlag(gun_index) == YES)
  11524. {
  11525. LOG_INFO("********** Gun %d InvalidId Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  11526. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  11527. ChargingTerminalProcess(gun_index);
  11528. }
  11529. else if(isBackendStopFlag(gun_index))
  11530. {
  11531. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  11532. Set_Ocpp_StopReason(gun_index, "Other");
  11533. ChargingTerminalProcess(gun_index);
  11534. }
  11535. if(time >= GUN_EV_WAIT_TIMEOUT)
  11536. {
  11537. LOG_INFO("********** Gun %d GFD timeout **********", gun_index + 1);
  11538. ChargingTerminalProcess(gun_index);
  11539. }
  11540. }
  11541. break;
  11542. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  11543. {
  11544. if (isModeChange(gun_index))
  11545. {
  11546. LOG_INFO("================== S_CHARGING (%x) ================== \n", gun_index + 1);
  11547. //StopGunInfoTimeoutDet(gun_index);
  11548. GetClockTime(&_SystemStatus_Time[gun_index]);
  11549. GetClockTime(&_ChargedEnergy_Time[gun_index]);
  11550. CheckoutStartChargingSoc(gun_index, YES);
  11551. chargingInfo[gun_index]->PresentChargedDuration = 0;
  11552. ChangeStartOrStopDateTime(YES, gun_index);
  11553. #ifndef STARTTRANSATION_IMMEDIATELY
  11554. OcppStartTransaction(gun_index);
  11555. #endif
  11556. }
  11557. // only for test & debug purpose
  11558. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11559. {
  11560. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  11561. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  11562. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  11563. {
  11564. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  11565. }
  11566. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  11567. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  11568. }
  11569. // 計算 Power
  11570. //chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  11571. //chargingInfo[gun_index]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent))) / 1000 / 100);
  11572. UpdateGunPresentChargingPower(gun_index);
  11573. ChargedEnergySummation(gun_index);
  11574. CheckSessionTargetStop(gun_index);
  11575. if (isEvBoardStopChargeFlag(gun_index))
  11576. {
  11577. LOG_INFO("********** Gun %d Alarm Stop (S_CHARGING) **********", gun_index + 1);
  11578. Set_Ocpp_StopReason(gun_index, "Local");
  11579. ChargingAlarmProcess(gun_index);
  11580. }
  11581. else if(isNormalStopChargeFlag(gun_index) == YES)
  11582. {
  11583. LOG_INFO("********** Gun %d Normal Stop (S_CHARGING) %s**********", gun_index + 1,
  11584. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11585. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11586. ChargingTerminalProcess(gun_index);
  11587. }
  11588. else if(isResetStopChargeFlag(gun_index) == YES)
  11589. {
  11590. LOG_INFO("********** Gun %d Reset Stop (S_CHARGING) %s**********", gun_index + 1,
  11591. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11592. char *reason[16];
  11593. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11594. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11595. ChargingTerminalProcess(gun_index);
  11596. }
  11597. else if(isInvalidIdStopFlag(gun_index) == YES)
  11598. {
  11599. LOG_INFO("********** Gun %d InvalidId Stop (S_CHARGING) **********", gun_index + 1);
  11600. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  11601. ChargingTerminalProcess(gun_index);
  11602. }
  11603. else if(CheckBackendChargingTimeout(gun_index) ||
  11604. CheckBackendChargingEnergy(gun_index) ||
  11605. IsReachConfigMaxChargingSoc(gun_index))
  11606. {
  11607. LOG_INFO("********** Gun %d Backend Condition Stop (S_CHARGING) **********", gun_index + 1);
  11608. ChargingTerminalProcess(gun_index);
  11609. }
  11610. else if(isSessionTargetStopFlag(gun_index) == YES)
  11611. {
  11612. LOG_INFO("********** Gun %d SessionTarget Stop (S_CHARGING) **********", gun_index + 1);
  11613. Set_Ocpp_StopReason(gun_index, "Other");
  11614. ChargingTerminalProcess(gun_index);
  11615. }
  11616. else if(isBackendStopFlag(gun_index))
  11617. {
  11618. LOG_INFO("********** Gun %d Backend Stop (S_CHARGING) **********", gun_index + 1);
  11619. Set_Ocpp_StopReason(gun_index, "Other");
  11620. ChargingTerminalProcess(gun_index);
  11621. }
  11622. }
  11623. break;
  11624. case S_ALARM:
  11625. case S_TERMINATING:
  11626. {
  11627. if (isModeChange(gun_index))
  11628. {
  11629. Set_Ocpp_StopReason(gun_index, "Other");
  11630. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  11631. {
  11632. LOG_INFO("================== S_ALARM (%x) ================== \n", gun_index + 1);
  11633. UpdateErrorCodeToOcpp(gun_index);
  11634. TheEndCharging(gun_index);
  11635. }
  11636. else
  11637. {
  11638. LOG_INFO("================== S_TERMINATING (%x) ================== \n", gun_index + 1);
  11639. }
  11640. //StopGunInfoTimeoutDet(gun_index);
  11641. GetClockTime(&_SystemStatus_Time[gun_index]);
  11642. }
  11643. UpdateGunPresentChargingPower(gun_index);
  11644. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11645. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  11646. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  11647. {
  11648. OcppStopTransaction(gun_index);
  11649. }
  11650. else
  11651. {
  11652. ChargedEnergySummation(gun_index);
  11653. }
  11654. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  11655. {
  11656. if(time >= GUN_COMP_WAIT_TIMEOUT)
  11657. {
  11658. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11659. {
  11660. LOG_INFO ("********** Gun %d psu is releasable **********\n", gun_index + 1);
  11661. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  11662. }
  11663. }
  11664. if(IsAvailableBackToIdle(gun_index) == YES &&
  11665. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11666. {
  11667. ReleaseAlarmCode(gun_index);
  11668. setChargerMode(gun_index, MODE_IDLE);
  11669. }
  11670. }
  11671. else
  11672. {
  11673. setChargerMode(gun_index, MODE_COMPLETE);
  11674. }
  11675. }
  11676. break;
  11677. case S_COMPLETE:
  11678. {
  11679. if (isModeChange(gun_index))
  11680. {
  11681. LOG_INFO("================== S_COMPLETE (%x) ================== \n", gun_index + 1);
  11682. TheEndCharging(gun_index);
  11683. GetClockTime(&_SystemStatus_Time[gun_index]);
  11684. }
  11685. UpdateGunPresentChargingPower(gun_index);
  11686. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11687. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  11688. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  11689. {
  11690. OcppStopTransaction(gun_index);
  11691. }
  11692. else
  11693. {
  11694. ChargedEnergySummation(gun_index);
  11695. }
  11696. if(time >= GUN_COMP_WAIT_TIMEOUT)
  11697. {
  11698. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11699. {
  11700. LOG_INFO ("********** Gun %d psu is releasable **********", gun_index + 1);
  11701. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  11702. }
  11703. }
  11704. if(IsAvailableBackToIdle(gun_index) == YES &&
  11705. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11706. {
  11707. setChargerMode(gun_index, MODE_IDLE);
  11708. }
  11709. }
  11710. break;
  11711. case S_CCS_PRECHARGE_ST0:
  11712. if (isModeChange(gun_index))
  11713. {
  11714. LOG_INFO("CCS Precharge Processing 1....................%x \n", gun_index);
  11715. //StopGunInfoTimeoutDet(gun_index);
  11716. //StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  11717. }
  11718. break;
  11719. case S_CCS_PRECHARGE_ST1:
  11720. if (isModeChange(gun_index))
  11721. {
  11722. LOG_INFO("CCS Precharge Processing 2....................%x \n", gun_index);
  11723. }
  11724. break;
  11725. case S_DEBUG:
  11726. if (isModeChange(gun_index))
  11727. {
  11728. LOG_INFO("================== S_DEBUG (%x) ================== \n", gun_index + 1);
  11729. }
  11730. if(!ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay &&
  11731. !ShmChargerInfo->Control.FanCtrl.bits.AbnormalFan)
  11732. {
  11733. setChargerMode(gun_index, MODE_IDLE);
  11734. }
  11735. break;
  11736. }
  11737. }
  11738. TryFeedWatchdog();
  11739. usleep(whileLoopTime);
  11740. }
  11741. return FAIL;
  11742. }