main.c 441 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427
  1. /*
  2. * main.c
  3. *
  4. * Created on: 2020年9月10日
  5. * Author: Wendell
  6. */
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/time.h>
  10. #include <sys/timeb.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/mman.h>
  17. #include <linux/wireless.h>
  18. #include <arpa/inet.h>
  19. #include <netinet/in.h>
  20. #include <unistd.h>
  21. #include <stdarg.h>
  22. #include <stdio.h> /*標準輸入輸出定義*/
  23. #include <stdlib.h> /*標準函數庫定義*/
  24. #include <unistd.h> /*Unix 標準函數定義*/
  25. #include <fcntl.h> /*檔控制定義*/
  26. #include <termios.h> /*PPSIX 終端控制定義*/
  27. #include <errno.h> /*錯誤號定義*/
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <math.h>
  35. #include "../../define.h"
  36. #include "Config.h"
  37. #include <stdbool.h>
  38. #include <dirent.h>
  39. #include "timeout.h"
  40. #include "Module_EvComm.h"
  41. #include "Module_CabinetParallel.h"
  42. #include "Common.h"
  43. #define BTN_RELEASE 0
  44. #define BTN_PRESS 1
  45. #define MAX_BUF 64
  46. #define MtdBlockSize 0x300000
  47. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  48. #define UPGRADE_FAN 0x02
  49. #define UPGRADE_RB 0x03
  50. #define UPGRADE_PRI 0x04
  51. #define UPGRADE_AC 0x05
  52. #define UPGRADE_LED 0x06
  53. #define UPGRADE_DORC1 0x07
  54. #define UPGRADE_DORC2 0x08
  55. #define UPGRADE_DDRB 0x09
  56. #define SYSTEM_MIN_VOL 150
  57. #define MIN_OUTPUT_CUR 0
  58. #define AC_OUTPUT_VOL 220
  59. #define NO_DEFINE 255
  60. #define DEFAULT_AC_INDEX 2
  61. #define PSU_MIN_CUR 100
  62. #define MAIN_PRIORITY_INTERVAL 5
  63. #define CABINET_ROLE_TIMEOUT 3 // unit: 1s
  64. #define GUN_PREPARE_TIMEOUT 30
  65. #define GUN_EV_WAIT_TIMEOUT (120 + 10)
  66. #define GUN_EVSE_WAIT_TIMEOUT (60 + 10)
  67. #define GUN_COMP_WAIT_TIMEOUT 10
  68. #define GUN_PRECHARGING_TIMEOUT 60
  69. #define REBOOT_TIMEOUT 10
  70. #define RESET_TIMEOUT 10
  71. #define OCPP_COST_REQ_INTERVAL 1
  72. #define DISPENSER_RECONNECTE_TIME 30
  73. #define DISPENSER_AUTH_COMP_TIME 3
  74. #define FORCE_BALANCE_TIME 5
  75. #define DISPENSER_UPGRADE_TIME 600
  76. #define AC_CONTACTOR_REOPEN_TIME 60
  77. #define PSU_FAILURE_RESUME_TIME 180
  78. #define MAX_AC_CONTACTOR_COUNT 3
  79. #define STANDBY_TIME 600
  80. #define FAST_STANDBY_TIME 30
  81. #define WIRING_INFO_DELAY 30
  82. #define WAIT_REASSIGN_TIME 40 // unit: second
  83. #define MAX_REQUEST_CHARGING_TIME 15 // unit: second
  84. #define MIN_WAIT_PREPARNING_TIME 1 // unit: second
  85. #define FORCE_CHARGING_CMD_DELAY 3 // unit: second
  86. #define FORCE_CHARGING_WAIT_EVSE 10 // unit: second
  87. #define TRY_RESET_4G_WIFI_TIME 300 // unit: second
  88. #define FAST_RESET_4G_WIFI_TIME 60 // unit: second
  89. #define ENERAGY_INTERVAL 1 // unit: second
  90. #define FAULT_RELEASE_TIME 2 // unit: second
  91. #define MAINTAIN_RECOVERY_TIME 1 // unit: second
  92. #define PARALLEL_WAIT_TIME 3 // unit: second
  93. #define START_UPGRADE_TIME 60 // unit: second
  94. #define PCABINET_UPGRADE_TIME 300 // unit: second
  95. #define MCABINET_UPGRADE_TIME 60 // unit: second
  96. #define CONNECTION_TIME_OFFSET 5 // unit: second
  97. #define PLUG_IN_HOLD_TIME 1 // unit: second
  98. #define FINAL_COST_TIMEOUT 3 // unit: second
  99. #define FINAL_COST_UPDATE_TIME 10 // unit: second
  100. #define MAX_FINAL_COST_MISC_COUNT 3
  101. #define PARKING_STATUS_TIMEOUT 3 // unit: second
  102. #define OCCUPANCY_FEE_TIMEOUT 10 // unit: second
  103. #define MIN_AVAILABLE_MEMORY 200 // unit: MB
  104. #define AUTO_START_CHARGING "AutoStartCharging"
  105. #define CONTACTOR_AUTO_ON_OFF 0
  106. #define CONTACTOR_AUTO_ON_TIME 60
  107. #define CONTACTOR_AUTO_OFF_TIME 300
  108. #define WAIT_DISPENSER_STOP_TIMEOUT 3 // unit: second
  109. #define STARTTRANSATION_TIME 1
  110. #define STOP_TARTTRANSATION_DELAY 2
  111. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  112. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  113. int whileLoopTime = 10000; // 10 ms
  114. int wtdFd = -1;
  115. byte _authorizeIndex = NO_DEFINE;
  116. BOOL _UpgradeNeedReboot = FALSE;
  117. bool _isBridgeModeConfig = false;
  118. #define GUN_DERATING_COUNT 2
  119. const int GunDeratingTemp[GUN_DERATING_COUNT] = {80, 85};
  120. const byte Group_to_Location_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  121. const byte Location_to_Group_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  122. const byte GroupParallelRelay_2_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  123. {
  124. {0, 1, 0, 0},
  125. {1, 0, 0, 0},
  126. {0, 0, 0, 0},
  127. {0, 0, 0, 0}
  128. };
  129. //Location: 1 2 3 4
  130. // Gun: 1 3 4 2
  131. //Parallel: 1 2 3
  132. //const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 1, 2};
  133. //const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 2, 3, 1};
  134. //const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  135. //{
  136. // {0, 3, 1, 2}, // group 0
  137. // {1, 0, 2, 3}, // group 1
  138. // {1, 3, 0, 2}, // group 2
  139. // {1, 3, 2, 0} // group 3
  140. //};
  141. //Location: 1 2 3 4
  142. // Gun: 1 4 3 2
  143. //Parallel: 1 2 3
  144. const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  145. const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  146. const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  147. {
  148. {0, 3, 2, 1}, // group 0
  149. {1, 0, 3, 2}, // group 1
  150. {1, 3, 0, 2}, // group 2
  151. {1, 3, 2, 0} // group 3
  152. };
  153. const byte GroupParallelRelay_4_Gun_6Parallel[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  154. {
  155. {0, 4, 6, 1}, // group 0
  156. {4, 0, 3, 5}, // group 1
  157. {6, 3, 0, 2}, // group 2
  158. {1, 5, 2, 0} // group 3
  159. };
  160. long long DiffTimebWithNow(struct timeb ST);
  161. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  162. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  163. void ChargingTerminalProcess(byte gunIndex);
  164. void ChkPrimaryStatus();
  165. void ChargingAlarmProcess(byte gunIndex);
  166. void InformOcppErrOccur(byte codeType);
  167. void SetOcppErrorCode(unsigned char _index, char *errCode);
  168. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode);
  169. void ReleaseAlarmCode(byte gunIndex);
  170. int DB_Open(sqlite3 *db);
  171. int DB_Insert_Record(sqlite3 *db, int gun_index);
  172. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  173. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  174. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value);
  175. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value);
  176. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index);
  177. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index);
  178. float Get_PowerConsumption(uint8_t gun_index);
  179. void Update_PowerConsumption(uint8_t gun_index);
  180. int DB_Insert_FinalCost(sqlite3 *db, uint8_t gun_index, int txId, char *description);
  181. int DB_Update_FinalCost_Status(sqlite3 *db, int txId, int status);
  182. int DB_Get_Undisposed_FinalCost(sqlite3 *db, int gunIndex, char *costString);
  183. int DB_Reboot_Record(sqlite3 *db);
  184. void InitialDHCP();
  185. void InitialDispenserDhcpServerConfig(void);
  186. void StartDispenserDhcpServer(void);
  187. int GetStartScheduleTime(unsigned char *time);
  188. void CheckConnectionTimeout(void);
  189. bool IsParallelCabinetIdle(void);
  190. bool IsStandbyEnable(void);
  191. bool IsGunIdle(int gunIndex, int idleType);
  192. bool IsDispenserIdle(int dispenserIndex);
  193. bool IsConnectorWholeIdle(void);
  194. void SetAcContactor(unsigned char OnOff);
  195. void UpdateErrorCodeToOcpp(byte index);
  196. bool CheckConnectorTypeStatus(void);
  197. void CreateWatchdog(void);
  198. void TryCloseWatchdog(void);
  199. void TryFeedWatchdog(void);
  200. bool IsReadyParalleling(void);
  201. void SetParallelingStart(void);
  202. void SetParallelingDone(void);
  203. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff);
  204. void SetParallelCabinetAcContactor(unsigned char OnOff);
  205. unsigned short GetTotalParallelCabinetPower(void);
  206. void MCabinetParallelingProcess(void);
  207. void SCabinetControllProcess(void);
  208. bool Is_WebServer_Alive(void);
  209. void Run_WebServer_Service(void);
  210. bool Get_Ocpp_ConnectionStatus(void);
  211. void Run_Ocpp_Service(void);
  212. bool Is_Ocpp_Task_Alive(void);
  213. void Kill_Ocpp_Service(void);
  214. void Run_MaintainOcpp_Service(void);
  215. bool Is_MaintainOcpp_Task_Alive(void);
  216. void Kill_MaintainOcpp_Service(void);
  217. long int Get_Ocpp_procDogTime(void);
  218. void Clean_Ocpp_procDogTime(void);
  219. bool Is_Ocpp_BootNotification(void);
  220. void Clean_Ocpp_BootNotification(void);
  221. bool Is_Ocpp_ResetReq(void);
  222. bool Chk_Ocpp_Reset_Type(char *type);
  223. void Clean_Ocpp_ResetReq(void);
  224. void Set_Ocpp_AuthorizeReq(void);
  225. bool Is_Ocpp_AuthorizeConf(void);
  226. bool Is_Ocpp_Authorize_Status(char *status);
  227. bool Is_Ocpp_UpdateFirmwareReq(void);
  228. void Clean_Ocpp_UpdateFirmwareReq(void);
  229. bool Is_Ocpp_UpdateFirmwareStatus(char *status);
  230. void Set_Ocpp_UpdateFirmwareStatus(char *status);
  231. void Clean_Ocpp_UpdateFirmwareStatus(char *status);
  232. void Set_Ocpp_FirmwareStatusNotificationReq(void);
  233. bool Is_Ocpp_StopOnInvalidId_Enable(void);
  234. bool Is_Ocpp_AuthorizeRemoteTxRequests(void);
  235. int Get_Ocpp_ConnectionTimeOut(void);
  236. void Get_Ocpp_DefaultPrice(char *defaultPrice);
  237. void Get_Ocpp_UserPrice(char * userId, char *userPrice);
  238. void Clean_Ocpp_UserPrice(void);
  239. void Get_Ocpp_RunningCost(int gun_index, char *cost);
  240. void Clean_Ocpp_RunningCost(int gun_index);
  241. void Get_Ocpp_FinalCost(int gun_index, char *cost);
  242. void Clean_Ocpp_FinalCost(int gun_index);
  243. int Get_Ocpp_FinalCostTxId(int gun_index);
  244. void Get_Ocpp_TimeOffset(char *timeOffset);
  245. void Get_Ocpp_PreAuthAmount(char *amount);
  246. void Get_Ocpp_PowerSharingServerIP(char *ip);
  247. void Get_Ocpp_Refunding(char *Refunding);
  248. void Clean_Ocpp_Refunding(void);
  249. bool Is_Ocpp_LcdOverideReq(int *gun, int *page);
  250. void Clean_Ocpp_LcdOverideReq(void);
  251. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name);
  252. bool Is_Ocpp_ChargerStationReq(void);
  253. void Clean_Ocpp_ChargerStationReq(void);
  254. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature);
  255. bool Is_Ocpp_WeatherReq(void);
  256. void Clean_Ocpp_WeatherReq(void);
  257. void Set_Ocpp_DeductTransaction(int transaction);
  258. void Set_Ocpp_DeductCreditNo(char *userId);
  259. void Set_Ocpp_DeductVemData(char *vemData);
  260. void Set_Ocpp_DeductAmount(double value);
  261. void Set_Ocpp_DeductResult(unsigned char result);
  262. void Set_Ocpp_DonateInvoice(unsigned char donate);
  263. bool Is_Ocpp_ReportCreditDeductReq(void);
  264. void Set_Ocpp_ReportCreditDeductReq(void);
  265. void Clean_Ocpp_TcciSerialNo(int gun_index);
  266. bool Set_Ocpp_TcciReaderStatus(int gun_index, DeductResultInfoData *ReaderStatus);
  267. void Clean_Ocpp_TcciCreditInfo(int gun_index);
  268. void Set_Ocpp_TcciReaderStatusReq(int gun_index);
  269. bool Is_Ocpp_TcciReaderStatusReq(int gun_index);
  270. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id);
  271. void Set_Ocpp_StartTransactionReq(int gun_index);
  272. bool Is_Ocpp_StartTransactionConf(int gun_index);
  273. void Clean_Ocpp_StartTransactionConf(int gun_index);
  274. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status);
  275. bool Is_Ocpp_StartTransaction_Accepted(int gun_index);
  276. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status);
  277. void Get_Ocpp_TransactionId(int gun_index, char *transactionId);
  278. bool Is_Invalid_StartTransactionId(int gun_index);
  279. void Set_Ocpp_StopTransactionId(int gun_index, char *id);
  280. void Set_Ocpp_StopTransactionReq(int gun_index);
  281. void Chk_Ocpp_Backup_ErrorCode(int gun_index);
  282. bool Is_Ocpp_ReserveNowReq(int gun_index);
  283. void Clean_Ocpp_ReserveNowReq(int gun_index);
  284. int Get_Ocpp_ReserveNowId(int gun_index);
  285. void Set_Ocpp_ReserveNowConf(int gun_index);
  286. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date);
  287. bool Is_Ocpp_ReserveNowExpired(int gun_index);
  288. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag);
  289. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag);
  290. bool Is_Ocpp_CancelReservationReq(int gun_index);
  291. void Clean_Ocpp_CancelReservationReq(int gun_index);
  292. void Set_Ocpp_CancelReservationConf(int gun_index);
  293. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index);
  294. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index);
  295. bool Chk_Ocpp_Availability_Type(int gun_index, char *type);
  296. bool Is_Ocpp_UnlockConnectorReq(int gun_index);
  297. void Clean_Ocpp_UnlockConnectorReq(int gun_index);
  298. void Set_Ocpp_UnlockConnectorConf(int gun_index);
  299. bool Is_Ocpp_RemoteStartReq(int gun_index);
  300. void Clean_Ocpp_RemoteStartReq(int gun_index);
  301. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag);
  302. bool Is_Ocpp_RemoteStopReq(int gun_index);
  303. void Clean_Ocpp_RemoteStopReq(int gun_index);
  304. void Set_Ocpp_StopReason(int gun_index, char *reason);
  305. void Get_Ocpp_StopReason(int gun_index, char *reason);
  306. void Clean_Ocpp_StopReason(int gun_index);
  307. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  308. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  309. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  310. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  311. bool Is_Ocpp_ChargingProfileConf(int gun_index);
  312. void Clean_Ocpp_ChargingProfileConf(int gun_index);
  313. bool Is_Ocpp_ChargingProfileReq(int gun_index);
  314. void Set_Ocpp_ChargingProfileReq(int gun_index);
  315. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind);
  316. bool Is_Ocpp_ChargingProfileId(int gun_index);
  317. int Get_Ocpp_ChargingProfileStartTime(int gun_index);
  318. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index);
  319. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index);
  320. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index);
  321. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index);
  322. int Get_Ocpp_ChargingProfileStartIndex(int gun_index);
  323. bool Is_Ocpp_MaxChargingProfileKind(char *kind);
  324. bool Is_Ocpp_MaxChargingProfileId(void);
  325. int Get_Ocpp_MaxProfileStartTime(void);
  326. int Get_Ocpp_MaxProfileScheduleCount(void);
  327. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index);
  328. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index);
  329. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index);
  330. int Get_Ocpp_MaxProfileStartIndex(void);
  331. int Is_Ocpp_RemoteStartWaitReq(void);
  332. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget);
  333. int Get_Ocpp_OffLineMaxChargingPower(void);
  334. void Set_Deduct_UserId(int gunIndex, char *userId);
  335. void Set_Deduct_TransactionId(int gunIndex, int transaction);
  336. void Clean_DeductInfo(int gunIndex);
  337. bool Is_DeductResultReq(int gunIndex);
  338. bool Is_ReaderStatusReq(int gunIndex);
  339. void Clean_ReaderStatusInfo(int gunIndex);
  340. void Set_UserTransactionId(int gunIndex, int transaction);
  341. void Set_UserPrice(int gunIndex, float price);
  342. void Set_UserTotalCost(int gunIndex, float cost);
  343. void Set_UserAccountBalance(int gunIndex, float balance);
  344. void Set_UserDiscount(int gunIndex, float discount);
  345. void Clean_UserTransaction(int gunIndex);
  346. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  347. struct StatusCodeData *ShmStatusCodeData;
  348. struct PsuData *ShmPsuData;
  349. struct CHAdeMOData *ShmCHAdeMOData;
  350. struct GBTData *ShmGBTData;
  351. struct CcsData *ShmCcsData;
  352. struct PrimaryMcuData *ShmPrimaryMcuData;
  353. struct FanModuleData *ShmFanModuleData;
  354. struct RelayModuleData *ShmRelayModuleData[2];
  355. struct LedModuleData *ShmLedModuleData;
  356. struct OCPP16Data *ShmOCPP16Data;
  357. struct OCPP20Data *ShmOCPP20Data;
  358. struct OCPP16Data *ShmOCPP16DataPH;
  359. struct MeterInformation *ShmCsuMeterData;
  360. ChargerInfoData *ShmChargerInfo;
  361. PsuPositionInfoData *ShmPsuPosition;
  362. PsuGroupingInfoData *ShmPsuGrouping;
  363. struct ChargingInfoData *chargingInfo[CONNECTOR_QUANTITY];
  364. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  365. struct timespec _cmdMainPriority_time;
  366. struct timespec _cmdSubPriority_time;
  367. struct timespec _ConnectorAuthorizing_Time[CONNECTOR_QUANTITY];
  368. struct timespec _ConnectorAuthorizeFail_Time[CONNECTOR_QUANTITY];
  369. struct timespec _DispenserAuthorizing_Time[CONNECTOR_QUANTITY];
  370. struct timespec _DispenserUpgrade_time;
  371. struct timespec _Standby_time;
  372. struct timespec _SystemStatus_Time[CONNECTOR_QUANTITY];
  373. struct timespec _ChargedEnergy_Time[CONNECTOR_QUANTITY];
  374. struct timespec _Parallel_Time;
  375. struct timespec _RemoteStartNoId_Time;
  376. float gunOutputVol[CONNECTOR_QUANTITY];
  377. unsigned char _ocppProfileChkFlag[CONNECTOR_QUANTITY];
  378. float _lastProfilePower[CONNECTOR_QUANTITY];
  379. float _lastProfileCurrent[CONNECTOR_QUANTITY];
  380. float _lastMaxProfilePower;
  381. float _lsatGunMeterValue[CONNECTOR_QUANTITY];
  382. struct StructStatusNotification _BackupStatusNotification[CONNECTOR_QUANTITY];
  383. bool _useGunMeterValue[MAX_GUN_QUANTITY];
  384. bool _triggerTransaction[MAX_GUN_QUANTITY];
  385. bool _plugInHold[MAX_GUN_QUANTITY];
  386. struct timespec _PlugInHold_Time[MAX_GUN_QUANTITY];
  387. struct timespec _DispenserBill_Time[MAX_DISPENSER_QUANTITY];
  388. struct timespec _ConnectorBill_Time[MAX_GUN_QUANTITY];
  389. struct timespec _ParkingStatus_Time[MAX_GUN_QUANTITY];
  390. struct timespec _GetOccupancyFee_Time[MAX_GUN_QUANTITY];
  391. bool _canUpdateFinalCost[MAX_DISPENSER_QUANTITY];
  392. bool _isUndisposedFinalCost[MAX_DISPENSER_QUANTITY];
  393. bool _isGetOccupancyFeeReq[MAX_GUN_QUANTITY];
  394. bool _NeedReset4gWifi;
  395. struct timespec _4gWifiReset_time;
  396. int _psuLostQuantity = 0;
  397. bool _isStandbyEnable = true;
  398. bool isModelNameMatch = true;
  399. //int rfidFd = -1;
  400. //char* rfidPortName = "/dev/ttyS2";
  401. #if ENABLE_PCBA_TEST == 0
  402. char* fwVersion = "V2.05.00.0000.00";
  403. char* subVersion = "01";
  404. #else
  405. char* fwVersion = "PCBA.00.04";
  406. char* subVersion = "00";
  407. #endif
  408. sqlite3 *localDb;
  409. bool isDb_ready;
  410. sqlite3 *networkDb;
  411. //================================================
  412. // initial can-bus
  413. //================================================
  414. int InitCanBus()
  415. {
  416. int s0,nbytes;
  417. struct timeval tv;
  418. struct ifreq ifr0;
  419. struct sockaddr_can addr0;
  420. system("/sbin/ip link set can0 down");
  421. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  422. system("/sbin/ip link set can0 up");
  423. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  424. tv.tv_sec = 0;
  425. tv.tv_usec = 10000;
  426. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  427. {
  428. #ifdef SystemLogMessage
  429. LOG_ERROR("Set SO_RCVTIMEO NG");
  430. #endif
  431. }
  432. nbytes=40960;
  433. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  434. {
  435. #ifdef SystemLogMessage
  436. LOG_ERROR("Set SO_RCVBUF NG");
  437. #endif
  438. }
  439. nbytes=40960;
  440. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  441. {
  442. #ifdef SystemLogMessage
  443. LOG_ERROR("Set SO_SNDBUF NG");
  444. #endif
  445. }
  446. strcpy(ifr0.ifr_name, "can0" );
  447. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  448. addr0.can_family = AF_CAN;
  449. addr0.can_ifindex = ifr0.ifr_ifindex;
  450. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  451. return s0;
  452. }
  453. //================================================
  454. // initial uart port
  455. //================================================
  456. char *_priPortName = "/dev/ttyS1";
  457. char *_485PortName = "/dev/ttyS5";
  458. int InitComPort(byte target)
  459. {
  460. int fd;
  461. struct termios tios;
  462. if(target == UPGRADE_PRI)
  463. fd = open(_priPortName, O_RDWR);
  464. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  465. fd = open(_485PortName, O_RDWR);
  466. if(fd<=0)
  467. {
  468. #ifdef SystemLogMessage
  469. LOG_ERROR("open 407 Communication port NG");
  470. #endif
  471. return -1;
  472. }
  473. ioctl (fd, TCGETS, &tios);
  474. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  475. tios.c_lflag = 0;
  476. tios.c_iflag = 0;
  477. tios.c_oflag = 0;
  478. tios.c_cc[VMIN]=0;
  479. tios.c_cc[VTIME]=(unsigned char)1;
  480. tios.c_lflag=0;
  481. tcflush(fd, TCIFLUSH);
  482. ioctl (fd, TCSETS, &tios);
  483. return fd;
  484. }
  485. //=================================
  486. // Common routine
  487. //=================================
  488. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  489. {
  490. strncpy(dest, src + start, cnt);
  491. dest[cnt] = 0;
  492. }
  493. int InitWatchDog()
  494. {
  495. int fd;
  496. int timeout = 180;
  497. system("/usr/bin/fuser -k /dev/watchdog");
  498. sleep(1);
  499. system("echo V > /dev/watchdog");
  500. sleep(1);
  501. fd=open("/dev/watchdog", O_RDWR);
  502. if(fd<=0)
  503. {
  504. LOG_ERROR("System watch dog initial fail.\r\n");
  505. }
  506. ioctl(fd, _IOWR('W', 6, int), &timeout);
  507. return fd;
  508. }
  509. void setChargerMode(byte gun_index, byte mode)
  510. {
  511. chargingInfo[gun_index]->SystemStatus = mode;
  512. }
  513. long long DiffTimebWithNow(struct timeb ST)
  514. {
  515. //return milli-second
  516. struct timeb ET;
  517. long long StartTime,StopTime;
  518. ftime(&ET);
  519. StartTime=(long long)ST.time;
  520. StopTime=(long long)ET.time;
  521. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  522. }
  523. //=================================
  524. // Create all share memory
  525. //=================================
  526. int CreateShareMemory()
  527. {
  528. int MeterSMId;
  529. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  530. {
  531. #ifdef SystemLogMessage
  532. LOG_ERROR("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG");
  533. #endif
  534. return 0;
  535. }
  536. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  537. {
  538. #ifdef SystemLogMessage
  539. LOG_ERROR("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG");
  540. #endif
  541. return 0;
  542. }
  543. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  544. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  545. {
  546. #ifdef SystemLogMessage
  547. LOG_ERROR("[main]CreatShareMemory:shmget ShmStatusCodeData NG");
  548. #endif
  549. return 0;
  550. }
  551. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  552. {
  553. #ifdef SystemLogMessage
  554. LOG_ERROR("[main]CreatShareMemory:shmat ShmStatusCodeData NG");
  555. #endif
  556. return 0;
  557. }
  558. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  559. //creat ShmPsuData
  560. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  561. {
  562. #ifdef SystemLogMessage
  563. LOG_ERROR("[main]CreatShareMemory:shmget ShmPsuData NG");
  564. #endif
  565. return 0;
  566. }
  567. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  568. {
  569. #ifdef SystemLogMessage
  570. LOG_ERROR("[main]CreatShareMemory:shmat ShmPsuData NG");
  571. #endif
  572. return 0;
  573. }
  574. memset(ShmPsuData, 0, sizeof(struct PsuData));
  575. /*
  576. if(CHAdeMO_QUANTITY > 0)
  577. {
  578. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  579. {
  580. #ifdef SystemLogMessage
  581. LOG_ERROR("[main]CreatShareMemory:shmget ShmCHAdeMOData NG");
  582. #endif
  583. return 0;
  584. }
  585. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  586. {
  587. #ifdef SystemLogMessage
  588. LOG_ERROR("[main]CreatShareMemory:shmat ShmCHAdeMOData NG");
  589. #endif
  590. return 0;
  591. }
  592. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  593. }
  594. if(GB_QUANTITY > 0)
  595. {
  596. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  597. {
  598. #ifdef SystemLogMessage
  599. LOG_ERROR("[main]CreatShareMemory:shmget ShmGBTData NG");
  600. #endif
  601. return 0;
  602. }
  603. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  604. {
  605. #ifdef SystemLogMessage
  606. LOG_ERROR("[main]CreatShareMemory:shmat ShmGBTData NG");
  607. #endif
  608. return 0;
  609. }
  610. memset(ShmGBTData, 0, sizeof(struct GBTData));
  611. }
  612. //creat ShmCcsData
  613. if(CCS_QUANTITY > 0)
  614. {
  615. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  616. {
  617. #ifdef SystemLogMessage
  618. LOG_ERROR("[main]CreatShareMemory:shmget ShmCcsData NG");
  619. #endif
  620. return 0;
  621. }
  622. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  623. {
  624. #ifdef SystemLogMessage
  625. LOG_ERROR("[main]CreatShareMemory:shmat ShmCcsData NG");
  626. #endif
  627. return 0;
  628. }
  629. memset(ShmCcsData, 0, sizeof(struct CcsData));
  630. }
  631. */
  632. //creat ShmPrimaryMcuData
  633. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  634. {
  635. #ifdef SystemLogMessage
  636. LOG_ERROR("[main]CreatShareMemory:shmget ShmPrimaryMcuData NGn");
  637. #endif
  638. return 0;
  639. }
  640. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  641. {
  642. #ifdef SystemLogMessage
  643. LOG_ERROR("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG");
  644. #endif
  645. return 0;
  646. }
  647. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  648. //creat ShmFanModuleData
  649. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  650. {
  651. #ifdef SystemLogMessage
  652. LOG_ERROR("[main]CreatShareMemory:shmget ShmFanModuleData NG");
  653. #endif
  654. return 0;
  655. }
  656. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  657. {
  658. #ifdef SystemLogMessage
  659. LOG_ERROR("[main]CreatShareMemory:shmat ShmFanModuleData NG");
  660. #endif
  661. return 0;
  662. }
  663. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  664. //creat ShmRelayModuleData
  665. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  666. {
  667. #ifdef SystemLogMessage
  668. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[0] NG");
  669. #endif
  670. return 0;
  671. }
  672. else if ((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  673. {
  674. #ifdef SystemLogMessage
  675. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[0] NG");
  676. #endif
  677. return 0;
  678. }
  679. memset(ShmRelayModuleData[0], 0, sizeof(struct RelayModuleData));
  680. // DO360 RC2
  681. if ((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  682. {
  683. #ifdef SystemLogMessage
  684. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[1] NG");
  685. #endif
  686. return 0;
  687. }
  688. else if ((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  689. {
  690. #ifdef SystemLogMessage
  691. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[1] NG");
  692. #endif
  693. return 0;
  694. }
  695. memset(ShmRelayModuleData[1], 0, sizeof(struct RelayModuleData));
  696. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  697. {
  698. #ifdef SystemLogMessage
  699. LOG_ERROR("[main]CreatShareMemory:shmget ShmLedModuleData NG");
  700. #endif
  701. return 0;
  702. }
  703. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  704. {
  705. #ifdef SystemLogMessage
  706. LOG_ERROR("[main]CreatShareMemory:shmat ShmLedModuleData NG");
  707. #endif
  708. return 0;
  709. }
  710. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  711. //creat ShmOCPP16Data
  712. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  713. {
  714. #ifdef SystemLogMessage
  715. LOG_ERROR("[main]CreatShareMemory:shmget ShmOCPP16Data NG");
  716. #endif
  717. return 0;
  718. }
  719. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  720. {
  721. #ifdef SystemLogMessage
  722. LOG_ERROR("[main]CreatShareMemory:shmat ShmOCPP16Data NG");
  723. #endif
  724. return 0;
  725. }
  726. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  727. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  728. {
  729. LOG_ERROR("[main]CreatShareMemory:shmget OCPP20Data NG");
  730. return 0;
  731. }
  732. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  733. {
  734. LOG_ERROR("[main]CreatShareMemory:shmat OCPP20Data NG");
  735. return 0;
  736. }
  737. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  738. //creat ShmOCPP16DataPH
  739. if ((MeterSMId = shmget(ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  740. {
  741. LOG_ERROR("shmget ShmOCPP16DataPH NG");
  742. return 0;
  743. }
  744. else if ((ShmOCPP16DataPH = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  745. {
  746. LOG_ERROR("shmat ShmOCPP16DataPH NG");
  747. return 0;
  748. }
  749. memset(ShmOCPP16DataPH,0,sizeof(struct OCPP16Data));
  750. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), IPC_CREAT | 0777)) < 0)
  751. {
  752. #ifdef SystemLogMessage
  753. LOG_ERROR("[main]CreatShareMemory:shmget ChargerInfoData NG");
  754. #endif
  755. return 0;
  756. }
  757. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  758. {
  759. #ifdef SystemLogMessage
  760. LOG_ERROR("[main]CreatShareMemory:shmat ChargerInfoData NG");
  761. #endif
  762. return 0;
  763. }
  764. memset(ShmChargerInfo, 0, sizeof(ChargerInfoData));
  765. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  766. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  767. return 1;
  768. }
  769. //======================================================
  770. // Peripheral initial
  771. //======================================================
  772. void InitGPIO()
  773. {
  774. /*****************0~3, 4 bank, bank x 32+ num*********************/
  775. /***************************************************************/
  776. /*************** GPIO 0 ***************************************/
  777. /***************************************************************/
  778. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/ // no use
  779. system("echo 22 > /sys/class/gpio/export");
  780. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  781. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/ // no use
  782. system("echo 23 > /sys/class/gpio/export");
  783. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  784. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/ // no use
  785. system("echo 26 > /sys/class/gpio/export");
  786. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  787. system("echo 1 > /sys/class/gpio/gpio26/value");
  788. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/ // no use
  789. system("echo 27 > /sys/class/gpio/export");
  790. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  791. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  792. system("echo 29 > /sys/class/gpio/export");
  793. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  794. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/ // no use
  795. system("echo 19 > /sys/class/gpio/export");
  796. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  797. system("echo 1 > /sys/class/gpio/gpio19/value");
  798. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/ // no use
  799. system("echo 20 > /sys/class/gpio/export");
  800. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  801. /***************************************************************/
  802. /*************** GPIO 1 ***************************************/
  803. /***************************************************************/
  804. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/ // no use
  805. system("echo 44 > /sys/class/gpio/export");
  806. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  807. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/ // no use
  808. system("echo 45 > /sys/class/gpio/export");
  809. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  810. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/ // no use
  811. system("echo 46 > /sys/class/gpio/export");
  812. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  813. system("echo 0 > /sys/class/gpio/gpio46/value");
  814. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/ // no use
  815. system("echo 47 > /sys/class/gpio/export");
  816. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  817. /***************************************************************/
  818. /*************** GPIO 2 ***************************************/
  819. /***************************************************************/
  820. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  821. system("echo 89 > /sys/class/gpio/export");
  822. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  823. system("echo 1 > /sys/class/gpio/gpio89/value");
  824. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  825. system("echo 87 > /sys/class/gpio/export");
  826. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  827. system("echo 0 > /sys/class/gpio/gpio87/value");
  828. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/ // no use
  829. system("echo 88 > /sys/class/gpio/export");
  830. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  831. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/ // no use
  832. system("echo 86 > /sys/class/gpio/export");
  833. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  834. /***************************************************************/
  835. /*************** GPIO 3 ***************************************/
  836. /***************************************************************/
  837. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  838. system("echo 111 > /sys/class/gpio/export");
  839. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  840. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  841. system("echo 114 > /sys/class/gpio/export");
  842. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  843. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  844. system("echo 113 > /sys/class/gpio/export");
  845. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  846. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  847. system("echo 110 > /sys/class/gpio/export");
  848. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  849. system("echo 0 > /sys/class/gpio/gpio110/value");
  850. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/ // no use
  851. system("echo 115 > /sys/class/gpio/export");
  852. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  853. system("echo 0 > /sys/class/gpio/gpio115/value");
  854. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  855. system("echo 112 > /sys/class/gpio/export");
  856. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  857. system("echo 1 > /sys/class/gpio/gpio112/value");
  858. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/ // no use
  859. system("echo 116 > /sys/class/gpio/export");
  860. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  861. system("echo 0 > /sys/class/gpio/gpio116/value");
  862. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  863. system("echo 103 > /sys/class/gpio/export");
  864. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  865. system("echo 0 > /sys/class/gpio/gpio103/value");
  866. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  867. system("echo 104 > /sys/class/gpio/export");
  868. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  869. system("echo 0 > /sys/class/gpio/gpio104/value");
  870. LOG_INFO("[main]InitGPIO: Initial GPIO OK");
  871. }
  872. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  873. {
  874. int fd, wrd;
  875. unsigned char *buf;
  876. unsigned int ChkSum, ChkSumOrg;
  877. if((buf = malloc(MtdBlockSize)) == NULL)
  878. {
  879. LOG_ERROR("malloc buffer NG, rebooting..\n");
  880. if(ShmStatusCodeData!=NULL)
  881. {
  882. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  883. }
  884. sleep(5);
  885. system("reboot -f");
  886. sleep(5);
  887. system("reboot -f");
  888. }
  889. memset(buf, 0, MtdBlockSize);
  890. //================================================
  891. // Load configuration from mtdblock10
  892. //================================================
  893. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  894. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  895. if(fd < 0)
  896. {
  897. free(buf);
  898. LOG_ERROR("open mtdblock10 NG, rebooting..\n");
  899. if(ShmStatusCodeData != NULL)
  900. {
  901. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  902. }
  903. sleep(5);
  904. system("reboot -f");
  905. sleep(5);
  906. system("reboot -f");
  907. }
  908. wrd = read(fd, buf, MtdBlockSize);
  909. close(fd);
  910. if(wrd < MtdBlockSize)
  911. {
  912. free(buf);
  913. LOG_ERROR("read SysConfigData data NG, rebooting..\n");
  914. if(ShmStatusCodeData != NULL)
  915. {
  916. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  917. }
  918. sleep(5);
  919. system("reboot -f");
  920. sleep(5);
  921. system("reboot -f");
  922. }
  923. ChkSum = 0;
  924. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  925. {
  926. ChkSum += buf[wrd];
  927. }
  928. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  929. memcpy(&ptr->ModelName, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), ARRAY_SIZE(ptr->ModelName));
  930. memcpy(&ptr->SerialNumber, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), ARRAY_SIZE(ptr->SerialNumber));
  931. //================================================
  932. // Load configuration from mtdblock11
  933. //================================================
  934. if(ChkSum != ChkSumOrg)
  935. {
  936. LOG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  937. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  938. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  939. if(fd < 0)
  940. {
  941. free(buf);
  942. LOG_ERROR("open mtdblock11 (backup) NG, rebooting..\n");
  943. if(ShmStatusCodeData != NULL)
  944. {
  945. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  946. }
  947. sleep(5);
  948. system("reboot -f");
  949. sleep(5);
  950. system("reboot -f");
  951. }
  952. memset(buf, 0, MtdBlockSize);
  953. wrd = read(fd, buf, MtdBlockSize);
  954. close(fd);
  955. if(wrd < MtdBlockSize)
  956. {
  957. free(buf);
  958. LOG_ERROR("read backup SysConfigData data NG, rebooting..\n");
  959. if(ShmStatusCodeData != NULL)
  960. {
  961. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  962. }
  963. sleep(5);
  964. system("reboot -f");
  965. sleep(5);
  966. system("reboot -f");
  967. }
  968. ChkSum = 0;
  969. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  970. {
  971. ChkSum += buf[wrd];
  972. }
  973. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  974. //================================================
  975. // Load configuration from mtdblock12 (Factory default)
  976. //================================================
  977. if(ChkSum != ChkSumOrg)
  978. {
  979. LOG_ERROR("backup SysConfigData checksum NG, read Factory default\n");
  980. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  981. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  982. if(fd < 0)
  983. {
  984. free(buf);
  985. LOG_ERROR("open mtdblock12 (Factory default) NG, rebooting..\n");
  986. if(ShmStatusCodeData != NULL)
  987. {
  988. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  989. }
  990. sleep(5);
  991. system("reboot -f");
  992. sleep(5);
  993. system("reboot -f");
  994. }
  995. memset(buf, 0, MtdBlockSize);
  996. wrd = read(fd, buf, MtdBlockSize);
  997. close(fd);
  998. if(wrd < MtdBlockSize)
  999. {
  1000. free(buf);
  1001. LOG_ERROR("read factory default SysConfigData data NG, rebooting..\n");
  1002. if(ShmStatusCodeData != NULL)
  1003. {
  1004. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1005. }
  1006. sleep(5);
  1007. system("reboot -f");
  1008. sleep(5);
  1009. system("reboot -f");
  1010. }
  1011. ChkSum = 0;
  1012. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  1013. {
  1014. ChkSum += buf[wrd];
  1015. }
  1016. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  1017. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1018. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1019. if(ChkSum != ChkSumOrg)
  1020. {
  1021. LOG_ERROR("factory default SysConfigData checksum NG, restore factory default\n");
  1022. free(buf);
  1023. system("cd /root;./FactoryConfig -m");
  1024. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1025. system("sync");
  1026. sleep(5);
  1027. system("reboot -f");
  1028. sleep(5);
  1029. system("reboot -f");
  1030. return FAIL;
  1031. }
  1032. }
  1033. }
  1034. //load OK
  1035. memcpy((struct SysConfigData *)ptr, buf, sizeof(struct SysConfigData));
  1036. free(buf);
  1037. system("rm -f /mnt/EvseConfig.bin");
  1038. // SysConfig in flash is empty (0xffffffff)
  1039. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1040. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1041. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1042. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1043. {
  1044. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1045. {
  1046. LOG_INFO("Model name over length.");
  1047. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1048. }
  1049. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1050. {
  1051. LOG_INFO("Model serial number over length.");
  1052. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1053. }
  1054. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1055. {
  1056. LOG_INFO("System id over length.");
  1057. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1058. }
  1059. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1060. {
  1061. LOG_INFO("Ethernet dhcp config is null.");
  1062. }
  1063. system("cd /root;./Module_FactoryConfig -m");
  1064. sleep(3);
  1065. system("/usr/bin/run_evse_restart.sh");
  1066. }
  1067. LOG_INFO("Load SysConfigData OK");
  1068. return PASS;
  1069. }
  1070. int isEthernetCablePlugIn(int ethernet)
  1071. {
  1072. int fd, PlugInStatus = 0;
  1073. char ch[2];
  1074. char buffer[64];
  1075. snprintf(buffer, sizeof(buffer), "/sys/class/net/eth%d/carrier", ethernet);
  1076. fd = open(buffer, O_RDONLY);
  1077. if(fd < 0)
  1078. {
  1079. return -1;
  1080. }
  1081. if(read(fd, ch, 1) < 0)
  1082. {
  1083. return -1;
  1084. }
  1085. PlugInStatus = atoi(ch);
  1086. close(fd);
  1087. return PlugInStatus;
  1088. }
  1089. int isRouteFail()
  1090. {
  1091. int result = YES;
  1092. FILE *fp;
  1093. char buf[512];
  1094. fp = popen("route -n", "r");
  1095. if(fp != NULL)
  1096. {
  1097. while(fgets(buf, sizeof(buf), fp) != NULL)
  1098. {
  1099. if(strstr(buf, "eth0") != NULL)
  1100. result = NO;
  1101. }
  1102. }
  1103. pclose(fp);
  1104. return result;
  1105. }
  1106. int isReachableInternet()
  1107. {
  1108. int result = FAIL;
  1109. FILE *fp;
  1110. char cmd[256];
  1111. char buf[512];
  1112. char tmp[512];
  1113. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1114. // {
  1115. // result = PASS;
  1116. // }
  1117. // else
  1118. {
  1119. strcpy(cmd, "ifconfig eth0");
  1120. fp = popen(cmd, "r");
  1121. if (fp != NULL)
  1122. {
  1123. while(fgets(buf, sizeof(buf), fp) != NULL)
  1124. {
  1125. if (strstr(buf, "inet addr:") > 0)
  1126. {
  1127. sscanf(buf, "%*s%s", tmp);
  1128. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
  1129. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  1130. {
  1131. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  1132. }
  1133. }
  1134. }
  1135. }
  1136. pclose(fp);
  1137. memset(buf, 0x00, sizeof(buf));
  1138. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1139. {
  1140. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1141. fp = popen(cmd, "r");
  1142. if(fp != NULL)
  1143. {
  1144. while(fgets(buf, sizeof(buf), fp) != NULL)
  1145. {
  1146. if(strstr(buf, "transmitted") > 0)
  1147. {
  1148. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1149. if(strstr(buf,"100%") != NULL)
  1150. {
  1151. }
  1152. else
  1153. {
  1154. result = PASS;
  1155. }
  1156. //DEBUG_INFO("%s",buf);
  1157. //DEBUG_INFO("%s\n",tmp);
  1158. }
  1159. }
  1160. }
  1161. pclose(fp);
  1162. }
  1163. }
  1164. return result;
  1165. }
  1166. int isUap0up(void)
  1167. {
  1168. int result = FAIL;
  1169. FILE *fp;
  1170. char cmd[256];
  1171. char buf[512];
  1172. strcpy(cmd, "ifconfig uap0");;
  1173. fp = popen(cmd, "r");
  1174. if(fp != NULL)
  1175. {
  1176. while(fgets(buf, sizeof(buf), fp) != NULL)
  1177. {
  1178. if(strstr(buf, "uap0") > 0)
  1179. {
  1180. result = PASS;
  1181. }
  1182. }
  1183. }
  1184. pclose(fp);
  1185. return result;
  1186. }
  1187. void InitialApModeDhcpServerConfig(void)
  1188. {
  1189. system("echo 'start 192.168.10.100' > /etc/udhcpd.conf");
  1190. system("echo 'end 192.168.10.150' >> /etc/udhcpd.conf");
  1191. system("echo 'interface uap0' >> /etc/udhcpd.conf");
  1192. system("echo 'lease_file /var/log/udhcpd.leases'>> /etc/udhcpd.conf");
  1193. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd.conf");
  1194. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd.conf");
  1195. system("echo 'opt router 192.168.10.10' >> /etc/udhcpd.conf");
  1196. system("echo 'option domain local' >> /etc/udhcpd.conf");
  1197. system("echo 'option lease 86400' >> /etc/udhcpd.conf");
  1198. usleep(1000);
  1199. }
  1200. void InitialBridgeModeDhcpServerConfig(void)
  1201. {
  1202. system("echo 'start 192.168.10.100' > /etc/udhcpd.conf");
  1203. system("echo 'end 192.168.10.150' >> /etc/udhcpd.conf");
  1204. system("echo 'interface br0' >> /etc/udhcpd.conf");
  1205. system("echo 'lease_file /var/log/udhcpd.leases'>> /etc/udhcpd.conf");
  1206. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd.conf");
  1207. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd.conf");
  1208. system("echo 'opt router 192.168.10.10' >> /etc/udhcpd.conf");
  1209. system("echo 'option domain local' >> /etc/udhcpd.conf");
  1210. system("echo 'option lease 86400' >> /etc/udhcpd.conf");
  1211. usleep(1000);
  1212. }
  1213. bool Is_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1214. {
  1215. return (ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue & misc_cmd) > 0 ? true : false;
  1216. }
  1217. void Set_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1218. {
  1219. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue |= misc_cmd;
  1220. }
  1221. void Clean_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1222. {
  1223. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue &= ~misc_cmd;
  1224. }
  1225. bool Is_Connector_MiscCommand(int connector, int misc_cmd)
  1226. {
  1227. return (ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue & misc_cmd) > 0 ? true : false;
  1228. }
  1229. void Set_Connector_MiscCommand(int connector, int misc_cmd)
  1230. {
  1231. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue |= misc_cmd;
  1232. }
  1233. void Clean_Connector_MiscCommand(int connector, int misc_cmd)
  1234. {
  1235. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue &= ~misc_cmd;
  1236. }
  1237. void AnnounceEthernetStatus(void)
  1238. {
  1239. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1240. {
  1241. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1242. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1243. {
  1244. Set_Dispenser_MiscCommand(index, MISC_DISP_ETHERNET_STATUS);
  1245. }
  1246. }
  1247. }
  1248. void AnnounceWiFiStatus(void)
  1249. {
  1250. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1251. {
  1252. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1253. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1254. {
  1255. Set_Dispenser_MiscCommand(index, MISC_DISP_WIFI_STATUS);
  1256. }
  1257. }
  1258. }
  1259. void AnnounceTelcomModemStatus(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_TELCOM_MODEM_STATUS);
  1267. }
  1268. }
  1269. }
  1270. void AnnounceBackendStatus(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_BACKEND_STATUS);
  1278. }
  1279. }
  1280. }
  1281. void AnnounceBillingStatus(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_BILLING_STATUS);
  1289. }
  1290. }
  1291. }
  1292. void AnnounceStopChargingButtonStatus(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_STOP_BUTTON_STATUS);
  1300. }
  1301. }
  1302. }
  1303. void AnnounceAuthModeConfig(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_AUTH_MODE_CONFIG);
  1311. }
  1312. }
  1313. }
  1314. void AnnounceEVCCIDConfig(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_EVCCID_CONFIG);
  1322. }
  1323. }
  1324. }
  1325. void AnnounceLEDIntensity(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_LED_INTENSITY);
  1333. }
  1334. }
  1335. }
  1336. void AnnounceCurrency(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_CURRENCY);
  1344. }
  1345. }
  1346. }
  1347. void AnnounceRfidEndian(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_RFID_CARD_ENDIAN);
  1355. }
  1356. }
  1357. }
  1358. void AnnounceRfidReader(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_RFID_ENABLE);
  1366. }
  1367. }
  1368. }
  1369. void AnnounceRemoteStartNoID(void)
  1370. {
  1371. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  1372. {
  1373. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1374. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1375. {
  1376. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  1377. {
  1378. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  1379. if(chargingInfo[gun]->SystemStatus == S_IDLE)
  1380. {
  1381. ShmChargerInfo->ConnectorPageReq[gun].bits.RemoteStartNoID = true;
  1382. break;
  1383. }
  1384. }
  1385. }
  1386. }
  1387. }
  1388. void AnnounceLineStatusCode(int gun, int status)
  1389. {
  1390. int dispenser = 0;
  1391. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].ParentDispensetIndex;
  1392. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_None &&
  1393. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  1394. {
  1395. ShmChargerInfo->LineApi.StatusCode[gun] = status;
  1396. Set_Connector_MiscCommand(gun, MISC_CONN_LINE_STATUS);
  1397. }
  1398. }
  1399. void AnnounceStationInfo(void)
  1400. {
  1401. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1402. {
  1403. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1404. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1405. {
  1406. Set_Dispenser_MiscCommand(index, MISC_DISP_STATION_INFO);
  1407. }
  1408. }
  1409. }
  1410. void AnnounceOccupancyPrice(void)
  1411. {
  1412. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1413. {
  1414. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1415. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1416. {
  1417. Set_Dispenser_MiscCommand(index, MISC_DISP_PARKING_PRICE);
  1418. }
  1419. }
  1420. }
  1421. void UpdateConfigAndIcon(void)
  1422. {
  1423. unsigned char status = _Connnection_Disable;
  1424. // update ethernet connection status
  1425. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1426. if(ShmChargerInfo->CabinetMiscValue.EthernetStatus != status)
  1427. {
  1428. ShmChargerInfo->CabinetMiscValue.EthernetStatus = status;
  1429. AnnounceEthernetStatus();
  1430. }
  1431. // update wifi connection status
  1432. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1433. {
  1434. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1435. }
  1436. else
  1437. {
  1438. status = _Connnection_Disable;
  1439. }
  1440. if(ShmChargerInfo->CabinetMiscValue.WiFiStatus != status)
  1441. {
  1442. ShmChargerInfo->CabinetMiscValue.WiFiStatus = status;
  1443. AnnounceWiFiStatus();
  1444. }
  1445. // update 4g connection status
  1446. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1447. {
  1448. status = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1449. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? _Connnection_Connected : _Connnection_Disconnected;
  1450. }
  1451. else
  1452. {
  1453. status = _Connnection_Disable;
  1454. }
  1455. if(ShmChargerInfo->CabinetMiscValue.TelcomModemStatus != status)
  1456. {
  1457. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = status;
  1458. AnnounceTelcomModemStatus();
  1459. }
  1460. // update backend connection status
  1461. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1462. if(ShmChargerInfo->CabinetMiscValue.BackendStatus != status)
  1463. {
  1464. ShmChargerInfo->CabinetMiscValue.BackendStatus = status;
  1465. AnnounceBackendStatus();
  1466. }
  1467. // update billing status
  1468. status = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1469. if(ShmChargerInfo->CabinetMiscValue.BillingStatus != status)
  1470. {
  1471. ShmChargerInfo->CabinetMiscValue.BillingStatus = status;
  1472. AnnounceBillingStatus();
  1473. }
  1474. // update stop charging button
  1475. status = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1476. if(ShmChargerInfo->CabinetMiscValue.StopChargingButton != status)
  1477. {
  1478. ShmChargerInfo->CabinetMiscValue.StopChargingButton = status;
  1479. AnnounceStopChargingButtonStatus();
  1480. }
  1481. // update AuthMode config
  1482. status = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  1483. if(ShmChargerInfo->CabinetMiscValue.AuthModeConfig != status)
  1484. {
  1485. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = status;
  1486. AnnounceAuthModeConfig();
  1487. AnnounceEVCCIDConfig();
  1488. }
  1489. // update EVCCID config
  1490. status = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  1491. if(ShmChargerInfo->CabinetMiscValue.EVCCIDConfig != status)
  1492. {
  1493. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = status;
  1494. AnnounceEVCCIDConfig();
  1495. }
  1496. // update LED Intensity
  1497. status = ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity;
  1498. if(ShmChargerInfo->CabinetMiscValue.LEDIntensity != status)
  1499. {
  1500. ShmChargerInfo->CabinetMiscValue.LEDIntensity = status;
  1501. AnnounceLEDIntensity();
  1502. }
  1503. // update currency
  1504. status = ShmSysConfigAndInfo->SysConfig.BillingData.Currency;
  1505. if(ShmChargerInfo->CabinetMiscValue.Currency != status)
  1506. {
  1507. ShmChargerInfo->CabinetMiscValue.Currency = status;
  1508. AnnounceCurrency();
  1509. }
  1510. // update RfidCardNumEndian
  1511. status = ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian;
  1512. if(ShmChargerInfo->CabinetMiscValue.RfidEndian != status)
  1513. {
  1514. ShmChargerInfo->CabinetMiscValue.RfidEndian = status;
  1515. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Phihong ||
  1516. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  1517. {
  1518. AnnounceRfidEndian();
  1519. }
  1520. }
  1521. // update AuthMode config
  1522. status = ShmSysConfigAndInfo->SysConfig.isRFID;
  1523. if(ShmChargerInfo->AuthInfo.AuthMode.bits.RFIDEnable != status)
  1524. {
  1525. ShmChargerInfo->AuthInfo.AuthMode.bits.RFIDEnable = status;
  1526. ShmChargerInfo->CabinetMiscValue.RfidReaderEnable = status;
  1527. AnnounceRfidReader();
  1528. }
  1529. }
  1530. void Check4gWifiReset(void)
  1531. {
  1532. int timeDelay = 0;
  1533. if(ShmChargerInfo->Control.SysCtrl.bits.BackendEnable)
  1534. {
  1535. if((!ShmSysConfigAndInfo->SysInfo.InternetConn && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  1536. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE ||
  1537. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)) ||
  1538. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES)
  1539. {
  1540. if(!_NeedReset4gWifi)
  1541. {
  1542. GetClockTime(&_4gWifiReset_time);
  1543. LOG_INFO("Backend Enable But Internet Not Available!");
  1544. }
  1545. _NeedReset4gWifi = true;
  1546. }
  1547. else
  1548. {
  1549. _NeedReset4gWifi = false;
  1550. }
  1551. if(_NeedReset4gWifi)
  1552. {
  1553. timeDelay = ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES ?
  1554. FAST_RESET_4G_WIFI_TIME : TRY_RESET_4G_WIFI_TIME;
  1555. if(GetTimeoutValue(_4gWifiReset_time) / uSEC_VAL >= timeDelay)
  1556. {
  1557. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = true;
  1558. usleep(100000);
  1559. // before reset 4g io, kill Module_4g & Module_Wifi task
  1560. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1561. {
  1562. system("killall Module_4g &");
  1563. }
  1564. usleep(100000);
  1565. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1566. {
  1567. system("killall Module_Wifi &");
  1568. }
  1569. LOG_INFO("Reset 4g_Wifi Module");
  1570. system("echo 1 > /sys/class/gpio/gpio104/value");
  1571. sleep(1);
  1572. system("echo 0 > /sys/class/gpio/gpio104/value");
  1573. _NeedReset4gWifi = false;
  1574. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = false;
  1575. }
  1576. }
  1577. }
  1578. else
  1579. {
  1580. _NeedReset4gWifi = false;
  1581. }
  1582. }
  1583. void MasterCabinetDhcpClientConfig(void)
  1584. {
  1585. char tmpbuf[256];
  1586. //Run DHCP client if enabled
  1587. system("killall udhcpc");
  1588. system("rm -rf /etc/resolv.conf");
  1589. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1590. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1591. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1592. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1593. {
  1594. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1595. system(tmpbuf);
  1596. }
  1597. //Upgrade system id to /etc/hostname
  1598. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1599. system(tmpbuf);
  1600. }
  1601. void InitMasterCabinetEthernet(void)
  1602. {
  1603. char tmpbuf[256];
  1604. //Init Eth0 for internet
  1605. memset(tmpbuf,0,256);
  1606. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1607. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1608. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1609. system(tmpbuf);
  1610. memset(tmpbuf,0,256);
  1611. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1612. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1613. system(tmpbuf);
  1614. //system("ifconfig lo up");
  1615. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1616. //Init Eth1 for administrator tool
  1617. memset(tmpbuf,0,256);
  1618. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", "192.168.100.1", "255.255.255.0");
  1619. system(tmpbuf);
  1620. }
  1621. void InitSlaveCabinetEthernet(void)
  1622. {
  1623. char tmpbuf[256];
  1624. system("/sbin/ifconfig eth0 down");
  1625. system("/sbin/ifconfig eth1 down");
  1626. system("killall udhcpc");
  1627. system("rm -rf /etc/resolv.conf");
  1628. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  1629. //Init Eth0 for internet
  1630. memset(tmpbuf,0,256);
  1631. sprintf(tmpbuf,"/sbin/ifconfig eth0 192.168.100.3%d netmask %s up",
  1632. (ShmChargerInfo->Control.CabinetSwitch - 1),
  1633. "255.255.255.0");
  1634. system(tmpbuf);
  1635. system("/sbin/route add default gw 192.168.100.1 eth0");
  1636. //Init Eth1 for internet
  1637. memset(tmpbuf,0,256);
  1638. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", SLAVE_ETH1_IP, SLAVE_ETH1_SUBMASK);
  1639. system(tmpbuf);
  1640. }
  1641. void InitEthernet(void)
  1642. {
  1643. system("ifconfig eth0 down");
  1644. system("ifconfig eth1 down");
  1645. sleep(2);
  1646. char tmpbuf[256];
  1647. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1648. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1649. sleep(2);
  1650. InitMasterCabinetEthernet();
  1651. MasterCabinetDhcpClientConfig();
  1652. // DO360 DHCP Server
  1653. InitialDispenserDhcpServerConfig();
  1654. StartDispenserDhcpServer();
  1655. pid_t pid = fork();
  1656. uint8_t cnt_pingDNS_Fail;
  1657. if(pid == 0)
  1658. {
  1659. ShmChargerInfo->CabinetMiscValue.BackendStatus = _Connnection_Disconnected;
  1660. ShmChargerInfo->CabinetMiscValue.EthernetStatus = _Connnection_Disconnected;
  1661. ShmChargerInfo->CabinetMiscValue.WiFiStatus = _Connnection_Disable;
  1662. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = _Connnection_Disable;
  1663. LOG_INFO("Internet Fork PID = %d", getpid());
  1664. for(;;)
  1665. {
  1666. if(isEthernetCablePlugIn(0) == YES)
  1667. {
  1668. if(isRouteFail())
  1669. {
  1670. LOG_INFO("eth0 not in route, restart eht0.");
  1671. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1672. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1673. {
  1674. InitialDHCP();
  1675. }
  1676. else
  1677. {
  1678. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1679. memset(tmpbuf,0,256);
  1680. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1681. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1682. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1683. system(tmpbuf);
  1684. memset(tmpbuf,0,256);
  1685. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1686. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1687. system(tmpbuf);
  1688. }
  1689. }
  1690. }
  1691. if(isReachableInternet() == PASS)
  1692. {
  1693. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != NO)
  1694. {
  1695. LOG_INFO("Internet is Reachable");
  1696. }
  1697. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = NO;
  1698. ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1699. cnt_pingDNS_Fail = 0;
  1700. }
  1701. else
  1702. {
  1703. if(++cnt_pingDNS_Fail > 3)
  1704. {
  1705. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != YES)
  1706. {
  1707. LOG_INFO("Internet is Disconnect Via Ethernet");
  1708. }
  1709. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1710. ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1711. }
  1712. }
  1713. bool ethResult = ShmSysConfigAndInfo->SysInfo.ethInternetConn;
  1714. if(ethResult == YES)
  1715. {
  1716. system("/sbin/ifmetric eth0 0");
  1717. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1718. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1719. {
  1720. system("/sbin/ifmetric mlan0 1");
  1721. }
  1722. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1723. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1724. {
  1725. system("/sbin/ifmetric ppp0 2");
  1726. }
  1727. }
  1728. if (!ethResult &&
  1729. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1730. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1731. {
  1732. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1733. if (ethResult)
  1734. {
  1735. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1736. {
  1737. system("/sbin/ifmetric eth0 1");
  1738. system("/sbin/ifmetric mlan0 0");
  1739. }
  1740. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1741. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1742. {
  1743. system("/sbin/ifmetric ppp0 2");
  1744. }
  1745. }
  1746. }
  1747. if (!ethResult &&
  1748. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1749. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1750. {
  1751. ethResult = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1752. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? YES : NO;
  1753. if (ethResult)
  1754. {
  1755. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1756. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1757. {
  1758. system("/sbin/ifmetric mlan0 2");
  1759. }
  1760. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1761. {
  1762. system("/sbin/ifmetric eth0 1");
  1763. system("/sbin/ifmetric ppp0 0");
  1764. }
  1765. }
  1766. }
  1767. // Bridge ethernet to uap0 to get dynamic ip address
  1768. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE &&
  1769. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP))
  1770. {
  1771. if((isUap0up() == PASS) && access("/sys/class/net/br0/address", F_OK) == -1)
  1772. {
  1773. system("/usr/sbin/brctl addbr br0");
  1774. system("/usr/sbin/brctl addif br0 eth0");
  1775. system("/usr/sbin/brctl addif br0 uap0");
  1776. system("/sbin/ifconfig br0 192.168.10.200 up");
  1777. InitialBridgeModeDhcpServerConfig();
  1778. //system("/bin/sed -i '/interface/d' /etc/udhcpd.conf");
  1779. //system("/bin/echo 'interface br0' >> /etc/udhcpd.conf");
  1780. system("pgrep -f \"udhcpd /etc/udhcpd.conf\" | xargs kill");
  1781. system("/usr/sbin/udhcpd /etc/udhcpd.conf");
  1782. LOG_INFO("Bridge uap0 & eth0 for local power sharing by ethernet.");
  1783. _isBridgeModeConfig = true;
  1784. }
  1785. }
  1786. else
  1787. {
  1788. if(access("/sys/class/net/br0/address", F_OK) != -1)
  1789. {
  1790. system("/sbin/ifconfig br0 down");
  1791. system("/usr/sbin/brctl delbr br0");
  1792. LOG_INFO("Disable uap0 & eth0 Bridge.");
  1793. }
  1794. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP && _isBridgeModeConfig)
  1795. {
  1796. InitialApModeDhcpServerConfig();
  1797. system("pgrep -f \"udhcpd /etc/udhcpd.conf\" | xargs kill");
  1798. system("/usr/sbin/udhcpd /etc/udhcpd.conf");
  1799. LOG_INFO("Restart uap0 udhcpd.");
  1800. _isBridgeModeConfig = false;
  1801. }
  1802. }
  1803. // Change uap0 to low priority
  1804. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE &&
  1805. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP) &&
  1806. (isUap0up() == PASS))
  1807. {
  1808. system("/sbin/ifmetric uap0 1");
  1809. }
  1810. //==============================================
  1811. // Check Local Power Sharing Communication
  1812. //==============================================
  1813. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing == _SYS_POWER_SHARING_MODE_SLAVE)
  1814. {
  1815. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  1816. {
  1817. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == ON)
  1818. {
  1819. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
  1820. }
  1821. }
  1822. else
  1823. {
  1824. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == OFF)
  1825. {
  1826. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = ON;
  1827. }
  1828. }
  1829. }
  1830. else
  1831. {
  1832. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
  1833. }
  1834. if(!Is_Ocpp_Task_Alive())
  1835. {
  1836. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = false;
  1837. }
  1838. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1839. ShmStatusCodeData->InfoCode.InfoEvents.bits.WiFiDisable = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_DISABLE ? true : false;
  1840. ShmStatusCodeData->InfoCode.InfoEvents.bits.Telocom4GModuleDisable = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == false ? true : false;
  1841. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet = ShmSysConfigAndInfo->SysInfo.OcppConnStatus == false ? true : false;
  1842. UpdateConfigAndIcon();
  1843. Check4gWifiReset();
  1844. sleep(5);
  1845. }
  1846. }
  1847. #ifdef SystemLogMessage
  1848. LOG_INFO("[main]InitEthernet: Initial Ethernet OK.");
  1849. #endif
  1850. }
  1851. void GetMacAddress()
  1852. {
  1853. for (byte index = 0; index < 2; index++)
  1854. {
  1855. int fd;
  1856. struct ifreq ifr;
  1857. char tarEth[5];
  1858. char Mac[18];
  1859. sprintf(tarEth,"eth%d",index);
  1860. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1861. ifr.ifr_addr.sa_family = AF_INET;
  1862. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1863. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1864. close(fd);
  1865. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1866. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1867. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1868. if (index == 0)
  1869. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1870. else
  1871. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1872. }
  1873. }
  1874. void GetFirmwareVersion()
  1875. {
  1876. // Get CSU root file system version
  1877. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1878. sprintf(ShmChargerInfo->SysMisc.MainVersion, fwVersion);
  1879. sprintf(ShmChargerInfo->SysMisc.SubVersion, subVersion);
  1880. #if ENABLE_PCBA_TEST == 0
  1881. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1882. for(uint8_t idx=0;idx<3;idx++)
  1883. {
  1884. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J')
  1885. {
  1886. chademo++;
  1887. count++;
  1888. }
  1889. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1890. {
  1891. gb++;
  1892. count++;
  1893. }
  1894. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  1895. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E')
  1896. {
  1897. ccs++;
  1898. count++;
  1899. }
  1900. }
  1901. if (count == 1)
  1902. {
  1903. if (chademo > 0)
  1904. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1905. else if (ccs > 0)
  1906. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1907. else if (gb > 0)
  1908. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1909. }
  1910. else
  1911. {
  1912. if (chademo > 0 && ccs > 0)
  1913. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1914. else if (chademo > 0 && gb > 0)
  1915. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1916. else if (ccs > 0 && gb > 0)
  1917. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1918. }
  1919. // Get network option from model name
  1920. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1921. {
  1922. case 'B':
  1923. case 'U':
  1924. //Blue tooth
  1925. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1926. break;
  1927. case 'W':
  1928. // WIFI
  1929. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1930. break;
  1931. case 'T':
  1932. // 3G/4G
  1933. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1934. break;
  1935. case 'D':
  1936. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1937. break;
  1938. default:
  1939. // LAN
  1940. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1941. break;
  1942. }
  1943. // Get rating power from model name
  1944. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1945. // Get IEC or UL
  1946. char _buf[3] = {0};
  1947. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  1948. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL)
  1949. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1950. else if (strcmp(_buf, "WU") == EQUAL)
  1951. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1952. #endif
  1953. }
  1954. void InitialShareMemoryInfo()
  1955. {
  1956. FILE *fp;
  1957. char cmd[512];
  1958. char buf[512];
  1959. _UpgradeNeedReboot = FALSE;
  1960. _psuLostQuantity = 0;
  1961. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1962. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1963. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1964. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1965. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1966. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1967. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1968. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1969. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1970. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1971. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1972. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1973. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1974. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1975. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1976. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1977. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1978. sprintf(cmd, "/bin/uname -r");
  1979. fp = popen(cmd, "r");
  1980. if(fp == NULL)
  1981. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1982. else
  1983. {
  1984. while(fgets(buf, sizeof(buf), fp) != NULL)
  1985. {
  1986. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1987. }
  1988. }
  1989. // 雙槍 CCS + Chademo
  1990. GetFirmwareVersion();
  1991. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1992. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1993. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1994. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1995. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1996. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1997. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1998. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1999. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  2000. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  2001. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  2002. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  2003. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  2004. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleHwRev, " "); //DO360 RC2
  2005. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, " "); //DO360 RC2
  2006. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  2007. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, " ");
  2008. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector1FwRev, " ");
  2009. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector2FwRev, " ");
  2010. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  2011. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  2012. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  2013. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  2014. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  2015. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  2016. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2017. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  2018. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  2019. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  2020. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  2021. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  2022. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  2023. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  2024. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  2025. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  2026. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  2027. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  2028. memset(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev)); //DO360 RC2
  2029. ShmPrimaryMcuData->SelfTest_Comp = NO;
  2030. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  2031. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  2032. ShmFanModuleData->SelfTest_Comp = NO;
  2033. ShmLedModuleData->SelfTest_Comp = NO;
  2034. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2035. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  2036. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  2037. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  2038. ShmFanModuleData->TestFanSpeed = 0;
  2039. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  2040. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  2041. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  2042. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  2043. LOG_INFO("DispenserSequence = %d, MaxConnectorQuantity = %d",
  2044. ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence,
  2045. ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity);
  2046. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  2047. {
  2048. LOG_INFO("Dispenser[%d] <=> %d", i, ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[i]);
  2049. }
  2050. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2051. {
  2052. memset((char *)&_BackupStatusNotification[i], 0, sizeof(struct StructStatusNotification));
  2053. _useGunMeterValue[i] = false;
  2054. _isGetOccupancyFeeReq[i] = false;
  2055. }
  2056. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  2057. {
  2058. _canUpdateFinalCost[i] = false;
  2059. _isUndisposedFinalCost[i] = true;
  2060. }
  2061. }
  2062. int Initialization(void)
  2063. {
  2064. // int pinOut[2] = { 116, 115 };
  2065. for (byte count = 0; count < CONNECTOR_QUANTITY; count++)
  2066. {
  2067. chargingInfo[count]->RemoteStartFlag = NO;
  2068. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStartConfirm = false;
  2069. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStopConfirm = false;
  2070. SetOcppErrorCode(count, "NoError");
  2071. }
  2072. LOG_INFO("Initialization OK");
  2073. return PASS;
  2074. }
  2075. void InitialPsuGroupingAndLocation(void)
  2076. {
  2077. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  2078. {
  2079. memcpy(ShmPsuGrouping->Location, Group_to_Location_4_Gun, sizeof(ShmPsuGrouping->Location));
  2080. memcpy(ShmPsuGrouping->Layout, Location_to_Group_4_Gun, sizeof(ShmPsuGrouping->Layout));
  2081. }
  2082. else
  2083. {
  2084. memcpy(ShmPsuGrouping->Location, Group_to_Location_2_Gun, sizeof(ShmPsuGrouping->Location));
  2085. memcpy(ShmPsuGrouping->Layout, Location_to_Group_2_Gun, sizeof(ShmPsuGrouping->Layout));
  2086. }
  2087. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2088. {
  2089. ShmPsuGrouping->GroupCollection[i].Index = i;
  2090. ShmPsuGrouping->GroupCollection[i].Location = ShmPsuGrouping->Location[i];
  2091. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  2092. {
  2093. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2094. {
  2095. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun[i][0], GENERAL_GUN_QUANTITY);
  2096. }
  2097. else
  2098. {
  2099. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun_6Parallel[i][0], GENERAL_GUN_QUANTITY);
  2100. }
  2101. }
  2102. else
  2103. {
  2104. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_2_Gun[i][0], GENERAL_GUN_QUANTITY);
  2105. }
  2106. ShmChargerInfo->Control.GunAvailable[i] = NO;
  2107. }
  2108. }
  2109. void InitialCustomerCode(void)
  2110. {
  2111. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  2112. {
  2113. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Audi;
  2114. }
  2115. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'E' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == '4')
  2116. {
  2117. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_E4U;
  2118. }
  2119. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'U' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'U')
  2120. {
  2121. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_UCar;
  2122. }
  2123. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'T' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'C')
  2124. {
  2125. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_TCC;
  2126. }
  2127. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'O' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'L')
  2128. {
  2129. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Shell;
  2130. }
  2131. else
  2132. {
  2133. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Phihong;
  2134. }
  2135. }
  2136. void InitialMaxDispenserConnectorQuantity(void)
  2137. {
  2138. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable == false)
  2139. {
  2140. ShmChargerInfo->Control.MaxDispenser = 2;
  2141. ShmChargerInfo->Control.MaxConnector = 2;
  2142. }
  2143. else
  2144. {
  2145. ShmChargerInfo->Control.MaxDispenser = GENERAL_GUN_QUANTITY;
  2146. ShmChargerInfo->Control.MaxConnector = GENERAL_GUN_QUANTITY;
  2147. }
  2148. }
  2149. void InitialAuthMode(void)
  2150. {
  2151. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  2152. {
  2153. }
  2154. }
  2155. void InitialQRCodeMode(void)
  2156. {
  2157. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi ||
  2158. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  2159. {
  2160. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_ChargeBoxId;
  2161. }
  2162. else
  2163. {
  2164. // customized
  2165. if(ShmSysConfigAndInfo->SysConfig.QRCodeMadeMode)
  2166. {
  2167. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Customized;
  2168. }
  2169. else
  2170. {
  2171. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Default;
  2172. }
  2173. }
  2174. }
  2175. void InitialPowerCabinetSetting(void)
  2176. {
  2177. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  2178. {
  2179. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = true;
  2180. }
  2181. ShmChargerInfo->CabinetMiscValue.BillingStatus = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  2182. ShmChargerInfo->CabinetMiscValue.StopChargingButton = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  2183. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  2184. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  2185. }
  2186. void Initial6ParallelSetting(void)
  2187. {
  2188. if(ShmSysConfigAndInfo->SysConfig.ModelName[11] == '3')
  2189. {
  2190. ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay = 1;
  2191. }
  2192. }
  2193. void PowerModuleLibSelection(void)
  2194. {
  2195. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'L')
  2196. {
  2197. ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib = true;
  2198. }
  2199. else
  2200. {
  2201. ShmChargerInfo->Control.LibCtrl.bits.InfyPwrLib = true;
  2202. }
  2203. }
  2204. bool InitialChargerSetting(void)
  2205. {
  2206. bool result = true;
  2207. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' &&
  2208. (ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'O' || ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'L'))
  2209. {
  2210. ShmChargerInfo->Control.PrimaryCtrl.bits.AcContactorReverse = true;
  2211. ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse = true;
  2212. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2213. ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable = true;
  2214. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  2215. }
  2216. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  2217. {
  2218. if(!(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'P' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'K'))
  2219. {
  2220. ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  2221. }
  2222. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2223. ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  2224. }
  2225. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'B')
  2226. {
  2227. //ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  2228. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2229. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  2230. //ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  2231. ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable = true;
  2232. ShmChargerInfo->Control.RelayCtrl.bits.DcInputEnable = true;
  2233. }
  2234. else
  2235. {
  2236. result = false;
  2237. }
  2238. InitialCustomerCode();
  2239. InitialMaxDispenserConnectorQuantity();
  2240. InitialAuthMode();
  2241. InitialQRCodeMode();
  2242. Initial6ParallelSetting();
  2243. InitialPsuGroupingAndLocation();
  2244. InitialPowerCabinetSetting();
  2245. PowerModuleLibSelection();
  2246. if(!CheckConnectorTypeStatus())
  2247. {
  2248. result = false;
  2249. }
  2250. return result;
  2251. }
  2252. bool InitialSystemDefaultConfig()
  2253. {
  2254. bool result = true;
  2255. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2256. InitGPIO();
  2257. InitEthernet();
  2258. GetMacAddress();
  2259. return result;
  2260. }
  2261. void SelfTestRun(void)
  2262. {
  2263. LOG_INFO("Self test.");
  2264. system("/root/Module_ChargerSelfTest &");
  2265. }
  2266. void SpawnPowerModuleTask(void)
  2267. {
  2268. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  2269. {
  2270. system("/root/Module_PsuComm_PH &");
  2271. }
  2272. else
  2273. {
  2274. system("/root/Module_PsuComm &");
  2275. }
  2276. }
  2277. void KillPowerModuleTask(void)
  2278. {
  2279. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  2280. {
  2281. system("killall Module_PsuComm_PH");
  2282. }
  2283. else
  2284. {
  2285. system("killall Module_PsuComm");
  2286. }
  2287. }
  2288. void SpawnNormalService(void)
  2289. {
  2290. system("/root/Module_EventLogging &");
  2291. system("/root/Module_PrimaryComm &");
  2292. system("/root/Module_EvComm &");
  2293. //system("/root/Module_LcmControl &");
  2294. system("/root/Module_InternalComm &");
  2295. SpawnPowerModuleTask();
  2296. system("/root/Module_ProduceUtils &");
  2297. system("/root/Module_CabinetParallel &");
  2298. system("/root/Module_LedIndication &");
  2299. system("/root/Module_Authorize &");
  2300. }
  2301. void SpawnOcppService(void)
  2302. {
  2303. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  2304. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  2305. {
  2306. Run_Ocpp_Service();
  2307. }
  2308. }
  2309. void SpawnMaintainOcppService(void)
  2310. {
  2311. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  2312. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  2313. {
  2314. Run_MaintainOcpp_Service();
  2315. }
  2316. }
  2317. void SpawnInternetService(void)
  2318. {
  2319. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  2320. {
  2321. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2322. {
  2323. system("/root/Module_4g &");
  2324. }
  2325. }
  2326. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2327. {
  2328. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2329. {
  2330. system("/root/Module_Wifi &");
  2331. }
  2332. }
  2333. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  2334. {
  2335. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2336. {
  2337. system("/root/Module_4g &");
  2338. }
  2339. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2340. {
  2341. system("/root/Module_Wifi &");
  2342. }
  2343. }
  2344. }
  2345. int SpawnTask(void)
  2346. {
  2347. sleep(2);
  2348. SpawnNormalService();
  2349. SpawnInternetService();
  2350. return PASS;
  2351. }
  2352. int runFlashShellCmd(const char *cmd)
  2353. {
  2354. int result = FAIL;
  2355. char buf[256];
  2356. FILE *fp;
  2357. fp = popen(cmd, "r");
  2358. if(fp != NULL)
  2359. {
  2360. while(fgets(buf, sizeof(buf), fp) != NULL)
  2361. {
  2362. LOG_INFO("%s\n", buf);
  2363. }
  2364. result = PASS;
  2365. }
  2366. pclose(fp);
  2367. return result;
  2368. }
  2369. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2370. {
  2371. int result = PASS;
  2372. int fd, wrd;
  2373. unsigned int i, Chk;
  2374. unsigned char *ptr, *BufTmp;
  2375. Chk = 0;
  2376. ptr = (unsigned char *)UsrData;
  2377. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2378. {
  2379. memset(BufTmp, 0, MtdBlockSize);
  2380. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2381. for(i = ARRAY_SIZE(UsrData->CsuBootLoadFwRev); i < MtdBlockSize - 4; i++)
  2382. {
  2383. Chk += *(BufTmp+i);
  2384. }
  2385. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  2386. // Output configuration to file.
  2387. fd = open("/mnt/EvseTempConfig.bin", O_RDWR | O_CREAT | O_TRUNC);
  2388. if(fd < 0)
  2389. {
  2390. LOG_ERROR("open /mnt/EvseTempConfig.bin NG");
  2391. free(BufTmp);
  2392. return 0;
  2393. }
  2394. wrd = write(fd, BufTmp, MtdBlockSize);
  2395. close(fd);
  2396. if(wrd < MtdBlockSize)
  2397. {
  2398. LOG_ERROR("write /mnt/EvseTempConfig.bin NG");
  2399. free(BufTmp);
  2400. return 0;
  2401. }
  2402. LOG_INFO("EvseTempConfig write to file in /mnt OK.");
  2403. LOG_INFO("Erase /dev/mtd10.");
  2404. runFlashShellCmd("flash_erase /dev/mtd10 0 0");
  2405. LOG_INFO("Write /dev/mtd10.");
  2406. runFlashShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseTempConfig.bin");
  2407. LOG_INFO("Erase /dev/mtd11.");
  2408. runFlashShellCmd("flash_erase /dev/mtd11 0 0");
  2409. LOG_INFO("Write /dev/mtd11.");
  2410. runFlashShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseTempConfig.bin");
  2411. system("rm -f /mnt/EvseTempConfig.bin");
  2412. LOG_INFO("EvseTempConfig write to flash OK");
  2413. }
  2414. else
  2415. {
  2416. LOG_ERROR("alloc BlockSize NG");
  2417. result = FAIL;
  2418. }
  2419. if(BufTmp != NULL)
  2420. {
  2421. free(BufTmp);
  2422. }
  2423. return result;
  2424. }
  2425. //===============================================
  2426. // Common Detect Chk - Stop Charging ?
  2427. //===============================================
  2428. bool isEvBoardStopChargeFlag(byte gunIndex)
  2429. {
  2430. return chargingInfo[gunIndex]->ChargingStopFlag.bits.AlarmStop;
  2431. }
  2432. bool isNormalStopChargeFlag(byte gunIndex)
  2433. {
  2434. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.NormalStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.ManualStop);
  2435. }
  2436. bool isResetStopChargeFlag(byte gunIndex)
  2437. {
  2438. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.HardResetStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.SoftResetStop);
  2439. }
  2440. bool isInvalidIdStopFlag(byte gunIndex)
  2441. {
  2442. return chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop;
  2443. }
  2444. bool isSessionTargetStopFlag(byte gunIndex)
  2445. {
  2446. return chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop;
  2447. }
  2448. bool isBackendStopFlag(byte gunIndex)
  2449. {
  2450. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.BackendStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop);
  2451. }
  2452. void ReleaseAlarmCode(byte gunIndex)
  2453. {
  2454. if(!Is_Ocpp_StatusNotificationErrorCode(gunIndex, "NoError"))
  2455. {
  2456. SetOcppErrorCode(gunIndex, "NoError");
  2457. }
  2458. if(!Is_Ocpp_StatusNotificationVendorCode(gunIndex, ""))
  2459. {
  2460. SetOcppVendorErrorCode(gunIndex, "");
  2461. }
  2462. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  2463. {
  2464. strcpy((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "");
  2465. }
  2466. if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  2467. {
  2468. strcpy((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "");
  2469. }
  2470. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "") != EQUAL)
  2471. {
  2472. strcpy((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "");
  2473. }
  2474. if (strcmp((char *)ShmChargerInfo->GunError[gunIndex].GunAlarmCode, "") != EQUAL)
  2475. {
  2476. strcpy((char *)ShmChargerInfo->GunError[gunIndex].GunAlarmCode, "");
  2477. }
  2478. }
  2479. //===============================================
  2480. // EmergencyStop and Charging Stop
  2481. //===============================================
  2482. void ChargingTerminalProcess(byte gunIndex)
  2483. {
  2484. setChargerMode(gunIndex, MODE_TERMINATING);
  2485. }
  2486. void ChargingAlarmProcess(byte gunIndex)
  2487. {
  2488. UpdateErrorCodeToOcpp(gunIndex);
  2489. setChargerMode(gunIndex, MODE_ALARM);
  2490. }
  2491. void AcChargingTerminalProcess()
  2492. {
  2493. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2494. }
  2495. void StopChargingProcessByString(byte level)
  2496. {
  2497. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2498. {
  2499. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2500. }
  2501. }
  2502. void ReleaseChargingProcessByString(byte level)
  2503. {
  2504. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2505. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2506. }
  2507. // 一般錯誤停止充電處理函式
  2508. void BoardErrOccurByString(byte index, char *code)
  2509. {
  2510. byte level = 1;
  2511. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2512. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2513. {
  2514. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2515. {
  2516. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2517. }
  2518. ChargingTerminalProcess(index);
  2519. }
  2520. StopChargingProcessByString(level);
  2521. }
  2522. // 急停狀況的停止充電處理函式
  2523. void EmcOccureByString(char *code)
  2524. {
  2525. byte level = 2;
  2526. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  2527. {
  2528. return;
  2529. }
  2530. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2531. // 其錯誤等級為 2
  2532. if (strncmp(code, "042251", 6) == EQUAL || strncmp(code, "042252", 6) == EQUAL ||
  2533. strncmp(code, "042238", 6) == EQUAL || strncmp(code, "042267", 6) == EQUAL ||
  2534. strncmp(code, "042328", 6) == EQUAL)
  2535. {
  2536. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2537. {
  2538. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2539. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2540. {
  2541. if (strncmp((char *)chargingInfo[gun]->ConnectorAlarmCode, "", 6) == EQUAL)
  2542. {
  2543. memcpy(chargingInfo[gun]->ConnectorAlarmCode, code, 6);
  2544. }
  2545. InformOcppErrOccur(4);
  2546. ChargingAlarmProcess(gun);
  2547. }
  2548. }
  2549. StopChargingProcessByString(level);
  2550. }
  2551. }
  2552. void ReleaseEmsOccureByString(byte index, char *code)
  2553. {
  2554. bool isTrigger = false;
  2555. byte level = 2;
  2556. if (strncmp(code, "042251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2557. {
  2558. isTrigger = true;
  2559. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2560. }
  2561. else if (strncmp(code, "042252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2562. {
  2563. isTrigger = true;
  2564. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2565. }
  2566. else if (strncmp(code, "042237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2567. {
  2568. isTrigger = true;
  2569. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2570. }
  2571. else if (strncmp(code, "042238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2572. {
  2573. isTrigger = true;
  2574. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2575. }
  2576. else if (strncmp(code, "042267", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm == YES)
  2577. {
  2578. isTrigger = true;
  2579. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  2580. }
  2581. if (isTrigger)
  2582. {
  2583. ReleaseChargingProcessByString(level);
  2584. }
  2585. }
  2586. //===============================================
  2587. // 確認硬體 (按鈕) 狀態
  2588. //===============================================
  2589. void ChkPrimaryStatus()
  2590. {
  2591. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  2592. {
  2593. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2594. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2595. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2596. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2597. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2598. return;
  2599. }
  2600. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2601. {
  2602. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2603. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = YES;
  2604. EmcOccureByString("042251");
  2605. }
  2606. else
  2607. {
  2608. ReleaseEmsOccureByString(0, "042251");
  2609. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2610. }
  2611. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2612. {
  2613. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2614. //EmcOccureByString("042238");
  2615. }
  2616. else
  2617. {
  2618. //ReleaseEmsOccureByString(0, "042238");
  2619. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2620. }
  2621. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2622. {
  2623. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2624. }
  2625. else
  2626. {
  2627. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2628. }
  2629. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2630. {
  2631. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2632. EmcOccureByString("042252");
  2633. }
  2634. else
  2635. {
  2636. ReleaseEmsOccureByString(0, "042252");
  2637. }
  2638. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2639. {
  2640. EmcOccureByString("042328");
  2641. SetAcContactor(OFF);
  2642. }
  2643. }
  2644. struct timespec _AcContactReOpen_Time;
  2645. void ChkPsuStatus(void)
  2646. {
  2647. if(!ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2648. {
  2649. for(int psuGroup = 0; psuGroup < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; psuGroup++)
  2650. {
  2651. if(ShmPsuData->PsuGroup[psuGroup].GroupErrorFlag.PsuGroupErrorValue != 0)
  2652. {
  2653. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2654. }
  2655. }
  2656. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2657. {
  2658. EmcOccureByString("042267");
  2659. }
  2660. }
  2661. else
  2662. {
  2663. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor &&
  2664. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume == NO)
  2665. {
  2666. bool allFaultStatus = true;
  2667. for(int gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2668. {
  2669. if(chargingInfo[gunIndex]->SystemStatus != S_FAULT && chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  2670. {
  2671. allFaultStatus = false;
  2672. }
  2673. }
  2674. if(allFaultStatus)
  2675. {
  2676. LOG_INFO("PSU Failure, Need Resume");
  2677. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = YES;
  2678. }
  2679. }
  2680. }
  2681. }
  2682. int AcContactorReOpenCount = 0;
  2683. void PsuFailureResume(void)
  2684. {
  2685. if(ShmChargerInfo->Control.PsuCtrl.bits.FailureResume)
  2686. {
  2687. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO)
  2688. {
  2689. LOG_INFO("Ac Contactor Off By Psu Failure");
  2690. // update _AcContactReOpen_Time
  2691. GetClockTime(&_AcContactReOpen_Time);
  2692. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = YES;
  2693. AcContactorReOpenCount++;
  2694. }
  2695. else
  2696. {
  2697. if(AcContactorReOpenCount < MAX_AC_CONTACTOR_COUNT)
  2698. {
  2699. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= AC_CONTACTOR_REOPEN_TIME)
  2700. {
  2701. ReleaseEmsOccureByString(0, "042267");
  2702. // update _AcContactReOpen_Time
  2703. GetClockTime(&_AcContactReOpen_Time);
  2704. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = NO;
  2705. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = NO;
  2706. LOG_INFO("Psu Failure Resume");
  2707. }
  2708. }
  2709. }
  2710. }
  2711. else
  2712. {
  2713. if(AcContactorReOpenCount > 0)
  2714. {
  2715. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= PSU_FAILURE_RESUME_TIME)
  2716. {
  2717. AcContactorReOpenCount = 0;
  2718. }
  2719. }
  2720. }
  2721. }
  2722. void SetAcContactor(unsigned char OnOff)
  2723. {
  2724. OnOff = OnOff ? ON : OFF;
  2725. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor != OnOff)
  2726. {
  2727. if(OnOff)
  2728. {
  2729. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2730. }
  2731. LOG_INFO("Set Ac Contactor %s", OnOff == ON ? "On" : "Off");
  2732. }
  2733. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = OnOff == ON ? ON : OFF;
  2734. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER && ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  2735. {
  2736. SetParallelCabinetAcContactor(OnOff);
  2737. }
  2738. }
  2739. int GetStandbyTime(void)
  2740. {
  2741. int time = STANDBY_TIME;
  2742. if(ShmChargerInfo->Control.TestCtrl.bits.FastStandbyTime)
  2743. {
  2744. time = FAST_STANDBY_TIME;
  2745. }
  2746. return time;
  2747. }
  2748. void StandbyCheck(void)
  2749. {
  2750. bool enable = IsStandbyEnable();
  2751. if(IsConnectorWholeIdle() && IsParallelCabinetIdle())
  2752. {
  2753. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2754. {
  2755. int time = GetStandbyTime();
  2756. #if CONTACTOR_AUTO_ON_OFF == 1
  2757. time = CONTACTOR_AUTO_ON_TIME;
  2758. #endif
  2759. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor)
  2760. {
  2761. if(GetSecTimeoutValue(_Standby_time) >= time && enable)
  2762. {
  2763. SetAcContactor(OFF);
  2764. }
  2765. }
  2766. #if CONTACTOR_AUTO_ON_OFF == 1
  2767. time = CONTACTOR_AUTO_ON_TIME + CONTACTOR_AUTO_OFF_TIME;
  2768. if((GetTimeoutValue(_Standby_time) / uSEC_VAL) >= time)
  2769. {
  2770. SetAcContactor(ON);
  2771. }
  2772. #endif
  2773. }
  2774. else
  2775. {
  2776. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = YES;
  2777. GetClockTime(&_Standby_time);
  2778. }
  2779. }
  2780. else
  2781. {
  2782. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2783. {
  2784. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2785. }
  2786. }
  2787. }
  2788. void CheckDisconnectionStatusRecovery(void)
  2789. {
  2790. bool recovery = true;
  2791. // check all connector
  2792. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2793. {
  2794. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection)
  2795. {
  2796. recovery = false;
  2797. }
  2798. }
  2799. if(recovery)
  2800. {
  2801. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = false;
  2802. }
  2803. }
  2804. //===============================================
  2805. // 確認各小板偵測的錯誤狀況
  2806. //===============================================
  2807. void CheckErrorOccurStatus(byte index)
  2808. {
  2809. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.NormalStopRequest)
  2810. {
  2811. chargingInfo[index]->ChargingStopFlag.bits.NormalStop = true;
  2812. }
  2813. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AlarmStopRequest)
  2814. {
  2815. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2816. {
  2817. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemotenAlarmCode, 6);
  2818. }
  2819. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2820. {
  2821. LOG_INFO("Connector %d Error Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2822. }
  2823. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2824. }
  2825. if(ShmChargerInfo->GunError[index].ErrFlag.ErrorValue != 0)
  2826. {
  2827. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2828. {
  2829. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmChargerInfo->GunError[index].GunAlarmCode, 6);
  2830. }
  2831. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2832. {
  2833. LOG_INFO("Connector %d GunError Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2834. }
  2835. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2836. }
  2837. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest)
  2838. {
  2839. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest = false;
  2840. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.Disconnection = true;
  2841. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = true;
  2842. if(chargingInfo[index]->SystemStatus == S_AUTHORIZING ||
  2843. chargingInfo[index]->SystemStatus == S_PREPARNING ||
  2844. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EV ||
  2845. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EVSE ||
  2846. chargingInfo[index]->SystemStatus == S_CHARGING)
  2847. {
  2848. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2849. {
  2850. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042304", 6);
  2851. }
  2852. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2853. {
  2854. LOG_INFO("Connector %d Disconnect Alarm Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2855. }
  2856. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2857. }
  2858. else if(chargingInfo[index]->SystemStatus == S_REASSIGN_CHECK ||
  2859. chargingInfo[index]->SystemStatus == S_REASSIGN)
  2860. {
  2861. setChargerMode(index, MODE_IDLE);
  2862. }
  2863. }
  2864. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo)
  2865. {
  2866. CheckDisconnectionStatusRecovery();
  2867. }
  2868. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES ||
  2869. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2870. {
  2871. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2872. {
  2873. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_PRIMARY;
  2874. StopChargingProcessByString(2);
  2875. }
  2876. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2877. {
  2878. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2879. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042251", 6);
  2880. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2881. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042252", 6);
  2882. }
  2883. }
  2884. else
  2885. {
  2886. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_PRIMARY)
  2887. {
  2888. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2889. ReleaseChargingProcessByString(2);
  2890. }
  2891. }
  2892. // RB
  2893. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2894. {
  2895. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2896. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2897. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2898. {
  2899. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2900. {
  2901. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2902. StopChargingProcessByString(2);
  2903. }
  2904. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2905. {
  2906. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2907. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042203", 6);
  2908. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2909. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042204", 6);
  2910. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2911. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042205", 6);
  2912. }
  2913. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2914. {
  2915. LOG_INFO("Connector %d RC Board Input UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2916. }
  2917. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2918. }
  2919. else
  2920. {
  2921. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2922. {
  2923. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2924. ReleaseChargingProcessByString(2);
  2925. }
  2926. }
  2927. }
  2928. else
  2929. {
  2930. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2931. {
  2932. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2933. ReleaseChargingProcessByString(2);
  2934. }
  2935. }
  2936. if(ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable == NO)
  2937. {
  2938. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2939. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2940. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2941. {
  2942. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2943. {
  2944. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2945. StopChargingProcessByString(2);
  2946. }
  2947. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2948. {
  2949. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  2950. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042200", 6);
  2951. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  2952. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042201", 6);
  2953. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2954. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042202", 6);
  2955. }
  2956. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2957. {
  2958. LOG_INFO("Connector %d RC Board Input OVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2959. }
  2960. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2961. }
  2962. else
  2963. {
  2964. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2965. {
  2966. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2967. ReleaseChargingProcessByString(2);
  2968. }
  2969. }
  2970. }
  2971. else
  2972. {
  2973. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES ||
  2974. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2975. {
  2976. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2977. {
  2978. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2979. StopChargingProcessByString(2);
  2980. }
  2981. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2982. {
  2983. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES)
  2984. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042327", 6);
  2985. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2986. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042328", 6);
  2987. }
  2988. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2989. {
  2990. LOG_INFO("Gun %d DC Input OVP/UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2991. }
  2992. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2993. }
  2994. else
  2995. {
  2996. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2997. {
  2998. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2999. ReleaseChargingProcessByString(2);
  3000. }
  3001. }
  3002. }
  3003. }
  3004. int DiffTimebWithNowSec(struct timeb ST)
  3005. {
  3006. //return milli-second
  3007. struct timeb ET;
  3008. unsigned int StartTime,StopTime;
  3009. ftime(&ET);
  3010. StartTime=(unsigned int)ST.time;
  3011. StopTime=(unsigned int)ET.time;
  3012. return (StopTime-StartTime);
  3013. }
  3014. int CheckTimeOut(char *start)
  3015. {
  3016. int result = YES;
  3017. struct ParsingResult
  3018. {
  3019. int result;
  3020. int scanedElement;
  3021. int year;
  3022. int month;
  3023. int mday;
  3024. int hour;
  3025. int min;
  3026. int sec;
  3027. int tz_hour;
  3028. int tz_min;
  3029. float minSec;
  3030. }parsingResult;
  3031. struct tm tmStart;
  3032. struct timeb tbStart;
  3033. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  3034. if(strstr((char*)start, ".") != NULL)
  3035. {
  3036. // Original data with mini second
  3037. if(strstr((char*)start, "Z") != NULL)
  3038. {
  3039. // Original data with Z
  3040. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  3041. &parsingResult.year,
  3042. &parsingResult.month,
  3043. &parsingResult.mday,
  3044. &parsingResult.hour,
  3045. &parsingResult.min,
  3046. &parsingResult.sec,
  3047. &parsingResult.minSec);
  3048. }
  3049. else
  3050. {
  3051. // Original data without Z
  3052. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  3053. &parsingResult.year,
  3054. &parsingResult.month,
  3055. &parsingResult.mday,
  3056. &parsingResult.hour,
  3057. &parsingResult.min,
  3058. &parsingResult.sec,
  3059. &parsingResult.minSec,
  3060. &parsingResult.tz_hour,
  3061. &parsingResult.tz_min);
  3062. }
  3063. }
  3064. else
  3065. {
  3066. // Original data without mini second
  3067. if(strstr((char*)start, "Z") != NULL)
  3068. {
  3069. // Original data with Z
  3070. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  3071. &parsingResult.year,
  3072. &parsingResult.month,
  3073. &parsingResult.mday,
  3074. &parsingResult.hour,
  3075. &parsingResult.min,
  3076. &parsingResult.sec);
  3077. }
  3078. else
  3079. {
  3080. // Original data without Z
  3081. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  3082. &parsingResult.year,
  3083. &parsingResult.month,
  3084. &parsingResult.mday,
  3085. &parsingResult.hour,
  3086. &parsingResult.min,
  3087. &parsingResult.sec,
  3088. &parsingResult.tz_hour,
  3089. &parsingResult.tz_min);
  3090. }
  3091. }
  3092. if(parsingResult.scanedElement >= 6)
  3093. {
  3094. tmStart.tm_year = parsingResult.year - 1900;
  3095. tmStart.tm_mon = parsingResult.month - 1;
  3096. tmStart.tm_mday = parsingResult.mday;
  3097. tmStart.tm_hour = parsingResult.hour;
  3098. tmStart.tm_min = parsingResult.min;
  3099. tmStart.tm_sec = parsingResult.sec;
  3100. tmStart.tm_gmtoff = 0;
  3101. tbStart.time = mktime(&tmStart);
  3102. tbStart.timezone = 0;
  3103. tbStart.millitm = 0;
  3104. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  3105. if(DiffTimebWithNowSec(tbStart) <= 0)
  3106. result = NO;
  3107. else
  3108. result = YES;
  3109. }
  3110. else
  3111. {
  3112. LOG_INFO("Start date parsing error");
  3113. }
  3114. return result;
  3115. }
  3116. bool Is_WebServer_Alive(void)
  3117. {
  3118. if(system("pidof -s lighttpd > /dev/null") != 0)
  3119. {
  3120. return false;
  3121. }
  3122. return true;
  3123. }
  3124. void Run_WebServer_Service(void)
  3125. {
  3126. system("/root/web.sh");
  3127. }
  3128. bool Is_LocalPowerSharing_Alive(void)
  3129. {
  3130. if(system("pidof -s Module_PowerSharing > /dev/null") != 0)
  3131. {
  3132. return false;
  3133. }
  3134. return true;
  3135. }
  3136. void Run_LocalPowerSharing_Service(void)
  3137. {
  3138. system ("/root/Module_PowerSharing &");
  3139. }
  3140. //===============================================
  3141. // Ocpp_Service
  3142. //===============================================
  3143. void Run_Ocpp_Service(void)
  3144. {
  3145. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3146. {
  3147. system("/root/OcppBackend &");
  3148. return;
  3149. }
  3150. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3151. {
  3152. system("/root/OcppBackend20 &");
  3153. return;
  3154. }
  3155. }
  3156. bool Is_Ocpp_Task_Alive(void)
  3157. {
  3158. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3159. {
  3160. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3161. {
  3162. return false;
  3163. }
  3164. return true;
  3165. }
  3166. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3167. {
  3168. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  3169. {
  3170. return false;
  3171. }
  3172. return true;
  3173. }
  3174. return false;
  3175. }
  3176. void Kill_Ocpp_Service(void)
  3177. {
  3178. system("killall OcppBackend");
  3179. system("killall OcppBackend20");
  3180. }
  3181. //===============================================
  3182. // Maintain Ocpp_Service
  3183. //===============================================
  3184. void Run_MaintainOcpp_Service(void)
  3185. {
  3186. system("/root/OcppBackendPH &");
  3187. }
  3188. bool Is_MaintainOcpp_Task_Alive(void)
  3189. {
  3190. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  3191. {
  3192. return false;
  3193. }
  3194. return true;
  3195. }
  3196. void Kill_MaintainOcpp_Service(void)
  3197. {
  3198. system("killall OcppBackendPH");
  3199. }
  3200. //===============================================
  3201. // Ocpp OcppConnStatus
  3202. //===============================================
  3203. bool Get_Ocpp_ConnectionStatus(void)
  3204. {
  3205. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3206. {
  3207. return ShmOCPP16Data->OcppConnStatus > 0 ? true : false;
  3208. }
  3209. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3210. {
  3211. return ShmOCPP20Data->OcppConnStatus > 0 ? true : false;
  3212. }
  3213. return false;
  3214. }
  3215. //===============================================
  3216. // Ocpp procDogTime
  3217. //===============================================
  3218. long int Get_Ocpp_procDogTime(void)
  3219. {
  3220. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3221. {
  3222. return ShmOCPP16Data->procDogTime;
  3223. }
  3224. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3225. {
  3226. return ShmOCPP20Data->procDogTime;
  3227. }
  3228. return 0;
  3229. }
  3230. void Clean_Ocpp_procDogTime(void)
  3231. {
  3232. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3233. {
  3234. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  3235. return;
  3236. }
  3237. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3238. {
  3239. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  3240. return;
  3241. }
  3242. }
  3243. //===============================================
  3244. // Ocpp Boot Notification
  3245. //===============================================
  3246. bool Is_Ocpp_BootNotification(void)
  3247. {
  3248. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3249. {
  3250. return ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  3251. }
  3252. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3253. {
  3254. return ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  3255. }
  3256. return false;
  3257. }
  3258. void Clean_Ocpp_BootNotification(void)
  3259. {
  3260. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3261. {
  3262. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = false;
  3263. return;
  3264. }
  3265. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3266. {
  3267. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = false;
  3268. return;
  3269. }
  3270. }
  3271. //===============================================
  3272. // Ocpp Rest
  3273. //===============================================
  3274. bool Is_Ocpp_ResetReq(void)
  3275. {
  3276. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3277. {
  3278. return ShmOCPP16Data->MsMsg.bits.ResetReq == YES ? true : false;
  3279. }
  3280. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3281. {
  3282. return ShmOCPP20Data->MsMsg.bits.ResetReq == YES ? true : false;
  3283. }
  3284. return false;
  3285. }
  3286. bool Chk_Ocpp_Reset_Type(char *type)
  3287. {
  3288. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3289. {
  3290. return strcmp((char *)ShmOCPP16Data->Reset.Type, type) == EQUAL ? true : false;
  3291. }
  3292. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3293. {
  3294. return strcmp((char *)ShmOCPP20Data->Reset.type, type) == EQUAL ? true : false;
  3295. }
  3296. return false;
  3297. }
  3298. void Clean_Ocpp_ResetReq(void)
  3299. {
  3300. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3301. {
  3302. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3303. ShmOCPP16Data->MsMsg.bits.ResetReq = false;
  3304. ShmOCPP16Data->MsMsg.bits.ResetConf = true;
  3305. return;
  3306. }
  3307. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3308. {
  3309. sprintf ( (char*) ShmOCPP20Data->Reset.Response_status, "Accepted" );
  3310. ShmOCPP20Data->MsMsg.bits.ResetReq = false;
  3311. ShmOCPP20Data->MsMsg.bits.ResetConf = true;
  3312. return;
  3313. }
  3314. }
  3315. //===============================================
  3316. // Ocpp AuthorizeReq
  3317. //===============================================
  3318. void Set_Ocpp_AuthorizeReq(void)
  3319. {
  3320. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3321. {
  3322. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  3323. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  3324. return;
  3325. }
  3326. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3327. {
  3328. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = false;
  3329. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = true;
  3330. return;
  3331. }
  3332. }
  3333. bool Is_Ocpp_AuthorizeConf(void)
  3334. {
  3335. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3336. {
  3337. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3338. }
  3339. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3340. {
  3341. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3342. }
  3343. return false;
  3344. }
  3345. bool Is_Ocpp_Authorize_Status(char *status)
  3346. {
  3347. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3348. {
  3349. return strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  3350. }
  3351. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3352. {
  3353. return strcmp((char *)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, status) == EQUAL ? true : false;
  3354. }
  3355. return false;
  3356. }
  3357. //===============================================
  3358. // Ocpp UpdateFirmwareReq
  3359. //===============================================
  3360. bool Is_Ocpp_UpdateFirmwareReq(void)
  3361. {
  3362. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3363. {
  3364. return ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3365. }
  3366. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3367. {
  3368. return ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3369. }
  3370. return false;
  3371. }
  3372. void Clean_Ocpp_UpdateFirmwareReq(void)
  3373. {
  3374. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3375. {
  3376. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = false;
  3377. return;
  3378. }
  3379. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3380. {
  3381. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = false;
  3382. return;
  3383. }
  3384. }
  3385. bool Is_Ocpp_UpdateFirmwareStatus(char *status)
  3386. {
  3387. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3388. {
  3389. return strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status) == EQUAL ? true : false;
  3390. }
  3391. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3392. {
  3393. return strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status) == EQUAL ? true : false;
  3394. }
  3395. return false;
  3396. }
  3397. void Set_Ocpp_UpdateFirmwareStatus(char *status)
  3398. {
  3399. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3400. {
  3401. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status);
  3402. return;
  3403. }
  3404. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3405. {
  3406. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status);
  3407. return;
  3408. }
  3409. }
  3410. void Clean_Ocpp_UpdateFirmwareStatus(char *status)
  3411. {
  3412. Set_Ocpp_UpdateFirmwareStatus("");
  3413. }
  3414. void Set_Ocpp_FirmwareStatusNotificationReq(void)
  3415. {
  3416. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3417. {
  3418. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3419. return;
  3420. }
  3421. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3422. {
  3423. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3424. return;
  3425. }
  3426. }
  3427. //===============================================
  3428. // Ocpp StopTransactionOnInvalidId
  3429. //===============================================
  3430. bool Is_Ocpp_StopOnInvalidId_Enable(void)
  3431. {
  3432. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3433. {
  3434. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE") > 0 ? true : false;
  3435. }
  3436. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3437. {
  3438. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3439. }
  3440. return false;
  3441. }
  3442. //===============================================
  3443. // Ocpp AuthorizeRemoteTxRequests
  3444. //===============================================
  3445. bool Is_Ocpp_AuthorizeRemoteTxRequests(void)
  3446. {
  3447. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3448. {
  3449. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE") > 0 ? true : false;
  3450. }
  3451. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3452. {
  3453. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3454. }
  3455. return false;
  3456. }
  3457. //===============================================
  3458. // Ocpp ConnectionTimeOut
  3459. //===============================================
  3460. int Get_Ocpp_ConnectionTimeOut(void)
  3461. {
  3462. int timeout = 0;
  3463. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3464. {
  3465. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData, "") != 0)
  3466. {
  3467. timeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3468. }
  3469. }
  3470. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3471. {
  3472. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value, "" ) != 0)
  3473. {
  3474. timeout = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  3475. }
  3476. }
  3477. return timeout > 0 ? timeout : 0;
  3478. }
  3479. //===============================================
  3480. // Ocpp DefaultPrice
  3481. //===============================================
  3482. void Get_Ocpp_DefaultPrice(char *defaultPrice)
  3483. {
  3484. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3485. {
  3486. strcpy(defaultPrice, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  3487. return;
  3488. }
  3489. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3490. {
  3491. strcpy(defaultPrice, "");
  3492. return;
  3493. }
  3494. strcpy(defaultPrice, "");
  3495. }
  3496. //===============================================
  3497. // Ocpp UserPrice
  3498. //===============================================
  3499. void Get_Ocpp_UserPrice(char *userId, char *userPrice)
  3500. {
  3501. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3502. {
  3503. strcpy(userId, (char *)ShmOCPP16Data->Cost.SetUserPrice.idToken);
  3504. strcpy(userPrice, (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  3505. return;
  3506. }
  3507. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3508. {
  3509. strcpy(userId, "");
  3510. strcpy(userPrice, "");
  3511. return;
  3512. }
  3513. strcpy(userId, "");
  3514. strcpy(userPrice, "");
  3515. }
  3516. void Clean_Ocpp_UserPrice(void)
  3517. {
  3518. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3519. {
  3520. memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  3521. return;
  3522. }
  3523. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3524. {
  3525. return;
  3526. }
  3527. }
  3528. //===============================================
  3529. // Ocpp RunningCost
  3530. //===============================================
  3531. void Get_Ocpp_RunningCost(int gun_index, char *cost)
  3532. {
  3533. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3534. {
  3535. strcpy(cost, (char *)ShmOCPP16Data->Cost.RunningCost[gun_index].description);
  3536. return;
  3537. }
  3538. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3539. {
  3540. strcpy(cost, (char *)"");
  3541. return;
  3542. }
  3543. }
  3544. void Clean_Ocpp_RunningCost(int gun_index)
  3545. {
  3546. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3547. {
  3548. memset(&ShmOCPP16Data->Cost.RunningCost[gun_index], 0x00, sizeof(struct StrcutRunningCost));
  3549. return;
  3550. }
  3551. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3552. {
  3553. return;
  3554. }
  3555. }
  3556. //===============================================
  3557. // Ocpp FinalCost
  3558. //===============================================
  3559. void Get_Ocpp_FinalCost(int gun_index, char *cost)
  3560. {
  3561. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3562. {
  3563. strcpy(cost, (char *)ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  3564. return;
  3565. }
  3566. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3567. {
  3568. strcpy(cost, (char *)"");
  3569. return;
  3570. }
  3571. }
  3572. void Clean_Ocpp_FinalCost(int gun_index)
  3573. {
  3574. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3575. {
  3576. memset(&ShmOCPP16Data->Cost.FinalCost[gun_index], 0x00, sizeof(struct StrcutFinalCost));
  3577. return;
  3578. }
  3579. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3580. {
  3581. return;
  3582. }
  3583. }
  3584. int Get_Ocpp_FinalCostTxId(int gun_index)
  3585. {
  3586. int txId = 0;
  3587. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3588. {
  3589. txId = ShmOCPP16Data->Cost.FinalCost[gun_index].txId;
  3590. }
  3591. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3592. {
  3593. }
  3594. return txId;
  3595. }
  3596. //===============================================
  3597. // Ocpp TimeOffset
  3598. //===============================================
  3599. void Get_Ocpp_TimeOffset(char *timeOffset)
  3600. {
  3601. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3602. {
  3603. strcpy(timeOffset, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[TimeOffset].ItemData);
  3604. return;
  3605. }
  3606. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3607. {
  3608. strcpy(timeOffset, "");
  3609. return;
  3610. }
  3611. strcpy(timeOffset, "");
  3612. }
  3613. //===============================================
  3614. // Ocpp PreAuthAmount
  3615. //===============================================
  3616. void Get_Ocpp_PreAuthAmount(char *amount)
  3617. {
  3618. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3619. {
  3620. strcpy(amount, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PreAuthAmount].ItemData);
  3621. return;
  3622. }
  3623. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3624. {
  3625. strcpy(amount, "");
  3626. return;
  3627. }
  3628. strcpy(amount, "");
  3629. }
  3630. //===============================================
  3631. // Ocpp PowerSharingServerIP
  3632. //===============================================
  3633. void Get_Ocpp_PowerSharingServerIP(char *ip)
  3634. {
  3635. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3636. {
  3637. strcpy(ip, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PowerSharingServerIP].ItemData);
  3638. return;
  3639. }
  3640. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3641. {
  3642. strcpy(ip, "");
  3643. return;
  3644. }
  3645. strcpy(ip, "");
  3646. }
  3647. //===============================================
  3648. // Ocpp Refunding
  3649. //===============================================
  3650. void Get_Ocpp_Refunding(char *Refunding)
  3651. {
  3652. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3653. {
  3654. return;
  3655. }
  3656. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3657. {
  3658. return;
  3659. }
  3660. }
  3661. void Clean_Ocpp_Refunding(void)
  3662. {
  3663. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3664. {
  3665. return;
  3666. }
  3667. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3668. {
  3669. return;
  3670. }
  3671. }
  3672. //===============================================
  3673. // Ocpp LcdOveride
  3674. //===============================================
  3675. bool Is_Ocpp_LcdOverideReq(int *gun, int *page)
  3676. {
  3677. *gun = ShmSysConfigAndInfo->SysInfo.LcdOveride.connectorId;
  3678. *page = ShmSysConfigAndInfo->SysInfo.LcdOveride.page_index;
  3679. return ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq ? true : false;
  3680. }
  3681. void Clean_Ocpp_LcdOverideReq(void)
  3682. {
  3683. ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq = false;
  3684. }
  3685. //===============================================
  3686. // Ocpp StationInfo
  3687. //===============================================
  3688. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name)
  3689. {
  3690. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3691. {
  3692. strcpy(name, (char *)ShmOCPP16Data->TcciCustomData.ChargerInfo.station_name);
  3693. }
  3694. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3695. {
  3696. }
  3697. }
  3698. bool Is_Ocpp_ChargerStationReq(void)
  3699. {
  3700. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3701. {
  3702. return ShmOCPP16Data->TcciCustomData.ChargerInfoReq ? true : false;
  3703. }
  3704. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3705. {
  3706. return false;
  3707. }
  3708. return false;
  3709. }
  3710. void Clean_Ocpp_ChargerStationReq(void)
  3711. {
  3712. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3713. {
  3714. ShmOCPP16Data->TcciCustomData.ChargerInfoReq = false;
  3715. }
  3716. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3717. {
  3718. }
  3719. }
  3720. //===============================================
  3721. // Ocpp WeatherInfo
  3722. //===============================================
  3723. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature)
  3724. {
  3725. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3726. {
  3727. *weatherID = ShmOCPP16Data->TcciCustomData.WeatherInfo.weatherId;
  3728. *temperature = ShmOCPP16Data->TcciCustomData.WeatherInfo.temperature;
  3729. }
  3730. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3731. {
  3732. }
  3733. }
  3734. bool Is_Ocpp_WeatherReq(void)
  3735. {
  3736. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3737. {
  3738. return ShmOCPP16Data->TcciCustomData.WeatherInfoReq ? true : false;
  3739. }
  3740. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3741. {
  3742. return false;
  3743. }
  3744. return false;
  3745. }
  3746. void Clean_Ocpp_WeatherReq(void)
  3747. {
  3748. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3749. {
  3750. ShmOCPP16Data->TcciCustomData.WeatherInfoReq = false;
  3751. }
  3752. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3753. {
  3754. }
  3755. }
  3756. //===============================================
  3757. // Ocpp DeductInfo
  3758. //===============================================
  3759. void Set_Ocpp_DeductTransaction(int transaction)
  3760. {
  3761. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3762. {
  3763. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = transaction;
  3764. }
  3765. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3766. {
  3767. }
  3768. }
  3769. void Set_Ocpp_DeductCreditNo(char *userId)
  3770. {
  3771. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3772. {
  3773. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, userId, strlen(userId));
  3774. }
  3775. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3776. {
  3777. }
  3778. }
  3779. void Set_Ocpp_DeductVemData(char *vemData)
  3780. {
  3781. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3782. {
  3783. strcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, vemData);
  3784. }
  3785. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3786. {
  3787. }
  3788. }
  3789. void Set_Ocpp_DeductAmount(double value)
  3790. {
  3791. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3792. {
  3793. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = value;
  3794. }
  3795. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3796. {
  3797. }
  3798. }
  3799. void Set_Ocpp_DeductResult(unsigned char result)
  3800. {
  3801. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3802. {
  3803. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = result;
  3804. }
  3805. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3806. {
  3807. }
  3808. }
  3809. void Set_Ocpp_DonateInvoice(unsigned char donate)
  3810. {
  3811. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3812. {
  3813. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = donate ? YES : NO;
  3814. }
  3815. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3816. {
  3817. }
  3818. }
  3819. bool Set_Ocpp_DeductInfo(DeductResultInfoData *deductInfo)
  3820. {
  3821. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3822. {
  3823. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = deductInfo->TransactionId;
  3824. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, (char *)deductInfo->creditNo, sizeof(deductInfo->creditNo));
  3825. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, deductInfo->VemData, sizeof(deductInfo->VemData));
  3826. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = ((double)deductInfo->DeductAmount / 100);
  3827. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.approvalNo, deductInfo->ApprovalNumber, sizeof(deductInfo->ApprovalNumber));
  3828. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = deductInfo->DeductResult;
  3829. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = deductInfo->IsDonateInvoice ? YES : NO;
  3830. return true;
  3831. }
  3832. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3833. {
  3834. }
  3835. return false;
  3836. }
  3837. bool Is_Ocpp_ReportCreditDeductReq(void)
  3838. {
  3839. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3840. {
  3841. return ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq ? true : false;
  3842. }
  3843. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3844. {
  3845. return false;
  3846. }
  3847. return false;
  3848. }
  3849. void Set_Ocpp_ReportCreditDeductReq(void)
  3850. {
  3851. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3852. {
  3853. ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq = YES;
  3854. }
  3855. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3856. {
  3857. }
  3858. }
  3859. //===============================================
  3860. // Ocpp Tcci SerialNo
  3861. //===============================================
  3862. void Clean_Ocpp_TcciSerialNo(int gun_index)
  3863. {
  3864. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3865. {
  3866. memset((char *)&ShmOCPP16Data->TcciCustomData.SerialNo[gun_index], 0x00, sizeof(37));
  3867. }
  3868. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3869. {
  3870. }
  3871. }
  3872. //===============================================
  3873. // Ocpp Tcci ReaderStatus
  3874. //===============================================
  3875. bool Set_Ocpp_TcciReaderStatus(int gun_index, DeductResultInfoData *ReaderStatus)
  3876. {
  3877. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3878. {
  3879. if(ReaderStatus->DeductResult == _Deduct_PreAuth_Cancel || ReaderStatus->DeductResult == _Deduct_PreAuth || ReaderStatus->DeductResult == _Deduct_PreAuth_Pass)
  3880. {
  3881. switch(ReaderStatus->DeductResult)
  3882. {
  3883. case _Deduct_PreAuth_Pass:
  3884. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 1;
  3885. break;
  3886. case _Deduct_PreAuth_Cancel:
  3887. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 2;
  3888. break;
  3889. case _Deduct_PreAuth:
  3890. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 0;
  3891. break;
  3892. }
  3893. memcpy((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, ReaderStatus->creditNo, sizeof(ReaderStatus->creditNo));
  3894. memcpy((char *)&ShmOCPP16Data->TcciCustomData.VEMData[gun_index], ReaderStatus->VemData, sizeof(ReaderStatus->VemData));
  3895. return true;
  3896. }
  3897. }
  3898. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3899. {
  3900. }
  3901. return false;
  3902. }
  3903. void Clean_Ocpp_TcciCreditInfo(int gun_index)
  3904. {
  3905. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3906. {
  3907. memset((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo));
  3908. memset((char *)&ShmOCPP16Data->TcciCustomData.VEMData[gun_index], 0x00, sizeof(ShmOCPP16Data->TcciCustomData.VEMData[gun_index]));
  3909. }
  3910. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3911. {
  3912. }
  3913. }
  3914. void Set_Ocpp_TcciReaderStatusReq(int gun_index)
  3915. {
  3916. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3917. {
  3918. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq = true;
  3919. }
  3920. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3921. {
  3922. }
  3923. }
  3924. bool Is_Ocpp_TcciReaderStatusReq(int gun_index)
  3925. {
  3926. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3927. {
  3928. return ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq ? true : false;
  3929. }
  3930. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3931. {
  3932. return false;
  3933. }
  3934. return false;
  3935. }
  3936. //===============================================
  3937. // Ocpp Tcci OccupancyFeeDisplay
  3938. //===============================================
  3939. bool Is_Ocpp_TcciOccupancyDisplayReq(int gun_index)
  3940. {
  3941. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3942. {
  3943. return ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].OccupancyFeeDisplayReq ? true : false;
  3944. }
  3945. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3946. {
  3947. return false;
  3948. }
  3949. return false;
  3950. }
  3951. void Clean_Ocpp_TcciOccupancyDisplayReq(int gun_index)
  3952. {
  3953. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3954. {
  3955. ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].OccupancyFeeDisplayReq = false;
  3956. }
  3957. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3958. {
  3959. }
  3960. }
  3961. // input: gun_index, sn_len
  3962. // output: sn
  3963. // return: action
  3964. int Get_Ocpp_TcciOccupancyAction(int gun_index, int sn_len, char *sn)
  3965. {
  3966. int action = 0;
  3967. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3968. {
  3969. action = ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].actionId;
  3970. if(action == _ParkingAction_DisplayButton)
  3971. {
  3972. memcpy(sn, ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN, sn_len);
  3973. }
  3974. }
  3975. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3976. {
  3977. }
  3978. return action;
  3979. }
  3980. void Clean_Ocpp_TcciOccupancyActionAndSn(int gun_index)
  3981. {
  3982. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3983. {
  3984. ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].actionId = 0;
  3985. memset(ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN));
  3986. }
  3987. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3988. {
  3989. }
  3990. }
  3991. //===============================================
  3992. // Ocpp Tcci SetOccupancyPrice
  3993. //===============================================
  3994. bool Is_Ocpp_TcciSetOccupancyPriceReq(void)
  3995. {
  3996. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3997. {
  3998. return ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.SetOccupancyPriceReq ? true : false;
  3999. }
  4000. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4001. {
  4002. return false;
  4003. }
  4004. return false;
  4005. }
  4006. void Clean_Ocpp_TcciSetOccupancyPriceReq(void)
  4007. {
  4008. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4009. {
  4010. ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.SetOccupancyPriceReq = false;
  4011. }
  4012. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4013. {
  4014. }
  4015. }
  4016. float Get_Ocpp_TcciOccupancyPrice(void)
  4017. {
  4018. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4019. {
  4020. return ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.price;
  4021. }
  4022. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4023. {
  4024. }
  4025. return 0;
  4026. }
  4027. //===============================================
  4028. // Ocpp Tcci GetOccupancyFee
  4029. //===============================================
  4030. bool Is_Ocpp_TcciGetOccupancyFeeReq(int gun_index)
  4031. {
  4032. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4033. {
  4034. return ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].GetOccupancyFeeReq ? true : false;
  4035. }
  4036. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4037. {
  4038. return false;
  4039. }
  4040. return false;
  4041. }
  4042. void Set_Ocpp_TcciGetOccupancyFeeReq(int gun_index, OccupancyBillInfo *bill)
  4043. {
  4044. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4045. {
  4046. ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].GetOccupancyFeeReq = true;
  4047. memset(ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].occupancySN, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].occupancySN));
  4048. memcpy(ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].occupancySN, bill->Sn, sizeof(bill->Sn));
  4049. }
  4050. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4051. {
  4052. }
  4053. }
  4054. void Clean_Ocpp_TcciGetOccupancyFeeReq(int gun_index)
  4055. {
  4056. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4057. {
  4058. ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].GetOccupancyFeeReq = false;
  4059. }
  4060. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4061. {
  4062. }
  4063. }
  4064. void Get_Ocpp_TcciGetOccupancyFeeResponse(int gun_index, OccupancyBillInfo *bill)
  4065. {
  4066. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4067. {
  4068. bill->Duration = ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].response_duration;
  4069. bill->Fee = ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].response_occupancyFee;
  4070. memcpy(bill->StartTime, ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].response_startTime, sizeof(bill->StartTime));
  4071. }
  4072. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4073. {
  4074. }
  4075. }
  4076. bool Is_Ocpp_TcciGetOccupancyFeeStatus(int gun_index, char *status)
  4077. {
  4078. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4079. {
  4080. return strcmp((char *)ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].response_status, status) == EQUAL ? true : false;
  4081. }
  4082. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4083. {
  4084. }
  4085. return false;
  4086. }
  4087. //===============================================
  4088. // Ocpp Tcci ReportOccupancyDeductReq
  4089. //===============================================
  4090. bool Is_Ocpp_TcciReportOccupancyDeductReq(void)
  4091. {
  4092. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4093. {
  4094. return ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq ? true : false;
  4095. }
  4096. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4097. {
  4098. return false;
  4099. }
  4100. return false;
  4101. }
  4102. void Set_Ocpp_TcciReportOccupancyDeductReq(void)
  4103. {
  4104. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4105. {
  4106. ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq = true;
  4107. }
  4108. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4109. {
  4110. }
  4111. }
  4112. //===============================================
  4113. // Ocpp Tcci OccupancyDeductResult
  4114. //===============================================
  4115. bool Set_Ocpp_OccupancyDeductResult(OccupancyDeductInfo *deduct, bool pass_fail)
  4116. {
  4117. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4118. {
  4119. memset(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult, 0x00, sizeof(struct StructOccupancyDeductResult));
  4120. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.deductResult = pass_fail;
  4121. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.amount = deduct->Fee;
  4122. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.occupancySN[0], &deduct->Sn[0], sizeof(deduct->Sn));
  4123. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.creditNo[0], &deduct->creditNo[0], sizeof(deduct->creditNo));
  4124. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.approvalNo[0], &deduct->ApprovalNumber[0], sizeof(deduct->ApprovalNumber));
  4125. return true;
  4126. }
  4127. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4128. {
  4129. }
  4130. return false;
  4131. }
  4132. //===============================================
  4133. // Ocpp Start Transaction
  4134. //===============================================
  4135. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id)
  4136. {
  4137. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4138. {
  4139. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag, id);
  4140. }
  4141. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4142. {
  4143. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4144. }
  4145. // MaintainServer
  4146. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4147. {
  4148. strcpy((char *)ShmOCPP16DataPH->StartTransaction[gun_index].IdTag, id);
  4149. }
  4150. }
  4151. void Set_Ocpp_StartTransactionReq(int gun_index)
  4152. {
  4153. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4154. {
  4155. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = true;
  4156. }
  4157. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4158. {
  4159. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4160. }
  4161. // MaintainServer
  4162. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4163. {
  4164. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq = true;
  4165. }
  4166. }
  4167. bool Is_Ocpp_StartTransactionConf(int gun_index)
  4168. {
  4169. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4170. {
  4171. return ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == YES ? true : false;
  4172. }
  4173. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4174. {
  4175. return ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf == YES ? true : false;
  4176. }
  4177. return false;
  4178. }
  4179. void Clean_Ocpp_StartTransactionConf(int gun_index)
  4180. {
  4181. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4182. {
  4183. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = false;
  4184. }
  4185. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4186. {
  4187. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  4188. }
  4189. // MaintainServer
  4190. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4191. {
  4192. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf = false;
  4193. }
  4194. }
  4195. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4196. {
  4197. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4198. {
  4199. return strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  4200. }
  4201. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4202. {
  4203. return strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, status) == EQUAL ? true : false;
  4204. }
  4205. return false;
  4206. }
  4207. bool Is_Ocpp_StartTransaction_Accepted(int gun_index)
  4208. {
  4209. return Is_Ocpp_StartTransaction_Status(gun_index, "Accepted");
  4210. }
  4211. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4212. {
  4213. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4214. {
  4215. strcpy(status, (char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status);
  4216. }
  4217. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4218. {
  4219. strcpy(status, (char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status);
  4220. }
  4221. }
  4222. void Get_Ocpp_TransactionId(int gun_index, char *transactionId)
  4223. {
  4224. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4225. {
  4226. sprintf(transactionId, "%d", ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId);
  4227. return;
  4228. }
  4229. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4230. {
  4231. strcpy(transactionId, (char *)&ShmOCPP20Data->TransactionEvent [gun_index].transactionInfo.transactionId[0]);
  4232. return;
  4233. }
  4234. sprintf(transactionId, "%d", 0);
  4235. }
  4236. bool Is_Invalid_StartTransactionId(int gun_index)
  4237. {
  4238. if(Is_Ocpp_StartTransaction_Status(gun_index, "Blocked") ||
  4239. Is_Ocpp_StartTransaction_Status(gun_index, "Expired") ||
  4240. Is_Ocpp_StartTransaction_Status(gun_index, "Invalid") ||
  4241. Is_Ocpp_StartTransaction_Status(gun_index, "NoCredit") ||
  4242. Is_Ocpp_StartTransaction_Status(gun_index, "NotAllowedTypeEVSE") ||
  4243. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisLocation") ||
  4244. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisTime") ||
  4245. Is_Ocpp_StartTransaction_Status(gun_index, "Unknown"))
  4246. {
  4247. return true;
  4248. }
  4249. return false;
  4250. }
  4251. //===============================================
  4252. // Ocpp Stop Transaction
  4253. //===============================================
  4254. void Set_Ocpp_StopTransactionId(int gun_index, char *id)
  4255. {
  4256. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4257. {
  4258. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].IdTag, id);
  4259. return;
  4260. }
  4261. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4262. {
  4263. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4264. return;
  4265. }
  4266. }
  4267. void Set_Ocpp_StopTransactionReq(int gun_index)
  4268. {
  4269. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4270. {
  4271. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = true;
  4272. }
  4273. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4274. {
  4275. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4276. }
  4277. // MaintainServer
  4278. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4279. {
  4280. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = true;
  4281. }
  4282. }
  4283. //===============================================
  4284. // Ocpp Check Backup Error Code
  4285. //===============================================
  4286. void Chk_Ocpp_Backup_ErrorCode(int gun_index)
  4287. {
  4288. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4289. {
  4290. if(strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, "") == EQUAL)
  4291. {
  4292. if(strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, "") != EQUAL)
  4293. {
  4294. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, (char *)_BackupStatusNotification[gun_index].ErrorCode);
  4295. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, (char *)_BackupStatusNotification[gun_index].VendorErrorCode);
  4296. }
  4297. }
  4298. }
  4299. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4300. {
  4301. }
  4302. }
  4303. //===============================================
  4304. // Ocpp Reserve Now
  4305. //===============================================
  4306. bool Is_Ocpp_ReserveNowReq(int gun_index)
  4307. {
  4308. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4309. {
  4310. return ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4311. }
  4312. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4313. {
  4314. return ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4315. }
  4316. return false;
  4317. }
  4318. void Clean_Ocpp_ReserveNowReq(int gun_index)
  4319. {
  4320. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4321. {
  4322. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4323. return;
  4324. }
  4325. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4326. {
  4327. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4328. return;
  4329. }
  4330. }
  4331. int Get_Ocpp_ReserveNowId(int gun_index)
  4332. {
  4333. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4334. {
  4335. return ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  4336. }
  4337. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4338. {
  4339. return ShmOCPP20Data->ReserveNow[gun_index].id;
  4340. }
  4341. return 0;
  4342. }
  4343. void Set_Ocpp_ReserveNowConf(int gun_index)
  4344. {
  4345. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4346. {
  4347. ShmOCPP16Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4348. return;
  4349. }
  4350. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4351. {
  4352. ShmOCPP20Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4353. return;
  4354. }
  4355. }
  4356. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date)
  4357. {
  4358. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4359. {
  4360. strcpy(date, (char *)ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate);
  4361. return;
  4362. }
  4363. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4364. {
  4365. strcpy(date, (char *)ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime);
  4366. return;
  4367. }
  4368. strcpy(date, "");
  4369. return;
  4370. }
  4371. bool Is_Ocpp_ReserveNowExpired(int gun_index)
  4372. {
  4373. bool _expired = true;
  4374. char _expiryDate[128];
  4375. Get_Ocpp_ReserveNowExpiryDate(gun_index, _expiryDate);
  4376. _expired = CheckTimeOut(_expiryDate);
  4377. return _expired;
  4378. }
  4379. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag)
  4380. {
  4381. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4382. {
  4383. strcpy(idTag, (char *)ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  4384. return;
  4385. }
  4386. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4387. {
  4388. strcpy(idTag, (char *)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken);
  4389. return;
  4390. }
  4391. strcpy(idTag, "");
  4392. return;
  4393. }
  4394. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag)
  4395. {
  4396. char ReserveIdTag[32];
  4397. Get_Ocpp_ReserveNowIdTag(gun_index, ReserveIdTag);
  4398. return strcmp(ReserveIdTag, authIdTag) == EQUAL ? true : false;
  4399. }
  4400. //===============================================
  4401. // Ocpp Cancel Reservation
  4402. //===============================================
  4403. bool Is_Ocpp_CancelReservationReq(int gun_index)
  4404. {
  4405. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4406. {
  4407. return ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4408. }
  4409. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4410. {
  4411. return ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4412. }
  4413. return false;
  4414. }
  4415. void Clean_Ocpp_CancelReservationReq(int gun_index)
  4416. {
  4417. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4418. {
  4419. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4420. return;
  4421. }
  4422. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4423. {
  4424. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4425. return;
  4426. }
  4427. }
  4428. void Set_Ocpp_CancelReservationConf(int gun_index)
  4429. {
  4430. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4431. {
  4432. ShmOCPP16Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4433. return;
  4434. }
  4435. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4436. {
  4437. ShmOCPP20Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4438. return;
  4439. }
  4440. }
  4441. //===============================================
  4442. // Ocpp Change Availability
  4443. //===============================================
  4444. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index)
  4445. {
  4446. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4447. {
  4448. return ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4449. }
  4450. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4451. {
  4452. return ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4453. }
  4454. return false;
  4455. }
  4456. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index)
  4457. {
  4458. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4459. {
  4460. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4461. return;
  4462. }
  4463. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4464. {
  4465. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4466. return;
  4467. }
  4468. }
  4469. bool Chk_Ocpp_Availability_Type(int gun_index, char *type)
  4470. {
  4471. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4472. {
  4473. return strcmp((char *)ShmOCPP16Data->ChangeAvailability[gun_index].Type, type) == EQUAL ? true : false;
  4474. }
  4475. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4476. {
  4477. return strcmp((char *)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, type) == EQUAL ? true : false;
  4478. }
  4479. return false;
  4480. }
  4481. //===============================================
  4482. // Ocpp Unlock Connector
  4483. //===============================================
  4484. bool Is_Ocpp_UnlockConnectorReq(int gun_index)
  4485. {
  4486. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4487. {
  4488. return ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4489. }
  4490. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4491. {
  4492. return ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4493. }
  4494. return false;
  4495. }
  4496. void Clean_Ocpp_UnlockConnectorReq(int gun_index)
  4497. {
  4498. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4499. {
  4500. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4501. return;
  4502. }
  4503. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4504. {
  4505. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4506. return;
  4507. }
  4508. }
  4509. void Set_Ocpp_UnlockConnectorConf(int gun_index)
  4510. {
  4511. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4512. {
  4513. strcpy((char *)ShmOCPP16Data->UnlockConnector[gun_index].ResponseStatus, "Unlocked");
  4514. ShmOCPP16Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4515. return;
  4516. }
  4517. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4518. {
  4519. strcpy((char *)ShmOCPP20Data->UnlockConnector[gun_index].Response_status, "Unlocked");
  4520. ShmOCPP20Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4521. return;
  4522. }
  4523. }
  4524. //===============================================
  4525. // Ocpp Remote Start
  4526. //===============================================
  4527. bool Is_Ocpp_RemoteStartReq(int gun_index)
  4528. {
  4529. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4530. {
  4531. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES ? true : false;
  4532. }
  4533. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4534. {
  4535. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq == YES ? true : false;
  4536. }
  4537. return false;
  4538. }
  4539. void Clean_Ocpp_RemoteStartReq(int gun_index)
  4540. {
  4541. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4542. {
  4543. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = false;
  4544. return;
  4545. }
  4546. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4547. {
  4548. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = false;
  4549. return;
  4550. }
  4551. }
  4552. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag)
  4553. {
  4554. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4555. {
  4556. strcpy(idTag, (char *)&ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag[0]);
  4557. return;
  4558. }
  4559. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4560. {
  4561. strcpy(idTag, (char *)&ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken[0]);
  4562. return;
  4563. }
  4564. strcpy(idTag, "");
  4565. }
  4566. //===============================================
  4567. // Ocpp Remote Stop
  4568. //===============================================
  4569. bool Is_Ocpp_RemoteStopReq(int gun_index)
  4570. {
  4571. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4572. {
  4573. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == YES ? true : false;
  4574. }
  4575. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4576. {
  4577. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq == YES ? true : false;
  4578. }
  4579. return false;
  4580. }
  4581. void Clean_Ocpp_RemoteStopReq(int gun_index)
  4582. {
  4583. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4584. {
  4585. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = false;
  4586. return;
  4587. }
  4588. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4589. {
  4590. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = false;
  4591. return;
  4592. }
  4593. }
  4594. //===============================================
  4595. // Ocpp StopReason
  4596. //===============================================
  4597. void Set_Ocpp_StopReason(int gun_index, char *reason)
  4598. {
  4599. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4600. {
  4601. if(strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  4602. {
  4603. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, reason);
  4604. }
  4605. }
  4606. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4607. {
  4608. if(strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "" ) == EQUAL)
  4609. {
  4610. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, reason);
  4611. }
  4612. }
  4613. // Maintain server
  4614. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4615. {
  4616. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, ShmOCPP16Data->StopTransaction[gun_index].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  4617. }
  4618. }
  4619. void Get_Ocpp_StopReason(int gun_index, char *reason)
  4620. {
  4621. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4622. {
  4623. strcpy(reason, (char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4624. return;
  4625. }
  4626. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4627. {
  4628. strcpy(reason, (char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4629. return;
  4630. }
  4631. strcpy(reason, "");
  4632. }
  4633. void Clean_Ocpp_StopReason(int gun_index)
  4634. {
  4635. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4636. {
  4637. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  4638. }
  4639. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4640. {
  4641. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "");
  4642. }
  4643. // Maintain server
  4644. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4645. {
  4646. strcpy((char *)ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, "");
  4647. }
  4648. }
  4649. //===============================================
  4650. // Ocpp StatusNotification ErrorCode
  4651. //===============================================
  4652. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4653. {
  4654. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4655. {
  4656. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode);
  4657. return;
  4658. }
  4659. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4660. {
  4661. return;
  4662. }
  4663. }
  4664. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4665. {
  4666. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4667. {
  4668. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4669. }
  4670. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4671. {
  4672. return strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4673. }
  4674. return false;
  4675. }
  4676. //===============================================
  4677. // Ocpp StatusNotification VendorErrorCode
  4678. //===============================================
  4679. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4680. {
  4681. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4682. {
  4683. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode);
  4684. return;
  4685. }
  4686. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4687. {
  4688. return;
  4689. }
  4690. }
  4691. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4692. {
  4693. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4694. {
  4695. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4696. }
  4697. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4698. {
  4699. return strcmp((char *)_BackupStatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4700. }
  4701. return false;
  4702. }
  4703. //===============================================
  4704. // Ocpp ChargingProfileConf
  4705. //===============================================
  4706. bool Is_Ocpp_ChargingProfileConf(int gun_index)
  4707. {
  4708. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4709. {
  4710. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4711. }
  4712. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4713. {
  4714. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4715. }
  4716. return false;
  4717. }
  4718. void Clean_Ocpp_ChargingProfileConf(int gun_index)
  4719. {
  4720. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4721. {
  4722. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4723. return;
  4724. }
  4725. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4726. {
  4727. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4728. return;
  4729. }
  4730. }
  4731. //===============================================
  4732. // Ocpp ChargingProfileReq
  4733. //===============================================
  4734. bool Is_Ocpp_ChargingProfileReq(int gun_index)
  4735. {
  4736. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4737. {
  4738. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4739. }
  4740. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4741. {
  4742. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4743. }
  4744. return false;
  4745. }
  4746. void Set_Ocpp_ChargingProfileReq(int gun_index)
  4747. {
  4748. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4749. {
  4750. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4751. return;
  4752. }
  4753. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4754. {
  4755. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4756. return;
  4757. }
  4758. }
  4759. //===============================================
  4760. // Ocpp SmartChargingProfile ChargingProfileKind
  4761. //===============================================
  4762. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind)
  4763. {
  4764. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4765. {
  4766. return strcmp((char *)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, kind) == EQUAL ? true : false;
  4767. }
  4768. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4769. {
  4770. return strcmp((char *)ShmOCPP20Data->SmartChargingProfile [gun_index].chargingProfileKind, kind) == EQUAL ? true : false;
  4771. }
  4772. return false;
  4773. }
  4774. bool Is_Ocpp_ChargingProfileId(int gun_index)
  4775. {
  4776. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4777. {
  4778. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == YES ? true : false;
  4779. }
  4780. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4781. {
  4782. return ShmOCPP20Data->SmartChargingProfile[gun_index].id == YES ? true : false;
  4783. }
  4784. return false;
  4785. }
  4786. int Get_Ocpp_ChargingProfileStartTime(int gun_index)
  4787. {
  4788. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4789. {
  4790. return GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  4791. }
  4792. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4793. {
  4794. return GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  4795. }
  4796. return 0;
  4797. }
  4798. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index)
  4799. {
  4800. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4801. {
  4802. return ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);
  4803. }
  4804. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4805. {
  4806. return ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);
  4807. }
  4808. return 0;
  4809. }
  4810. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index)
  4811. {
  4812. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4813. {
  4814. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4815. }
  4816. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4817. {
  4818. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4819. }
  4820. return 0;
  4821. }
  4822. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index)
  4823. {
  4824. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4825. {
  4826. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4827. }
  4828. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4829. {
  4830. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4831. }
  4832. return 0;
  4833. }
  4834. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index)
  4835. {
  4836. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4837. {
  4838. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4839. }
  4840. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4841. {
  4842. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].numberPhases;
  4843. }
  4844. return 0;
  4845. }
  4846. int Get_Ocpp_ChargingProfileStartIndex(int gun_index)
  4847. {
  4848. int _time = 0, _scheduleTime = 0;
  4849. int _maxCount = 0;
  4850. float _limit = 0;
  4851. int _startCount = NO_DEFINE;
  4852. _time = Get_Ocpp_ChargingProfileStartTime(gun_index);
  4853. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(gun_index);
  4854. for(int i = 0; i < _maxCount; i++)
  4855. {
  4856. _scheduleTime = Get_Ocpp_ChargingProfileScheduleTime(gun_index, i);
  4857. if(_time >= _scheduleTime)
  4858. {
  4859. _limit = Get_Ocpp_ChargingProfileScheduleLimit(gun_index, i);
  4860. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4861. {
  4862. _startCount = i;
  4863. }
  4864. }
  4865. }
  4866. return _startCount;
  4867. }
  4868. //===============================================
  4869. // Ocpp MaxChargingProfile ChargingProfileKind
  4870. //===============================================
  4871. bool Is_Ocpp_MaxChargingProfileKind(char *kind)
  4872. {
  4873. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4874. {
  4875. return strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, kind) == EQUAL ? true : false;
  4876. }
  4877. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4878. {
  4879. return strcmp((char *)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, kind) == EQUAL ? true : false;
  4880. }
  4881. return false;
  4882. }
  4883. bool Is_Ocpp_MaxChargingProfileId(void)
  4884. {
  4885. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4886. {
  4887. return ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES ? true : false;
  4888. }
  4889. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4890. {
  4891. return ShmOCPP20Data->MaxChargingProfile.id == YES ? true : false;
  4892. }
  4893. return false;
  4894. }
  4895. int Get_Ocpp_MaxProfileStartTime(void)
  4896. {
  4897. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4898. {
  4899. return GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  4900. }
  4901. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4902. {
  4903. return GetStartScheduleTime(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule);
  4904. }
  4905. return 0;
  4906. }
  4907. int Get_Ocpp_MaxProfileScheduleCount(void)
  4908. {
  4909. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4910. {
  4911. return ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  4912. }
  4913. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4914. {
  4915. return ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);
  4916. }
  4917. return 0;
  4918. }
  4919. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index)
  4920. {
  4921. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4922. {
  4923. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4924. }
  4925. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4926. {
  4927. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4928. }
  4929. return 0;
  4930. }
  4931. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index)
  4932. {
  4933. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4934. {
  4935. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4936. }
  4937. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4938. {
  4939. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4940. }
  4941. return 0;
  4942. }
  4943. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index)
  4944. {
  4945. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4946. {
  4947. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4948. }
  4949. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4950. {
  4951. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4952. }
  4953. return 0;
  4954. }
  4955. int Get_Ocpp_MaxProfileStartIndex(void)
  4956. {
  4957. int _time = 0, _scheduleTime = 0;
  4958. int _maxCount = 0;
  4959. float _limit = 0;
  4960. int _startCount = NO_DEFINE;
  4961. _time = Get_Ocpp_MaxProfileStartTime();
  4962. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  4963. for(int i = 0; i < _maxCount; i++)
  4964. {
  4965. _scheduleTime = Get_Ocpp_MaxProfileScheduleTime(i);
  4966. if(_time >= _scheduleTime)
  4967. {
  4968. _limit = Get_Ocpp_MaxProfileScheduleLimit(i);
  4969. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4970. {
  4971. _startCount = i;
  4972. }
  4973. }
  4974. }
  4975. return _startCount;
  4976. }
  4977. //===============================================
  4978. // Ocpp isRemoteStartWaitReq
  4979. //===============================================
  4980. int Is_Ocpp_RemoteStartWaitReq(void)
  4981. {
  4982. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4983. {
  4984. return ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  4985. }
  4986. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4987. {
  4988. return ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  4989. }
  4990. return 0;
  4991. }
  4992. //===============================================
  4993. // Ocpp SessionTarget
  4994. //===============================================
  4995. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget)
  4996. {
  4997. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4998. {
  4999. memcpy(sessionTarget, &ShmOCPP16Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5000. }
  5001. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5002. {
  5003. memcpy(sessionTarget, &ShmOCPP20Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5004. }
  5005. }
  5006. //===============================================
  5007. // Ocpp OffLineMaxChargingPower
  5008. //===============================================
  5009. int Get_Ocpp_OffLineMaxChargingPower(void)
  5010. {
  5011. // unit: W
  5012. int maxChargingPower = 0;
  5013. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5014. {
  5015. maxChargingPower = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[OffLineMaxChargingPower].ItemData);
  5016. }
  5017. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5018. {
  5019. maxChargingPower = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[OffLineMaxChargingPower].variableAttribute[0].value);
  5020. }
  5021. return maxChargingPower > 0 ? maxChargingPower : 0;
  5022. }
  5023. //===============================================
  5024. // DeductResult
  5025. //===============================================
  5026. void Set_Deduct_UserId(int gunIndex, char *userId)
  5027. {
  5028. memcpy((char *)&ShmChargerInfo->DeductResultReq[gunIndex].creditNo, userId, strlen(userId));
  5029. }
  5030. void Set_Deduct_TransactionId(int gunIndex, int transaction)
  5031. {
  5032. ShmChargerInfo->DeductResultReq[gunIndex].TransactionId = transaction;
  5033. }
  5034. void Clean_DeductInfo(int gunIndex)
  5035. {
  5036. memset(&ShmChargerInfo->DeductResultReq[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5037. }
  5038. bool Is_DeductResultReq(int gunIndex)
  5039. {
  5040. return ShmChargerInfo->DeductResultReq[gunIndex].DeductReq ? true : false;
  5041. }
  5042. //===============================================
  5043. // ReaderStatus
  5044. //===============================================
  5045. bool Is_ReaderStatusReq(int gunIndex)
  5046. {
  5047. return ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq ? true : false;
  5048. }
  5049. void Clean_ReaderStatusInfo(int gunIndex)
  5050. {
  5051. memset(&ShmChargerInfo->ReaderStatus[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5052. }
  5053. //===============================================
  5054. // TransactionInfoData
  5055. //===============================================
  5056. void Set_UserTransactionId(int gunIndex, int transaction)
  5057. {
  5058. ShmChargerInfo->UserTransaction[gunIndex].TransactionId = transaction;
  5059. }
  5060. void Set_UserPrice(int gunIndex, float price)
  5061. {
  5062. ShmChargerInfo->UserTransaction[gunIndex].UserPrice = price;
  5063. }
  5064. void Set_UserTotalCost(int gunIndex, float cost)
  5065. {
  5066. ShmChargerInfo->UserTransaction[gunIndex].TotalCost = cost;
  5067. }
  5068. void Set_UserAccountBalance(int gunIndex, float balance)
  5069. {
  5070. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = balance;
  5071. }
  5072. void Set_UserDiscount(int gunIndex, float discount)
  5073. {
  5074. ShmChargerInfo->UserTransaction[gunIndex].CostDiscount = discount;
  5075. }
  5076. void Clean_UserTransaction(int gunIndex)
  5077. {
  5078. memset(&ShmChargerInfo->UserTransaction[gunIndex], 0x00, sizeof(TransactionInfoData));
  5079. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = -65535;
  5080. }
  5081. void Clean_UserPriceAndReceipt(int gunIndex)
  5082. {
  5083. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex]));
  5084. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex]));
  5085. }
  5086. //===============================================
  5087. // OccupancyDeductReq
  5088. //===============================================
  5089. bool Is_OccupancyDeductReq(int gunIndex)
  5090. {
  5091. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq ? true : false;
  5092. }
  5093. void Clean_OccupancyDeductReq(int gunIndex)
  5094. {
  5095. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq = false;
  5096. }
  5097. bool Is_OccupancyKeepCountingReq(int gunIndex)
  5098. {
  5099. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting ? true : false;
  5100. }
  5101. void Set_OccupancyKeepCountingReq(int gunIndex)
  5102. {
  5103. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = true;
  5104. }
  5105. void Clean_OccupancyKeepCountingReq(int gunIndex)
  5106. {
  5107. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = false;
  5108. }
  5109. //===============================================
  5110. // Main process
  5111. //===============================================
  5112. // 檢查 Byte 中某個 Bit 的值
  5113. // _byte : 欲改變的 byte
  5114. // _bit : 該 byte 的第幾個 bit
  5115. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  5116. {
  5117. return ( _byte & mask_table[_bit] ) != 0x00;
  5118. }
  5119. // 設定 Byte 中某個 Bit的值
  5120. // _byte : 欲改變的 byte
  5121. // _bit : 該 byte 的第幾個 bit
  5122. // value : 修改的值為 0 or 1
  5123. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  5124. {
  5125. if(value == 1)
  5126. *_byte |= (1 << _bit);
  5127. else if (value == 0)
  5128. *_byte ^= (1 << _bit);
  5129. }
  5130. void UserScanFunction()
  5131. {
  5132. }
  5133. unsigned char isModeChange(unsigned char gun_index)
  5134. {
  5135. unsigned char result = NO;
  5136. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  5137. {
  5138. result = YES;
  5139. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  5140. }
  5141. return result;
  5142. }
  5143. void AnnounceAccountBalance(unsigned char index, float balance)
  5144. {
  5145. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  5146. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = balance;
  5147. if(!ShmChargerInfo->Control.TestCtrl.bits.DisableBalance)
  5148. {
  5149. Set_Connector_MiscCommand(index, MISC_CONN_ACCOUNT_BALANCE);
  5150. }
  5151. LOG_INFO("%sAnnounce Account Balance %4.2f To Connector %d",
  5152. ShmChargerInfo->Control.TestCtrl.bits.DisableBalance ? "Do Not " : "", balance, index + 1);
  5153. }
  5154. void FouceAnnounceAccountBalance(unsigned char index)
  5155. {
  5156. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  5157. {
  5158. AnnounceAccountBalance(index, 0);
  5159. }
  5160. }
  5161. void CleanConnectorUserIdAndPrice(unsigned char index)
  5162. {
  5163. if(chargingInfo[index]->SystemStatus == S_IDLE)
  5164. {
  5165. LOG_INFO("*********** Connector %d Clean UserId & UserPrice ***********", index + 1);
  5166. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  5167. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  5168. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -65535;
  5169. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  5170. }
  5171. }
  5172. void DispenserAuthInitial(unsigned char dispenser)
  5173. {
  5174. LOG_INFO("*********** Dispenser %d Authorizing Initial ***********", dispenser + 1);
  5175. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5176. // clean dispenser price
  5177. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser]));
  5178. }
  5179. void GunAuthInitial(unsigned char connector)
  5180. {
  5181. LOG_INFO("*********** Gun %d Authorizing Initial ***********", connector + 1);
  5182. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5183. }
  5184. unsigned char _preGunAuthResult[GENERAL_GUN_QUANTITY];
  5185. unsigned char _preDispenserAuthResult[GENERAL_GUN_QUANTITY];
  5186. struct timespec _GunAuth_Time[GENERAL_GUN_QUANTITY];
  5187. struct timespec _DispenserAuth_Time[GENERAL_GUN_QUANTITY];
  5188. void AuthorizeTimeoutProcess(void)
  5189. {
  5190. int CompletedTime = 0;
  5191. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5192. {
  5193. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult != _AuthResult_None)
  5194. {
  5195. if(_preGunAuthResult[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult)
  5196. {
  5197. GetClockTime(&_GunAuth_Time[index]);
  5198. }
  5199. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5200. {
  5201. FouceAnnounceAccountBalance(index);
  5202. }
  5203. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5204. {
  5205. if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= AUTHORIZE_COMPLETED_TIME)
  5206. {
  5207. LOG_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
  5208. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5209. CleanConnectorUserIdAndPrice(index);
  5210. }
  5211. }
  5212. }
  5213. _preGunAuthResult[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult;
  5214. }
  5215. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5216. {
  5217. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult != _AuthResult_None)
  5218. {
  5219. if(_preDispenserAuthResult[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult)
  5220. {
  5221. GetClockTime(&_DispenserAuth_Time[index]);
  5222. }
  5223. CompletedTime = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
  5224. (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + AUTHORIZE_COMPLETED_TIME) : AUTHORIZE_COMPLETED_TIME;
  5225. if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= CompletedTime)
  5226. {
  5227. DispenserAuthInitial(index);
  5228. }
  5229. }
  5230. _preDispenserAuthResult[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult;
  5231. }
  5232. }
  5233. BOOL IsConnectorAuthorizeSuccess(unsigned char connector)
  5234. {
  5235. BOOL success = false;
  5236. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult == _AuthResult_Valid)
  5237. {
  5238. success = true;
  5239. }
  5240. return success;
  5241. }
  5242. BOOL IsAutoGunSelectionAuthorizeSuccess(unsigned char connector)
  5243. {
  5244. BOOL success = false;
  5245. unsigned char dispenser = 0;
  5246. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  5247. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo.AuthResult == _AuthResult_Valid)
  5248. {
  5249. success = true;
  5250. }
  5251. return success;
  5252. }
  5253. bool CheckConnectorTypeStatus(void)
  5254. {
  5255. bool result = true;
  5256. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 14)
  5257. {
  5258. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = ShmChargerInfo->Control.MaxConnector;
  5259. ShmChargerInfo->Control.ChargerRatingPower = ParsingRatingPower((char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5260. ShmChargerInfo->Control.PsuQuantityByModel = ShmChargerInfo->Control.ChargerRatingPower / (INFY_SINGLE_PSU_POWER / 100);
  5261. LOG_INFO("DC Gun Count = %d, RatingPower = %d.%d kW",
  5262. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  5263. (ShmChargerInfo->Control.ChargerRatingPower / 10),
  5264. (ShmChargerInfo->Control.ChargerRatingPower % 10));
  5265. if(ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  5266. {
  5267. result = false;
  5268. }
  5269. //DO360 charging info default setting
  5270. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5271. {
  5272. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  5273. chargingInfo[i]->Index = (i < ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity || i == 0) ? i : 0xFF;
  5274. chargingInfo[i]->Evboard_id = 0x00;
  5275. chargingInfo[i]->ReservationId = -1;
  5276. chargingInfo[i]->slotsIndex = 0;
  5277. chargingInfo[i]->SystemStatus = S_BOOTING;
  5278. chargingInfo[i]->Type = _Type_Unknown;
  5279. chargingInfo[i]->type_index = 0;
  5280. chargingInfo[i]->IsAvailable = YES;
  5281. }
  5282. }
  5283. else
  5284. {
  5285. // Module Name 不正確 - 告警
  5286. result = false;
  5287. }
  5288. return result;
  5289. }
  5290. void KillTaskExceptPrimary()
  5291. {
  5292. system("killall Module_Authorize");
  5293. system("killall Module_CabinetParallel");
  5294. system("killall Module_ChargerSelfTest");
  5295. system("killall Module_LedIndication");
  5296. system("killall Module_EvComm");
  5297. system("killall Module_InternalComm");
  5298. KillPowerModuleTask();
  5299. system("killall Module_4g &");
  5300. system("killall Module_Wifi &");
  5301. }
  5302. void KillNtpd(void)
  5303. {
  5304. system("killall ntpd");
  5305. }
  5306. void KillAllTask()
  5307. {
  5308. system("killall Module_Authorize");
  5309. system("killall Module_CabinetParallel");
  5310. system("killall Module_ChargerSelfTest");
  5311. system("killall Module_LedIndication");
  5312. system("killall Module_EventLogging");
  5313. system("killall Module_PrimaryComm");
  5314. system("killall Module_EvComm");
  5315. system("killall Module_LcmControl");
  5316. system("killall Module_InternalComm");
  5317. KillPowerModuleTask();
  5318. Kill_Ocpp_Service();
  5319. Kill_MaintainOcpp_Service();
  5320. system("killall Module_4g &");
  5321. system("killall Module_Wifi &");
  5322. }
  5323. int TransferImageViaFTP(char *IpAddress, char *SourcePath)
  5324. {
  5325. int isPass = PASS;
  5326. unsigned char ftpcmdbuf[256];
  5327. //Using ftpput command to transfer upgrade image,
  5328. //User name : root
  5329. //User password : y42j/4cj84
  5330. //Destination : as same as source path
  5331. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s %s %s", IpAddress, SourcePath, SourcePath);
  5332. if(system((char*)ftpcmdbuf) != 0)
  5333. {
  5334. isPass = FAIL;
  5335. }
  5336. return isPass;
  5337. }
  5338. void TriggerDispenserUpgrade(unsigned char index, char *FileName)
  5339. {
  5340. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, 0x00, 128);
  5341. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, FileName, strlen(FileName));
  5342. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.FirmwareUpdateRequest = true;
  5343. LOG_INFO("Trigger dispenser %d to update image [%s]", index + 1, FileName);
  5344. }
  5345. BOOL PutImageToAllDispenser(char *SourcePath, char *FileName, char *model_name)
  5346. {
  5347. int isPass = FAIL;
  5348. int isImageOK[GENERAL_GUN_QUANTITY];
  5349. int dispenser_index = 0;
  5350. BOOL transferred = FALSE;
  5351. memset(isImageOK, 0x00, sizeof(isImageOK));
  5352. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5353. {
  5354. isPass = FAIL;
  5355. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  5356. {
  5357. dispenser_index = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex;
  5358. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName, (char *)model_name) != EQUAL)
  5359. {
  5360. LOG_INFO("Dispenser %d model name [%s] update ignore",
  5361. dispenser_index + 1,
  5362. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName);
  5363. continue;
  5364. }
  5365. char DispenserIpAddress[16];
  5366. memset((char*)DispenserIpAddress, 0x00, 16);
  5367. sprintf((char*)DispenserIpAddress, "%d.%d.%d.%d",
  5368. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  5369. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  5370. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  5371. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  5372. LOG_INFO("Transfer Image(%s) to Dispenser %d (IP: %s)", SourcePath, dispenser_index + 1, DispenserIpAddress);
  5373. isPass = TransferImageViaFTP(DispenserIpAddress, SourcePath);
  5374. isImageOK[dispenser_index] = isPass;
  5375. LOG_INFO("Dispenser %d Put Image %s", dispenser_index + 1, isPass == PASS ? "OK" : "Fail");
  5376. }
  5377. }
  5378. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5379. {
  5380. if(isImageOK[i] > 0)
  5381. {
  5382. TriggerDispenserUpgrade(i, FileName);
  5383. transferred = true;
  5384. }
  5385. }
  5386. return transferred;
  5387. }
  5388. BOOL WaitAllDispenserUpgradeCompleted(void)
  5389. {
  5390. BOOL completed = TRUE;
  5391. GetClockTime(&_DispenserUpgrade_time);
  5392. do
  5393. {
  5394. completed = TRUE;
  5395. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5396. {
  5397. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest ||
  5398. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm)
  5399. {
  5400. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted)
  5401. {
  5402. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest = false;
  5403. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm = false;
  5404. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted = false;
  5405. LOG_INFO("Dispenser %d Upgrade Success", i + 1);
  5406. }
  5407. else
  5408. {
  5409. completed = FALSE;
  5410. }
  5411. }
  5412. }
  5413. if(GetTimeoutValue(_DispenserUpgrade_time) / uSEC_VAL >= DISPENSER_UPGRADE_TIME)
  5414. {
  5415. LOG_INFO("Wait Upgrade Timeout");
  5416. completed = FALSE;
  5417. break;
  5418. }
  5419. if(completed == FALSE)
  5420. {
  5421. sleep(1);
  5422. }
  5423. }while(!completed);
  5424. return completed;
  5425. }
  5426. void UpgradePrepare(void)
  5427. {
  5428. int count = 0;
  5429. SetAcContactor(OFF);
  5430. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5431. {
  5432. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5433. {
  5434. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5435. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5436. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5437. {
  5438. count++;
  5439. }
  5440. }
  5441. }
  5442. if(count > 0)
  5443. {
  5444. sleep(3);
  5445. }
  5446. else
  5447. {
  5448. sleep(1);
  5449. }
  5450. }
  5451. void TriggerPCabinetUpgrade(unsigned char index, char *FileName)
  5452. {
  5453. memset(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, 0x00, 128);
  5454. memcpy(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, FileName, strlen(FileName));
  5455. ShmChargerInfo->ParallelCabinet.PCabinet[index].UpdateRequest = YES;
  5456. LOG_INFO("Trigger PCabinet %d to update image [%s]", index + 1, FileName);
  5457. }
  5458. BOOL PutImageToAllParallelCabinet(char *SourcePath, char *FileName)
  5459. {
  5460. int isPass = FAIL;
  5461. BOOL transferred = FALSE;
  5462. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5463. {
  5464. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5465. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5466. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5467. {
  5468. int channel = ShmChargerInfo->ParallelCabinet.PCabinet[i].SocketChannel;
  5469. if(ShmChargerInfo->CabinetConnInfo[channel].Status == _SCONN_MATCHED &&
  5470. ShmChargerInfo->ParallelCabinet.PCUpdateState[i] == Not_Update)
  5471. {
  5472. char PCabinetIpAddress[16];
  5473. memset((char*)PCabinetIpAddress, 0x00, 16);
  5474. sprintf((char*)PCabinetIpAddress, "%d.%d.%d.%d",
  5475. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 0) & 0xFF),
  5476. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 8) & 0xFF),
  5477. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 16) & 0xFF),
  5478. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 24) & 0xFF));
  5479. isPass = TransferImageViaFTP(PCabinetIpAddress, SourcePath);
  5480. if(isPass == PASS)
  5481. {
  5482. TriggerPCabinetUpgrade(i, FileName);
  5483. }
  5484. transferred = true;
  5485. }
  5486. }
  5487. }
  5488. return transferred;
  5489. }
  5490. BOOL WaitAllPCabinetStartUpgrade(void)
  5491. {
  5492. BOOL isStart = true;
  5493. int count = 0;
  5494. struct timespec _waitStart;
  5495. GetClockTime(&_waitStart);
  5496. do
  5497. {
  5498. isStart = true;
  5499. count = 0;
  5500. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5501. {
  5502. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5503. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5504. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5505. {
  5506. count++;
  5507. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updating)
  5508. {
  5509. isStart = false;
  5510. }
  5511. }
  5512. }
  5513. if((GetTimeoutValue(_waitStart) / uSEC_VAL) >= START_UPGRADE_TIME)
  5514. {
  5515. isStart = false;
  5516. break;
  5517. }
  5518. usleep(10000);
  5519. }while(!isStart);
  5520. if(isStart && count > 0)
  5521. {
  5522. LOG_INFO("Total %d PCabinet upgrade start...", count);
  5523. }
  5524. else
  5525. {
  5526. if(count > 0)
  5527. {
  5528. LOG_INFO("Wait PCabinet upgrade timeout");
  5529. }
  5530. else
  5531. {
  5532. LOG_INFO("No PCabinet need to upgrade");
  5533. }
  5534. }
  5535. return isStart;
  5536. }
  5537. BOOL WaitAllPCabinetUpgradeCompleted(void)
  5538. {
  5539. BOOL completed = true;
  5540. int count = 0;
  5541. struct timespec _waitCompleted;
  5542. GetClockTime(&_waitCompleted);
  5543. do
  5544. {
  5545. completed = true;
  5546. count = 0;
  5547. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5548. {
  5549. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5550. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5551. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5552. {
  5553. count++;
  5554. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updated)
  5555. {
  5556. completed = false;
  5557. }
  5558. }
  5559. }
  5560. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= PCABINET_UPGRADE_TIME)
  5561. {
  5562. completed = false;
  5563. break;
  5564. }
  5565. usleep(10000);
  5566. }while(!completed);
  5567. if(completed && count > 0)
  5568. {
  5569. LOG_INFO("Total %d PCabinet upgrade completed!", count);
  5570. }
  5571. else
  5572. {
  5573. if(count > 0)
  5574. {
  5575. LOG_INFO("Wait PCabinet upgrade completed timeout");
  5576. }
  5577. else
  5578. {
  5579. LOG_INFO("Need not to wait PCabinet completed");
  5580. }
  5581. }
  5582. return completed;
  5583. }
  5584. BOOL WaitMCabinetUpgradeCompleted(void)
  5585. {
  5586. BOOL completed = true;
  5587. struct timespec _waitCompleted;
  5588. GetClockTime(&_waitCompleted);
  5589. do
  5590. {
  5591. completed = false;
  5592. if(ShmChargerInfo->SCabinetControl.NeedUpgrade == _R_NoUpgrade)
  5593. {
  5594. completed = true;
  5595. }
  5596. if(ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Idle &&
  5597. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Alarm &&
  5598. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Charging)
  5599. {
  5600. completed = false;
  5601. break;
  5602. }
  5603. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= MCABINET_UPGRADE_TIME)
  5604. {
  5605. completed = false;
  5606. break;
  5607. }
  5608. usleep(10000);
  5609. }while(!completed);
  5610. if(completed)
  5611. {
  5612. LOG_INFO("MCabinet upgrade completed!");
  5613. }
  5614. else
  5615. {
  5616. LOG_INFO("Wait MCabinet upgrade timeout or disconnected...");
  5617. }
  5618. return completed;
  5619. }
  5620. bool IsModelNameMatch(char *model)
  5621. {
  5622. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == model[0] &&
  5623. ShmSysConfigAndInfo->SysConfig.ModelName[1] == model[1] &&
  5624. ShmSysConfigAndInfo->SysConfig.ModelName[7] == model[7] &&
  5625. ShmSysConfigAndInfo->SysConfig.ModelName[8] == model[8] &&
  5626. ShmSysConfigAndInfo->SysConfig.ModelName[9] == model[9] &&
  5627. ShmSysConfigAndInfo->SysConfig.ModelName[11] == model[11] &&
  5628. ShmSysConfigAndInfo->SysConfig.ModelName[12] == model[12] &&
  5629. ShmSysConfigAndInfo->SysConfig.ModelName[13] == model[13])
  5630. {
  5631. return true;
  5632. }
  5633. return false;
  5634. }
  5635. unsigned long long getAvailableMemory(void)
  5636. {
  5637. long pages = sysconf(_SC_AVPHYS_PAGES);
  5638. long page_size = sysconf(_SC_PAGE_SIZE);
  5639. return pages * page_size;
  5640. }
  5641. void ImageTypeParsing(unsigned int type, char *type_str)
  5642. {
  5643. switch(type)
  5644. {
  5645. case 0x10000001:
  5646. sprintf(type_str, "Image_uboot");
  5647. break;
  5648. case 0x10000002:
  5649. sprintf(type_str, "Image_dtb");
  5650. break;
  5651. case 0x10000003:
  5652. sprintf(type_str, "Image_zImage");
  5653. break;
  5654. case 0x10000004:
  5655. sprintf(type_str, "Image_ramdisk");
  5656. break;
  5657. case 0x10000005:
  5658. sprintf(type_str, "Image_Factory");
  5659. break;
  5660. case 0x10000006:
  5661. sprintf(type_str, "Image_Primary");
  5662. break;
  5663. case 0x10000007:
  5664. sprintf(type_str, "Image_CCS_uboot");
  5665. break;
  5666. case 0x10000008:
  5667. sprintf(type_str, "Image_CCS_dtb");
  5668. break;
  5669. case 0x10000009:
  5670. sprintf(type_str, "Image_CCS_zImage");
  5671. break;
  5672. case 0x1000000A:
  5673. sprintf(type_str, "Image_CCS_ramdisk");
  5674. break;
  5675. case 0x1000000B:
  5676. sprintf(type_str, "Image_CAHdeMO");
  5677. break;
  5678. case 0x1000000C:
  5679. sprintf(type_str, "Image_GBT");
  5680. break;
  5681. case 0x1000000D:
  5682. sprintf(type_str, "Image_Relay");
  5683. break;
  5684. case 0x1000000E:
  5685. sprintf(type_str, "Image_Fan");
  5686. break;
  5687. case 0x1000000F:
  5688. sprintf(type_str, "Image_LCM");
  5689. break;
  5690. case 0x10000010:
  5691. sprintf(type_str, "Image_Infy_PFC");
  5692. break;
  5693. case 0x10000011:
  5694. sprintf(type_str, "Image_Infy_DD");
  5695. break;
  5696. case 0x10000014:
  5697. sprintf(type_str, "Image_LED");
  5698. break;
  5699. case 0x10000015:
  5700. sprintf(type_str, "Image_LCM_APK");
  5701. break;
  5702. default:
  5703. sprintf(type_str, "Image_Unknown");
  5704. break;
  5705. }
  5706. }
  5707. char CheckUpdateProcess()
  5708. {
  5709. char _model[32];
  5710. char _type[32];
  5711. bool support = false;
  5712. bool OneImageOnly = false;
  5713. int isPass = true;
  5714. uint8_t retSucc = 0;
  5715. uint8_t retFail = 0;
  5716. byte target = 0;
  5717. char *new_str = NULL;
  5718. unsigned char *ptr = NULL;
  5719. int fd = 0;
  5720. int uartFd = 0;
  5721. unsigned int Type = 0;
  5722. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  5723. DIR *d;
  5724. struct dirent *dir;
  5725. char cmdBuf[128];
  5726. int availableMem = 0;
  5727. memset(_model, 0x00, sizeof(_model));
  5728. memset(_type, 0x00, sizeof(_type));
  5729. availableMem = getAvailableMemory() / (1024 * 1024);
  5730. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5731. // If available memory too small, free memory cache first
  5732. if(availableMem < MIN_AVAILABLE_MEMORY)
  5733. {
  5734. LOG_INFO("Available memory less than %d MBytes, free cache & kill ntpd.", MIN_AVAILABLE_MEMORY);
  5735. system("echo 3 > /proc/sys/vm/drop_caches");
  5736. KillNtpd();
  5737. availableMem = getAvailableMemory() / (1024 * 1024);
  5738. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5739. }
  5740. d = opendir("/mnt/");
  5741. if(d)
  5742. {
  5743. while((dir = readdir(d)) != NULL && !OneImageOnly)
  5744. {
  5745. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  5746. continue;
  5747. }
  5748. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  5749. strcat(new_str, "/mnt/");
  5750. strcat(new_str, dir->d_name);
  5751. fd = open(new_str, O_RDONLY);
  5752. if(fd < 0)
  5753. {
  5754. close(fd);
  5755. free(new_str);
  5756. continue;
  5757. }
  5758. LOG_INFO("%s\n", new_str);
  5759. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  5760. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  5761. //get the image length
  5762. ImageLen = read(fd, ptr, MaxLen);
  5763. if(ImageLen > 20)
  5764. {
  5765. memcpy(_model, ptr, 16);
  5766. Type = (((unsigned int)ptr[16]) << 24 | ((unsigned int)ptr[17]) << 16 | ((unsigned int)ptr[18]) << 8 | ((unsigned int)ptr[19]));
  5767. ImageTypeParsing(Type, _type);
  5768. LOG_INFO("Model: %s, Type: %s", _model, _type);
  5769. }
  5770. free(ptr);
  5771. if (ImageLen > 20)
  5772. {
  5773. support = false;
  5774. availableMem = getAvailableMemory() / (1024 * 1024);
  5775. LOG_INFO("AvailableMemory: %dMByte", availableMem);
  5776. //if(strcmp(model, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == EQUAL)
  5777. if(IsModelNameMatch(_model))
  5778. {
  5779. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5780. {
  5781. PutImageToAllParallelCabinet(new_str, dir->d_name);
  5782. WaitAllPCabinetStartUpgrade();
  5783. }
  5784. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.NeedUpgrade)
  5785. {
  5786. ShmChargerInfo->SCabinetControl.UpgradeState = Updating;
  5787. sleep(1);
  5788. }
  5789. switch (Type)
  5790. {
  5791. case 0x10000001:
  5792. case 0x10000002:
  5793. case 0x10000003:
  5794. case 0x10000004:
  5795. case 0x10000005:
  5796. support = true;
  5797. _UpgradeNeedReboot = TRUE;
  5798. LOG_INFO("Upgrade Flash Start...", Type);
  5799. isPass = Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5800. LOG_INFO("Upgrade Flash End, %s", isPass ? "Pass" : "Fail");
  5801. break;
  5802. case 0x10000006: // primary mcu
  5803. case 0x1000000D: // relay board
  5804. case 0x1000000E: // fan board
  5805. case 0x10000014: // led board
  5806. support = true;
  5807. target = 0x00;
  5808. if (Type == 0x10000006)
  5809. {
  5810. target = UPGRADE_PRI;
  5811. }
  5812. else if (Type == 0x1000000D)
  5813. {
  5814. target = UPGRADE_RB;
  5815. }
  5816. else if (Type == 0x1000000E)
  5817. {
  5818. target = UPGRADE_FAN;
  5819. }
  5820. else if (Type == 0x10000014)
  5821. {
  5822. target = UPGRADE_LED;
  5823. }
  5824. uartFd = InitComPort(target);
  5825. if(target == UPGRADE_RB)
  5826. {
  5827. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC1, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5828. if(isPass == PASS)
  5829. {
  5830. retSucc++;
  5831. }
  5832. else
  5833. {
  5834. retFail++;
  5835. }
  5836. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5837. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  5838. {
  5839. sleep(3);
  5840. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC2, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5841. }
  5842. }
  5843. else
  5844. {
  5845. isPass = Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5846. }
  5847. close(uartFd);
  5848. break;
  5849. }
  5850. if(support)
  5851. {
  5852. if(isPass == PASS)
  5853. {
  5854. retSucc++;
  5855. }
  5856. else
  5857. {
  5858. retFail++;
  5859. }
  5860. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5861. }
  5862. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5863. {
  5864. LOG_INFO("MCabinet upgrade completed");
  5865. WaitAllPCabinetUpgradeCompleted();
  5866. }
  5867. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.UpgradeState == Updating)
  5868. {
  5869. ShmChargerInfo->SCabinetControl.UpgradeState = Updated;
  5870. LOG_INFO("SCabinet upgrade completed");
  5871. WaitMCabinetUpgradeCompleted();
  5872. }
  5873. }
  5874. else
  5875. {
  5876. if(_model[0] == 'D' && (_model[1] == 'D' || _model[1] == 'X'))
  5877. {
  5878. OneImageOnly = true;
  5879. _UpgradeNeedReboot = TRUE;
  5880. LOG_INFO("Start Put Image To Dispenser");
  5881. PutImageToAllDispenser(new_str, dir->d_name, _model);
  5882. WaitAllDispenserUpgradeCompleted();
  5883. LOG_INFO("All Dispenser Upgrade Completed");
  5884. }
  5885. }
  5886. }
  5887. else
  5888. {
  5889. retFail++;
  5890. LOG_INFO("Model Name Is Not Match: %s", _model);
  5891. }
  5892. sprintf(cmdBuf, "rm -f %s", new_str);
  5893. system(cmdBuf);
  5894. close(fd);
  5895. free(new_str);
  5896. }
  5897. }
  5898. free(dir);
  5899. closedir(d);
  5900. if(retFail != 0)
  5901. {
  5902. return FAIL;
  5903. }
  5904. return PASS;
  5905. }
  5906. void TriggerDispenserConnectionTimeoutSetting(void)
  5907. {
  5908. BOOL trigger = FALSE;
  5909. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5910. {
  5911. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0 &&
  5912. !Is_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT))
  5913. {
  5914. trigger = TRUE;
  5915. Set_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT);
  5916. }
  5917. }
  5918. if(trigger)
  5919. {
  5920. LOG_INFO("********** Trigger Dispenser Connector Timeout Value: %d **********", ShmChargerInfo->CabinetMiscValue.ConnectionTimeout);
  5921. }
  5922. }
  5923. void CheckConnectionTimeout(void)
  5924. {
  5925. if(!Is_Ocpp_Task_Alive())
  5926. {
  5927. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5928. }
  5929. else
  5930. {
  5931. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  5932. {
  5933. _connectionTimeout = Get_Ocpp_ConnectionTimeOut();
  5934. if(_connectionTimeout <= 0)
  5935. {
  5936. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5937. }
  5938. }
  5939. else
  5940. {
  5941. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5942. }
  5943. }
  5944. if(_connectionTimeout != ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  5945. {
  5946. ShmChargerInfo->CabinetMiscValue.ConnectionTimeout = _connectionTimeout;
  5947. TriggerDispenserConnectionTimeoutSetting();
  5948. }
  5949. }
  5950. void CreateTimeoutFork()
  5951. {
  5952. pid_t timeoutPid;
  5953. timeoutPid = fork();
  5954. if (timeoutPid == 0)
  5955. {
  5956. GetClockTime(&_cmdSubPriority_time);
  5957. CheckConnectionTimeout();
  5958. LOG_INFO("Timeout Fork PID = %d", getpid());
  5959. while(true)
  5960. {
  5961. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  5962. {
  5963. CheckConnectionTimeout();
  5964. GetClockTime(&_cmdSubPriority_time);
  5965. }
  5966. sleep(1);
  5967. }
  5968. }
  5969. }
  5970. void CheckFactoryConfigFunction(void)
  5971. {
  5972. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  5973. {
  5974. char Buf[256];
  5975. sprintf(Buf, "cd /root;./FactoryConfig -m");
  5976. system(Buf);
  5977. system("rm -f /Storage/OCPP/OCPPConfiguration");
  5978. system("sync");
  5979. sleep(5);
  5980. system("reboot -f");
  5981. sleep(5);
  5982. system("reboot -f");
  5983. }
  5984. }
  5985. void CheckFwUpdateFunction(void)
  5986. {
  5987. char UpdateResult = PASS;
  5988. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  5989. {
  5990. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  5991. UpgradePrepare();
  5992. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  5993. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  5994. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  5995. LOG_INFO("ftp : update start.");
  5996. TryCloseWatchdog();
  5997. //KillTask();
  5998. system("killall Module_PrimaryComm");
  5999. system("killall Module_InternalComm");
  6000. UpdateResult = CheckUpdateProcess();
  6001. if (UpdateResult == PASS)
  6002. {
  6003. LOG_INFO("ftp : update complete.");
  6004. }
  6005. else
  6006. {
  6007. LOG_INFO("ftp : update fail.");
  6008. }
  6009. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  6010. sleep(10);
  6011. if(_UpgradeNeedReboot)
  6012. {
  6013. system("reboot -f");
  6014. }
  6015. else
  6016. {
  6017. KillAllTask();
  6018. system("/usr/bin/run_evse_restart.sh");
  6019. }
  6020. }
  6021. else if(Is_Ocpp_UpdateFirmwareReq())
  6022. {
  6023. Clean_Ocpp_UpdateFirmwareReq();
  6024. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  6025. UpgradePrepare();
  6026. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  6027. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  6028. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  6029. if(Is_Ocpp_UpdateFirmwareStatus("Downloaded"))
  6030. {
  6031. LOG_INFO("Backend : update start.");
  6032. TryCloseWatchdog();
  6033. Set_Ocpp_UpdateFirmwareStatus("Installing");
  6034. Set_Ocpp_FirmwareStatusNotificationReq();
  6035. //KillTask();
  6036. system("killall Module_PrimaryComm");
  6037. system("killall Module_InternalComm");
  6038. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6039. {
  6040. setChargerMode(_index, MODE_UPDATE);
  6041. }
  6042. UpdateResult = CheckUpdateProcess();
  6043. if (UpdateResult == PASS)
  6044. {
  6045. LOG_INFO("Backend : update complete.");
  6046. Set_Ocpp_UpdateFirmwareStatus("Installed");
  6047. }
  6048. else
  6049. {
  6050. LOG_INFO("Backend : update fail.");
  6051. Set_Ocpp_UpdateFirmwareStatus("InstallationFailed");
  6052. }
  6053. Set_Ocpp_FirmwareStatusNotificationReq();
  6054. sleep(10);
  6055. if(_UpgradeNeedReboot)
  6056. {
  6057. system("reboot -f");
  6058. }
  6059. else
  6060. {
  6061. KillAllTask();
  6062. system("/usr/bin/run_evse_restart.sh");
  6063. }
  6064. }
  6065. }
  6066. }
  6067. void ExecuteWriteFlash(void)
  6068. {
  6069. struct SysConfigData config;
  6070. memcpy(&config, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  6071. config.SwitchDebugFlag = NO;
  6072. strcpy((char *) config.UserId, "");
  6073. int result = StoreUsrConfigData(&config);
  6074. if(result != 1)
  6075. {
  6076. LOG_INFO("Store User Config NG");
  6077. }
  6078. else
  6079. {
  6080. LOG_INFO("Store User Config OK");
  6081. }
  6082. }
  6083. struct timespec _WiringInfo_time;
  6084. int CheckFlashUpdate(void)
  6085. {
  6086. if(ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged)
  6087. {
  6088. ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged = false;
  6089. LOG_INFO("Flash Config Changed");
  6090. GetClockTime(&_WiringInfo_time);
  6091. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = true;
  6092. }
  6093. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable)
  6094. {
  6095. // wait for 30s to write flash or write flash immediately
  6096. if((GetTimeoutValue(_WiringInfo_time) / uSEC_VAL) >= WIRING_INFO_DELAY ||
  6097. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashImmediately)
  6098. {
  6099. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = false;
  6100. LOG_INFO("Start Writing Flash");
  6101. pid_t writePid = fork();
  6102. if(writePid == 0)
  6103. {
  6104. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = true;
  6105. ExecuteWriteFlash();
  6106. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = false;
  6107. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset)
  6108. {
  6109. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset = false;
  6110. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  6111. LOG_INFO("Write Flash OK And Need Soft Reset");
  6112. }
  6113. return NO;
  6114. }
  6115. LOG_INFO("Write Flash Conpleted");
  6116. }
  6117. }
  6118. return YES;
  6119. }
  6120. void CheckSoftHardReset(void)
  6121. {
  6122. if(ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset)
  6123. {
  6124. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = false;
  6125. LOG_INFO("********** Need Soft Reset **********");
  6126. sleep(5);
  6127. Kill_Ocpp_Service();
  6128. KillAllTask();
  6129. TryCloseWatchdog();
  6130. system("/usr/bin/run_evse_restart.sh");
  6131. }
  6132. if(ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset)
  6133. {
  6134. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = false;
  6135. LOG_INFO("********** Need Hard Reset **********");
  6136. sleep(5);
  6137. system("reboot -f");
  6138. }
  6139. }
  6140. struct timespec _Reboot_time;
  6141. void TriggerDispenserHardwareReboot(void)
  6142. {
  6143. BOOL trigger = FALSE;
  6144. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6145. {
  6146. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6147. {
  6148. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm == 0 &&
  6149. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  6150. {
  6151. trigger = TRUE;
  6152. Set_Dispenser_MiscCommand(i, MISC_DISP_HARDWARE_REBOOT);
  6153. }
  6154. }
  6155. }
  6156. if(trigger)
  6157. {
  6158. GetClockTime(&_Reboot_time);
  6159. LOG_INFO("********** Trigger Dispenser Hardware Reboot **********");
  6160. }
  6161. }
  6162. BOOL IsDispenserHardwareRebootResponsed(void)
  6163. {
  6164. BOOL response_ok = TRUE;
  6165. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6166. {
  6167. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6168. {
  6169. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  6170. {
  6171. response_ok = FALSE;
  6172. // 10s for reboot request timeout
  6173. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  6174. {
  6175. response_ok = TRUE;
  6176. LOG_INFO("Reboot request timeout");
  6177. }
  6178. }
  6179. }
  6180. }
  6181. return response_ok;
  6182. }
  6183. struct timespec _Reset_time;
  6184. void TriggerDispenserSoftwareReset(void)
  6185. {
  6186. BOOL trigger = FALSE;
  6187. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6188. {
  6189. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6190. {
  6191. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  6192. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  6193. {
  6194. trigger = TRUE;
  6195. Set_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET);
  6196. }
  6197. }
  6198. }
  6199. if(trigger)
  6200. {
  6201. GetClockTime(&_Reset_time);
  6202. LOG_INFO("********** Trigger Dispenser Software Reset **********");
  6203. }
  6204. }
  6205. BOOL IsDispenserSoftwareResetResponsed(void)
  6206. {
  6207. BOOL response_ok = TRUE;
  6208. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6209. {
  6210. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6211. {
  6212. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  6213. {
  6214. response_ok = FALSE;
  6215. // 10s for reset request timeout
  6216. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  6217. {
  6218. response_ok = TRUE;
  6219. LOG_INFO("Reset request timeout");
  6220. }
  6221. }
  6222. }
  6223. }
  6224. return response_ok;
  6225. }
  6226. // return time offset
  6227. int TimeOffsetKeyParsing(char *StringItem)
  6228. {
  6229. int offset = 0;
  6230. int hour = 0, minute = 0, sign = 1;
  6231. char *ptrSave, *ptrToken;
  6232. char strSource[501];
  6233. memset(strSource, 0x00, sizeof(strSource));
  6234. strcpy(strSource, StringItem);
  6235. if(strSource[0] != '+' && strSource[0] != '-')
  6236. {
  6237. return offset;
  6238. }
  6239. sign = strSource[0] == '+' ? (1) : (-1);
  6240. ptrToken = strtok_r(&strSource[1], ":", &ptrSave);
  6241. if(ptrToken != NULL)
  6242. {
  6243. hour = atoi(ptrToken);
  6244. ptrToken = strtok_r(NULL, ":", &ptrSave);
  6245. if(ptrToken != NULL)
  6246. {
  6247. minute = atoi(ptrToken);
  6248. offset = ((hour * 60) + minute) * sign;
  6249. }
  6250. }
  6251. return offset;
  6252. }
  6253. char MyDefaultPriceString[501];
  6254. void DefaultPriceHandler(void)
  6255. {
  6256. char price[501];
  6257. char defaultPrice[512];
  6258. memset(defaultPrice, 0x00, sizeof(defaultPrice));
  6259. Get_Ocpp_DefaultPrice((char *)price);
  6260. if(strcmp(MyDefaultPriceString, (char *)price) != EQUAL)
  6261. {
  6262. float ConnectionFee = 0, OccupancyFee = 0;
  6263. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6264. {
  6265. ConnectionFee = PriceParsing(price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6266. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = PriceParsing(price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6267. OccupancyFee = PriceParsing(price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6268. LOG_INFO("******** Customer Audi Default Price *******");
  6269. LOG_INFO("********** Connection Fee: %4.2f **********", ConnectionFee);
  6270. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6271. LOG_INFO("********** Occupancy Fee: %4.2f **********", OccupancyFee);
  6272. }
  6273. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6274. {
  6275. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6276. ShellPriceParsing(price, defaultPrice);
  6277. LOG_INFO("******* Customer Shell Default Price *******");
  6278. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6279. }
  6280. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6281. {
  6282. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = TccDefaultPriceParsing(price, &ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6283. LOG_INFO("******* Customer Tcc Default Price *******");
  6284. LOG_INFO("*** TimeOfDayPricing: %4.2f", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6285. }
  6286. else
  6287. {
  6288. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6289. strcpy(defaultPrice, price);
  6290. LOG_INFO("********** Standard Default Price **********");
  6291. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6292. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6293. }
  6294. strcpy(MyDefaultPriceString, price);
  6295. }
  6296. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6297. {
  6298. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = GetNowTimePricing(&ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6299. }
  6300. if(ShmChargerInfo->CabinetMiscValue.DefaultPrice != ShmChargerInfo->PriceAndReceiptInfo.CurrentRate)
  6301. {
  6302. ShmChargerInfo->CabinetMiscValue.DefaultPrice = ShmChargerInfo->PriceAndReceiptInfo.CurrentRate;
  6303. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6304. {
  6305. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6306. {
  6307. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE))
  6308. {
  6309. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE);
  6310. }
  6311. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY))
  6312. {
  6313. Set_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY);
  6314. }
  6315. }
  6316. }
  6317. LOG_INFO("********** Trigger Dispenser Default Price Value **********");
  6318. }
  6319. if(strlen(defaultPrice) > 0)
  6320. {
  6321. strcpy(ShmChargerInfo->PriceAndReceiptInfo.DefaultPriceString, defaultPrice);
  6322. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6323. {
  6324. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6325. {
  6326. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING))
  6327. {
  6328. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING);
  6329. }
  6330. }
  6331. }
  6332. LOG_INFO("********** Trigger Dispenser Default Price Value String **********");
  6333. }
  6334. }
  6335. void UserPriceHandler(void)
  6336. {
  6337. char UserId[32];
  6338. char price[500];
  6339. char userPrice[512];
  6340. memset(userPrice, 0x00, sizeof(userPrice));
  6341. Get_Ocpp_UserPrice(UserId, price);
  6342. if(strlen((char *)UserId) > 0 && strlen((char *)price) > 0)
  6343. {
  6344. float ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  6345. float AccountBalance = 0;
  6346. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6347. {
  6348. ConnectionFee = PriceParsing((char *)price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6349. CurrentRate = PriceParsing((char *)price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6350. OccupancyFee = PriceParsing((char *)price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6351. AccountBalance = PriceParsing((char *)price, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6352. LOG_INFO("********* Customer Audi User Price *********");
  6353. LOG_INFO("********* User Id: [%s]", UserId);
  6354. LOG_INFO("********* Connection Fee: [%7.2f]", ConnectionFee);
  6355. LOG_INFO("********* Current Rate: [%7.2f]", CurrentRate);
  6356. LOG_INFO("********* Occupancy Fee: [%7.2f]", OccupancyFee);
  6357. LOG_INFO("********* Account Balance: [%7.2f]", AccountBalance);
  6358. }
  6359. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6360. {
  6361. ShellPriceParsing(price, userPrice);
  6362. LOG_INFO("******** Customer Shell User Price *********");
  6363. LOG_INFO("******** User Id: [%s]", UserId);
  6364. LOG_INFO("******** User Price: [%s]", userPrice);
  6365. }
  6366. else
  6367. {
  6368. CurrentRate = PriceParsing((char *)price, "chargineFee", _PHIHONG_PRICE_SPLIT_KEY);
  6369. OccupancyFee = PriceParsing((char *)price, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6370. strcpy(userPrice, price);
  6371. LOG_INFO("*********** Standard User Price ************");
  6372. LOG_INFO("*********** User Id: [%s]", UserId);
  6373. LOG_INFO("*********** Current Rate: [%7.2f]", CurrentRate);
  6374. LOG_INFO("*********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6375. LOG_INFO("*********** User Price: [%s]", userPrice);
  6376. }
  6377. #if 0
  6378. LOG_INFO("********** User Id: %s **********", UserId);
  6379. LOG_INFO("********** User Price: %s", price);
  6380. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6381. LOG_INFO("********** Current Rate: %7.2f **********", CurrentRate);
  6382. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6383. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6384. #endif
  6385. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6386. {
  6387. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  6388. {
  6389. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  6390. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  6391. {
  6392. LOG_INFO("********** Connector Id %d User Price %4.2f, Account Balance %7.2f **********",
  6393. (i + 1), CurrentRate, AccountBalance);
  6394. }
  6395. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  6396. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  6397. if(strlen(userPrice) > 0)
  6398. {
  6399. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[i][0], userPrice);
  6400. Set_Connector_MiscCommand(i, MISC_CONN_USER_PRICE_STRING);
  6401. }
  6402. if(chargingInfo[i]->SystemStatus == S_AUTHORIZING)
  6403. {
  6404. AnnounceAccountBalance(i, AccountBalance);
  6405. }
  6406. }
  6407. }
  6408. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6409. {
  6410. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthResult == _AuthResult_Valid)
  6411. {
  6412. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthId, UserId) == EQUAL)
  6413. {
  6414. if(strlen(userPrice) > 0)
  6415. {
  6416. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[i][0], userPrice);
  6417. }
  6418. }
  6419. }
  6420. }
  6421. Clean_Ocpp_UserPrice();
  6422. }
  6423. }
  6424. void RunningCostParsing(int gunIndex, char *strCost)
  6425. {
  6426. float ConnectionFee = 0, SessionFee = 0, OccupancyFee = -1, TotalCost = 0, EnergyCost = -1;
  6427. float AccountBalance = 0;
  6428. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6429. {
  6430. ConnectionFee = PriceParsing((char *)strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6431. SessionFee = PriceParsing((char *)strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6432. OccupancyFee = PriceParsing((char *)strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6433. TotalCost = PriceParsing((char *)strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6434. AccountBalance = PriceParsing((char *)strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6435. LOG_INFO("******** Customer Audi Gun %d Running Cost ********", (gunIndex + 1));
  6436. LOG_INFO("******** Connection Fee: [%7.2f]", ConnectionFee);
  6437. LOG_INFO("******** Session Fee: [%7.2f]", SessionFee);
  6438. LOG_INFO("******** Occupancy Fee: [%7.2f]", OccupancyFee);
  6439. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6440. LOG_INFO("******** Account Balance: [%7.2f]", AccountBalance);
  6441. }
  6442. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6443. {
  6444. TotalCost = ShellRunningCostParsing(strCost);
  6445. LOG_INFO("******** Customer Shell Gun %d Running Cost *******", (gunIndex + 1));
  6446. LOG_INFO("******** Gun %d Running Cost", (gunIndex + 1));
  6447. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6448. }
  6449. else
  6450. {
  6451. EnergyCost = PriceParsing((char *)strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6452. TotalCost = EnergyCost;
  6453. OccupancyFee = PriceParsing((char *)strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6454. LOG_INFO("********** Standard Gun %d Running Cost ***********", (gunIndex + 1));
  6455. LOG_INFO("********** Energy Cost: [%7.2f]", EnergyCost);
  6456. LOG_INFO("********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6457. LOG_INFO("********** Total Cost: [%7.2f]", TotalCost);
  6458. }
  6459. #if 0
  6460. LOG_INFO("********** Connector Id: %d Running Cost **********", (gunIndex + 1));
  6461. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6462. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6463. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6464. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6465. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6466. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6467. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance)
  6468. {
  6469. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance %7.2f **********",
  6470. (gunIndex + 1), TotalCost, AccountBalance);
  6471. }
  6472. #endif
  6473. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = TotalCost;
  6474. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = AccountBalance;
  6475. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = EnergyCost;
  6476. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = OccupancyFee;
  6477. }
  6478. void RunningCostHandler(void)
  6479. {
  6480. char strCost[256];
  6481. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6482. {
  6483. Get_Ocpp_RunningCost(i, strCost);
  6484. if(strlen((char *)strCost) > 0)
  6485. {
  6486. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  6487. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_CHARGING)
  6488. {
  6489. RunningCostParsing(i, strCost);
  6490. }
  6491. Clean_Ocpp_RunningCost(i);
  6492. }
  6493. }
  6494. }
  6495. void EnableConnectorBillRequest(int gunIndex, ChargingBillInfoData *bill)
  6496. {
  6497. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6498. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6499. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = bill->TxId;
  6500. memcpy(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, &bill->FeeInfo, sizeof(FeeInfoData));
  6501. }
  6502. void DisableConnectorBill(int gunIndex)
  6503. {
  6504. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Idle;
  6505. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6506. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = 0;
  6507. memset(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, 0x00, sizeof(FeeInfoData));
  6508. }
  6509. void FinalCostParsing(int txId, char *strCost, ChargingBillInfoData *_bill)
  6510. {
  6511. memset(_bill, 0x00, sizeof(ChargingBillInfoData));
  6512. _bill->TxId = txId;
  6513. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6514. {
  6515. _bill->FeeInfo.ConnectionFee = PriceParsing(strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6516. _bill->FeeInfo.SessionFee = PriceParsing(strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6517. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6518. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6519. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6520. }
  6521. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6522. {
  6523. _bill->FeeInfo.TotalCost = ShellFinalCostParsing(strCost);
  6524. GetShellReceiptInfo(strCost, (char *)_bill->Receipt);
  6525. }
  6526. else
  6527. {
  6528. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6529. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6530. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "accountBalance", _PHIHONG_PRICE_SPLIT_KEY);
  6531. _bill->FeeInfo.CostDiscount = PriceParsing(strCost, "couponPoint", _PHIHONG_PRICE_SPLIT_KEY);
  6532. _bill->FeeInfo.RemainAmount = PriceParsing(strCost, "farewellMessage", _PHIHONG_PRICE_SPLIT_KEY);
  6533. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0)
  6534. {
  6535. sprintf(_bill->Receipt, "%s%d",
  6536. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0],
  6537. txId);
  6538. }
  6539. }
  6540. }
  6541. void UpdateCurrentFinalCost(int gunIndex, ChargingBillInfoData *_bill)
  6542. {
  6543. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6544. {
  6545. LOG_INFO("********* Customer Audi Gun %d Final Cost *********", (gunIndex + 1));
  6546. LOG_INFO("********* Connection Fee: [%7.2f]", _bill->FeeInfo.ConnectionFee);
  6547. LOG_INFO("********* Session Fee: [%7.2f]", _bill->FeeInfo.SessionFee);
  6548. LOG_INFO("********* Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6549. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6550. LOG_INFO("********* Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6551. }
  6552. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6553. {
  6554. LOG_INFO("********* Customer Shell Gun %d Final Cost ********", (gunIndex + 1));
  6555. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6556. LOG_INFO("********* Receipt: [%s]", _bill->Receipt);
  6557. }
  6558. else
  6559. {
  6560. LOG_INFO("*********** Standard Gun %d Final Cost ************", (gunIndex + 1));
  6561. LOG_INFO("*********** Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6562. LOG_INFO("*********** Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6563. LOG_INFO("*********** Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6564. LOG_INFO("*********** Coupon Point: [%7.2f]", _bill->FeeInfo.CostDiscount);
  6565. LOG_INFO("*********** Remain Amount: [%7.2f]", _bill->FeeInfo.RemainAmount);
  6566. LOG_INFO("*********** Receipt: [%s]", _bill->Receipt);
  6567. }
  6568. #if 0
  6569. LOG_INFO("********** Connector Id: %d Final Cost **********", (gunIndex + 1));
  6570. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6571. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6572. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6573. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6574. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6575. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6576. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance ||
  6577. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount != CostDiscount)
  6578. {
  6579. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance: %7.2f, Discount: %d.%02d **********",
  6580. (gunIndex + 1), TotalCost, AccountBalance, (CostDiscount / 100), (CostDiscount % 100));
  6581. }
  6582. #endif
  6583. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = _bill->FeeInfo.TotalCost;
  6584. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = _bill->FeeInfo.AccountBalance;
  6585. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount = _bill->FeeInfo.CostDiscount;
  6586. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = _bill->FeeInfo.EnergyCost;
  6587. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = _bill->FeeInfo.ParkingFee;
  6588. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemainAmount = _bill->FeeInfo.RemainAmount;
  6589. Set_Connector_MiscCommand(gunIndex, MISC_CONN_FINAL_COST);
  6590. if(strlen(_bill->Receipt) > 0)
  6591. {
  6592. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], _bill->Receipt);
  6593. Set_Connector_MiscCommand(gunIndex, MISC_CONN_RECEIPT);
  6594. }
  6595. DB_Update_FinalCost_Status(localDb, _bill->TxId, BILL_SATAUS_Bypass_OK);
  6596. }
  6597. bool IsBillStatusChanged(int gunIndex)
  6598. {
  6599. return ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus == ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus ?
  6600. false : true;
  6601. }
  6602. void SyncBillStatus(int gunIndex)
  6603. {
  6604. ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6605. }
  6606. void EnableUndisposedBill(int dispenserIndex)
  6607. {
  6608. LOG_INFO("Enable Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6609. _isUndisposedFinalCost[dispenserIndex] = true;
  6610. }
  6611. void FindUndisposedAndTrigger(int dispenserIndex)
  6612. {
  6613. int txId = 0;
  6614. char strCost[256];
  6615. ChargingBillInfoData _bill;
  6616. bool find = false;
  6617. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6618. {
  6619. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6620. txId = DB_Get_Undisposed_FinalCost(localDb, gun, strCost);
  6621. if(txId >= 0)
  6622. {
  6623. LOG_INFO("Find Gun %d Undisposed ChargingBill, txId: [%d]", gun + 1, txId);
  6624. FinalCostParsing(txId, (char *)strCost, &_bill);
  6625. EnableConnectorBillRequest(gun, &_bill);
  6626. find = true;
  6627. break;
  6628. }
  6629. }
  6630. if(!find)
  6631. {
  6632. LOG_INFO("Dispenser %d Undisposed ChargingBill Not Found", dispenserIndex + 1);
  6633. _isUndisposedFinalCost[dispenserIndex] = false;
  6634. }
  6635. }
  6636. bool IsDispenserBillStatusBusy(int dispenserIndex)
  6637. {
  6638. bool busy = false;
  6639. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6640. {
  6641. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6642. if(ShmChargerInfo->AllBill.GunBill[gun].BillStatus != _BillStatus_Idle)
  6643. {
  6644. busy = true;
  6645. break;
  6646. }
  6647. }
  6648. return busy;
  6649. }
  6650. void DispenserBillRequestProcess(int dispenserIndex)
  6651. {
  6652. if(IsDispenserIdle(dispenserIndex) && !IsDispenserBillStatusBusy(dispenserIndex))
  6653. {
  6654. if(!_canUpdateFinalCost[dispenserIndex])
  6655. {
  6656. _canUpdateFinalCost[dispenserIndex] = true;
  6657. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6658. }
  6659. if(_canUpdateFinalCost[dispenserIndex] && _isUndisposedFinalCost[dispenserIndex])
  6660. {
  6661. if(GetSecTimeoutValue(_DispenserBill_Time[dispenserIndex]) >= FINAL_COST_UPDATE_TIME)
  6662. {
  6663. //LOG_INFO("Try Find Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6664. FindUndisposedAndTrigger(dispenserIndex);
  6665. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6666. }
  6667. }
  6668. }
  6669. else
  6670. {
  6671. _canUpdateFinalCost[dispenserIndex] = false;
  6672. }
  6673. }
  6674. void GunBillRequestProcess(int gunIndex)
  6675. {
  6676. unsigned char _status = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6677. switch(_status)
  6678. {
  6679. case _BillStatus_Idle:
  6680. if(IsBillStatusChanged(gunIndex))
  6681. {
  6682. SyncBillStatus(gunIndex);
  6683. }
  6684. break;
  6685. case _BillStatus_MiscReq:
  6686. if(IsBillStatusChanged(gunIndex))
  6687. {
  6688. Database_INFO("Gun %d ChargingBill MiscRequest", gunIndex + 1);
  6689. SyncBillStatus(gunIndex);
  6690. }
  6691. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_WaitMiscClean;
  6692. Set_Connector_MiscCommand(gunIndex, MISC_CONN_CHARGING_BILL);
  6693. break;
  6694. case _BillStatus_WaitMiscClean:
  6695. if(IsBillStatusChanged(gunIndex))
  6696. {
  6697. SyncBillStatus(gunIndex);
  6698. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6699. }
  6700. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6701. {
  6702. Database_INFO("Gun %d ChargingBill WaitMisc Timeout", gunIndex + 1);
  6703. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6704. }
  6705. break;
  6706. case _BillStatus_MiscClean:
  6707. if(IsBillStatusChanged(gunIndex))
  6708. {
  6709. Database_INFO("Gun %d ChargingBill MiscClean", gunIndex + 1);
  6710. SyncBillStatus(gunIndex);
  6711. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6712. }
  6713. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6714. {
  6715. Database_INFO("Gun %d ChargingBill Wait Dispenser Timeout", gunIndex + 1);
  6716. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt++;
  6717. if(ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt >= MAX_FINAL_COST_MISC_COUNT)
  6718. {
  6719. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6720. }
  6721. else
  6722. {
  6723. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6724. }
  6725. }
  6726. break;
  6727. case _BillStatus_UpdateDone:
  6728. if(IsBillStatusChanged(gunIndex))
  6729. {
  6730. Database_INFO("Gun %d ChargingBill UpdateDone", gunIndex + 1);
  6731. SyncBillStatus(gunIndex);
  6732. }
  6733. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Completed;
  6734. break;
  6735. case _BillStatus_Completed:
  6736. if(IsBillStatusChanged(gunIndex))
  6737. {
  6738. SyncBillStatus(gunIndex);
  6739. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6740. }
  6741. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6742. {
  6743. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_End;
  6744. }
  6745. break;
  6746. case _BillStatus_Fail:
  6747. if(IsBillStatusChanged(gunIndex))
  6748. {
  6749. SyncBillStatus(gunIndex);
  6750. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_FAIL);
  6751. }
  6752. DisableConnectorBill(gunIndex);
  6753. break;
  6754. case _BillStatus_End:
  6755. if(IsBillStatusChanged(gunIndex))
  6756. {
  6757. SyncBillStatus(gunIndex);
  6758. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_OK);
  6759. }
  6760. DisableConnectorBill(gunIndex);
  6761. break;
  6762. default:
  6763. DisableConnectorBill(gunIndex);
  6764. break;
  6765. }
  6766. }
  6767. void FinalCostHandler(void)
  6768. {
  6769. int txId = 0;
  6770. char strCost[256];
  6771. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6772. {
  6773. txId = Get_Ocpp_FinalCostTxId(i);
  6774. Get_Ocpp_FinalCost(i, strCost);
  6775. if(strlen((char *)strCost) > 0)
  6776. {
  6777. DB_Insert_FinalCost(localDb, i, txId, strCost);
  6778. Database_INFO("Insert FinalCost: Gun %d TxId[%d]", i + 1, txId);
  6779. if(ShmChargerInfo->UserTransaction[i].TransactionId == txId)
  6780. {
  6781. ChargingBillInfoData _bill;
  6782. FinalCostParsing(txId, (char *)strCost, &_bill);
  6783. UpdateCurrentFinalCost(i, &_bill);
  6784. }
  6785. else
  6786. {
  6787. EnableUndisposedBill(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex);
  6788. }
  6789. Clean_Ocpp_FinalCost(i);
  6790. }
  6791. }
  6792. if(ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill)
  6793. {
  6794. ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill = false;
  6795. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6796. {
  6797. _isUndisposedFinalCost[i] = true;
  6798. }
  6799. }
  6800. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6801. {
  6802. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  6803. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  6804. {
  6805. DispenserBillRequestProcess(i);
  6806. }
  6807. else
  6808. {
  6809. _canUpdateFinalCost[i] = false;
  6810. }
  6811. }
  6812. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6813. {
  6814. GunBillRequestProcess(i);
  6815. }
  6816. }
  6817. char MyTimeOffsetString[501];
  6818. void TimeOffsetHandler(void)
  6819. {
  6820. char timeOffset[501];
  6821. Get_Ocpp_TimeOffset((char *)timeOffset);
  6822. if(strcmp(MyTimeOffsetString, (char *)timeOffset) != EQUAL)
  6823. {
  6824. BOOL trigger = FALSE;
  6825. int offset = 0;
  6826. offset = TimeOffsetKeyParsing(timeOffset);
  6827. LOG_INFO("********** Time Offset String [%s] -> Value [%d] -> Time [%s%02d:%02d]",
  6828. timeOffset, offset, offset >= 0 ? "+" : "-", (abs(offset) / 60), (abs(offset) % 60));
  6829. ShmChargerInfo->CabinetMiscValue.TimeOffset = offset;
  6830. #if 0
  6831. time_t timep;
  6832. struct tm *tm;
  6833. time(&timep);
  6834. tm = localtime(&timep);
  6835. LOG_INFO(" NowTime: %d/%02d/%02d %02d:%02d:%02d",
  6836. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  6837. timep += (offset * 60);
  6838. tm = localtime(&timep);
  6839. LOG_INFO("OffsetTime: %d/%02d/%02d %02d:%02d:%02d",
  6840. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  6841. #endif
  6842. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6843. {
  6844. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6845. {
  6846. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET))
  6847. {
  6848. trigger = TRUE;
  6849. Set_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET);
  6850. }
  6851. }
  6852. }
  6853. if(trigger)
  6854. {
  6855. LOG_INFO("********** Trigger Dispenser Time Offset **********");
  6856. }
  6857. strcpy(MyTimeOffsetString, timeOffset);
  6858. }
  6859. }
  6860. struct timespec _Cost_Price_time;
  6861. void CheckOcppCostAndPrice(void)
  6862. {
  6863. // 5s for reboot request timeout
  6864. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  6865. {
  6866. GetClockTime(&_Cost_Price_time);
  6867. DefaultPriceHandler();
  6868. UserPriceHandler();
  6869. RunningCostHandler();
  6870. TimeOffsetHandler();
  6871. }
  6872. FinalCostHandler();
  6873. }
  6874. void CheckRemoteStartNoIdRequest(void)
  6875. {
  6876. if(Is_Ocpp_RemoteStartWaitReq())
  6877. {
  6878. if(!ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6879. {
  6880. LOG_INFO("Ocpp remote start without connector id is trigger");
  6881. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = true;
  6882. AnnounceRemoteStartNoID();
  6883. GetClockTime(&_RemoteStartNoId_Time);
  6884. }
  6885. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6886. {
  6887. // to avoid keep RemoteStartNoId state when ocpp is disconnected
  6888. if((GetTimeoutValue(_RemoteStartNoId_Time) / uSEC_VAL) >= (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + CONNECTION_TIME_OFFSET))
  6889. {
  6890. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  6891. }
  6892. }
  6893. }
  6894. else
  6895. {
  6896. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6897. {
  6898. LOG_INFO("Ocpp remote start without connector id is expired");
  6899. }
  6900. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  6901. }
  6902. }
  6903. void CheckOcppPageChangeRequest(void)
  6904. {
  6905. int gunId = 0, pageIndex = 0;
  6906. if(Is_Ocpp_LcdOverideReq(&gunId, &pageIndex))
  6907. {
  6908. Clean_Ocpp_LcdOverideReq();
  6909. LOG_INFO("Gun %d Ocpp Line Status [%d] is trigger", gunId, pageIndex);
  6910. AnnounceLineStatusCode(gunId - 1, pageIndex);
  6911. }
  6912. }
  6913. void CheckLcmPageChangeRequest(void)
  6914. {
  6915. bool isTrigger = false;
  6916. CheckRemoteStartNoIdRequest();
  6917. CheckOcppPageChangeRequest();
  6918. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  6919. {
  6920. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  6921. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  6922. {
  6923. isTrigger = false;
  6924. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  6925. {
  6926. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  6927. if(ShmChargerInfo->ConnectorPageReq[gun].CtrlValue != 0)
  6928. {
  6929. isTrigger = true;
  6930. break;
  6931. }
  6932. }
  6933. if(isTrigger)
  6934. {
  6935. Set_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  6936. }
  6937. else
  6938. {
  6939. Clean_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  6940. }
  6941. }
  6942. }
  6943. }
  6944. void CheckStationInfo(void)
  6945. {
  6946. int station_id = 0;
  6947. char station_name[64];
  6948. if(Is_Ocpp_ChargerStationReq())
  6949. {
  6950. memset(station_name, 0x00, sizeof(station_name));
  6951. Get_Ocpp_ChargerStationInfo(&station_id, station_name);
  6952. if(strcmp(station_name, ShmChargerInfo->StationInfo.StationName) != EQUAL ||
  6953. ShmChargerInfo->StationInfo.StationID != station_id)
  6954. {
  6955. strcpy(ShmChargerInfo->StationInfo.StationName, station_name);
  6956. ShmChargerInfo->StationInfo.StationID = station_id;
  6957. LOG_INFO("Station ID: %d, Name: %s", ShmChargerInfo->StationInfo.StationID, ShmChargerInfo->StationInfo.StationName);
  6958. AnnounceStationInfo();
  6959. }
  6960. Clean_Ocpp_ChargerStationReq();
  6961. }
  6962. }
  6963. void CheckWeatherInfo(void)
  6964. {
  6965. int weatherID = 0;
  6966. float temperature = 0;
  6967. char strWeather[16];
  6968. 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};
  6969. if(Is_Ocpp_WeatherReq())
  6970. {
  6971. Get_Ocpp_WeatherInfo(&weatherID, &temperature);
  6972. if(weatherID != ShmChargerInfo->StationInfo.WeatherID ||
  6973. temperature != ShmChargerInfo->StationInfo.Temperature)
  6974. {
  6975. ShmChargerInfo->StationInfo.WeatherID = weatherID;
  6976. ShmChargerInfo->StationInfo.Temperature = temperature;
  6977. if(ShmChargerInfo->StationInfo.WeatherID <= _WEATHER_Mist)
  6978. {
  6979. strcpy(strWeather, str_weather_type[ShmChargerInfo->StationInfo.WeatherID]);
  6980. }
  6981. else
  6982. {
  6983. sprintf(strWeather, "%d", ShmChargerInfo->StationInfo.WeatherID);
  6984. }
  6985. LOG_INFO("Weather: [%s][%d], Temperature: %4.1f", strWeather, ShmChargerInfo->StationInfo.WeatherID, ShmChargerInfo->StationInfo.Temperature);
  6986. AnnounceStationInfo();
  6987. }
  6988. Clean_Ocpp_WeatherReq();
  6989. }
  6990. }
  6991. bool _WriteDeductBusy[CONNECTOR_QUANTITY] = {0};
  6992. void CheckDeductResultRequest(void)
  6993. {
  6994. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  6995. char cardNo[21];
  6996. char appNo[10];
  6997. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6998. {
  6999. if(Is_DeductResultReq(i))
  7000. {
  7001. if(!_WriteDeductBusy[i])
  7002. {
  7003. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7004. {
  7005. _WriteDeductBusy[i] = true;
  7006. //Set_Ocpp_DeductTransaction(_BackupDeductResult[i].TransactionId);
  7007. //Set_Ocpp_DeductCreditNo((char *)&_BackupDeductResult[i].creditNo);
  7008. //Set_Ocpp_DeductVemData("");
  7009. //Set_Ocpp_DeductAmount(((double)_BackupDeductResult[i].DeductAmount / 100));
  7010. //Set_Ocpp_DeductResult(_BackupDeductResult[i].DeductResult);
  7011. //Set_Ocpp_DonateInvoice(_BackupDeductResult[i].IsDonateInvoice);
  7012. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->DeductResultReq[i]))
  7013. {
  7014. memset(cardNo, 0x00, sizeof(cardNo));
  7015. memcpy(cardNo, (char *)&ShmChargerInfo->DeductResultReq[i].creditNo[0], sizeof(ShmChargerInfo->DeductResultReq[i].creditNo));
  7016. memset(appNo, 0x00, sizeof(appNo));
  7017. memcpy(appNo, (char *)&ShmChargerInfo->DeductResultReq[i].ApprovalNumber[0], sizeof(ShmChargerInfo->DeductResultReq[i].ApprovalNumber));
  7018. Set_Ocpp_ReportCreditDeductReq();
  7019. LOG_INFO("DeductReport Gun %d TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7020. i + 1,
  7021. ShmChargerInfo->DeductResultReq[i].TransactionId,
  7022. cardNo,
  7023. appNo,
  7024. (ShmChargerInfo->DeductResultReq[i].DeductAmount / 100),
  7025. (ShmChargerInfo->DeductResultReq[i].DeductAmount % 100),
  7026. str_ReaderStatus[ShmChargerInfo->DeductResultReq[i].DeductResult],
  7027. ShmChargerInfo->DeductResultReq[i].IsDonateInvoice ? "Yes" : "No");
  7028. if(ShmChargerInfo->DeductResultReq[i].DeductResult == _Deduct_PreAuth_Pass)
  7029. {
  7030. memcpy(&ShmChargerInfo->ReaderStatus[i], &ShmChargerInfo->DeductResultReq[i], sizeof(DeductResultInfoData));
  7031. ShmChargerInfo->ReaderStatus[i].ReaderStatusReq = YES;
  7032. }
  7033. }
  7034. }
  7035. }
  7036. else
  7037. {
  7038. if(!Is_Ocpp_ReportCreditDeductReq())
  7039. {
  7040. Clean_DeductInfo(i);
  7041. _WriteDeductBusy[i] = false;
  7042. }
  7043. }
  7044. }
  7045. else
  7046. {
  7047. _WriteDeductBusy[i] = false;
  7048. }
  7049. }
  7050. }
  7051. bool _WriteReDeductBusy = false;
  7052. void CheckReDeductRequest(void)
  7053. {
  7054. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7055. char cardNo[21];
  7056. char appNo[10];
  7057. if(ShmChargerInfo->ReDeductReq.DeductReq)
  7058. {
  7059. if(!_WriteReDeductBusy)
  7060. {
  7061. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7062. {
  7063. _WriteReDeductBusy = true;
  7064. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->ReDeductReq))
  7065. {
  7066. memset(cardNo, 0x00, sizeof(cardNo));
  7067. memcpy(cardNo, (char *)&ShmChargerInfo->ReDeductReq.creditNo[0], sizeof(ShmChargerInfo->ReDeductReq.creditNo));
  7068. memset(appNo, 0x00, sizeof(appNo));
  7069. memcpy(appNo, (char *)&ShmChargerInfo->ReDeductReq.ApprovalNumber[0], sizeof(ShmChargerInfo->ReDeductReq.ApprovalNumber));
  7070. Set_Ocpp_ReportCreditDeductReq();
  7071. LOG_INFO("ReDeductReport TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7072. ShmChargerInfo->ReDeductReq.TransactionId,
  7073. cardNo,
  7074. appNo,
  7075. (ShmChargerInfo->ReDeductReq.DeductAmount / 100),
  7076. (ShmChargerInfo->ReDeductReq.DeductAmount % 100),
  7077. str_ReaderStatus[ShmChargerInfo->ReDeductReq.DeductResult],
  7078. ShmChargerInfo->ReDeductReq.IsDonateInvoice ? "Yes" : "No");
  7079. if(ShmChargerInfo->ReDeductReq.DeductResult == _Deduct_PreAuth_Pass && ShmChargerInfo->ReDeductReq.ConnectorId != 0xFF)
  7080. {
  7081. int gunIndex = ShmChargerInfo->ReDeductReq.ConnectorId - 1;
  7082. memcpy(&ShmChargerInfo->ReaderStatus[gunIndex], &ShmChargerInfo->ReDeductReq, sizeof(DeductResultInfoData));
  7083. ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq = YES;
  7084. }
  7085. }
  7086. }
  7087. }
  7088. else
  7089. {
  7090. if(!Is_Ocpp_ReportCreditDeductReq())
  7091. {
  7092. memset(&ShmChargerInfo->ReDeductReq, 0x00, sizeof(DeductResultInfoData));
  7093. _WriteReDeductBusy = false;
  7094. }
  7095. }
  7096. }
  7097. else
  7098. {
  7099. _WriteReDeductBusy = false;
  7100. }
  7101. }
  7102. bool _WriteReaderStatusBusy[CONNECTOR_QUANTITY] = {0};
  7103. void CheckReaderStatusRequest(void)
  7104. {
  7105. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7106. char cardNo[21];
  7107. char appNo[10];
  7108. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7109. {
  7110. if(Is_ReaderStatusReq(i))
  7111. {
  7112. if(!_WriteReaderStatusBusy[i])
  7113. {
  7114. if(!Is_Ocpp_TcciReaderStatusReq(i) && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7115. {
  7116. _WriteReaderStatusBusy[i] = true;
  7117. if(Set_Ocpp_TcciReaderStatus(i, &ShmChargerInfo->ReaderStatus[i]))
  7118. {
  7119. memset(cardNo, 0x00, sizeof(cardNo));
  7120. memcpy(cardNo, (char *)&ShmChargerInfo->ReaderStatus[i].creditNo[0], sizeof(ShmChargerInfo->ReaderStatus[i].creditNo));
  7121. memset(appNo, 0x00, sizeof(appNo));
  7122. memcpy(appNo, (char *)&ShmChargerInfo->ReaderStatus[i].ApprovalNumber[0], sizeof(ShmChargerInfo->ReaderStatus[i].ApprovalNumber));
  7123. Set_Ocpp_TcciReaderStatusReq(i);
  7124. LOG_INFO("StatusReport Gun %d: [%s], CreditNo: [%s], ApprovalNo: [%s]",
  7125. i + 1, str_ReaderStatus[ShmChargerInfo->ReaderStatus[i].DeductResult], cardNo, appNo);
  7126. }
  7127. }
  7128. }
  7129. else
  7130. {
  7131. if(!Is_Ocpp_TcciReaderStatusReq(i))
  7132. {
  7133. Clean_ReaderStatusInfo(i);
  7134. _WriteReaderStatusBusy[i] = false;
  7135. }
  7136. }
  7137. }
  7138. else
  7139. {
  7140. _WriteReaderStatusBusy[i] = false;
  7141. }
  7142. }
  7143. }
  7144. void CheckOccupancyPrice(void)
  7145. {
  7146. float _price = 0;
  7147. if(Is_Ocpp_TcciSetOccupancyPriceReq())
  7148. {
  7149. _price = Get_Ocpp_TcciOccupancyPrice();
  7150. if(ShmChargerInfo->CabinetMiscValue.ParkingPrice != _price)
  7151. {
  7152. ShmChargerInfo->CabinetMiscValue.ParkingPrice = _price;
  7153. LOG_INFO("Set OccupancyPrice: [%6.2f]", ShmChargerInfo->CabinetMiscValue.ParkingPrice);
  7154. AnnounceOccupancyPrice();
  7155. }
  7156. Clean_Ocpp_TcciSetOccupancyPriceReq();
  7157. }
  7158. }
  7159. bool IsParkingButtonStatusChanged(int gunIndex)
  7160. {
  7161. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus ?
  7162. false : true;
  7163. }
  7164. void SyncParkingButtonStatus(int gunIndex)
  7165. {
  7166. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7167. }
  7168. int OccupancyButtonSync(int gunIndex)
  7169. {
  7170. int _pass = FAIL;
  7171. unsigned char _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7172. switch(_status)
  7173. {
  7174. case _BillStatus_Idle:
  7175. if(IsParkingButtonStatusChanged(gunIndex))
  7176. {
  7177. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7178. SyncParkingButtonStatus(gunIndex);
  7179. }
  7180. break;
  7181. case _BillStatus_MiscReq:
  7182. if(IsParkingButtonStatusChanged(gunIndex))
  7183. {
  7184. Occupancy_INFO("Gun %d OccupancyButton [MiscRequest]", gunIndex + 1);
  7185. SyncParkingButtonStatus(gunIndex);
  7186. }
  7187. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_WaitMiscClean;
  7188. Set_Connector_MiscCommand(gunIndex, MISC_CONN_PARKING_STATUS);
  7189. break;
  7190. case _BillStatus_WaitMiscClean:
  7191. if(IsParkingButtonStatusChanged(gunIndex))
  7192. {
  7193. SyncParkingButtonStatus(gunIndex);
  7194. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7195. }
  7196. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7197. {
  7198. Occupancy_INFO("Gun %d OccupancyButton [WaitMisc Timeout]", gunIndex + 1);
  7199. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7200. }
  7201. break;
  7202. case _BillStatus_MiscClean:
  7203. if(IsParkingButtonStatusChanged(gunIndex))
  7204. {
  7205. Occupancy_INFO("Gun %d OccupancyButton [MiscClean]", gunIndex + 1);
  7206. SyncParkingButtonStatus(gunIndex);
  7207. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7208. }
  7209. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7210. {
  7211. LOG_INFO("Gun %d OccupancyButton [Wait Timeout]", gunIndex + 1);
  7212. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7213. }
  7214. break;
  7215. case _BillStatus_UpdateDone:
  7216. if(IsParkingButtonStatusChanged(gunIndex))
  7217. {
  7218. Occupancy_INFO("Gun %d OccupancyButton [UpdateDone]", gunIndex + 1);
  7219. SyncParkingButtonStatus(gunIndex);
  7220. }
  7221. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_Idle;
  7222. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7223. _pass = PASS;
  7224. break;
  7225. default:
  7226. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7227. break;
  7228. }
  7229. return _pass;
  7230. }
  7231. int OccupancyFeeRequest(int gunIndex)
  7232. {
  7233. if(!_isGetOccupancyFeeReq[gunIndex])
  7234. {
  7235. if(!Is_Ocpp_TcciGetOccupancyFeeReq(gunIndex))
  7236. {
  7237. Set_Ocpp_TcciGetOccupancyFeeReq(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill);
  7238. _isGetOccupancyFeeReq[gunIndex] = true;
  7239. LOG_INFO("Gun %d Set OccupancyFee Request", gunIndex + 1);
  7240. }
  7241. }
  7242. else
  7243. {
  7244. if(!Is_Ocpp_TcciGetOccupancyFeeReq(gunIndex))
  7245. {
  7246. if(Is_Ocpp_TcciGetOccupancyFeeStatus(gunIndex, "Accepted"))
  7247. {
  7248. _isGetOccupancyFeeReq[gunIndex] = false;
  7249. Get_Ocpp_TcciGetOccupancyFeeResponse(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill);
  7250. LOG_INFO("Gun %d Get Occupancy Fee: [%.2f], Duration: [%d], StartTime: [%s]",
  7251. gunIndex + 1,
  7252. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Fee,
  7253. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Duration,
  7254. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.StartTime);
  7255. return PASS;
  7256. }
  7257. else
  7258. {
  7259. LOG_INFO("Gun %d Get OccupancyFee Rejected", gunIndex + 1);
  7260. return FAIL;
  7261. }
  7262. }
  7263. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7264. {
  7265. // shall not reach here
  7266. Clean_Ocpp_TcciGetOccupancyFeeReq(gunIndex);
  7267. _isGetOccupancyFeeReq[gunIndex] = false;
  7268. LOG_INFO("Gun %d GetOccupancyFee Request Timeout", gunIndex + 1);
  7269. return FAIL;
  7270. }
  7271. }
  7272. return WAIT;
  7273. }
  7274. bool IsParkingStatusChanged(int gunIndex)
  7275. {
  7276. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus ?
  7277. false : true;
  7278. }
  7279. void SyncParkingStatus(int gunIndex)
  7280. {
  7281. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7282. }
  7283. bool Is_Occupancy_Cancel(int gunIndex)
  7284. {
  7285. unsigned char dispenser = 0;
  7286. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParentDispensetIndex;
  7287. if((!ShmSysConfigAndInfo->SysInfo.OcppConnStatus && !ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq) ||
  7288. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_None ||
  7289. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_Timeout)
  7290. {
  7291. return true;
  7292. }
  7293. return false;
  7294. }
  7295. void OccupancyRequest(int gunIndex)
  7296. {
  7297. int _action = 0;
  7298. bool feeRequest = false;
  7299. char _sn[37];
  7300. unsigned char _status = 0;
  7301. 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};
  7302. if(Is_Ocpp_TcciOccupancyDisplayReq(gunIndex))
  7303. {
  7304. memset(_sn, 0x00, sizeof(_sn));
  7305. _action = Get_Ocpp_TcciOccupancyAction(gunIndex, sizeof(_sn) - 1, _sn);
  7306. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ActionId = _action;
  7307. LOG_INFO("Gun %d OccupancyAction is [%s]", gunIndex + 1, str_action[_action]);
  7308. Clean_Ocpp_TcciOccupancyDisplayReq(gunIndex);
  7309. }
  7310. if(ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest)
  7311. {
  7312. feeRequest = true;
  7313. ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest =false;
  7314. }
  7315. _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7316. if(_status != _Parking_NoOccupancy && chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7317. {
  7318. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7319. Occupancy_INFO("Gun %d OccupancyStatus [ForceClean]", gunIndex + 1);
  7320. return;
  7321. }
  7322. switch(_status)
  7323. {
  7324. case _Parking_NoOccupancy:
  7325. if(IsParkingStatusChanged(gunIndex))
  7326. {
  7327. Occupancy_INFO("Gun %d OccupancyStatus [Clean]", gunIndex + 1);
  7328. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex], 0x00, sizeof(OccupancyInfoData));
  7329. SyncParkingStatus(gunIndex);
  7330. }
  7331. if(_action == _ParkingAction_DisplayButton)
  7332. {
  7333. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7334. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_SyncOccupancy;
  7335. }
  7336. break;
  7337. case _Parking_SyncOccupancy:
  7338. if(IsParkingStatusChanged(gunIndex))
  7339. {
  7340. Occupancy_INFO("Gun %d OccupancyStatus [Sync]", gunIndex + 1);
  7341. SyncParkingStatus(gunIndex);
  7342. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7343. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7344. }
  7345. if(_action == _ParkingAction_DisplayButton)
  7346. {
  7347. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7348. }
  7349. else if(_action == _ParkingAction_CancelOccupancy ||
  7350. Is_Occupancy_Cancel(gunIndex))
  7351. {
  7352. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7353. }
  7354. else if(_action == _ParkingAction_PaidOnlineOK)
  7355. {
  7356. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7357. }
  7358. else
  7359. {
  7360. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  7361. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  7362. {
  7363. if(OccupancyButtonSync(gunIndex) == PASS)
  7364. {
  7365. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7366. }
  7367. }
  7368. }
  7369. break;
  7370. case _Parking_Occupied:
  7371. if(IsParkingStatusChanged(gunIndex))
  7372. {
  7373. Occupancy_INFO("Gun %d OccupancyStatus [Start]", gunIndex + 1);
  7374. SyncParkingStatus(gunIndex);
  7375. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7376. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7377. }
  7378. if(_action == _ParkingAction_DisplayButton)
  7379. {
  7380. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7381. }
  7382. else if(_action == _ParkingAction_CancelOccupancy ||
  7383. Is_Occupancy_Cancel(gunIndex))
  7384. {
  7385. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7386. }
  7387. else if(_action == _ParkingAction_PaidOnlineOK)
  7388. {
  7389. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7390. }
  7391. else if(_action == _ParkingAction_PaidOnlineFail)
  7392. {
  7393. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7394. }
  7395. else
  7396. {
  7397. if(feeRequest)
  7398. {
  7399. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_RequestFee;
  7400. }
  7401. }
  7402. break;
  7403. case _Parking_RequestFee:
  7404. if(IsParkingStatusChanged(gunIndex))
  7405. {
  7406. Occupancy_INFO("Gun %d OccupancyStatus [RequestFee]", gunIndex + 1);
  7407. SyncParkingStatus(gunIndex);
  7408. _isGetOccupancyFeeReq[gunIndex] = false;
  7409. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7410. }
  7411. if(_action == _ParkingAction_CancelOccupancy ||
  7412. Is_Occupancy_Cancel(gunIndex))
  7413. {
  7414. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7415. }
  7416. else if(_action == _ParkingAction_PaidOnlineOK)
  7417. {
  7418. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7419. }
  7420. else if(_action == _ParkingAction_PaidOnlineFail)
  7421. {
  7422. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7423. }
  7424. else
  7425. {
  7426. if(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq)
  7427. {
  7428. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7429. }
  7430. else
  7431. {
  7432. int res = OccupancyFeeRequest(gunIndex);
  7433. if(res == PASS)
  7434. {
  7435. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7436. }
  7437. else if(res == FAIL)
  7438. {
  7439. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7440. }
  7441. else
  7442. {
  7443. // wait response
  7444. }
  7445. }
  7446. }
  7447. break;
  7448. case _Parking_WaitToPay:
  7449. if(IsParkingStatusChanged(gunIndex))
  7450. {
  7451. Occupancy_INFO("Gun %d OccupancyStatus [WaitToPay]", gunIndex + 1);
  7452. SyncParkingStatus(gunIndex);
  7453. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].BillReqStatus, 0x00, sizeof(MiscRequestStatus));
  7454. }
  7455. if(_action == _ParkingAction_CancelOccupancy ||
  7456. Is_Occupancy_Cancel(gunIndex))
  7457. {
  7458. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7459. }
  7460. else if(_action == _ParkingAction_PaidOnlineOK)
  7461. {
  7462. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7463. }
  7464. else if(_action == _ParkingAction_PaidOnlineFail)
  7465. {
  7466. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7467. }
  7468. break;
  7469. case _Parking_PaidOnlineOK:
  7470. if(IsParkingStatusChanged(gunIndex))
  7471. {
  7472. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineOK]", gunIndex + 1);
  7473. SyncParkingStatus(gunIndex);
  7474. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7475. }
  7476. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7477. {
  7478. LOG_INFO("Gun %d Reset OccupancyStatus via PaidOnlineOK", gunIndex + 1);
  7479. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7480. }
  7481. break;
  7482. case _Parking_PaidOnlineFail:
  7483. if(IsParkingStatusChanged(gunIndex))
  7484. {
  7485. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineFail]", gunIndex + 1);
  7486. SyncParkingStatus(gunIndex);
  7487. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7488. }
  7489. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7490. {
  7491. LOG_INFO("Gun %d Rerutn to Occupied via PaidOnlineFail", gunIndex + 1);
  7492. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7493. }
  7494. break;
  7495. default:
  7496. DisableConnectorBill(gunIndex);
  7497. break;
  7498. }
  7499. }
  7500. void CheckOccupancyFee(void)
  7501. {
  7502. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7503. {
  7504. OccupancyRequest(i);
  7505. }
  7506. }
  7507. #define OCCUPANCY_DEDUCT_IDLE 0
  7508. #define OCCUPANCY_DEDUCT_REQUEST 1
  7509. #define OCCUPANCY_DEDUCT_CANCEL 2
  7510. unsigned char _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7511. int _OccupancyReportGunId = 0;
  7512. void CheckOccupancyDeduct(void)
  7513. {
  7514. char cardNo[21];
  7515. char appNo[10];
  7516. if(_OccupancyDeductStatus == OCCUPANCY_DEDUCT_IDLE)
  7517. {
  7518. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7519. {
  7520. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7521. {
  7522. if(Is_OccupancyDeductReq(i))
  7523. {
  7524. if(ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyReq.bits.SelfReq &&
  7525. ShmChargerInfo->AllBill.OccupancyInfo[i].Status == _Parking_WaitToPay)
  7526. {
  7527. ShmChargerInfo->AllBill.OccupancyInfo[i].Status = _Parking_NoOccupancy;
  7528. LOG_INFO("Gun %d Occupancy Concluded!", i + 1);
  7529. break;
  7530. }
  7531. Set_Ocpp_OccupancyDeductResult(&ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyDeduct, YES);
  7532. Set_Ocpp_TcciReportOccupancyDeductReq();
  7533. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_REQUEST;
  7534. _OccupancyReportGunId = i + 1;
  7535. Clean_OccupancyDeductReq(i);
  7536. break;
  7537. }
  7538. if(Is_OccupancyKeepCountingReq(i))
  7539. {
  7540. if(ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyReq.bits.SelfReq)
  7541. {
  7542. LOG_INFO("Gun %d Occupancy Resume!", i + 1);
  7543. Clean_OccupancyKeepCountingReq(i);
  7544. break;
  7545. }
  7546. OccupancyDeductInfo _cancel_deduct;
  7547. memset(&_cancel_deduct, 0x00, sizeof(OccupancyDeductInfo));
  7548. memcpy(&_cancel_deduct.Sn, ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyBill.Sn, sizeof(_cancel_deduct.Sn));
  7549. Set_Ocpp_OccupancyDeductResult(&_cancel_deduct, NO);
  7550. Set_Ocpp_TcciReportOccupancyDeductReq();
  7551. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_CANCEL;
  7552. _OccupancyReportGunId = i + 1;
  7553. Clean_OccupancyKeepCountingReq(i);
  7554. break;
  7555. }
  7556. }
  7557. if(_OccupancyDeductStatus != OCCUPANCY_DEDUCT_IDLE)
  7558. {
  7559. memset(cardNo, 0x00, sizeof(cardNo));
  7560. memset(appNo, 0x00, sizeof(appNo));
  7561. memcpy(cardNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo));
  7562. memcpy(appNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber));
  7563. if(_OccupancyDeductStatus == OCCUPANCY_DEDUCT_REQUEST)
  7564. {
  7565. LOG_INFO("Gun %d Occupancy Deduct [%s] Sn: [%s], Fee: [%.2f], CreditNo: [%s], ApprovalNo: [%s]",
  7566. _OccupancyReportGunId,
  7567. _OccupancyDeductStatus == OCCUPANCY_DEDUCT_REQUEST ? "Request" : "Cancel",
  7568. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Sn,
  7569. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Fee,
  7570. cardNo,
  7571. appNo);
  7572. }
  7573. else
  7574. {
  7575. LOG_INFO("Gun %d Occupancy Deduct [%s] Sn: [%s]",
  7576. _OccupancyReportGunId,
  7577. _OccupancyDeductStatus == OCCUPANCY_DEDUCT_REQUEST ? "Request" : "Cancel",
  7578. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyBill.Sn);
  7579. }
  7580. }
  7581. }
  7582. }
  7583. else
  7584. {
  7585. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7586. {
  7587. if(_OccupancyDeductStatus == OCCUPANCY_DEDUCT_REQUEST)
  7588. {
  7589. if(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status == _Parking_WaitToPay)
  7590. {
  7591. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status = _Parking_NoOccupancy;
  7592. LOG_INFO("Gun %d OccupancyDeduct Request Completed!", _OccupancyReportGunId);
  7593. }
  7594. }
  7595. else
  7596. {
  7597. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status = _Parking_Occupied;
  7598. LOG_INFO("Gun %d Occupancy Continued!", _OccupancyReportGunId);
  7599. }
  7600. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7601. }
  7602. }
  7603. }
  7604. //===============================================
  7605. // OCPP
  7606. //===============================================
  7607. void CheckOcppStatus()
  7608. {
  7609. if (Is_Ocpp_BootNotification())
  7610. {
  7611. Clean_Ocpp_BootNotification();
  7612. LOG_INFO("*********************OCPP Boot Notification****************************");
  7613. }
  7614. if (Is_Ocpp_ResetReq())
  7615. {
  7616. bool canReset = true;
  7617. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  7618. {
  7619. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7620. {
  7621. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  7622. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  7623. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  7624. {
  7625. canReset = false;
  7626. //if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  7627. //{
  7628. // ChargingTerminalProcess(_index);
  7629. //}
  7630. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7631. {
  7632. chargingInfo[_index]->ChargingStopFlag.bits.HardResetStop = true;
  7633. }
  7634. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7635. {
  7636. chargingInfo[_index]->ChargingStopFlag.bits.SoftResetStop = true;
  7637. }
  7638. }
  7639. }
  7640. }
  7641. if(canReset)
  7642. {
  7643. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7644. {
  7645. //trigger dispenser to hardware reboot
  7646. TriggerDispenserHardwareReboot();
  7647. if(IsDispenserHardwareRebootResponsed())
  7648. {
  7649. Clean_Ocpp_ResetReq();
  7650. LOG_INFO("****** Hard Reboot ******");
  7651. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7652. {
  7653. setChargerMode(i, MODE_MAINTAIN);
  7654. }
  7655. sleep(3);
  7656. system("reboot -f");
  7657. }
  7658. }
  7659. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7660. {
  7661. //trigger dispenser to software reset
  7662. TriggerDispenserSoftwareReset();
  7663. if(IsDispenserSoftwareResetResponsed())
  7664. {
  7665. Clean_Ocpp_ResetReq();
  7666. LOG_INFO("****** Soft Reboot ******");
  7667. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7668. {
  7669. setChargerMode(i, MODE_MAINTAIN);
  7670. }
  7671. sleep(3);
  7672. Kill_Ocpp_Service();
  7673. KillAllTask();
  7674. TryCloseWatchdog();
  7675. system("/usr/bin/run_evse_restart.sh");
  7676. }
  7677. }
  7678. }
  7679. }
  7680. CheckOcppCostAndPrice();
  7681. CheckLcmPageChangeRequest();
  7682. CheckStationInfo();
  7683. CheckWeatherInfo();
  7684. CheckDeductResultRequest();
  7685. CheckReDeductRequest();
  7686. CheckReaderStatusRequest();
  7687. CheckOccupancyPrice();
  7688. CheckOccupancyFee();
  7689. CheckOccupancyDeduct();
  7690. }
  7691. void CheckoutStartChargingSoc(byte gunIndex, bool force)
  7692. {
  7693. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc)
  7694. {
  7695. if(chargingInfo[gunIndex]->EvBatterySoc > 0 || force)
  7696. {
  7697. chargingInfo[gunIndex]->EvBatteryStartSoc = chargingInfo[gunIndex]->EvBatterySoc;
  7698. LOG_INFO("Gun %d StartSoc = %d", chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->EvBatteryStartSoc);
  7699. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc = true;
  7700. }
  7701. }
  7702. }
  7703. void UpdateChargingTimestamp(char *timestamp)
  7704. {
  7705. struct timeb SeqEndTime;
  7706. struct tm *tm;
  7707. ftime(&SeqEndTime);
  7708. SeqEndTime.time = time(NULL);
  7709. tm = localtime(&SeqEndTime.time);
  7710. 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);
  7711. }
  7712. void OcppStartTransaction(byte gunIndex)
  7713. {
  7714. if(_triggerTransaction[gunIndex])
  7715. {
  7716. return;
  7717. }
  7718. // clean deduct info
  7719. Clean_DeductInfo(gunIndex);
  7720. // set deduct user id
  7721. //Set_Deduct_UserId(gunIndex, (char *)&chargingInfo[gunIndex]->StartUserId);
  7722. // initial start & stop datetime
  7723. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00");
  7724. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, "00:00:00");
  7725. Update_PowerConsumption(gunIndex);
  7726. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  7727. Set_Ocpp_StartTransactionIdTag(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  7728. LOG_INFO("Gun %d Start Transaction IdTag = %s, MeterValue: %.4f",
  7729. chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->StartUserId, chargingInfo[gunIndex]->PowerConsumption);
  7730. Clean_Ocpp_StartTransactionConf(gunIndex);
  7731. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StartCharging);
  7732. Set_Ocpp_StartTransactionReq(gunIndex);
  7733. // set _triggerTransaction
  7734. _triggerTransaction[gunIndex] = true;
  7735. }
  7736. void OcppStopTransaction(byte gunIndex)
  7737. {
  7738. if(!_triggerTransaction[gunIndex])
  7739. {
  7740. return;
  7741. }
  7742. Update_PowerConsumption(gunIndex);
  7743. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  7744. Set_Ocpp_StopTransactionId(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  7745. LOG_INFO("Gun %d Stop Transaction IdTag = %s, StopSoc = %d, MeterValue: %.4f",
  7746. chargingInfo[gunIndex]->Index + 1,
  7747. chargingInfo[gunIndex]->StartUserId,
  7748. chargingInfo[gunIndex]->EvBatterySoc,
  7749. chargingInfo[gunIndex]->PowerConsumption);
  7750. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StopCharging);
  7751. Set_Ocpp_StopTransactionReq(gunIndex);
  7752. // clean _triggerTransaction
  7753. _triggerTransaction[gunIndex] = false;
  7754. }
  7755. bool Is_DispenserStop(byte gunIndex)
  7756. {
  7757. bool _isStop = false;
  7758. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  7759. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  7760. {
  7761. _isStop = true;
  7762. }
  7763. return _isStop;
  7764. }
  7765. void ChkOcppStatus(byte gunIndex)
  7766. {
  7767. Chk_Ocpp_Backup_ErrorCode(gunIndex);
  7768. if(Is_Ocpp_ReserveNowReq(gunIndex))
  7769. {
  7770. Clean_Ocpp_ReserveNowReq(gunIndex);
  7771. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE && !Is_Ocpp_ReserveNowExpired(gunIndex))
  7772. {
  7773. LOG_INFO("***************ChkOcppStatus: Gun %d OcppReservedStatus********************", gunIndex + 1);
  7774. chargingInfo[gunIndex]->ReservationId = Get_Ocpp_ReserveNowId(gunIndex);
  7775. setChargerMode(gunIndex, MODE_RESERVATION);
  7776. }
  7777. Set_Ocpp_ReserveNowConf(gunIndex);
  7778. }
  7779. if(Is_Ocpp_CancelReservationReq(gunIndex))
  7780. {
  7781. Clean_Ocpp_CancelReservationReq(gunIndex);
  7782. if(chargingInfo[gunIndex]->SystemStatus == S_RESERVATION)
  7783. {
  7784. LOG_INFO("***************ChkOcppStatus: Gun %d Cancel OcppReservedStatus********************", gunIndex + 1);
  7785. chargingInfo[gunIndex]->ReservationId = -1;
  7786. setChargerMode(gunIndex, MODE_IDLE);
  7787. }
  7788. Set_Ocpp_CancelReservationConf(gunIndex);
  7789. }
  7790. if (Is_Ocpp_ChangeAvailabilityReq(gunIndex) == YES)
  7791. {
  7792. LOG_INFO("***************ChkOcppStatus: Gun %d OcppChangeAvailability********************", gunIndex + 1);
  7793. Clean_Ocpp_ChangeAvailabilityReq(gunIndex);
  7794. if(Chk_Ocpp_Availability_Type(gunIndex, "Operative"))
  7795. {
  7796. if (isDb_ready)
  7797. DB_Update_Operactive(localDb, gunIndex, true);
  7798. chargingInfo[gunIndex]->IsAvailable = YES;
  7799. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  7800. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  7801. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  7802. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  7803. {
  7804. setChargerMode(gunIndex, MODE_IDLE);
  7805. }
  7806. }
  7807. else if(Chk_Ocpp_Availability_Type(gunIndex, "Inoperative"))
  7808. {
  7809. if (isDb_ready)
  7810. DB_Update_Operactive(localDb, gunIndex, false);
  7811. chargingInfo[gunIndex]->IsAvailable = NO;
  7812. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  7813. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  7814. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  7815. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  7816. {
  7817. setChargerMode(gunIndex, MODE_MAINTAIN);
  7818. }
  7819. }
  7820. }
  7821. if (Is_Ocpp_UnlockConnectorReq(gunIndex))
  7822. {
  7823. Clean_Ocpp_UnlockConnectorReq(gunIndex);
  7824. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  7825. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  7826. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  7827. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7828. {
  7829. // 充電中,需停止充電
  7830. Set_Ocpp_StopReason(gunIndex, "UnlockCommand");
  7831. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_UNLOCK);
  7832. chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop = true;
  7833. LOG_INFO("********** OcppUnlockStop Gun %d **********", gunIndex + 1);
  7834. }
  7835. Set_Ocpp_UnlockConnectorConf(gunIndex);
  7836. }
  7837. if (Is_Ocpp_RemoteStopReq(gunIndex))
  7838. {
  7839. Clean_Ocpp_RemoteStopReq(gunIndex);
  7840. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  7841. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  7842. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  7843. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7844. {
  7845. Set_Ocpp_StopReason(gunIndex, "Remote");
  7846. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_REMOTE_STOP);
  7847. chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop = true;
  7848. LOG_INFO("********** OcppRemoteStop Gun %d **********", gunIndex + 1);
  7849. }
  7850. }
  7851. if(Is_Ocpp_StartTransactionConf(gunIndex))
  7852. {
  7853. char acceptId[128] = {0};
  7854. char status[32] = {0};
  7855. Get_Ocpp_StartTransaction_Status(gunIndex, status);
  7856. Get_Ocpp_TransactionId(gunIndex, acceptId);
  7857. Set_Deduct_TransactionId(gunIndex, atoi(acceptId));
  7858. Set_UserTransactionId(gunIndex, atoi(acceptId));
  7859. LOG_INFO("Gun %d OCPP Start Transaction %s, TransactionId [%s]", gunIndex + 1, status, acceptId);
  7860. if(!Is_Ocpp_StartTransaction_Accepted(gunIndex))
  7861. {
  7862. if(Is_Ocpp_StopOnInvalidId_Enable())
  7863. {
  7864. if(Is_Invalid_StartTransactionId(gunIndex))
  7865. {
  7866. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop = true;
  7867. LOG_INFO("Gun %d OCPP StopOnInvalidId", gunIndex + 1);
  7868. }
  7869. }
  7870. LOG_INFO("Gun %d Transaction [%s] Not Accepted, %s",
  7871. gunIndex + 1, acceptId,
  7872. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop > 0 ? "And Stop Charging" : "But StopOnInvalidId is Disable");
  7873. }
  7874. Clean_Ocpp_StartTransactionConf(gunIndex);
  7875. }
  7876. }
  7877. bool CheckBackendChargingTimeout(byte gunIndex)
  7878. {
  7879. bool result = false;
  7880. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  7881. {
  7882. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  7883. {
  7884. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  7885. result = true;
  7886. }
  7887. }
  7888. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  7889. {
  7890. // 隨插即充電的要看 offline
  7891. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  7892. {
  7893. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  7894. result = true;
  7895. }
  7896. }
  7897. return result;
  7898. }
  7899. bool CheckBackendChargingEnergy(byte gunIndex)
  7900. {
  7901. bool result = false;
  7902. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  7903. {
  7904. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  7905. {
  7906. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  7907. result = true;
  7908. }
  7909. }
  7910. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  7911. {
  7912. // 隨插即充電的要看 offline
  7913. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  7914. {
  7915. if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  7916. result = true;
  7917. }
  7918. }
  7919. return result;
  7920. }
  7921. bool IsReachConfigMaxChargingSoc(byte gunIndex)
  7922. {
  7923. bool result = false;
  7924. if(ShmSysConfigAndInfo->SysConfig.MaxChargingSoc > 0)
  7925. {
  7926. if(chargingInfo[gunIndex]->EvBatterySoc >= ShmSysConfigAndInfo->SysConfig.MaxChargingSoc)
  7927. {
  7928. LOG_INFO("Gun %d MaxChargingSoc: [%d%%] Stop", gunIndex + 1, ShmSysConfigAndInfo->SysConfig.MaxChargingSoc);
  7929. result = true;
  7930. }
  7931. }
  7932. return result;
  7933. }
  7934. bool CheckSessionTargetStop(byte gunIndex)
  7935. {
  7936. bool stop = false;
  7937. struct StructSessionTarget target;
  7938. Get_Ocpp_SessionTarget(gunIndex, &target);
  7939. if(target.targetDuration > 0)
  7940. {
  7941. if(chargingInfo[gunIndex]->PresentChargedDuration >= (target.targetDuration * 60))
  7942. {
  7943. LOG_INFO("Gun %d SessionTarget(Duration) Stop, Present: %d (sec), Target: %d (min)",
  7944. gunIndex + 1,
  7945. chargingInfo[gunIndex]->PresentChargedDuration,
  7946. target.targetDuration);
  7947. stop = true;
  7948. }
  7949. }
  7950. if(target.targetSoc > 0)
  7951. {
  7952. if(chargingInfo[gunIndex]->EvBatterySoc >= target.targetSoc)
  7953. {
  7954. LOG_INFO("Gun %d SessionTarget(SOC) Stop, Present: %d (%%), Target: %d (%%)",
  7955. gunIndex + 1,
  7956. chargingInfo[gunIndex]->EvBatterySoc,
  7957. target.targetSoc);
  7958. stop = true;
  7959. }
  7960. }
  7961. if(target.targetEnergy > 0)
  7962. {
  7963. if(chargingInfo[gunIndex]->PresentChargedEnergy >= target.targetEnergy)
  7964. {
  7965. LOG_INFO("Gun %d SessionTarget(Energy) Stop, Present: %8.3f (kWh), Target: %4d (kWh)",
  7966. gunIndex + 1,
  7967. chargingInfo[gunIndex]->PresentChargedEnergy,
  7968. target.targetEnergy);
  7969. stop = true;
  7970. }
  7971. }
  7972. if(stop)
  7973. {
  7974. chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop = true;
  7975. }
  7976. return stop;
  7977. }
  7978. void InformOcppErrOccur(byte codeType)
  7979. {
  7980. char _error[25];
  7981. switch(codeType)
  7982. {
  7983. case 4: strcpy(_error, "InternalError"); break;
  7984. case 6: strcpy(_error, "NoError"); break;
  7985. case 7: strcpy(_error, "OtherError"); break;
  7986. case 13: strcpy(_error, "UnderVoltage"); break;
  7987. case 14: strcpy(_error, "OverVoltage"); break;
  7988. }
  7989. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  7990. {
  7991. SetOcppErrorCode(gun_index, _error);
  7992. }
  7993. }
  7994. void SetOcppErrorCode(unsigned char _index, char *errCode)
  7995. {
  7996. Set_Ocpp_StatusNotificationErrorCode(_index, errCode);
  7997. strcpy((char *)_BackupStatusNotification[_index].ErrorCode, errCode);
  7998. }
  7999. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode)
  8000. {
  8001. Set_Ocpp_StatusNotificationVendorCode(_index, vendorCode);
  8002. strcpy((char *)_BackupStatusNotification[_index].VendorErrorCode, vendorCode);
  8003. }
  8004. //===============================================
  8005. // SQLite3 related routine
  8006. //===============================================
  8007. int NetworkDB_Open(sqlite3 *db)
  8008. {
  8009. int result = PASS;
  8010. char* errMsg = NULL;
  8011. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  8012. "idx integer primary key AUTOINCREMENT, "
  8013. "occurDatetime text NOT NULL, "
  8014. "isInternet text NOT NULL, "
  8015. "isOcppConnected text NOT NULL, "
  8016. "isEth0Internet text NOT NULL, "
  8017. "isMlan0Internet text NOT NULL, "
  8018. "isPpp0Internet text NOT NULL, "
  8019. "rssiMlan0 text NOT NULL, "
  8020. "rssiPpp0 text NOT NULL"
  8021. ");";
  8022. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  8023. if(sqlite3_open(NETWORK_DB_FILE, &db))
  8024. {
  8025. result = FAIL;
  8026. LOG_ERROR("Can't open database (Net) : %s", sqlite3_errmsg(db));
  8027. sqlite3_close(db);
  8028. }
  8029. else
  8030. {
  8031. LOG_INFO("Local network status record database open successfully.");
  8032. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8033. {
  8034. result = FAIL;
  8035. LOG_ERROR("Create local network status record table error message: %s", errMsg);
  8036. }
  8037. else
  8038. {
  8039. LOG_INFO("Opened local network status record table successfully");
  8040. }
  8041. sqlite3_close(db);
  8042. }
  8043. return result;
  8044. }
  8045. int DB_Open(sqlite3 *db)
  8046. {
  8047. int result = PASS;
  8048. char* errMsg = NULL;
  8049. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  8050. "idx integer primary key AUTOINCREMENT, "
  8051. "reservationId text, "
  8052. "transactionId text, "
  8053. "startMethod text, "
  8054. "userId text, "
  8055. "dateTimeStart text, "
  8056. "dateTimeStop text,"
  8057. "socStart text, "
  8058. "socStop text, "
  8059. "chargeEnergy text, "
  8060. "stopReason text"
  8061. ");";
  8062. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  8063. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8064. "`IsAvailable` TEXT NOT NULL, "
  8065. "`connector` INTEGER NOT NULL, "
  8066. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  8067. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  8068. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8069. "`occurDatetime` TEXT NOT NULL, "
  8070. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  8071. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  8072. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8073. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  8074. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  8075. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8076. "`connector` INTEGER NOT NULL, "
  8077. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8078. char *createGunConsumptionSql = "CREATE TABLE IF NOT EXISTS `gun_consumption` ( "
  8079. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8080. "`connector` INTEGER NOT NULL, "
  8081. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8082. char *createFinalCostSql = "CREATE TABLE IF NOT EXISTS `final_cost` ( "
  8083. "idx integer primary key AUTOINCREMENT, "
  8084. "gun text, "
  8085. "transactionId text, "
  8086. "occurDatetime text, "
  8087. "updateDatetime text, "
  8088. "description text, "
  8089. "updateStatus integer"
  8090. ");";
  8091. if(sqlite3_open(DB_FILE, &db))
  8092. {
  8093. result = FAIL;
  8094. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8095. sqlite3_close(db);
  8096. }
  8097. else
  8098. {
  8099. LOG_INFO( "Local charging record database open successfully.");
  8100. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8101. {
  8102. result = FAIL;
  8103. LOG_ERROR( "Create local charging record table error message: %s", errMsg);
  8104. }
  8105. else
  8106. {
  8107. LOG_INFO( "Opened local charging record table successfully");
  8108. }
  8109. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  8110. {
  8111. result = FAIL;
  8112. LOG_ERROR( "Create local config table error message: %s", errMsg);
  8113. }
  8114. else
  8115. {
  8116. LOG_INFO( "Opened local config table successfully");
  8117. }
  8118. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8119. {
  8120. result = FAIL;
  8121. LOG_ERROR( "Create local record table error message: %s", errMsg);
  8122. }
  8123. else
  8124. {
  8125. LOG_INFO( "Opened local record table successfully");
  8126. }
  8127. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  8128. {
  8129. result = FAIL;
  8130. LOG_ERROR( "Create reboot record table error message: %s", errMsg);
  8131. }
  8132. else
  8133. {
  8134. LOG_INFO( "Opened reboot record table successfully");
  8135. }
  8136. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8137. {
  8138. result = FAIL;
  8139. LOG_ERROR( "Create power consumption record table error message: %s", errMsg);
  8140. }
  8141. else
  8142. {
  8143. LOG_INFO( "Opened power consumption record table successfully");
  8144. }
  8145. if (sqlite3_exec(db, createGunConsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8146. {
  8147. result = FAIL;
  8148. LOG_ERROR( "Create gun consumption record table error message: %s", errMsg);
  8149. }
  8150. else
  8151. {
  8152. LOG_INFO( "Opened gun consumption record table successfully");
  8153. }
  8154. if (sqlite3_exec(db, createFinalCostSql, 0, 0, &errMsg) != SQLITE_OK)
  8155. {
  8156. result = FAIL;
  8157. LOG_ERROR( "Create final cost table error message: %s", errMsg);
  8158. }
  8159. else
  8160. {
  8161. LOG_INFO( "Opened final cost table successfully");
  8162. }
  8163. sqlite3_close(db);
  8164. }
  8165. return result;
  8166. }
  8167. int DB_Insert_Record(sqlite3 *db, int gun_index)
  8168. {
  8169. int result = PASS;
  8170. char* errMsg = NULL;
  8171. char insertSql[1024];
  8172. char transactionId[128];
  8173. char reason[32];
  8174. Get_Ocpp_TransactionId(gun_index, transactionId);
  8175. Get_Ocpp_StopReason(gun_index, reason);
  8176. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  8177. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  8178. chargingInfo[gun_index]->ReservationId,
  8179. transactionId,
  8180. chargingInfo[gun_index]->StartMethod,
  8181. chargingInfo[gun_index]->StartUserId,
  8182. chargingInfo[gun_index]->StartDateTime,
  8183. chargingInfo[gun_index]->StopDateTime,
  8184. chargingInfo[gun_index]->EvBatteryStartSoc,
  8185. chargingInfo[gun_index]->EvBatterySoc,
  8186. chargingInfo[gun_index]->PresentChargedEnergy,
  8187. reason);
  8188. if(sqlite3_open(DB_FILE, &db))
  8189. {
  8190. result = FAIL;
  8191. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8192. sqlite3_close(db);
  8193. }
  8194. else
  8195. {
  8196. //LOG_INFO( "Local charging record database open successfully.");
  8197. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8198. {
  8199. result = FAIL;
  8200. LOG_ERROR("Insert local charging record error message: %s", errMsg);
  8201. }
  8202. else
  8203. {
  8204. //LOG_INFO("Insert local charging record successfully");
  8205. }
  8206. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  8207. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8208. {
  8209. result = FAIL;
  8210. LOG_ERROR("delete local charging error message: %s", errMsg);
  8211. }
  8212. else
  8213. {
  8214. //LOG_INFO("delete local charging record successfully");
  8215. }
  8216. sqlite3_close(db);
  8217. }
  8218. return result;
  8219. }
  8220. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  8221. {
  8222. uint8_t result = false;
  8223. char* errMsg = NULL;
  8224. char sqlStr[1024];
  8225. srand(time(NULL));
  8226. if(sqlite3_open(DB_FILE, &db))
  8227. {
  8228. result = FAIL;
  8229. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8230. sqlite3_close(db);
  8231. }
  8232. else
  8233. {
  8234. LOG_INFO( "Local charging record database open successfully (%d).", IsAvailable);
  8235. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  8236. LOG_INFO("sqlStr= %s", sqlStr);
  8237. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8238. {
  8239. result = FAIL;
  8240. LOG_INFO( "update config error message: %s", errMsg);
  8241. }
  8242. else
  8243. {
  8244. LOG_INFO("update connector-%d config item isOperactive to %d", gun_index, IsAvailable);
  8245. }
  8246. sqlite3_close(db);
  8247. }
  8248. return result;
  8249. }
  8250. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  8251. {
  8252. uint8_t result = true;
  8253. char* errMsg = NULL;
  8254. char sqlStr[1024];
  8255. char **rs;
  8256. int rows, cols;
  8257. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  8258. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  8259. if(sqlite3_open(DB_FILE, &db))
  8260. {
  8261. result = FAIL;
  8262. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8263. sqlite3_close(db);
  8264. }
  8265. else
  8266. {
  8267. LOG_INFO( "Local config query database open successfully.");
  8268. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8269. if(rows>0)
  8270. {
  8271. for(int idxRow=1;idxRow<=rows;idxRow++)
  8272. {
  8273. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  8274. {
  8275. result = false;
  8276. }
  8277. LOG_INFO("Query connector-%d isOperactive: %s", gun_index, rs[(idxRow*cols)+3]);
  8278. }
  8279. }
  8280. else
  8281. {
  8282. LOG_INFO("Query connector-%d fail, set default value to operactive.", gun_index);
  8283. }
  8284. sqlite3_free_table(rs);
  8285. sqlite3_close(db);
  8286. }
  8287. return result;
  8288. }
  8289. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8290. {
  8291. char* errMsg = NULL;
  8292. char sqlStr[1024];
  8293. srand(time(NULL));
  8294. if(sqlite3_open(DB_FILE, &db))
  8295. {
  8296. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8297. sqlite3_close(db);
  8298. }
  8299. else
  8300. {
  8301. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  8302. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8303. {
  8304. LOG_ERROR("update power_consumption error message: %s", errMsg);
  8305. }
  8306. else
  8307. {
  8308. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8309. }
  8310. sqlite3_close(db);
  8311. }
  8312. }
  8313. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8314. {
  8315. char* errMsg = NULL;
  8316. char sqlStr[1024];
  8317. srand(time(NULL));
  8318. if(sqlite3_open(DB_FILE, &db))
  8319. {
  8320. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8321. sqlite3_close(db);
  8322. }
  8323. else
  8324. {
  8325. sprintf(sqlStr, "insert or replace into gun_consumption (connector, val) values(%d, %f);", gun_index, value);
  8326. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8327. {
  8328. LOG_ERROR("update gun_consumption error message: %s", errMsg);
  8329. }
  8330. else
  8331. {
  8332. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8333. }
  8334. sqlite3_close(db);
  8335. }
  8336. }
  8337. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index)
  8338. {
  8339. float result = 0;
  8340. char* errMsg = NULL;
  8341. char sqlStr[1024];
  8342. char **rs;
  8343. int rows, cols;
  8344. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  8345. if (sqlite3_open(DB_FILE, &db))
  8346. {
  8347. result = FAIL;
  8348. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8349. sqlite3_close(db);
  8350. }
  8351. else
  8352. {
  8353. //LOG_INFO("Local config query database open successfully.");
  8354. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8355. if (rows > 0)
  8356. {
  8357. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8358. {
  8359. result = atof(rs[(idxRow * cols) + 2]);
  8360. }
  8361. }
  8362. else
  8363. {
  8364. LOG_INFO("Query connector-%d fail, set default value to consumption.", gun_index);
  8365. }
  8366. sqlite3_free_table(rs);
  8367. sqlite3_close(db);
  8368. }
  8369. return result;
  8370. }
  8371. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index)
  8372. {
  8373. float result = 0;
  8374. char* errMsg = NULL;
  8375. char sqlStr[1024];
  8376. char **rs;
  8377. int rows, cols;
  8378. sprintf(sqlStr, "select * from gun_consumption where connector=%d;", gun_index);
  8379. if (sqlite3_open(DB_FILE, &db))
  8380. {
  8381. result = FAIL;
  8382. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8383. sqlite3_close(db);
  8384. }
  8385. else
  8386. {
  8387. //LOG_INFO("Local config query database open successfully.");
  8388. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8389. if (rows > 0)
  8390. {
  8391. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8392. {
  8393. result = atof(rs[(idxRow * cols) + 2]);
  8394. }
  8395. }
  8396. else
  8397. {
  8398. LOG_INFO("Query gun %d fail, set -1 to gun consumption.", gun_index + 1);
  8399. result = -1;
  8400. }
  8401. sqlite3_free_table(rs);
  8402. sqlite3_close(db);
  8403. }
  8404. return result;
  8405. }
  8406. float Get_PowerConsumption(uint8_t gun_index)
  8407. {
  8408. float value = 0;
  8409. //value = ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0 ?
  8410. // ShmChargerInfo->MeterValue[gun_index].GunConsumption : ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8411. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  8412. {
  8413. if(!_useGunMeterValue[gun_index])
  8414. {
  8415. LOG_INFO("***** Gun %d Use GunMeterValue *****", gun_index + 1);
  8416. }
  8417. _useGunMeterValue[gun_index] = true;
  8418. value = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  8419. }
  8420. else
  8421. {
  8422. value = ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8423. }
  8424. return value;
  8425. }
  8426. void Update_PowerConsumption(uint8_t gun_index)
  8427. {
  8428. chargingInfo[gun_index]->PowerConsumption = Get_PowerConsumption(gun_index);
  8429. }
  8430. int DB_Insert_FinalCost(sqlite3 *db, uint8_t gun_index, int txId, char *description)
  8431. {
  8432. char timeBuf[32];
  8433. int result = PASS;
  8434. char* errMsg = NULL;
  8435. char insertSql[1024];
  8436. getNowDatetime((unsigned char *)timeBuf);
  8437. sprintf(insertSql, "insert into final_cost(gun, transactionId, occurDatetime, updateDatetime, description, updateStatus) "
  8438. "values('%d', '%d', '%s', '%s', '%s', '%d');",
  8439. gun_index + 1,
  8440. txId,
  8441. timeBuf,
  8442. "",
  8443. description,
  8444. BILL_SATAUS_UNDISPOSED);
  8445. if(sqlite3_open(DB_FILE, &db))
  8446. {
  8447. result = FAIL;
  8448. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8449. sqlite3_close(db);
  8450. }
  8451. else
  8452. {
  8453. //LOG_INFO("FinalCost database open successfully.");
  8454. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8455. {
  8456. result = FAIL;
  8457. LOG_ERROR("Insert final_cost error message: %s", errMsg);
  8458. }
  8459. else
  8460. {
  8461. //LOG_INFO("Insert final_cost successfully");
  8462. }
  8463. sprintf(insertSql, "delete from final_cost where idx < (select idx from final_cost order by idx desc limit 1)-2000;");
  8464. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8465. {
  8466. result = FAIL;
  8467. LOG_ERROR("delete final_cost error message: %s", errMsg);
  8468. }
  8469. else
  8470. {
  8471. //LOG_INFO("delete final_cost successfully");
  8472. }
  8473. sqlite3_close(db);
  8474. }
  8475. return result;
  8476. }
  8477. int DB_Update_FinalCost_Status(sqlite3 *db, int txId, int status)
  8478. {
  8479. char timeBuf[32];
  8480. int result = PASS;
  8481. char* errMsg = NULL;
  8482. char updateSql[1024];
  8483. getNowDatetime((unsigned char *)timeBuf);
  8484. sprintf(updateSql, "update final_cost set updateStatus = %d, updateDatetime = '%s' where transactionId = '%d';",
  8485. status,
  8486. timeBuf,
  8487. txId);
  8488. if(sqlite3_open(DB_FILE, &db))
  8489. {
  8490. result = FAIL;
  8491. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8492. sqlite3_close(db);
  8493. }
  8494. else
  8495. {
  8496. if(sqlite3_exec(db, updateSql, 0, 0, &errMsg) != SQLITE_OK)
  8497. {
  8498. LOG_ERROR("update final_cost status error message: %s", errMsg);
  8499. result = FAIL;
  8500. }
  8501. else
  8502. {
  8503. //LOG_INFO("update final_cost txId[%d] status %d ok", txId, status);
  8504. }
  8505. sqlite3_close(db);
  8506. }
  8507. return result;
  8508. }
  8509. int DB_Get_Undisposed_FinalCost(sqlite3 *db, int gunIndex, char *costString)
  8510. {
  8511. int txId = -1;
  8512. char **rs;
  8513. int rows, cols;
  8514. char* errMsg = NULL;
  8515. char sqlString[1024];
  8516. sprintf(sqlString, "select * from final_cost where updateStatus = -1 and gun = '%d' order by idx asc limit 1;",
  8517. gunIndex + 1);
  8518. if(sqlite3_open(DB_FILE, &db))
  8519. {
  8520. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8521. sqlite3_close(db);
  8522. }
  8523. else
  8524. {
  8525. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  8526. if(rows > 0)
  8527. {
  8528. for(int idxRow = 1; idxRow <= rows; idxRow++)
  8529. {
  8530. txId = atoi(rs[(idxRow * cols) + 2]);
  8531. strcpy(costString, rs[(idxRow * cols) + 5]);
  8532. }
  8533. }
  8534. sqlite3_free_table(rs);
  8535. sqlite3_close(db);
  8536. }
  8537. return txId;
  8538. }
  8539. void CheckInProgress(uint8_t gun_index)
  8540. {
  8541. if((ShmChargerInfo->Connector[gun_index].Status > S_IDLE && ShmChargerInfo->Connector[gun_index].Status <= S_FAULT) ||
  8542. (ShmChargerInfo->Connector[gun_index].Status >= S_CCS_PRECHARGE_ST0 && ShmChargerInfo->Connector[gun_index].Status <= S_CCS_PRECHARGE_ST1))
  8543. {
  8544. if(chargingInfo[gun_index]->InProgress == NO)
  8545. {
  8546. LOG_INFO("Gun %d In Progress", gun_index + 1);
  8547. }
  8548. chargingInfo[gun_index]->InProgress = YES;
  8549. }
  8550. else
  8551. {
  8552. if(chargingInfo[gun_index]->InProgress == YES)
  8553. {
  8554. LOG_INFO("Gun %d Real Idle", gun_index + 1);
  8555. }
  8556. chargingInfo[gun_index]->InProgress = NO;
  8557. }
  8558. }
  8559. void CheckDebugMode(uint8_t gun_index)
  8560. {
  8561. if(ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay)
  8562. {
  8563. if(chargingInfo[gun_index]->SystemStatus != S_DEBUG)
  8564. {
  8565. setChargerMode(gun_index, MODE_DEBUG);
  8566. }
  8567. }
  8568. }
  8569. int DB_Reboot_Record(sqlite3 *db)
  8570. {
  8571. int result = PASS;
  8572. char* errMsg = NULL;
  8573. char insertSql[256];
  8574. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  8575. if(sqlite3_open(DB_FILE, &db))
  8576. {
  8577. result = FAIL;
  8578. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8579. sqlite3_close(db);
  8580. }
  8581. else
  8582. {
  8583. LOG_INFO("Local charging record database open successfully.");
  8584. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8585. {
  8586. result = FAIL;
  8587. LOG_ERROR("Insert reboot record error message: %s", errMsg);
  8588. }
  8589. else
  8590. {
  8591. //LOG_INFO("Insert reboot record successfully");
  8592. }
  8593. sprintf(insertSql, "delete from reboot_record where idx < (select idx from reboot_record order by idx desc limit 1)-2000;");
  8594. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8595. {
  8596. result = FAIL;
  8597. LOG_ERROR("delete reboot record error message: %s", errMsg);
  8598. }
  8599. else
  8600. {
  8601. //LOG_INFO("delete reboot record successfully");
  8602. }
  8603. sqlite3_close(db);
  8604. }
  8605. return result;
  8606. }
  8607. //===============================================
  8608. // Config process
  8609. //===============================================
  8610. void AddPlugInTimes(byte gunIndex)
  8611. {
  8612. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  8613. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  8614. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  8615. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  8616. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  8617. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  8618. }
  8619. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  8620. {
  8621. char cmdBuf[32];
  8622. struct timeb csuTime;
  8623. struct tm *tmCSU;
  8624. ftime(&csuTime);
  8625. tmCSU = localtime(&csuTime.time);
  8626. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  8627. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  8628. tmCSU->tm_sec);
  8629. if (isStart)
  8630. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  8631. else
  8632. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  8633. }
  8634. void zipLogFiles()
  8635. {
  8636. const char* logPath = "/Storage/SystemLog";
  8637. // 獲取目錄
  8638. DIR* pDir = opendir(logPath);
  8639. if (pDir != NULL)
  8640. {
  8641. struct timeb csuTime;
  8642. struct tm *tmCSU;
  8643. ftime(&csuTime);
  8644. tmCSU = localtime(&csuTime.time);
  8645. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  8646. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  8647. // tmCSU->tm_sec);
  8648. // Read items inside the folder
  8649. struct dirent* pEntry = NULL;
  8650. while ((pEntry = readdir(pDir)) != NULL)
  8651. {
  8652. if (strcmp(pEntry->d_name, ".") != 0 &&
  8653. strcmp(pEntry->d_name, "..") != 0 &&
  8654. strncmp(pEntry->d_name, "[", 1) == 0 &&
  8655. strstr(pEntry->d_name, "tar") < 0)
  8656. {
  8657. char yearC[5];
  8658. unsigned short year = 0;
  8659. char monthC[3];
  8660. unsigned short month = 0;
  8661. yearC[4] = '\0';
  8662. strncpy(yearC, pEntry->d_name + 1, 4);
  8663. monthC[2] = '\0';
  8664. strncpy(monthC, pEntry->d_name + 6, 2);
  8665. year = atoi(yearC);
  8666. month = atoi(monthC);
  8667. if (year != 0)
  8668. {
  8669. if (year < tmCSU->tm_year + 1900 ||
  8670. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  8671. {
  8672. LOG_INFO("tar file name : %s", pEntry->d_name);
  8673. char file[256];
  8674. memset(file, 0x00, sizeof(file));
  8675. strcat(file, "tar zcvf ");
  8676. strcat(file, logPath);
  8677. strncat(file, "/", 1);
  8678. strcat(file, pEntry->d_name);
  8679. strcat(file, ".tar");
  8680. strncat(file, " ", 1);
  8681. strcat(file, logPath);
  8682. strncat(file, "/", 1);
  8683. strcat(file, pEntry->d_name);
  8684. LOG_INFO("zip = %s \n", file);
  8685. system(file);
  8686. }
  8687. }
  8688. }
  8689. }
  8690. }
  8691. // Close folder
  8692. closedir(pDir);
  8693. }
  8694. void StopProcessingLoop()
  8695. {
  8696. for (;;)
  8697. {
  8698. CheckFactoryConfigFunction();
  8699. CheckFwUpdateFunction();
  8700. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  8701. {
  8702. ChkPrimaryStatus();
  8703. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0)
  8704. {
  8705. LOG_INFO("Soft reboot for retry self-tets (Primary).");
  8706. KillAllTask();
  8707. sleep(3);
  8708. TryCloseWatchdog();
  8709. system("/usr/bin/run_evse_restart.sh");
  8710. return;
  8711. }
  8712. }
  8713. if(Is_Ocpp_ResetReq())
  8714. {
  8715. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  8716. {
  8717. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = true;
  8718. }
  8719. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  8720. {
  8721. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  8722. }
  8723. }
  8724. CheckSoftHardReset();
  8725. sleep(1);
  8726. }
  8727. }
  8728. void CreateWatchdog(void)
  8729. {
  8730. wtdFd = InitWatchDog();
  8731. if(wtdFd < 0)
  8732. {
  8733. LOG_INFO("Watchdog Initial Fail");
  8734. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  8735. }
  8736. else
  8737. {
  8738. LOG_INFO("Watchdog Initial Success");
  8739. }
  8740. }
  8741. void TryCloseWatchdog(void)
  8742. {
  8743. if(wtdFd > 0)
  8744. {
  8745. write(wtdFd, "V", 1);
  8746. close(wtdFd);
  8747. }
  8748. }
  8749. void TryFeedWatchdog(void)
  8750. {
  8751. if(wtdFd > 0)
  8752. {
  8753. write(wtdFd, "a", 1);
  8754. }
  8755. }
  8756. bool IsParallelCabinetIdle(void)
  8757. {
  8758. bool result = true;
  8759. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  8760. {
  8761. result = false;
  8762. }
  8763. return result;
  8764. }
  8765. bool IsStandbyEnable(void)
  8766. {
  8767. bool result = true;
  8768. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  8769. {
  8770. time_t timep;
  8771. struct tm *tm;
  8772. time(&timep);
  8773. timep += (ShmChargerInfo->CabinetMiscValue.TimeOffset * 60);
  8774. tm = localtime(&timep);
  8775. if(tm->tm_hour >= 0 && tm->tm_hour < 8)
  8776. {
  8777. if(!_isStandbyEnable)
  8778. {
  8779. LOG_INFO("[TCC]Customer Enable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  8780. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  8781. }
  8782. _isStandbyEnable = true;
  8783. result = true;
  8784. }
  8785. else
  8786. {
  8787. if(_isStandbyEnable)
  8788. {
  8789. LOG_INFO("[TCC]Customer Disable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  8790. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  8791. }
  8792. _isStandbyEnable = false;
  8793. result = false;
  8794. }
  8795. }
  8796. return result;
  8797. }
  8798. bool IsGunIdle(int gunIndex, int idleType)
  8799. {
  8800. bool idle = true;
  8801. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  8802. chargingInfo[gunIndex]->ConnectorPlugIn &&
  8803. idleType == GUN_IDLE_TYPE_WHOLE)
  8804. {
  8805. idle = false;
  8806. }
  8807. else if (chargingInfo[gunIndex]->SystemStatus != S_IDLE &&
  8808. chargingInfo[gunIndex]->SystemStatus != S_RESERVATION &&
  8809. chargingInfo[gunIndex]->SystemStatus != S_FAULT &&
  8810. chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  8811. {
  8812. idle = false;
  8813. }
  8814. return idle;
  8815. }
  8816. bool IsDispenserIdle(int dispenserIndex)
  8817. {
  8818. bool dispenserIdle = true;
  8819. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  8820. {
  8821. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  8822. if(!IsGunIdle(gun, GUN_IDLE_TYPE_NORMAL))
  8823. {
  8824. dispenserIdle = false;
  8825. break;
  8826. }
  8827. }
  8828. return dispenserIdle;
  8829. }
  8830. bool IsConnectorWholeIdle(void)
  8831. {
  8832. bool result = true;
  8833. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  8834. {
  8835. if(!IsGunIdle(count, GUN_IDLE_TYPE_WHOLE))
  8836. {
  8837. result = false;
  8838. break;
  8839. }
  8840. }
  8841. return result;
  8842. }
  8843. bool IsUpgradeAvailable(void)
  8844. {
  8845. bool result = true;
  8846. for(int count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  8847. {
  8848. if(!IsGunIdle(count, GUN_IDLE_TYPE_NORMAL))
  8849. {
  8850. result = false;
  8851. break;
  8852. }
  8853. }
  8854. return result;
  8855. }
  8856. void ClearAlarmCodeWhenAcOff()
  8857. {
  8858. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  8859. {
  8860. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  8861. }
  8862. }
  8863. //==========================================
  8864. // Check task processing
  8865. //==========================================
  8866. void CheckTask()
  8867. {
  8868. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  8869. {
  8870. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  8871. {
  8872. if(system("pidof -s Module_4g > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  8873. {
  8874. LOG_ERROR("Module_4g not running, restart it.");
  8875. system("/root/Module_4g &");
  8876. }
  8877. }
  8878. }
  8879. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  8880. {
  8881. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  8882. {
  8883. if(system("pidof -s Module_Wifi > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  8884. {
  8885. LOG_ERROR("Module_Wifi not running, restart it.");
  8886. system("/root/Module_Wifi &");
  8887. }
  8888. }
  8889. }
  8890. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  8891. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  8892. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  8893. {
  8894. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = true;
  8895. if((time((time_t*)NULL) - Get_Ocpp_procDogTime()) > 180)
  8896. {
  8897. LOG_ERROR("OcppBackend watch dog timeout task restart.");
  8898. Clean_Ocpp_procDogTime();
  8899. Kill_Ocpp_Service();
  8900. }
  8901. if(!Is_Ocpp_Task_Alive())
  8902. {
  8903. LOG_ERROR("OcppBackend not running, restart it.");
  8904. Run_Ocpp_Service();
  8905. }
  8906. }
  8907. else
  8908. {
  8909. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = false;
  8910. }
  8911. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  8912. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  8913. {
  8914. if(!Is_MaintainOcpp_Task_Alive())
  8915. {
  8916. LOG_ERROR("Maintain Backend not running, restart it.");
  8917. Run_MaintainOcpp_Service();
  8918. }
  8919. }
  8920. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  8921. {
  8922. LOG_ERROR("Module_ProduceUtils not running, restart it.");
  8923. system ("/root/Module_ProduceUtils &");
  8924. }
  8925. if(system("pidof -s Module_EvComm > /dev/null") != 0)
  8926. {
  8927. LOG_ERROR("Module_EvComm not running, restart it.");
  8928. system ("/root/Module_EvComm &");
  8929. }
  8930. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  8931. {
  8932. LOG_ERROR("Module_InternalComm not running, restart it.");
  8933. system ("/root/Module_InternalComm &");
  8934. //ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  8935. }
  8936. if(!Is_WebServer_Alive())
  8937. {
  8938. LOG_ERROR("lighttpd not running, restart it.");
  8939. Run_WebServer_Service();
  8940. }
  8941. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  8942. {
  8943. if(!Is_LocalPowerSharing_Alive())
  8944. {
  8945. LOG_ERROR("Module_PowerSharing not running, restart it.");
  8946. Run_LocalPowerSharing_Service();
  8947. }
  8948. }
  8949. }
  8950. void InitialDHCP()
  8951. {
  8952. char tmpbuf[256];
  8953. memset(tmpbuf,0,256);
  8954. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  8955. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  8956. system(tmpbuf);
  8957. }
  8958. void InitialDispenserDhcpServerConfig(void)
  8959. {
  8960. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  8961. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  8962. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  8963. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  8964. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  8965. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  8966. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  8967. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  8968. usleep(1000);
  8969. }
  8970. void StartDispenserDhcpServer(void)
  8971. {
  8972. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  8973. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  8974. }
  8975. //==========================================
  8976. // Check Smart Charging Profile
  8977. //==========================================
  8978. void Ocpp_ProfileReq_Cmd(byte gunIndex)
  8979. {
  8980. if(!Is_Ocpp_ChargingProfileConf(gunIndex))
  8981. {
  8982. if(!Is_Ocpp_ChargingProfileReq(gunIndex))
  8983. {
  8984. Set_Ocpp_ChargingProfileReq(gunIndex);
  8985. }
  8986. }
  8987. }
  8988. int GetStartScheduleTime(unsigned char *time)
  8989. {
  8990. int result = -1;
  8991. struct tm tmScheduleStart;
  8992. struct timeb tbScheduleStart;
  8993. 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))
  8994. {
  8995. tmScheduleStart.tm_year -= 1900;
  8996. tmScheduleStart.tm_mon -= 1;
  8997. tbScheduleStart.time = mktime(&tmScheduleStart);
  8998. tbScheduleStart.millitm = 0;
  8999. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  9000. }
  9001. return result;
  9002. }
  9003. void Ocpp_ChargingProfile_Process(byte _index)
  9004. {
  9005. int _startCount = NO_DEFINE;
  9006. int _maxCount = 0;
  9007. float _limit = 0;
  9008. int _numberPhases = 0;
  9009. if(Is_Ocpp_ChargingProfileKind(_index, "Absolute") && Is_Ocpp_ChargingProfileId(_index))
  9010. {
  9011. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(_index);
  9012. _startCount = Get_Ocpp_ChargingProfileStartIndex(_index);
  9013. if(_startCount < _maxCount)
  9014. {
  9015. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9016. _numberPhases = Get_Ocpp_ChargingProfileScheduleNumberPhases(_index, _startCount);
  9017. chargingInfo[_index]->ChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9018. if(chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  9019. (int)chargingInfo[_index]->PresentChargingVoltage > 0)
  9020. {
  9021. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  9022. }
  9023. else
  9024. {
  9025. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9026. }
  9027. }
  9028. else
  9029. {
  9030. chargingInfo[_index]->ChargingProfilePower = -1;
  9031. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9032. }
  9033. }
  9034. else
  9035. {
  9036. chargingInfo[_index]->ChargingProfilePower = -1;
  9037. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9038. }
  9039. if((int)_lastProfilePower[_index] != (int)chargingInfo[_index]->ChargingProfilePower ||
  9040. (int)_lastProfileCurrent[_index] != (int)chargingInfo[_index]->ChargingProfileCurrent)
  9041. {
  9042. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9043. LOG_INFO("Gun %d Get Profile - Index = %d, Limit = %f",
  9044. _index + 1, _startCount, _limit);
  9045. LOG_INFO("Max: %d kW, Gun %d ChargingProfile Power = %d, Current = %d",
  9046. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower == -1 ? (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower : ((int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000),
  9047. _index + 1, (int)chargingInfo[_index]->ChargingProfilePower, (int)chargingInfo[_index]->ChargingProfileCurrent);
  9048. }
  9049. _lastProfilePower[_index] = chargingInfo[_index]->ChargingProfilePower;
  9050. _lastProfileCurrent[_index] = chargingInfo[_index]->ChargingProfileCurrent;
  9051. }
  9052. void Ocpp_MaxChargingProfile_Process(void)
  9053. {
  9054. int _startCount = NO_DEFINE;
  9055. int _maxCount = 0;
  9056. float _limit = 0;
  9057. int _numberPhases = 0;
  9058. if(Is_Ocpp_MaxChargingProfileKind("Absolute") && Is_Ocpp_MaxChargingProfileId())
  9059. {
  9060. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  9061. _startCount = Get_Ocpp_MaxProfileStartIndex();
  9062. if(_startCount < _maxCount)
  9063. {
  9064. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9065. _numberPhases = Get_Ocpp_MaxProfileScheduleNumberPhases(_startCount);
  9066. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9067. }
  9068. else
  9069. {
  9070. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9071. }
  9072. }
  9073. else
  9074. {
  9075. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9076. }
  9077. if((int)_lastMaxProfilePower != (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower)
  9078. {
  9079. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9080. LOG_INFO("Charger Get Max Profile - Index = %d, Limit = %f",
  9081. _startCount, _limit);
  9082. LOG_INFO("Charger Max Profile Power = %d", (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower);
  9083. }
  9084. _lastMaxProfilePower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower;
  9085. }
  9086. void CheckSmartChargeProfile(byte _index)
  9087. {
  9088. if(Is_Ocpp_ChargingProfileConf(_index))
  9089. {
  9090. Clean_Ocpp_ChargingProfileConf(_index);
  9091. Ocpp_MaxChargingProfile_Process();
  9092. Ocpp_ChargingProfile_Process(_index);
  9093. }
  9094. }
  9095. void GetLocalPowerLimit(byte gunIndex)
  9096. {
  9097. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9098. {
  9099. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  9100. {
  9101. float _localLimitPower = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gunIndex] * AC_OUTPUT_VOL;
  9102. if(chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 && (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  9103. {
  9104. chargingInfo[gunIndex]->LocalPowerLimitCurrent = _localLimitPower / chargingInfo[gunIndex]->PresentChargingVoltage;
  9105. }
  9106. else
  9107. {
  9108. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9109. }
  9110. }
  9111. else
  9112. {
  9113. chargingInfo[gunIndex]->LocalPowerLimitCurrent = 0;
  9114. }
  9115. }
  9116. else
  9117. {
  9118. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9119. }
  9120. }
  9121. void TheEndCharging(byte gun_index)
  9122. {
  9123. chargingInfo[gun_index]->isRemoteStart = NO;
  9124. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0 && ShmChargerInfo->UserTransaction[gun_index].TransactionId > 0)
  9125. {
  9126. char receiptString[512];
  9127. sprintf(receiptString, "%s%d",
  9128. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0], ShmChargerInfo->UserTransaction[gun_index].TransactionId);
  9129. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gun_index][0], receiptString);
  9130. Set_Connector_MiscCommand(gun_index, MISC_CONN_RECEIPT);
  9131. LOG_INFO("Gun %d ReceiptrURL: [%s]", gun_index + 1, receiptString);
  9132. }
  9133. ChangeStartOrStopDateTime(NO, gun_index);
  9134. DB_Insert_Record(localDb, gun_index);
  9135. }
  9136. void UpdateErrorCodeToOcpp(byte index)
  9137. {
  9138. //printf("ConnectorAlarmCode = %s \n", chargingInfo[index]->ConnectorAlarmCode);
  9139. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL)
  9140. {
  9141. SetOcppErrorCode(index, "InternalError");
  9142. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->ConnectorAlarmCode);
  9143. }
  9144. else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL)
  9145. {
  9146. SetOcppErrorCode(index, "OtherError");
  9147. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->EvConnAlarmCode);
  9148. }
  9149. }
  9150. void CheckMiscCommandRequirement(void)
  9151. {
  9152. BOOL NeedAnnouncement = FALSE;
  9153. unsigned char connector = 0;
  9154. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate != YES)
  9155. {
  9156. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9157. {
  9158. NeedAnnouncement = FALSE;
  9159. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  9160. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  9161. {
  9162. if(ShmChargerInfo->DispenserMiscReq[i].CtrlValue != 0)
  9163. {
  9164. NeedAnnouncement = TRUE;
  9165. }
  9166. for(int j = 0; j < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity; j++)
  9167. {
  9168. connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorID[j] - 1;
  9169. if(ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue != 0)
  9170. {
  9171. NeedAnnouncement = TRUE;
  9172. }
  9173. }
  9174. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement != NeedAnnouncement)
  9175. {
  9176. if(NeedAnnouncement)
  9177. {
  9178. //LOG_INFO("********** Dispenser %d Misc Command Need Announcement **********", i + 1);
  9179. }
  9180. else
  9181. {
  9182. //LOG_INFO("********** Dispenser %d Misc Command Announced **********", i + 1);
  9183. }
  9184. }
  9185. }
  9186. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  9187. }
  9188. }
  9189. }
  9190. void CheckDispenserVersionUpdateRequirement(void)
  9191. {
  9192. }
  9193. bool IsReadyParalleling(void)
  9194. {
  9195. bool ready = true;
  9196. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  9197. {
  9198. if(chargingInfo[i]->SystemStatus != S_IDLE && chargingInfo[i]->SystemStatus != S_MAINTAIN)
  9199. {
  9200. ready = false;
  9201. break;
  9202. }
  9203. }
  9204. return ready;
  9205. }
  9206. void SetParallelingStart(void)
  9207. {
  9208. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9209. {
  9210. if(chargingInfo[i]->SystemStatus == S_IDLE)
  9211. {
  9212. setChargerMode(i, MODE_MAINTAIN);
  9213. }
  9214. }
  9215. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Wait;
  9216. }
  9217. void SetParallelingDone(void)
  9218. {
  9219. unsigned short totalPow = 0;
  9220. totalPow = GetTotalParallelCabinetPower();
  9221. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9222. {
  9223. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9224. }
  9225. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9226. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_None;
  9227. }
  9228. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff)
  9229. {
  9230. unsigned char relay = 0;
  9231. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9232. {
  9233. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9234. {
  9235. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9236. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9237. {
  9238. relay = OnOff == YES ? YES : NO;
  9239. if(ShmChargerInfo->ParallelCabinet.PCabinet[index].OutputRelaySetting[index] != relay)
  9240. {
  9241. LOG_INFO("Set Parallel Cabinet Gun %d Output Relay %s", index + 1, relay == YES ? "ON" : "OFF");
  9242. }
  9243. ShmChargerInfo->ParallelCabinet.PCabinet[i].OutputRelaySetting[index] = relay;
  9244. }
  9245. }
  9246. }
  9247. }
  9248. void SetParallelCabinetAcContactor(unsigned char OnOff)
  9249. {
  9250. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9251. {
  9252. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9253. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9254. {
  9255. ShmChargerInfo->ParallelCabinet.PCabinet[i].AcContactorSetting = OnOff == YES ? YES : NO;
  9256. }
  9257. }
  9258. }
  9259. unsigned short GetTotalParallelCabinetPower(void)
  9260. {
  9261. unsigned short power = 0, slavePow = 0;
  9262. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9263. {
  9264. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  9265. {
  9266. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  9267. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  9268. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  9269. {
  9270. slavePow = ParsingRatingPower((char *)ShmChargerInfo->ParallelCabinet.PCabinet[i].ModelName);
  9271. power += slavePow;
  9272. }
  9273. }
  9274. }
  9275. return power;
  9276. }
  9277. unsigned char preParallelStatus = 0;
  9278. void MCabinetParallelingProcess(void)
  9279. {
  9280. bool PsuReInitial = false;
  9281. unsigned char status = 0;
  9282. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9283. {
  9284. if(IsReadyParalleling())
  9285. {
  9286. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9287. {
  9288. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest)
  9289. {
  9290. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest = false;
  9291. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm = true;
  9292. PsuReInitial = true;
  9293. }
  9294. }
  9295. if(PsuReInitial)
  9296. {
  9297. SetParallelingStart();
  9298. }
  9299. }
  9300. status = ShmChargerInfo->ParallelCabinet.ParallelStatus;
  9301. switch(status)
  9302. {
  9303. case _Parallel_None:
  9304. if(preParallelStatus != status)
  9305. {
  9306. LOG_INFO("MCabinet Parallel Status [None]");
  9307. GetClockTime(&_Parallel_Time);
  9308. }
  9309. unsigned short totalPow = 0;
  9310. totalPow = GetTotalParallelCabinetPower();
  9311. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9312. {
  9313. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9314. }
  9315. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9316. break;
  9317. case _Parallel_Wait:
  9318. if(preParallelStatus != status)
  9319. {
  9320. LOG_INFO("MCabinet Parallel Status [Wait]");
  9321. SetAcContactor(OFF);
  9322. SetParallelCabinetAcContactor(OFF);
  9323. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9324. {
  9325. SetPCabinetOutputRelay(i, OFF);
  9326. }
  9327. GetClockTime(&_Parallel_Time);
  9328. }
  9329. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9330. {
  9331. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Working;
  9332. }
  9333. break;
  9334. case _Parallel_Working:
  9335. if(preParallelStatus != status)
  9336. {
  9337. LOG_INFO("MCabinet Parallel Status [Working]");
  9338. SetAcContactor(ON);
  9339. SetParallelCabinetAcContactor(ON);
  9340. GetClockTime(&_Parallel_Time);
  9341. }
  9342. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  9343. {
  9344. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Done;
  9345. }
  9346. break;
  9347. case _Parallel_Done:
  9348. if(preParallelStatus != status)
  9349. {
  9350. LOG_INFO("MCabinet Parallel Status [Done]");
  9351. GetClockTime(&_Parallel_Time);
  9352. }
  9353. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9354. {
  9355. SetParallelingDone();
  9356. }
  9357. break;
  9358. }
  9359. preParallelStatus = status;
  9360. }
  9361. }
  9362. void SCabinetControllProcess(void)
  9363. {
  9364. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  9365. {
  9366. if(ShmChargerInfo->SCabinetControl.SAcContactor)
  9367. {
  9368. SetAcContactor(ON);
  9369. }
  9370. else
  9371. {
  9372. SetAcContactor(OFF);
  9373. }
  9374. unsigned short _parallelRelay = 0;
  9375. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9376. {
  9377. _parallelRelay = ShmChargerInfo->SCabinetControl.SParallelRelay[i] > 0 ? (1 < i) : 0;
  9378. }
  9379. if(_parallelRelay != ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue)
  9380. {
  9381. LOG_INFO("SCabinet Set Parallel Relay %X -> %X", ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue, _parallelRelay);
  9382. }
  9383. ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue = _parallelRelay;
  9384. }
  9385. }
  9386. void CheckConnectorDisconnectionRecovery(unsigned char connector)
  9387. {
  9388. unsigned char dispenser = 0;
  9389. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  9390. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  9391. {
  9392. LOG_INFO("Connector %d Disconnection Recovery", connector + 1);
  9393. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.Disconnection = false;
  9394. }
  9395. }
  9396. bool ConnectorRequestToCharging(unsigned char connector)
  9397. {
  9398. unsigned char role = 0;
  9399. bool done = false;
  9400. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9401. switch(role)
  9402. {
  9403. case _GROLE_IDLE:
  9404. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest)
  9405. {
  9406. LOG_INFO("Request Gun %d To Start Charging", connector + 1);
  9407. }
  9408. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest = true;
  9409. break;
  9410. case _GROLE_MASTER:
  9411. done = true;
  9412. break;
  9413. case _GROLE_SLAVE:
  9414. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest)
  9415. {
  9416. LOG_INFO("Gun %d Wait For Re-Grouping", connector + 1);
  9417. }
  9418. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest = true;
  9419. break;
  9420. case _GROLE_REQUEST_TO_CHARGING:
  9421. done = true;
  9422. break;
  9423. default:
  9424. break;
  9425. }
  9426. return done;
  9427. }
  9428. bool IsConnectorGroupingCompleted(unsigned char connector)
  9429. {
  9430. unsigned char role = 0;
  9431. bool completed = false;
  9432. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9433. switch(role)
  9434. {
  9435. case _GROLE_MASTER:
  9436. completed = true;
  9437. break;
  9438. default:
  9439. break;
  9440. }
  9441. return completed;
  9442. }
  9443. void SelfTestFailReason(void)
  9444. {
  9445. char reason[50];
  9446. memset(reason, 0x00, sizeof(reason));
  9447. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  9448. {
  9449. StatusCodeCompose(reason, "042251");
  9450. }
  9451. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail ||
  9452. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail ||
  9453. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9454. {
  9455. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail)
  9456. {
  9457. StatusCodeCompose(reason, "042280");
  9458. }
  9459. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9460. {
  9461. StatusCodeCompose(reason, "042281");
  9462. }
  9463. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail)
  9464. {
  9465. StatusCodeCompose(reason, "042282");
  9466. }
  9467. }
  9468. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail ||
  9469. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  9470. {
  9471. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail)
  9472. {
  9473. StatusCodeCompose(reason, "042285");
  9474. }
  9475. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  9476. {
  9477. StatusCodeCompose(reason, "042286");
  9478. }
  9479. }
  9480. else
  9481. {
  9482. StatusCodeCompose(reason, "Self Test Fail");
  9483. }
  9484. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  9485. {
  9486. SetOcppErrorCode(gun_index, "InternalError");
  9487. SetOcppVendorErrorCode(gun_index, reason);
  9488. setChargerMode(gun_index, MODE_MAINTAIN);
  9489. }
  9490. }
  9491. bool IsAvailableBackToIdle(int gun_index)
  9492. {
  9493. if(_plugInHold[gun_index] == false &&
  9494. (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false || chargingInfo[gun_index]->ConnectorPlugIn == false))
  9495. {
  9496. _plugInHold[gun_index] = true;
  9497. GetClockTime(&_PlugInHold_Time[gun_index]);
  9498. LOG_INFO ("Gun %d Plug In Hold Delay", gun_index + 1);
  9499. }
  9500. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle ||
  9501. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false ||
  9502. (!chargingInfo[gun_index]->ConnectorPlugIn && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm) ||
  9503. isResetStopChargeFlag(gun_index) == YES)
  9504. {
  9505. if(!_plugInHold[gun_index])
  9506. {
  9507. _plugInHold[gun_index] = true;
  9508. GetClockTime(&_PlugInHold_Time[gun_index]);
  9509. LOG_INFO ("Force Gun %d Plug In Hold Delay", gun_index + 1);
  9510. }
  9511. if((GetTimeoutValue(_PlugInHold_Time[gun_index]) / uSEC_VAL) >= PLUG_IN_HOLD_TIME)
  9512. {
  9513. return true;
  9514. }
  9515. }
  9516. return false;
  9517. }
  9518. void CheckGunAvailable(void)
  9519. {
  9520. if(!ShmChargerInfo->Control.SysCtrl.bits.PsuInit)
  9521. {
  9522. if(ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  9523. {
  9524. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9525. {
  9526. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  9527. {
  9528. ShmChargerInfo->Control.GunAvailable[i] = YES;
  9529. }
  9530. }
  9531. ShmChargerInfo->Control.SysCtrl.bits.PsuInit = true;
  9532. }
  9533. }
  9534. }
  9535. int presentChargedEnergyClear(unsigned char gun_index)
  9536. {
  9537. int result = FAIL;
  9538. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  9539. memset(chargingInfo[gun_index]->presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod) * sizeof(float));
  9540. result = PASS;
  9541. return result;
  9542. }
  9543. float presentChargedEnergyTotal(unsigned char gun_index)
  9544. {
  9545. float result = 0.0f;
  9546. for(int idx = 0; idx < ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod); idx++)
  9547. {
  9548. result += chargingInfo[gun_index]->presentChargedEnergyPeriod[idx];
  9549. }
  9550. return result;
  9551. }
  9552. int presentChargedEnergyUpdate(unsigned char gun_index, float kwh)
  9553. {
  9554. int result = FAIL;
  9555. time_t CurrentTime;
  9556. struct tm *tm;
  9557. CurrentTime = time(NULL);
  9558. tm=localtime(&CurrentTime);
  9559. chargingInfo[gun_index]->presentChargedEnergyPeriod[tm->tm_hour] += kwh;
  9560. return result;
  9561. }
  9562. void ChargedEnergySummation(unsigned char gun_index)
  9563. {
  9564. unsigned long time = 0;
  9565. time = GetSecTimeoutValue(_ChargedEnergy_Time[gun_index]);
  9566. if(time >= (chargingInfo[gun_index]->PresentChargedDuration + ENERAGY_INTERVAL))
  9567. {
  9568. chargingInfo[gun_index]->PresentChargedDuration++;
  9569. float changedEnergy = 0;
  9570. float _meterEnergy = 0;
  9571. float _calChangingEnergy = (chargingInfo[gun_index]->PresentChargingPower) / 3600;
  9572. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  9573. {
  9574. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= _lsatGunMeterValue[gun_index])
  9575. {
  9576. _meterEnergy = ShmChargerInfo->MeterValue[gun_index].GunConsumption - _lsatGunMeterValue[gun_index];
  9577. }
  9578. else
  9579. {
  9580. _meterEnergy = 0;
  9581. }
  9582. changedEnergy = _meterEnergy;
  9583. _lsatGunMeterValue[gun_index] = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  9584. }
  9585. else
  9586. {
  9587. changedEnergy = _calChangingEnergy;
  9588. }
  9589. if(ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  9590. {
  9591. chargingInfo[gun_index]->ChargingFee += changedEnergy * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  9592. }
  9593. presentChargedEnergyUpdate(gun_index, changedEnergy);
  9594. chargingInfo[gun_index]->PresentChargedEnergy += changedEnergy;
  9595. ShmChargerInfo->MeterValue[gun_index].LocalConsumption += changedEnergy;
  9596. }
  9597. }
  9598. void ChkConnectorAction(void)
  9599. {
  9600. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9601. {
  9602. if(ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel)
  9603. {
  9604. if(ShmChargerInfo->AllBill.OccupancyInfo[i].Status != _Parking_WaitToPay)
  9605. {
  9606. LOG_INFO("Gun %d Charging Canceled", i + 1);
  9607. Clean_Ocpp_TcciSerialNo(i);
  9608. }
  9609. else
  9610. {
  9611. LOG_INFO("Gun %d Occupancy Keep Counting", i + 1);
  9612. Set_OccupancyKeepCountingReq(i);
  9613. }
  9614. ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel = false;
  9615. }
  9616. }
  9617. }
  9618. void Update_DispenserAvailability(int dispenserIndex)
  9619. {
  9620. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  9621. {
  9622. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  9623. Set_Connector_MiscCommand(gun, MISC_CONN_AVAILABILITY);
  9624. }
  9625. }
  9626. void InitialDataBaseValue(void)
  9627. {
  9628. // Local DB
  9629. if(DB_Open(localDb) != PASS)
  9630. {
  9631. LOG_INFO("DB_Open fail.");
  9632. isDb_ready = false;
  9633. }
  9634. else
  9635. {
  9636. isDb_ready = true;
  9637. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9638. {
  9639. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  9640. LOG_INFO("Gun %d IsAvailable Initial: [%s]", _index + 1, chargingInfo[_index]->IsAvailable == YES ? "Enable" : "Disable");
  9641. }
  9642. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9643. {
  9644. ShmChargerInfo->MeterValue[_index].LocalConsumption = DB_Get_LocalConsumption(localDb, _index);
  9645. LOG_INFO("Local Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  9646. }
  9647. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9648. {
  9649. ShmChargerInfo->MeterValue[_index].GunConsumption = DB_Get_GunConsumption(localDb, _index);
  9650. LOG_INFO("Remote Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].GunConsumption);
  9651. }
  9652. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9653. {
  9654. Update_PowerConsumption(_index);
  9655. LOG_INFO("Gun %d Power Consumption Initial: [%.4f kWh]", _index + 1, chargingInfo[_index]->PowerConsumption);
  9656. }
  9657. DB_Reboot_Record(localDb);
  9658. }
  9659. if (NetworkDB_Open(networkDb) != PASS)
  9660. {
  9661. LOG_ERROR("NetworkDB_Open fail.");
  9662. }
  9663. }
  9664. float CalculateMaxOutputRatio(int gunIndex)
  9665. {
  9666. float ratio = 0;
  9667. int inUsingCnt = 0;
  9668. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9669. {
  9670. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  9671. {
  9672. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  9673. }
  9674. }
  9675. if(inUsingCnt != 0)
  9676. {
  9677. ratio = ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].Role == _GROLE_MASTER ?
  9678. ((float)ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].GunPsuQuantity / inUsingCnt) : 0;
  9679. }
  9680. ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio = ratio;
  9681. return ratio;
  9682. }
  9683. // return unit: 0.1kW
  9684. unsigned short GetModelNameLimitPower(int gunIndex)
  9685. {
  9686. unsigned short limitPower = 0;
  9687. float fPower = 0;
  9688. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9689. ShmChargerInfo->Control.ChargerRatingPower != 0)
  9690. {
  9691. fPower = (float)ShmChargerInfo->Control.ChargerRatingPower / 10;
  9692. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9693. {
  9694. fPower += (float)ShmChargerInfo->ParallelCabinet.TotalParallelPower / 10;
  9695. }
  9696. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9697. limitPower = (unsigned short)(fPower * 10);
  9698. }
  9699. return limitPower;
  9700. }
  9701. void GetWebPageConfigPowerLimit(int gunIndex)
  9702. {
  9703. unsigned short limitPower = 0;
  9704. float fPower = 0;
  9705. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9706. ShmSysConfigAndInfo->SysConfig.MaxChargingPower != 0)
  9707. {
  9708. fPower = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingPower;
  9709. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9710. limitPower = (unsigned short)(fPower * 10);
  9711. }
  9712. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower = limitPower;
  9713. }
  9714. void GetWebPageConfigCurrentLimit(int gunIndex)
  9715. {
  9716. unsigned short limitCurrent = 0;
  9717. float fCurrent = 0;
  9718. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9719. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent != 0)
  9720. {
  9721. fCurrent = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent;
  9722. fCurrent = fCurrent * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9723. limitCurrent = (unsigned short)(fCurrent * 10);
  9724. }
  9725. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent = limitCurrent;
  9726. }
  9727. void GetOffLineMaxChargingPowerLimit(int gunIndex)
  9728. {
  9729. unsigned short limitPower = 0;
  9730. float fPower = 0;
  9731. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9732. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower != 0 &&
  9733. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES)
  9734. {
  9735. fPower = ShmChargerInfo->OutputLimit.Offline_MaxChargingPower / 1000;
  9736. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9737. limitPower = (unsigned short)(fPower * 10);
  9738. }
  9739. ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] = limitPower * 100;
  9740. }
  9741. void GetMaxChargingProfileLimit(int gunIndex)
  9742. {
  9743. unsigned short limitPower = 0, limitCurrent = 0;
  9744. float fPower = 0;
  9745. // max charging profile
  9746. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9747. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower >= 0)
  9748. {
  9749. fPower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000;
  9750. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9751. limitPower = (unsigned short)(fPower * 10);
  9752. if (chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 &&
  9753. (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  9754. {
  9755. limitCurrent = (limitPower * 100 / chargingInfo[gunIndex]->PresentChargingVoltage) * 10;
  9756. }
  9757. }
  9758. ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] = limitPower * 100;
  9759. ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] = limitCurrent;
  9760. }
  9761. void GetMaxChargingLimit(int gunIndex)
  9762. {
  9763. GetWebPageConfigPowerLimit(gunIndex);
  9764. GetWebPageConfigCurrentLimit(gunIndex);
  9765. GetOffLineMaxChargingPowerLimit(gunIndex);
  9766. GetMaxChargingProfileLimit(gunIndex);
  9767. }
  9768. void GetGunOtpDerating(int gunIndex)
  9769. {
  9770. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp.isNeedDerating)
  9771. {
  9772. struct DERATING_BY_OTP *_derating;
  9773. _derating = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp;
  9774. if(_derating->deratingIndex < GUN_DERATING_COUNT)
  9775. {
  9776. int _connectorTemp = (int)chargingInfo[gunIndex]->ConnectorTemp - 60;
  9777. int _chillerTemp = (int)chargingInfo[gunIndex]->ChillerTemp - 60;
  9778. if(_connectorTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  9779. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  9780. {
  9781. LOG_INFO("Gun %d GunTemp %d is Derating(%d) to %d A",
  9782. gunIndex + 1,
  9783. _connectorTemp,
  9784. GunDeratingTemp[_derating->deratingIndex],
  9785. (_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  9786. _derating->deratingIndex++;
  9787. }
  9788. else if(_chillerTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  9789. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  9790. {
  9791. LOG_INFO("Gun %d ChillerTemp %d is Derating(%d) to %d A",
  9792. gunIndex + 1,
  9793. _chillerTemp,
  9794. GunDeratingTemp[_derating->deratingIndex],
  9795. (_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  9796. _derating->deratingIndex++;
  9797. }
  9798. }
  9799. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = _derating->deratingTargetCurrent[_derating->deratingIndex];
  9800. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  9801. }
  9802. else
  9803. {
  9804. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = 0;
  9805. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  9806. }
  9807. }
  9808. void CalculateGunAbsMaxPower(int gunIndex)
  9809. {
  9810. int maxValue = 0;
  9811. maxValue = (int)GetModelNameLimitPower(gunIndex);
  9812. ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] = maxValue != 0 ? (maxValue * 100) : 0;
  9813. }
  9814. void CalculateGunAbsMaxVoltage(int gunIndex)
  9815. {
  9816. int maxValue = 0;
  9817. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalVoltage;
  9818. ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] = maxValue;
  9819. }
  9820. void CalculateGunAbsMaxCurrent(int gunIndex)
  9821. {
  9822. int maxValue = 0;
  9823. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalCurrent;
  9824. ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] = maxValue;
  9825. }
  9826. void RestrictChargingCapability(int gunIndex, unsigned short *power, unsigned short *voltage, unsigned short *current)
  9827. {
  9828. unsigned short limitPower = 0;
  9829. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] != 0)
  9830. {
  9831. limitPower = ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] / 100;
  9832. if(limitPower != 0 && *power > limitPower)
  9833. {
  9834. *power = limitPower;
  9835. }
  9836. }
  9837. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] != 0 &&
  9838. *voltage > ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex])
  9839. {
  9840. *voltage = ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex];
  9841. }
  9842. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] != 0 &&
  9843. *current > ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex])
  9844. {
  9845. *current = ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex];
  9846. }
  9847. // restrict from WebPageConfig power
  9848. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower != 0)
  9849. {
  9850. if(*power > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower)
  9851. {
  9852. *power = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower;
  9853. }
  9854. }
  9855. // restrict from WebPageConfig current
  9856. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent != 0)
  9857. {
  9858. if(*current > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent)
  9859. {
  9860. *current = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent;
  9861. }
  9862. }
  9863. // restrict from Offline_MaxChargingPower
  9864. if(ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] != 0)
  9865. {
  9866. limitPower = ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] / 100;
  9867. if(limitPower != 0 && *power > limitPower)
  9868. {
  9869. *power = limitPower;
  9870. }
  9871. }
  9872. // restrict from MaxChargingProfile
  9873. if(ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] != 0)
  9874. {
  9875. limitPower = ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] / 100;
  9876. if(limitPower != 0 && *power > limitPower)
  9877. {
  9878. *power = limitPower;
  9879. }
  9880. }
  9881. // restrict from MaxChargingProfile
  9882. if(ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] != 0)
  9883. {
  9884. if(*current > ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex])
  9885. {
  9886. *current = ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex];
  9887. }
  9888. }
  9889. // restrict from ChargingProfile
  9890. if((chargingInfo[gunIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gunIndex]->SystemStatus <= S_CHARGING) ||
  9891. (chargingInfo[gunIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gunIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  9892. {
  9893. if(chargingInfo[gunIndex]->ChargingProfilePower >= 0 && *power > ((int)chargingInfo[gunIndex]->ChargingProfilePower / 100))
  9894. {
  9895. *power = (int)chargingInfo[gunIndex]->ChargingProfilePower / 100;
  9896. }
  9897. if(chargingInfo[gunIndex]->ChargingProfileCurrent >= 0 && *current > (int)chargingInfo[gunIndex]->ChargingProfileCurrent)
  9898. {
  9899. *current = (int)chargingInfo[gunIndex]->ChargingProfileCurrent;
  9900. }
  9901. }
  9902. // restrict from local power limit
  9903. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9904. {
  9905. if(chargingInfo[gunIndex]->LocalPowerLimitCurrent >= 0 && *current > (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10))
  9906. {
  9907. *current = (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10);
  9908. }
  9909. }
  9910. // restrict after otp
  9911. if(ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] != 0)
  9912. {
  9913. limitPower = ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] / 100;
  9914. if(limitPower != 0 && *power > limitPower)
  9915. {
  9916. *power = limitPower;
  9917. }
  9918. }
  9919. // restrict after otp
  9920. if(ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] != 0 &&
  9921. *current > ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex])
  9922. {
  9923. *current = ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex];
  9924. }
  9925. }
  9926. void UpdateChargingCapability(void)
  9927. {
  9928. unsigned short voltage = 0, current = 0, power = 0;
  9929. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower = Get_Ocpp_OffLineMaxChargingPower();
  9930. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  9931. {
  9932. voltage = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.MaximumChargingVoltage;
  9933. current = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingCurrent;
  9934. power = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingPower;
  9935. CalculateGunAbsMaxPower(i);
  9936. CalculateGunAbsMaxVoltage(i);
  9937. CalculateGunAbsMaxCurrent(i);
  9938. CalculateMaxOutputRatio(i);
  9939. GetMaxChargingLimit(i);
  9940. GetLocalPowerLimit(i);
  9941. GetGunOtpDerating(i);
  9942. RestrictChargingCapability(i, &power, &voltage, &current);
  9943. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityVoltage = voltage;
  9944. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityCurrent = current;
  9945. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityPower = power;
  9946. }
  9947. }
  9948. void GunErrorCollection(void)
  9949. {
  9950. bool _PsuProtectionAlarm = false;
  9951. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  9952. {
  9953. _PsuProtectionAlarm |= ShmChargerInfo->GunError[i].ErrFlag.bits.PsuGroupAllAlarmFault;
  9954. }
  9955. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = _PsuProtectionAlarm;
  9956. }
  9957. void UpdateGunPresentChargingPower(int gunIndex)
  9958. {
  9959. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Enable)
  9960. {
  9961. chargingInfo[gunIndex]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingCurrent))) / 1000 / 100);
  9962. }
  9963. else
  9964. {
  9965. chargingInfo[gunIndex]->PresentChargingPower = ((float)((chargingInfo[gunIndex]->PresentChargingVoltage) * (chargingInfo[gunIndex]->PresentChargingCurrent)) / 1000);
  9966. }
  9967. }
  9968. void CheckPsuLostQuantity(void)
  9969. {
  9970. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  9971. {
  9972. if(ShmChargerInfo->Control.PsuQuantityByModel == ShmPsuData->SystemPresentPsuQuantity)
  9973. {
  9974. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = false;
  9975. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = 0;
  9976. }
  9977. else
  9978. {
  9979. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = ShmChargerInfo->Control.PsuQuantityByModel > ShmPsuData->SystemPresentPsuQuantity ?
  9980. (ShmChargerInfo->Control.PsuQuantityByModel - ShmPsuData->SystemPresentPsuQuantity) : 0;
  9981. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch ||
  9982. _psuLostQuantity != ShmSysConfigAndInfo->SysInfo.PsuLostQuantity)
  9983. {
  9984. LOG_INFO("Psu Quantity is %d, it should be %d, Psu Lost: %d",
  9985. ShmPsuData->SystemPresentPsuQuantity, ShmChargerInfo->Control.PsuQuantityByModel,
  9986. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity);
  9987. }
  9988. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = true;
  9989. _psuLostQuantity = ShmSysConfigAndInfo->SysInfo.PsuLostQuantity;
  9990. }
  9991. }
  9992. }
  9993. int main(void)
  9994. {
  9995. unsigned long time = 0;
  9996. int _dispenser = 0;
  9997. _isStandbyEnable = true;
  9998. if(CreateShareMemory() == 0)
  9999. {
  10000. #ifdef SystemLogMessage
  10001. LOG_ERROR("CreatShareMemory NG");
  10002. #endif
  10003. if(ShmStatusCodeData!=NULL)
  10004. {
  10005. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  10006. }
  10007. sleep(5);
  10008. system("reboot -f");
  10009. sleep(5);
  10010. system("reboot -f");
  10011. }
  10012. LOG_INFO("\n");
  10013. LOG_INFO("===== Boot and Initialization start =====");
  10014. LOG_INFO("Main Fork PID = %d", getpid());
  10015. if (!InitialSystemDefaultConfig())
  10016. {
  10017. LOG_INFO("InitialSystemDefaultConfig NG");
  10018. StopProcessingLoop();
  10019. }
  10020. LOG_INFO("ModelName: [%s], SN: [%s], SW Version: [%s][%s]",
  10021. ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber, fwVersion, subVersion);
  10022. LOG_INFO("InitialShareMemoryInfo");
  10023. InitialShareMemoryInfo();
  10024. LOG_INFO("ChargerType (IEC or UL) = %d", ShmSysConfigAndInfo->SysInfo.ChargerType);
  10025. if(!InitialChargerSetting())
  10026. {
  10027. isModelNameMatch = false;
  10028. }
  10029. Initialization();
  10030. InitialDataBaseValue();
  10031. LOG_INFO("Spawn all Task.");
  10032. SpawnTask();
  10033. if (!isModelNameMatch)
  10034. {
  10035. LOG_INFO("Module Name & HW info none match.");
  10036. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  10037. // Module Name 與硬體對應不正確
  10038. LOG_ERROR("Module Name & HW info none match.");
  10039. sleep(3);
  10040. KillAllTask();
  10041. StopProcessingLoop();
  10042. }
  10043. LOG_INFO("Module Name & HW info correct. Initialize.......");
  10044. CreateTimeoutFork();
  10045. SelfTestRun();
  10046. while(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  10047. {
  10048. sleep(1);
  10049. }
  10050. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  10051. {
  10052. sleep(1);
  10053. SelfTestFailReason();
  10054. }
  10055. else
  10056. {
  10057. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10058. {
  10059. setChargerMode(gun_index, MODE_IDLE);
  10060. if(ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 ||
  10061. !ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  10062. {
  10063. ShmChargerInfo->Control.GunAvailable[gun_index] = YES;
  10064. }
  10065. }
  10066. }
  10067. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE)
  10068. {
  10069. SetAcContactor(ON);
  10070. SpawnOcppService();
  10071. SpawnMaintainOcppService();
  10072. }
  10073. else
  10074. {
  10075. InitSlaveCabinetEthernet();
  10076. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10077. {
  10078. setChargerMode(gun_index, MODE_MAINTAIN);
  10079. }
  10080. }
  10081. sleep(1);
  10082. CreateWatchdog();
  10083. // Main loop
  10084. LOG_INFO("%s \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_COMPLETE ?
  10085. "****************************Main Loop**********************************" :
  10086. "********************** SelfTest Fail **********************");
  10087. GetClockTime(&_cmdMainPriority_time);
  10088. for (;;)
  10089. {
  10090. CheckGunAvailable();
  10091. CheckOcppStatus();
  10092. ChkPrimaryStatus();
  10093. ChkPsuStatus();
  10094. PsuFailureResume();
  10095. ChkConnectorAction();
  10096. if(IsUpgradeAvailable())
  10097. {
  10098. CheckFactoryConfigFunction();
  10099. CheckFwUpdateFunction();
  10100. }
  10101. if(CheckFlashUpdate() != YES)
  10102. {
  10103. // return fork process
  10104. return 0;
  10105. }
  10106. CheckSoftHardReset();
  10107. StandbyCheck();
  10108. // 讀卡邏輯
  10109. //ScannerCardProcess();
  10110. AuthorizeTimeoutProcess();
  10111. // 當 AC 沒有搭上時,清除一些錯誤碼
  10112. ClearAlarmCodeWhenAcOff();
  10113. if ((GetTimeoutValue(_cmdMainPriority_time) / uSEC_VAL) > MAIN_PRIORITY_INTERVAL)
  10114. {
  10115. CheckTask();
  10116. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10117. {
  10118. if((chargingInfo[_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[_index]->SystemStatus <= S_CHARGING) ||
  10119. (chargingInfo[_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10120. {
  10121. if (chargingInfo[_index]->SystemStatus == S_CHARGING && _ocppProfileChkFlag[_index] == 12)
  10122. {
  10123. Ocpp_ProfileReq_Cmd(_index);
  10124. gunOutputVol[_index] = chargingInfo[_index]->PresentChargingVoltage;
  10125. _ocppProfileChkFlag[_index] = 0;
  10126. }
  10127. else if (chargingInfo[_index]->SystemStatus != S_CHARGING)
  10128. {
  10129. Ocpp_ProfileReq_Cmd(_index);
  10130. _ocppProfileChkFlag[_index] = 0;
  10131. }
  10132. else
  10133. {
  10134. _ocppProfileChkFlag[_index]++;
  10135. }
  10136. }
  10137. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  10138. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  10139. {
  10140. DB_Update_LocalConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  10141. if(ShmChargerInfo->MeterValue[_index].GunConsumption >= 0)
  10142. {
  10143. DB_Update_GunConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].GunConsumption);
  10144. }
  10145. }
  10146. }
  10147. CheckPsuLostQuantity();
  10148. GetClockTime(&_cmdMainPriority_time);
  10149. }
  10150. CheckMiscCommandRequirement();
  10151. CheckDispenserVersionUpdateRequirement();
  10152. MCabinetParallelingProcess();
  10153. SCabinetControllProcess();
  10154. UpdateChargingCapability();
  10155. GunErrorCollection();
  10156. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10157. {
  10158. //CheckGpioInStatus();
  10159. CheckErrorOccurStatus(gun_index);
  10160. ChkOcppStatus(gun_index);
  10161. if((chargingInfo[gun_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= S_CHARGING) ||
  10162. (chargingInfo[gun_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10163. {
  10164. if(chargingInfo[gun_index]->SystemStatus == S_CHARGING)
  10165. {
  10166. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  10167. if (chargingInfo[gun_index]->PresentChargingVoltage >= gunOutputVol[gun_index] + 10 ||
  10168. chargingInfo[gun_index]->PresentChargingVoltage <= gunOutputVol[gun_index] - 10)
  10169. {
  10170. _ocppProfileChkFlag[gun_index] = 12;
  10171. gunOutputVol[gun_index] = chargingInfo[gun_index]->PresentChargingVoltage;
  10172. }
  10173. }
  10174. }
  10175. CheckSmartChargeProfile(gun_index);
  10176. Update_PowerConsumption(gun_index);
  10177. CheckInProgress(gun_index);
  10178. CheckDebugMode(gun_index);
  10179. //LOG_INFO("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  10180. switch(chargingInfo[gun_index]->SystemStatus)
  10181. {
  10182. case S_MAINTAIN:
  10183. if(isModeChange(gun_index))
  10184. {
  10185. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10186. {
  10187. LOG_INFO("================== Slave Cabinet %d ==================", ShmChargerInfo->Control.CabinetSwitch - 1);
  10188. }
  10189. LOG_INFO("================== S_MAINTAIN (%x) ================== \n", gun_index + 1);
  10190. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10191. GetClockTime(&_SystemStatus_Time[gun_index]);
  10192. }
  10193. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  10194. if(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE ||
  10195. ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  10196. ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10197. {
  10198. break;
  10199. }
  10200. if(chargingInfo[gun_index]->IsAvailable == YES &&
  10201. ShmChargerInfo->Control.GunAvailable[gun_index] == YES &&
  10202. ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  10203. {
  10204. if(time >= MAINTAIN_RECOVERY_TIME)
  10205. {
  10206. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10207. setChargerMode(gun_index, MODE_IDLE);
  10208. }
  10209. }
  10210. break;
  10211. case S_RESERVATION:
  10212. if(isModeChange(gun_index))
  10213. {
  10214. LOG_INFO("================== S_RESERVATION (%x) ================== \n", gun_index + 1);
  10215. char expiryDate[128];
  10216. Get_Ocpp_ReserveNowExpiryDate(gun_index, expiryDate);
  10217. Get_Ocpp_ReserveNowIdTag(gun_index, &ShmChargerInfo->Control.ResevedIdTag[gun_index][0]);
  10218. Set_Connector_MiscCommand(gun_index, MISC_CONN_RESERVATION);
  10219. LOG_INFO("****** Reservation IdTag: [%s], Expiry: [%s]", &ShmChargerInfo->Control.ResevedIdTag[gun_index][0], expiryDate);
  10220. GetClockTime(&_SystemStatus_Time[gun_index]);
  10221. }
  10222. if(chargingInfo[gun_index]->ConnectorPlugIn)
  10223. {
  10224. SetAcContactor(ON);
  10225. }
  10226. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  10227. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  10228. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  10229. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10230. {
  10231. UpdateErrorCodeToOcpp(gun_index);
  10232. setChargerMode(gun_index, MODE_FAULT);
  10233. continue;
  10234. }
  10235. if(Is_Ocpp_ReserveNowExpired(gun_index))
  10236. {
  10237. chargingInfo[gun_index]->ReservationId = -1;
  10238. LOG_INFO("Gun %d reservation is expired...", gun_index + 1);
  10239. setChargerMode(gun_index, MODE_IDLE);
  10240. }
  10241. else
  10242. {
  10243. if(IsConnectorAuthorizeSuccess(gun_index))
  10244. {
  10245. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10246. {
  10247. LOG_INFO("Gun %d remote start reserve charging", gun_index + 1);
  10248. }
  10249. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  10250. {
  10251. LOG_INFO("Gun %d swipe rfid start reserve charging", gun_index + 1);
  10252. }
  10253. setChargerMode(gun_index, MODE_AUTHORIZING);
  10254. }
  10255. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  10256. {
  10257. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  10258. if(Is_Ocpp_ReserveNowIdTag(gun_index, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId))
  10259. {
  10260. SetAcContactor(ON);
  10261. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  10262. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10263. {
  10264. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  10265. LOG_INFO("Gun %d reserve Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  10266. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  10267. {
  10268. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  10269. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  10270. }
  10271. DispenserAuthInitial(_dispenser);
  10272. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10273. }
  10274. }
  10275. }
  10276. else
  10277. {
  10278. if(chargingInfo[gun_index]->ReservationId == -1)
  10279. {
  10280. LOG_INFO("Gun %d reservation is clean", gun_index + 1);
  10281. setChargerMode(gun_index, MODE_IDLE);
  10282. }
  10283. }
  10284. }
  10285. break;
  10286. case S_IDLE:
  10287. case S_FAULT:
  10288. {
  10289. // clean PermissionRequest flag
  10290. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  10291. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  10292. if(isModeChange(gun_index))
  10293. {
  10294. if (chargingInfo[gun_index]->SystemStatus == S_IDLE)
  10295. {
  10296. LOG_INFO("================== S_IDLE (%x) ================== \n", gun_index + 1);
  10297. chargingInfo[gun_index]->ReservationId = -1;
  10298. _triggerTransaction[gun_index] = false;
  10299. _plugInHold[gun_index] = false;
  10300. chargingInfo[gun_index]->PresentChargedDuration = 0;
  10301. chargingInfo[gun_index]->RemainChargingDuration = 0;
  10302. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  10303. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  10304. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  10305. memset(ShmChargerInfo->Timestamp[gun_index].StartCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StartCharging));
  10306. memset(ShmChargerInfo->Timestamp[gun_index].StopCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StopCharging));
  10307. Clean_Ocpp_StopReason(gun_index);
  10308. ReleaseAlarmCode(gun_index);
  10309. // clean connector information
  10310. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  10311. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  10312. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  10313. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  10314. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  10315. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  10316. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -65535;
  10317. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].EnergyCost = -1;
  10318. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParkingFee = -1;
  10319. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  10320. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.GetStartChargingSoc = false;
  10321. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  10322. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  10323. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  10324. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.OutputLimitEnable = false;
  10325. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].GeneralChargingData.deratingByConnOtp.deratingIndex = 0;
  10326. chargingInfo[gun_index]->EvBatterytargetVoltage = 0;
  10327. chargingInfo[gun_index]->EvBatterytargetCurrent = 0;
  10328. chargingInfo[gun_index]->EvBatteryMaxVoltage = 0;
  10329. chargingInfo[gun_index]->EvBatterySoc = 0;
  10330. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetVoltage = 0;
  10331. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetCurrent = 0;
  10332. // clean RemoteStart & RemoteStop flag
  10333. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_START);
  10334. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_STOP);
  10335. Clean_Connector_MiscCommand(gun_index, MISC_CONN_UNLOCK);
  10336. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  10337. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopConfirm = false;
  10338. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopConfirm = false;
  10339. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NormalStopRequest = false;
  10340. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AlarmStopRequest = false;
  10341. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  10342. ShmChargerInfo->GunError[gun_index].ErrFlag.ErrorValue = 0;
  10343. memset(chargingInfo[gun_index]->ConnectorAlarmCode, 0x00, sizeof(chargingInfo[gun_index]->ConnectorAlarmCode));
  10344. Clean_Ocpp_RunningCost(gun_index);
  10345. Clean_Ocpp_FinalCost(gun_index);
  10346. Clean_DeductInfo(gun_index);
  10347. Clean_Ocpp_TcciSerialNo(gun_index);
  10348. Clean_UserTransaction(gun_index);
  10349. Clean_UserPriceAndReceipt(gun_index);
  10350. presentChargedEnergyClear(gun_index);
  10351. chargingInfo[gun_index]->PresentChargingPower = 0;
  10352. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower = 0;
  10353. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent = 0;
  10354. chargingInfo[gun_index]->ChargingProfilePower = -1;
  10355. chargingInfo[gun_index]->ChargingProfileCurrent = -1;
  10356. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  10357. {
  10358. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging = false;
  10359. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger = false;
  10360. // reset dispenser availability
  10361. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  10362. }
  10363. // clean force charging info
  10364. memset(&ShmChargerInfo->Control.FCharging[gun_index], 0x00, sizeof(ForceCharging));
  10365. }
  10366. else if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  10367. {
  10368. LOG_INFO("================== S_FAULT (%x) ================== \n", gun_index + 1);
  10369. }
  10370. GetClockTime(&_SystemStatus_Time[gun_index]);
  10371. }
  10372. if (chargingInfo[gun_index]->IsAvailable == NO ||
  10373. ShmChargerInfo->Control.GunAvailable[gun_index] == NO)
  10374. {
  10375. setChargerMode(gun_index, MODE_MAINTAIN);
  10376. continue;
  10377. }
  10378. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  10379. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  10380. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  10381. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10382. {
  10383. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest)
  10384. {
  10385. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  10386. {
  10387. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemotenAlarmCode, 6);
  10388. }
  10389. }
  10390. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  10391. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10392. {
  10393. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  10394. {
  10395. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042304", 6);
  10396. LOG_INFO("Connector %d Disconnect Occur", gun_index + 1);
  10397. }
  10398. }
  10399. if(chargingInfo[gun_index]->SystemStatus != S_FAULT)
  10400. {
  10401. UpdateErrorCodeToOcpp(gun_index);
  10402. setChargerMode(gun_index, MODE_FAULT);
  10403. }
  10404. else
  10405. {
  10406. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10407. {
  10408. CheckConnectorDisconnectionRecovery(gun_index);
  10409. }
  10410. }
  10411. }
  10412. else
  10413. {
  10414. if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  10415. {
  10416. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  10417. if(time >= FAULT_RELEASE_TIME)
  10418. {
  10419. ReleaseAlarmCode(gun_index);
  10420. setChargerMode(gun_index, MODE_IDLE);
  10421. }
  10422. break;
  10423. }
  10424. // clean stop charge flag
  10425. chargingInfo[gun_index]->StopChargeFlag = false;
  10426. chargingInfo[gun_index]->ChargingStopFlag.ChargingStopValue = 0;
  10427. if(chargingInfo[gun_index]->ConnectorPlugIn)
  10428. {
  10429. SetAcContactor(ON);
  10430. }
  10431. { // Idle 正常程序起點
  10432. // 判斷是否有啟用檢查插槍
  10433. if(IsConnectorAuthorizeSuccess(gun_index))
  10434. {
  10435. if(chargingInfo[gun_index]->IsAvailable &&
  10436. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  10437. {
  10438. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10439. {
  10440. LOG_INFO("Gun %d remote start charging", gun_index + 1);
  10441. Clean_Ocpp_TcciCreditInfo(gun_index);
  10442. }
  10443. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  10444. {
  10445. LOG_INFO("Gun %d swipe rfid start charging", gun_index + 1);
  10446. }
  10447. setChargerMode(gun_index, MODE_AUTHORIZING);
  10448. continue;
  10449. }
  10450. }
  10451. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  10452. {
  10453. SetAcContactor(ON);
  10454. if(chargingInfo[gun_index]->IsAvailable &&
  10455. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  10456. chargingInfo[gun_index]->ConnectorPlugIn &&
  10457. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10458. {
  10459. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  10460. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  10461. LOG_INFO("Gun %d Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  10462. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  10463. {
  10464. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  10465. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  10466. }
  10467. DispenserAuthInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  10468. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10469. continue;
  10470. }
  10471. }
  10472. // only for test & debug purpose
  10473. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging &&
  10474. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging)
  10475. {
  10476. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging = false;
  10477. strcpy((char *)chargingInfo[gun_index]->StartUserId, "ChargingTest");
  10478. LOG_INFO("Gun %d Start Force Charging%s",
  10479. gun_index + 1,
  10480. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger > 0 ? ", Triggered by WebAPI" : "");
  10481. if(ShmChargerInfo->Control.TestCtrl.bits.FChargingReleaseExtend)
  10482. {
  10483. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableReleaseAndExtend = true;
  10484. LOG_INFO("Gun %d Enable ReleaseAndExtend At Force Charging", gun_index + 1);
  10485. }
  10486. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  10487. {
  10488. LOG_INFO("Set Dispenser %d Inoperative",
  10489. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex + 1);
  10490. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  10491. }
  10492. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10493. continue;
  10494. }
  10495. else
  10496. {
  10497. }
  10498. } // Idle 正常程序終點
  10499. }
  10500. }
  10501. break;
  10502. case S_AUTHORIZING:
  10503. if(isModeChange(gun_index))
  10504. {
  10505. LOG_INFO("================== S_AUTHORIZING (%x) ================== \n", gun_index + 1);
  10506. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10507. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  10508. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  10509. //StopSystemTimeoutDet();
  10510. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  10511. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  10512. SetAcContactor(ON);
  10513. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  10514. }
  10515. if(!_triggerTransaction[gun_index] &&
  10516. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC &&
  10517. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10518. {
  10519. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= STARTTRANSATION_TIME)
  10520. {
  10521. OcppStartTransaction(gun_index);
  10522. }
  10523. }
  10524. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  10525. {
  10526. FouceAnnounceAccountBalance(gun_index);
  10527. }
  10528. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm)
  10529. {
  10530. // sync with dispenser
  10531. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  10532. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  10533. LOG_INFO("Sync with Gun %d through remote start", gun_index + 1);
  10534. FouceAnnounceAccountBalance(gun_index);
  10535. }
  10536. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm)
  10537. {
  10538. // sync with dispenser
  10539. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm = false;
  10540. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  10541. LOG_INFO("Sync with Connector %d through swipe rfid", gun_index + 1);
  10542. }
  10543. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  10544. {
  10545. if(!chargingInfo[gun_index]->ConnectorPlugIn)
  10546. {
  10547. LOG_INFO("*********** Gun %d Plug In Timeout ***********", gun_index + 1);
  10548. }
  10549. else
  10550. {
  10551. LOG_INFO("*********** Gun %d Preparing State Timeout ***********", gun_index + 1);
  10552. }
  10553. OcppStopTransaction(gun_index);
  10554. setChargerMode(gun_index, MODE_IDLE);
  10555. }
  10556. else
  10557. {
  10558. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  10559. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10560. {
  10561. AddPlugInTimes(gun_index);
  10562. LOG_INFO("Gun %d CardNumber = [%s]", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  10563. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10564. GunAuthInitial(gun_index);
  10565. }
  10566. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  10567. {
  10568. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  10569. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  10570. LOG_INFO("*********** Gun %d User Stop Plug In ***********", gun_index + 1);
  10571. OcppStopTransaction(gun_index);
  10572. setChargerMode(gun_index, MODE_IDLE);
  10573. }
  10574. else if(isEvBoardStopChargeFlag(gun_index) == YES)
  10575. {
  10576. LOG_INFO("********** Gun %d Alarm Stop (S_AUTHORIZING) **********", gun_index + 1);
  10577. OcppStopTransaction(gun_index);
  10578. setChargerMode(gun_index, MODE_IDLE);
  10579. }
  10580. }
  10581. break;
  10582. case S_REASSIGN_CHECK:
  10583. {
  10584. if (isModeChange(gun_index))
  10585. {
  10586. LOG_INFO("================== S_REASSIGN_CHECK (%x) ================== \n", gun_index + 1);
  10587. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10588. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  10589. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  10590. //StopSystemTimeoutDet();
  10591. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  10592. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy = ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy;
  10593. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration = ShmSysConfigAndInfo->SysConfig.MaxChargingDuration;
  10594. LOG_INFO("Max Total Current %d A, Max Total Power %d kW, Total Energy %d kWh, Total Duration %d",
  10595. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent,
  10596. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  10597. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy,
  10598. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration);
  10599. SetAcContactor(ON);
  10600. SetPCabinetOutputRelay(gun_index, ON);
  10601. GetClockTime(&_SystemStatus_Time[gun_index]);
  10602. }
  10603. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10604. if(ConnectorRequestToCharging(gun_index))
  10605. {
  10606. setChargerMode(gun_index, MODE_REASSIGN);
  10607. }
  10608. else if(time >= WAIT_REASSIGN_TIME)
  10609. {
  10610. if(ShmPsuData->Work_Step != _WORK_CHARGING)
  10611. {
  10612. LOG_INFO("Gun %d Wait PSU Work Step Timeout", gun_index + 1);
  10613. }
  10614. else
  10615. {
  10616. LOG_INFO("Gun %d Request Charging Timeout", gun_index + 1);
  10617. }
  10618. setChargerMode(gun_index, MODE_IDLE);
  10619. }
  10620. }
  10621. break;
  10622. case S_REASSIGN:
  10623. {
  10624. if (isModeChange(gun_index))
  10625. {
  10626. LOG_INFO("================== S_REASSIGN (%x) ================== \n", gun_index + 1);
  10627. //gettimeofday(&_toAverage_time, NULL);
  10628. GetClockTime(&_SystemStatus_Time[gun_index]);
  10629. }
  10630. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10631. if(IsConnectorGroupingCompleted(gun_index))
  10632. {
  10633. setChargerMode(gun_index, MODE_PRECHARGE);
  10634. }
  10635. else if(time >= MAX_REQUEST_CHARGING_TIME)
  10636. {
  10637. LOG_INFO("Gun %d Grouping Timeout", gun_index + 1);
  10638. setChargerMode(gun_index, MODE_IDLE);
  10639. }
  10640. }
  10641. break;
  10642. case S_PREPARNING:
  10643. {
  10644. if (isModeChange(gun_index))
  10645. {
  10646. LOG_INFO("================== S_PREPARNING (%x) ================== \n", gun_index + 1);
  10647. //StopGunInfoTimeoutDet(gun_index);
  10648. //StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  10649. SetAcContactor(ON);
  10650. GetClockTime(&_SystemStatus_Time[gun_index]);
  10651. }
  10652. UpdateGunPresentChargingPower(gun_index);
  10653. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10654. if ((chargingInfo[gun_index]->AvailableChargingPower > 10) && time >= MIN_WAIT_PREPARNING_TIME)
  10655. {
  10656. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  10657. break;;
  10658. }
  10659. if (isEvBoardStopChargeFlag(gun_index) == YES)
  10660. {
  10661. // 板端或後臺要求停止
  10662. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARNING) **********", gun_index + 1);
  10663. Set_Ocpp_StopReason(gun_index, "Local");
  10664. ChargingAlarmProcess(gun_index);
  10665. }
  10666. else if(isNormalStopChargeFlag(gun_index) == YES)
  10667. {
  10668. LOG_INFO("********** Gun %d Normal Stop (S_PREPARNING) %s**********", gun_index + 1,
  10669. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10670. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10671. ChargingTerminalProcess(gun_index);
  10672. }
  10673. else if(isResetStopChargeFlag(gun_index) == YES)
  10674. {
  10675. LOG_INFO("********** Gun %d Reset Stop (S_PREPARNING) %s**********", gun_index + 1,
  10676. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10677. char *reason[16];
  10678. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10679. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10680. ChargingTerminalProcess(gun_index);
  10681. }
  10682. else if(isBackendStopFlag(gun_index))
  10683. {
  10684. LOG_INFO("********** Gun %d Backend Stop (S_PREPARNING) **********", gun_index + 1);
  10685. Set_Ocpp_StopReason(gun_index, "Other");
  10686. ChargingTerminalProcess(gun_index);
  10687. }
  10688. if(time > GUN_PREPARE_TIMEOUT)
  10689. {
  10690. LOG_INFO("********** Gun %d PrepareTimeout **********", gun_index + 1);
  10691. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  10692. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  10693. {
  10694. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "012279", 6);
  10695. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  10696. }
  10697. Set_Ocpp_StopReason(gun_index, "Local");
  10698. ChargingAlarmProcess(gun_index);
  10699. }
  10700. }
  10701. break;
  10702. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  10703. {
  10704. if (isModeChange(gun_index))
  10705. {
  10706. LOG_INFO("================== S_PREPARING_FOR_EV (%x) ================== \n", gun_index + 1);
  10707. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  10708. //StopGunInfoTimeoutDet(gun_index);
  10709. //StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  10710. GetClockTime(&_SystemStatus_Time[gun_index]);
  10711. }
  10712. UpdateGunPresentChargingPower(gun_index);
  10713. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10714. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  10715. {
  10716. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  10717. }
  10718. // only for test & debug purpose
  10719. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10720. {
  10721. LOG_INFO("Gun %d [Force Charging] S_PREPARING_FOR_EV OK", gun_index + 1);
  10722. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = true;
  10723. }
  10724. if (isEvBoardStopChargeFlag(gun_index) == YES)
  10725. {
  10726. // 板端或後臺要求停止
  10727. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  10728. Set_Ocpp_StopReason(gun_index, "Local");
  10729. ChargingAlarmProcess(gun_index);
  10730. }
  10731. else if(isNormalStopChargeFlag(gun_index) == YES)
  10732. {
  10733. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  10734. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10735. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10736. ChargingTerminalProcess(gun_index);
  10737. }
  10738. else if(isResetStopChargeFlag(gun_index) == YES)
  10739. {
  10740. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  10741. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10742. char *reason[16];
  10743. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10744. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10745. ChargingTerminalProcess(gun_index);
  10746. }
  10747. else if(isBackendStopFlag(gun_index))
  10748. {
  10749. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  10750. Set_Ocpp_StopReason(gun_index, "Other");
  10751. ChargingTerminalProcess(gun_index);
  10752. }
  10753. if(time >= GUN_EV_WAIT_TIMEOUT)
  10754. {
  10755. LOG_INFO("********** Gun %d Wait Dispenser Timeout **********", gun_index + 1);
  10756. ChargingTerminalProcess(gun_index);
  10757. }
  10758. }
  10759. break;
  10760. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  10761. {
  10762. if (isModeChange(gun_index))
  10763. {
  10764. LOG_INFO("================== S_PREPARING_FOR_EVSE (%x) ================== \n", gun_index + 1);
  10765. GetClockTime(&_SystemStatus_Time[gun_index]);
  10766. #ifdef STARTTRANSATION_IMMEDIATELY
  10767. OcppStartTransaction(gun_index);
  10768. #endif
  10769. }
  10770. CheckoutStartChargingSoc(gun_index, NO);
  10771. UpdateGunPresentChargingPower(gun_index);
  10772. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10773. // only for test & debug purpose
  10774. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10775. {
  10776. if(time >= FORCE_CHARGING_CMD_DELAY)
  10777. {
  10778. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  10779. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  10780. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  10781. {
  10782. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  10783. }
  10784. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  10785. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  10786. }
  10787. }
  10788. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Charging)
  10789. {
  10790. setChargerMode(gun_index, MODE_CHARGING);
  10791. }
  10792. // only for test & debug purpose
  10793. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10794. {
  10795. if(time >= FORCE_CHARGING_WAIT_EVSE)
  10796. {
  10797. LOG_INFO("Gun %d [Charging Simulation] S_PREPARING_FOR_EVSE OK", gun_index + 1);
  10798. setChargerMode(gun_index, MODE_CHARGING);
  10799. }
  10800. }
  10801. if (isEvBoardStopChargeFlag(gun_index) == YES)
  10802. {
  10803. // 板端或後臺要求停止
  10804. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  10805. Set_Ocpp_StopReason(gun_index, "Local");
  10806. ChargingAlarmProcess(gun_index);
  10807. }
  10808. else if(isNormalStopChargeFlag(gun_index) == YES)
  10809. {
  10810. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  10811. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10812. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10813. ChargingTerminalProcess(gun_index);
  10814. }
  10815. else if(isResetStopChargeFlag(gun_index) == YES)
  10816. {
  10817. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  10818. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10819. char *reason[16];
  10820. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10821. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10822. ChargingTerminalProcess(gun_index);
  10823. }
  10824. else if(isInvalidIdStopFlag(gun_index) == YES)
  10825. {
  10826. LOG_INFO("********** Gun %d InvalidId Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  10827. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  10828. ChargingTerminalProcess(gun_index);
  10829. }
  10830. else if(isBackendStopFlag(gun_index))
  10831. {
  10832. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  10833. Set_Ocpp_StopReason(gun_index, "Other");
  10834. ChargingTerminalProcess(gun_index);
  10835. }
  10836. if(time >= GUN_EV_WAIT_TIMEOUT)
  10837. {
  10838. LOG_INFO("********** Gun %d GFD timeout **********", gun_index + 1);
  10839. ChargingTerminalProcess(gun_index);
  10840. }
  10841. }
  10842. break;
  10843. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  10844. {
  10845. if (isModeChange(gun_index))
  10846. {
  10847. LOG_INFO("================== S_CHARGING (%x) ================== \n", gun_index + 1);
  10848. //StopGunInfoTimeoutDet(gun_index);
  10849. GetClockTime(&_SystemStatus_Time[gun_index]);
  10850. GetClockTime(&_ChargedEnergy_Time[gun_index]);
  10851. CheckoutStartChargingSoc(gun_index, YES);
  10852. chargingInfo[gun_index]->PresentChargedDuration = 0;
  10853. ChangeStartOrStopDateTime(YES, gun_index);
  10854. #ifndef STARTTRANSATION_IMMEDIATELY
  10855. OcppStartTransaction(gun_index);
  10856. #endif
  10857. }
  10858. // only for test & debug purpose
  10859. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10860. {
  10861. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  10862. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  10863. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  10864. {
  10865. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  10866. }
  10867. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  10868. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  10869. }
  10870. // 計算 Power
  10871. //chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  10872. //chargingInfo[gun_index]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent))) / 1000 / 100);
  10873. UpdateGunPresentChargingPower(gun_index);
  10874. ChargedEnergySummation(gun_index);
  10875. CheckSessionTargetStop(gun_index);
  10876. if (isEvBoardStopChargeFlag(gun_index))
  10877. {
  10878. LOG_INFO("********** Gun %d Alarm Stop (S_CHARGING) **********", gun_index + 1);
  10879. Set_Ocpp_StopReason(gun_index, "Local");
  10880. ChargingAlarmProcess(gun_index);
  10881. }
  10882. else if(isNormalStopChargeFlag(gun_index) == YES)
  10883. {
  10884. LOG_INFO("********** Gun %d Normal Stop (S_CHARGING) %s**********", gun_index + 1,
  10885. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10886. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10887. ChargingTerminalProcess(gun_index);
  10888. }
  10889. else if(isResetStopChargeFlag(gun_index) == YES)
  10890. {
  10891. LOG_INFO("********** Gun %d Reset Stop (S_CHARGING) %s**********", gun_index + 1,
  10892. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10893. char *reason[16];
  10894. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10895. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10896. ChargingTerminalProcess(gun_index);
  10897. }
  10898. else if(isInvalidIdStopFlag(gun_index) == YES)
  10899. {
  10900. LOG_INFO("********** Gun %d InvalidId Stop (S_CHARGING) **********", gun_index + 1);
  10901. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  10902. ChargingTerminalProcess(gun_index);
  10903. }
  10904. else if(CheckBackendChargingTimeout(gun_index) ||
  10905. CheckBackendChargingEnergy(gun_index) ||
  10906. IsReachConfigMaxChargingSoc(gun_index))
  10907. {
  10908. LOG_INFO("********** Gun %d Backend Condition Stop (S_CHARGING) **********", gun_index + 1);
  10909. ChargingTerminalProcess(gun_index);
  10910. }
  10911. else if(isSessionTargetStopFlag(gun_index) == YES)
  10912. {
  10913. LOG_INFO("********** Gun %d SessionTarget Stop (S_CHARGING) **********", gun_index + 1);
  10914. Set_Ocpp_StopReason(gun_index, "Other");
  10915. ChargingTerminalProcess(gun_index);
  10916. }
  10917. else if(isBackendStopFlag(gun_index))
  10918. {
  10919. LOG_INFO("********** Gun %d Backend Stop (S_CHARGING) **********", gun_index + 1);
  10920. Set_Ocpp_StopReason(gun_index, "Other");
  10921. ChargingTerminalProcess(gun_index);
  10922. }
  10923. }
  10924. break;
  10925. case S_ALARM:
  10926. case S_TERMINATING:
  10927. {
  10928. if (isModeChange(gun_index))
  10929. {
  10930. Set_Ocpp_StopReason(gun_index, "Other");
  10931. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  10932. {
  10933. LOG_INFO("================== S_ALARM (%x) ================== \n", gun_index + 1);
  10934. UpdateErrorCodeToOcpp(gun_index);
  10935. TheEndCharging(gun_index);
  10936. }
  10937. else
  10938. {
  10939. LOG_INFO("================== S_TERMINATING (%x) ================== \n", gun_index + 1);
  10940. }
  10941. //StopGunInfoTimeoutDet(gun_index);
  10942. GetClockTime(&_SystemStatus_Time[gun_index]);
  10943. }
  10944. UpdateGunPresentChargingPower(gun_index);
  10945. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10946. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  10947. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  10948. {
  10949. OcppStopTransaction(gun_index);
  10950. }
  10951. else
  10952. {
  10953. ChargedEnergySummation(gun_index);
  10954. }
  10955. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  10956. {
  10957. if(time >= GUN_COMP_WAIT_TIMEOUT)
  10958. {
  10959. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  10960. {
  10961. LOG_INFO ("********** Gun %d psu is releasable **********\n", gun_index + 1);
  10962. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  10963. }
  10964. }
  10965. if(IsAvailableBackToIdle(gun_index) == YES &&
  10966. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  10967. {
  10968. ReleaseAlarmCode(gun_index);
  10969. setChargerMode(gun_index, MODE_IDLE);
  10970. }
  10971. }
  10972. else
  10973. {
  10974. setChargerMode(gun_index, MODE_COMPLETE);
  10975. }
  10976. }
  10977. break;
  10978. case S_COMPLETE:
  10979. {
  10980. if (isModeChange(gun_index))
  10981. {
  10982. LOG_INFO("================== S_COMPLETE (%x) ================== \n", gun_index + 1);
  10983. TheEndCharging(gun_index);
  10984. GetClockTime(&_SystemStatus_Time[gun_index]);
  10985. }
  10986. UpdateGunPresentChargingPower(gun_index);
  10987. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10988. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  10989. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  10990. {
  10991. OcppStopTransaction(gun_index);
  10992. }
  10993. else
  10994. {
  10995. ChargedEnergySummation(gun_index);
  10996. }
  10997. if(time >= GUN_COMP_WAIT_TIMEOUT)
  10998. {
  10999. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11000. {
  11001. LOG_INFO ("********** Gun %d psu is releasable **********", gun_index + 1);
  11002. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  11003. }
  11004. }
  11005. if(IsAvailableBackToIdle(gun_index) == YES &&
  11006. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11007. {
  11008. setChargerMode(gun_index, MODE_IDLE);
  11009. }
  11010. }
  11011. break;
  11012. case S_CCS_PRECHARGE_ST0:
  11013. if (isModeChange(gun_index))
  11014. {
  11015. LOG_INFO("CCS Precharge Processing 1....................%x \n", gun_index);
  11016. //StopGunInfoTimeoutDet(gun_index);
  11017. //StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  11018. }
  11019. break;
  11020. case S_CCS_PRECHARGE_ST1:
  11021. if (isModeChange(gun_index))
  11022. {
  11023. LOG_INFO("CCS Precharge Processing 2....................%x \n", gun_index);
  11024. }
  11025. break;
  11026. case S_DEBUG:
  11027. if (isModeChange(gun_index))
  11028. {
  11029. LOG_INFO("================== S_DEBUG (%x) ================== \n", gun_index + 1);
  11030. }
  11031. if(!ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay &&
  11032. !ShmChargerInfo->Control.FanCtrl.bits.AbnormalFan)
  11033. {
  11034. setChargerMode(gun_index, MODE_IDLE);
  11035. }
  11036. break;
  11037. }
  11038. }
  11039. TryFeedWatchdog();
  11040. usleep(whileLoopTime);
  11041. }
  11042. return FAIL;
  11043. }