main.c 470 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210
  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 = true;
  2246. ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse = true;
  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. InitialCustomerCode();
  2274. InitialCustomizedSetting();
  2275. InitialMaxDispenserConnectorQuantity();
  2276. InitialAuthMode();
  2277. InitialQRCodeMode();
  2278. Initial6ParallelSetting();
  2279. InitialPsuGroupingAndLocation();
  2280. InitialPowerCabinetSetting();
  2281. PowerModuleLibSelection();
  2282. if(!CheckConnectorTypeStatus())
  2283. {
  2284. result = false;
  2285. }
  2286. return result;
  2287. }
  2288. bool InitialSystemDefaultConfig()
  2289. {
  2290. bool result = true;
  2291. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2292. InitGPIO();
  2293. InitEthernet();
  2294. GetMacAddress();
  2295. return result;
  2296. }
  2297. void SelfTestRun(void)
  2298. {
  2299. LOG_INFO("Self test.");
  2300. system("/root/Module_ChargerSelfTest &");
  2301. }
  2302. void SpawnPowerModuleTask(void)
  2303. {
  2304. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  2305. {
  2306. system("/root/Module_PsuComm_PH &");
  2307. }
  2308. else
  2309. {
  2310. system("/root/Module_PsuComm &");
  2311. }
  2312. }
  2313. void KillPowerModuleTask(void)
  2314. {
  2315. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  2316. {
  2317. system("killall Module_PsuComm_PH");
  2318. }
  2319. else
  2320. {
  2321. system("killall Module_PsuComm");
  2322. }
  2323. }
  2324. void SpawnNormalService(void)
  2325. {
  2326. system("/root/Module_EventLogging &");
  2327. system("/root/Module_PrimaryComm &");
  2328. system("/root/Module_EvComm &");
  2329. //system("/root/Module_LcmControl &");
  2330. system("/root/Module_InternalComm &");
  2331. SpawnPowerModuleTask();
  2332. system("/root/Module_ProduceUtils &");
  2333. system("/root/Module_CabinetParallel &");
  2334. system("/root/Module_LedIndication &");
  2335. system("/root/Module_Authorize &");
  2336. }
  2337. void SpawnOcppService(void)
  2338. {
  2339. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  2340. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  2341. {
  2342. Run_Ocpp_Service();
  2343. }
  2344. }
  2345. void SpawnMaintainOcppService(void)
  2346. {
  2347. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  2348. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  2349. {
  2350. Run_MaintainOcpp_Service();
  2351. }
  2352. }
  2353. void SpawnInternetService(void)
  2354. {
  2355. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  2356. {
  2357. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2358. {
  2359. system("/root/Module_4g &");
  2360. }
  2361. }
  2362. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2363. {
  2364. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2365. {
  2366. system("/root/Module_Wifi &");
  2367. }
  2368. }
  2369. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  2370. {
  2371. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2372. {
  2373. system("/root/Module_4g &");
  2374. }
  2375. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2376. {
  2377. system("/root/Module_Wifi &");
  2378. }
  2379. }
  2380. }
  2381. int SpawnTask(void)
  2382. {
  2383. sleep(2);
  2384. SpawnNormalService();
  2385. SpawnInternetService();
  2386. return PASS;
  2387. }
  2388. int runFlashShellCmd(const char *cmd)
  2389. {
  2390. int result = FAIL;
  2391. char buf[256];
  2392. FILE *fp;
  2393. fp = popen(cmd, "r");
  2394. if(fp != NULL)
  2395. {
  2396. while(fgets(buf, sizeof(buf), fp) != NULL)
  2397. {
  2398. LOG_INFO("%s\n", buf);
  2399. }
  2400. result = PASS;
  2401. }
  2402. pclose(fp);
  2403. return result;
  2404. }
  2405. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2406. {
  2407. int result = PASS;
  2408. int fd, wrd;
  2409. unsigned int i, Chk;
  2410. unsigned char *ptr, *BufTmp;
  2411. Chk = 0;
  2412. ptr = (unsigned char *)UsrData;
  2413. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2414. {
  2415. memset(BufTmp, 0, MtdBlockSize);
  2416. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2417. for(i = ARRAY_SIZE(UsrData->CsuBootLoadFwRev); i < MtdBlockSize - 4; i++)
  2418. {
  2419. Chk += *(BufTmp+i);
  2420. }
  2421. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  2422. // Output configuration to file.
  2423. fd = open("/mnt/EvseTempConfig.bin", O_RDWR | O_CREAT | O_TRUNC);
  2424. if(fd < 0)
  2425. {
  2426. LOG_ERROR("open /mnt/EvseTempConfig.bin NG");
  2427. free(BufTmp);
  2428. return 0;
  2429. }
  2430. wrd = write(fd, BufTmp, MtdBlockSize);
  2431. close(fd);
  2432. if(wrd < MtdBlockSize)
  2433. {
  2434. LOG_ERROR("write /mnt/EvseTempConfig.bin NG");
  2435. free(BufTmp);
  2436. return 0;
  2437. }
  2438. LOG_INFO("EvseTempConfig write to file in /mnt OK.");
  2439. LOG_INFO("Erase /dev/mtd10.");
  2440. runFlashShellCmd("flash_erase /dev/mtd10 0 0");
  2441. LOG_INFO("Write /dev/mtd10.");
  2442. runFlashShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseTempConfig.bin");
  2443. LOG_INFO("Erase /dev/mtd11.");
  2444. runFlashShellCmd("flash_erase /dev/mtd11 0 0");
  2445. LOG_INFO("Write /dev/mtd11.");
  2446. runFlashShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseTempConfig.bin");
  2447. system("rm -f /mnt/EvseTempConfig.bin");
  2448. LOG_INFO("EvseTempConfig write to flash OK");
  2449. }
  2450. else
  2451. {
  2452. LOG_ERROR("alloc BlockSize NG");
  2453. result = FAIL;
  2454. }
  2455. if(BufTmp != NULL)
  2456. {
  2457. free(BufTmp);
  2458. }
  2459. return result;
  2460. }
  2461. //===============================================
  2462. // Common Detect Chk - Stop Charging ?
  2463. //===============================================
  2464. bool isEvBoardStopChargeFlag(byte gunIndex)
  2465. {
  2466. return chargingInfo[gunIndex]->ChargingStopFlag.bits.AlarmStop;
  2467. }
  2468. bool isNormalStopChargeFlag(byte gunIndex)
  2469. {
  2470. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.NormalStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.ManualStop);
  2471. }
  2472. bool isResetStopChargeFlag(byte gunIndex)
  2473. {
  2474. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.HardResetStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.SoftResetStop);
  2475. }
  2476. bool isInvalidIdStopFlag(byte gunIndex)
  2477. {
  2478. return chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop;
  2479. }
  2480. bool isSessionTargetStopFlag(byte gunIndex)
  2481. {
  2482. return chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop;
  2483. }
  2484. bool isBackendStopFlag(byte gunIndex)
  2485. {
  2486. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.BackendStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop);
  2487. }
  2488. void ReleaseAlarmCode(byte gunIndex)
  2489. {
  2490. if(!Is_Ocpp_StatusNotificationErrorCode(gunIndex, "NoError"))
  2491. {
  2492. SetOcppErrorCode(gunIndex, "NoError");
  2493. }
  2494. if(!Is_Ocpp_StatusNotificationVendorCode(gunIndex, ""))
  2495. {
  2496. SetOcppVendorErrorCode(gunIndex, "");
  2497. }
  2498. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  2499. {
  2500. strcpy((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "");
  2501. }
  2502. if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  2503. {
  2504. strcpy((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "");
  2505. }
  2506. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "") != EQUAL)
  2507. {
  2508. strcpy((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "");
  2509. }
  2510. if (strcmp((char *)ShmChargerInfo->GunError[gunIndex].GunAlarmCode, "") != EQUAL)
  2511. {
  2512. strcpy((char *)ShmChargerInfo->GunError[gunIndex].GunAlarmCode, "");
  2513. }
  2514. }
  2515. //===============================================
  2516. // EmergencyStop and Charging Stop
  2517. //===============================================
  2518. void ChargingTerminalProcess(byte gunIndex)
  2519. {
  2520. setChargerMode(gunIndex, MODE_TERMINATING);
  2521. }
  2522. void ChargingAlarmProcess(byte gunIndex)
  2523. {
  2524. UpdateErrorCodeToOcpp(gunIndex);
  2525. setChargerMode(gunIndex, MODE_ALARM);
  2526. }
  2527. void AcChargingTerminalProcess()
  2528. {
  2529. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2530. }
  2531. void StopChargingProcessByString(byte level)
  2532. {
  2533. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2534. {
  2535. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2536. }
  2537. }
  2538. void ReleaseChargingProcessByString(byte level)
  2539. {
  2540. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2541. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2542. }
  2543. // 一般錯誤停止充電處理函式
  2544. void BoardErrOccurByString(byte index, char *code)
  2545. {
  2546. byte level = 1;
  2547. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2548. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2549. {
  2550. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2551. {
  2552. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2553. }
  2554. ChargingTerminalProcess(index);
  2555. }
  2556. StopChargingProcessByString(level);
  2557. }
  2558. // 急停狀況的停止充電處理函式
  2559. void EmcOccureByString(char *code)
  2560. {
  2561. byte level = 2;
  2562. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  2563. {
  2564. return;
  2565. }
  2566. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2567. // 其錯誤等級為 2
  2568. if (strncmp(code, "042251", 6) == EQUAL || strncmp(code, "042252", 6) == EQUAL ||
  2569. strncmp(code, "042238", 6) == EQUAL || strncmp(code, "042267", 6) == EQUAL ||
  2570. strncmp(code, "042328", 6) == EQUAL)
  2571. {
  2572. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2573. {
  2574. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2575. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2576. {
  2577. if (strncmp((char *)chargingInfo[gun]->ConnectorAlarmCode, "", 6) == EQUAL)
  2578. {
  2579. memcpy(chargingInfo[gun]->ConnectorAlarmCode, code, 6);
  2580. }
  2581. InformOcppErrOccur(4);
  2582. ChargingAlarmProcess(gun);
  2583. }
  2584. }
  2585. StopChargingProcessByString(level);
  2586. }
  2587. }
  2588. void ReleaseEmsOccureByString(byte index, char *code)
  2589. {
  2590. bool isTrigger = false;
  2591. byte level = 2;
  2592. if (strncmp(code, "042251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2593. {
  2594. isTrigger = true;
  2595. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2596. }
  2597. else if (strncmp(code, "042252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2598. {
  2599. isTrigger = true;
  2600. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2601. }
  2602. else if (strncmp(code, "042237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2603. {
  2604. isTrigger = true;
  2605. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2606. }
  2607. else if (strncmp(code, "042238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2608. {
  2609. isTrigger = true;
  2610. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2611. }
  2612. else if (strncmp(code, "042267", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm == YES)
  2613. {
  2614. isTrigger = true;
  2615. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  2616. }
  2617. if (isTrigger)
  2618. {
  2619. ReleaseChargingProcessByString(level);
  2620. }
  2621. }
  2622. //===============================================
  2623. // 確認硬體 (按鈕) 狀態
  2624. //===============================================
  2625. void ChkPrimaryStatus()
  2626. {
  2627. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  2628. {
  2629. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2630. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2631. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2632. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2633. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2634. return;
  2635. }
  2636. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2637. {
  2638. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2639. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = YES;
  2640. EmcOccureByString("042251");
  2641. }
  2642. else
  2643. {
  2644. ReleaseEmsOccureByString(0, "042251");
  2645. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2646. }
  2647. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2648. {
  2649. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2650. //EmcOccureByString("042238");
  2651. }
  2652. else
  2653. {
  2654. //ReleaseEmsOccureByString(0, "042238");
  2655. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2656. }
  2657. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2658. {
  2659. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2660. }
  2661. else
  2662. {
  2663. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2664. }
  2665. if(ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL &&
  2666. (ShmChargerInfo->Control.CustomizedInfo.TiltSensorStep == _TILT_SENSOR_NONE ||
  2667. ShmChargerInfo->Control.CustomizedInfo.TiltSensorStep == _TILT_SENSOR_FINISH))
  2668. {
  2669. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2670. EmcOccureByString("042252");
  2671. }
  2672. else
  2673. {
  2674. ReleaseEmsOccureByString(0, "042252");
  2675. }
  2676. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2677. {
  2678. EmcOccureByString("042328");
  2679. SetAcContactor(OFF);
  2680. }
  2681. if(ShmChargerInfo->Control.CustomizedInfo.TiltSensorFail)
  2682. {
  2683. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.TiltSensorStestFail = YES;
  2684. }
  2685. else
  2686. {
  2687. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.TiltSensorStestFail = NO;
  2688. }
  2689. }
  2690. struct timespec _AcContactReOpen_Time;
  2691. void ChkPsuStatus(void)
  2692. {
  2693. if(!ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2694. {
  2695. if(IsConnectorWholeIdle())
  2696. {
  2697. for(int psuGroup = 0; psuGroup < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; psuGroup++)
  2698. {
  2699. //if(ShmPsuData->PsuGroup[psuGroup].GroupErrorFlag.PsuGroupErrorValue != 0)
  2700. //{
  2701. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2702. //}
  2703. if(ShmChargerInfo->PsuGrouping.GroupPsuStatus_OR[psuGroup].StatusVal & PSU_STATUS_FLAG_FAULT)
  2704. {
  2705. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2706. }
  2707. }
  2708. }
  2709. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2710. {
  2711. EmcOccureByString("042267");
  2712. }
  2713. }
  2714. else
  2715. {
  2716. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor &&
  2717. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume == NO)
  2718. {
  2719. bool allFaultStatus = true;
  2720. for(int gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2721. {
  2722. if(chargingInfo[gunIndex]->SystemStatus != S_FAULT && chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  2723. {
  2724. allFaultStatus = false;
  2725. }
  2726. }
  2727. if(allFaultStatus)
  2728. {
  2729. LOG_INFO("PSU Failure, Need Resume");
  2730. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = YES;
  2731. }
  2732. }
  2733. }
  2734. }
  2735. int AcContactorReOpenCount = 0;
  2736. void PsuFailureResume(void)
  2737. {
  2738. if(ShmChargerInfo->Control.PsuCtrl.bits.FailureResume)
  2739. {
  2740. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO)
  2741. {
  2742. LOG_INFO("Ac Contactor Off By Psu Failure");
  2743. // update _AcContactReOpen_Time
  2744. GetClockTime(&_AcContactReOpen_Time);
  2745. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = YES;
  2746. AcContactorReOpenCount++;
  2747. }
  2748. else
  2749. {
  2750. if(AcContactorReOpenCount < MAX_AC_CONTACTOR_COUNT)
  2751. {
  2752. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= AC_CONTACTOR_REOPEN_TIME)
  2753. {
  2754. ReleaseEmsOccureByString(0, "042267");
  2755. // update _AcContactReOpen_Time
  2756. GetClockTime(&_AcContactReOpen_Time);
  2757. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = NO;
  2758. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = NO;
  2759. LOG_INFO("Psu Failure Resume");
  2760. }
  2761. }
  2762. }
  2763. }
  2764. else
  2765. {
  2766. if(AcContactorReOpenCount > 0)
  2767. {
  2768. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= PSU_FAILURE_RESUME_TIME)
  2769. {
  2770. AcContactorReOpenCount = 0;
  2771. }
  2772. }
  2773. }
  2774. }
  2775. void SetAcContactor(unsigned char OnOff)
  2776. {
  2777. OnOff = OnOff ? ON : OFF;
  2778. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor != OnOff)
  2779. {
  2780. if(OnOff)
  2781. {
  2782. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2783. }
  2784. LOG_INFO("Set Ac Contactor %s", OnOff == ON ? "On" : "Off");
  2785. }
  2786. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = OnOff == ON ? ON : OFF;
  2787. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER && ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  2788. {
  2789. SetParallelCabinetAcContactor(OnOff);
  2790. }
  2791. }
  2792. int GetStandbyTime(void)
  2793. {
  2794. int time = STANDBY_TIME;
  2795. if(ShmChargerInfo->Control.TestCtrl.bits.FastStandbyTime)
  2796. {
  2797. time = FAST_STANDBY_TIME;
  2798. }
  2799. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.CustomizedStandbyTime)
  2800. {
  2801. time = ShmChargerInfo->Control.CustomizedInfo.StandbyTime;
  2802. }
  2803. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyImmediately)
  2804. {
  2805. time = FAST_STANDBY_TIME;
  2806. }
  2807. return time;
  2808. }
  2809. void StandbyCheck(void)
  2810. {
  2811. bool enable = IsStandbyEnable();
  2812. if(IsConnectorWholeIdle() && IsParallelCabinetIdle())
  2813. {
  2814. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2815. {
  2816. int time = GetStandbyTime();
  2817. #if CONTACTOR_AUTO_ON_OFF == 1
  2818. time = CONTACTOR_AUTO_ON_TIME;
  2819. #endif
  2820. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor)
  2821. {
  2822. if(GetSecTimeoutValue(_Standby_time) >= time && enable)
  2823. {
  2824. SetAcContactor(OFF);
  2825. }
  2826. }
  2827. #if CONTACTOR_AUTO_ON_OFF == 1
  2828. time = CONTACTOR_AUTO_ON_TIME + CONTACTOR_AUTO_OFF_TIME;
  2829. if((GetTimeoutValue(_Standby_time) / uSEC_VAL) >= time)
  2830. {
  2831. SetAcContactor(ON);
  2832. }
  2833. #endif
  2834. }
  2835. else
  2836. {
  2837. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = YES;
  2838. GetClockTime(&_Standby_time);
  2839. }
  2840. }
  2841. else
  2842. {
  2843. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2844. {
  2845. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2846. }
  2847. }
  2848. }
  2849. void CheckDisconnectionStatusRecovery(void)
  2850. {
  2851. bool recovery = true;
  2852. // check all connector
  2853. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2854. {
  2855. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection)
  2856. {
  2857. recovery = false;
  2858. }
  2859. }
  2860. if(recovery)
  2861. {
  2862. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = false;
  2863. }
  2864. }
  2865. //===============================================
  2866. // 確認各小板偵測的錯誤狀況
  2867. //===============================================
  2868. void CheckErrorOccurStatus(byte index)
  2869. {
  2870. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.NormalStopRequest)
  2871. {
  2872. chargingInfo[index]->ChargingStopFlag.bits.NormalStop = true;
  2873. }
  2874. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AlarmStopRequest)
  2875. {
  2876. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2877. {
  2878. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemotenAlarmCode, 6);
  2879. }
  2880. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2881. {
  2882. LOG_INFO("Connector %d Error Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2883. }
  2884. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2885. }
  2886. if(ShmChargerInfo->GunError[index].ErrFlag.ErrorValue != 0)
  2887. {
  2888. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2889. {
  2890. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmChargerInfo->GunError[index].GunAlarmCode, 6);
  2891. }
  2892. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2893. {
  2894. LOG_INFO("Connector %d GunError Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2895. }
  2896. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2897. }
  2898. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest)
  2899. {
  2900. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest = false;
  2901. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.Disconnection = true;
  2902. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = true;
  2903. if(chargingInfo[index]->SystemStatus == S_AUTHORIZING ||
  2904. chargingInfo[index]->SystemStatus == S_PREPARNING ||
  2905. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EV ||
  2906. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EVSE ||
  2907. chargingInfo[index]->SystemStatus == S_CHARGING)
  2908. {
  2909. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2910. {
  2911. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042304", 6);
  2912. }
  2913. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2914. {
  2915. LOG_INFO("Connector %d Disconnect Alarm Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2916. }
  2917. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2918. }
  2919. else if(chargingInfo[index]->SystemStatus == S_REASSIGN_CHECK ||
  2920. chargingInfo[index]->SystemStatus == S_REASSIGN)
  2921. {
  2922. setChargerMode(index, MODE_IDLE);
  2923. }
  2924. }
  2925. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo)
  2926. {
  2927. CheckDisconnectionStatusRecovery();
  2928. }
  2929. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES ||
  2930. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2931. {
  2932. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2933. {
  2934. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_PRIMARY;
  2935. StopChargingProcessByString(2);
  2936. }
  2937. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2938. {
  2939. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2940. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042251", 6);
  2941. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2942. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042252", 6);
  2943. }
  2944. }
  2945. else
  2946. {
  2947. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_PRIMARY)
  2948. {
  2949. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2950. ReleaseChargingProcessByString(2);
  2951. }
  2952. }
  2953. // RB
  2954. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2955. {
  2956. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2957. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2958. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2959. {
  2960. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2961. {
  2962. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2963. StopChargingProcessByString(2);
  2964. }
  2965. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2966. {
  2967. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2968. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042203", 6);
  2969. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2970. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042204", 6);
  2971. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2972. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042205", 6);
  2973. }
  2974. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2975. {
  2976. LOG_INFO("Connector %d RC Board Input UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2977. }
  2978. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2979. }
  2980. else
  2981. {
  2982. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2983. {
  2984. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2985. ReleaseChargingProcessByString(2);
  2986. }
  2987. }
  2988. }
  2989. else
  2990. {
  2991. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2992. {
  2993. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2994. ReleaseChargingProcessByString(2);
  2995. }
  2996. }
  2997. if(ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable == NO)
  2998. {
  2999. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  3000. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  3001. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  3002. {
  3003. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  3004. {
  3005. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  3006. StopChargingProcessByString(2);
  3007. }
  3008. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  3009. {
  3010. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  3011. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042200", 6);
  3012. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  3013. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042201", 6);
  3014. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  3015. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042202", 6);
  3016. }
  3017. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  3018. {
  3019. LOG_INFO("Connector %d RC Board Input OVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  3020. }
  3021. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  3022. }
  3023. else
  3024. {
  3025. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  3026. {
  3027. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3028. ReleaseChargingProcessByString(2);
  3029. }
  3030. }
  3031. }
  3032. else
  3033. {
  3034. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES ||
  3035. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  3036. {
  3037. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  3038. {
  3039. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  3040. StopChargingProcessByString(2);
  3041. }
  3042. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  3043. {
  3044. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES)
  3045. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042327", 6);
  3046. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  3047. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042328", 6);
  3048. }
  3049. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  3050. {
  3051. LOG_INFO("Gun %d DC Input OVP/UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  3052. }
  3053. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  3054. }
  3055. else
  3056. {
  3057. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  3058. {
  3059. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  3060. ReleaseChargingProcessByString(2);
  3061. }
  3062. }
  3063. }
  3064. }
  3065. int DiffTimebWithNowSec(struct timeb ST)
  3066. {
  3067. //return milli-second
  3068. struct timeb ET;
  3069. unsigned int StartTime,StopTime;
  3070. ftime(&ET);
  3071. StartTime=(unsigned int)ST.time;
  3072. StopTime=(unsigned int)ET.time;
  3073. return (StopTime-StartTime);
  3074. }
  3075. int CheckTimeOut(char *start)
  3076. {
  3077. int result = YES;
  3078. struct ParsingResult
  3079. {
  3080. int result;
  3081. int scanedElement;
  3082. int year;
  3083. int month;
  3084. int mday;
  3085. int hour;
  3086. int min;
  3087. int sec;
  3088. int tz_hour;
  3089. int tz_min;
  3090. float minSec;
  3091. }parsingResult;
  3092. struct tm tmStart;
  3093. struct timeb tbStart;
  3094. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  3095. if(strstr((char*)start, ".") != NULL)
  3096. {
  3097. // Original data with mini second
  3098. if(strstr((char*)start, "Z") != NULL)
  3099. {
  3100. // Original data with Z
  3101. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  3102. &parsingResult.year,
  3103. &parsingResult.month,
  3104. &parsingResult.mday,
  3105. &parsingResult.hour,
  3106. &parsingResult.min,
  3107. &parsingResult.sec,
  3108. &parsingResult.minSec);
  3109. }
  3110. else
  3111. {
  3112. // Original data without Z
  3113. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  3114. &parsingResult.year,
  3115. &parsingResult.month,
  3116. &parsingResult.mday,
  3117. &parsingResult.hour,
  3118. &parsingResult.min,
  3119. &parsingResult.sec,
  3120. &parsingResult.minSec,
  3121. &parsingResult.tz_hour,
  3122. &parsingResult.tz_min);
  3123. }
  3124. }
  3125. else
  3126. {
  3127. // Original data without mini second
  3128. if(strstr((char*)start, "Z") != NULL)
  3129. {
  3130. // Original data with Z
  3131. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  3132. &parsingResult.year,
  3133. &parsingResult.month,
  3134. &parsingResult.mday,
  3135. &parsingResult.hour,
  3136. &parsingResult.min,
  3137. &parsingResult.sec);
  3138. }
  3139. else
  3140. {
  3141. // Original data without Z
  3142. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  3143. &parsingResult.year,
  3144. &parsingResult.month,
  3145. &parsingResult.mday,
  3146. &parsingResult.hour,
  3147. &parsingResult.min,
  3148. &parsingResult.sec,
  3149. &parsingResult.tz_hour,
  3150. &parsingResult.tz_min);
  3151. }
  3152. }
  3153. if(parsingResult.scanedElement >= 6)
  3154. {
  3155. tmStart.tm_year = parsingResult.year - 1900;
  3156. tmStart.tm_mon = parsingResult.month - 1;
  3157. tmStart.tm_mday = parsingResult.mday;
  3158. tmStart.tm_hour = parsingResult.hour;
  3159. tmStart.tm_min = parsingResult.min;
  3160. tmStart.tm_sec = parsingResult.sec;
  3161. tmStart.tm_gmtoff = 0;
  3162. tbStart.time = mktime(&tmStart);
  3163. tbStart.timezone = 0;
  3164. tbStart.millitm = 0;
  3165. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  3166. if(DiffTimebWithNowSec(tbStart) <= 0)
  3167. result = NO;
  3168. else
  3169. result = YES;
  3170. }
  3171. else
  3172. {
  3173. LOG_INFO("Start date parsing error");
  3174. }
  3175. return result;
  3176. }
  3177. bool Is_WebServer_Alive(void)
  3178. {
  3179. if(system("pidof -s lighttpd > /dev/null") != 0)
  3180. {
  3181. return false;
  3182. }
  3183. return true;
  3184. }
  3185. void Run_WebServer_Service(void)
  3186. {
  3187. system("/root/web.sh");
  3188. }
  3189. bool Is_LocalPowerSharing_Alive(void)
  3190. {
  3191. if(system("pidof -s Module_PowerSharing > /dev/null") != 0)
  3192. {
  3193. return false;
  3194. }
  3195. return true;
  3196. }
  3197. void Run_LocalPowerSharing_Service(void)
  3198. {
  3199. system ("/root/Module_PowerSharing &");
  3200. }
  3201. //===============================================
  3202. // Ocpp_Service
  3203. //===============================================
  3204. void Run_Ocpp_Service(void)
  3205. {
  3206. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3207. {
  3208. system("/root/OcppBackend &");
  3209. return;
  3210. }
  3211. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3212. {
  3213. system("/root/OcppBackend20 &");
  3214. return;
  3215. }
  3216. }
  3217. bool Is_Ocpp_Task_Alive(void)
  3218. {
  3219. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3220. {
  3221. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3222. {
  3223. return false;
  3224. }
  3225. return true;
  3226. }
  3227. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3228. {
  3229. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  3230. {
  3231. return false;
  3232. }
  3233. return true;
  3234. }
  3235. return false;
  3236. }
  3237. void Kill_Ocpp_Service(void)
  3238. {
  3239. system("killall OcppBackend");
  3240. system("killall OcppBackend20");
  3241. }
  3242. //===============================================
  3243. // Maintain Ocpp_Service
  3244. //===============================================
  3245. void Run_MaintainOcpp_Service(void)
  3246. {
  3247. system("/root/OcppBackendPH &");
  3248. }
  3249. bool Is_MaintainOcpp_Task_Alive(void)
  3250. {
  3251. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  3252. {
  3253. return false;
  3254. }
  3255. return true;
  3256. }
  3257. void Kill_MaintainOcpp_Service(void)
  3258. {
  3259. system("killall OcppBackendPH");
  3260. }
  3261. //===============================================
  3262. // Ocpp OcppConnStatus
  3263. //===============================================
  3264. bool Get_Ocpp_ConnectionStatus(void)
  3265. {
  3266. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3267. {
  3268. return ShmOCPP16Data->OcppConnStatus > 0 ? true : false;
  3269. }
  3270. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3271. {
  3272. return ShmOCPP20Data->OcppConnStatus > 0 ? true : false;
  3273. }
  3274. return false;
  3275. }
  3276. //===============================================
  3277. // Ocpp procDogTime
  3278. //===============================================
  3279. long int Get_Ocpp_procDogTime(void)
  3280. {
  3281. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3282. {
  3283. return ShmOCPP16Data->procDogTime;
  3284. }
  3285. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3286. {
  3287. return ShmOCPP20Data->procDogTime;
  3288. }
  3289. return 0;
  3290. }
  3291. void Clean_Ocpp_procDogTime(void)
  3292. {
  3293. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3294. {
  3295. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  3296. return;
  3297. }
  3298. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3299. {
  3300. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  3301. return;
  3302. }
  3303. }
  3304. //===============================================
  3305. // Ocpp Boot Notification
  3306. //===============================================
  3307. bool Is_Ocpp_BootNotification(void)
  3308. {
  3309. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3310. {
  3311. return ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  3312. }
  3313. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3314. {
  3315. return ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  3316. }
  3317. return false;
  3318. }
  3319. void Clean_Ocpp_BootNotification(void)
  3320. {
  3321. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3322. {
  3323. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = false;
  3324. return;
  3325. }
  3326. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3327. {
  3328. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = false;
  3329. return;
  3330. }
  3331. }
  3332. //===============================================
  3333. // Ocpp Rest
  3334. //===============================================
  3335. bool Is_Ocpp_ResetReq(void)
  3336. {
  3337. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3338. {
  3339. return ShmOCPP16Data->MsMsg.bits.ResetReq == YES ? true : false;
  3340. }
  3341. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3342. {
  3343. return ShmOCPP20Data->MsMsg.bits.ResetReq == YES ? true : false;
  3344. }
  3345. return false;
  3346. }
  3347. bool Chk_Ocpp_Reset_Type(char *type)
  3348. {
  3349. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3350. {
  3351. return strcmp((char *)ShmOCPP16Data->Reset.Type, type) == EQUAL ? true : false;
  3352. }
  3353. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3354. {
  3355. return strcmp((char *)ShmOCPP20Data->Reset.type, type) == EQUAL ? true : false;
  3356. }
  3357. return false;
  3358. }
  3359. void Clean_Ocpp_ResetReq(void)
  3360. {
  3361. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3362. {
  3363. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3364. ShmOCPP16Data->MsMsg.bits.ResetReq = false;
  3365. ShmOCPP16Data->MsMsg.bits.ResetConf = true;
  3366. return;
  3367. }
  3368. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3369. {
  3370. sprintf ( (char*) ShmOCPP20Data->Reset.Response_status, "Accepted" );
  3371. ShmOCPP20Data->MsMsg.bits.ResetReq = false;
  3372. ShmOCPP20Data->MsMsg.bits.ResetConf = true;
  3373. return;
  3374. }
  3375. }
  3376. //===============================================
  3377. // Ocpp AuthorizeReq
  3378. //===============================================
  3379. void Set_Ocpp_AuthorizeReq(void)
  3380. {
  3381. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3382. {
  3383. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  3384. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  3385. return;
  3386. }
  3387. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3388. {
  3389. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = false;
  3390. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = true;
  3391. return;
  3392. }
  3393. }
  3394. bool Is_Ocpp_AuthorizeConf(void)
  3395. {
  3396. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3397. {
  3398. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3399. }
  3400. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3401. {
  3402. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3403. }
  3404. return false;
  3405. }
  3406. bool Is_Ocpp_Authorize_Status(char *status)
  3407. {
  3408. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3409. {
  3410. return strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  3411. }
  3412. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3413. {
  3414. return strcmp((char *)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, status) == EQUAL ? true : false;
  3415. }
  3416. return false;
  3417. }
  3418. //===============================================
  3419. // Ocpp UpdateFirmwareReq
  3420. //===============================================
  3421. bool Is_Ocpp_UpdateFirmwareReq(void)
  3422. {
  3423. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3424. {
  3425. return ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3426. }
  3427. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3428. {
  3429. return ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3430. }
  3431. return false;
  3432. }
  3433. void Clean_Ocpp_UpdateFirmwareReq(void)
  3434. {
  3435. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3436. {
  3437. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = false;
  3438. return;
  3439. }
  3440. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3441. {
  3442. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = false;
  3443. return;
  3444. }
  3445. }
  3446. bool Is_Ocpp_UpdateFirmwareStatus(char *status)
  3447. {
  3448. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3449. {
  3450. return strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status) == EQUAL ? true : false;
  3451. }
  3452. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3453. {
  3454. return strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status) == EQUAL ? true : false;
  3455. }
  3456. return false;
  3457. }
  3458. void Set_Ocpp_UpdateFirmwareStatus(char *status)
  3459. {
  3460. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3461. {
  3462. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status);
  3463. return;
  3464. }
  3465. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3466. {
  3467. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status);
  3468. return;
  3469. }
  3470. }
  3471. void Clean_Ocpp_UpdateFirmwareStatus(char *status)
  3472. {
  3473. Set_Ocpp_UpdateFirmwareStatus("");
  3474. }
  3475. void Set_Ocpp_FirmwareStatusNotificationReq(void)
  3476. {
  3477. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3478. {
  3479. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3480. return;
  3481. }
  3482. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3483. {
  3484. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3485. return;
  3486. }
  3487. }
  3488. //===============================================
  3489. // Ocpp StopTransactionOnInvalidId
  3490. //===============================================
  3491. bool Is_Ocpp_StopOnInvalidId_Enable(void)
  3492. {
  3493. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3494. {
  3495. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE") > 0 ? true : false;
  3496. }
  3497. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3498. {
  3499. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3500. }
  3501. return false;
  3502. }
  3503. //===============================================
  3504. // Ocpp AuthorizeRemoteTxRequests
  3505. //===============================================
  3506. bool Is_Ocpp_AuthorizeRemoteTxRequests(void)
  3507. {
  3508. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3509. {
  3510. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE") > 0 ? true : false;
  3511. }
  3512. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3513. {
  3514. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3515. }
  3516. return false;
  3517. }
  3518. //===============================================
  3519. // Ocpp ConnectionTimeOut
  3520. //===============================================
  3521. int Get_Ocpp_ConnectionTimeOut(void)
  3522. {
  3523. int timeout = 0;
  3524. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3525. {
  3526. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData, "") != 0)
  3527. {
  3528. timeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3529. }
  3530. }
  3531. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3532. {
  3533. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value, "" ) != 0)
  3534. {
  3535. timeout = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  3536. }
  3537. }
  3538. return timeout > 0 ? timeout : 0;
  3539. }
  3540. //===============================================
  3541. // Ocpp DefaultPrice
  3542. //===============================================
  3543. void Get_Ocpp_DefaultPrice(char *defaultPrice)
  3544. {
  3545. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3546. {
  3547. strcpy(defaultPrice, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  3548. return;
  3549. }
  3550. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3551. {
  3552. strcpy(defaultPrice, "");
  3553. return;
  3554. }
  3555. strcpy(defaultPrice, "");
  3556. }
  3557. //===============================================
  3558. // Ocpp UserPrice
  3559. //===============================================
  3560. void Get_Ocpp_UserPrice(char *userId, char *userPrice)
  3561. {
  3562. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3563. {
  3564. strcpy(userId, (char *)ShmOCPP16Data->Cost.SetUserPrice.idToken);
  3565. strcpy(userPrice, (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  3566. return;
  3567. }
  3568. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3569. {
  3570. strcpy(userId, "");
  3571. strcpy(userPrice, "");
  3572. return;
  3573. }
  3574. strcpy(userId, "");
  3575. strcpy(userPrice, "");
  3576. }
  3577. void Clean_Ocpp_UserPrice(void)
  3578. {
  3579. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3580. {
  3581. memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  3582. return;
  3583. }
  3584. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3585. {
  3586. return;
  3587. }
  3588. }
  3589. //===============================================
  3590. // Ocpp RunningCost
  3591. //===============================================
  3592. void Get_Ocpp_RunningCost(int gun_index, char *cost)
  3593. {
  3594. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3595. {
  3596. strcpy(cost, (char *)ShmOCPP16Data->Cost.RunningCost[gun_index].description);
  3597. return;
  3598. }
  3599. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3600. {
  3601. strcpy(cost, (char *)"");
  3602. return;
  3603. }
  3604. }
  3605. void Clean_Ocpp_RunningCost(int gun_index)
  3606. {
  3607. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3608. {
  3609. memset(&ShmOCPP16Data->Cost.RunningCost[gun_index], 0x00, sizeof(struct StrcutRunningCost));
  3610. return;
  3611. }
  3612. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3613. {
  3614. return;
  3615. }
  3616. }
  3617. //===============================================
  3618. // Ocpp FinalCost
  3619. //===============================================
  3620. void Get_Ocpp_FinalCost(int gun_index, char *cost)
  3621. {
  3622. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3623. {
  3624. strcpy(cost, (char *)ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  3625. return;
  3626. }
  3627. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3628. {
  3629. strcpy(cost, (char *)"");
  3630. return;
  3631. }
  3632. }
  3633. void Clean_Ocpp_FinalCost(int gun_index)
  3634. {
  3635. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3636. {
  3637. memset(&ShmOCPP16Data->Cost.FinalCost[gun_index], 0x00, sizeof(struct StrcutFinalCost));
  3638. return;
  3639. }
  3640. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3641. {
  3642. return;
  3643. }
  3644. }
  3645. int Get_Ocpp_FinalCostTxId(int gun_index)
  3646. {
  3647. int txId = 0;
  3648. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3649. {
  3650. txId = ShmOCPP16Data->Cost.FinalCost[gun_index].txId;
  3651. }
  3652. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3653. {
  3654. }
  3655. return txId;
  3656. }
  3657. //===============================================
  3658. // Ocpp TimeOffset
  3659. //===============================================
  3660. void Get_Ocpp_TimeOffset(char *timeOffset)
  3661. {
  3662. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3663. {
  3664. strcpy(timeOffset, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[TimeOffset].ItemData);
  3665. return;
  3666. }
  3667. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3668. {
  3669. strcpy(timeOffset, "");
  3670. return;
  3671. }
  3672. strcpy(timeOffset, "");
  3673. }
  3674. //===============================================
  3675. // Ocpp PreAuthAmount
  3676. //===============================================
  3677. void Get_Ocpp_PreAuthAmount(char *amount)
  3678. {
  3679. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3680. {
  3681. strcpy(amount, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PreAuthAmount].ItemData);
  3682. return;
  3683. }
  3684. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3685. {
  3686. strcpy(amount, "");
  3687. return;
  3688. }
  3689. strcpy(amount, "");
  3690. }
  3691. //===============================================
  3692. // Ocpp PowerSharingServerIP
  3693. //===============================================
  3694. void Get_Ocpp_PowerSharingServerIP(char *ip)
  3695. {
  3696. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3697. {
  3698. strcpy(ip, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PowerSharingServerIP].ItemData);
  3699. return;
  3700. }
  3701. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3702. {
  3703. strcpy(ip, "");
  3704. return;
  3705. }
  3706. strcpy(ip, "");
  3707. }
  3708. //===============================================
  3709. // Ocpp Refunding
  3710. //===============================================
  3711. void Get_Ocpp_Refunding(char *Refunding)
  3712. {
  3713. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3714. {
  3715. return;
  3716. }
  3717. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3718. {
  3719. return;
  3720. }
  3721. }
  3722. void Clean_Ocpp_Refunding(void)
  3723. {
  3724. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3725. {
  3726. return;
  3727. }
  3728. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3729. {
  3730. return;
  3731. }
  3732. }
  3733. //===============================================
  3734. // Ocpp LcdOveride
  3735. //===============================================
  3736. bool Is_Ocpp_LcdOverideReq(int *gun, int *page)
  3737. {
  3738. *gun = ShmSysConfigAndInfo->SysInfo.LcdOveride.connectorId;
  3739. *page = ShmSysConfigAndInfo->SysInfo.LcdOveride.page_index;
  3740. return ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq ? true : false;
  3741. }
  3742. void Clean_Ocpp_LcdOverideReq(void)
  3743. {
  3744. ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq = false;
  3745. }
  3746. //===============================================
  3747. // Ocpp StationInfo
  3748. //===============================================
  3749. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name)
  3750. {
  3751. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3752. {
  3753. strcpy(name, (char *)ShmOCPP16Data->TcciCustomData.ChargerInfo.station_name);
  3754. }
  3755. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3756. {
  3757. }
  3758. }
  3759. bool Is_Ocpp_ChargerStationReq(void)
  3760. {
  3761. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3762. {
  3763. return ShmOCPP16Data->TcciCustomData.ChargerInfoReq ? true : false;
  3764. }
  3765. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3766. {
  3767. return false;
  3768. }
  3769. return false;
  3770. }
  3771. void Clean_Ocpp_ChargerStationReq(void)
  3772. {
  3773. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3774. {
  3775. ShmOCPP16Data->TcciCustomData.ChargerInfoReq = false;
  3776. }
  3777. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3778. {
  3779. }
  3780. }
  3781. //===============================================
  3782. // Ocpp WeatherInfo
  3783. //===============================================
  3784. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature)
  3785. {
  3786. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3787. {
  3788. *weatherID = ShmOCPP16Data->TcciCustomData.WeatherInfo.weatherId;
  3789. *temperature = ShmOCPP16Data->TcciCustomData.WeatherInfo.temperature;
  3790. }
  3791. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3792. {
  3793. }
  3794. }
  3795. bool Is_Ocpp_WeatherReq(void)
  3796. {
  3797. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3798. {
  3799. return ShmOCPP16Data->TcciCustomData.WeatherInfoReq ? true : false;
  3800. }
  3801. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3802. {
  3803. return false;
  3804. }
  3805. return false;
  3806. }
  3807. void Clean_Ocpp_WeatherReq(void)
  3808. {
  3809. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3810. {
  3811. ShmOCPP16Data->TcciCustomData.WeatherInfoReq = false;
  3812. }
  3813. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3814. {
  3815. }
  3816. }
  3817. //===============================================
  3818. // Ocpp DeductInfo
  3819. //===============================================
  3820. void Set_Ocpp_DeductTransaction(int transaction)
  3821. {
  3822. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3823. {
  3824. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = transaction;
  3825. }
  3826. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3827. {
  3828. }
  3829. }
  3830. void Set_Ocpp_DeductCreditNo(char *userId)
  3831. {
  3832. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3833. {
  3834. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, userId, strlen(userId));
  3835. }
  3836. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3837. {
  3838. }
  3839. }
  3840. void Set_Ocpp_DeductVemData(char *vemData)
  3841. {
  3842. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3843. {
  3844. strcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, vemData);
  3845. }
  3846. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3847. {
  3848. }
  3849. }
  3850. void Set_Ocpp_DeductAmount(double value)
  3851. {
  3852. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3853. {
  3854. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = value;
  3855. }
  3856. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3857. {
  3858. }
  3859. }
  3860. void Set_Ocpp_DeductResult(unsigned char result)
  3861. {
  3862. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3863. {
  3864. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = result;
  3865. }
  3866. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3867. {
  3868. }
  3869. }
  3870. void Set_Ocpp_DonateInvoice(unsigned char donate)
  3871. {
  3872. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3873. {
  3874. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = donate ? YES : NO;
  3875. }
  3876. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3877. {
  3878. }
  3879. }
  3880. bool Set_Ocpp_DeductInfo(DeductResultInfoData *deductInfo)
  3881. {
  3882. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3883. {
  3884. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = deductInfo->TransactionId;
  3885. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, (char *)deductInfo->creditNo, sizeof(deductInfo->creditNo));
  3886. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, deductInfo->VemData, sizeof(deductInfo->VemData));
  3887. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = ((double)deductInfo->DeductAmount / 100);
  3888. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.approvalNo, deductInfo->ApprovalNumber, sizeof(deductInfo->ApprovalNumber));
  3889. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = deductInfo->DeductResult;
  3890. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = deductInfo->IsDonateInvoice ? YES : NO;
  3891. return true;
  3892. }
  3893. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3894. {
  3895. }
  3896. return false;
  3897. }
  3898. bool Is_Ocpp_ReportCreditDeductReq(void)
  3899. {
  3900. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3901. {
  3902. return ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq ? true : false;
  3903. }
  3904. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3905. {
  3906. return false;
  3907. }
  3908. return false;
  3909. }
  3910. void Set_Ocpp_ReportCreditDeductReq(void)
  3911. {
  3912. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3913. {
  3914. ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq = YES;
  3915. }
  3916. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3917. {
  3918. }
  3919. }
  3920. //===============================================
  3921. // Ocpp Tcci SerialNo
  3922. //===============================================
  3923. void Clean_Ocpp_TcciSerialNo(int gun_index)
  3924. {
  3925. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3926. {
  3927. memset((char *)&ShmOCPP16Data->TcciCustomData.SerialNo[gun_index], 0x00, sizeof(37));
  3928. }
  3929. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3930. {
  3931. }
  3932. }
  3933. //===============================================
  3934. // Ocpp Tcci ReaderStatus
  3935. //===============================================
  3936. bool Set_Ocpp_TcciReaderStatus(int gun_index, DeductResultInfoData *ReaderStatus)
  3937. {
  3938. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3939. {
  3940. if(ReaderStatus->DeductResult == _Deduct_PreAuth_Cancel || ReaderStatus->DeductResult == _Deduct_PreAuth || ReaderStatus->DeductResult == _Deduct_PreAuth_Pass)
  3941. {
  3942. switch(ReaderStatus->DeductResult)
  3943. {
  3944. case _Deduct_PreAuth_Pass:
  3945. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 1;
  3946. break;
  3947. case _Deduct_PreAuth_Cancel:
  3948. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 2;
  3949. break;
  3950. case _Deduct_PreAuth:
  3951. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 0;
  3952. break;
  3953. }
  3954. memcpy((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, ReaderStatus->creditNo, sizeof(ReaderStatus->creditNo));
  3955. memcpy((char *)&ShmOCPP16Data->TcciCustomData.VEMData[gun_index], ReaderStatus->VemData, sizeof(ReaderStatus->VemData));
  3956. return true;
  3957. }
  3958. }
  3959. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3960. {
  3961. }
  3962. return false;
  3963. }
  3964. void Clean_Ocpp_TcciCreditInfo(int gun_index)
  3965. {
  3966. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3967. {
  3968. memset((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo));
  3969. memset((char *)&ShmOCPP16Data->TcciCustomData.VEMData[gun_index], 0x00, sizeof(ShmOCPP16Data->TcciCustomData.VEMData[gun_index]));
  3970. }
  3971. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3972. {
  3973. }
  3974. }
  3975. void Set_Ocpp_TcciReaderStatusReq(int gun_index)
  3976. {
  3977. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3978. {
  3979. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq = true;
  3980. }
  3981. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3982. {
  3983. }
  3984. }
  3985. bool Is_Ocpp_TcciReaderStatusReq(int gun_index)
  3986. {
  3987. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3988. {
  3989. return ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq ? true : false;
  3990. }
  3991. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3992. {
  3993. return false;
  3994. }
  3995. return false;
  3996. }
  3997. //===============================================
  3998. // Ocpp Tcci OccupancyFeeDisplay
  3999. //===============================================
  4000. bool Is_Ocpp_TcciOccupancyDisplayReq(int gun_index)
  4001. {
  4002. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4003. {
  4004. return ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].OccupancyFeeDisplayReq ? true : false;
  4005. }
  4006. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4007. {
  4008. return false;
  4009. }
  4010. return false;
  4011. }
  4012. void Clean_Ocpp_TcciOccupancyDisplayReq(int gun_index)
  4013. {
  4014. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4015. {
  4016. ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].OccupancyFeeDisplayReq = false;
  4017. }
  4018. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4019. {
  4020. }
  4021. }
  4022. // input: gun_index, sn_len
  4023. // output: sn
  4024. // return: action
  4025. int Get_Ocpp_TcciOccupancyAction(int gun_index, int sn_len, char *sn)
  4026. {
  4027. int action = 0;
  4028. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4029. {
  4030. action = ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].actionId;
  4031. if(action == _ParkingAction_DisplayButton)
  4032. {
  4033. memcpy(sn, ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN, sn_len);
  4034. }
  4035. }
  4036. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4037. {
  4038. }
  4039. return action;
  4040. }
  4041. void Clean_Ocpp_TcciOccupancyActionAndSn(int gun_index)
  4042. {
  4043. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4044. {
  4045. ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].actionId = 0;
  4046. memset(ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN));
  4047. }
  4048. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4049. {
  4050. }
  4051. }
  4052. //===============================================
  4053. // Ocpp Tcci SetOccupancyPrice
  4054. //===============================================
  4055. bool Is_Ocpp_TcciSetOccupancyPriceReq(void)
  4056. {
  4057. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4058. {
  4059. return ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.SetOccupancyPriceReq ? true : false;
  4060. }
  4061. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4062. {
  4063. return false;
  4064. }
  4065. return false;
  4066. }
  4067. void Clean_Ocpp_TcciSetOccupancyPriceReq(void)
  4068. {
  4069. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4070. {
  4071. ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.SetOccupancyPriceReq = false;
  4072. }
  4073. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4074. {
  4075. }
  4076. }
  4077. float Get_Ocpp_TcciOccupancyPrice(void)
  4078. {
  4079. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4080. {
  4081. return ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.price;
  4082. }
  4083. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4084. {
  4085. }
  4086. return 0;
  4087. }
  4088. //===============================================
  4089. // Ocpp Tcci GetOccupancyFee
  4090. //===============================================
  4091. bool Is_Ocpp_TcciOccupancyFeeActReq(int gun_index)
  4092. {
  4093. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4094. {
  4095. return ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq ? true : false;
  4096. }
  4097. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4098. {
  4099. return false;
  4100. }
  4101. return false;
  4102. }
  4103. void Set_Ocpp_TcciOccupancyFeeAct(int gun_index, OccupancyBillInfo *bill, int action)
  4104. {
  4105. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4106. {
  4107. memset(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN));
  4108. memcpy(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN, bill->Sn, sizeof(bill->Sn));
  4109. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].action = action;
  4110. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq = true;
  4111. }
  4112. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4113. {
  4114. }
  4115. }
  4116. void Clean_Ocpp_TcciNotifyOccupancyFeeActReq(int gun_index)
  4117. {
  4118. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4119. {
  4120. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].action = _Notify_None;
  4121. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq = false;
  4122. }
  4123. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4124. {
  4125. }
  4126. }
  4127. void Get_Ocpp_TcciGetOccupancyFeeResponse(int gun_index, OccupancyBillInfo *bill)
  4128. {
  4129. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4130. {
  4131. bill->Duration = ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_duration;
  4132. bill->Fee = ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_occupancyFee;
  4133. memcpy(bill->StartTime, ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_startTime, sizeof(bill->StartTime));
  4134. }
  4135. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4136. {
  4137. }
  4138. }
  4139. bool Is_Ocpp_TcciOccupancyFeeActStatus(int gun_index, char *status)
  4140. {
  4141. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4142. {
  4143. return strcmp((char *)ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_status, status) == EQUAL ? true : false;
  4144. }
  4145. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4146. {
  4147. }
  4148. return false;
  4149. }
  4150. //===============================================
  4151. // Ocpp Tcci ReportOccupancyDeductReq
  4152. //===============================================
  4153. bool Is_Ocpp_TcciReportOccupancyDeductReq(void)
  4154. {
  4155. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4156. {
  4157. return ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq ? true : false;
  4158. }
  4159. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4160. {
  4161. return false;
  4162. }
  4163. return false;
  4164. }
  4165. void Set_Ocpp_TcciReportOccupancyDeductReq(void)
  4166. {
  4167. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4168. {
  4169. ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq = true;
  4170. }
  4171. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4172. {
  4173. }
  4174. }
  4175. //===============================================
  4176. // Ocpp Tcci OccupancyDeductResult
  4177. //===============================================
  4178. bool Set_Ocpp_OccupancyDeductResult(OccupancyDeductInfo *deduct, bool pass_fail)
  4179. {
  4180. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4181. {
  4182. memset(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult, 0x00, sizeof(struct StructOccupancyDeductResult));
  4183. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.deductResult = pass_fail;
  4184. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.amount = deduct->Fee;
  4185. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.occupancySN[0], &deduct->Sn[0], sizeof(deduct->Sn));
  4186. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.creditNo[0], &deduct->creditNo[0], sizeof(deduct->creditNo));
  4187. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.approvalNo[0], &deduct->ApprovalNumber[0], sizeof(deduct->ApprovalNumber));
  4188. return true;
  4189. }
  4190. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4191. {
  4192. }
  4193. return false;
  4194. }
  4195. //===============================================
  4196. // Ocpp Start Transaction
  4197. //===============================================
  4198. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id)
  4199. {
  4200. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4201. {
  4202. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag, id);
  4203. }
  4204. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4205. {
  4206. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4207. }
  4208. // MaintainServer
  4209. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4210. {
  4211. strcpy((char *)ShmOCPP16DataPH->StartTransaction[gun_index].IdTag, id);
  4212. }
  4213. }
  4214. void Set_Ocpp_StartTransactionReq(int gun_index)
  4215. {
  4216. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4217. {
  4218. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = true;
  4219. }
  4220. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4221. {
  4222. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4223. }
  4224. // MaintainServer
  4225. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4226. {
  4227. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq = true;
  4228. }
  4229. }
  4230. bool Is_Ocpp_StartTransactionConf(int gun_index)
  4231. {
  4232. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4233. {
  4234. return ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == YES ? true : false;
  4235. }
  4236. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4237. {
  4238. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf == YES)
  4239. {
  4240. if(strstr((char*)ShmOCPP20Data->TransactionEvent[gun_index].eventType, "Started") != NULL)
  4241. {
  4242. return true;
  4243. }
  4244. else
  4245. {
  4246. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  4247. return false;
  4248. }
  4249. }
  4250. else
  4251. {
  4252. return false;
  4253. }
  4254. }
  4255. return false;
  4256. }
  4257. void Clean_Ocpp_StartTransactionConf(int gun_index)
  4258. {
  4259. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4260. {
  4261. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = false;
  4262. }
  4263. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4264. {
  4265. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  4266. }
  4267. // MaintainServer
  4268. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4269. {
  4270. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf = false;
  4271. }
  4272. }
  4273. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4274. {
  4275. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4276. {
  4277. return strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  4278. }
  4279. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4280. {
  4281. return strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, status) == EQUAL ? true : false;
  4282. }
  4283. return false;
  4284. }
  4285. bool Is_Ocpp_StartTransaction_Accepted(int gun_index)
  4286. {
  4287. return Is_Ocpp_StartTransaction_Status(gun_index, "Accepted");
  4288. }
  4289. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4290. {
  4291. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4292. {
  4293. strcpy(status, (char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status);
  4294. }
  4295. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4296. {
  4297. strcpy(status, (char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status);
  4298. }
  4299. }
  4300. void Get_Ocpp_TransactionId(int gun_index, char *transactionId)
  4301. {
  4302. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4303. {
  4304. sprintf(transactionId, "%d", ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId);
  4305. return;
  4306. }
  4307. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4308. {
  4309. strcpy(transactionId, (char *)&ShmOCPP20Data->TransactionEvent [gun_index].transactionInfo.transactionId[0]);
  4310. return;
  4311. }
  4312. sprintf(transactionId, "%d", 0);
  4313. }
  4314. bool Is_Invalid_StartTransactionId(int gun_index)
  4315. {
  4316. if(Is_Ocpp_StartTransaction_Status(gun_index, "Blocked") ||
  4317. Is_Ocpp_StartTransaction_Status(gun_index, "Expired") ||
  4318. Is_Ocpp_StartTransaction_Status(gun_index, "Invalid") ||
  4319. Is_Ocpp_StartTransaction_Status(gun_index, "NoCredit") ||
  4320. Is_Ocpp_StartTransaction_Status(gun_index, "NotAllowedTypeEVSE") ||
  4321. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisLocation") ||
  4322. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisTime") ||
  4323. Is_Ocpp_StartTransaction_Status(gun_index, "Unknown"))
  4324. {
  4325. return true;
  4326. }
  4327. return false;
  4328. }
  4329. //===============================================
  4330. // Ocpp Stop Transaction
  4331. //===============================================
  4332. void Set_Ocpp_StopTransactionId(int gun_index, char *id)
  4333. {
  4334. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4335. {
  4336. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].IdTag, id);
  4337. return;
  4338. }
  4339. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4340. {
  4341. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4342. return;
  4343. }
  4344. }
  4345. void Set_Ocpp_StopTransactionReq(int gun_index)
  4346. {
  4347. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4348. {
  4349. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = true;
  4350. }
  4351. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4352. {
  4353. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4354. }
  4355. // MaintainServer
  4356. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4357. {
  4358. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = true;
  4359. }
  4360. }
  4361. //===============================================
  4362. // Ocpp Check Backup Error Code
  4363. //===============================================
  4364. void Chk_Ocpp_Backup_ErrorCode(int gun_index)
  4365. {
  4366. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4367. {
  4368. if(strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, "") == EQUAL)
  4369. {
  4370. if(strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, "") != EQUAL)
  4371. {
  4372. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, (char *)_BackupStatusNotification[gun_index].ErrorCode);
  4373. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, (char *)_BackupStatusNotification[gun_index].VendorErrorCode);
  4374. }
  4375. }
  4376. }
  4377. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4378. {
  4379. }
  4380. }
  4381. //===============================================
  4382. // Ocpp Reserve Now
  4383. //===============================================
  4384. bool Is_Ocpp_ReserveNowReq(int gun_index)
  4385. {
  4386. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4387. {
  4388. return ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4389. }
  4390. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4391. {
  4392. return ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4393. }
  4394. return false;
  4395. }
  4396. void Clean_Ocpp_ReserveNowReq(int gun_index)
  4397. {
  4398. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4399. {
  4400. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4401. return;
  4402. }
  4403. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4404. {
  4405. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4406. return;
  4407. }
  4408. }
  4409. int Get_Ocpp_ReserveNowId(int gun_index)
  4410. {
  4411. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4412. {
  4413. return ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  4414. }
  4415. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4416. {
  4417. return ShmOCPP20Data->ReserveNow[gun_index].id;
  4418. }
  4419. return 0;
  4420. }
  4421. void Set_Ocpp_ReserveNowConf(int gun_index)
  4422. {
  4423. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4424. {
  4425. ShmOCPP16Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4426. return;
  4427. }
  4428. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4429. {
  4430. ShmOCPP20Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4431. return;
  4432. }
  4433. }
  4434. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date)
  4435. {
  4436. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4437. {
  4438. strcpy(date, (char *)ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate);
  4439. return;
  4440. }
  4441. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4442. {
  4443. strcpy(date, (char *)ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime);
  4444. return;
  4445. }
  4446. strcpy(date, "");
  4447. return;
  4448. }
  4449. bool Is_Ocpp_ReserveNowExpired(int gun_index)
  4450. {
  4451. bool _expired = true;
  4452. char _expiryDate[128];
  4453. Get_Ocpp_ReserveNowExpiryDate(gun_index, _expiryDate);
  4454. _expired = CheckTimeOut(_expiryDate);
  4455. return _expired;
  4456. }
  4457. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag)
  4458. {
  4459. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4460. {
  4461. strcpy(idTag, (char *)ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  4462. return;
  4463. }
  4464. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4465. {
  4466. strcpy(idTag, (char *)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken);
  4467. return;
  4468. }
  4469. strcpy(idTag, "");
  4470. return;
  4471. }
  4472. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag)
  4473. {
  4474. char ReserveIdTag[32];
  4475. Get_Ocpp_ReserveNowIdTag(gun_index, ReserveIdTag);
  4476. return strcmp(ReserveIdTag, authIdTag) == EQUAL ? true : false;
  4477. }
  4478. //===============================================
  4479. // Ocpp Cancel Reservation
  4480. //===============================================
  4481. bool Is_Ocpp_CancelReservationReq(int gun_index)
  4482. {
  4483. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4484. {
  4485. return ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4486. }
  4487. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4488. {
  4489. return ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4490. }
  4491. return false;
  4492. }
  4493. void Clean_Ocpp_CancelReservationReq(int gun_index)
  4494. {
  4495. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4496. {
  4497. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4498. return;
  4499. }
  4500. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4501. {
  4502. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4503. return;
  4504. }
  4505. }
  4506. void Set_Ocpp_CancelReservationConf(int gun_index)
  4507. {
  4508. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4509. {
  4510. ShmOCPP16Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4511. return;
  4512. }
  4513. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4514. {
  4515. ShmOCPP20Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4516. return;
  4517. }
  4518. }
  4519. //===============================================
  4520. // Ocpp Change Availability
  4521. //===============================================
  4522. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index)
  4523. {
  4524. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4525. {
  4526. return ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4527. }
  4528. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4529. {
  4530. return ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4531. }
  4532. return false;
  4533. }
  4534. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index)
  4535. {
  4536. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4537. {
  4538. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4539. return;
  4540. }
  4541. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4542. {
  4543. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4544. return;
  4545. }
  4546. }
  4547. bool Chk_Ocpp_Availability_Type(int gun_index, char *type)
  4548. {
  4549. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4550. {
  4551. return strcmp((char *)ShmOCPP16Data->ChangeAvailability[gun_index].Type, type) == EQUAL ? true : false;
  4552. }
  4553. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4554. {
  4555. return strcmp((char *)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, type) == EQUAL ? true : false;
  4556. }
  4557. return false;
  4558. }
  4559. //===============================================
  4560. // Ocpp Unlock Connector
  4561. //===============================================
  4562. bool Is_Ocpp_UnlockConnectorReq(int gun_index)
  4563. {
  4564. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4565. {
  4566. return ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4567. }
  4568. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4569. {
  4570. return ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4571. }
  4572. return false;
  4573. }
  4574. void Clean_Ocpp_UnlockConnectorReq(int gun_index)
  4575. {
  4576. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4577. {
  4578. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4579. return;
  4580. }
  4581. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4582. {
  4583. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4584. return;
  4585. }
  4586. }
  4587. void Set_Ocpp_UnlockConnectorConf(int gun_index)
  4588. {
  4589. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4590. {
  4591. strcpy((char *)ShmOCPP16Data->UnlockConnector[gun_index].ResponseStatus, "Unlocked");
  4592. ShmOCPP16Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4593. return;
  4594. }
  4595. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4596. {
  4597. strcpy((char *)ShmOCPP20Data->UnlockConnector[gun_index].Response_status, "Unlocked");
  4598. ShmOCPP20Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4599. return;
  4600. }
  4601. }
  4602. //===============================================
  4603. // Ocpp Remote Start
  4604. //===============================================
  4605. bool Is_Ocpp_RemoteStartReq(int gun_index)
  4606. {
  4607. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4608. {
  4609. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES ? true : false;
  4610. }
  4611. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4612. {
  4613. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq == YES ? true : false;
  4614. }
  4615. return false;
  4616. }
  4617. void Clean_Ocpp_RemoteStartReq(int gun_index)
  4618. {
  4619. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4620. {
  4621. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = false;
  4622. return;
  4623. }
  4624. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4625. {
  4626. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = false;
  4627. return;
  4628. }
  4629. }
  4630. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag)
  4631. {
  4632. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4633. {
  4634. strcpy(idTag, (char *)&ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag[0]);
  4635. return;
  4636. }
  4637. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4638. {
  4639. strcpy(idTag, (char *)&ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken[0]);
  4640. return;
  4641. }
  4642. strcpy(idTag, "");
  4643. }
  4644. //===============================================
  4645. // Ocpp Remote Stop
  4646. //===============================================
  4647. bool Is_Ocpp_RemoteStopReq(int gun_index)
  4648. {
  4649. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4650. {
  4651. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == YES ? true : false;
  4652. }
  4653. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4654. {
  4655. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq == YES ? true : false;
  4656. }
  4657. return false;
  4658. }
  4659. void Clean_Ocpp_RemoteStopReq(int gun_index)
  4660. {
  4661. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4662. {
  4663. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = false;
  4664. return;
  4665. }
  4666. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4667. {
  4668. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = false;
  4669. return;
  4670. }
  4671. }
  4672. //===============================================
  4673. // Ocpp StopReason
  4674. //===============================================
  4675. void Set_Ocpp_StopReason(int gun_index, char *reason)
  4676. {
  4677. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4678. {
  4679. if(strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  4680. {
  4681. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, reason);
  4682. }
  4683. }
  4684. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4685. {
  4686. if(strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "" ) == EQUAL)
  4687. {
  4688. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, reason);
  4689. }
  4690. }
  4691. // Maintain server
  4692. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4693. {
  4694. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, ShmOCPP16Data->StopTransaction[gun_index].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  4695. }
  4696. }
  4697. void Get_Ocpp_StopReason(int gun_index, char *reason)
  4698. {
  4699. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4700. {
  4701. strcpy(reason, (char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4702. return;
  4703. }
  4704. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4705. {
  4706. strcpy(reason, (char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4707. return;
  4708. }
  4709. strcpy(reason, "");
  4710. }
  4711. void Clean_Ocpp_StopReason(int gun_index)
  4712. {
  4713. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4714. {
  4715. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  4716. }
  4717. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4718. {
  4719. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "");
  4720. }
  4721. // Maintain server
  4722. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4723. {
  4724. strcpy((char *)ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, "");
  4725. }
  4726. }
  4727. //===============================================
  4728. // Ocpp StatusNotification ErrorCode
  4729. //===============================================
  4730. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4731. {
  4732. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4733. {
  4734. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode);
  4735. return;
  4736. }
  4737. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4738. {
  4739. return;
  4740. }
  4741. }
  4742. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4743. {
  4744. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4745. {
  4746. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4747. }
  4748. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4749. {
  4750. return strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4751. }
  4752. return false;
  4753. }
  4754. //===============================================
  4755. // Ocpp StatusNotification VendorErrorCode
  4756. //===============================================
  4757. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4758. {
  4759. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4760. {
  4761. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode);
  4762. return;
  4763. }
  4764. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4765. {
  4766. return;
  4767. }
  4768. }
  4769. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4770. {
  4771. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4772. {
  4773. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4774. }
  4775. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4776. {
  4777. return strcmp((char *)_BackupStatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4778. }
  4779. return false;
  4780. }
  4781. //===============================================
  4782. // Ocpp ChargingProfileConf
  4783. //===============================================
  4784. bool Is_Ocpp_ChargingProfileConf(int gun_index)
  4785. {
  4786. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4787. {
  4788. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4789. }
  4790. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4791. {
  4792. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4793. }
  4794. return false;
  4795. }
  4796. void Clean_Ocpp_ChargingProfileConf(int gun_index)
  4797. {
  4798. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4799. {
  4800. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4801. return;
  4802. }
  4803. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4804. {
  4805. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4806. return;
  4807. }
  4808. }
  4809. //===============================================
  4810. // Ocpp ChargingProfileReq
  4811. //===============================================
  4812. bool Is_Ocpp_ChargingProfileReq(int gun_index)
  4813. {
  4814. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4815. {
  4816. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4817. }
  4818. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4819. {
  4820. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4821. }
  4822. return false;
  4823. }
  4824. void Set_Ocpp_ChargingProfileReq(int gun_index)
  4825. {
  4826. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4827. {
  4828. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4829. return;
  4830. }
  4831. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4832. {
  4833. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4834. return;
  4835. }
  4836. }
  4837. //===============================================
  4838. // Ocpp SmartChargingProfile ChargingProfileKind
  4839. //===============================================
  4840. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind)
  4841. {
  4842. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4843. {
  4844. return strcmp((char *)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, kind) == EQUAL ? true : false;
  4845. }
  4846. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4847. {
  4848. return strcmp((char *)ShmOCPP20Data->SmartChargingProfile [gun_index].chargingProfileKind, kind) == EQUAL ? true : false;
  4849. }
  4850. return false;
  4851. }
  4852. bool Is_Ocpp_ChargingProfileId(int gun_index)
  4853. {
  4854. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4855. {
  4856. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == YES ? true : false;
  4857. }
  4858. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4859. {
  4860. return ShmOCPP20Data->SmartChargingProfile[gun_index].id == YES ? true : false;
  4861. }
  4862. return false;
  4863. }
  4864. int Get_Ocpp_ChargingProfileStartTime(int gun_index)
  4865. {
  4866. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4867. {
  4868. return GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  4869. }
  4870. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4871. {
  4872. return GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  4873. }
  4874. return 0;
  4875. }
  4876. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index)
  4877. {
  4878. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4879. {
  4880. return ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);
  4881. }
  4882. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4883. {
  4884. return ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);
  4885. }
  4886. return 0;
  4887. }
  4888. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index)
  4889. {
  4890. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4891. {
  4892. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4893. }
  4894. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4895. {
  4896. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4897. }
  4898. return 0;
  4899. }
  4900. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index)
  4901. {
  4902. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4903. {
  4904. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4905. }
  4906. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4907. {
  4908. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4909. }
  4910. return 0;
  4911. }
  4912. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index)
  4913. {
  4914. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4915. {
  4916. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4917. }
  4918. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4919. {
  4920. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].numberPhases;
  4921. }
  4922. return 0;
  4923. }
  4924. int Get_Ocpp_ChargingProfileStartIndex(int gun_index)
  4925. {
  4926. int _time = 0, _scheduleTime = 0;
  4927. int _maxCount = 0;
  4928. float _limit = 0;
  4929. int _startCount = NO_DEFINE;
  4930. _time = Get_Ocpp_ChargingProfileStartTime(gun_index);
  4931. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(gun_index);
  4932. for(int i = 0; i < _maxCount; i++)
  4933. {
  4934. _scheduleTime = Get_Ocpp_ChargingProfileScheduleTime(gun_index, i);
  4935. if(_time >= _scheduleTime)
  4936. {
  4937. _limit = Get_Ocpp_ChargingProfileScheduleLimit(gun_index, i);
  4938. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4939. {
  4940. _startCount = i;
  4941. }
  4942. }
  4943. }
  4944. return _startCount;
  4945. }
  4946. //===============================================
  4947. // Ocpp MaxChargingProfile ChargingProfileKind
  4948. //===============================================
  4949. bool Is_Ocpp_MaxChargingProfileKind(char *kind)
  4950. {
  4951. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4952. {
  4953. return strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, kind) == EQUAL ? true : false;
  4954. }
  4955. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4956. {
  4957. return strcmp((char *)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, kind) == EQUAL ? true : false;
  4958. }
  4959. return false;
  4960. }
  4961. bool Is_Ocpp_MaxChargingProfileId(void)
  4962. {
  4963. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4964. {
  4965. return ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES ? true : false;
  4966. }
  4967. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4968. {
  4969. return ShmOCPP20Data->MaxChargingProfile.id == YES ? true : false;
  4970. }
  4971. return false;
  4972. }
  4973. int Get_Ocpp_MaxProfileStartTime(void)
  4974. {
  4975. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4976. {
  4977. return GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  4978. }
  4979. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4980. {
  4981. return GetStartScheduleTime(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule);
  4982. }
  4983. return 0;
  4984. }
  4985. int Get_Ocpp_MaxProfileScheduleCount(void)
  4986. {
  4987. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4988. {
  4989. return ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  4990. }
  4991. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4992. {
  4993. return ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);
  4994. }
  4995. return 0;
  4996. }
  4997. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index)
  4998. {
  4999. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5000. {
  5001. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  5002. }
  5003. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5004. {
  5005. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  5006. }
  5007. return 0;
  5008. }
  5009. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index)
  5010. {
  5011. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5012. {
  5013. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  5014. }
  5015. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5016. {
  5017. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  5018. }
  5019. return 0;
  5020. }
  5021. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index)
  5022. {
  5023. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5024. {
  5025. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  5026. }
  5027. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5028. {
  5029. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  5030. }
  5031. return 0;
  5032. }
  5033. int Get_Ocpp_MaxProfileStartIndex(void)
  5034. {
  5035. int _time = 0, _scheduleTime = 0;
  5036. int _maxCount = 0;
  5037. float _limit = 0;
  5038. int _startCount = NO_DEFINE;
  5039. _time = Get_Ocpp_MaxProfileStartTime();
  5040. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  5041. for(int i = 0; i < _maxCount; i++)
  5042. {
  5043. _scheduleTime = Get_Ocpp_MaxProfileScheduleTime(i);
  5044. if(_time >= _scheduleTime)
  5045. {
  5046. _limit = Get_Ocpp_MaxProfileScheduleLimit(i);
  5047. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  5048. {
  5049. _startCount = i;
  5050. }
  5051. }
  5052. }
  5053. return _startCount;
  5054. }
  5055. //===============================================
  5056. // Ocpp isRemoteStartWaitReq
  5057. //===============================================
  5058. int Is_Ocpp_RemoteStartWaitReq(void)
  5059. {
  5060. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5061. {
  5062. return ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  5063. }
  5064. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5065. {
  5066. return ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  5067. }
  5068. return 0;
  5069. }
  5070. //===============================================
  5071. // Ocpp SessionTarget
  5072. //===============================================
  5073. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget)
  5074. {
  5075. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5076. {
  5077. memcpy(sessionTarget, &ShmOCPP16Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5078. }
  5079. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5080. {
  5081. memcpy(sessionTarget, &ShmOCPP20Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5082. }
  5083. }
  5084. //===============================================
  5085. // Ocpp OffLineMaxChargingPower
  5086. //===============================================
  5087. int Get_Ocpp_OffLineMaxChargingPower(void)
  5088. {
  5089. // unit: W
  5090. int maxChargingPower = 0;
  5091. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5092. {
  5093. maxChargingPower = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[OffLineMaxChargingPower].ItemData);
  5094. }
  5095. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5096. {
  5097. maxChargingPower = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[OffLineMaxChargingPower].variableAttribute[0].value);
  5098. }
  5099. return maxChargingPower > 0 ? maxChargingPower : 0;
  5100. }
  5101. //===============================================
  5102. // Ocpp OcmfData
  5103. //===============================================
  5104. bool Is_Ocpp_SendOcmfDataReq(int gunIndex)
  5105. {
  5106. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5107. {
  5108. return ShmOCPP16Data->OcmfData[gunIndex].SendOcmfDataReq == YES ? true : false;
  5109. }
  5110. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5111. {
  5112. }
  5113. return false;
  5114. }
  5115. void Set_Ocpp_SendOcmfDataReq(int gunIndex)
  5116. {
  5117. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5118. {
  5119. ShmOCPP16Data->OcmfData[gunIndex].SendOcmfDataReq = YES;
  5120. }
  5121. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5122. {
  5123. }
  5124. }
  5125. void Clean_Ocpp_SendOcmfDataReq(int gunIndex)
  5126. {
  5127. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5128. {
  5129. ShmOCPP16Data->OcmfData[gunIndex].SendOcmfDataReq = NO;
  5130. }
  5131. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5132. {
  5133. }
  5134. }
  5135. void Set_Ocpp_OcmfData(int gunIndex, char *key, char *ocmfInfo)
  5136. {
  5137. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5138. {
  5139. strncpy((char *)ShmOCPP16Data->OcmfData[gunIndex].PublicKey, key, sizeof(ShmOCPP16Data->OcmfData[gunIndex].PublicKey));
  5140. strncpy((char *)ShmOCPP16Data->OcmfData[gunIndex].DataString, ocmfInfo, sizeof(ShmOCPP16Data->OcmfData[gunIndex].DataString));
  5141. }
  5142. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5143. {
  5144. }
  5145. }
  5146. //===============================================
  5147. // Set_StartIdType
  5148. //===============================================
  5149. void Set_StartIdType(int gunIndex, int type)
  5150. {
  5151. chargingInfo[gunIndex]->StartIdType = type;
  5152. }
  5153. //===============================================
  5154. // DeductResult
  5155. //===============================================
  5156. void Set_Deduct_UserId(int gunIndex, char *userId)
  5157. {
  5158. memcpy((char *)&ShmChargerInfo->DeductResultReq[gunIndex].creditNo, userId, strlen(userId));
  5159. }
  5160. void Set_Deduct_TransactionId(int gunIndex, int transaction)
  5161. {
  5162. ShmChargerInfo->DeductResultReq[gunIndex].TransactionId = transaction;
  5163. }
  5164. void Clean_DeductInfo(int gunIndex)
  5165. {
  5166. memset(&ShmChargerInfo->DeductResultReq[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5167. }
  5168. bool Is_DeductResultReq(int gunIndex)
  5169. {
  5170. return ShmChargerInfo->DeductResultReq[gunIndex].DeductReq ? true : false;
  5171. }
  5172. //===============================================
  5173. // ReaderStatus
  5174. //===============================================
  5175. bool Is_ReaderStatusReq(int gunIndex)
  5176. {
  5177. return ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq ? true : false;
  5178. }
  5179. void Clean_ReaderStatusInfo(int gunIndex)
  5180. {
  5181. memset(&ShmChargerInfo->ReaderStatus[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5182. }
  5183. //===============================================
  5184. // TransactionInfoData
  5185. //===============================================
  5186. void Set_UserTransactionId(int gunIndex, int transaction)
  5187. {
  5188. ShmChargerInfo->UserTransaction[gunIndex].TransactionId = transaction;
  5189. }
  5190. void Set_UserPrice(int gunIndex, float price)
  5191. {
  5192. ShmChargerInfo->UserTransaction[gunIndex].UserPrice = price;
  5193. }
  5194. void Set_UserTotalCost(int gunIndex, float cost)
  5195. {
  5196. ShmChargerInfo->UserTransaction[gunIndex].TotalCost = cost;
  5197. }
  5198. void Set_UserAccountBalance(int gunIndex, float balance)
  5199. {
  5200. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = balance;
  5201. }
  5202. void Set_UserDiscount(int gunIndex, float discount)
  5203. {
  5204. ShmChargerInfo->UserTransaction[gunIndex].CostDiscount = discount;
  5205. }
  5206. void Clean_UserTransaction(int gunIndex)
  5207. {
  5208. memset(&ShmChargerInfo->UserTransaction[gunIndex], 0x00, sizeof(TransactionInfoData));
  5209. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = -65535;
  5210. }
  5211. void Clean_UserPriceAndReceipt(int gunIndex)
  5212. {
  5213. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex]));
  5214. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex]));
  5215. }
  5216. //===============================================
  5217. // OccupancyDeductReq
  5218. //===============================================
  5219. bool Is_OccupancyDeductReq(int gunIndex)
  5220. {
  5221. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq ? true : false;
  5222. }
  5223. void Clean_OccupancyDeductReq(int gunIndex)
  5224. {
  5225. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq = false;
  5226. }
  5227. bool Is_OccupancyKeepCountingReq(int gunIndex)
  5228. {
  5229. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting ? true : false;
  5230. }
  5231. void Set_OccupancyKeepCountingReq(int gunIndex)
  5232. {
  5233. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = true;
  5234. }
  5235. void Clean_OccupancyKeepCountingReq(int gunIndex)
  5236. {
  5237. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = false;
  5238. }
  5239. bool Is_OccupancyFeeForceCancelReq(int gunIndex)
  5240. {
  5241. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.ForceCancel ? true : false;
  5242. }
  5243. void Set_OccupancyFeeForceCancelReq(int gunIndex)
  5244. {
  5245. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.ForceCancel = true;
  5246. }
  5247. void Clean_OccupancyFeeForceCancelReq(int gunIndex)
  5248. {
  5249. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.ForceCancel = false;
  5250. }
  5251. //===============================================
  5252. // Ocpp GetDiagnosticsReq
  5253. //===============================================
  5254. bool Is_Ocpp_GetDiagnosticsReq(void)
  5255. {
  5256. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5257. {
  5258. return ShmOCPP16Data->MsMsg.bits.GetDiagnosticsReq == YES ? true : false;
  5259. }
  5260. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5261. {
  5262. }
  5263. return false;
  5264. }
  5265. void Clean_Ocpp_GetDiagnosticsReq(void)
  5266. {
  5267. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5268. {
  5269. ShmOCPP16Data->MsMsg.bits.GetDiagnosticsReq = false;
  5270. }
  5271. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5272. {
  5273. }
  5274. }
  5275. //===============================================
  5276. // Main process
  5277. //===============================================
  5278. // 檢查 Byte 中某個 Bit 的值
  5279. // _byte : 欲改變的 byte
  5280. // _bit : 該 byte 的第幾個 bit
  5281. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  5282. {
  5283. return ( _byte & mask_table[_bit] ) != 0x00;
  5284. }
  5285. // 設定 Byte 中某個 Bit的值
  5286. // _byte : 欲改變的 byte
  5287. // _bit : 該 byte 的第幾個 bit
  5288. // value : 修改的值為 0 or 1
  5289. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  5290. {
  5291. if(value == 1)
  5292. *_byte |= (1 << _bit);
  5293. else if (value == 0)
  5294. *_byte ^= (1 << _bit);
  5295. }
  5296. void UserScanFunction()
  5297. {
  5298. }
  5299. unsigned char isModeChange(unsigned char gun_index)
  5300. {
  5301. unsigned char result = NO;
  5302. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  5303. {
  5304. result = YES;
  5305. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  5306. }
  5307. return result;
  5308. }
  5309. void AnnounceAccountBalance(unsigned char index, float balance)
  5310. {
  5311. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  5312. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = balance;
  5313. if(!ShmChargerInfo->Control.TestCtrl.bits.DisableBalance)
  5314. {
  5315. Set_Connector_MiscCommand(index, MISC_CONN_ACCOUNT_BALANCE);
  5316. }
  5317. LOG_INFO("%sAnnounce Account Balance %4.2f To Connector %d",
  5318. ShmChargerInfo->Control.TestCtrl.bits.DisableBalance ? "Do Not " : "", balance, index + 1);
  5319. }
  5320. void FouceAnnounceAccountBalance(unsigned char index)
  5321. {
  5322. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  5323. {
  5324. AnnounceAccountBalance(index, 0);
  5325. }
  5326. }
  5327. void CleanConnectorUserIdAndPrice(unsigned char index)
  5328. {
  5329. if(chargingInfo[index]->SystemStatus == S_IDLE)
  5330. {
  5331. LOG_INFO("*********** Connector %d Clean UserId & UserPrice ***********", index + 1);
  5332. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  5333. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  5334. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -65535;
  5335. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  5336. }
  5337. }
  5338. void DispenserAuthInitial(unsigned char dispenser)
  5339. {
  5340. LOG_INFO("*********** Dispenser %d Authorizing Initial ***********", dispenser + 1);
  5341. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5342. // clean dispenser price
  5343. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser]));
  5344. }
  5345. void GunAuthInitial(unsigned char connector)
  5346. {
  5347. LOG_INFO("*********** Gun %d Authorizing Initial ***********", connector + 1);
  5348. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5349. }
  5350. unsigned char _preGunAuthResult[GENERAL_GUN_QUANTITY];
  5351. unsigned char _preDispenserAuthResult[GENERAL_GUN_QUANTITY];
  5352. struct timespec _GunAuth_Time[GENERAL_GUN_QUANTITY];
  5353. struct timespec _DispenserAuth_Time[GENERAL_GUN_QUANTITY];
  5354. void AuthorizeTimeoutProcess(void)
  5355. {
  5356. int CompletedTime = 0;
  5357. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5358. {
  5359. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult != _AuthResult_None)
  5360. {
  5361. if(_preGunAuthResult[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult)
  5362. {
  5363. GetClockTime(&_GunAuth_Time[index]);
  5364. }
  5365. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5366. {
  5367. FouceAnnounceAccountBalance(index);
  5368. }
  5369. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5370. {
  5371. if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= AUTHORIZE_COMPLETED_TIME)
  5372. {
  5373. LOG_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
  5374. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5375. CleanConnectorUserIdAndPrice(index);
  5376. }
  5377. }
  5378. }
  5379. _preGunAuthResult[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult;
  5380. }
  5381. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5382. {
  5383. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult != _AuthResult_None)
  5384. {
  5385. if(_preDispenserAuthResult[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult)
  5386. {
  5387. GetClockTime(&_DispenserAuth_Time[index]);
  5388. }
  5389. CompletedTime = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
  5390. (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + AUTHORIZE_COMPLETED_TIME) : AUTHORIZE_COMPLETED_TIME;
  5391. if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= CompletedTime)
  5392. {
  5393. DispenserAuthInitial(index);
  5394. }
  5395. }
  5396. _preDispenserAuthResult[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult;
  5397. }
  5398. }
  5399. BOOL IsConnectorAuthorizeSuccess(unsigned char connector)
  5400. {
  5401. BOOL success = false;
  5402. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult == _AuthResult_Valid)
  5403. {
  5404. success = true;
  5405. }
  5406. return success;
  5407. }
  5408. BOOL IsAutoGunSelectionAuthorizeSuccess(unsigned char connector)
  5409. {
  5410. BOOL success = false;
  5411. unsigned char dispenser = 0;
  5412. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  5413. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo.AuthResult == _AuthResult_Valid)
  5414. {
  5415. success = true;
  5416. }
  5417. return success;
  5418. }
  5419. bool CheckConnectorTypeStatus(void)
  5420. {
  5421. bool result = true;
  5422. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 14)
  5423. {
  5424. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = ShmChargerInfo->Control.MaxConnector;
  5425. ShmChargerInfo->Control.ChargerRatingPower = ParsingRatingPower((char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5426. ShmChargerInfo->Control.PsuQuantityByModel = ShmChargerInfo->Control.ChargerRatingPower / (INFY_SINGLE_PSU_POWER / 100);
  5427. LOG_INFO("DC Gun Count = %d, RatingPower = %d.%d kW",
  5428. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  5429. (ShmChargerInfo->Control.ChargerRatingPower / 10),
  5430. (ShmChargerInfo->Control.ChargerRatingPower % 10));
  5431. if(ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  5432. {
  5433. result = false;
  5434. }
  5435. //DO360 charging info default setting
  5436. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5437. {
  5438. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  5439. chargingInfo[i]->Index = (i < ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity || i == 0) ? i : 0xFF;
  5440. chargingInfo[i]->Evboard_id = 0x00;
  5441. chargingInfo[i]->ReservationId = -1;
  5442. chargingInfo[i]->slotsIndex = 0;
  5443. chargingInfo[i]->SystemStatus = S_BOOTING;
  5444. chargingInfo[i]->Type = _Type_Unknown;
  5445. chargingInfo[i]->type_index = 0;
  5446. chargingInfo[i]->IsAvailable = YES;
  5447. }
  5448. }
  5449. else
  5450. {
  5451. // Module Name 不正確 - 告警
  5452. result = false;
  5453. }
  5454. return result;
  5455. }
  5456. void KillTaskExceptPrimary()
  5457. {
  5458. system("killall Module_Authorize");
  5459. system("killall Module_CabinetParallel");
  5460. system("killall Module_ChargerSelfTest");
  5461. system("killall Module_LedIndication");
  5462. system("killall Module_EvComm");
  5463. system("killall Module_InternalComm");
  5464. KillPowerModuleTask();
  5465. system("killall Module_4g &");
  5466. system("killall Module_Wifi &");
  5467. }
  5468. void KillNtpd(void)
  5469. {
  5470. system("killall ntpd");
  5471. }
  5472. void KillAllTask()
  5473. {
  5474. system("killall Module_Authorize");
  5475. system("killall Module_CabinetParallel");
  5476. system("killall Module_ChargerSelfTest");
  5477. system("killall Module_LedIndication");
  5478. system("killall Module_EventLogging");
  5479. system("killall Module_PrimaryComm");
  5480. system("killall Module_EvComm");
  5481. system("killall Module_LcmControl");
  5482. system("killall Module_InternalComm");
  5483. KillPowerModuleTask();
  5484. Kill_Ocpp_Service();
  5485. Kill_MaintainOcpp_Service();
  5486. system("killall Module_4g &");
  5487. system("killall Module_Wifi &");
  5488. }
  5489. int TransferImageViaFTP(char *IpAddress, char *SourcePath)
  5490. {
  5491. int isPass = PASS;
  5492. unsigned char ftpcmdbuf[256];
  5493. //Using ftpput command to transfer upgrade image,
  5494. //User name : root
  5495. //User password : y42j/4cj84
  5496. //Destination : as same as source path
  5497. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s \"%s\" \"%s\"", IpAddress, SourcePath, SourcePath);
  5498. if(system((char*)ftpcmdbuf) != 0)
  5499. {
  5500. isPass = FAIL;
  5501. }
  5502. return isPass;
  5503. }
  5504. void TriggerDispenserUpgrade(unsigned char index, char *FileName)
  5505. {
  5506. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, 0x00, 128);
  5507. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, FileName, strlen(FileName));
  5508. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.FirmwareUpdateRequest = true;
  5509. LOG_INFO("Trigger dispenser %d to update image [%s]", index + 1, FileName);
  5510. }
  5511. BOOL PutImageToAllDispenser(char *SourcePath, char *FileName, char *model_name)
  5512. {
  5513. int isPass = FAIL;
  5514. int isImageOK[GENERAL_GUN_QUANTITY];
  5515. int dispenser_index = 0;
  5516. BOOL transferred = FALSE;
  5517. memset(isImageOK, 0x00, sizeof(isImageOK));
  5518. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5519. {
  5520. isPass = FAIL;
  5521. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  5522. {
  5523. dispenser_index = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex;
  5524. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName, (char *)model_name) != EQUAL)
  5525. {
  5526. LOG_INFO("Dispenser %d model name [%s] update ignore",
  5527. dispenser_index + 1,
  5528. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName);
  5529. continue;
  5530. }
  5531. char DispenserIpAddress[16];
  5532. memset((char*)DispenserIpAddress, 0x00, 16);
  5533. sprintf((char*)DispenserIpAddress, "%d.%d.%d.%d",
  5534. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  5535. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  5536. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  5537. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  5538. LOG_INFO("Transfer Image(%s) to Dispenser %d (IP: %s)", SourcePath, dispenser_index + 1, DispenserIpAddress);
  5539. isPass = TransferImageViaFTP(DispenserIpAddress, SourcePath);
  5540. isImageOK[dispenser_index] = isPass;
  5541. LOG_INFO("Dispenser %d Put Image %s", dispenser_index + 1, isPass == PASS ? "OK" : "Fail");
  5542. }
  5543. }
  5544. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5545. {
  5546. if(isImageOK[i] > 0)
  5547. {
  5548. TriggerDispenserUpgrade(i, FileName);
  5549. transferred = true;
  5550. }
  5551. }
  5552. return transferred;
  5553. }
  5554. BOOL WaitAllDispenserUpgradeCompleted(void)
  5555. {
  5556. BOOL completed = TRUE;
  5557. GetClockTime(&_DispenserUpgrade_time);
  5558. do
  5559. {
  5560. completed = TRUE;
  5561. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5562. {
  5563. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest ||
  5564. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm)
  5565. {
  5566. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted)
  5567. {
  5568. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest = false;
  5569. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm = false;
  5570. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted = false;
  5571. LOG_INFO("Dispenser %d Upgrade Success", i + 1);
  5572. }
  5573. else
  5574. {
  5575. completed = FALSE;
  5576. }
  5577. }
  5578. }
  5579. if(GetTimeoutValue(_DispenserUpgrade_time) / uSEC_VAL >= DISPENSER_UPGRADE_TIME)
  5580. {
  5581. LOG_INFO("Wait Upgrade Timeout");
  5582. completed = FALSE;
  5583. break;
  5584. }
  5585. if(completed == FALSE)
  5586. {
  5587. sleep(1);
  5588. }
  5589. }while(!completed);
  5590. return completed;
  5591. }
  5592. void UpgradePrepare(void)
  5593. {
  5594. int count = 0;
  5595. SetAcContactor(OFF);
  5596. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5597. {
  5598. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5599. {
  5600. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5601. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5602. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5603. {
  5604. count++;
  5605. }
  5606. }
  5607. }
  5608. if(count > 0)
  5609. {
  5610. sleep(3);
  5611. }
  5612. else
  5613. {
  5614. sleep(1);
  5615. }
  5616. }
  5617. void TriggerPCabinetUpgrade(unsigned char index, char *FileName)
  5618. {
  5619. memset(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, 0x00, 128);
  5620. memcpy(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, FileName, strlen(FileName));
  5621. ShmChargerInfo->ParallelCabinet.PCabinet[index].UpdateRequest = YES;
  5622. LOG_INFO("Trigger PCabinet %d to update image [%s]", index + 1, FileName);
  5623. }
  5624. BOOL PutImageToAllParallelCabinet(char *SourcePath, char *FileName)
  5625. {
  5626. int isPass = FAIL;
  5627. BOOL transferred = FALSE;
  5628. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5629. {
  5630. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5631. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5632. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5633. {
  5634. int channel = ShmChargerInfo->ParallelCabinet.PCabinet[i].SocketChannel;
  5635. if(ShmChargerInfo->CabinetConnInfo[channel].Status == _SCONN_MATCHED &&
  5636. ShmChargerInfo->ParallelCabinet.PCUpdateState[i] == Not_Update)
  5637. {
  5638. char PCabinetIpAddress[16];
  5639. memset((char*)PCabinetIpAddress, 0x00, 16);
  5640. sprintf((char*)PCabinetIpAddress, "%d.%d.%d.%d",
  5641. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 0) & 0xFF),
  5642. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 8) & 0xFF),
  5643. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 16) & 0xFF),
  5644. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 24) & 0xFF));
  5645. isPass = TransferImageViaFTP(PCabinetIpAddress, SourcePath);
  5646. if(isPass == PASS)
  5647. {
  5648. TriggerPCabinetUpgrade(i, FileName);
  5649. }
  5650. transferred = true;
  5651. }
  5652. }
  5653. }
  5654. return transferred;
  5655. }
  5656. BOOL WaitAllPCabinetStartUpgrade(void)
  5657. {
  5658. BOOL isStart = true;
  5659. int count = 0;
  5660. struct timespec _waitStart;
  5661. GetClockTime(&_waitStart);
  5662. do
  5663. {
  5664. isStart = true;
  5665. count = 0;
  5666. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5667. {
  5668. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5669. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5670. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5671. {
  5672. count++;
  5673. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updating)
  5674. {
  5675. isStart = false;
  5676. }
  5677. }
  5678. }
  5679. if((GetTimeoutValue(_waitStart) / uSEC_VAL) >= START_UPGRADE_TIME)
  5680. {
  5681. isStart = false;
  5682. break;
  5683. }
  5684. usleep(10000);
  5685. }while(!isStart);
  5686. if(isStart && count > 0)
  5687. {
  5688. LOG_INFO("Total %d PCabinet upgrade start...", count);
  5689. }
  5690. else
  5691. {
  5692. if(count > 0)
  5693. {
  5694. LOG_INFO("Wait PCabinet upgrade timeout");
  5695. }
  5696. else
  5697. {
  5698. LOG_INFO("No PCabinet need to upgrade");
  5699. }
  5700. }
  5701. return isStart;
  5702. }
  5703. BOOL WaitAllPCabinetUpgradeCompleted(void)
  5704. {
  5705. BOOL completed = true;
  5706. int count = 0;
  5707. struct timespec _waitCompleted;
  5708. GetClockTime(&_waitCompleted);
  5709. do
  5710. {
  5711. completed = true;
  5712. count = 0;
  5713. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5714. {
  5715. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5716. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5717. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5718. {
  5719. count++;
  5720. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updated)
  5721. {
  5722. completed = false;
  5723. }
  5724. }
  5725. }
  5726. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= PCABINET_UPGRADE_TIME)
  5727. {
  5728. completed = false;
  5729. break;
  5730. }
  5731. usleep(10000);
  5732. }while(!completed);
  5733. if(completed && count > 0)
  5734. {
  5735. LOG_INFO("Total %d PCabinet upgrade completed!", count);
  5736. }
  5737. else
  5738. {
  5739. if(count > 0)
  5740. {
  5741. LOG_INFO("Wait PCabinet upgrade completed timeout");
  5742. }
  5743. else
  5744. {
  5745. LOG_INFO("Need not to wait PCabinet completed");
  5746. }
  5747. }
  5748. return completed;
  5749. }
  5750. BOOL WaitMCabinetUpgradeCompleted(void)
  5751. {
  5752. BOOL completed = true;
  5753. struct timespec _waitCompleted;
  5754. GetClockTime(&_waitCompleted);
  5755. do
  5756. {
  5757. completed = false;
  5758. if(ShmChargerInfo->SCabinetControl.NeedUpgrade == _R_NoUpgrade)
  5759. {
  5760. completed = true;
  5761. }
  5762. if(ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Idle &&
  5763. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Alarm &&
  5764. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Charging)
  5765. {
  5766. completed = false;
  5767. break;
  5768. }
  5769. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= MCABINET_UPGRADE_TIME)
  5770. {
  5771. completed = false;
  5772. break;
  5773. }
  5774. usleep(10000);
  5775. }while(!completed);
  5776. if(completed)
  5777. {
  5778. LOG_INFO("MCabinet upgrade completed!");
  5779. }
  5780. else
  5781. {
  5782. LOG_INFO("Wait MCabinet upgrade timeout or disconnected...");
  5783. }
  5784. return completed;
  5785. }
  5786. bool IsModelNameMatch(char *model)
  5787. {
  5788. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == model[0] &&
  5789. ShmSysConfigAndInfo->SysConfig.ModelName[1] == model[1] &&
  5790. ShmSysConfigAndInfo->SysConfig.ModelName[7] == model[7] &&
  5791. ShmSysConfigAndInfo->SysConfig.ModelName[8] == model[8] &&
  5792. ShmSysConfigAndInfo->SysConfig.ModelName[9] == model[9] &&
  5793. ShmSysConfigAndInfo->SysConfig.ModelName[11] == model[11] &&
  5794. ShmSysConfigAndInfo->SysConfig.ModelName[12] == model[12] &&
  5795. ShmSysConfigAndInfo->SysConfig.ModelName[13] == model[13])
  5796. {
  5797. return true;
  5798. }
  5799. return false;
  5800. }
  5801. unsigned long long getAvailableMemory(void)
  5802. {
  5803. long pages = sysconf(_SC_AVPHYS_PAGES);
  5804. long page_size = sysconf(_SC_PAGE_SIZE);
  5805. return pages * page_size;
  5806. }
  5807. void ImageTypeParsing(unsigned int type, char *type_str)
  5808. {
  5809. switch(type)
  5810. {
  5811. case 0x10000001:
  5812. sprintf(type_str, "Image_uboot");
  5813. break;
  5814. case 0x10000002:
  5815. sprintf(type_str, "Image_dtb");
  5816. break;
  5817. case 0x10000003:
  5818. sprintf(type_str, "Image_zImage");
  5819. break;
  5820. case 0x10000004:
  5821. sprintf(type_str, "Image_ramdisk");
  5822. break;
  5823. case 0x10000005:
  5824. sprintf(type_str, "Image_Factory");
  5825. break;
  5826. case 0x10000006:
  5827. sprintf(type_str, "Image_Primary");
  5828. break;
  5829. case 0x10000007:
  5830. sprintf(type_str, "Image_CCS_uboot");
  5831. break;
  5832. case 0x10000008:
  5833. sprintf(type_str, "Image_CCS_dtb");
  5834. break;
  5835. case 0x10000009:
  5836. sprintf(type_str, "Image_CCS_zImage");
  5837. break;
  5838. case 0x1000000A:
  5839. sprintf(type_str, "Image_CCS_ramdisk");
  5840. break;
  5841. case 0x1000000B:
  5842. sprintf(type_str, "Image_CAHdeMO");
  5843. break;
  5844. case 0x1000000C:
  5845. sprintf(type_str, "Image_GBT");
  5846. break;
  5847. case 0x1000000D:
  5848. sprintf(type_str, "Image_Relay");
  5849. break;
  5850. case 0x1000000E:
  5851. sprintf(type_str, "Image_Fan");
  5852. break;
  5853. case 0x1000000F:
  5854. sprintf(type_str, "Image_LCM");
  5855. break;
  5856. case 0x10000010:
  5857. sprintf(type_str, "Image_Infy_PFC");
  5858. break;
  5859. case 0x10000011:
  5860. sprintf(type_str, "Image_Infy_DD");
  5861. break;
  5862. case 0x10000014:
  5863. sprintf(type_str, "Image_LED");
  5864. break;
  5865. case 0x10000015:
  5866. sprintf(type_str, "Image_LCM_APK");
  5867. break;
  5868. default:
  5869. sprintf(type_str, "Image_Unknown");
  5870. break;
  5871. }
  5872. }
  5873. char CheckUpdateProcess()
  5874. {
  5875. char _model[32];
  5876. char _type[32];
  5877. bool support = false;
  5878. bool OneImageOnly = false;
  5879. int isPass = true;
  5880. uint8_t retSucc = 0;
  5881. uint8_t retFail = 0;
  5882. byte target = 0;
  5883. char *new_str = NULL;
  5884. unsigned char *ptr = NULL;
  5885. int fd = 0;
  5886. int uartFd = 0;
  5887. unsigned int Type = 0;
  5888. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  5889. DIR *d;
  5890. struct dirent *dir;
  5891. char cmdBuf[128];
  5892. int availableMem = 0;
  5893. memset(_model, 0x00, sizeof(_model));
  5894. memset(_type, 0x00, sizeof(_type));
  5895. availableMem = getAvailableMemory() / (1024 * 1024);
  5896. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5897. // If available memory too small, free memory cache first
  5898. if(availableMem < MIN_AVAILABLE_MEMORY)
  5899. {
  5900. LOG_INFO("Available memory less than %d MBytes, free cache & kill ntpd.", MIN_AVAILABLE_MEMORY);
  5901. system("echo 3 > /proc/sys/vm/drop_caches");
  5902. KillNtpd();
  5903. availableMem = getAvailableMemory() / (1024 * 1024);
  5904. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5905. }
  5906. d = opendir("/mnt/");
  5907. if(d)
  5908. {
  5909. while((dir = readdir(d)) != NULL && !OneImageOnly)
  5910. {
  5911. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  5912. continue;
  5913. }
  5914. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  5915. strcat(new_str, "/mnt/");
  5916. strcat(new_str, dir->d_name);
  5917. fd = open(new_str, O_RDONLY);
  5918. if(fd < 0)
  5919. {
  5920. close(fd);
  5921. free(new_str);
  5922. continue;
  5923. }
  5924. LOG_INFO("%s\n", new_str);
  5925. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  5926. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  5927. //get the image length
  5928. ImageLen = read(fd, ptr, MaxLen);
  5929. if(ImageLen > 20)
  5930. {
  5931. memcpy(_model, ptr, 16);
  5932. Type = (((unsigned int)ptr[16]) << 24 | ((unsigned int)ptr[17]) << 16 | ((unsigned int)ptr[18]) << 8 | ((unsigned int)ptr[19]));
  5933. ImageTypeParsing(Type, _type);
  5934. LOG_INFO("Model: %s, Type: %s", _model, _type);
  5935. }
  5936. free(ptr);
  5937. if (ImageLen > 20)
  5938. {
  5939. support = false;
  5940. availableMem = getAvailableMemory() / (1024 * 1024);
  5941. LOG_INFO("AvailableMemory: %dMByte", availableMem);
  5942. //if(strcmp(model, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == EQUAL)
  5943. if(IsModelNameMatch(_model))
  5944. {
  5945. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5946. {
  5947. PutImageToAllParallelCabinet(new_str, dir->d_name);
  5948. WaitAllPCabinetStartUpgrade();
  5949. }
  5950. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.NeedUpgrade)
  5951. {
  5952. ShmChargerInfo->SCabinetControl.UpgradeState = Updating;
  5953. sleep(1);
  5954. }
  5955. switch (Type)
  5956. {
  5957. case 0x10000001:
  5958. case 0x10000002:
  5959. case 0x10000003:
  5960. case 0x10000004:
  5961. case 0x10000005:
  5962. support = true;
  5963. _UpgradeNeedReboot = TRUE;
  5964. LOG_INFO("Upgrade Flash Start...", Type);
  5965. isPass = Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5966. LOG_INFO("Upgrade Flash End, %s", isPass ? "Pass" : "Fail");
  5967. break;
  5968. case 0x10000006: // primary mcu
  5969. case 0x1000000D: // relay board
  5970. case 0x1000000E: // fan board
  5971. case 0x10000014: // led board
  5972. support = true;
  5973. target = 0x00;
  5974. if (Type == 0x10000006)
  5975. {
  5976. target = UPGRADE_PRI;
  5977. }
  5978. else if (Type == 0x1000000D)
  5979. {
  5980. target = UPGRADE_RB;
  5981. }
  5982. else if (Type == 0x1000000E)
  5983. {
  5984. target = UPGRADE_FAN;
  5985. }
  5986. else if (Type == 0x10000014)
  5987. {
  5988. target = UPGRADE_LED;
  5989. }
  5990. uartFd = InitComPort(target);
  5991. if(target == UPGRADE_RB)
  5992. {
  5993. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC1, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5994. if(isPass == PASS)
  5995. {
  5996. retSucc++;
  5997. }
  5998. else
  5999. {
  6000. retFail++;
  6001. }
  6002. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  6003. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  6004. {
  6005. sleep(3);
  6006. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC2, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  6007. }
  6008. }
  6009. else
  6010. {
  6011. isPass = Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  6012. }
  6013. close(uartFd);
  6014. break;
  6015. }
  6016. if(support)
  6017. {
  6018. if(isPass == PASS)
  6019. {
  6020. retSucc++;
  6021. }
  6022. else
  6023. {
  6024. retFail++;
  6025. }
  6026. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  6027. }
  6028. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  6029. {
  6030. LOG_INFO("MCabinet upgrade completed");
  6031. WaitAllPCabinetUpgradeCompleted();
  6032. }
  6033. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.UpgradeState == Updating)
  6034. {
  6035. ShmChargerInfo->SCabinetControl.UpgradeState = Updated;
  6036. LOG_INFO("SCabinet upgrade completed");
  6037. WaitMCabinetUpgradeCompleted();
  6038. }
  6039. }
  6040. else
  6041. {
  6042. if(_model[0] == 'D' && (_model[1] == 'D' || _model[1] == 'X'))
  6043. {
  6044. OneImageOnly = true;
  6045. _UpgradeNeedReboot = TRUE;
  6046. LOG_INFO("Start Put Image To Dispenser");
  6047. PutImageToAllDispenser(new_str, dir->d_name, _model);
  6048. WaitAllDispenserUpgradeCompleted();
  6049. LOG_INFO("All Dispenser Upgrade Completed");
  6050. }
  6051. else
  6052. {
  6053. retFail++;
  6054. LOG_INFO("Model Name Is Not Match: %s", _model);
  6055. }
  6056. }
  6057. }
  6058. else
  6059. {
  6060. retFail++;
  6061. LOG_INFO("Image length is illegal");
  6062. }
  6063. sprintf(cmdBuf, "rm -f %s", new_str);
  6064. system(cmdBuf);
  6065. close(fd);
  6066. free(new_str);
  6067. }
  6068. }
  6069. closedir(d);
  6070. if(retFail != 0)
  6071. {
  6072. return FAIL;
  6073. }
  6074. return PASS;
  6075. }
  6076. void TriggerDispenserConnectionTimeoutSetting(void)
  6077. {
  6078. BOOL trigger = FALSE;
  6079. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6080. {
  6081. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0 &&
  6082. !Is_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT))
  6083. {
  6084. trigger = TRUE;
  6085. Set_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT);
  6086. }
  6087. }
  6088. if(trigger)
  6089. {
  6090. LOG_INFO("********** Trigger Dispenser Connector Timeout Value: %d **********", ShmChargerInfo->CabinetMiscValue.ConnectionTimeout);
  6091. }
  6092. }
  6093. void CheckConnectionTimeout(void)
  6094. {
  6095. if(!Is_Ocpp_Task_Alive())
  6096. {
  6097. _connectionTimeout = CONN_PLUG_TIME_OUT;
  6098. }
  6099. else
  6100. {
  6101. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  6102. {
  6103. _connectionTimeout = Get_Ocpp_ConnectionTimeOut();
  6104. if(_connectionTimeout <= 0)
  6105. {
  6106. _connectionTimeout = CONN_PLUG_TIME_OUT;
  6107. }
  6108. }
  6109. else
  6110. {
  6111. _connectionTimeout = CONN_PLUG_TIME_OUT;
  6112. }
  6113. }
  6114. if(_connectionTimeout != ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  6115. {
  6116. ShmChargerInfo->CabinetMiscValue.ConnectionTimeout = _connectionTimeout;
  6117. TriggerDispenserConnectionTimeoutSetting();
  6118. }
  6119. }
  6120. void CreateTimeoutFork()
  6121. {
  6122. pid_t timeoutPid;
  6123. timeoutPid = fork();
  6124. if (timeoutPid == 0)
  6125. {
  6126. GetClockTime(&_cmdSubPriority_time);
  6127. CheckConnectionTimeout();
  6128. LOG_INFO("Timeout Fork PID = %d", getpid());
  6129. while(true)
  6130. {
  6131. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  6132. {
  6133. CheckConnectionTimeout();
  6134. GetClockTime(&_cmdSubPriority_time);
  6135. }
  6136. sleep(1);
  6137. }
  6138. }
  6139. }
  6140. void CheckFactoryConfigFunction(void)
  6141. {
  6142. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  6143. {
  6144. char Buf[256];
  6145. sprintf(Buf, "cd /root;./FactoryConfig -m");
  6146. system(Buf);
  6147. system("rm -f /Storage/OCPP/OCPPConfiguration");
  6148. system("sync");
  6149. sleep(5);
  6150. system("reboot -f");
  6151. sleep(5);
  6152. system("reboot -f");
  6153. }
  6154. }
  6155. void CheckFwUpdateFunction(void)
  6156. {
  6157. char UpdateResult = PASS;
  6158. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  6159. {
  6160. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  6161. UpgradePrepare();
  6162. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  6163. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  6164. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  6165. LOG_INFO("ftp : update start.");
  6166. TryCloseWatchdog();
  6167. //KillTask();
  6168. system("killall Module_PrimaryComm");
  6169. system("killall Module_InternalComm");
  6170. UpdateResult = CheckUpdateProcess();
  6171. if (UpdateResult == PASS)
  6172. {
  6173. LOG_INFO("ftp : update complete.");
  6174. }
  6175. else
  6176. {
  6177. LOG_INFO("ftp : update fail.");
  6178. }
  6179. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  6180. sleep(10);
  6181. if(_UpgradeNeedReboot)
  6182. {
  6183. system("reboot -f");
  6184. }
  6185. else
  6186. {
  6187. KillAllTask();
  6188. system("/usr/bin/run_evse_restart.sh");
  6189. }
  6190. }
  6191. else if(Is_Ocpp_UpdateFirmwareReq())
  6192. {
  6193. Clean_Ocpp_UpdateFirmwareReq();
  6194. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  6195. UpgradePrepare();
  6196. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  6197. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  6198. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  6199. if(Is_Ocpp_UpdateFirmwareStatus("Downloaded"))
  6200. {
  6201. LOG_INFO("Backend : update start.");
  6202. TryCloseWatchdog();
  6203. Set_Ocpp_UpdateFirmwareStatus("Installing");
  6204. Set_Ocpp_FirmwareStatusNotificationReq();
  6205. //KillTask();
  6206. system("killall Module_PrimaryComm");
  6207. system("killall Module_InternalComm");
  6208. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6209. {
  6210. setChargerMode(_index, MODE_UPDATE);
  6211. }
  6212. UpdateResult = CheckUpdateProcess();
  6213. if (UpdateResult == PASS)
  6214. {
  6215. LOG_INFO("Backend : update complete.");
  6216. Set_Ocpp_UpdateFirmwareStatus("Installed");
  6217. }
  6218. else
  6219. {
  6220. LOG_INFO("Backend : update fail.");
  6221. Set_Ocpp_UpdateFirmwareStatus("InstallationFailed");
  6222. }
  6223. Set_Ocpp_FirmwareStatusNotificationReq();
  6224. sleep(10);
  6225. if(_UpgradeNeedReboot)
  6226. {
  6227. system("reboot -f");
  6228. }
  6229. else
  6230. {
  6231. KillAllTask();
  6232. system("/usr/bin/run_evse_restart.sh");
  6233. }
  6234. }
  6235. }
  6236. }
  6237. void ExecuteWriteFlash(void)
  6238. {
  6239. struct SysConfigData config;
  6240. memcpy(&config, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  6241. config.SwitchDebugFlag = NO;
  6242. strcpy((char *) config.UserId, "");
  6243. int result = StoreUsrConfigData(&config);
  6244. if(result != 1)
  6245. {
  6246. LOG_INFO("Store User Config NG");
  6247. }
  6248. else
  6249. {
  6250. LOG_INFO("Store User Config OK");
  6251. }
  6252. }
  6253. struct timespec _WiringInfo_time;
  6254. int CheckFlashUpdate(void)
  6255. {
  6256. if(ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged)
  6257. {
  6258. ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged = false;
  6259. LOG_INFO("Flash Config Changed");
  6260. GetClockTime(&_WiringInfo_time);
  6261. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = true;
  6262. }
  6263. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable)
  6264. {
  6265. // wait for 30s to write flash or write flash immediately
  6266. if((GetTimeoutValue(_WiringInfo_time) / uSEC_VAL) >= WIRING_INFO_DELAY ||
  6267. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashImmediately)
  6268. {
  6269. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = false;
  6270. LOG_INFO("Start Writing Flash");
  6271. pid_t writePid = fork();
  6272. if(writePid == 0)
  6273. {
  6274. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = true;
  6275. ExecuteWriteFlash();
  6276. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = false;
  6277. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset)
  6278. {
  6279. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset = false;
  6280. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  6281. LOG_INFO("Write Flash OK And Need Soft Reset");
  6282. }
  6283. return NO;
  6284. }
  6285. LOG_INFO("Write Flash Conpleted");
  6286. }
  6287. }
  6288. return YES;
  6289. }
  6290. void CheckSoftHardReset(void)
  6291. {
  6292. if(ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset)
  6293. {
  6294. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = false;
  6295. LOG_INFO("********** Need Soft Reset **********");
  6296. sleep(5);
  6297. Kill_Ocpp_Service();
  6298. KillAllTask();
  6299. TryCloseWatchdog();
  6300. system("/usr/bin/run_evse_restart.sh");
  6301. }
  6302. if(ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset)
  6303. {
  6304. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = false;
  6305. LOG_INFO("********** Need Hard Reset **********");
  6306. sleep(5);
  6307. system("reboot -f");
  6308. }
  6309. }
  6310. struct timespec _Reboot_time;
  6311. void TriggerDispenserHardwareReboot(void)
  6312. {
  6313. BOOL trigger = FALSE;
  6314. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6315. {
  6316. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6317. {
  6318. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm == 0 &&
  6319. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  6320. {
  6321. trigger = TRUE;
  6322. Set_Dispenser_MiscCommand(i, MISC_DISP_HARDWARE_REBOOT);
  6323. }
  6324. }
  6325. }
  6326. if(trigger)
  6327. {
  6328. GetClockTime(&_Reboot_time);
  6329. LOG_INFO("********** Trigger Dispenser Hardware Reboot **********");
  6330. }
  6331. }
  6332. BOOL IsDispenserHardwareRebootResponsed(void)
  6333. {
  6334. BOOL response_ok = TRUE;
  6335. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6336. {
  6337. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6338. {
  6339. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  6340. {
  6341. response_ok = FALSE;
  6342. // 10s for reboot request timeout
  6343. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  6344. {
  6345. response_ok = TRUE;
  6346. LOG_INFO("Reboot request timeout");
  6347. }
  6348. }
  6349. }
  6350. }
  6351. return response_ok;
  6352. }
  6353. struct timespec _Reset_time;
  6354. void TriggerDispenserSoftwareReset(void)
  6355. {
  6356. BOOL trigger = FALSE;
  6357. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6358. {
  6359. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6360. {
  6361. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  6362. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  6363. {
  6364. trigger = TRUE;
  6365. Set_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET);
  6366. }
  6367. }
  6368. }
  6369. if(trigger)
  6370. {
  6371. GetClockTime(&_Reset_time);
  6372. LOG_INFO("********** Trigger Dispenser Software Reset **********");
  6373. }
  6374. }
  6375. BOOL IsDispenserSoftwareResetResponsed(void)
  6376. {
  6377. BOOL response_ok = TRUE;
  6378. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6379. {
  6380. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6381. {
  6382. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  6383. {
  6384. response_ok = FALSE;
  6385. // 10s for reset request timeout
  6386. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  6387. {
  6388. response_ok = TRUE;
  6389. LOG_INFO("Reset request timeout");
  6390. }
  6391. }
  6392. }
  6393. }
  6394. return response_ok;
  6395. }
  6396. // return time offset
  6397. int TimeOffsetKeyParsing(char *StringItem)
  6398. {
  6399. int offset = 0;
  6400. int hour = 0, minute = 0, sign = 1;
  6401. char *ptrSave, *ptrToken;
  6402. char strSource[501];
  6403. memset(strSource, 0x00, sizeof(strSource));
  6404. strcpy(strSource, StringItem);
  6405. if(strSource[0] != '+' && strSource[0] != '-')
  6406. {
  6407. return offset;
  6408. }
  6409. sign = strSource[0] == '+' ? (1) : (-1);
  6410. ptrToken = strtok_r(&strSource[1], ":", &ptrSave);
  6411. if(ptrToken != NULL)
  6412. {
  6413. hour = atoi(ptrToken);
  6414. ptrToken = strtok_r(NULL, ":", &ptrSave);
  6415. if(ptrToken != NULL)
  6416. {
  6417. minute = atoi(ptrToken);
  6418. offset = ((hour * 60) + minute) * sign;
  6419. }
  6420. }
  6421. return offset;
  6422. }
  6423. char MyDefaultPriceString[501];
  6424. void DefaultPriceHandler(void)
  6425. {
  6426. char price[501];
  6427. char defaultPrice[512];
  6428. memset(defaultPrice, 0x00, sizeof(defaultPrice));
  6429. Get_Ocpp_DefaultPrice((char *)price);
  6430. if(strcmp(MyDefaultPriceString, (char *)price) != EQUAL)
  6431. {
  6432. float ConnectionFee = 0, OccupancyFee = 0;
  6433. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6434. {
  6435. ConnectionFee = PriceParsing(price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6436. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = PriceParsing(price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6437. OccupancyFee = PriceParsing(price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6438. LOG_INFO("******** Customer Audi Default Price *******");
  6439. LOG_INFO("********** Connection Fee: %4.2f **********", ConnectionFee);
  6440. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6441. LOG_INFO("********** Occupancy Fee: %4.2f **********", OccupancyFee);
  6442. }
  6443. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6444. {
  6445. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6446. ShellPriceParsing(price, defaultPrice);
  6447. LOG_INFO("******* Customer Shell Default Price *******");
  6448. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6449. }
  6450. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6451. {
  6452. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = TccDefaultPriceParsing(price, &ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6453. LOG_INFO("******* Customer Tcc Default Price *******");
  6454. LOG_INFO("*** TimeOfDayPricing: %4.2f", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6455. }
  6456. else
  6457. {
  6458. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6459. strcpy(defaultPrice, price);
  6460. LOG_INFO("********** Standard Default Price **********");
  6461. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6462. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6463. }
  6464. strcpy(MyDefaultPriceString, price);
  6465. }
  6466. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6467. {
  6468. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = GetNowTimePricing(&ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6469. }
  6470. if(ShmChargerInfo->CabinetMiscValue.DefaultPrice != ShmChargerInfo->PriceAndReceiptInfo.CurrentRate)
  6471. {
  6472. ShmChargerInfo->CabinetMiscValue.DefaultPrice = ShmChargerInfo->PriceAndReceiptInfo.CurrentRate;
  6473. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6474. {
  6475. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6476. {
  6477. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE))
  6478. {
  6479. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE);
  6480. }
  6481. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY))
  6482. {
  6483. Set_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY);
  6484. }
  6485. }
  6486. }
  6487. LOG_INFO("********** Trigger Dispenser Default Price Value **********");
  6488. }
  6489. if(strlen(defaultPrice) > 0)
  6490. {
  6491. strcpy(ShmChargerInfo->PriceAndReceiptInfo.DefaultPriceString, defaultPrice);
  6492. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6493. {
  6494. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6495. {
  6496. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING))
  6497. {
  6498. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING);
  6499. }
  6500. }
  6501. }
  6502. LOG_INFO("********** Trigger Dispenser Default Price Value String **********");
  6503. }
  6504. }
  6505. void UserPriceHandler(void)
  6506. {
  6507. char UserId[32];
  6508. char price[500];
  6509. char userPrice[512];
  6510. memset(userPrice, 0x00, sizeof(userPrice));
  6511. Get_Ocpp_UserPrice(UserId, price);
  6512. if(strlen((char *)UserId) > 0 && strlen((char *)price) > 0)
  6513. {
  6514. float ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  6515. float AccountBalance = 0;
  6516. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6517. {
  6518. ConnectionFee = PriceParsing((char *)price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6519. CurrentRate = PriceParsing((char *)price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6520. OccupancyFee = PriceParsing((char *)price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6521. AccountBalance = PriceParsing((char *)price, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6522. LOG_INFO("********* Customer Audi User Price *********");
  6523. LOG_INFO("********* User Id: [%s]", UserId);
  6524. LOG_INFO("********* Connection Fee: [%7.2f]", ConnectionFee);
  6525. LOG_INFO("********* Current Rate: [%7.2f]", CurrentRate);
  6526. LOG_INFO("********* Occupancy Fee: [%7.2f]", OccupancyFee);
  6527. LOG_INFO("********* Account Balance: [%7.2f]", AccountBalance);
  6528. }
  6529. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6530. {
  6531. ShellPriceParsing(price, userPrice);
  6532. LOG_INFO("******** Customer Shell User Price *********");
  6533. LOG_INFO("******** User Id: [%s]", UserId);
  6534. LOG_INFO("******** User Price: [%s]", userPrice);
  6535. }
  6536. else
  6537. {
  6538. CurrentRate = PriceParsing((char *)price, "chargineFee", _PHIHONG_PRICE_SPLIT_KEY);
  6539. OccupancyFee = PriceParsing((char *)price, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6540. strcpy(userPrice, price);
  6541. LOG_INFO("*********** Standard User Price ************");
  6542. LOG_INFO("*********** User Id: [%s]", UserId);
  6543. LOG_INFO("*********** Current Rate: [%7.2f]", CurrentRate);
  6544. LOG_INFO("*********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6545. LOG_INFO("*********** User Price: [%s]", userPrice);
  6546. }
  6547. #if 0
  6548. LOG_INFO("********** User Id: %s **********", UserId);
  6549. LOG_INFO("********** User Price: %s", price);
  6550. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6551. LOG_INFO("********** Current Rate: %7.2f **********", CurrentRate);
  6552. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6553. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6554. #endif
  6555. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6556. {
  6557. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  6558. {
  6559. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  6560. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  6561. {
  6562. LOG_INFO("********** Connector Id %d User Price %4.2f, Account Balance %7.2f **********",
  6563. (i + 1), CurrentRate, AccountBalance);
  6564. }
  6565. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  6566. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  6567. if(strlen(userPrice) > 0)
  6568. {
  6569. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[i][0], userPrice);
  6570. Set_Connector_MiscCommand(i, MISC_CONN_USER_PRICE_STRING);
  6571. }
  6572. if(chargingInfo[i]->SystemStatus == S_AUTHORIZING)
  6573. {
  6574. AnnounceAccountBalance(i, AccountBalance);
  6575. }
  6576. }
  6577. }
  6578. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6579. {
  6580. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthResult == _AuthResult_Valid)
  6581. {
  6582. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthId, UserId) == EQUAL)
  6583. {
  6584. if(strlen(userPrice) > 0)
  6585. {
  6586. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[i][0], userPrice);
  6587. }
  6588. }
  6589. }
  6590. }
  6591. Clean_Ocpp_UserPrice();
  6592. }
  6593. }
  6594. void RunningCostParsing(int gunIndex, char *strCost)
  6595. {
  6596. float ConnectionFee = 0, SessionFee = 0, OccupancyFee = -1, TotalCost = 0, EnergyCost = -1;
  6597. float AccountBalance = 0;
  6598. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6599. {
  6600. ConnectionFee = PriceParsing((char *)strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6601. SessionFee = PriceParsing((char *)strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6602. OccupancyFee = PriceParsing((char *)strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6603. TotalCost = PriceParsing((char *)strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6604. AccountBalance = PriceParsing((char *)strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6605. LOG_INFO("******** Customer Audi Gun %d Running Cost ********", (gunIndex + 1));
  6606. LOG_INFO("******** Connection Fee: [%7.2f]", ConnectionFee);
  6607. LOG_INFO("******** Session Fee: [%7.2f]", SessionFee);
  6608. LOG_INFO("******** Occupancy Fee: [%7.2f]", OccupancyFee);
  6609. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6610. LOG_INFO("******** Account Balance: [%7.2f]", AccountBalance);
  6611. }
  6612. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6613. {
  6614. TotalCost = ShellRunningCostParsing(strCost);
  6615. LOG_INFO("******** Customer Shell Gun %d Running Cost *******", (gunIndex + 1));
  6616. LOG_INFO("******** Gun %d Running Cost", (gunIndex + 1));
  6617. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6618. }
  6619. else
  6620. {
  6621. EnergyCost = PriceParsing((char *)strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6622. TotalCost = EnergyCost;
  6623. OccupancyFee = PriceParsing((char *)strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6624. LOG_INFO("********** Standard Gun %d Running Cost ***********", (gunIndex + 1));
  6625. LOG_INFO("********** Energy Cost: [%7.2f]", EnergyCost);
  6626. LOG_INFO("********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6627. LOG_INFO("********** Total Cost: [%7.2f]", TotalCost);
  6628. }
  6629. #if 0
  6630. LOG_INFO("********** Connector Id: %d Running Cost **********", (gunIndex + 1));
  6631. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6632. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6633. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6634. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6635. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6636. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6637. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance)
  6638. {
  6639. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance %7.2f **********",
  6640. (gunIndex + 1), TotalCost, AccountBalance);
  6641. }
  6642. #endif
  6643. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = TotalCost;
  6644. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = AccountBalance;
  6645. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = EnergyCost;
  6646. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = OccupancyFee;
  6647. }
  6648. void RunningCostHandler(void)
  6649. {
  6650. char strCost[256];
  6651. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6652. {
  6653. Get_Ocpp_RunningCost(i, strCost);
  6654. if(strlen((char *)strCost) > 0)
  6655. {
  6656. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  6657. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_CHARGING)
  6658. {
  6659. RunningCostParsing(i, strCost);
  6660. }
  6661. Clean_Ocpp_RunningCost(i);
  6662. }
  6663. }
  6664. }
  6665. void EnableConnectorBillRequest(int gunIndex, ChargingBillInfoData *bill)
  6666. {
  6667. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6668. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6669. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = bill->TxId;
  6670. memcpy(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, &bill->FeeInfo, sizeof(FeeInfoData));
  6671. }
  6672. void DisableConnectorBill(int gunIndex)
  6673. {
  6674. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Idle;
  6675. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6676. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = 0;
  6677. memset(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, 0x00, sizeof(FeeInfoData));
  6678. }
  6679. void FinalCostParsing(int txId, char *strCost, ChargingBillInfoData *_bill)
  6680. {
  6681. memset(_bill, 0x00, sizeof(ChargingBillInfoData));
  6682. _bill->TxId = txId;
  6683. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6684. {
  6685. _bill->FeeInfo.ConnectionFee = PriceParsing(strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6686. _bill->FeeInfo.SessionFee = PriceParsing(strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6687. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6688. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6689. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6690. }
  6691. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6692. {
  6693. _bill->FeeInfo.TotalCost = ShellFinalCostParsing(strCost);
  6694. GetShellReceiptInfo(strCost, (char *)_bill->Receipt);
  6695. }
  6696. else
  6697. {
  6698. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6699. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6700. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "accountBalance", _PHIHONG_PRICE_SPLIT_KEY);
  6701. _bill->FeeInfo.CostDiscount = PriceParsing(strCost, "couponPoint", _PHIHONG_PRICE_SPLIT_KEY);
  6702. _bill->FeeInfo.RemainAmount = PriceParsing(strCost, "farewellMessage", _PHIHONG_PRICE_SPLIT_KEY);
  6703. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0)
  6704. {
  6705. sprintf(_bill->Receipt, "%s%d",
  6706. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0],
  6707. txId);
  6708. }
  6709. }
  6710. }
  6711. void UpdateCurrentFinalCost(int gunIndex, ChargingBillInfoData *_bill)
  6712. {
  6713. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6714. {
  6715. LOG_INFO("********* Customer Audi Gun %d Final Cost *********", (gunIndex + 1));
  6716. LOG_INFO("********* Connection Fee: [%7.2f]", _bill->FeeInfo.ConnectionFee);
  6717. LOG_INFO("********* Session Fee: [%7.2f]", _bill->FeeInfo.SessionFee);
  6718. LOG_INFO("********* Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6719. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6720. LOG_INFO("********* Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6721. }
  6722. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6723. {
  6724. LOG_INFO("********* Customer Shell Gun %d Final Cost ********", (gunIndex + 1));
  6725. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6726. LOG_INFO("********* Receipt: [%s]", _bill->Receipt);
  6727. }
  6728. else
  6729. {
  6730. LOG_INFO("*********** Standard Gun %d Final Cost ************", (gunIndex + 1));
  6731. LOG_INFO("*********** Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6732. LOG_INFO("*********** Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6733. LOG_INFO("*********** Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6734. LOG_INFO("*********** Coupon Point: [%7.2f]", _bill->FeeInfo.CostDiscount);
  6735. LOG_INFO("*********** Remain Amount: [%7.2f]", _bill->FeeInfo.RemainAmount);
  6736. LOG_INFO("*********** Receipt: [%s]", _bill->Receipt);
  6737. }
  6738. #if 0
  6739. LOG_INFO("********** Connector Id: %d Final Cost **********", (gunIndex + 1));
  6740. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6741. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6742. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6743. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6744. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6745. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6746. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance ||
  6747. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount != CostDiscount)
  6748. {
  6749. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance: %7.2f, Discount: %d.%02d **********",
  6750. (gunIndex + 1), TotalCost, AccountBalance, (CostDiscount / 100), (CostDiscount % 100));
  6751. }
  6752. #endif
  6753. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = _bill->FeeInfo.TotalCost;
  6754. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = _bill->FeeInfo.AccountBalance;
  6755. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount = _bill->FeeInfo.CostDiscount;
  6756. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = _bill->FeeInfo.EnergyCost;
  6757. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = _bill->FeeInfo.ParkingFee;
  6758. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemainAmount = _bill->FeeInfo.RemainAmount;
  6759. Set_Connector_MiscCommand(gunIndex, MISC_CONN_FINAL_COST);
  6760. if(strlen(_bill->Receipt) > 0)
  6761. {
  6762. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], _bill->Receipt);
  6763. Set_Connector_MiscCommand(gunIndex, MISC_CONN_RECEIPT);
  6764. }
  6765. DB_Update_FinalCost_Status(localDb, _bill->TxId, BILL_SATAUS_Bypass_OK);
  6766. }
  6767. bool IsBillStatusChanged(int gunIndex)
  6768. {
  6769. return ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus == ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus ?
  6770. false : true;
  6771. }
  6772. void SyncBillStatus(int gunIndex)
  6773. {
  6774. ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6775. }
  6776. void EnableUndisposedBill(int dispenserIndex)
  6777. {
  6778. LOG_INFO("Enable Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6779. _isUndisposedFinalCost[dispenserIndex] = true;
  6780. }
  6781. void FindUndisposedAndTrigger(int dispenserIndex)
  6782. {
  6783. int txId = 0;
  6784. char strCost[256];
  6785. ChargingBillInfoData _bill;
  6786. bool find = false;
  6787. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6788. {
  6789. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6790. txId = DB_Get_Undisposed_FinalCost(localDb, gun, strCost);
  6791. if(txId >= 0)
  6792. {
  6793. LOG_INFO("Find Gun %d Undisposed ChargingBill, txId: [%d]", gun + 1, txId);
  6794. FinalCostParsing(txId, (char *)strCost, &_bill);
  6795. EnableConnectorBillRequest(gun, &_bill);
  6796. find = true;
  6797. break;
  6798. }
  6799. }
  6800. if(!find)
  6801. {
  6802. //LOG_INFO("Dispenser %d Undisposed ChargingBill Not Found", dispenserIndex + 1);
  6803. _isUndisposedFinalCost[dispenserIndex] = false;
  6804. }
  6805. }
  6806. bool IsDispenserBillStatusBusy(int dispenserIndex)
  6807. {
  6808. bool busy = false;
  6809. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6810. {
  6811. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6812. if(ShmChargerInfo->AllBill.GunBill[gun].BillStatus != _BillStatus_Idle)
  6813. {
  6814. busy = true;
  6815. break;
  6816. }
  6817. }
  6818. return busy;
  6819. }
  6820. void DispenserBillRequestProcess(int dispenserIndex)
  6821. {
  6822. if(IsDispenserIdle(dispenserIndex) && !IsDispenserBillStatusBusy(dispenserIndex))
  6823. {
  6824. if(!_canUpdateFinalCost[dispenserIndex])
  6825. {
  6826. _canUpdateFinalCost[dispenserIndex] = true;
  6827. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6828. }
  6829. if(_canUpdateFinalCost[dispenserIndex] && _isUndisposedFinalCost[dispenserIndex])
  6830. {
  6831. if(GetSecTimeoutValue(_DispenserBill_Time[dispenserIndex]) >= FINAL_COST_UPDATE_TIME)
  6832. {
  6833. //LOG_INFO("Try Find Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6834. FindUndisposedAndTrigger(dispenserIndex);
  6835. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6836. }
  6837. }
  6838. }
  6839. else
  6840. {
  6841. _canUpdateFinalCost[dispenserIndex] = false;
  6842. }
  6843. }
  6844. void GunBillRequestProcess(int gunIndex)
  6845. {
  6846. unsigned char _status = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6847. switch(_status)
  6848. {
  6849. case _BillStatus_Idle:
  6850. if(IsBillStatusChanged(gunIndex))
  6851. {
  6852. SyncBillStatus(gunIndex);
  6853. }
  6854. break;
  6855. case _BillStatus_MiscReq:
  6856. if(IsBillStatusChanged(gunIndex))
  6857. {
  6858. Database_INFO("Gun %d ChargingBill MiscRequest", gunIndex + 1);
  6859. SyncBillStatus(gunIndex);
  6860. }
  6861. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_WaitMiscClean;
  6862. Set_Connector_MiscCommand(gunIndex, MISC_CONN_CHARGING_BILL);
  6863. break;
  6864. case _BillStatus_WaitMiscClean:
  6865. if(IsBillStatusChanged(gunIndex))
  6866. {
  6867. SyncBillStatus(gunIndex);
  6868. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6869. }
  6870. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6871. {
  6872. Database_INFO("Gun %d ChargingBill WaitMisc Timeout", gunIndex + 1);
  6873. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6874. }
  6875. break;
  6876. case _BillStatus_MiscClean:
  6877. if(IsBillStatusChanged(gunIndex))
  6878. {
  6879. Database_INFO("Gun %d ChargingBill MiscClean", gunIndex + 1);
  6880. SyncBillStatus(gunIndex);
  6881. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6882. }
  6883. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6884. {
  6885. Database_INFO("Gun %d ChargingBill Wait Dispenser Timeout", gunIndex + 1);
  6886. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt++;
  6887. if(ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt >= MAX_FINAL_COST_MISC_COUNT)
  6888. {
  6889. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6890. }
  6891. else
  6892. {
  6893. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6894. }
  6895. }
  6896. break;
  6897. case _BillStatus_UpdateDone:
  6898. if(IsBillStatusChanged(gunIndex))
  6899. {
  6900. Database_INFO("Gun %d ChargingBill UpdateDone", gunIndex + 1);
  6901. SyncBillStatus(gunIndex);
  6902. }
  6903. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Completed;
  6904. break;
  6905. case _BillStatus_Completed:
  6906. if(IsBillStatusChanged(gunIndex))
  6907. {
  6908. SyncBillStatus(gunIndex);
  6909. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6910. }
  6911. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6912. {
  6913. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_End;
  6914. }
  6915. break;
  6916. case _BillStatus_Fail:
  6917. if(IsBillStatusChanged(gunIndex))
  6918. {
  6919. SyncBillStatus(gunIndex);
  6920. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_FAIL);
  6921. }
  6922. DisableConnectorBill(gunIndex);
  6923. break;
  6924. case _BillStatus_End:
  6925. if(IsBillStatusChanged(gunIndex))
  6926. {
  6927. SyncBillStatus(gunIndex);
  6928. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_OK);
  6929. }
  6930. DisableConnectorBill(gunIndex);
  6931. break;
  6932. default:
  6933. DisableConnectorBill(gunIndex);
  6934. break;
  6935. }
  6936. }
  6937. void FinalCostHandler(void)
  6938. {
  6939. int txId = 0;
  6940. char strCost[256];
  6941. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6942. {
  6943. txId = Get_Ocpp_FinalCostTxId(i);
  6944. Get_Ocpp_FinalCost(i, strCost);
  6945. if(strlen((char *)strCost) > 0)
  6946. {
  6947. DB_Insert_FinalCost(localDb, i, txId, strCost);
  6948. Database_INFO("Insert FinalCost: Gun %d TxId[%d]", i + 1, txId);
  6949. if(ShmChargerInfo->UserTransaction[i].TransactionId == txId)
  6950. {
  6951. ChargingBillInfoData _bill;
  6952. FinalCostParsing(txId, (char *)strCost, &_bill);
  6953. UpdateCurrentFinalCost(i, &_bill);
  6954. }
  6955. else
  6956. {
  6957. EnableUndisposedBill(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex);
  6958. }
  6959. Clean_Ocpp_FinalCost(i);
  6960. }
  6961. }
  6962. if(ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill)
  6963. {
  6964. ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill = false;
  6965. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6966. {
  6967. _isUndisposedFinalCost[i] = true;
  6968. }
  6969. }
  6970. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6971. {
  6972. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  6973. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  6974. {
  6975. DispenserBillRequestProcess(i);
  6976. }
  6977. else
  6978. {
  6979. _canUpdateFinalCost[i] = false;
  6980. }
  6981. }
  6982. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6983. {
  6984. GunBillRequestProcess(i);
  6985. }
  6986. }
  6987. char MyTimeOffsetString[501];
  6988. void TimeOffsetHandler(void)
  6989. {
  6990. char timeOffset[501];
  6991. Get_Ocpp_TimeOffset((char *)timeOffset);
  6992. if(strcmp(MyTimeOffsetString, (char *)timeOffset) != EQUAL)
  6993. {
  6994. BOOL trigger = FALSE;
  6995. int offset = 0;
  6996. offset = TimeOffsetKeyParsing(timeOffset);
  6997. LOG_INFO("********** Time Offset String [%s] -> Value [%d] -> Time [%s%02d:%02d]",
  6998. timeOffset, offset, offset >= 0 ? "+" : "-", (abs(offset) / 60), (abs(offset) % 60));
  6999. ShmChargerInfo->CabinetMiscValue.TimeOffset = offset;
  7000. #if 0
  7001. time_t timep;
  7002. struct tm *tm;
  7003. time(&timep);
  7004. tm = localtime(&timep);
  7005. LOG_INFO(" NowTime: %d/%02d/%02d %02d:%02d:%02d",
  7006. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  7007. timep += (offset * 60);
  7008. tm = localtime(&timep);
  7009. LOG_INFO("OffsetTime: %d/%02d/%02d %02d:%02d:%02d",
  7010. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  7011. #endif
  7012. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7013. {
  7014. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  7015. {
  7016. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET))
  7017. {
  7018. trigger = TRUE;
  7019. Set_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET);
  7020. }
  7021. }
  7022. }
  7023. if(trigger)
  7024. {
  7025. LOG_INFO("********** Trigger Dispenser Time Offset **********");
  7026. }
  7027. strcpy(MyTimeOffsetString, timeOffset);
  7028. }
  7029. }
  7030. struct timespec _Cost_Price_time;
  7031. void CheckOcppCostAndPrice(void)
  7032. {
  7033. // 5s for reboot request timeout
  7034. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  7035. {
  7036. GetClockTime(&_Cost_Price_time);
  7037. DefaultPriceHandler();
  7038. UserPriceHandler();
  7039. RunningCostHandler();
  7040. TimeOffsetHandler();
  7041. }
  7042. FinalCostHandler();
  7043. }
  7044. void CheckRemoteStartNoIdRequest(void)
  7045. {
  7046. if(Is_Ocpp_RemoteStartWaitReq())
  7047. {
  7048. if(!ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  7049. {
  7050. LOG_INFO("Ocpp remote start without connector id is trigger");
  7051. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = true;
  7052. AnnounceRemoteStartNoID();
  7053. GetClockTime(&_RemoteStartNoId_Time);
  7054. }
  7055. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  7056. {
  7057. // to avoid keep RemoteStartNoId state when ocpp is disconnected
  7058. if((GetTimeoutValue(_RemoteStartNoId_Time) / uSEC_VAL) >= (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + CONNECTION_TIME_OFFSET))
  7059. {
  7060. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  7061. }
  7062. }
  7063. }
  7064. else
  7065. {
  7066. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  7067. {
  7068. LOG_INFO("Ocpp remote start without connector id is expired");
  7069. }
  7070. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  7071. }
  7072. }
  7073. void CheckOcppPageChangeRequest(void)
  7074. {
  7075. int gunId = 0, pageIndex = 0;
  7076. if(Is_Ocpp_LcdOverideReq(&gunId, &pageIndex))
  7077. {
  7078. Clean_Ocpp_LcdOverideReq();
  7079. LOG_INFO("Gun %d Ocpp Line Status [%d] is trigger", gunId, pageIndex);
  7080. AnnounceLineStatusCode(gunId - 1, pageIndex);
  7081. }
  7082. }
  7083. void CheckLcmPageChangeRequest(void)
  7084. {
  7085. bool isTrigger = false;
  7086. CheckRemoteStartNoIdRequest();
  7087. CheckOcppPageChangeRequest();
  7088. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  7089. {
  7090. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  7091. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  7092. {
  7093. isTrigger = false;
  7094. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  7095. {
  7096. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  7097. if(ShmChargerInfo->ConnectorPageReq[gun].CtrlValue != 0)
  7098. {
  7099. isTrigger = true;
  7100. break;
  7101. }
  7102. }
  7103. if(isTrigger)
  7104. {
  7105. Set_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  7106. }
  7107. else
  7108. {
  7109. Clean_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  7110. }
  7111. }
  7112. }
  7113. }
  7114. void CheckStationInfo(void)
  7115. {
  7116. int station_id = 0;
  7117. char station_name[64];
  7118. if(Is_Ocpp_ChargerStationReq())
  7119. {
  7120. memset(station_name, 0x00, sizeof(station_name));
  7121. Get_Ocpp_ChargerStationInfo(&station_id, station_name);
  7122. if(strcmp(station_name, ShmChargerInfo->StationInfo.StationName) != EQUAL ||
  7123. ShmChargerInfo->StationInfo.StationID != station_id)
  7124. {
  7125. strcpy(ShmChargerInfo->StationInfo.StationName, station_name);
  7126. ShmChargerInfo->StationInfo.StationID = station_id;
  7127. LOG_INFO("Station ID: %d, Name: %s", ShmChargerInfo->StationInfo.StationID, ShmChargerInfo->StationInfo.StationName);
  7128. AnnounceStationInfo();
  7129. }
  7130. Clean_Ocpp_ChargerStationReq();
  7131. }
  7132. }
  7133. void CheckWeatherInfo(void)
  7134. {
  7135. int weatherID = 0;
  7136. float temperature = 0;
  7137. char strWeather[16];
  7138. 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};
  7139. if(Is_Ocpp_WeatherReq())
  7140. {
  7141. Get_Ocpp_WeatherInfo(&weatherID, &temperature);
  7142. if(weatherID != ShmChargerInfo->StationInfo.WeatherID ||
  7143. temperature != ShmChargerInfo->StationInfo.Temperature)
  7144. {
  7145. ShmChargerInfo->StationInfo.WeatherID = weatherID;
  7146. ShmChargerInfo->StationInfo.Temperature = temperature;
  7147. if(ShmChargerInfo->StationInfo.WeatherID <= _WEATHER_Mist)
  7148. {
  7149. strcpy(strWeather, str_weather_type[ShmChargerInfo->StationInfo.WeatherID]);
  7150. }
  7151. else
  7152. {
  7153. sprintf(strWeather, "%d", ShmChargerInfo->StationInfo.WeatherID);
  7154. }
  7155. LOG_INFO("Weather: [%s][%d], Temperature: %4.1f", strWeather, ShmChargerInfo->StationInfo.WeatherID, ShmChargerInfo->StationInfo.Temperature);
  7156. AnnounceStationInfo();
  7157. }
  7158. Clean_Ocpp_WeatherReq();
  7159. }
  7160. }
  7161. bool _WriteDeductBusy[CONNECTOR_QUANTITY] = {0};
  7162. void CheckDeductResultRequest(void)
  7163. {
  7164. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7165. char cardNo[21];
  7166. char appNo[10];
  7167. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7168. {
  7169. if(Is_DeductResultReq(i))
  7170. {
  7171. if(!_WriteDeductBusy[i])
  7172. {
  7173. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7174. {
  7175. _WriteDeductBusy[i] = true;
  7176. //Set_Ocpp_DeductTransaction(_BackupDeductResult[i].TransactionId);
  7177. //Set_Ocpp_DeductCreditNo((char *)&_BackupDeductResult[i].creditNo);
  7178. //Set_Ocpp_DeductVemData("");
  7179. //Set_Ocpp_DeductAmount(((double)_BackupDeductResult[i].DeductAmount / 100));
  7180. //Set_Ocpp_DeductResult(_BackupDeductResult[i].DeductResult);
  7181. //Set_Ocpp_DonateInvoice(_BackupDeductResult[i].IsDonateInvoice);
  7182. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->DeductResultReq[i]))
  7183. {
  7184. memset(cardNo, 0x00, sizeof(cardNo));
  7185. memcpy(cardNo, (char *)&ShmChargerInfo->DeductResultReq[i].creditNo[0], sizeof(ShmChargerInfo->DeductResultReq[i].creditNo));
  7186. memset(appNo, 0x00, sizeof(appNo));
  7187. memcpy(appNo, (char *)&ShmChargerInfo->DeductResultReq[i].ApprovalNumber[0], sizeof(ShmChargerInfo->DeductResultReq[i].ApprovalNumber));
  7188. Set_Ocpp_ReportCreditDeductReq();
  7189. LOG_INFO("DeductReport Gun %d TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7190. i + 1,
  7191. ShmChargerInfo->DeductResultReq[i].TransactionId,
  7192. cardNo,
  7193. appNo,
  7194. (ShmChargerInfo->DeductResultReq[i].DeductAmount / 100),
  7195. (ShmChargerInfo->DeductResultReq[i].DeductAmount % 100),
  7196. str_ReaderStatus[ShmChargerInfo->DeductResultReq[i].DeductResult],
  7197. ShmChargerInfo->DeductResultReq[i].IsDonateInvoice ? "Yes" : "No");
  7198. if(ShmChargerInfo->DeductResultReq[i].DeductResult == _Deduct_PreAuth_Pass)
  7199. {
  7200. memcpy(&ShmChargerInfo->ReaderStatus[i], &ShmChargerInfo->DeductResultReq[i], sizeof(DeductResultInfoData));
  7201. ShmChargerInfo->ReaderStatus[i].ReaderStatusReq = YES;
  7202. }
  7203. }
  7204. }
  7205. }
  7206. else
  7207. {
  7208. if(!Is_Ocpp_ReportCreditDeductReq())
  7209. {
  7210. Clean_DeductInfo(i);
  7211. _WriteDeductBusy[i] = false;
  7212. }
  7213. }
  7214. }
  7215. else
  7216. {
  7217. _WriteDeductBusy[i] = false;
  7218. }
  7219. }
  7220. }
  7221. bool _WriteReDeductBusy = false;
  7222. void CheckReDeductRequest(void)
  7223. {
  7224. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7225. char cardNo[21];
  7226. char appNo[10];
  7227. if(ShmChargerInfo->ReDeductReq.DeductReq)
  7228. {
  7229. if(!_WriteReDeductBusy)
  7230. {
  7231. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7232. {
  7233. _WriteReDeductBusy = true;
  7234. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->ReDeductReq))
  7235. {
  7236. memset(cardNo, 0x00, sizeof(cardNo));
  7237. memcpy(cardNo, (char *)&ShmChargerInfo->ReDeductReq.creditNo[0], sizeof(ShmChargerInfo->ReDeductReq.creditNo));
  7238. memset(appNo, 0x00, sizeof(appNo));
  7239. memcpy(appNo, (char *)&ShmChargerInfo->ReDeductReq.ApprovalNumber[0], sizeof(ShmChargerInfo->ReDeductReq.ApprovalNumber));
  7240. Set_Ocpp_ReportCreditDeductReq();
  7241. LOG_INFO("ReDeductReport TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7242. ShmChargerInfo->ReDeductReq.TransactionId,
  7243. cardNo,
  7244. appNo,
  7245. (ShmChargerInfo->ReDeductReq.DeductAmount / 100),
  7246. (ShmChargerInfo->ReDeductReq.DeductAmount % 100),
  7247. str_ReaderStatus[ShmChargerInfo->ReDeductReq.DeductResult],
  7248. ShmChargerInfo->ReDeductReq.IsDonateInvoice ? "Yes" : "No");
  7249. if(ShmChargerInfo->ReDeductReq.DeductResult == _Deduct_PreAuth_Pass && ShmChargerInfo->ReDeductReq.ConnectorId != 0xFF)
  7250. {
  7251. int gunIndex = ShmChargerInfo->ReDeductReq.ConnectorId - 1;
  7252. memcpy(&ShmChargerInfo->ReaderStatus[gunIndex], &ShmChargerInfo->ReDeductReq, sizeof(DeductResultInfoData));
  7253. ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq = YES;
  7254. }
  7255. }
  7256. }
  7257. }
  7258. else
  7259. {
  7260. if(!Is_Ocpp_ReportCreditDeductReq())
  7261. {
  7262. memset(&ShmChargerInfo->ReDeductReq, 0x00, sizeof(DeductResultInfoData));
  7263. _WriteReDeductBusy = false;
  7264. }
  7265. }
  7266. }
  7267. else
  7268. {
  7269. _WriteReDeductBusy = false;
  7270. }
  7271. }
  7272. bool _WriteReaderStatusBusy[CONNECTOR_QUANTITY] = {0};
  7273. void CheckReaderStatusRequest(void)
  7274. {
  7275. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7276. char cardNo[21];
  7277. char appNo[10];
  7278. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7279. {
  7280. if(Is_ReaderStatusReq(i))
  7281. {
  7282. if(!_WriteReaderStatusBusy[i])
  7283. {
  7284. if(!Is_Ocpp_TcciReaderStatusReq(i) && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7285. {
  7286. _WriteReaderStatusBusy[i] = true;
  7287. if(Set_Ocpp_TcciReaderStatus(i, &ShmChargerInfo->ReaderStatus[i]))
  7288. {
  7289. memset(cardNo, 0x00, sizeof(cardNo));
  7290. memcpy(cardNo, (char *)&ShmChargerInfo->ReaderStatus[i].creditNo[0], sizeof(ShmChargerInfo->ReaderStatus[i].creditNo));
  7291. memset(appNo, 0x00, sizeof(appNo));
  7292. memcpy(appNo, (char *)&ShmChargerInfo->ReaderStatus[i].ApprovalNumber[0], sizeof(ShmChargerInfo->ReaderStatus[i].ApprovalNumber));
  7293. Set_Ocpp_TcciReaderStatusReq(i);
  7294. LOG_INFO("StatusReport Gun %d: [%s], CreditNo: [%s], ApprovalNo: [%s]",
  7295. i + 1, str_ReaderStatus[ShmChargerInfo->ReaderStatus[i].DeductResult], cardNo, appNo);
  7296. }
  7297. }
  7298. }
  7299. else
  7300. {
  7301. if(!Is_Ocpp_TcciReaderStatusReq(i))
  7302. {
  7303. Clean_ReaderStatusInfo(i);
  7304. _WriteReaderStatusBusy[i] = false;
  7305. }
  7306. }
  7307. }
  7308. else
  7309. {
  7310. _WriteReaderStatusBusy[i] = false;
  7311. }
  7312. }
  7313. }
  7314. void CheckOccupancyPrice(void)
  7315. {
  7316. float _price = 0;
  7317. if(Is_Ocpp_TcciSetOccupancyPriceReq())
  7318. {
  7319. _price = Get_Ocpp_TcciOccupancyPrice();
  7320. if(ShmChargerInfo->CabinetMiscValue.ParkingPrice != _price)
  7321. {
  7322. ShmChargerInfo->CabinetMiscValue.ParkingPrice = _price;
  7323. LOG_INFO("Set OccupancyPrice: [%6.2f]", ShmChargerInfo->CabinetMiscValue.ParkingPrice);
  7324. AnnounceOccupancyPrice();
  7325. }
  7326. Clean_Ocpp_TcciSetOccupancyPriceReq();
  7327. }
  7328. }
  7329. bool IsParkingButtonStatusChanged(int gunIndex)
  7330. {
  7331. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus ?
  7332. false : true;
  7333. }
  7334. void SyncParkingButtonStatus(int gunIndex)
  7335. {
  7336. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7337. }
  7338. int OccupancyButtonSync(int gunIndex)
  7339. {
  7340. int _pass = FAIL;
  7341. unsigned char _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7342. switch(_status)
  7343. {
  7344. case _BillStatus_Idle:
  7345. if(IsParkingButtonStatusChanged(gunIndex))
  7346. {
  7347. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7348. SyncParkingButtonStatus(gunIndex);
  7349. }
  7350. break;
  7351. case _BillStatus_MiscReq:
  7352. if(IsParkingButtonStatusChanged(gunIndex))
  7353. {
  7354. Occupancy_INFO("Gun %d OccupancyButton [MiscRequest]", gunIndex + 1);
  7355. SyncParkingButtonStatus(gunIndex);
  7356. }
  7357. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_WaitMiscClean;
  7358. Set_Connector_MiscCommand(gunIndex, MISC_CONN_PARKING_STATUS);
  7359. break;
  7360. case _BillStatus_WaitMiscClean:
  7361. if(IsParkingButtonStatusChanged(gunIndex))
  7362. {
  7363. SyncParkingButtonStatus(gunIndex);
  7364. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7365. }
  7366. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7367. {
  7368. Occupancy_INFO("Gun %d OccupancyButton [WaitMisc Timeout]", gunIndex + 1);
  7369. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7370. }
  7371. break;
  7372. case _BillStatus_MiscClean:
  7373. if(IsParkingButtonStatusChanged(gunIndex))
  7374. {
  7375. Occupancy_INFO("Gun %d OccupancyButton [MiscClean]", gunIndex + 1);
  7376. SyncParkingButtonStatus(gunIndex);
  7377. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7378. }
  7379. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7380. {
  7381. LOG_INFO("Gun %d OccupancyButton [Wait Timeout]", gunIndex + 1);
  7382. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7383. }
  7384. break;
  7385. case _BillStatus_UpdateDone:
  7386. if(IsParkingButtonStatusChanged(gunIndex))
  7387. {
  7388. Occupancy_INFO("Gun %d OccupancyButton [UpdateDone]", gunIndex + 1);
  7389. SyncParkingButtonStatus(gunIndex);
  7390. }
  7391. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_Idle;
  7392. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7393. _pass = PASS;
  7394. break;
  7395. default:
  7396. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7397. break;
  7398. }
  7399. return _pass;
  7400. }
  7401. int NotifyOccupancyFeeActRequest(int gunIndex, int action)
  7402. {
  7403. char *str_notify_occupancy_act[] = {STR_NOTIFY_NONE, STR_GET_OCCUPANCY_FEE, STR_CANCEL_OCCUPANCY_DEDUCT, STR_CANCEL_OCCUPANCY_FEE};
  7404. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_None)
  7405. {
  7406. if(!Is_Ocpp_TcciOccupancyFeeActReq(gunIndex))
  7407. {
  7408. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7409. Set_Ocpp_TcciOccupancyFeeAct(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill, action);
  7410. _notifyOccupancyFeeAct[gunIndex] = action;
  7411. LOG_INFO("Gun %d Set OccupancyFee Action [%s] Request", gunIndex + 1, str_notify_occupancy_act[action]);
  7412. }
  7413. }
  7414. else
  7415. {
  7416. if(!Is_Ocpp_TcciOccupancyFeeActReq(gunIndex))
  7417. {
  7418. if(Is_Ocpp_TcciOccupancyFeeActStatus(gunIndex, "Accepted"))
  7419. {
  7420. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_GetOccupancyFee)
  7421. {
  7422. Get_Ocpp_TcciGetOccupancyFeeResponse(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill);
  7423. LOG_INFO("Gun %d Get Occupancy Fee: [%.2f], Duration: [%d], StartTime: [%s]",
  7424. gunIndex + 1,
  7425. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Fee,
  7426. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Duration,
  7427. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.StartTime);
  7428. }
  7429. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_CancelLocalDeduction)
  7430. {
  7431. LOG_INFO("Gun %d Occupancy Deduction Stopped", gunIndex + 1);
  7432. }
  7433. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_CancelOccupancyFee)
  7434. {
  7435. LOG_INFO("Gun %d Occupancy Fee Force Clean", gunIndex + 1);
  7436. }
  7437. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7438. return PASS;
  7439. }
  7440. else
  7441. {
  7442. LOG_INFO("Gun %d OccupancyFee Action [%s] Rejected", gunIndex + 1, str_notify_occupancy_act[action]);
  7443. return FAIL;
  7444. }
  7445. }
  7446. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7447. {
  7448. // shall not reach here
  7449. Clean_Ocpp_TcciNotifyOccupancyFeeActReq(gunIndex);
  7450. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7451. LOG_INFO("Gun %d OccupancyFee Action [%s] Request Timeout", gunIndex + 1, str_notify_occupancy_act[action]);
  7452. return FAIL;
  7453. }
  7454. }
  7455. return WAIT;
  7456. }
  7457. bool IsParkingStatusChanged(int gunIndex)
  7458. {
  7459. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus ?
  7460. false : true;
  7461. }
  7462. void SyncParkingStatus(int gunIndex)
  7463. {
  7464. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7465. }
  7466. bool Is_Occupancy_Cancel(int gunIndex)
  7467. {
  7468. unsigned char dispenser = 0;
  7469. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParentDispensetIndex;
  7470. if((!ShmSysConfigAndInfo->SysInfo.OcppConnStatus && !ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq) ||
  7471. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_None ||
  7472. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_Timeout)
  7473. {
  7474. return true;
  7475. }
  7476. return false;
  7477. }
  7478. void OccupancyRequest(int gunIndex)
  7479. {
  7480. int _action = 0;
  7481. bool feeRequest = false;
  7482. bool keepCounting = false;
  7483. int res = FAIL;
  7484. char _sn[37];
  7485. unsigned char _status = 0;
  7486. 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};
  7487. _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7488. if(Is_Ocpp_TcciOccupancyDisplayReq(gunIndex))
  7489. {
  7490. memset(_sn, 0x00, sizeof(_sn));
  7491. _action = Get_Ocpp_TcciOccupancyAction(gunIndex, sizeof(_sn) - 1, _sn);
  7492. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ActionId = _action;
  7493. LOG_INFO("Gun %d OccupancyAction is [%s]", gunIndex + 1, str_action[_action]);
  7494. Clean_Ocpp_TcciOccupancyDisplayReq(gunIndex);
  7495. }
  7496. if(ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest)
  7497. {
  7498. feeRequest = true;
  7499. ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest =false;
  7500. }
  7501. if(Is_OccupancyKeepCountingReq(gunIndex))
  7502. {
  7503. if(_status == _Parking_WaitToPay)
  7504. {
  7505. keepCounting = true;
  7506. }
  7507. else
  7508. {
  7509. Clean_OccupancyKeepCountingReq(gunIndex);
  7510. }
  7511. }
  7512. if(_status != _Parking_NoOccupancy && chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7513. {
  7514. if(!Is_OccupancyFeeForceCancelReq(gunIndex))
  7515. {
  7516. Set_OccupancyFeeForceCancelReq(gunIndex);
  7517. Occupancy_INFO("Gun %d Need Force Cancel OccupancyStatus", gunIndex + 1);
  7518. }
  7519. }
  7520. if(Is_OccupancyFeeForceCancelReq(gunIndex))
  7521. {
  7522. res = NotifyOccupancyFeeActRequest(gunIndex, _Notify_CancelOccupancyFee);
  7523. if(res == PASS || res == FAIL)
  7524. {
  7525. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7526. Clean_OccupancyFeeForceCancelReq(gunIndex);
  7527. return;
  7528. }
  7529. else
  7530. {
  7531. // wait response
  7532. }
  7533. }
  7534. switch(_status)
  7535. {
  7536. case _Parking_NoOccupancy:
  7537. if(IsParkingStatusChanged(gunIndex))
  7538. {
  7539. Occupancy_INFO("Gun %d OccupancyStatus [Clean]", gunIndex + 1);
  7540. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex], 0x00, sizeof(OccupancyInfoData));
  7541. SyncParkingStatus(gunIndex);
  7542. }
  7543. if(_action == _ParkingAction_DisplayButton)
  7544. {
  7545. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7546. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_SyncOccupancy;
  7547. }
  7548. break;
  7549. case _Parking_SyncOccupancy:
  7550. if(IsParkingStatusChanged(gunIndex))
  7551. {
  7552. Occupancy_INFO("Gun %d OccupancyStatus [Sync]", gunIndex + 1);
  7553. SyncParkingStatus(gunIndex);
  7554. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7555. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7556. }
  7557. if(_action == _ParkingAction_DisplayButton)
  7558. {
  7559. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7560. }
  7561. else if(_action == _ParkingAction_CancelOccupancy ||
  7562. Is_Occupancy_Cancel(gunIndex))
  7563. {
  7564. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7565. }
  7566. else if(_action == _ParkingAction_PaidOnlineOK)
  7567. {
  7568. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7569. }
  7570. else
  7571. {
  7572. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  7573. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  7574. {
  7575. if(OccupancyButtonSync(gunIndex) == PASS)
  7576. {
  7577. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7578. }
  7579. }
  7580. }
  7581. break;
  7582. case _Parking_Occupied:
  7583. if(IsParkingStatusChanged(gunIndex))
  7584. {
  7585. Occupancy_INFO("Gun %d OccupancyStatus [Start]", gunIndex + 1);
  7586. SyncParkingStatus(gunIndex);
  7587. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7588. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7589. }
  7590. if(_action == _ParkingAction_DisplayButton)
  7591. {
  7592. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7593. }
  7594. else if(_action == _ParkingAction_CancelOccupancy ||
  7595. Is_Occupancy_Cancel(gunIndex))
  7596. {
  7597. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7598. }
  7599. else if(_action == _ParkingAction_PaidOnlineOK)
  7600. {
  7601. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7602. }
  7603. else if(_action == _ParkingAction_PaidOnlineFail)
  7604. {
  7605. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7606. }
  7607. else
  7608. {
  7609. if(feeRequest)
  7610. {
  7611. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_RequestFee;
  7612. }
  7613. }
  7614. break;
  7615. case _Parking_RequestFee:
  7616. if(IsParkingStatusChanged(gunIndex))
  7617. {
  7618. Occupancy_INFO("Gun %d OccupancyStatus [RequestFee]", gunIndex + 1);
  7619. SyncParkingStatus(gunIndex);
  7620. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7621. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7622. }
  7623. if(_action == _ParkingAction_CancelOccupancy ||
  7624. Is_Occupancy_Cancel(gunIndex))
  7625. {
  7626. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7627. }
  7628. else if(_action == _ParkingAction_PaidOnlineOK)
  7629. {
  7630. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7631. }
  7632. else if(_action == _ParkingAction_PaidOnlineFail)
  7633. {
  7634. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7635. }
  7636. else
  7637. {
  7638. if(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq)
  7639. {
  7640. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7641. }
  7642. else
  7643. {
  7644. res = NotifyOccupancyFeeActRequest(gunIndex, _Notify_GetOccupancyFee);
  7645. if(res == PASS)
  7646. {
  7647. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7648. }
  7649. else if(res == FAIL)
  7650. {
  7651. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7652. }
  7653. else
  7654. {
  7655. // wait response
  7656. }
  7657. }
  7658. }
  7659. break;
  7660. case _Parking_WaitToPay:
  7661. if(IsParkingStatusChanged(gunIndex))
  7662. {
  7663. Occupancy_INFO("Gun %d OccupancyStatus [WaitToPay]", gunIndex + 1);
  7664. SyncParkingStatus(gunIndex);
  7665. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].BillReqStatus, 0x00, sizeof(MiscRequestStatus));
  7666. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7667. }
  7668. if(_action == _ParkingAction_CancelOccupancy ||
  7669. Is_Occupancy_Cancel(gunIndex))
  7670. {
  7671. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7672. }
  7673. else if(_action == _ParkingAction_PaidOnlineOK)
  7674. {
  7675. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7676. }
  7677. else if(_action == _ParkingAction_PaidOnlineFail)
  7678. {
  7679. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7680. }
  7681. else if(keepCounting)
  7682. {
  7683. res = NotifyOccupancyFeeActRequest(gunIndex, _Notify_CancelLocalDeduction);
  7684. if(res == PASS)
  7685. {
  7686. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7687. Clean_OccupancyKeepCountingReq(gunIndex);
  7688. }
  7689. else if(res == FAIL)
  7690. {
  7691. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7692. Clean_OccupancyKeepCountingReq(gunIndex);
  7693. }
  7694. else
  7695. {
  7696. // wait response
  7697. }
  7698. }
  7699. break;
  7700. case _Parking_PaidOnlineOK:
  7701. if(IsParkingStatusChanged(gunIndex))
  7702. {
  7703. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineOK]", gunIndex + 1);
  7704. SyncParkingStatus(gunIndex);
  7705. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7706. }
  7707. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7708. {
  7709. LOG_INFO("Gun %d Reset OccupancyStatus via PaidOnlineOK", gunIndex + 1);
  7710. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7711. }
  7712. break;
  7713. case _Parking_PaidOnlineFail:
  7714. if(IsParkingStatusChanged(gunIndex))
  7715. {
  7716. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineFail]", gunIndex + 1);
  7717. SyncParkingStatus(gunIndex);
  7718. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7719. }
  7720. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7721. {
  7722. LOG_INFO("Gun %d Return to Occupied via PaidOnlineFail", gunIndex + 1);
  7723. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7724. }
  7725. break;
  7726. default:
  7727. DisableConnectorBill(gunIndex);
  7728. break;
  7729. }
  7730. }
  7731. void CheckOccupancyFee(void)
  7732. {
  7733. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7734. {
  7735. OccupancyRequest(i);
  7736. }
  7737. }
  7738. #define OCCUPANCY_DEDUCT_IDLE 0
  7739. #define OCCUPANCY_DEDUCT_REQUEST 1
  7740. unsigned char _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7741. int _OccupancyReportGunId = 0;
  7742. void CheckOccupancyDeduct(void)
  7743. {
  7744. char cardNo[21];
  7745. char appNo[10];
  7746. if(_OccupancyDeductStatus == OCCUPANCY_DEDUCT_IDLE)
  7747. {
  7748. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7749. {
  7750. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7751. {
  7752. if(Is_OccupancyDeductReq(i))
  7753. {
  7754. if(ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyReq.bits.SelfReq &&
  7755. ShmChargerInfo->AllBill.OccupancyInfo[i].Status == _Parking_WaitToPay)
  7756. {
  7757. ShmChargerInfo->AllBill.OccupancyInfo[i].Status = _Parking_NoOccupancy;
  7758. LOG_INFO("Gun %d Occupancy Concluded!", i + 1);
  7759. break;
  7760. }
  7761. Set_Ocpp_OccupancyDeductResult(&ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyDeduct, YES);
  7762. Set_Ocpp_TcciReportOccupancyDeductReq();
  7763. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_REQUEST;
  7764. _OccupancyReportGunId = i + 1;
  7765. Clean_OccupancyDeductReq(i);
  7766. break;
  7767. }
  7768. }
  7769. if(_OccupancyDeductStatus != OCCUPANCY_DEDUCT_IDLE)
  7770. {
  7771. memset(cardNo, 0x00, sizeof(cardNo));
  7772. memset(appNo, 0x00, sizeof(appNo));
  7773. memcpy(cardNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo));
  7774. memcpy(appNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber));
  7775. LOG_INFO("Gun %d Occupancy Deduct [Request] Sn: [%s], Fee: [%.2f], CreditNo: [%s], ApprovalNo: [%s]",
  7776. _OccupancyReportGunId,
  7777. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Sn,
  7778. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Fee,
  7779. cardNo,
  7780. appNo);
  7781. }
  7782. }
  7783. }
  7784. else
  7785. {
  7786. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7787. {
  7788. if(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status == _Parking_WaitToPay)
  7789. {
  7790. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status = _Parking_NoOccupancy;
  7791. LOG_INFO("Gun %d OccupancyDeduct Request Completed!", _OccupancyReportGunId);
  7792. }
  7793. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7794. }
  7795. }
  7796. }
  7797. void CheckDiagnosticsReq(void)
  7798. {
  7799. bool _DiagnosticsCompleted = true;
  7800. int _DispenserId = 0;
  7801. char _IpAddress[32];
  7802. char cmdBuf[256] = {0};
  7803. if(Is_Ocpp_GetDiagnosticsReq() || ShmChargerInfo->Control.Diagnostics.DiagnosticsType != DIAGNOSTICS_TYPE_NONE)
  7804. {
  7805. // check dispenser status and set diagnostics request
  7806. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  7807. {
  7808. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  7809. {
  7810. _DispenserId = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex + 1;
  7811. if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_DispenserId - 1].bits.Request)
  7812. {
  7813. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_DispenserId - 1].CtrlValue = 0;
  7814. sprintf((char*)_IpAddress, "%d.%d.%d.%d",
  7815. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  7816. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  7817. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  7818. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  7819. sprintf(cmdBuf, "/root/Module_Diagnostics %d %s &", _DispenserId, _IpAddress);
  7820. system(cmdBuf);
  7821. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[_DispenserId - 1].bits.Request = true;
  7822. }
  7823. if(!_NeedDiagnostics)
  7824. {
  7825. GetClockTime(&_Diagnostics_Time);
  7826. LOG_INFO("Dispenser Need Diagnostics!");
  7827. }
  7828. _NeedDiagnostics = true;
  7829. }
  7830. }
  7831. if(_NeedDiagnostics)
  7832. {
  7833. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  7834. {
  7835. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  7836. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  7837. {
  7838. if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Stop)
  7839. {
  7840. if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Confirm)
  7841. {
  7842. if(GetSecTimeoutValue(_Diagnostics_Time) >= WAIT_DIAGNOSTICS_CONFIRM)
  7843. {
  7844. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Stop = true;
  7845. LOG_INFO("Wait Dispenser %d diagnostics confirm timeout!", i + 1);
  7846. }
  7847. _DiagnosticsCompleted = false;
  7848. }
  7849. else if(!ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Completed)
  7850. {
  7851. if(GetSecTimeoutValue(_Diagnostics_Time) >= WAIT_DIAGNOSTICS_TIMEOUT)
  7852. {
  7853. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].bits.Stop = true;
  7854. LOG_INFO("Wait Dispenser %d diagnostics completed timeout!", i + 1);
  7855. }
  7856. _DiagnosticsCompleted = false;
  7857. }
  7858. }
  7859. }
  7860. }
  7861. }
  7862. if(_DiagnosticsCompleted)
  7863. {
  7864. // diagnostics completed, clean ocpp GetDiagnosticsReq
  7865. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  7866. {
  7867. ShmChargerInfo->Control.Diagnostics.DispenserDiagnostics[i].CtrlValue = 0;
  7868. }
  7869. if(Is_Ocpp_GetDiagnosticsReq())
  7870. {
  7871. Clean_Ocpp_GetDiagnosticsReq();
  7872. }
  7873. if(ShmChargerInfo->Control.Diagnostics.DiagnosticsType != DIAGNOSTICS_TYPE_NONE)
  7874. {
  7875. ShmChargerInfo->Control.Diagnostics.DiagnosticsType = DIAGNOSTICS_TYPE_NONE;
  7876. }
  7877. LOG_INFO("Dispenser Diagnostics Completed!");
  7878. }
  7879. }
  7880. else
  7881. {
  7882. _NeedDiagnostics = false;
  7883. }
  7884. }
  7885. //===============================================
  7886. // OCPP
  7887. //===============================================
  7888. void CheckOcppStatus()
  7889. {
  7890. if (Is_Ocpp_BootNotification())
  7891. {
  7892. Clean_Ocpp_BootNotification();
  7893. LOG_INFO("*********************OCPP Boot Notification****************************");
  7894. }
  7895. if (Is_Ocpp_ResetReq())
  7896. {
  7897. bool canReset = true;
  7898. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  7899. {
  7900. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7901. {
  7902. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  7903. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  7904. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  7905. {
  7906. canReset = false;
  7907. //if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  7908. //{
  7909. // ChargingTerminalProcess(_index);
  7910. //}
  7911. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7912. {
  7913. chargingInfo[_index]->ChargingStopFlag.bits.HardResetStop = true;
  7914. }
  7915. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7916. {
  7917. chargingInfo[_index]->ChargingStopFlag.bits.SoftResetStop = true;
  7918. }
  7919. }
  7920. }
  7921. }
  7922. if(canReset)
  7923. {
  7924. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7925. {
  7926. //trigger dispenser to hardware reboot
  7927. TriggerDispenserHardwareReboot();
  7928. if(IsDispenserHardwareRebootResponsed())
  7929. {
  7930. Clean_Ocpp_ResetReq();
  7931. LOG_INFO("****** Hard Reboot ******");
  7932. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7933. {
  7934. setChargerMode(i, MODE_MAINTAIN);
  7935. }
  7936. sleep(3);
  7937. system("reboot -f");
  7938. }
  7939. }
  7940. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7941. {
  7942. //trigger dispenser to software reset
  7943. TriggerDispenserSoftwareReset();
  7944. if(IsDispenserSoftwareResetResponsed())
  7945. {
  7946. Clean_Ocpp_ResetReq();
  7947. LOG_INFO("****** Soft Reboot ******");
  7948. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7949. {
  7950. setChargerMode(i, MODE_MAINTAIN);
  7951. }
  7952. sleep(3);
  7953. Kill_Ocpp_Service();
  7954. KillAllTask();
  7955. TryCloseWatchdog();
  7956. system("/usr/bin/run_evse_restart.sh");
  7957. }
  7958. }
  7959. }
  7960. }
  7961. CheckOcppCostAndPrice();
  7962. CheckLcmPageChangeRequest();
  7963. CheckStationInfo();
  7964. CheckWeatherInfo();
  7965. CheckDeductResultRequest();
  7966. CheckReDeductRequest();
  7967. CheckReaderStatusRequest();
  7968. CheckOccupancyPrice();
  7969. CheckOccupancyFee();
  7970. CheckOccupancyDeduct();
  7971. CheckDiagnosticsReq();
  7972. }
  7973. void CheckoutStartChargingSoc(byte gunIndex, bool force)
  7974. {
  7975. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc)
  7976. {
  7977. if(chargingInfo[gunIndex]->EvBatterySoc > 0 || force)
  7978. {
  7979. chargingInfo[gunIndex]->EvBatteryStartSoc = chargingInfo[gunIndex]->EvBatterySoc;
  7980. LOG_INFO("Gun %d StartSoc = %d", chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->EvBatteryStartSoc);
  7981. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc = true;
  7982. }
  7983. }
  7984. }
  7985. void UpdateChargingTimestamp(char *timestamp)
  7986. {
  7987. struct timeb SeqEndTime;
  7988. struct tm *tm;
  7989. ftime(&SeqEndTime);
  7990. SeqEndTime.time = time(NULL);
  7991. tm = localtime(&SeqEndTime.time);
  7992. 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);
  7993. }
  7994. void OcppStartTransaction(byte gunIndex)
  7995. {
  7996. if(_triggerTransaction[gunIndex])
  7997. {
  7998. return;
  7999. }
  8000. // clean deduct info
  8001. Clean_DeductInfo(gunIndex);
  8002. // set deduct user id
  8003. //Set_Deduct_UserId(gunIndex, (char *)&chargingInfo[gunIndex]->StartUserId);
  8004. // initial start & stop datetime
  8005. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00");
  8006. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, "00:00:00");
  8007. Update_PowerConsumption(gunIndex);
  8008. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  8009. Set_Ocpp_StartTransactionIdTag(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  8010. LOG_INFO("Gun %d Start Transaction IdTag = %s, MeterValue: %.4f",
  8011. chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->StartUserId, chargingInfo[gunIndex]->PowerConsumption);
  8012. Clean_Ocpp_StartTransactionConf(gunIndex);
  8013. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StartCharging);
  8014. Set_Ocpp_StartTransactionReq(gunIndex);
  8015. // set _triggerTransaction
  8016. _triggerTransaction[gunIndex] = true;
  8017. }
  8018. void OcppStopTransaction(byte gunIndex)
  8019. {
  8020. if(!_triggerTransaction[gunIndex])
  8021. {
  8022. return;
  8023. }
  8024. Update_PowerConsumption(gunIndex);
  8025. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  8026. Set_Ocpp_StopTransactionId(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  8027. LOG_INFO("Gun %d Stop Transaction IdTag = %s, StopSoc = %d, MeterValue: %.4f",
  8028. chargingInfo[gunIndex]->Index + 1,
  8029. chargingInfo[gunIndex]->StartUserId,
  8030. chargingInfo[gunIndex]->EvBatterySoc,
  8031. chargingInfo[gunIndex]->PowerConsumption);
  8032. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StopCharging);
  8033. Set_Ocpp_StopTransactionReq(gunIndex);
  8034. // clean _triggerTransaction
  8035. _triggerTransaction[gunIndex] = false;
  8036. }
  8037. bool Is_DispenserStop(byte gunIndex)
  8038. {
  8039. bool _isStop = false;
  8040. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  8041. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  8042. {
  8043. _isStop = true;
  8044. }
  8045. return _isStop;
  8046. }
  8047. void ChkOcppStatus(byte gunIndex)
  8048. {
  8049. Chk_Ocpp_Backup_ErrorCode(gunIndex);
  8050. if(Is_Ocpp_ReserveNowReq(gunIndex))
  8051. {
  8052. Clean_Ocpp_ReserveNowReq(gunIndex);
  8053. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE && !Is_Ocpp_ReserveNowExpired(gunIndex))
  8054. {
  8055. LOG_INFO("***************ChkOcppStatus: Gun %d OcppReservedStatus********************", gunIndex + 1);
  8056. chargingInfo[gunIndex]->ReservationId = Get_Ocpp_ReserveNowId(gunIndex);
  8057. setChargerMode(gunIndex, MODE_RESERVATION);
  8058. }
  8059. Set_Ocpp_ReserveNowConf(gunIndex);
  8060. }
  8061. if(Is_Ocpp_CancelReservationReq(gunIndex))
  8062. {
  8063. Clean_Ocpp_CancelReservationReq(gunIndex);
  8064. if(chargingInfo[gunIndex]->SystemStatus == S_RESERVATION)
  8065. {
  8066. LOG_INFO("***************ChkOcppStatus: Gun %d Cancel OcppReservedStatus********************", gunIndex + 1);
  8067. chargingInfo[gunIndex]->ReservationId = -1;
  8068. setChargerMode(gunIndex, MODE_IDLE);
  8069. }
  8070. Set_Ocpp_CancelReservationConf(gunIndex);
  8071. }
  8072. if (Is_Ocpp_ChangeAvailabilityReq(gunIndex) == YES)
  8073. {
  8074. LOG_INFO("***************ChkOcppStatus: Gun %d OcppChangeAvailability********************", gunIndex + 1);
  8075. Clean_Ocpp_ChangeAvailabilityReq(gunIndex);
  8076. if(Chk_Ocpp_Availability_Type(gunIndex, "Operative"))
  8077. {
  8078. if (isDb_ready)
  8079. DB_Update_Operactive(localDb, gunIndex, true);
  8080. chargingInfo[gunIndex]->IsAvailable = YES;
  8081. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  8082. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  8083. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  8084. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  8085. {
  8086. setChargerMode(gunIndex, MODE_IDLE);
  8087. }
  8088. }
  8089. else if(Chk_Ocpp_Availability_Type(gunIndex, "Inoperative"))
  8090. {
  8091. if (isDb_ready)
  8092. DB_Update_Operactive(localDb, gunIndex, false);
  8093. chargingInfo[gunIndex]->IsAvailable = NO;
  8094. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  8095. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  8096. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  8097. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  8098. {
  8099. setChargerMode(gunIndex, MODE_MAINTAIN);
  8100. }
  8101. }
  8102. }
  8103. if (Is_Ocpp_UnlockConnectorReq(gunIndex))
  8104. {
  8105. Clean_Ocpp_UnlockConnectorReq(gunIndex);
  8106. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  8107. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  8108. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  8109. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  8110. {
  8111. // 充電中,需停止充電
  8112. Set_Ocpp_StopReason(gunIndex, "UnlockCommand");
  8113. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_UNLOCK);
  8114. chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop = true;
  8115. LOG_INFO("********** OcppUnlockStop Gun %d **********", gunIndex + 1);
  8116. }
  8117. Set_Ocpp_UnlockConnectorConf(gunIndex);
  8118. }
  8119. if (Is_Ocpp_RemoteStopReq(gunIndex))
  8120. {
  8121. Clean_Ocpp_RemoteStopReq(gunIndex);
  8122. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  8123. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  8124. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  8125. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  8126. {
  8127. Set_Ocpp_StopReason(gunIndex, "Remote");
  8128. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_REMOTE_STOP);
  8129. chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop = true;
  8130. LOG_INFO("********** OcppRemoteStop Gun %d **********", gunIndex + 1);
  8131. }
  8132. }
  8133. if(Is_Ocpp_StartTransactionConf(gunIndex))
  8134. {
  8135. char acceptId[128] = {0};
  8136. char status[32] = {0};
  8137. Get_Ocpp_StartTransaction_Status(gunIndex, status);
  8138. Get_Ocpp_TransactionId(gunIndex, acceptId);
  8139. Set_Deduct_TransactionId(gunIndex, atoi(acceptId));
  8140. Set_UserTransactionId(gunIndex, atoi(acceptId));
  8141. LOG_INFO("Gun %d OCPP Start Transaction %s, TransactionId [%s]", gunIndex + 1, status, acceptId);
  8142. if(!Is_Ocpp_StartTransaction_Accepted(gunIndex))
  8143. {
  8144. if(Is_Ocpp_StopOnInvalidId_Enable())
  8145. {
  8146. if(Is_Invalid_StartTransactionId(gunIndex))
  8147. {
  8148. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop = true;
  8149. LOG_INFO("Gun %d OCPP StopOnInvalidId", gunIndex + 1);
  8150. }
  8151. }
  8152. LOG_INFO("Gun %d Transaction [%s] Not Accepted, %s",
  8153. gunIndex + 1, acceptId,
  8154. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop > 0 ? "And Stop Charging" : "But StopOnInvalidId is Disable");
  8155. }
  8156. Clean_Ocpp_StartTransactionConf(gunIndex);
  8157. }
  8158. }
  8159. bool CheckBackendChargingTimeout(byte gunIndex)
  8160. {
  8161. bool result = false;
  8162. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  8163. {
  8164. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  8165. {
  8166. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  8167. result = true;
  8168. }
  8169. }
  8170. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  8171. {
  8172. // 隨插即充電的要看 offline
  8173. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  8174. {
  8175. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  8176. result = true;
  8177. }
  8178. }
  8179. return result;
  8180. }
  8181. bool CheckBackendChargingEnergy(byte gunIndex)
  8182. {
  8183. bool result = false;
  8184. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  8185. {
  8186. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  8187. {
  8188. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  8189. result = true;
  8190. }
  8191. }
  8192. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  8193. {
  8194. // 隨插即充電的要看 offline
  8195. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  8196. {
  8197. if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  8198. result = true;
  8199. }
  8200. }
  8201. return result;
  8202. }
  8203. bool IsReachConfigMaxChargingSoc(byte gunIndex)
  8204. {
  8205. bool result = false;
  8206. if(ShmSysConfigAndInfo->SysConfig.MaxChargingSoc > 0)
  8207. {
  8208. if(chargingInfo[gunIndex]->EvBatterySoc >= ShmSysConfigAndInfo->SysConfig.MaxChargingSoc)
  8209. {
  8210. LOG_INFO("Gun %d MaxChargingSoc: [%d%%] Stop", gunIndex + 1, ShmSysConfigAndInfo->SysConfig.MaxChargingSoc);
  8211. result = true;
  8212. }
  8213. }
  8214. return result;
  8215. }
  8216. bool CheckSessionTargetStop(byte gunIndex)
  8217. {
  8218. bool stop = false;
  8219. struct StructSessionTarget target;
  8220. Get_Ocpp_SessionTarget(gunIndex, &target);
  8221. if(target.targetDuration > 0)
  8222. {
  8223. if(chargingInfo[gunIndex]->PresentChargedDuration >= (target.targetDuration * 60))
  8224. {
  8225. LOG_INFO("Gun %d SessionTarget(Duration) Stop, Present: %d (sec), Target: %d (min)",
  8226. gunIndex + 1,
  8227. chargingInfo[gunIndex]->PresentChargedDuration,
  8228. target.targetDuration);
  8229. stop = true;
  8230. }
  8231. }
  8232. if(target.targetSoc > 0)
  8233. {
  8234. if(chargingInfo[gunIndex]->EvBatterySoc >= target.targetSoc)
  8235. {
  8236. LOG_INFO("Gun %d SessionTarget(SOC) Stop, Present: %d (%%), Target: %d (%%)",
  8237. gunIndex + 1,
  8238. chargingInfo[gunIndex]->EvBatterySoc,
  8239. target.targetSoc);
  8240. stop = true;
  8241. }
  8242. }
  8243. if(target.targetEnergy > 0)
  8244. {
  8245. if(chargingInfo[gunIndex]->PresentChargedEnergy >= target.targetEnergy)
  8246. {
  8247. LOG_INFO("Gun %d SessionTarget(Energy) Stop, Present: %8.3f (kWh), Target: %4d (kWh)",
  8248. gunIndex + 1,
  8249. chargingInfo[gunIndex]->PresentChargedEnergy,
  8250. target.targetEnergy);
  8251. stop = true;
  8252. }
  8253. }
  8254. if(stop)
  8255. {
  8256. chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop = true;
  8257. }
  8258. return stop;
  8259. }
  8260. void InformOcppErrOccur(byte codeType)
  8261. {
  8262. char _error[25];
  8263. switch(codeType)
  8264. {
  8265. case 4: strcpy(_error, "InternalError"); break;
  8266. case 6: strcpy(_error, "NoError"); break;
  8267. case 7: strcpy(_error, "OtherError"); break;
  8268. case 13: strcpy(_error, "UnderVoltage"); break;
  8269. case 14: strcpy(_error, "OverVoltage"); break;
  8270. }
  8271. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  8272. {
  8273. SetOcppErrorCode(gun_index, _error);
  8274. }
  8275. }
  8276. void SetOcppErrorCode(unsigned char _index, char *errCode)
  8277. {
  8278. Set_Ocpp_StatusNotificationErrorCode(_index, errCode);
  8279. strcpy((char *)_BackupStatusNotification[_index].ErrorCode, errCode);
  8280. }
  8281. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode)
  8282. {
  8283. Set_Ocpp_StatusNotificationVendorCode(_index, vendorCode);
  8284. strcpy((char *)_BackupStatusNotification[_index].VendorErrorCode, vendorCode);
  8285. }
  8286. //===============================================
  8287. // SQLite3 related routine
  8288. //===============================================
  8289. int NetworkDB_Open(sqlite3 *db)
  8290. {
  8291. int result = PASS;
  8292. char* errMsg = NULL;
  8293. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  8294. "idx integer primary key AUTOINCREMENT, "
  8295. "occurDatetime text NOT NULL, "
  8296. "isInternet text NOT NULL, "
  8297. "isOcppConnected text NOT NULL, "
  8298. "isEth0Internet text NOT NULL, "
  8299. "isMlan0Internet text NOT NULL, "
  8300. "isPpp0Internet text NOT NULL, "
  8301. "rssiMlan0 text NOT NULL, "
  8302. "rssiPpp0 text NOT NULL"
  8303. ");";
  8304. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  8305. if(sqlite3_open(NETWORK_DB_FILE, &db))
  8306. {
  8307. result = FAIL;
  8308. LOG_ERROR("Can't open database (Net) : %s", sqlite3_errmsg(db));
  8309. sqlite3_close(db);
  8310. }
  8311. else
  8312. {
  8313. LOG_INFO("Local network status record database open successfully.");
  8314. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8315. {
  8316. result = FAIL;
  8317. LOG_ERROR("Create local network status record table error message: %s", errMsg);
  8318. }
  8319. else
  8320. {
  8321. LOG_INFO("Opened local network status record table successfully");
  8322. }
  8323. sqlite3_close(db);
  8324. }
  8325. return result;
  8326. }
  8327. int DB_Open(sqlite3 *db)
  8328. {
  8329. int result = PASS;
  8330. char* errMsg = NULL;
  8331. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  8332. "idx integer primary key AUTOINCREMENT, "
  8333. "reservationId text, "
  8334. "transactionId text, "
  8335. "startMethod text, "
  8336. "userId text, "
  8337. "dateTimeStart text, "
  8338. "dateTimeStop text,"
  8339. "socStart text, "
  8340. "socStop text, "
  8341. "chargeEnergy text, "
  8342. "stopReason text, "
  8343. "connector INTEGER"
  8344. ");";
  8345. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  8346. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8347. "`IsAvailable` TEXT NOT NULL, "
  8348. "`connector` INTEGER NOT NULL, "
  8349. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  8350. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  8351. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8352. "`occurDatetime` TEXT NOT NULL, "
  8353. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  8354. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  8355. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8356. "`rebootDatetime` TEXT NOT NULL, "
  8357. "`model_name` TEXT, "
  8358. "`serial_number` TEXT, unique(rebootDatetime) on conflict replace);";
  8359. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  8360. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8361. "`connector` INTEGER NOT NULL, "
  8362. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8363. char *createGunConsumptionSql = "CREATE TABLE IF NOT EXISTS `gun_consumption` ( "
  8364. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8365. "`connector` INTEGER NOT NULL, "
  8366. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8367. char *createFinalCostSql = "CREATE TABLE IF NOT EXISTS `final_cost` ( "
  8368. "idx integer primary key AUTOINCREMENT, "
  8369. "gun text, "
  8370. "transactionId text, "
  8371. "occurDatetime text, "
  8372. "updateDatetime text, "
  8373. "description text, "
  8374. "updateStatus integer"
  8375. ");";
  8376. char *createOcmfRecordSql = "CREATE TABLE IF NOT EXISTS `ocmf_record` ( "
  8377. "idx integer primary key AUTOINCREMENT, "
  8378. "gun text, "
  8379. "transactionId text, "
  8380. "publicKey text, "
  8381. "ocmfInfo text, "
  8382. "updateStatus integer, "
  8383. "occurDatetime text, "
  8384. "updateDatetime text"
  8385. ");";
  8386. if(sqlite3_open(DB_FILE, &db))
  8387. {
  8388. result = FAIL;
  8389. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8390. sqlite3_close(db);
  8391. }
  8392. else
  8393. {
  8394. LOG_INFO( "Local charging record database open successfully.");
  8395. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8396. {
  8397. result = FAIL;
  8398. LOG_ERROR( "Create local charging record table error message: %s", errMsg);
  8399. }
  8400. else
  8401. {
  8402. LOG_INFO( "Opened local charging record table successfully");
  8403. }
  8404. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  8405. {
  8406. result = FAIL;
  8407. LOG_ERROR( "Create local config table error message: %s", errMsg);
  8408. }
  8409. else
  8410. {
  8411. LOG_INFO( "Opened local config table successfully");
  8412. }
  8413. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8414. {
  8415. result = FAIL;
  8416. LOG_ERROR( "Create local record table error message: %s", errMsg);
  8417. }
  8418. else
  8419. {
  8420. LOG_INFO( "Opened local record table successfully");
  8421. }
  8422. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  8423. {
  8424. result = FAIL;
  8425. LOG_ERROR( "Create reboot record table error message: %s", errMsg);
  8426. }
  8427. else
  8428. {
  8429. LOG_INFO( "Opened reboot record table successfully");
  8430. }
  8431. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8432. {
  8433. result = FAIL;
  8434. LOG_ERROR( "Create power consumption record table error message: %s", errMsg);
  8435. }
  8436. else
  8437. {
  8438. LOG_INFO( "Opened power consumption record table successfully");
  8439. }
  8440. if (sqlite3_exec(db, createGunConsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8441. {
  8442. result = FAIL;
  8443. LOG_ERROR( "Create gun consumption record table error message: %s", errMsg);
  8444. }
  8445. else
  8446. {
  8447. LOG_INFO( "Opened gun consumption record table successfully");
  8448. }
  8449. if (sqlite3_exec(db, createFinalCostSql, 0, 0, &errMsg) != SQLITE_OK)
  8450. {
  8451. result = FAIL;
  8452. LOG_ERROR( "Create final cost table error message: %s", errMsg);
  8453. }
  8454. else
  8455. {
  8456. LOG_INFO( "Opened final cost table successfully");
  8457. }
  8458. if(sqlite3_exec(db, createOcmfRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8459. {
  8460. result = FAIL;
  8461. LOG_ERROR("Create OCMF record table error message: %s", errMsg);
  8462. }
  8463. else
  8464. {
  8465. LOG_INFO("Opened OCMF record table successfully");
  8466. }
  8467. sqlite3_close(db);
  8468. }
  8469. return result;
  8470. }
  8471. int DB_Insert_Record(sqlite3 *db, int gun_index)
  8472. {
  8473. int result = PASS;
  8474. char* errMsg = NULL;
  8475. char insertSql[1024];
  8476. char transactionId[128];
  8477. char reason[32];
  8478. Get_Ocpp_TransactionId(gun_index, transactionId);
  8479. Get_Ocpp_StopReason(gun_index, reason);
  8480. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, connector) "
  8481. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', %d);",
  8482. chargingInfo[gun_index]->ReservationId,
  8483. transactionId,
  8484. chargingInfo[gun_index]->StartMethod,
  8485. chargingInfo[gun_index]->StartUserId,
  8486. chargingInfo[gun_index]->StartDateTime,
  8487. chargingInfo[gun_index]->StopDateTime,
  8488. chargingInfo[gun_index]->EvBatteryStartSoc,
  8489. chargingInfo[gun_index]->EvBatterySoc,
  8490. chargingInfo[gun_index]->PresentChargedEnergy,
  8491. reason,
  8492. gun_index + 1);
  8493. if(sqlite3_open(DB_FILE, &db))
  8494. {
  8495. result = FAIL;
  8496. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8497. sqlite3_close(db);
  8498. }
  8499. else
  8500. {
  8501. //LOG_INFO( "Local charging record database open successfully.");
  8502. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8503. {
  8504. result = FAIL;
  8505. LOG_ERROR("Insert local charging record error message: %s", errMsg);
  8506. }
  8507. else
  8508. {
  8509. //LOG_INFO("Insert local charging record successfully");
  8510. }
  8511. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  8512. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8513. {
  8514. result = FAIL;
  8515. LOG_ERROR("delete local charging error message: %s", errMsg);
  8516. }
  8517. else
  8518. {
  8519. //LOG_INFO("delete local charging record successfully");
  8520. }
  8521. sqlite3_close(db);
  8522. }
  8523. return result;
  8524. }
  8525. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  8526. {
  8527. uint8_t result = false;
  8528. char* errMsg = NULL;
  8529. char sqlStr[1024];
  8530. srand(time(NULL));
  8531. if(sqlite3_open(DB_FILE, &db))
  8532. {
  8533. result = FAIL;
  8534. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8535. sqlite3_close(db);
  8536. }
  8537. else
  8538. {
  8539. LOG_INFO( "Local charging record database open successfully (%d).", IsAvailable);
  8540. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  8541. LOG_INFO("sqlStr= %s", sqlStr);
  8542. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8543. {
  8544. result = FAIL;
  8545. LOG_INFO( "update config error message: %s", errMsg);
  8546. }
  8547. else
  8548. {
  8549. LOG_INFO("update connector-%d config item isOperactive to %d", gun_index, IsAvailable);
  8550. }
  8551. sqlite3_close(db);
  8552. }
  8553. return result;
  8554. }
  8555. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  8556. {
  8557. uint8_t result = true;
  8558. char* errMsg = NULL;
  8559. char sqlStr[1024];
  8560. char **rs;
  8561. int rows, cols;
  8562. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  8563. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  8564. if(sqlite3_open(DB_FILE, &db))
  8565. {
  8566. result = FAIL;
  8567. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8568. sqlite3_close(db);
  8569. }
  8570. else
  8571. {
  8572. LOG_INFO( "Local config query database open successfully.");
  8573. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8574. if(rows>0)
  8575. {
  8576. for(int idxRow=1;idxRow<=rows;idxRow++)
  8577. {
  8578. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  8579. {
  8580. result = false;
  8581. }
  8582. LOG_INFO("Query connector-%d isOperactive: %s", gun_index, rs[(idxRow*cols)+3]);
  8583. }
  8584. }
  8585. else
  8586. {
  8587. LOG_INFO("Query connector-%d fail, set default value to operactive.", gun_index);
  8588. }
  8589. sqlite3_free_table(rs);
  8590. sqlite3_close(db);
  8591. }
  8592. return result;
  8593. }
  8594. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8595. {
  8596. char* errMsg = NULL;
  8597. char sqlStr[1024];
  8598. srand(time(NULL));
  8599. if(sqlite3_open(DB_FILE, &db))
  8600. {
  8601. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8602. sqlite3_close(db);
  8603. }
  8604. else
  8605. {
  8606. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  8607. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8608. {
  8609. LOG_ERROR("update power_consumption error message: %s", errMsg);
  8610. }
  8611. else
  8612. {
  8613. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8614. }
  8615. sqlite3_close(db);
  8616. }
  8617. }
  8618. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8619. {
  8620. char* errMsg = NULL;
  8621. char sqlStr[1024];
  8622. srand(time(NULL));
  8623. if(sqlite3_open(DB_FILE, &db))
  8624. {
  8625. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8626. sqlite3_close(db);
  8627. }
  8628. else
  8629. {
  8630. sprintf(sqlStr, "insert or replace into gun_consumption (connector, val) values(%d, %f);", gun_index, value);
  8631. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8632. {
  8633. LOG_ERROR("update gun_consumption error message: %s", errMsg);
  8634. }
  8635. else
  8636. {
  8637. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8638. }
  8639. sqlite3_close(db);
  8640. }
  8641. }
  8642. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index)
  8643. {
  8644. float result = 0;
  8645. char* errMsg = NULL;
  8646. char sqlStr[1024];
  8647. char **rs;
  8648. int rows, cols;
  8649. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  8650. if (sqlite3_open(DB_FILE, &db))
  8651. {
  8652. result = FAIL;
  8653. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8654. sqlite3_close(db);
  8655. }
  8656. else
  8657. {
  8658. //LOG_INFO("Local config query database open successfully.");
  8659. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8660. if (rows > 0)
  8661. {
  8662. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8663. {
  8664. result = atof(rs[(idxRow * cols) + 2]);
  8665. }
  8666. }
  8667. else
  8668. {
  8669. LOG_INFO("Query connector-%d fail, set default value to consumption.", gun_index);
  8670. }
  8671. sqlite3_free_table(rs);
  8672. sqlite3_close(db);
  8673. }
  8674. return result;
  8675. }
  8676. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index)
  8677. {
  8678. float result = 0;
  8679. char* errMsg = NULL;
  8680. char sqlStr[1024];
  8681. char **rs;
  8682. int rows, cols;
  8683. sprintf(sqlStr, "select * from gun_consumption where connector=%d;", gun_index);
  8684. if (sqlite3_open(DB_FILE, &db))
  8685. {
  8686. result = FAIL;
  8687. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8688. sqlite3_close(db);
  8689. }
  8690. else
  8691. {
  8692. //LOG_INFO("Local config query database open successfully.");
  8693. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8694. if (rows > 0)
  8695. {
  8696. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8697. {
  8698. result = atof(rs[(idxRow * cols) + 2]);
  8699. }
  8700. }
  8701. else
  8702. {
  8703. LOG_INFO("Query gun %d fail, set -1 to gun consumption.", gun_index + 1);
  8704. result = -1;
  8705. }
  8706. sqlite3_free_table(rs);
  8707. sqlite3_close(db);
  8708. }
  8709. return result;
  8710. }
  8711. float Get_PowerConsumption(uint8_t gun_index)
  8712. {
  8713. float value = 0;
  8714. //value = ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0 ?
  8715. // ShmChargerInfo->MeterValue[gun_index].GunConsumption : ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8716. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  8717. {
  8718. if(!_useGunMeterValue[gun_index])
  8719. {
  8720. LOG_INFO("***** Gun %d Use GunMeterValue *****", gun_index + 1);
  8721. }
  8722. _useGunMeterValue[gun_index] = true;
  8723. value = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  8724. }
  8725. else
  8726. {
  8727. value = ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8728. }
  8729. return value;
  8730. }
  8731. void Update_PowerConsumption(uint8_t gun_index)
  8732. {
  8733. chargingInfo[gun_index]->PowerConsumption = Get_PowerConsumption(gun_index);
  8734. }
  8735. int DB_Insert_FinalCost(sqlite3 *db, uint8_t gun_index, int txId, char *description)
  8736. {
  8737. char timeBuf[32];
  8738. int result = PASS;
  8739. char* errMsg = NULL;
  8740. char insertSql[1024];
  8741. getNowDatetime((unsigned char *)timeBuf);
  8742. sprintf(insertSql, "insert into final_cost(gun, transactionId, occurDatetime, updateDatetime, description, updateStatus) "
  8743. "values('%d', '%d', '%s', '%s', '%s', '%d');",
  8744. gun_index + 1,
  8745. txId,
  8746. timeBuf,
  8747. "",
  8748. description,
  8749. BILL_SATAUS_UNDISPOSED);
  8750. if(sqlite3_open(DB_FILE, &db))
  8751. {
  8752. result = FAIL;
  8753. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8754. sqlite3_close(db);
  8755. }
  8756. else
  8757. {
  8758. //LOG_INFO("FinalCost database open successfully.");
  8759. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8760. {
  8761. result = FAIL;
  8762. LOG_ERROR("Insert final_cost error message: %s", errMsg);
  8763. }
  8764. else
  8765. {
  8766. //LOG_INFO("Insert final_cost successfully");
  8767. }
  8768. sprintf(insertSql, "delete from final_cost where idx < (select idx from final_cost order by idx desc limit 1)-2000;");
  8769. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8770. {
  8771. result = FAIL;
  8772. LOG_ERROR("delete final_cost error message: %s", errMsg);
  8773. }
  8774. else
  8775. {
  8776. //LOG_INFO("delete final_cost successfully");
  8777. }
  8778. sqlite3_close(db);
  8779. }
  8780. return result;
  8781. }
  8782. int DB_Update_FinalCost_Status(sqlite3 *db, int txId, int status)
  8783. {
  8784. char timeBuf[32];
  8785. int result = PASS;
  8786. char* errMsg = NULL;
  8787. char updateSql[1024];
  8788. getNowDatetime((unsigned char *)timeBuf);
  8789. sprintf(updateSql, "update final_cost set updateStatus = %d, updateDatetime = '%s' where transactionId = '%d';",
  8790. status,
  8791. timeBuf,
  8792. txId);
  8793. if(sqlite3_open(DB_FILE, &db))
  8794. {
  8795. result = FAIL;
  8796. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8797. sqlite3_close(db);
  8798. }
  8799. else
  8800. {
  8801. if(sqlite3_exec(db, updateSql, 0, 0, &errMsg) != SQLITE_OK)
  8802. {
  8803. LOG_ERROR("update final_cost status error message: %s", errMsg);
  8804. result = FAIL;
  8805. }
  8806. else
  8807. {
  8808. //LOG_INFO("update final_cost txId[%d] status %d ok", txId, status);
  8809. }
  8810. sqlite3_close(db);
  8811. }
  8812. return result;
  8813. }
  8814. int DB_Get_Undisposed_FinalCost(sqlite3 *db, int gunIndex, char *costString)
  8815. {
  8816. int txId = -1;
  8817. char **rs;
  8818. int rows, cols;
  8819. char* errMsg = NULL;
  8820. char sqlString[1024];
  8821. sprintf(sqlString, "select * from final_cost where updateStatus = -1 and gun = '%d' order by idx asc limit 1;",
  8822. gunIndex + 1);
  8823. if(sqlite3_open(DB_FILE, &db))
  8824. {
  8825. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8826. sqlite3_close(db);
  8827. }
  8828. else
  8829. {
  8830. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  8831. if(rows > 0)
  8832. {
  8833. for(int idxRow = 1; idxRow <= rows; idxRow++)
  8834. {
  8835. txId = atoi(rs[(idxRow * cols) + 2]);
  8836. strcpy(costString, rs[(idxRow * cols) + 5]);
  8837. }
  8838. }
  8839. sqlite3_free_table(rs);
  8840. sqlite3_close(db);
  8841. }
  8842. return txId;
  8843. }
  8844. void CheckInProgress(uint8_t gun_index)
  8845. {
  8846. if((ShmChargerInfo->Connector[gun_index].Status > S_IDLE && ShmChargerInfo->Connector[gun_index].Status <= S_FAULT) ||
  8847. (ShmChargerInfo->Connector[gun_index].Status >= S_CCS_PRECHARGE_ST0 && ShmChargerInfo->Connector[gun_index].Status <= S_CCS_PRECHARGE_ST1))
  8848. {
  8849. if(chargingInfo[gun_index]->InProgress == NO)
  8850. {
  8851. LOG_INFO("Gun %d In Progress", gun_index + 1);
  8852. }
  8853. chargingInfo[gun_index]->InProgress = YES;
  8854. }
  8855. else
  8856. {
  8857. if(chargingInfo[gun_index]->InProgress == YES)
  8858. {
  8859. LOG_INFO("Gun %d Real Idle", gun_index + 1);
  8860. }
  8861. chargingInfo[gun_index]->InProgress = NO;
  8862. }
  8863. }
  8864. void CheckDebugMode(uint8_t gun_index)
  8865. {
  8866. if(ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay)
  8867. {
  8868. if(chargingInfo[gun_index]->SystemStatus != S_DEBUG)
  8869. {
  8870. setChargerMode(gun_index, MODE_DEBUG);
  8871. }
  8872. }
  8873. }
  8874. int DB_Reboot_Record(sqlite3 *db)
  8875. {
  8876. int result = PASS;
  8877. char* errMsg = NULL;
  8878. char insertSql[256];
  8879. sprintf(insertSql, "insert into reboot_record(rebootDatetime, model_name, serial_number) values(CURRENT_TIMESTAMP, '%s', '%s');",
  8880. (char *)ShmSysConfigAndInfo->SysConfig.ModelName,
  8881. (char *)ShmSysConfigAndInfo->SysConfig.SerialNumber);
  8882. if(sqlite3_open(DB_FILE, &db))
  8883. {
  8884. result = FAIL;
  8885. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8886. sqlite3_close(db);
  8887. }
  8888. else
  8889. {
  8890. LOG_INFO("Local charging record database open successfully.");
  8891. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8892. {
  8893. result = FAIL;
  8894. LOG_ERROR("Insert reboot record error message: %s", errMsg);
  8895. }
  8896. else
  8897. {
  8898. //LOG_INFO("Insert reboot record successfully");
  8899. }
  8900. sprintf(insertSql, "delete from reboot_record where idx < (select idx from reboot_record order by idx desc limit 1)-2000;");
  8901. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8902. {
  8903. result = FAIL;
  8904. LOG_ERROR("delete reboot record error message: %s", errMsg);
  8905. }
  8906. else
  8907. {
  8908. //LOG_INFO("delete reboot record successfully");
  8909. }
  8910. sqlite3_close(db);
  8911. }
  8912. return result;
  8913. }
  8914. int DB_Insert_Ocmf(sqlite3 *db, int gun_index, char *txId, char *ocmfKey, char *ocmfInfo)
  8915. {
  8916. char timeBuf[32];
  8917. int result = PASS;
  8918. char* errMsg = NULL;
  8919. char insertSql[3072];
  8920. getNowDatetime((unsigned char *)timeBuf);
  8921. sprintf(insertSql, "insert into ocmf_record(gun, transactionId, publicKey, ocmfInfo, updateStatus, occurDatetime, updateDatetime) "
  8922. "values('%d', '%s', '%s', '%s', '%d', '%s', '%s');",
  8923. gun_index + 1,
  8924. txId,
  8925. ocmfKey,
  8926. ocmfInfo,
  8927. OCMF_SATAUS_INSERTED,
  8928. timeBuf,
  8929. "");
  8930. if(sqlite3_open(DB_FILE, &db))
  8931. {
  8932. result = FAIL;
  8933. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8934. sqlite3_close(db);
  8935. }
  8936. else
  8937. {
  8938. //LOG_INFO("OCMF database open successfully.");
  8939. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8940. {
  8941. result = FAIL;
  8942. LOG_ERROR("Insert ocmf_record error message: %s", errMsg);
  8943. }
  8944. else
  8945. {
  8946. //LOG_INFO("Insert ocmf_record successfully");
  8947. }
  8948. sprintf(insertSql, "delete from ocmf_record where idx < (select idx from ocmf_record order by idx desc limit 1)-2000;");
  8949. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8950. {
  8951. result = FAIL;
  8952. LOG_ERROR("delete ocmf_record error message: %s", errMsg);
  8953. }
  8954. else
  8955. {
  8956. //LOG_INFO("delete ocmf_record successfully");
  8957. }
  8958. sqlite3_close(db);
  8959. }
  8960. return result;
  8961. }
  8962. int DB_Update_Ocmf_Status(sqlite3 *db, char *txId, int status)
  8963. {
  8964. char timeBuf[32];
  8965. int result = PASS;
  8966. char* errMsg = NULL;
  8967. char updateSql[1024];
  8968. getNowDatetime((unsigned char *)timeBuf);
  8969. sprintf(updateSql, "update ocmf_record set updateStatus = %d, updateDatetime = '%s' where transactionId = '%s';",
  8970. status,
  8971. timeBuf,
  8972. txId);
  8973. if(sqlite3_open(DB_FILE, &db))
  8974. {
  8975. result = FAIL;
  8976. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8977. sqlite3_close(db);
  8978. }
  8979. else
  8980. {
  8981. if(sqlite3_exec(db, updateSql, 0, 0, &errMsg) != SQLITE_OK)
  8982. {
  8983. LOG_ERROR("update updateStatus status error message: %s", errMsg);
  8984. result = FAIL;
  8985. }
  8986. else
  8987. {
  8988. //LOG_INFO("update updateStatus txId[%s] status %d ok", txId, status);
  8989. }
  8990. sqlite3_close(db);
  8991. }
  8992. return result;
  8993. }
  8994. // return YES if exist undisposed ocmf
  8995. int DB_Get_Undisposed_Ocmf(sqlite3 *db, int *gunIndex, char *txId, char *ocmfKey, char *ocmfInfo)
  8996. {
  8997. char **rs;
  8998. int rows, cols;
  8999. char* errMsg = NULL;
  9000. char sqlString[1024];
  9001. int result = NO;
  9002. sprintf(sqlString, "select * from ocmf_record where updateStatus = 0 order by idx asc limit 1;");
  9003. if(sqlite3_open(DB_FILE, &db))
  9004. {
  9005. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  9006. sqlite3_close(db);
  9007. }
  9008. else
  9009. {
  9010. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  9011. if(rows > 0)
  9012. {
  9013. for(int idxRow = 1; idxRow <= rows; idxRow++)
  9014. {
  9015. *gunIndex = atoi(rs[(idxRow * cols + 1)]) - 1;
  9016. strncpy(txId, rs[(idxRow * cols) + 2], 36);
  9017. strncpy(ocmfKey, rs[(idxRow * cols) + 3], 200);
  9018. strcpy(ocmfInfo, rs[(idxRow * cols) + 4]);
  9019. }
  9020. result = YES;
  9021. }
  9022. sqlite3_free_table(rs);
  9023. sqlite3_close(db);
  9024. }
  9025. return result;
  9026. }
  9027. void DB_TryAddRebootRecordColumn(sqlite3 *db)
  9028. {
  9029. char **rs;
  9030. int rows, cols;
  9031. char* errMsg = NULL;
  9032. char sqlString[1024];
  9033. int result = 0;
  9034. strcpy(sqlString, "select count(*) from pragma_table_info('reboot_record') where name = 'model_name';");
  9035. if(sqlite3_open(DB_FILE, &db))
  9036. {
  9037. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  9038. sqlite3_close(db);
  9039. }
  9040. else
  9041. {
  9042. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  9043. if(rows > 0)
  9044. {
  9045. for(int idxRow = 1; idxRow <= rows; idxRow++)
  9046. {
  9047. result = atoi(rs[(idxRow * cols) + 0]);
  9048. }
  9049. }
  9050. sqlite3_free_table(rs);
  9051. if(result == 0)
  9052. {
  9053. strcpy(sqlString, "ALTER TABLE 'reboot_record' ADD COLUMN 'model_name' TEXT;");
  9054. if(sqlite3_exec(db, sqlString, 0, 0, &errMsg) != SQLITE_OK)
  9055. {
  9056. result = FAIL;
  9057. LOG_ERROR("reboot_record add column 'model_name' error message: %s", errMsg);
  9058. }
  9059. else
  9060. {
  9061. LOG_INFO("reboot_record ADD COLUMN 'model_name' successfully");
  9062. }
  9063. strcpy(sqlString, "ALTER TABLE 'reboot_record' ADD COLUMN 'serial_number' TEXT;");
  9064. if(sqlite3_exec(db, sqlString, 0, 0, &errMsg) != SQLITE_OK)
  9065. {
  9066. result = FAIL;
  9067. LOG_ERROR("reboot_record add column 'serial_number' error message: %s", errMsg);
  9068. }
  9069. else
  9070. {
  9071. LOG_INFO("reboot_record ADD COLUMN 'serial_number' successfully");
  9072. }
  9073. }
  9074. sqlite3_close(db);
  9075. }
  9076. }
  9077. void DB_TryAddChargingRecordColumn(sqlite3 *db)
  9078. {
  9079. char **rs;
  9080. int rows, cols;
  9081. char* errMsg = NULL;
  9082. char sqlString[1024];
  9083. int result = 0;
  9084. strcpy(sqlString, "select count(*) from pragma_table_info('charging_record') where name = 'connector';");
  9085. if(sqlite3_open(DB_FILE, &db))
  9086. {
  9087. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  9088. sqlite3_close(db);
  9089. }
  9090. else
  9091. {
  9092. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  9093. if(rows > 0)
  9094. {
  9095. for(int idxRow = 1; idxRow <= rows; idxRow++)
  9096. {
  9097. result = atoi(rs[(idxRow * cols) + 0]);
  9098. }
  9099. }
  9100. sqlite3_free_table(rs);
  9101. if(result == 0)
  9102. {
  9103. strcpy(sqlString, "ALTER TABLE 'charging_record' ADD COLUMN 'connector' INTEGER;");
  9104. if(sqlite3_exec(db, sqlString, 0, 0, &errMsg) != SQLITE_OK)
  9105. {
  9106. result = FAIL;
  9107. LOG_ERROR("charging_record add column connector error message: %s", errMsg);
  9108. }
  9109. else
  9110. {
  9111. LOG_INFO("charging_record ADD COLUMN connector successfully");
  9112. }
  9113. }
  9114. sqlite3_close(db);
  9115. }
  9116. }
  9117. //===============================================
  9118. // Config process
  9119. //===============================================
  9120. void AddPlugInTimes(byte gunIndex)
  9121. {
  9122. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  9123. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  9124. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  9125. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  9126. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  9127. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  9128. }
  9129. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  9130. {
  9131. char cmdBuf[32];
  9132. struct timeb csuTime;
  9133. struct tm *tmCSU;
  9134. ftime(&csuTime);
  9135. tmCSU = localtime(&csuTime.time);
  9136. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  9137. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  9138. tmCSU->tm_sec);
  9139. if (isStart)
  9140. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  9141. else
  9142. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  9143. }
  9144. void zipLogFiles()
  9145. {
  9146. const char* logPath = "/Storage/SystemLog";
  9147. // 獲取目錄
  9148. DIR* pDir = opendir(logPath);
  9149. if (pDir != NULL)
  9150. {
  9151. struct timeb csuTime;
  9152. struct tm *tmCSU;
  9153. ftime(&csuTime);
  9154. tmCSU = localtime(&csuTime.time);
  9155. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  9156. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  9157. // tmCSU->tm_sec);
  9158. // Read items inside the folder
  9159. struct dirent* pEntry = NULL;
  9160. while ((pEntry = readdir(pDir)) != NULL)
  9161. {
  9162. if (strcmp(pEntry->d_name, ".") != 0 &&
  9163. strcmp(pEntry->d_name, "..") != 0 &&
  9164. strncmp(pEntry->d_name, "[", 1) == 0 &&
  9165. strstr(pEntry->d_name, "tar") < 0)
  9166. {
  9167. char yearC[5];
  9168. unsigned short year = 0;
  9169. char monthC[3];
  9170. unsigned short month = 0;
  9171. yearC[4] = '\0';
  9172. strncpy(yearC, pEntry->d_name + 1, 4);
  9173. monthC[2] = '\0';
  9174. strncpy(monthC, pEntry->d_name + 6, 2);
  9175. year = atoi(yearC);
  9176. month = atoi(monthC);
  9177. if (year != 0)
  9178. {
  9179. if (year < tmCSU->tm_year + 1900 ||
  9180. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  9181. {
  9182. LOG_INFO("tar file name : %s", pEntry->d_name);
  9183. char file[256];
  9184. memset(file, 0x00, sizeof(file));
  9185. strcat(file, "tar zcvf ");
  9186. strcat(file, logPath);
  9187. strncat(file, "/", 1);
  9188. strcat(file, pEntry->d_name);
  9189. strcat(file, ".tar");
  9190. strncat(file, " ", 1);
  9191. strcat(file, logPath);
  9192. strncat(file, "/", 1);
  9193. strcat(file, pEntry->d_name);
  9194. LOG_INFO("zip = %s \n", file);
  9195. system(file);
  9196. }
  9197. }
  9198. }
  9199. }
  9200. }
  9201. // Close folder
  9202. closedir(pDir);
  9203. }
  9204. void StopProcessingLoop()
  9205. {
  9206. for (;;)
  9207. {
  9208. CheckFactoryConfigFunction();
  9209. CheckFwUpdateFunction();
  9210. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  9211. {
  9212. ChkPrimaryStatus();
  9213. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0)
  9214. {
  9215. LOG_INFO("Soft reboot for retry self-tets (Primary).");
  9216. KillAllTask();
  9217. sleep(3);
  9218. TryCloseWatchdog();
  9219. system("/usr/bin/run_evse_restart.sh");
  9220. return;
  9221. }
  9222. }
  9223. if(Is_Ocpp_ResetReq())
  9224. {
  9225. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  9226. {
  9227. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = true;
  9228. }
  9229. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  9230. {
  9231. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  9232. }
  9233. }
  9234. CheckSoftHardReset();
  9235. sleep(1);
  9236. }
  9237. }
  9238. void CreateWatchdog(void)
  9239. {
  9240. wtdFd = InitWatchDog();
  9241. if(wtdFd < 0)
  9242. {
  9243. LOG_INFO("Watchdog Initial Fail");
  9244. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  9245. }
  9246. else
  9247. {
  9248. LOG_INFO("Watchdog Initial Success");
  9249. }
  9250. }
  9251. void TryCloseWatchdog(void)
  9252. {
  9253. if(wtdFd > 0)
  9254. {
  9255. write(wtdFd, "V", 1);
  9256. close(wtdFd);
  9257. }
  9258. }
  9259. void TryFeedWatchdog(void)
  9260. {
  9261. if(wtdFd > 0)
  9262. {
  9263. write(wtdFd, "a", 1);
  9264. }
  9265. }
  9266. bool IsParallelCabinetIdle(void)
  9267. {
  9268. bool result = true;
  9269. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  9270. {
  9271. result = false;
  9272. }
  9273. return result;
  9274. }
  9275. bool IsStandbyEnable(void)
  9276. {
  9277. bool result = true;
  9278. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyTimeDisableForTcc)
  9279. {
  9280. time_t timep;
  9281. struct tm *tm;
  9282. time(&timep);
  9283. timep += (ShmChargerInfo->CabinetMiscValue.TimeOffset * 60);
  9284. tm = localtime(&timep);
  9285. if(tm->tm_hour >= 0 && tm->tm_hour < 8)
  9286. {
  9287. if(!_isStandbyEnable)
  9288. {
  9289. LOG_INFO("[TCC]Customer Enable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  9290. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  9291. }
  9292. _isStandbyEnable = true;
  9293. result = true;
  9294. }
  9295. else
  9296. {
  9297. if(_isStandbyEnable)
  9298. {
  9299. LOG_INFO("[TCC]Customer Disable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  9300. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  9301. }
  9302. _isStandbyEnable = false;
  9303. result = false;
  9304. }
  9305. }
  9306. if(ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyTimeDisable)
  9307. {
  9308. result = false;
  9309. }
  9310. return result;
  9311. }
  9312. bool IsGunIdle(int gunIndex, int idleType)
  9313. {
  9314. bool idle = true;
  9315. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  9316. chargingInfo[gunIndex]->ConnectorPlugIn &&
  9317. idleType == GUN_IDLE_TYPE_WHOLE)
  9318. {
  9319. idle = false;
  9320. }
  9321. else if (chargingInfo[gunIndex]->SystemStatus != S_IDLE &&
  9322. chargingInfo[gunIndex]->SystemStatus != S_RESERVATION &&
  9323. chargingInfo[gunIndex]->SystemStatus != S_FAULT &&
  9324. chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  9325. {
  9326. idle = false;
  9327. }
  9328. return idle;
  9329. }
  9330. bool IsDispenserIdle(int dispenserIndex)
  9331. {
  9332. bool dispenserIdle = true;
  9333. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  9334. {
  9335. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  9336. if(!IsGunIdle(gun, GUN_IDLE_TYPE_NORMAL))
  9337. {
  9338. dispenserIdle = false;
  9339. break;
  9340. }
  9341. }
  9342. return dispenserIdle;
  9343. }
  9344. bool IsConnectorWholeIdle(void)
  9345. {
  9346. bool result = true;
  9347. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  9348. {
  9349. if(!IsGunIdle(count, GUN_IDLE_TYPE_WHOLE))
  9350. {
  9351. result = false;
  9352. break;
  9353. }
  9354. }
  9355. return result;
  9356. }
  9357. bool IsUpgradeAvailable(void)
  9358. {
  9359. bool result = true;
  9360. for(int count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  9361. {
  9362. if(!IsGunIdle(count, GUN_IDLE_TYPE_NORMAL))
  9363. {
  9364. result = false;
  9365. break;
  9366. }
  9367. }
  9368. return result;
  9369. }
  9370. void ClearAlarmCodeWhenAcOff()
  9371. {
  9372. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  9373. {
  9374. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  9375. }
  9376. }
  9377. //==========================================
  9378. // Check task processing
  9379. //==========================================
  9380. void CheckTask()
  9381. {
  9382. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  9383. {
  9384. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  9385. {
  9386. if(system("pidof -s Module_4g > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  9387. {
  9388. LOG_ERROR("Module_4g not running, restart it.");
  9389. system("/root/Module_4g &");
  9390. }
  9391. }
  9392. }
  9393. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  9394. {
  9395. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  9396. {
  9397. if(system("pidof -s Module_Wifi > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  9398. {
  9399. LOG_ERROR("Module_Wifi not running, restart it.");
  9400. system("/root/Module_Wifi &");
  9401. }
  9402. }
  9403. }
  9404. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  9405. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  9406. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  9407. {
  9408. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = true;
  9409. if((time((time_t*)NULL) - Get_Ocpp_procDogTime()) > 180)
  9410. {
  9411. LOG_ERROR("OcppBackend watch dog timeout task restart.");
  9412. Clean_Ocpp_procDogTime();
  9413. Kill_Ocpp_Service();
  9414. }
  9415. if(!Is_Ocpp_Task_Alive())
  9416. {
  9417. LOG_ERROR("OcppBackend not running, restart it.");
  9418. Run_Ocpp_Service();
  9419. }
  9420. }
  9421. else
  9422. {
  9423. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = false;
  9424. }
  9425. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  9426. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  9427. {
  9428. if(!Is_MaintainOcpp_Task_Alive())
  9429. {
  9430. LOG_ERROR("Maintain Backend not running, restart it.");
  9431. Run_MaintainOcpp_Service();
  9432. }
  9433. }
  9434. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  9435. {
  9436. LOG_ERROR("Module_ProduceUtils not running, restart it.");
  9437. system ("/root/Module_ProduceUtils &");
  9438. }
  9439. if(system("pidof -s Module_EvComm > /dev/null") != 0)
  9440. {
  9441. LOG_ERROR("Module_EvComm not running, restart it.");
  9442. system ("/root/Module_EvComm &");
  9443. }
  9444. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  9445. {
  9446. LOG_ERROR("Module_InternalComm not running, restart it.");
  9447. system ("/root/Module_InternalComm &");
  9448. //ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  9449. }
  9450. if(!Is_WebServer_Alive())
  9451. {
  9452. LOG_ERROR("lighttpd not running, restart it.");
  9453. Run_WebServer_Service();
  9454. }
  9455. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9456. {
  9457. if(!Is_LocalPowerSharing_Alive())
  9458. {
  9459. LOG_ERROR("Module_PowerSharing not running, restart it.");
  9460. Run_LocalPowerSharing_Service();
  9461. }
  9462. }
  9463. }
  9464. void InitialDHCP()
  9465. {
  9466. char tmpbuf[256];
  9467. memset(tmpbuf,0,256);
  9468. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  9469. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  9470. system(tmpbuf);
  9471. }
  9472. void InitialDispenserDhcpServerConfig(void)
  9473. {
  9474. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  9475. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  9476. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  9477. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  9478. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  9479. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  9480. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  9481. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  9482. usleep(1000);
  9483. }
  9484. void StartDispenserDhcpServer(void)
  9485. {
  9486. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  9487. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  9488. }
  9489. //==========================================
  9490. // Check Smart Charging Profile
  9491. //==========================================
  9492. void Ocpp_ProfileReq_Cmd(byte gunIndex)
  9493. {
  9494. if(!Is_Ocpp_ChargingProfileConf(gunIndex))
  9495. {
  9496. if(!Is_Ocpp_ChargingProfileReq(gunIndex))
  9497. {
  9498. Set_Ocpp_ChargingProfileReq(gunIndex);
  9499. }
  9500. }
  9501. }
  9502. int GetStartScheduleTime(unsigned char *time)
  9503. {
  9504. int result = -1;
  9505. struct tm tmScheduleStart;
  9506. struct timeb tbScheduleStart;
  9507. 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))
  9508. {
  9509. tmScheduleStart.tm_year -= 1900;
  9510. tmScheduleStart.tm_mon -= 1;
  9511. tbScheduleStart.time = mktime(&tmScheduleStart);
  9512. tbScheduleStart.millitm = 0;
  9513. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  9514. }
  9515. return result;
  9516. }
  9517. void Ocpp_ChargingProfile_Process(byte _index)
  9518. {
  9519. int _startCount = NO_DEFINE;
  9520. int _maxCount = 0;
  9521. float _limit = 0;
  9522. int _numberPhases = 0;
  9523. if(Is_Ocpp_ChargingProfileKind(_index, "Absolute") && Is_Ocpp_ChargingProfileId(_index))
  9524. {
  9525. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(_index);
  9526. _startCount = Get_Ocpp_ChargingProfileStartIndex(_index);
  9527. if(_startCount < _maxCount)
  9528. {
  9529. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9530. _numberPhases = Get_Ocpp_ChargingProfileScheduleNumberPhases(_index, _startCount);
  9531. chargingInfo[_index]->ChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9532. if(chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  9533. (int)chargingInfo[_index]->PresentChargingVoltage > 0)
  9534. {
  9535. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  9536. }
  9537. else
  9538. {
  9539. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9540. }
  9541. }
  9542. else
  9543. {
  9544. chargingInfo[_index]->ChargingProfilePower = -1;
  9545. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9546. }
  9547. }
  9548. else
  9549. {
  9550. chargingInfo[_index]->ChargingProfilePower = -1;
  9551. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9552. }
  9553. if((int)_lastProfilePower[_index] != (int)chargingInfo[_index]->ChargingProfilePower ||
  9554. (int)_lastProfileCurrent[_index] != (int)chargingInfo[_index]->ChargingProfileCurrent)
  9555. {
  9556. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9557. LOG_INFO("Gun %d Get Profile - Index = %d, Limit = %f",
  9558. _index + 1, _startCount, _limit);
  9559. LOG_INFO("Max: %d kW, Gun %d ChargingProfile Power = %d, Current = %d",
  9560. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower == -1 ? (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower : ((int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000),
  9561. _index + 1, (int)chargingInfo[_index]->ChargingProfilePower, (int)chargingInfo[_index]->ChargingProfileCurrent);
  9562. }
  9563. _lastProfilePower[_index] = chargingInfo[_index]->ChargingProfilePower;
  9564. _lastProfileCurrent[_index] = chargingInfo[_index]->ChargingProfileCurrent;
  9565. }
  9566. void Ocpp_MaxChargingProfile_Process(void)
  9567. {
  9568. int _startCount = NO_DEFINE;
  9569. int _maxCount = 0;
  9570. float _limit = 0;
  9571. int _numberPhases = 0;
  9572. if(Is_Ocpp_MaxChargingProfileKind("Absolute") && Is_Ocpp_MaxChargingProfileId())
  9573. {
  9574. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  9575. _startCount = Get_Ocpp_MaxProfileStartIndex();
  9576. if(_startCount < _maxCount)
  9577. {
  9578. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9579. _numberPhases = Get_Ocpp_MaxProfileScheduleNumberPhases(_startCount);
  9580. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9581. }
  9582. else
  9583. {
  9584. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9585. }
  9586. }
  9587. else
  9588. {
  9589. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9590. }
  9591. if((int)_lastMaxProfilePower != (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower)
  9592. {
  9593. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9594. LOG_INFO("Charger Get Max Profile - Index = %d, Limit = %f",
  9595. _startCount, _limit);
  9596. LOG_INFO("Charger Max Profile Power = %d", (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower);
  9597. }
  9598. _lastMaxProfilePower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower;
  9599. }
  9600. void CheckSmartChargeProfile(byte _index)
  9601. {
  9602. if(Is_Ocpp_ChargingProfileConf(_index))
  9603. {
  9604. Clean_Ocpp_ChargingProfileConf(_index);
  9605. Ocpp_MaxChargingProfile_Process();
  9606. Ocpp_ChargingProfile_Process(_index);
  9607. }
  9608. }
  9609. void GetLocalPowerLimit(byte gunIndex)
  9610. {
  9611. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9612. {
  9613. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  9614. {
  9615. float _localLimitPower = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gunIndex] * AC_OUTPUT_VOL;
  9616. if(chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 && (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  9617. {
  9618. chargingInfo[gunIndex]->LocalPowerLimitCurrent = _localLimitPower / chargingInfo[gunIndex]->PresentChargingVoltage;
  9619. }
  9620. else
  9621. {
  9622. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9623. }
  9624. }
  9625. else
  9626. {
  9627. chargingInfo[gunIndex]->LocalPowerLimitCurrent = 0;
  9628. }
  9629. }
  9630. else
  9631. {
  9632. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9633. }
  9634. }
  9635. void TheEndCharging(byte gun_index)
  9636. {
  9637. chargingInfo[gun_index]->isRemoteStart = NO;
  9638. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0 && ShmChargerInfo->UserTransaction[gun_index].TransactionId > 0)
  9639. {
  9640. char receiptString[512];
  9641. sprintf(receiptString, "%s%d",
  9642. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0], ShmChargerInfo->UserTransaction[gun_index].TransactionId);
  9643. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gun_index][0], receiptString);
  9644. Set_Connector_MiscCommand(gun_index, MISC_CONN_RECEIPT);
  9645. LOG_INFO("Gun %d ReceiptrURL: [%s]", gun_index + 1, receiptString);
  9646. }
  9647. ChangeStartOrStopDateTime(NO, gun_index);
  9648. DB_Insert_Record(localDb, gun_index);
  9649. }
  9650. void UpdateErrorCodeToOcpp(byte index)
  9651. {
  9652. //printf("ConnectorAlarmCode = %s \n", chargingInfo[index]->ConnectorAlarmCode);
  9653. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL)
  9654. {
  9655. SetOcppErrorCode(index, "InternalError");
  9656. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->ConnectorAlarmCode);
  9657. }
  9658. else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL)
  9659. {
  9660. SetOcppErrorCode(index, "OtherError");
  9661. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->EvConnAlarmCode);
  9662. }
  9663. }
  9664. void CheckMiscCommandRequirement(void)
  9665. {
  9666. BOOL NeedAnnouncement = FALSE;
  9667. unsigned char connector = 0;
  9668. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate != YES)
  9669. {
  9670. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9671. {
  9672. NeedAnnouncement = FALSE;
  9673. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  9674. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  9675. {
  9676. if(ShmChargerInfo->DispenserMiscReq[i].CtrlValue != 0)
  9677. {
  9678. NeedAnnouncement = TRUE;
  9679. }
  9680. for(int j = 0; j < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity; j++)
  9681. {
  9682. connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorID[j] - 1;
  9683. if(ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue != 0)
  9684. {
  9685. NeedAnnouncement = TRUE;
  9686. }
  9687. }
  9688. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement != NeedAnnouncement)
  9689. {
  9690. if(NeedAnnouncement)
  9691. {
  9692. //LOG_INFO("********** Dispenser %d Misc Command Need Announcement **********", i + 1);
  9693. }
  9694. else
  9695. {
  9696. //LOG_INFO("********** Dispenser %d Misc Command Announced **********", i + 1);
  9697. }
  9698. }
  9699. }
  9700. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  9701. }
  9702. }
  9703. }
  9704. void CheckDispenserVersionUpdateRequirement(void)
  9705. {
  9706. }
  9707. bool IsReadyParalleling(void)
  9708. {
  9709. bool ready = true;
  9710. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  9711. {
  9712. if(chargingInfo[i]->SystemStatus != S_IDLE && chargingInfo[i]->SystemStatus != S_MAINTAIN)
  9713. {
  9714. ready = false;
  9715. break;
  9716. }
  9717. }
  9718. return ready;
  9719. }
  9720. void SetParallelingStart(void)
  9721. {
  9722. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9723. {
  9724. if(chargingInfo[i]->SystemStatus == S_IDLE)
  9725. {
  9726. setChargerMode(i, MODE_MAINTAIN);
  9727. }
  9728. }
  9729. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Wait;
  9730. }
  9731. void SetParallelingDone(void)
  9732. {
  9733. unsigned short totalPow = 0;
  9734. totalPow = GetTotalParallelCabinetPower();
  9735. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9736. {
  9737. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9738. }
  9739. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9740. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_None;
  9741. }
  9742. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff)
  9743. {
  9744. unsigned char relay = 0;
  9745. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9746. {
  9747. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9748. {
  9749. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9750. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9751. {
  9752. relay = OnOff == YES ? YES : NO;
  9753. if(ShmChargerInfo->ParallelCabinet.PCabinet[index].OutputRelaySetting[index] != relay)
  9754. {
  9755. LOG_INFO("Set Parallel Cabinet Gun %d Output Relay %s", index + 1, relay == YES ? "ON" : "OFF");
  9756. }
  9757. ShmChargerInfo->ParallelCabinet.PCabinet[i].OutputRelaySetting[index] = relay;
  9758. }
  9759. }
  9760. }
  9761. }
  9762. void SetParallelCabinetAcContactor(unsigned char OnOff)
  9763. {
  9764. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9765. {
  9766. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9767. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9768. {
  9769. ShmChargerInfo->ParallelCabinet.PCabinet[i].AcContactorSetting = OnOff == YES ? YES : NO;
  9770. }
  9771. }
  9772. }
  9773. unsigned short GetTotalParallelCabinetPower(void)
  9774. {
  9775. unsigned short power = 0, slavePow = 0;
  9776. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9777. {
  9778. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  9779. {
  9780. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  9781. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  9782. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  9783. {
  9784. slavePow = ParsingRatingPower((char *)ShmChargerInfo->ParallelCabinet.PCabinet[i].ModelName);
  9785. power += slavePow;
  9786. }
  9787. }
  9788. }
  9789. return power;
  9790. }
  9791. unsigned char preParallelStatus = 0;
  9792. void MCabinetParallelingProcess(void)
  9793. {
  9794. bool PsuReInitial = false;
  9795. unsigned char status = 0;
  9796. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9797. {
  9798. if(IsReadyParalleling())
  9799. {
  9800. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9801. {
  9802. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest)
  9803. {
  9804. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest = false;
  9805. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm = true;
  9806. PsuReInitial = true;
  9807. }
  9808. }
  9809. if(PsuReInitial)
  9810. {
  9811. SetParallelingStart();
  9812. }
  9813. }
  9814. status = ShmChargerInfo->ParallelCabinet.ParallelStatus;
  9815. switch(status)
  9816. {
  9817. case _Parallel_None:
  9818. if(preParallelStatus != status)
  9819. {
  9820. LOG_INFO("MCabinet Parallel Status [None]");
  9821. GetClockTime(&_Parallel_Time);
  9822. }
  9823. unsigned short totalPow = 0;
  9824. totalPow = GetTotalParallelCabinetPower();
  9825. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9826. {
  9827. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9828. }
  9829. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9830. break;
  9831. case _Parallel_Wait:
  9832. if(preParallelStatus != status)
  9833. {
  9834. LOG_INFO("MCabinet Parallel Status [Wait]");
  9835. SetAcContactor(OFF);
  9836. SetParallelCabinetAcContactor(OFF);
  9837. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9838. {
  9839. SetPCabinetOutputRelay(i, OFF);
  9840. }
  9841. GetClockTime(&_Parallel_Time);
  9842. }
  9843. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9844. {
  9845. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Working;
  9846. }
  9847. break;
  9848. case _Parallel_Working:
  9849. if(preParallelStatus != status)
  9850. {
  9851. LOG_INFO("MCabinet Parallel Status [Working]");
  9852. SetAcContactor(ON);
  9853. SetParallelCabinetAcContactor(ON);
  9854. GetClockTime(&_Parallel_Time);
  9855. }
  9856. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  9857. {
  9858. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Done;
  9859. }
  9860. break;
  9861. case _Parallel_Done:
  9862. if(preParallelStatus != status)
  9863. {
  9864. LOG_INFO("MCabinet Parallel Status [Done]");
  9865. GetClockTime(&_Parallel_Time);
  9866. }
  9867. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9868. {
  9869. SetParallelingDone();
  9870. }
  9871. break;
  9872. }
  9873. preParallelStatus = status;
  9874. }
  9875. }
  9876. void SCabinetControllProcess(void)
  9877. {
  9878. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  9879. {
  9880. if(ShmChargerInfo->SCabinetControl.SAcContactor)
  9881. {
  9882. SetAcContactor(ON);
  9883. }
  9884. else
  9885. {
  9886. SetAcContactor(OFF);
  9887. }
  9888. unsigned short _parallelRelay = 0;
  9889. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9890. {
  9891. _parallelRelay = ShmChargerInfo->SCabinetControl.SParallelRelay[i] > 0 ? (1 < i) : 0;
  9892. }
  9893. if(_parallelRelay != ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue)
  9894. {
  9895. LOG_INFO("SCabinet Set Parallel Relay %X -> %X", ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue, _parallelRelay);
  9896. }
  9897. ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue = _parallelRelay;
  9898. }
  9899. }
  9900. void CheckConnectorDisconnectionRecovery(unsigned char connector)
  9901. {
  9902. unsigned char dispenser = 0;
  9903. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  9904. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  9905. {
  9906. LOG_INFO("Connector %d Disconnection Recovery", connector + 1);
  9907. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.Disconnection = false;
  9908. }
  9909. }
  9910. bool ConnectorRequestToCharging(unsigned char connector)
  9911. {
  9912. unsigned char role = 0;
  9913. bool done = false;
  9914. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9915. switch(role)
  9916. {
  9917. case _GROLE_IDLE:
  9918. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest)
  9919. {
  9920. LOG_INFO("Request Gun %d To Start Charging", connector + 1);
  9921. }
  9922. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest = true;
  9923. break;
  9924. case _GROLE_MASTER:
  9925. done = true;
  9926. break;
  9927. case _GROLE_SLAVE:
  9928. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest)
  9929. {
  9930. LOG_INFO("Gun %d Wait For Re-Grouping", connector + 1);
  9931. }
  9932. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest = true;
  9933. break;
  9934. case _GROLE_REQUEST_TO_CHARGING:
  9935. done = true;
  9936. break;
  9937. default:
  9938. break;
  9939. }
  9940. return done;
  9941. }
  9942. bool IsConnectorGroupingCompleted(unsigned char connector)
  9943. {
  9944. unsigned char role = 0;
  9945. bool completed = false;
  9946. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9947. switch(role)
  9948. {
  9949. case _GROLE_MASTER:
  9950. completed = true;
  9951. break;
  9952. default:
  9953. break;
  9954. }
  9955. return completed;
  9956. }
  9957. void SelfTestFailReason(void)
  9958. {
  9959. char reason[50];
  9960. memset(reason, 0x00, sizeof(reason));
  9961. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  9962. {
  9963. StatusCodeCompose(reason, "042251");
  9964. }
  9965. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail ||
  9966. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail ||
  9967. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9968. {
  9969. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail)
  9970. {
  9971. StatusCodeCompose(reason, "042280");
  9972. }
  9973. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9974. {
  9975. StatusCodeCompose(reason, "042281");
  9976. }
  9977. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail)
  9978. {
  9979. StatusCodeCompose(reason, "042282");
  9980. }
  9981. }
  9982. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail ||
  9983. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  9984. {
  9985. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail)
  9986. {
  9987. StatusCodeCompose(reason, "042285");
  9988. }
  9989. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  9990. {
  9991. StatusCodeCompose(reason, "042286");
  9992. }
  9993. }
  9994. else
  9995. {
  9996. StatusCodeCompose(reason, "Self Test Fail");
  9997. }
  9998. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  9999. {
  10000. SetOcppErrorCode(gun_index, "InternalError");
  10001. SetOcppVendorErrorCode(gun_index, reason);
  10002. setChargerMode(gun_index, MODE_MAINTAIN);
  10003. }
  10004. }
  10005. bool IsAvailableBackToIdle(int gun_index)
  10006. {
  10007. if(_plugInHold[gun_index] == false &&
  10008. (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false || chargingInfo[gun_index]->ConnectorPlugIn == false))
  10009. {
  10010. _plugInHold[gun_index] = true;
  10011. GetClockTime(&_PlugInHold_Time[gun_index]);
  10012. LOG_INFO ("Gun %d Plug In Hold Delay", gun_index + 1);
  10013. }
  10014. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle ||
  10015. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false ||
  10016. (!chargingInfo[gun_index]->ConnectorPlugIn && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm) ||
  10017. isResetStopChargeFlag(gun_index) == YES)
  10018. {
  10019. if(!_plugInHold[gun_index])
  10020. {
  10021. _plugInHold[gun_index] = true;
  10022. GetClockTime(&_PlugInHold_Time[gun_index]);
  10023. LOG_INFO ("Force Gun %d Plug In Hold Delay", gun_index + 1);
  10024. }
  10025. if((GetTimeoutValue(_PlugInHold_Time[gun_index]) / uSEC_VAL) >= PLUG_IN_HOLD_TIME)
  10026. {
  10027. return true;
  10028. }
  10029. }
  10030. return false;
  10031. }
  10032. void CheckGunAvailable(void)
  10033. {
  10034. if(!ShmChargerInfo->Control.SysCtrl.bits.PsuInit)
  10035. {
  10036. if(ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  10037. {
  10038. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  10039. {
  10040. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  10041. {
  10042. ShmChargerInfo->Control.GunAvailable[i] = YES;
  10043. }
  10044. }
  10045. ShmChargerInfo->Control.SysCtrl.bits.PsuInit = true;
  10046. }
  10047. }
  10048. }
  10049. int presentChargedEnergyClear(unsigned char gun_index)
  10050. {
  10051. int result = FAIL;
  10052. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  10053. memset(chargingInfo[gun_index]->presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod) * sizeof(float));
  10054. result = PASS;
  10055. return result;
  10056. }
  10057. float presentChargedEnergyTotal(unsigned char gun_index)
  10058. {
  10059. float result = 0.0f;
  10060. for(int idx = 0; idx < ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod); idx++)
  10061. {
  10062. result += chargingInfo[gun_index]->presentChargedEnergyPeriod[idx];
  10063. }
  10064. return result;
  10065. }
  10066. int presentChargedEnergyUpdate(unsigned char gun_index, float kwh)
  10067. {
  10068. int result = FAIL;
  10069. time_t CurrentTime;
  10070. struct tm *tm;
  10071. CurrentTime = time(NULL);
  10072. tm=localtime(&CurrentTime);
  10073. chargingInfo[gun_index]->presentChargedEnergyPeriod[tm->tm_hour] += kwh;
  10074. return result;
  10075. }
  10076. void ChargedEnergySummation(unsigned char gun_index)
  10077. {
  10078. unsigned long time = 0;
  10079. time = GetSecTimeoutValue(_ChargedEnergy_Time[gun_index]);
  10080. if(time >= (chargingInfo[gun_index]->PresentChargedDuration + ENERAGY_INTERVAL))
  10081. {
  10082. chargingInfo[gun_index]->PresentChargedDuration++;
  10083. float changedEnergy = 0;
  10084. float _meterEnergy = 0;
  10085. float _calChangingEnergy = (chargingInfo[gun_index]->PresentChargingPower) / 3600;
  10086. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  10087. {
  10088. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= _lsatGunMeterValue[gun_index])
  10089. {
  10090. _meterEnergy = ShmChargerInfo->MeterValue[gun_index].GunConsumption - _lsatGunMeterValue[gun_index];
  10091. }
  10092. else
  10093. {
  10094. _meterEnergy = 0;
  10095. }
  10096. changedEnergy = _meterEnergy;
  10097. _lsatGunMeterValue[gun_index] = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  10098. }
  10099. else
  10100. {
  10101. changedEnergy = _calChangingEnergy;
  10102. }
  10103. if(ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  10104. {
  10105. chargingInfo[gun_index]->ChargingFee += changedEnergy * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  10106. }
  10107. presentChargedEnergyUpdate(gun_index, changedEnergy);
  10108. chargingInfo[gun_index]->PresentChargedEnergy += changedEnergy;
  10109. ShmChargerInfo->MeterValue[gun_index].LocalConsumption += changedEnergy;
  10110. }
  10111. }
  10112. void ChkConnectorAction(void)
  10113. {
  10114. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  10115. {
  10116. if(ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel)
  10117. {
  10118. if(ShmChargerInfo->AllBill.OccupancyInfo[i].Status != _Parking_WaitToPay)
  10119. {
  10120. LOG_INFO("Gun %d Charging Canceled", i + 1);
  10121. Clean_Ocpp_TcciSerialNo(i);
  10122. }
  10123. else
  10124. {
  10125. LOG_INFO("Gun %d Occupancy Keep Counting", i + 1);
  10126. Set_OccupancyKeepCountingReq(i);
  10127. }
  10128. ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel = false;
  10129. }
  10130. }
  10131. }
  10132. void Update_DispenserAvailability(int dispenserIndex)
  10133. {
  10134. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  10135. {
  10136. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  10137. Set_Connector_MiscCommand(gun, MISC_CONN_AVAILABILITY);
  10138. }
  10139. }
  10140. void InitialDataBaseValue(void)
  10141. {
  10142. // Local DB
  10143. if(DB_Open(localDb) != PASS)
  10144. {
  10145. LOG_INFO("DB_Open fail.");
  10146. isDb_ready = false;
  10147. }
  10148. else
  10149. {
  10150. isDb_ready = true;
  10151. DB_TryAddRebootRecordColumn(localDb);
  10152. DB_TryAddChargingRecordColumn(localDb);
  10153. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10154. {
  10155. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  10156. LOG_INFO("Gun %d IsAvailable Initial: [%s]", _index + 1, chargingInfo[_index]->IsAvailable == YES ? "Enable" : "Disable");
  10157. }
  10158. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10159. {
  10160. ShmChargerInfo->MeterValue[_index].LocalConsumption = DB_Get_LocalConsumption(localDb, _index);
  10161. LOG_INFO("Local Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  10162. }
  10163. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10164. {
  10165. ShmChargerInfo->MeterValue[_index].GunConsumption = DB_Get_GunConsumption(localDb, _index);
  10166. LOG_INFO("Remote Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].GunConsumption);
  10167. }
  10168. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10169. {
  10170. Update_PowerConsumption(_index);
  10171. LOG_INFO("Gun %d Power Consumption Initial: [%.4f kWh]", _index + 1, chargingInfo[_index]->PowerConsumption);
  10172. }
  10173. DB_Reboot_Record(localDb);
  10174. }
  10175. if (NetworkDB_Open(networkDb) != PASS)
  10176. {
  10177. LOG_ERROR("NetworkDB_Open fail.");
  10178. }
  10179. }
  10180. float CalculateMaxOutputRatio(int gunIndex)
  10181. {
  10182. float ratio = 0;
  10183. int inUsingCnt = 0;
  10184. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  10185. {
  10186. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  10187. {
  10188. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  10189. }
  10190. }
  10191. if(inUsingCnt != 0)
  10192. {
  10193. ratio = ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].Role == _GROLE_MASTER ?
  10194. ((float)ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].GunPsuQuantity / inUsingCnt) : 0;
  10195. }
  10196. ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio = ratio;
  10197. return ratio;
  10198. }
  10199. // return unit: 0.1kW
  10200. unsigned short GetModelNameLimitPower(int gunIndex)
  10201. {
  10202. unsigned short limitPower = 0;
  10203. float fPower = 0;
  10204. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10205. ShmChargerInfo->Control.ChargerRatingPower != 0)
  10206. {
  10207. fPower = (float)ShmChargerInfo->Control.ChargerRatingPower / 10;
  10208. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  10209. {
  10210. fPower += (float)ShmChargerInfo->ParallelCabinet.TotalParallelPower / 10;
  10211. }
  10212. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10213. limitPower = (unsigned short)(fPower * 10);
  10214. }
  10215. return limitPower;
  10216. }
  10217. void GetWebPageConfigPowerLimit(int gunIndex)
  10218. {
  10219. unsigned short limitPower = 0;
  10220. float fPower = 0;
  10221. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10222. ShmSysConfigAndInfo->SysConfig.MaxChargingPower != 0)
  10223. {
  10224. fPower = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingPower;
  10225. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10226. limitPower = (unsigned short)(fPower * 10);
  10227. }
  10228. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower = limitPower;
  10229. }
  10230. void GetWebPageConfigCurrentLimit(int gunIndex)
  10231. {
  10232. unsigned short limitCurrent = 0;
  10233. float fCurrent = 0;
  10234. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10235. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent != 0)
  10236. {
  10237. fCurrent = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent;
  10238. fCurrent = fCurrent * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10239. limitCurrent = (unsigned short)(fCurrent * 10);
  10240. }
  10241. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent = limitCurrent;
  10242. }
  10243. void GetOffLineMaxChargingPowerLimit(int gunIndex)
  10244. {
  10245. unsigned short limitPower = 0;
  10246. float fPower = 0;
  10247. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10248. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower != 0 &&
  10249. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES)
  10250. {
  10251. fPower = ShmChargerInfo->OutputLimit.Offline_MaxChargingPower / 1000;
  10252. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10253. limitPower = (unsigned short)(fPower * 10);
  10254. }
  10255. ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] = limitPower * 100;
  10256. }
  10257. void GetMaxChargingProfileLimit(int gunIndex)
  10258. {
  10259. unsigned short limitPower = 0, limitCurrent = 0;
  10260. float fPower = 0;
  10261. // max charging profile
  10262. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  10263. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower >= 0)
  10264. {
  10265. fPower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000;
  10266. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  10267. limitPower = (unsigned short)(fPower * 10);
  10268. if (chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 &&
  10269. (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  10270. {
  10271. limitCurrent = (limitPower * 100 / chargingInfo[gunIndex]->PresentChargingVoltage) * 10;
  10272. }
  10273. }
  10274. ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] = limitPower * 100;
  10275. ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] = limitCurrent;
  10276. }
  10277. void GetMaxChargingLimit(int gunIndex)
  10278. {
  10279. GetWebPageConfigPowerLimit(gunIndex);
  10280. GetWebPageConfigCurrentLimit(gunIndex);
  10281. GetOffLineMaxChargingPowerLimit(gunIndex);
  10282. GetMaxChargingProfileLimit(gunIndex);
  10283. }
  10284. void GetGunOtpDerating(int gunIndex)
  10285. {
  10286. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp.isNeedDerating &&
  10287. chargingInfo[gunIndex]->SystemStatus > S_IDLE &&
  10288. chargingInfo[gunIndex]->SystemStatus <= S_CHARGING)
  10289. {
  10290. struct DERATING_BY_OTP *_derating;
  10291. _derating = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp;
  10292. if(_derating->deratingIndex < GUN_DERATING_COUNT)
  10293. {
  10294. int _connectorTemp = (int)chargingInfo[gunIndex]->ConnectorTemp - 60;
  10295. int _chillerTemp = (int)chargingInfo[gunIndex]->ChillerTemp - 60;
  10296. if(_connectorTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  10297. chargingInfo[gunIndex]->ConnectorTemp != 0xFF &&
  10298. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  10299. {
  10300. LOG_INFO("Gun %d GunTemp %d is Derating(%d) to %d A",
  10301. gunIndex + 1,
  10302. _connectorTemp,
  10303. GunDeratingTemp[_derating->deratingIndex],
  10304. ((int)_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  10305. _derating->deratingIndex++;
  10306. }
  10307. else if(_chillerTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  10308. chargingInfo[gunIndex]->ChillerTemp != 0xFF &&
  10309. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  10310. {
  10311. LOG_INFO("Gun %d ChillerTemp %d is Derating(%d) to %d A",
  10312. gunIndex + 1,
  10313. _chillerTemp,
  10314. GunDeratingTemp[_derating->deratingIndex],
  10315. ((int)_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  10316. _derating->deratingIndex++;
  10317. }
  10318. }
  10319. if(chargingInfo[gunIndex]->ConnectorTemp != 0xFF || chargingInfo[gunIndex]->ChillerTemp != 0xFF)
  10320. {
  10321. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = (int)_derating->deratingTargetCurrent[_derating->deratingIndex];
  10322. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  10323. }
  10324. else
  10325. {
  10326. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = 0;
  10327. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  10328. }
  10329. }
  10330. else
  10331. {
  10332. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = 0;
  10333. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  10334. }
  10335. }
  10336. void CalculateGunAbsMaxPower(int gunIndex)
  10337. {
  10338. int maxValue = 0;
  10339. maxValue = (int)GetModelNameLimitPower(gunIndex);
  10340. ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] = maxValue != 0 ? (maxValue * 100) : 0;
  10341. }
  10342. void CalculateGunAbsMaxVoltage(int gunIndex)
  10343. {
  10344. int maxValue = 0;
  10345. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalVoltage;
  10346. ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] = maxValue;
  10347. }
  10348. void CalculateGunAbsMaxCurrent(int gunIndex)
  10349. {
  10350. int maxValue = 0;
  10351. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalCurrent;
  10352. ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] = maxValue;
  10353. }
  10354. void RestrictChargingCapability(int gunIndex, unsigned short *power, unsigned short *voltage, unsigned short *current)
  10355. {
  10356. unsigned short limitPower = 0;
  10357. int _limitPowerResult = 0;
  10358. int _limitVoltageResult = 0;
  10359. int _limitCurrentResult = 0;
  10360. _limitPowerResult = ShmPsuData->SystemAvailablePower;
  10361. _limitVoltageResult = MAX_OUTPUT_VOLTAGE;
  10362. _limitCurrentResult = ShmPsuData->SystemAvailableCurrent;
  10363. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] != 0)
  10364. {
  10365. limitPower = ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] / 100;
  10366. if(limitPower != 0 && _limitPowerResult > limitPower)
  10367. {
  10368. _limitPowerResult = limitPower;
  10369. }
  10370. }
  10371. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] != 0 &&
  10372. _limitVoltageResult > ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex])
  10373. {
  10374. _limitVoltageResult = ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex];
  10375. }
  10376. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] != 0 &&
  10377. _limitCurrentResult > ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex])
  10378. {
  10379. _limitCurrentResult = ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex];
  10380. }
  10381. // restrict from WebPageConfig power
  10382. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower != 0)
  10383. {
  10384. if(_limitPowerResult > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower)
  10385. {
  10386. _limitPowerResult = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower;
  10387. }
  10388. }
  10389. // restrict from WebPageConfig current
  10390. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent != 0)
  10391. {
  10392. if(_limitCurrentResult > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent)
  10393. {
  10394. _limitCurrentResult = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent;
  10395. }
  10396. }
  10397. // restrict from Offline_MaxChargingPower
  10398. if(ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] != 0)
  10399. {
  10400. limitPower = ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] / 100;
  10401. if(limitPower != 0 && _limitPowerResult > limitPower)
  10402. {
  10403. _limitPowerResult = limitPower;
  10404. }
  10405. }
  10406. // restrict from MaxChargingProfile
  10407. if(ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] != 0)
  10408. {
  10409. limitPower = ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] / 100;
  10410. if(limitPower != 0 && _limitPowerResult > limitPower)
  10411. {
  10412. _limitPowerResult = limitPower;
  10413. }
  10414. }
  10415. // restrict from MaxChargingProfile
  10416. if(ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] != 0)
  10417. {
  10418. if(_limitCurrentResult > ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex])
  10419. {
  10420. _limitCurrentResult = ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex];
  10421. }
  10422. }
  10423. // restrict from ChargingProfile
  10424. if((chargingInfo[gunIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gunIndex]->SystemStatus <= S_CHARGING) ||
  10425. (chargingInfo[gunIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gunIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10426. {
  10427. if(chargingInfo[gunIndex]->ChargingProfilePower >= 0 && _limitPowerResult > ((int)chargingInfo[gunIndex]->ChargingProfilePower / 100))
  10428. {
  10429. _limitPowerResult = (int)chargingInfo[gunIndex]->ChargingProfilePower / 100;
  10430. }
  10431. if(chargingInfo[gunIndex]->ChargingProfileCurrent >= 0 && _limitCurrentResult > (int)chargingInfo[gunIndex]->ChargingProfileCurrent)
  10432. {
  10433. _limitCurrentResult = (int)chargingInfo[gunIndex]->ChargingProfileCurrent;
  10434. }
  10435. }
  10436. // restrict from local power limit
  10437. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  10438. {
  10439. if(chargingInfo[gunIndex]->LocalPowerLimitCurrent >= 0 && _limitCurrentResult > (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10))
  10440. {
  10441. _limitCurrentResult = (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10);
  10442. }
  10443. }
  10444. // restrict after otp
  10445. if(ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] != 0)
  10446. {
  10447. limitPower = ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] / 100;
  10448. if(limitPower != 0 && _limitPowerResult > limitPower)
  10449. {
  10450. _limitPowerResult = limitPower;
  10451. }
  10452. }
  10453. // restrict after otp
  10454. if(ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] != 0 &&
  10455. _limitCurrentResult > ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex])
  10456. {
  10457. _limitCurrentResult = ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex];
  10458. }
  10459. ShmChargerInfo->OutputLimit.GunLimitPower[gunIndex] = _limitPowerResult;
  10460. ShmChargerInfo->OutputLimit.GunLimitVoltage[gunIndex] = _limitVoltageResult;
  10461. ShmChargerInfo->OutputLimit.GunLimitCurrent[gunIndex] = _limitCurrentResult;
  10462. if(_limitPowerResult != 0 && *power > _limitPowerResult)
  10463. {
  10464. *power = _limitPowerResult;
  10465. }
  10466. if(_limitVoltageResult != 0 && *voltage > _limitVoltageResult)
  10467. {
  10468. *voltage = _limitVoltageResult;
  10469. }
  10470. if(_limitCurrentResult != 0 && *current > _limitCurrentResult)
  10471. {
  10472. *current = _limitCurrentResult;
  10473. }
  10474. }
  10475. void UpdateChargingCapability(void)
  10476. {
  10477. unsigned short voltage = 0, current = 0, power = 0;
  10478. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower = Get_Ocpp_OffLineMaxChargingPower();
  10479. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  10480. {
  10481. voltage = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.MaximumChargingVoltage;
  10482. current = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingCurrent;
  10483. power = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingPower;
  10484. CalculateGunAbsMaxPower(i);
  10485. CalculateGunAbsMaxVoltage(i);
  10486. CalculateGunAbsMaxCurrent(i);
  10487. CalculateMaxOutputRatio(i);
  10488. GetMaxChargingLimit(i);
  10489. GetLocalPowerLimit(i);
  10490. GetGunOtpDerating(i);
  10491. RestrictChargingCapability(i, &power, &voltage, &current);
  10492. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityVoltage = voltage;
  10493. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityCurrent = current;
  10494. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityPower = power;
  10495. }
  10496. }
  10497. void GunErrorCollection(void)
  10498. {
  10499. bool _PsuNoResource = false;
  10500. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  10501. {
  10502. if(ShmPsuData->Work_Step == INITIAL_START)
  10503. {
  10504. ShmChargerInfo->GunError[i].ErrFlag.bits.PsuGroupNoResource = false;
  10505. }
  10506. else
  10507. {
  10508. if(ShmChargerInfo->GunError[i].ErrFlag.bits.PsuGroupNoResource)
  10509. {
  10510. _PsuNoResource = true;
  10511. }
  10512. }
  10513. }
  10514. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = _PsuNoResource;
  10515. }
  10516. void UpdateGunPresentChargingPower(int gunIndex)
  10517. {
  10518. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Enable)
  10519. {
  10520. chargingInfo[gunIndex]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingCurrent))) / 1000 / 100);
  10521. }
  10522. else
  10523. {
  10524. chargingInfo[gunIndex]->PresentChargingPower = ((float)((chargingInfo[gunIndex]->PresentChargingVoltage) * (chargingInfo[gunIndex]->PresentChargingCurrent)) / 1000);
  10525. }
  10526. }
  10527. void CheckPsuLostQuantity(void)
  10528. {
  10529. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  10530. {
  10531. if(ShmChargerInfo->Control.PsuQuantityByModel == ShmPsuData->SystemPresentPsuQuantity)
  10532. {
  10533. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = false;
  10534. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = 0;
  10535. }
  10536. else
  10537. {
  10538. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = ShmChargerInfo->Control.PsuQuantityByModel > ShmPsuData->SystemPresentPsuQuantity ?
  10539. (ShmChargerInfo->Control.PsuQuantityByModel - ShmPsuData->SystemPresentPsuQuantity) : 0;
  10540. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch ||
  10541. _psuLostQuantity != ShmSysConfigAndInfo->SysInfo.PsuLostQuantity)
  10542. {
  10543. LOG_INFO("Psu Quantity is %d, it should be %d, Psu Lost: %d",
  10544. ShmPsuData->SystemPresentPsuQuantity, ShmChargerInfo->Control.PsuQuantityByModel,
  10545. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity);
  10546. }
  10547. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = true;
  10548. _psuLostQuantity = ShmSysConfigAndInfo->SysInfo.PsuLostQuantity;
  10549. }
  10550. }
  10551. }
  10552. void CheckOcmfHandler(void)
  10553. {
  10554. bool _newOcmf = false;
  10555. int _ocmfLength = 0;
  10556. int _gunIndex = 0;
  10557. char _txId[37];
  10558. char _ocmfKey[201];
  10559. char _ocmfInfo[2048];
  10560. // check new ocmf info
  10561. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  10562. {
  10563. if(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfFlag.bits.OcmfReceived)
  10564. {
  10565. _newOcmf = false;
  10566. char fileName[256] = {0};
  10567. sprintf(fileName, "/mnt/Gun_%d_%s_OCMF", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId);
  10568. if(access(fileName, F_OK) != -1)
  10569. {
  10570. _newOcmf = true;
  10571. }
  10572. if(_newOcmf)
  10573. {
  10574. int fd = 0;
  10575. fd = open(fileName, O_RDONLY);
  10576. if(fd < 0)
  10577. {
  10578. LOG_INFO("Gun %d TxId[%s] OCMF file [%s] open fail", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, fileName);
  10579. }
  10580. else
  10581. {
  10582. memset(_txId, 0x00, sizeof(_txId));
  10583. memset(_ocmfKey, 0x00, sizeof(_ocmfKey));
  10584. memset(_ocmfInfo, 0x00, sizeof(_ocmfInfo)); //-48 is take out the header
  10585. _ocmfLength = read(fd, _ocmfInfo, sizeof(_ocmfInfo) - 1);
  10586. close(fd);
  10587. memcpy(_txId, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId));
  10588. memcpy(_ocmfKey, ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey));
  10589. DB_Insert_Ocmf(localDb, i, _txId, _ocmfKey, _ocmfInfo);
  10590. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfPending = true;
  10591. char cmdBuf[256] = {0};
  10592. sprintf(cmdBuf, "rm -f %s", fileName);
  10593. system(cmdBuf);
  10594. LOG_INFO("Gun %d TxId[%s] insert OCMF info ok, length: %d", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, _ocmfLength);
  10595. }
  10596. }
  10597. else
  10598. {
  10599. LOG_INFO("Gun %d TxId[%s] OCMF info [%s] access fail", i + 1, ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, fileName);
  10600. }
  10601. memset(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId, 0x00, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfTxId));
  10602. memset(ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey, 0x00, sizeof(ShmChargerInfo->OcmfInfo.GunOcmf[i].PublicKey));
  10603. ShmChargerInfo->OcmfInfo.GunOcmf[i].OcmfFlag.bits.OcmfReceived = false;
  10604. }
  10605. }
  10606. // try to find ocmf pending in database
  10607. if(ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfPending && !ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload)
  10608. {
  10609. _gunIndex = 0;
  10610. memset(_txId, 0x00, sizeof(_txId));
  10611. memset(_ocmfKey, 0x00, sizeof(_ocmfKey));
  10612. memset(_ocmfInfo, 0x00, sizeof(_ocmfInfo));
  10613. ShmChargerInfo->OcmfInfo.SysOcmfIndex = 0;
  10614. memset(ShmChargerInfo->OcmfInfo.SysOcmfTxId, 0x00, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfTxId));
  10615. memset(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey, 0x00, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey));
  10616. memset(ShmChargerInfo->OcmfInfo.SysOcmfInfo, 0x00, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfInfo));
  10617. if(DB_Get_Undisposed_Ocmf(localDb, &_gunIndex, _txId, _ocmfKey, _ocmfInfo))
  10618. {
  10619. ShmChargerInfo->OcmfInfo.SysOcmfIndex = _gunIndex;
  10620. memcpy(ShmChargerInfo->OcmfInfo.SysOcmfTxId, _txId, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfTxId));
  10621. memcpy(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey, _ocmfKey, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfPublicKey));
  10622. memcpy(ShmChargerInfo->OcmfInfo.SysOcmfInfo, _ocmfInfo, sizeof(ShmChargerInfo->OcmfInfo.SysOcmfInfo));
  10623. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload = true;
  10624. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = false;
  10625. LOG_INFO("Gun %d TxId[%s] OCMF info need upload", _gunIndex + 1, _txId);
  10626. }
  10627. else
  10628. {
  10629. LOG_INFO("There is no ocmf info pending");
  10630. ShmChargerInfo->OcmfInfo.SysOcmfFlag.CtrlValue = 0;
  10631. }
  10632. }
  10633. // try to upload ocmf to backend
  10634. if(ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload)
  10635. {
  10636. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  10637. {
  10638. if(!ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading)
  10639. {
  10640. if(!Is_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex))
  10641. {
  10642. Set_Ocpp_OcmfData(ShmChargerInfo->OcmfInfo.SysOcmfIndex, ShmChargerInfo->OcmfInfo.SysOcmfPublicKey, ShmChargerInfo->OcmfInfo.SysOcmfInfo);
  10643. Set_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex);
  10644. LOG_INFO("Set Gun %d TxId[%s] update OCMF request", ShmChargerInfo->OcmfInfo.SysOcmfIndex + 1, ShmChargerInfo->OcmfInfo.SysOcmfTxId);
  10645. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = true;
  10646. }
  10647. }
  10648. else
  10649. {
  10650. if(!Is_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex))
  10651. {
  10652. DB_Update_Ocmf_Status(localDb, ShmChargerInfo->OcmfInfo.SysOcmfTxId, OCMF_SATAUS_UPDATED_OK);
  10653. LOG_INFO("Gun %d TxId[%s] update OCMF info to backend ok", ShmChargerInfo->OcmfInfo.SysOcmfIndex + 1, ShmChargerInfo->OcmfInfo.SysOcmfTxId);
  10654. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfNeedUpload = false;
  10655. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = false;
  10656. }
  10657. }
  10658. }
  10659. else
  10660. {
  10661. if(ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading)
  10662. {
  10663. Clean_Ocpp_SendOcmfDataReq(ShmChargerInfo->OcmfInfo.SysOcmfIndex);
  10664. LOG_INFO("Backend disconnected, Gun %d TxId[%s] ocmf will update later", ShmChargerInfo->OcmfInfo.SysOcmfIndex + 1, ShmChargerInfo->OcmfInfo.SysOcmfTxId);
  10665. }
  10666. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfUploading = false;
  10667. }
  10668. }
  10669. }
  10670. int main(void)
  10671. {
  10672. unsigned long time = 0;
  10673. int _dispenser = 0;
  10674. _isStandbyEnable = true;
  10675. if(CreateShareMemory() == 0)
  10676. {
  10677. #ifdef SystemLogMessage
  10678. LOG_ERROR("CreatShareMemory NG");
  10679. #endif
  10680. if(ShmStatusCodeData!=NULL)
  10681. {
  10682. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  10683. }
  10684. sleep(5);
  10685. system("reboot -f");
  10686. sleep(5);
  10687. system("reboot -f");
  10688. }
  10689. LOG_INFO("\n");
  10690. LOG_INFO("===== Boot and Initialization start =====");
  10691. LOG_INFO("Main Fork PID = %d", getpid());
  10692. if (!InitialSystemDefaultConfig())
  10693. {
  10694. LOG_INFO("InitialSystemDefaultConfig NG");
  10695. StopProcessingLoop();
  10696. }
  10697. LOG_INFO("ModelName: [%s], SN: [%s], SW Version: [%s][%s]",
  10698. ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber, fwVersion, subVersion);
  10699. LOG_INFO("InitialShareMemoryInfo");
  10700. InitialShareMemoryInfo();
  10701. LOG_INFO("ChargerType (IEC or UL) = %d", ShmSysConfigAndInfo->SysInfo.ChargerType);
  10702. if(!InitialChargerSetting())
  10703. {
  10704. isModelNameMatch = false;
  10705. }
  10706. Initialization();
  10707. InitialDataBaseValue();
  10708. LOG_INFO("Spawn all Task.");
  10709. SpawnTask();
  10710. if (!isModelNameMatch)
  10711. {
  10712. LOG_INFO("Module Name & HW info none match.");
  10713. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  10714. // Module Name 與硬體對應不正確
  10715. LOG_ERROR("Module Name & HW info none match.");
  10716. sleep(3);
  10717. KillAllTask();
  10718. StopProcessingLoop();
  10719. }
  10720. LOG_INFO("Module Name & HW info correct. Initialize.......");
  10721. CreateTimeoutFork();
  10722. SelfTestRun();
  10723. while(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  10724. {
  10725. sleep(1);
  10726. }
  10727. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  10728. {
  10729. sleep(1);
  10730. SelfTestFailReason();
  10731. }
  10732. else
  10733. {
  10734. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10735. {
  10736. setChargerMode(gun_index, MODE_IDLE);
  10737. if(ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 ||
  10738. !ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  10739. {
  10740. ShmChargerInfo->Control.GunAvailable[gun_index] = YES;
  10741. }
  10742. }
  10743. }
  10744. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE)
  10745. {
  10746. SetAcContactor(ON);
  10747. SpawnOcppService();
  10748. SpawnMaintainOcppService();
  10749. }
  10750. else
  10751. {
  10752. InitSlaveCabinetEthernet();
  10753. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10754. {
  10755. setChargerMode(gun_index, MODE_MAINTAIN);
  10756. }
  10757. }
  10758. sleep(1);
  10759. CreateWatchdog();
  10760. // Main loop
  10761. LOG_INFO("%s \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_COMPLETE ?
  10762. "****************************Main Loop**********************************" :
  10763. "********************** SelfTest Fail **********************");
  10764. GetClockTime(&_cmdMainPriority_time);
  10765. ShmChargerInfo->OcmfInfo.SysOcmfFlag.bits.OcmfPending = true;
  10766. for (;;)
  10767. {
  10768. CheckGunAvailable();
  10769. CheckOcppStatus();
  10770. ChkPrimaryStatus();
  10771. ChkPsuStatus();
  10772. PsuFailureResume();
  10773. ChkConnectorAction();
  10774. if(IsUpgradeAvailable())
  10775. {
  10776. CheckFactoryConfigFunction();
  10777. CheckFwUpdateFunction();
  10778. }
  10779. if(CheckFlashUpdate() != YES)
  10780. {
  10781. // return fork process
  10782. return 0;
  10783. }
  10784. CheckSoftHardReset();
  10785. StandbyCheck();
  10786. AuthorizeTimeoutProcess();
  10787. // check ocmf request
  10788. CheckOcmfHandler();
  10789. // 當 AC 沒有搭上時,清除一些錯誤碼
  10790. //ClearAlarmCodeWhenAcOff();
  10791. if ((GetTimeoutValue(_cmdMainPriority_time) / uSEC_VAL) > MAIN_PRIORITY_INTERVAL)
  10792. {
  10793. CheckTask();
  10794. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10795. {
  10796. if((chargingInfo[_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[_index]->SystemStatus <= S_CHARGING) ||
  10797. (chargingInfo[_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10798. {
  10799. if (chargingInfo[_index]->SystemStatus == S_CHARGING && _ocppProfileChkFlag[_index] == 12)
  10800. {
  10801. Ocpp_ProfileReq_Cmd(_index);
  10802. gunOutputVol[_index] = chargingInfo[_index]->PresentChargingVoltage;
  10803. _ocppProfileChkFlag[_index] = 0;
  10804. }
  10805. else if (chargingInfo[_index]->SystemStatus != S_CHARGING)
  10806. {
  10807. Ocpp_ProfileReq_Cmd(_index);
  10808. _ocppProfileChkFlag[_index] = 0;
  10809. }
  10810. else
  10811. {
  10812. _ocppProfileChkFlag[_index]++;
  10813. }
  10814. }
  10815. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  10816. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  10817. {
  10818. DB_Update_LocalConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  10819. if(ShmChargerInfo->MeterValue[_index].GunConsumption >= 0)
  10820. {
  10821. DB_Update_GunConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].GunConsumption);
  10822. }
  10823. }
  10824. }
  10825. CheckPsuLostQuantity();
  10826. GetClockTime(&_cmdMainPriority_time);
  10827. }
  10828. CheckMiscCommandRequirement();
  10829. CheckDispenserVersionUpdateRequirement();
  10830. MCabinetParallelingProcess();
  10831. SCabinetControllProcess();
  10832. UpdateChargingCapability();
  10833. GunErrorCollection();
  10834. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10835. {
  10836. //CheckGpioInStatus();
  10837. CheckErrorOccurStatus(gun_index);
  10838. ChkOcppStatus(gun_index);
  10839. if((chargingInfo[gun_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= S_CHARGING) ||
  10840. (chargingInfo[gun_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10841. {
  10842. if(chargingInfo[gun_index]->SystemStatus == S_CHARGING)
  10843. {
  10844. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  10845. if (chargingInfo[gun_index]->PresentChargingVoltage >= gunOutputVol[gun_index] + 10 ||
  10846. chargingInfo[gun_index]->PresentChargingVoltage <= gunOutputVol[gun_index] - 10)
  10847. {
  10848. _ocppProfileChkFlag[gun_index] = 12;
  10849. gunOutputVol[gun_index] = chargingInfo[gun_index]->PresentChargingVoltage;
  10850. }
  10851. }
  10852. }
  10853. CheckSmartChargeProfile(gun_index);
  10854. Update_PowerConsumption(gun_index);
  10855. CheckInProgress(gun_index);
  10856. CheckDebugMode(gun_index);
  10857. //LOG_INFO("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  10858. switch(chargingInfo[gun_index]->SystemStatus)
  10859. {
  10860. case S_MAINTAIN:
  10861. if(isModeChange(gun_index))
  10862. {
  10863. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10864. {
  10865. LOG_INFO("================== Slave Cabinet %d ==================", ShmChargerInfo->Control.CabinetSwitch - 1);
  10866. }
  10867. LOG_INFO("================== S_MAINTAIN (%x) ================== \n", gun_index + 1);
  10868. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10869. GetClockTime(&_SystemStatus_Time[gun_index]);
  10870. }
  10871. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  10872. if(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE ||
  10873. ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  10874. ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10875. {
  10876. break;
  10877. }
  10878. if(chargingInfo[gun_index]->IsAvailable == YES &&
  10879. ShmChargerInfo->Control.GunAvailable[gun_index] == YES &&
  10880. ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  10881. {
  10882. if(time >= MAINTAIN_RECOVERY_TIME)
  10883. {
  10884. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10885. setChargerMode(gun_index, MODE_IDLE);
  10886. }
  10887. }
  10888. break;
  10889. case S_RESERVATION:
  10890. if(isModeChange(gun_index))
  10891. {
  10892. LOG_INFO("================== S_RESERVATION (%x) ================== \n", gun_index + 1);
  10893. char expiryDate[128];
  10894. Get_Ocpp_ReserveNowExpiryDate(gun_index, expiryDate);
  10895. Get_Ocpp_ReserveNowIdTag(gun_index, &ShmChargerInfo->Control.ResevedIdTag[gun_index][0]);
  10896. Set_Connector_MiscCommand(gun_index, MISC_CONN_RESERVATION);
  10897. LOG_INFO("****** Reservation IdTag: [%s], Expiry: [%s]", &ShmChargerInfo->Control.ResevedIdTag[gun_index][0], expiryDate);
  10898. GetClockTime(&_SystemStatus_Time[gun_index]);
  10899. }
  10900. if(chargingInfo[gun_index]->ConnectorPlugIn)
  10901. {
  10902. SetAcContactor(ON);
  10903. }
  10904. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  10905. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  10906. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  10907. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10908. {
  10909. UpdateErrorCodeToOcpp(gun_index);
  10910. setChargerMode(gun_index, MODE_FAULT);
  10911. continue;
  10912. }
  10913. if(Is_Ocpp_ReserveNowExpired(gun_index))
  10914. {
  10915. chargingInfo[gun_index]->ReservationId = -1;
  10916. LOG_INFO("Gun %d reservation is expired...", gun_index + 1);
  10917. setChargerMode(gun_index, MODE_IDLE);
  10918. }
  10919. else
  10920. {
  10921. if(IsConnectorAuthorizeSuccess(gun_index))
  10922. {
  10923. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10924. {
  10925. LOG_INFO("Gun %d remote start reserve charging", gun_index + 1);
  10926. }
  10927. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  10928. {
  10929. LOG_INFO("Gun %d swipe rfid start reserve charging", gun_index + 1);
  10930. }
  10931. setChargerMode(gun_index, MODE_AUTHORIZING);
  10932. }
  10933. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  10934. {
  10935. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  10936. if(Is_Ocpp_ReserveNowIdTag(gun_index, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId))
  10937. {
  10938. SetAcContactor(ON);
  10939. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  10940. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10941. {
  10942. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  10943. LOG_INFO("Gun %d reserve Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  10944. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  10945. {
  10946. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  10947. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  10948. }
  10949. DispenserAuthInitial(_dispenser);
  10950. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10951. }
  10952. }
  10953. }
  10954. else
  10955. {
  10956. if(chargingInfo[gun_index]->ReservationId == -1)
  10957. {
  10958. LOG_INFO("Gun %d reservation is clean", gun_index + 1);
  10959. setChargerMode(gun_index, MODE_IDLE);
  10960. }
  10961. }
  10962. }
  10963. break;
  10964. case S_IDLE:
  10965. case S_FAULT:
  10966. {
  10967. // clean PermissionRequest flag
  10968. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  10969. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  10970. if(isModeChange(gun_index))
  10971. {
  10972. if (chargingInfo[gun_index]->SystemStatus == S_IDLE)
  10973. {
  10974. LOG_INFO("================== S_IDLE (%x) ================== \n", gun_index + 1);
  10975. chargingInfo[gun_index]->ReservationId = -1;
  10976. _triggerTransaction[gun_index] = false;
  10977. _plugInHold[gun_index] = false;
  10978. chargingInfo[gun_index]->PresentChargedDuration = 0;
  10979. chargingInfo[gun_index]->RemainChargingDuration = 0;
  10980. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  10981. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  10982. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  10983. memset(ShmChargerInfo->Timestamp[gun_index].StartCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StartCharging));
  10984. memset(ShmChargerInfo->Timestamp[gun_index].StopCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StopCharging));
  10985. Clean_Ocpp_StopReason(gun_index);
  10986. ReleaseAlarmCode(gun_index);
  10987. // clean connector information
  10988. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  10989. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  10990. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  10991. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  10992. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  10993. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  10994. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -65535;
  10995. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].CostDiscount = 0;
  10996. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemainAmount = 0;
  10997. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].EnergyCost = -1;
  10998. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParkingFee = -1;
  10999. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  11000. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.GetStartChargingSoc = false;
  11001. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  11002. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  11003. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  11004. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.OutputLimitEnable = false;
  11005. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].GeneralChargingData.deratingByConnOtp.deratingIndex = 0;
  11006. chargingInfo[gun_index]->EvBatterytargetVoltage = 0;
  11007. chargingInfo[gun_index]->EvBatterytargetCurrent = 0;
  11008. chargingInfo[gun_index]->EvBatteryMaxVoltage = 0;
  11009. chargingInfo[gun_index]->EvBatterySoc = 0;
  11010. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetVoltage = 0;
  11011. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetCurrent = 0;
  11012. // clean RemoteStart & RemoteStop flag
  11013. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_START);
  11014. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_STOP);
  11015. Clean_Connector_MiscCommand(gun_index, MISC_CONN_UNLOCK);
  11016. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  11017. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopConfirm = false;
  11018. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopConfirm = false;
  11019. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NormalStopRequest = false;
  11020. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AlarmStopRequest = false;
  11021. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  11022. ShmChargerInfo->GunError[gun_index].ErrFlag.ErrorValue = 0;
  11023. memset(chargingInfo[gun_index]->ConnectorAlarmCode, 0x00, sizeof(chargingInfo[gun_index]->ConnectorAlarmCode));
  11024. Clean_Ocpp_RunningCost(gun_index);
  11025. Clean_Ocpp_FinalCost(gun_index);
  11026. Clean_DeductInfo(gun_index);
  11027. Clean_Ocpp_TcciSerialNo(gun_index);
  11028. Clean_UserTransaction(gun_index);
  11029. Clean_UserPriceAndReceipt(gun_index);
  11030. presentChargedEnergyClear(gun_index);
  11031. chargingInfo[gun_index]->PresentChargingPower = 0;
  11032. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower = 0;
  11033. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent = 0;
  11034. chargingInfo[gun_index]->ChargingProfilePower = -1;
  11035. chargingInfo[gun_index]->ChargingProfileCurrent = -1;
  11036. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  11037. {
  11038. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging = false;
  11039. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger = false;
  11040. // reset dispenser availability
  11041. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  11042. }
  11043. // clean force charging info
  11044. memset(&ShmChargerInfo->Control.FCharging[gun_index], 0x00, sizeof(ForceCharging));
  11045. }
  11046. else if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  11047. {
  11048. LOG_INFO("================== S_FAULT (%x) ================== \n", gun_index + 1);
  11049. }
  11050. GetClockTime(&_SystemStatus_Time[gun_index]);
  11051. }
  11052. if (chargingInfo[gun_index]->IsAvailable == NO ||
  11053. ShmChargerInfo->Control.GunAvailable[gun_index] == NO)
  11054. {
  11055. setChargerMode(gun_index, MODE_MAINTAIN);
  11056. continue;
  11057. }
  11058. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  11059. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  11060. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  11061. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  11062. {
  11063. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest)
  11064. {
  11065. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11066. {
  11067. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemotenAlarmCode, 6);
  11068. }
  11069. }
  11070. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  11071. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  11072. {
  11073. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11074. {
  11075. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042304", 6);
  11076. LOG_INFO("Connector %d Disconnect Occur", gun_index + 1);
  11077. }
  11078. }
  11079. if(chargingInfo[gun_index]->SystemStatus != S_FAULT)
  11080. {
  11081. UpdateErrorCodeToOcpp(gun_index);
  11082. setChargerMode(gun_index, MODE_FAULT);
  11083. }
  11084. else
  11085. {
  11086. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  11087. {
  11088. CheckConnectorDisconnectionRecovery(gun_index);
  11089. }
  11090. }
  11091. }
  11092. else
  11093. {
  11094. if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  11095. {
  11096. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  11097. if(time >= FAULT_RELEASE_TIME)
  11098. {
  11099. ReleaseAlarmCode(gun_index);
  11100. setChargerMode(gun_index, MODE_IDLE);
  11101. }
  11102. break;
  11103. }
  11104. // clean stop charge flag
  11105. chargingInfo[gun_index]->StopChargeFlag = false;
  11106. chargingInfo[gun_index]->ChargingStopFlag.ChargingStopValue = 0;
  11107. if(chargingInfo[gun_index]->ConnectorPlugIn || ShmChargerInfo->Control.CustomizedInfo.Flag.bits.StandbyTimeDisable)
  11108. {
  11109. SetAcContactor(ON);
  11110. }
  11111. { // Idle 正常程序起點
  11112. // 判斷是否有啟用檢查插槍
  11113. if(IsConnectorAuthorizeSuccess(gun_index))
  11114. {
  11115. if(chargingInfo[gun_index]->IsAvailable &&
  11116. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  11117. {
  11118. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  11119. {
  11120. LOG_INFO("Gun %d remote start charging", gun_index + 1);
  11121. Clean_Ocpp_TcciCreditInfo(gun_index);
  11122. }
  11123. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  11124. {
  11125. LOG_INFO("Gun %d swipe rfid start charging", gun_index + 1);
  11126. }
  11127. Set_StartIdType(gun_index, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthIdType);
  11128. setChargerMode(gun_index, MODE_AUTHORIZING);
  11129. continue;
  11130. }
  11131. }
  11132. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  11133. {
  11134. SetAcContactor(ON);
  11135. if(chargingInfo[gun_index]->IsAvailable &&
  11136. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  11137. chargingInfo[gun_index]->ConnectorPlugIn &&
  11138. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  11139. {
  11140. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  11141. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  11142. LOG_INFO("Gun %d Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  11143. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  11144. {
  11145. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  11146. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  11147. }
  11148. Set_StartIdType(gun_index, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthIdType);
  11149. DispenserAuthInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  11150. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  11151. continue;
  11152. }
  11153. }
  11154. // only for test & debug purpose
  11155. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging &&
  11156. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging)
  11157. {
  11158. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging = false;
  11159. strcpy((char *)chargingInfo[gun_index]->StartUserId, "ChargingTest");
  11160. LOG_INFO("Gun %d Start Force Charging%s",
  11161. gun_index + 1,
  11162. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger > 0 ? ", Triggered by WebAPI" : "");
  11163. if(ShmChargerInfo->Control.TestCtrl.bits.FChargingReleaseExtend)
  11164. {
  11165. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableReleaseAndExtend = true;
  11166. LOG_INFO("Gun %d Enable ReleaseAndExtend At Force Charging", gun_index + 1);
  11167. }
  11168. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  11169. {
  11170. LOG_INFO("Set Dispenser %d Inoperative",
  11171. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex + 1);
  11172. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  11173. }
  11174. Set_StartIdType(gun_index, _Type_Local);
  11175. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  11176. continue;
  11177. }
  11178. else
  11179. {
  11180. }
  11181. } // Idle 正常程序終點
  11182. }
  11183. }
  11184. break;
  11185. case S_AUTHORIZING:
  11186. if(isModeChange(gun_index))
  11187. {
  11188. LOG_INFO("================== S_AUTHORIZING (%x) ================== \n", gun_index + 1);
  11189. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  11190. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  11191. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  11192. //StopSystemTimeoutDet();
  11193. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  11194. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  11195. SetAcContactor(ON);
  11196. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  11197. }
  11198. if(!_triggerTransaction[gun_index] &&
  11199. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC &&
  11200. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  11201. {
  11202. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= STARTTRANSATION_TIME)
  11203. {
  11204. OcppStartTransaction(gun_index);
  11205. }
  11206. }
  11207. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  11208. {
  11209. FouceAnnounceAccountBalance(gun_index);
  11210. }
  11211. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm)
  11212. {
  11213. // sync with dispenser
  11214. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  11215. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  11216. LOG_INFO("Sync with Gun %d through remote start", gun_index + 1);
  11217. FouceAnnounceAccountBalance(gun_index);
  11218. }
  11219. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm)
  11220. {
  11221. // sync with dispenser
  11222. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm = false;
  11223. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  11224. LOG_INFO("Sync with Connector %d through swipe rfid", gun_index + 1);
  11225. }
  11226. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  11227. {
  11228. if(!chargingInfo[gun_index]->ConnectorPlugIn)
  11229. {
  11230. LOG_INFO("*********** Gun %d Plug In Timeout ***********", gun_index + 1);
  11231. }
  11232. else
  11233. {
  11234. LOG_INFO("*********** Gun %d Preparing State Timeout ***********", gun_index + 1);
  11235. }
  11236. OcppStopTransaction(gun_index);
  11237. setChargerMode(gun_index, MODE_IDLE);
  11238. }
  11239. else
  11240. {
  11241. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  11242. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  11243. {
  11244. AddPlugInTimes(gun_index);
  11245. LOG_INFO("Gun %d CardNumber = [%s]", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  11246. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  11247. GunAuthInitial(gun_index);
  11248. }
  11249. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  11250. {
  11251. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  11252. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  11253. LOG_INFO("*********** Gun %d User Stop Plug In ***********", gun_index + 1);
  11254. OcppStopTransaction(gun_index);
  11255. setChargerMode(gun_index, MODE_IDLE);
  11256. }
  11257. else if(isEvBoardStopChargeFlag(gun_index) == YES)
  11258. {
  11259. LOG_INFO("********** Gun %d Alarm Stop (S_AUTHORIZING) **********", gun_index + 1);
  11260. Set_Ocpp_StopReason(gun_index, "Local");
  11261. ChargingAlarmProcess(gun_index);
  11262. }
  11263. else if(isNormalStopChargeFlag(gun_index) == YES)
  11264. {
  11265. LOG_INFO("********** Gun %d Normal Stop (S_AUTHORIZING) **********", gun_index + 1);
  11266. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11267. ChargingTerminalProcess(gun_index);
  11268. }
  11269. }
  11270. break;
  11271. case S_REASSIGN_CHECK:
  11272. {
  11273. if (isModeChange(gun_index))
  11274. {
  11275. LOG_INFO("================== S_REASSIGN_CHECK (%x) ================== \n", gun_index + 1);
  11276. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  11277. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  11278. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  11279. //StopSystemTimeoutDet();
  11280. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  11281. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy = ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy;
  11282. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration = ShmSysConfigAndInfo->SysConfig.MaxChargingDuration;
  11283. LOG_INFO("Max Total Current %d A, Max Total Power %d kW, Total Energy %d kWh, Total Duration %d",
  11284. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent,
  11285. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  11286. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy,
  11287. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration);
  11288. SetAcContactor(ON);
  11289. SetPCabinetOutputRelay(gun_index, ON);
  11290. GetClockTime(&_SystemStatus_Time[gun_index]);
  11291. }
  11292. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11293. if(ConnectorRequestToCharging(gun_index))
  11294. {
  11295. setChargerMode(gun_index, MODE_REASSIGN);
  11296. }
  11297. else if(time >= WAIT_REASSIGN_TIME)
  11298. {
  11299. if(ShmPsuData->Work_Step != _WORK_CHARGING)
  11300. {
  11301. LOG_INFO("Gun %d Wait PSU Work Step Timeout", gun_index + 1);
  11302. }
  11303. else
  11304. {
  11305. LOG_INFO("Gun %d Request Charging Timeout", gun_index + 1);
  11306. }
  11307. ShmChargerInfo->GunError[gun_index].ErrFlag.bits.PsuGroupNoResource = true;
  11308. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11309. {
  11310. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042279", 6);
  11311. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  11312. }
  11313. Set_Ocpp_StopReason(gun_index, "Local");
  11314. ChargingAlarmProcess(gun_index);
  11315. //setChargerMode(gun_index, MODE_IDLE);
  11316. }
  11317. }
  11318. break;
  11319. case S_REASSIGN:
  11320. {
  11321. if (isModeChange(gun_index))
  11322. {
  11323. LOG_INFO("================== S_REASSIGN (%x) ================== \n", gun_index + 1);
  11324. //gettimeofday(&_toAverage_time, NULL);
  11325. GetClockTime(&_SystemStatus_Time[gun_index]);
  11326. }
  11327. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11328. if(IsConnectorGroupingCompleted(gun_index))
  11329. {
  11330. setChargerMode(gun_index, MODE_PRECHARGE);
  11331. }
  11332. else if(time >= MAX_REQUEST_CHARGING_TIME)
  11333. {
  11334. LOG_INFO("Gun %d Grouping Timeout", gun_index + 1);
  11335. setChargerMode(gun_index, MODE_IDLE);
  11336. }
  11337. }
  11338. break;
  11339. case S_PREPARNING:
  11340. {
  11341. if (isModeChange(gun_index))
  11342. {
  11343. LOG_INFO("================== S_PREPARNING (%x) ================== \n", gun_index + 1);
  11344. //StopGunInfoTimeoutDet(gun_index);
  11345. //StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  11346. SetAcContactor(ON);
  11347. GetClockTime(&_SystemStatus_Time[gun_index]);
  11348. }
  11349. UpdateGunPresentChargingPower(gun_index);
  11350. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11351. if ((chargingInfo[gun_index]->AvailableChargingPower > 10) && time >= MIN_WAIT_PREPARNING_TIME)
  11352. {
  11353. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  11354. break;;
  11355. }
  11356. if (isEvBoardStopChargeFlag(gun_index) == YES)
  11357. {
  11358. // 板端或後臺要求停止
  11359. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARNING) **********", gun_index + 1);
  11360. Set_Ocpp_StopReason(gun_index, "Local");
  11361. ChargingAlarmProcess(gun_index);
  11362. }
  11363. else if(isNormalStopChargeFlag(gun_index) == YES)
  11364. {
  11365. LOG_INFO("********** Gun %d Normal Stop (S_PREPARNING) %s**********", gun_index + 1,
  11366. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11367. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11368. ChargingTerminalProcess(gun_index);
  11369. }
  11370. else if(isResetStopChargeFlag(gun_index) == YES)
  11371. {
  11372. LOG_INFO("********** Gun %d Reset Stop (S_PREPARNING) %s**********", gun_index + 1,
  11373. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11374. char *reason[16];
  11375. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11376. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11377. ChargingTerminalProcess(gun_index);
  11378. }
  11379. else if(isBackendStopFlag(gun_index))
  11380. {
  11381. LOG_INFO("********** Gun %d Backend Stop (S_PREPARNING) **********", gun_index + 1);
  11382. Set_Ocpp_StopReason(gun_index, "Other");
  11383. ChargingTerminalProcess(gun_index);
  11384. }
  11385. if(time > GUN_PREPARE_TIMEOUT)
  11386. {
  11387. LOG_INFO("********** Gun %d PrepareTimeout **********", gun_index + 1);
  11388. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  11389. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  11390. {
  11391. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "012279", 6);
  11392. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  11393. }
  11394. Set_Ocpp_StopReason(gun_index, "Local");
  11395. ChargingAlarmProcess(gun_index);
  11396. }
  11397. }
  11398. break;
  11399. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  11400. {
  11401. if (isModeChange(gun_index))
  11402. {
  11403. LOG_INFO("================== S_PREPARING_FOR_EV (%x) ================== \n", gun_index + 1);
  11404. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  11405. //StopGunInfoTimeoutDet(gun_index);
  11406. //StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  11407. GetClockTime(&_SystemStatus_Time[gun_index]);
  11408. }
  11409. UpdateGunPresentChargingPower(gun_index);
  11410. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11411. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  11412. {
  11413. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  11414. }
  11415. // only for test & debug purpose
  11416. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11417. {
  11418. LOG_INFO("Gun %d [Force Charging] S_PREPARING_FOR_EV OK", gun_index + 1);
  11419. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = true;
  11420. }
  11421. if (isEvBoardStopChargeFlag(gun_index) == YES)
  11422. {
  11423. // 板端或後臺要求停止
  11424. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  11425. Set_Ocpp_StopReason(gun_index, "Local");
  11426. ChargingAlarmProcess(gun_index);
  11427. }
  11428. else if(isNormalStopChargeFlag(gun_index) == YES)
  11429. {
  11430. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  11431. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11432. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11433. ChargingTerminalProcess(gun_index);
  11434. }
  11435. else if(isResetStopChargeFlag(gun_index) == YES)
  11436. {
  11437. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  11438. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11439. char *reason[16];
  11440. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11441. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11442. ChargingTerminalProcess(gun_index);
  11443. }
  11444. else if(isBackendStopFlag(gun_index))
  11445. {
  11446. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  11447. Set_Ocpp_StopReason(gun_index, "Other");
  11448. ChargingTerminalProcess(gun_index);
  11449. }
  11450. if(time >= GUN_EV_WAIT_TIMEOUT)
  11451. {
  11452. LOG_INFO("********** Gun %d Wait Dispenser Timeout **********", gun_index + 1);
  11453. ChargingTerminalProcess(gun_index);
  11454. }
  11455. }
  11456. break;
  11457. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  11458. {
  11459. if (isModeChange(gun_index))
  11460. {
  11461. LOG_INFO("================== S_PREPARING_FOR_EVSE (%x) ================== \n", gun_index + 1);
  11462. GetClockTime(&_SystemStatus_Time[gun_index]);
  11463. #ifdef STARTTRANSATION_IMMEDIATELY
  11464. OcppStartTransaction(gun_index);
  11465. #endif
  11466. }
  11467. CheckoutStartChargingSoc(gun_index, NO);
  11468. UpdateGunPresentChargingPower(gun_index);
  11469. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11470. // only for test & debug purpose
  11471. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11472. {
  11473. if(time >= FORCE_CHARGING_CMD_DELAY)
  11474. {
  11475. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  11476. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  11477. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  11478. {
  11479. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  11480. }
  11481. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  11482. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  11483. }
  11484. }
  11485. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Charging)
  11486. {
  11487. setChargerMode(gun_index, MODE_CHARGING);
  11488. }
  11489. // only for test & debug purpose
  11490. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11491. {
  11492. if(time >= FORCE_CHARGING_WAIT_EVSE)
  11493. {
  11494. LOG_INFO("Gun %d [Charging Simulation] S_PREPARING_FOR_EVSE OK", gun_index + 1);
  11495. setChargerMode(gun_index, MODE_CHARGING);
  11496. }
  11497. }
  11498. if (isEvBoardStopChargeFlag(gun_index) == YES)
  11499. {
  11500. // 板端或後臺要求停止
  11501. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  11502. Set_Ocpp_StopReason(gun_index, "Local");
  11503. ChargingAlarmProcess(gun_index);
  11504. }
  11505. else if(isNormalStopChargeFlag(gun_index) == YES)
  11506. {
  11507. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  11508. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11509. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11510. ChargingTerminalProcess(gun_index);
  11511. }
  11512. else if(isResetStopChargeFlag(gun_index) == YES)
  11513. {
  11514. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  11515. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11516. char *reason[16];
  11517. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11518. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11519. ChargingTerminalProcess(gun_index);
  11520. }
  11521. else if(isInvalidIdStopFlag(gun_index) == YES)
  11522. {
  11523. LOG_INFO("********** Gun %d InvalidId Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  11524. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  11525. ChargingTerminalProcess(gun_index);
  11526. }
  11527. else if(isBackendStopFlag(gun_index))
  11528. {
  11529. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  11530. Set_Ocpp_StopReason(gun_index, "Other");
  11531. ChargingTerminalProcess(gun_index);
  11532. }
  11533. if(time >= GUN_EV_WAIT_TIMEOUT)
  11534. {
  11535. LOG_INFO("********** Gun %d GFD timeout **********", gun_index + 1);
  11536. ChargingTerminalProcess(gun_index);
  11537. }
  11538. }
  11539. break;
  11540. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  11541. {
  11542. if (isModeChange(gun_index))
  11543. {
  11544. LOG_INFO("================== S_CHARGING (%x) ================== \n", gun_index + 1);
  11545. //StopGunInfoTimeoutDet(gun_index);
  11546. GetClockTime(&_SystemStatus_Time[gun_index]);
  11547. GetClockTime(&_ChargedEnergy_Time[gun_index]);
  11548. CheckoutStartChargingSoc(gun_index, YES);
  11549. chargingInfo[gun_index]->PresentChargedDuration = 0;
  11550. ChangeStartOrStopDateTime(YES, gun_index);
  11551. #ifndef STARTTRANSATION_IMMEDIATELY
  11552. OcppStartTransaction(gun_index);
  11553. #endif
  11554. }
  11555. // only for test & debug purpose
  11556. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  11557. {
  11558. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  11559. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  11560. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  11561. {
  11562. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  11563. }
  11564. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  11565. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  11566. }
  11567. // 計算 Power
  11568. //chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  11569. //chargingInfo[gun_index]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent))) / 1000 / 100);
  11570. UpdateGunPresentChargingPower(gun_index);
  11571. ChargedEnergySummation(gun_index);
  11572. CheckSessionTargetStop(gun_index);
  11573. if (isEvBoardStopChargeFlag(gun_index))
  11574. {
  11575. LOG_INFO("********** Gun %d Alarm Stop (S_CHARGING) **********", gun_index + 1);
  11576. Set_Ocpp_StopReason(gun_index, "Local");
  11577. ChargingAlarmProcess(gun_index);
  11578. }
  11579. else if(isNormalStopChargeFlag(gun_index) == YES)
  11580. {
  11581. LOG_INFO("********** Gun %d Normal Stop (S_CHARGING) %s**********", gun_index + 1,
  11582. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  11583. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  11584. ChargingTerminalProcess(gun_index);
  11585. }
  11586. else if(isResetStopChargeFlag(gun_index) == YES)
  11587. {
  11588. LOG_INFO("********** Gun %d Reset Stop (S_CHARGING) %s**********", gun_index + 1,
  11589. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  11590. char *reason[16];
  11591. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  11592. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  11593. ChargingTerminalProcess(gun_index);
  11594. }
  11595. else if(isInvalidIdStopFlag(gun_index) == YES)
  11596. {
  11597. LOG_INFO("********** Gun %d InvalidId Stop (S_CHARGING) **********", gun_index + 1);
  11598. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  11599. ChargingTerminalProcess(gun_index);
  11600. }
  11601. else if(CheckBackendChargingTimeout(gun_index) ||
  11602. CheckBackendChargingEnergy(gun_index) ||
  11603. IsReachConfigMaxChargingSoc(gun_index))
  11604. {
  11605. LOG_INFO("********** Gun %d Backend Condition Stop (S_CHARGING) **********", gun_index + 1);
  11606. ChargingTerminalProcess(gun_index);
  11607. }
  11608. else if(isSessionTargetStopFlag(gun_index) == YES)
  11609. {
  11610. LOG_INFO("********** Gun %d SessionTarget Stop (S_CHARGING) **********", gun_index + 1);
  11611. Set_Ocpp_StopReason(gun_index, "Other");
  11612. ChargingTerminalProcess(gun_index);
  11613. }
  11614. else if(isBackendStopFlag(gun_index))
  11615. {
  11616. LOG_INFO("********** Gun %d Backend Stop (S_CHARGING) **********", gun_index + 1);
  11617. Set_Ocpp_StopReason(gun_index, "Other");
  11618. ChargingTerminalProcess(gun_index);
  11619. }
  11620. }
  11621. break;
  11622. case S_ALARM:
  11623. case S_TERMINATING:
  11624. {
  11625. if (isModeChange(gun_index))
  11626. {
  11627. Set_Ocpp_StopReason(gun_index, "Other");
  11628. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  11629. {
  11630. LOG_INFO("================== S_ALARM (%x) ================== \n", gun_index + 1);
  11631. UpdateErrorCodeToOcpp(gun_index);
  11632. TheEndCharging(gun_index);
  11633. }
  11634. else
  11635. {
  11636. LOG_INFO("================== S_TERMINATING (%x) ================== \n", gun_index + 1);
  11637. }
  11638. //StopGunInfoTimeoutDet(gun_index);
  11639. GetClockTime(&_SystemStatus_Time[gun_index]);
  11640. }
  11641. UpdateGunPresentChargingPower(gun_index);
  11642. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11643. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  11644. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  11645. {
  11646. OcppStopTransaction(gun_index);
  11647. }
  11648. else
  11649. {
  11650. ChargedEnergySummation(gun_index);
  11651. }
  11652. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  11653. {
  11654. if(time >= GUN_COMP_WAIT_TIMEOUT)
  11655. {
  11656. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11657. {
  11658. LOG_INFO ("********** Gun %d psu is releasable **********\n", gun_index + 1);
  11659. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  11660. }
  11661. }
  11662. if(IsAvailableBackToIdle(gun_index) == YES &&
  11663. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11664. {
  11665. ReleaseAlarmCode(gun_index);
  11666. setChargerMode(gun_index, MODE_IDLE);
  11667. }
  11668. }
  11669. else
  11670. {
  11671. setChargerMode(gun_index, MODE_COMPLETE);
  11672. }
  11673. }
  11674. break;
  11675. case S_COMPLETE:
  11676. {
  11677. if (isModeChange(gun_index))
  11678. {
  11679. LOG_INFO("================== S_COMPLETE (%x) ================== \n", gun_index + 1);
  11680. TheEndCharging(gun_index);
  11681. GetClockTime(&_SystemStatus_Time[gun_index]);
  11682. }
  11683. UpdateGunPresentChargingPower(gun_index);
  11684. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  11685. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  11686. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  11687. {
  11688. OcppStopTransaction(gun_index);
  11689. }
  11690. else
  11691. {
  11692. ChargedEnergySummation(gun_index);
  11693. }
  11694. if(time >= GUN_COMP_WAIT_TIMEOUT)
  11695. {
  11696. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11697. {
  11698. LOG_INFO ("********** Gun %d psu is releasable **********", gun_index + 1);
  11699. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  11700. }
  11701. }
  11702. if(IsAvailableBackToIdle(gun_index) == YES &&
  11703. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11704. {
  11705. setChargerMode(gun_index, MODE_IDLE);
  11706. }
  11707. }
  11708. break;
  11709. case S_CCS_PRECHARGE_ST0:
  11710. if (isModeChange(gun_index))
  11711. {
  11712. LOG_INFO("CCS Precharge Processing 1....................%x \n", gun_index);
  11713. //StopGunInfoTimeoutDet(gun_index);
  11714. //StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  11715. }
  11716. break;
  11717. case S_CCS_PRECHARGE_ST1:
  11718. if (isModeChange(gun_index))
  11719. {
  11720. LOG_INFO("CCS Precharge Processing 2....................%x \n", gun_index);
  11721. }
  11722. break;
  11723. case S_DEBUG:
  11724. if (isModeChange(gun_index))
  11725. {
  11726. LOG_INFO("================== S_DEBUG (%x) ================== \n", gun_index + 1);
  11727. }
  11728. if(!ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay &&
  11729. !ShmChargerInfo->Control.FanCtrl.bits.AbnormalFan)
  11730. {
  11731. setChargerMode(gun_index, MODE_IDLE);
  11732. }
  11733. break;
  11734. }
  11735. }
  11736. TryFeedWatchdog();
  11737. usleep(whileLoopTime);
  11738. }
  11739. return FAIL;
  11740. }