main.c 441 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436
  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. int _notifyOccupancyFeeAct[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 = "02";
  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. _notifyOccupancyFeeAct[i] = _Notify_None;
  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_TcciOccupancyFeeActReq(int gun_index)
  4031. {
  4032. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4033. {
  4034. return ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq ? 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_TcciOccupancyFeeAct(int gun_index, OccupancyBillInfo *bill, int action)
  4043. {
  4044. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4045. {
  4046. memset(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN));
  4047. memcpy(ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].occupancySN, bill->Sn, sizeof(bill->Sn));
  4048. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].action = action;
  4049. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq = true;
  4050. }
  4051. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4052. {
  4053. }
  4054. }
  4055. void Clean_Ocpp_TcciNotifyOccupancyFeeActReq(int gun_index)
  4056. {
  4057. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4058. {
  4059. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].action = _Notify_None;
  4060. ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].OccupancyFeeActReq = false;
  4061. }
  4062. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4063. {
  4064. }
  4065. }
  4066. void Get_Ocpp_TcciGetOccupancyFeeResponse(int gun_index, OccupancyBillInfo *bill)
  4067. {
  4068. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4069. {
  4070. bill->Duration = ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_duration;
  4071. bill->Fee = ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_occupancyFee;
  4072. memcpy(bill->StartTime, ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_startTime, sizeof(bill->StartTime));
  4073. }
  4074. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4075. {
  4076. }
  4077. }
  4078. bool Is_Ocpp_TcciOccupancyFeeActStatus(int gun_index, char *status)
  4079. {
  4080. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4081. {
  4082. return strcmp((char *)ShmOCPP16Data->TcciCustomData.OccupancyFeeAct[gun_index].response_status, status) == EQUAL ? true : false;
  4083. }
  4084. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4085. {
  4086. }
  4087. return false;
  4088. }
  4089. //===============================================
  4090. // Ocpp Tcci ReportOccupancyDeductReq
  4091. //===============================================
  4092. bool Is_Ocpp_TcciReportOccupancyDeductReq(void)
  4093. {
  4094. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4095. {
  4096. return ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq ? true : false;
  4097. }
  4098. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4099. {
  4100. return false;
  4101. }
  4102. return false;
  4103. }
  4104. void Set_Ocpp_TcciReportOccupancyDeductReq(void)
  4105. {
  4106. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4107. {
  4108. ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq = true;
  4109. }
  4110. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4111. {
  4112. }
  4113. }
  4114. //===============================================
  4115. // Ocpp Tcci OccupancyDeductResult
  4116. //===============================================
  4117. bool Set_Ocpp_OccupancyDeductResult(OccupancyDeductInfo *deduct, bool pass_fail)
  4118. {
  4119. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4120. {
  4121. memset(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult, 0x00, sizeof(struct StructOccupancyDeductResult));
  4122. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.deductResult = pass_fail;
  4123. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.amount = deduct->Fee;
  4124. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.occupancySN[0], &deduct->Sn[0], sizeof(deduct->Sn));
  4125. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.creditNo[0], &deduct->creditNo[0], sizeof(deduct->creditNo));
  4126. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.approvalNo[0], &deduct->ApprovalNumber[0], sizeof(deduct->ApprovalNumber));
  4127. return true;
  4128. }
  4129. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4130. {
  4131. }
  4132. return false;
  4133. }
  4134. //===============================================
  4135. // Ocpp Start Transaction
  4136. //===============================================
  4137. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id)
  4138. {
  4139. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4140. {
  4141. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag, id);
  4142. }
  4143. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4144. {
  4145. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4146. }
  4147. // MaintainServer
  4148. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4149. {
  4150. strcpy((char *)ShmOCPP16DataPH->StartTransaction[gun_index].IdTag, id);
  4151. }
  4152. }
  4153. void Set_Ocpp_StartTransactionReq(int gun_index)
  4154. {
  4155. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4156. {
  4157. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = true;
  4158. }
  4159. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4160. {
  4161. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4162. }
  4163. // MaintainServer
  4164. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4165. {
  4166. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq = true;
  4167. }
  4168. }
  4169. bool Is_Ocpp_StartTransactionConf(int gun_index)
  4170. {
  4171. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4172. {
  4173. return ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == YES ? true : false;
  4174. }
  4175. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4176. {
  4177. return ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf == YES ? true : false;
  4178. }
  4179. return false;
  4180. }
  4181. void Clean_Ocpp_StartTransactionConf(int gun_index)
  4182. {
  4183. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4184. {
  4185. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = false;
  4186. }
  4187. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4188. {
  4189. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  4190. }
  4191. // MaintainServer
  4192. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4193. {
  4194. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf = false;
  4195. }
  4196. }
  4197. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4198. {
  4199. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4200. {
  4201. return strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  4202. }
  4203. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4204. {
  4205. return strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, status) == EQUAL ? true : false;
  4206. }
  4207. return false;
  4208. }
  4209. bool Is_Ocpp_StartTransaction_Accepted(int gun_index)
  4210. {
  4211. return Is_Ocpp_StartTransaction_Status(gun_index, "Accepted");
  4212. }
  4213. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4214. {
  4215. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4216. {
  4217. strcpy(status, (char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status);
  4218. }
  4219. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4220. {
  4221. strcpy(status, (char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status);
  4222. }
  4223. }
  4224. void Get_Ocpp_TransactionId(int gun_index, char *transactionId)
  4225. {
  4226. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4227. {
  4228. sprintf(transactionId, "%d", ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId);
  4229. return;
  4230. }
  4231. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4232. {
  4233. strcpy(transactionId, (char *)&ShmOCPP20Data->TransactionEvent [gun_index].transactionInfo.transactionId[0]);
  4234. return;
  4235. }
  4236. sprintf(transactionId, "%d", 0);
  4237. }
  4238. bool Is_Invalid_StartTransactionId(int gun_index)
  4239. {
  4240. if(Is_Ocpp_StartTransaction_Status(gun_index, "Blocked") ||
  4241. Is_Ocpp_StartTransaction_Status(gun_index, "Expired") ||
  4242. Is_Ocpp_StartTransaction_Status(gun_index, "Invalid") ||
  4243. Is_Ocpp_StartTransaction_Status(gun_index, "NoCredit") ||
  4244. Is_Ocpp_StartTransaction_Status(gun_index, "NotAllowedTypeEVSE") ||
  4245. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisLocation") ||
  4246. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisTime") ||
  4247. Is_Ocpp_StartTransaction_Status(gun_index, "Unknown"))
  4248. {
  4249. return true;
  4250. }
  4251. return false;
  4252. }
  4253. //===============================================
  4254. // Ocpp Stop Transaction
  4255. //===============================================
  4256. void Set_Ocpp_StopTransactionId(int gun_index, char *id)
  4257. {
  4258. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4259. {
  4260. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].IdTag, id);
  4261. return;
  4262. }
  4263. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4264. {
  4265. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4266. return;
  4267. }
  4268. }
  4269. void Set_Ocpp_StopTransactionReq(int gun_index)
  4270. {
  4271. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4272. {
  4273. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = true;
  4274. }
  4275. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4276. {
  4277. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4278. }
  4279. // MaintainServer
  4280. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4281. {
  4282. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = true;
  4283. }
  4284. }
  4285. //===============================================
  4286. // Ocpp Check Backup Error Code
  4287. //===============================================
  4288. void Chk_Ocpp_Backup_ErrorCode(int gun_index)
  4289. {
  4290. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4291. {
  4292. if(strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, "") == EQUAL)
  4293. {
  4294. if(strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, "") != EQUAL)
  4295. {
  4296. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, (char *)_BackupStatusNotification[gun_index].ErrorCode);
  4297. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, (char *)_BackupStatusNotification[gun_index].VendorErrorCode);
  4298. }
  4299. }
  4300. }
  4301. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4302. {
  4303. }
  4304. }
  4305. //===============================================
  4306. // Ocpp Reserve Now
  4307. //===============================================
  4308. bool Is_Ocpp_ReserveNowReq(int gun_index)
  4309. {
  4310. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4311. {
  4312. return ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4313. }
  4314. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4315. {
  4316. return ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4317. }
  4318. return false;
  4319. }
  4320. void Clean_Ocpp_ReserveNowReq(int gun_index)
  4321. {
  4322. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4323. {
  4324. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4325. return;
  4326. }
  4327. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4328. {
  4329. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4330. return;
  4331. }
  4332. }
  4333. int Get_Ocpp_ReserveNowId(int gun_index)
  4334. {
  4335. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4336. {
  4337. return ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  4338. }
  4339. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4340. {
  4341. return ShmOCPP20Data->ReserveNow[gun_index].id;
  4342. }
  4343. return 0;
  4344. }
  4345. void Set_Ocpp_ReserveNowConf(int gun_index)
  4346. {
  4347. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4348. {
  4349. ShmOCPP16Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4350. return;
  4351. }
  4352. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4353. {
  4354. ShmOCPP20Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4355. return;
  4356. }
  4357. }
  4358. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date)
  4359. {
  4360. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4361. {
  4362. strcpy(date, (char *)ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate);
  4363. return;
  4364. }
  4365. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4366. {
  4367. strcpy(date, (char *)ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime);
  4368. return;
  4369. }
  4370. strcpy(date, "");
  4371. return;
  4372. }
  4373. bool Is_Ocpp_ReserveNowExpired(int gun_index)
  4374. {
  4375. bool _expired = true;
  4376. char _expiryDate[128];
  4377. Get_Ocpp_ReserveNowExpiryDate(gun_index, _expiryDate);
  4378. _expired = CheckTimeOut(_expiryDate);
  4379. return _expired;
  4380. }
  4381. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag)
  4382. {
  4383. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4384. {
  4385. strcpy(idTag, (char *)ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  4386. return;
  4387. }
  4388. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4389. {
  4390. strcpy(idTag, (char *)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken);
  4391. return;
  4392. }
  4393. strcpy(idTag, "");
  4394. return;
  4395. }
  4396. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag)
  4397. {
  4398. char ReserveIdTag[32];
  4399. Get_Ocpp_ReserveNowIdTag(gun_index, ReserveIdTag);
  4400. return strcmp(ReserveIdTag, authIdTag) == EQUAL ? true : false;
  4401. }
  4402. //===============================================
  4403. // Ocpp Cancel Reservation
  4404. //===============================================
  4405. bool Is_Ocpp_CancelReservationReq(int gun_index)
  4406. {
  4407. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4408. {
  4409. return ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4410. }
  4411. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4412. {
  4413. return ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4414. }
  4415. return false;
  4416. }
  4417. void Clean_Ocpp_CancelReservationReq(int gun_index)
  4418. {
  4419. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4420. {
  4421. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4422. return;
  4423. }
  4424. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4425. {
  4426. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4427. return;
  4428. }
  4429. }
  4430. void Set_Ocpp_CancelReservationConf(int gun_index)
  4431. {
  4432. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4433. {
  4434. ShmOCPP16Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4435. return;
  4436. }
  4437. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4438. {
  4439. ShmOCPP20Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4440. return;
  4441. }
  4442. }
  4443. //===============================================
  4444. // Ocpp Change Availability
  4445. //===============================================
  4446. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index)
  4447. {
  4448. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4449. {
  4450. return ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4451. }
  4452. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4453. {
  4454. return ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4455. }
  4456. return false;
  4457. }
  4458. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index)
  4459. {
  4460. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4461. {
  4462. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4463. return;
  4464. }
  4465. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4466. {
  4467. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4468. return;
  4469. }
  4470. }
  4471. bool Chk_Ocpp_Availability_Type(int gun_index, char *type)
  4472. {
  4473. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4474. {
  4475. return strcmp((char *)ShmOCPP16Data->ChangeAvailability[gun_index].Type, type) == EQUAL ? true : false;
  4476. }
  4477. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4478. {
  4479. return strcmp((char *)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, type) == EQUAL ? true : false;
  4480. }
  4481. return false;
  4482. }
  4483. //===============================================
  4484. // Ocpp Unlock Connector
  4485. //===============================================
  4486. bool Is_Ocpp_UnlockConnectorReq(int gun_index)
  4487. {
  4488. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4489. {
  4490. return ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4491. }
  4492. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4493. {
  4494. return ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4495. }
  4496. return false;
  4497. }
  4498. void Clean_Ocpp_UnlockConnectorReq(int gun_index)
  4499. {
  4500. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4501. {
  4502. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4503. return;
  4504. }
  4505. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4506. {
  4507. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4508. return;
  4509. }
  4510. }
  4511. void Set_Ocpp_UnlockConnectorConf(int gun_index)
  4512. {
  4513. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4514. {
  4515. strcpy((char *)ShmOCPP16Data->UnlockConnector[gun_index].ResponseStatus, "Unlocked");
  4516. ShmOCPP16Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4517. return;
  4518. }
  4519. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4520. {
  4521. strcpy((char *)ShmOCPP20Data->UnlockConnector[gun_index].Response_status, "Unlocked");
  4522. ShmOCPP20Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4523. return;
  4524. }
  4525. }
  4526. //===============================================
  4527. // Ocpp Remote Start
  4528. //===============================================
  4529. bool Is_Ocpp_RemoteStartReq(int gun_index)
  4530. {
  4531. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4532. {
  4533. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES ? true : false;
  4534. }
  4535. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4536. {
  4537. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq == YES ? true : false;
  4538. }
  4539. return false;
  4540. }
  4541. void Clean_Ocpp_RemoteStartReq(int gun_index)
  4542. {
  4543. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4544. {
  4545. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = false;
  4546. return;
  4547. }
  4548. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4549. {
  4550. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = false;
  4551. return;
  4552. }
  4553. }
  4554. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag)
  4555. {
  4556. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4557. {
  4558. strcpy(idTag, (char *)&ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag[0]);
  4559. return;
  4560. }
  4561. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4562. {
  4563. strcpy(idTag, (char *)&ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken[0]);
  4564. return;
  4565. }
  4566. strcpy(idTag, "");
  4567. }
  4568. //===============================================
  4569. // Ocpp Remote Stop
  4570. //===============================================
  4571. bool Is_Ocpp_RemoteStopReq(int gun_index)
  4572. {
  4573. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4574. {
  4575. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == YES ? true : false;
  4576. }
  4577. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4578. {
  4579. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq == YES ? true : false;
  4580. }
  4581. return false;
  4582. }
  4583. void Clean_Ocpp_RemoteStopReq(int gun_index)
  4584. {
  4585. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4586. {
  4587. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = false;
  4588. return;
  4589. }
  4590. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4591. {
  4592. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = false;
  4593. return;
  4594. }
  4595. }
  4596. //===============================================
  4597. // Ocpp StopReason
  4598. //===============================================
  4599. void Set_Ocpp_StopReason(int gun_index, char *reason)
  4600. {
  4601. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4602. {
  4603. if(strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  4604. {
  4605. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, reason);
  4606. }
  4607. }
  4608. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4609. {
  4610. if(strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "" ) == EQUAL)
  4611. {
  4612. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, reason);
  4613. }
  4614. }
  4615. // Maintain server
  4616. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4617. {
  4618. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, ShmOCPP16Data->StopTransaction[gun_index].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  4619. }
  4620. }
  4621. void Get_Ocpp_StopReason(int gun_index, char *reason)
  4622. {
  4623. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4624. {
  4625. strcpy(reason, (char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4626. return;
  4627. }
  4628. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4629. {
  4630. strcpy(reason, (char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4631. return;
  4632. }
  4633. strcpy(reason, "");
  4634. }
  4635. void Clean_Ocpp_StopReason(int gun_index)
  4636. {
  4637. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4638. {
  4639. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  4640. }
  4641. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4642. {
  4643. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "");
  4644. }
  4645. // Maintain server
  4646. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4647. {
  4648. strcpy((char *)ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, "");
  4649. }
  4650. }
  4651. //===============================================
  4652. // Ocpp StatusNotification ErrorCode
  4653. //===============================================
  4654. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4655. {
  4656. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4657. {
  4658. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode);
  4659. return;
  4660. }
  4661. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4662. {
  4663. return;
  4664. }
  4665. }
  4666. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4667. {
  4668. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4669. {
  4670. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4671. }
  4672. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4673. {
  4674. return strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4675. }
  4676. return false;
  4677. }
  4678. //===============================================
  4679. // Ocpp StatusNotification VendorErrorCode
  4680. //===============================================
  4681. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4682. {
  4683. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4684. {
  4685. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode);
  4686. return;
  4687. }
  4688. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4689. {
  4690. return;
  4691. }
  4692. }
  4693. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4694. {
  4695. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4696. {
  4697. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4698. }
  4699. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4700. {
  4701. return strcmp((char *)_BackupStatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4702. }
  4703. return false;
  4704. }
  4705. //===============================================
  4706. // Ocpp ChargingProfileConf
  4707. //===============================================
  4708. bool Is_Ocpp_ChargingProfileConf(int gun_index)
  4709. {
  4710. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4711. {
  4712. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4713. }
  4714. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4715. {
  4716. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4717. }
  4718. return false;
  4719. }
  4720. void Clean_Ocpp_ChargingProfileConf(int gun_index)
  4721. {
  4722. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4723. {
  4724. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4725. return;
  4726. }
  4727. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4728. {
  4729. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4730. return;
  4731. }
  4732. }
  4733. //===============================================
  4734. // Ocpp ChargingProfileReq
  4735. //===============================================
  4736. bool Is_Ocpp_ChargingProfileReq(int gun_index)
  4737. {
  4738. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4739. {
  4740. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4741. }
  4742. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4743. {
  4744. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4745. }
  4746. return false;
  4747. }
  4748. void Set_Ocpp_ChargingProfileReq(int gun_index)
  4749. {
  4750. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4751. {
  4752. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4753. return;
  4754. }
  4755. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4756. {
  4757. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4758. return;
  4759. }
  4760. }
  4761. //===============================================
  4762. // Ocpp SmartChargingProfile ChargingProfileKind
  4763. //===============================================
  4764. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind)
  4765. {
  4766. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4767. {
  4768. return strcmp((char *)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, kind) == EQUAL ? true : false;
  4769. }
  4770. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4771. {
  4772. return strcmp((char *)ShmOCPP20Data->SmartChargingProfile [gun_index].chargingProfileKind, kind) == EQUAL ? true : false;
  4773. }
  4774. return false;
  4775. }
  4776. bool Is_Ocpp_ChargingProfileId(int gun_index)
  4777. {
  4778. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4779. {
  4780. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == YES ? true : false;
  4781. }
  4782. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4783. {
  4784. return ShmOCPP20Data->SmartChargingProfile[gun_index].id == YES ? true : false;
  4785. }
  4786. return false;
  4787. }
  4788. int Get_Ocpp_ChargingProfileStartTime(int gun_index)
  4789. {
  4790. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4791. {
  4792. return GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  4793. }
  4794. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4795. {
  4796. return GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  4797. }
  4798. return 0;
  4799. }
  4800. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index)
  4801. {
  4802. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4803. {
  4804. return ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);
  4805. }
  4806. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4807. {
  4808. return ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);
  4809. }
  4810. return 0;
  4811. }
  4812. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index)
  4813. {
  4814. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4815. {
  4816. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4817. }
  4818. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4819. {
  4820. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4821. }
  4822. return 0;
  4823. }
  4824. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index)
  4825. {
  4826. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4827. {
  4828. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4829. }
  4830. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4831. {
  4832. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4833. }
  4834. return 0;
  4835. }
  4836. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index)
  4837. {
  4838. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4839. {
  4840. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4841. }
  4842. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4843. {
  4844. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].numberPhases;
  4845. }
  4846. return 0;
  4847. }
  4848. int Get_Ocpp_ChargingProfileStartIndex(int gun_index)
  4849. {
  4850. int _time = 0, _scheduleTime = 0;
  4851. int _maxCount = 0;
  4852. float _limit = 0;
  4853. int _startCount = NO_DEFINE;
  4854. _time = Get_Ocpp_ChargingProfileStartTime(gun_index);
  4855. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(gun_index);
  4856. for(int i = 0; i < _maxCount; i++)
  4857. {
  4858. _scheduleTime = Get_Ocpp_ChargingProfileScheduleTime(gun_index, i);
  4859. if(_time >= _scheduleTime)
  4860. {
  4861. _limit = Get_Ocpp_ChargingProfileScheduleLimit(gun_index, i);
  4862. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4863. {
  4864. _startCount = i;
  4865. }
  4866. }
  4867. }
  4868. return _startCount;
  4869. }
  4870. //===============================================
  4871. // Ocpp MaxChargingProfile ChargingProfileKind
  4872. //===============================================
  4873. bool Is_Ocpp_MaxChargingProfileKind(char *kind)
  4874. {
  4875. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4876. {
  4877. return strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, kind) == EQUAL ? true : false;
  4878. }
  4879. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4880. {
  4881. return strcmp((char *)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, kind) == EQUAL ? true : false;
  4882. }
  4883. return false;
  4884. }
  4885. bool Is_Ocpp_MaxChargingProfileId(void)
  4886. {
  4887. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4888. {
  4889. return ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES ? true : false;
  4890. }
  4891. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4892. {
  4893. return ShmOCPP20Data->MaxChargingProfile.id == YES ? true : false;
  4894. }
  4895. return false;
  4896. }
  4897. int Get_Ocpp_MaxProfileStartTime(void)
  4898. {
  4899. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4900. {
  4901. return GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  4902. }
  4903. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4904. {
  4905. return GetStartScheduleTime(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule);
  4906. }
  4907. return 0;
  4908. }
  4909. int Get_Ocpp_MaxProfileScheduleCount(void)
  4910. {
  4911. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4912. {
  4913. return ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  4914. }
  4915. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4916. {
  4917. return ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);
  4918. }
  4919. return 0;
  4920. }
  4921. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index)
  4922. {
  4923. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4924. {
  4925. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4926. }
  4927. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4928. {
  4929. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4930. }
  4931. return 0;
  4932. }
  4933. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index)
  4934. {
  4935. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4936. {
  4937. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4938. }
  4939. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4940. {
  4941. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4942. }
  4943. return 0;
  4944. }
  4945. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index)
  4946. {
  4947. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4948. {
  4949. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4950. }
  4951. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4952. {
  4953. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4954. }
  4955. return 0;
  4956. }
  4957. int Get_Ocpp_MaxProfileStartIndex(void)
  4958. {
  4959. int _time = 0, _scheduleTime = 0;
  4960. int _maxCount = 0;
  4961. float _limit = 0;
  4962. int _startCount = NO_DEFINE;
  4963. _time = Get_Ocpp_MaxProfileStartTime();
  4964. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  4965. for(int i = 0; i < _maxCount; i++)
  4966. {
  4967. _scheduleTime = Get_Ocpp_MaxProfileScheduleTime(i);
  4968. if(_time >= _scheduleTime)
  4969. {
  4970. _limit = Get_Ocpp_MaxProfileScheduleLimit(i);
  4971. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4972. {
  4973. _startCount = i;
  4974. }
  4975. }
  4976. }
  4977. return _startCount;
  4978. }
  4979. //===============================================
  4980. // Ocpp isRemoteStartWaitReq
  4981. //===============================================
  4982. int Is_Ocpp_RemoteStartWaitReq(void)
  4983. {
  4984. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4985. {
  4986. return ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  4987. }
  4988. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4989. {
  4990. return ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  4991. }
  4992. return 0;
  4993. }
  4994. //===============================================
  4995. // Ocpp SessionTarget
  4996. //===============================================
  4997. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget)
  4998. {
  4999. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5000. {
  5001. memcpy(sessionTarget, &ShmOCPP16Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5002. }
  5003. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5004. {
  5005. memcpy(sessionTarget, &ShmOCPP20Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5006. }
  5007. }
  5008. //===============================================
  5009. // Ocpp OffLineMaxChargingPower
  5010. //===============================================
  5011. int Get_Ocpp_OffLineMaxChargingPower(void)
  5012. {
  5013. // unit: W
  5014. int maxChargingPower = 0;
  5015. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5016. {
  5017. maxChargingPower = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[OffLineMaxChargingPower].ItemData);
  5018. }
  5019. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5020. {
  5021. maxChargingPower = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[OffLineMaxChargingPower].variableAttribute[0].value);
  5022. }
  5023. return maxChargingPower > 0 ? maxChargingPower : 0;
  5024. }
  5025. //===============================================
  5026. // DeductResult
  5027. //===============================================
  5028. void Set_Deduct_UserId(int gunIndex, char *userId)
  5029. {
  5030. memcpy((char *)&ShmChargerInfo->DeductResultReq[gunIndex].creditNo, userId, strlen(userId));
  5031. }
  5032. void Set_Deduct_TransactionId(int gunIndex, int transaction)
  5033. {
  5034. ShmChargerInfo->DeductResultReq[gunIndex].TransactionId = transaction;
  5035. }
  5036. void Clean_DeductInfo(int gunIndex)
  5037. {
  5038. memset(&ShmChargerInfo->DeductResultReq[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5039. }
  5040. bool Is_DeductResultReq(int gunIndex)
  5041. {
  5042. return ShmChargerInfo->DeductResultReq[gunIndex].DeductReq ? true : false;
  5043. }
  5044. //===============================================
  5045. // ReaderStatus
  5046. //===============================================
  5047. bool Is_ReaderStatusReq(int gunIndex)
  5048. {
  5049. return ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq ? true : false;
  5050. }
  5051. void Clean_ReaderStatusInfo(int gunIndex)
  5052. {
  5053. memset(&ShmChargerInfo->ReaderStatus[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5054. }
  5055. //===============================================
  5056. // TransactionInfoData
  5057. //===============================================
  5058. void Set_UserTransactionId(int gunIndex, int transaction)
  5059. {
  5060. ShmChargerInfo->UserTransaction[gunIndex].TransactionId = transaction;
  5061. }
  5062. void Set_UserPrice(int gunIndex, float price)
  5063. {
  5064. ShmChargerInfo->UserTransaction[gunIndex].UserPrice = price;
  5065. }
  5066. void Set_UserTotalCost(int gunIndex, float cost)
  5067. {
  5068. ShmChargerInfo->UserTransaction[gunIndex].TotalCost = cost;
  5069. }
  5070. void Set_UserAccountBalance(int gunIndex, float balance)
  5071. {
  5072. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = balance;
  5073. }
  5074. void Set_UserDiscount(int gunIndex, float discount)
  5075. {
  5076. ShmChargerInfo->UserTransaction[gunIndex].CostDiscount = discount;
  5077. }
  5078. void Clean_UserTransaction(int gunIndex)
  5079. {
  5080. memset(&ShmChargerInfo->UserTransaction[gunIndex], 0x00, sizeof(TransactionInfoData));
  5081. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = -65535;
  5082. }
  5083. void Clean_UserPriceAndReceipt(int gunIndex)
  5084. {
  5085. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex]));
  5086. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex]));
  5087. }
  5088. //===============================================
  5089. // OccupancyDeductReq
  5090. //===============================================
  5091. bool Is_OccupancyDeductReq(int gunIndex)
  5092. {
  5093. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq ? true : false;
  5094. }
  5095. void Clean_OccupancyDeductReq(int gunIndex)
  5096. {
  5097. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq = false;
  5098. }
  5099. bool Is_OccupancyKeepCountingReq(int gunIndex)
  5100. {
  5101. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting ? true : false;
  5102. }
  5103. void Set_OccupancyKeepCountingReq(int gunIndex)
  5104. {
  5105. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = true;
  5106. }
  5107. void Clean_OccupancyKeepCountingReq(int gunIndex)
  5108. {
  5109. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = false;
  5110. }
  5111. //===============================================
  5112. // Main process
  5113. //===============================================
  5114. // 檢查 Byte 中某個 Bit 的值
  5115. // _byte : 欲改變的 byte
  5116. // _bit : 該 byte 的第幾個 bit
  5117. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  5118. {
  5119. return ( _byte & mask_table[_bit] ) != 0x00;
  5120. }
  5121. // 設定 Byte 中某個 Bit的值
  5122. // _byte : 欲改變的 byte
  5123. // _bit : 該 byte 的第幾個 bit
  5124. // value : 修改的值為 0 or 1
  5125. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  5126. {
  5127. if(value == 1)
  5128. *_byte |= (1 << _bit);
  5129. else if (value == 0)
  5130. *_byte ^= (1 << _bit);
  5131. }
  5132. void UserScanFunction()
  5133. {
  5134. }
  5135. unsigned char isModeChange(unsigned char gun_index)
  5136. {
  5137. unsigned char result = NO;
  5138. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  5139. {
  5140. result = YES;
  5141. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  5142. }
  5143. return result;
  5144. }
  5145. void AnnounceAccountBalance(unsigned char index, float balance)
  5146. {
  5147. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  5148. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = balance;
  5149. if(!ShmChargerInfo->Control.TestCtrl.bits.DisableBalance)
  5150. {
  5151. Set_Connector_MiscCommand(index, MISC_CONN_ACCOUNT_BALANCE);
  5152. }
  5153. LOG_INFO("%sAnnounce Account Balance %4.2f To Connector %d",
  5154. ShmChargerInfo->Control.TestCtrl.bits.DisableBalance ? "Do Not " : "", balance, index + 1);
  5155. }
  5156. void FouceAnnounceAccountBalance(unsigned char index)
  5157. {
  5158. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  5159. {
  5160. AnnounceAccountBalance(index, 0);
  5161. }
  5162. }
  5163. void CleanConnectorUserIdAndPrice(unsigned char index)
  5164. {
  5165. if(chargingInfo[index]->SystemStatus == S_IDLE)
  5166. {
  5167. LOG_INFO("*********** Connector %d Clean UserId & UserPrice ***********", index + 1);
  5168. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  5169. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  5170. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -65535;
  5171. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  5172. }
  5173. }
  5174. void DispenserAuthInitial(unsigned char dispenser)
  5175. {
  5176. LOG_INFO("*********** Dispenser %d Authorizing Initial ***********", dispenser + 1);
  5177. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5178. // clean dispenser price
  5179. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser]));
  5180. }
  5181. void GunAuthInitial(unsigned char connector)
  5182. {
  5183. LOG_INFO("*********** Gun %d Authorizing Initial ***********", connector + 1);
  5184. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5185. }
  5186. unsigned char _preGunAuthResult[GENERAL_GUN_QUANTITY];
  5187. unsigned char _preDispenserAuthResult[GENERAL_GUN_QUANTITY];
  5188. struct timespec _GunAuth_Time[GENERAL_GUN_QUANTITY];
  5189. struct timespec _DispenserAuth_Time[GENERAL_GUN_QUANTITY];
  5190. void AuthorizeTimeoutProcess(void)
  5191. {
  5192. int CompletedTime = 0;
  5193. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5194. {
  5195. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult != _AuthResult_None)
  5196. {
  5197. if(_preGunAuthResult[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult)
  5198. {
  5199. GetClockTime(&_GunAuth_Time[index]);
  5200. }
  5201. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5202. {
  5203. FouceAnnounceAccountBalance(index);
  5204. }
  5205. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5206. {
  5207. if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= AUTHORIZE_COMPLETED_TIME)
  5208. {
  5209. LOG_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
  5210. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5211. CleanConnectorUserIdAndPrice(index);
  5212. }
  5213. }
  5214. }
  5215. _preGunAuthResult[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult;
  5216. }
  5217. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5218. {
  5219. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult != _AuthResult_None)
  5220. {
  5221. if(_preDispenserAuthResult[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult)
  5222. {
  5223. GetClockTime(&_DispenserAuth_Time[index]);
  5224. }
  5225. CompletedTime = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
  5226. (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + AUTHORIZE_COMPLETED_TIME) : AUTHORIZE_COMPLETED_TIME;
  5227. if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= CompletedTime)
  5228. {
  5229. DispenserAuthInitial(index);
  5230. }
  5231. }
  5232. _preDispenserAuthResult[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult;
  5233. }
  5234. }
  5235. BOOL IsConnectorAuthorizeSuccess(unsigned char connector)
  5236. {
  5237. BOOL success = false;
  5238. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult == _AuthResult_Valid)
  5239. {
  5240. success = true;
  5241. }
  5242. return success;
  5243. }
  5244. BOOL IsAutoGunSelectionAuthorizeSuccess(unsigned char connector)
  5245. {
  5246. BOOL success = false;
  5247. unsigned char dispenser = 0;
  5248. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  5249. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo.AuthResult == _AuthResult_Valid)
  5250. {
  5251. success = true;
  5252. }
  5253. return success;
  5254. }
  5255. bool CheckConnectorTypeStatus(void)
  5256. {
  5257. bool result = true;
  5258. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 14)
  5259. {
  5260. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = ShmChargerInfo->Control.MaxConnector;
  5261. ShmChargerInfo->Control.ChargerRatingPower = ParsingRatingPower((char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5262. ShmChargerInfo->Control.PsuQuantityByModel = ShmChargerInfo->Control.ChargerRatingPower / (INFY_SINGLE_PSU_POWER / 100);
  5263. LOG_INFO("DC Gun Count = %d, RatingPower = %d.%d kW",
  5264. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  5265. (ShmChargerInfo->Control.ChargerRatingPower / 10),
  5266. (ShmChargerInfo->Control.ChargerRatingPower % 10));
  5267. if(ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  5268. {
  5269. result = false;
  5270. }
  5271. //DO360 charging info default setting
  5272. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5273. {
  5274. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  5275. chargingInfo[i]->Index = (i < ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity || i == 0) ? i : 0xFF;
  5276. chargingInfo[i]->Evboard_id = 0x00;
  5277. chargingInfo[i]->ReservationId = -1;
  5278. chargingInfo[i]->slotsIndex = 0;
  5279. chargingInfo[i]->SystemStatus = S_BOOTING;
  5280. chargingInfo[i]->Type = _Type_Unknown;
  5281. chargingInfo[i]->type_index = 0;
  5282. chargingInfo[i]->IsAvailable = YES;
  5283. }
  5284. }
  5285. else
  5286. {
  5287. // Module Name 不正確 - 告警
  5288. result = false;
  5289. }
  5290. return result;
  5291. }
  5292. void KillTaskExceptPrimary()
  5293. {
  5294. system("killall Module_Authorize");
  5295. system("killall Module_CabinetParallel");
  5296. system("killall Module_ChargerSelfTest");
  5297. system("killall Module_LedIndication");
  5298. system("killall Module_EvComm");
  5299. system("killall Module_InternalComm");
  5300. KillPowerModuleTask();
  5301. system("killall Module_4g &");
  5302. system("killall Module_Wifi &");
  5303. }
  5304. void KillNtpd(void)
  5305. {
  5306. system("killall ntpd");
  5307. }
  5308. void KillAllTask()
  5309. {
  5310. system("killall Module_Authorize");
  5311. system("killall Module_CabinetParallel");
  5312. system("killall Module_ChargerSelfTest");
  5313. system("killall Module_LedIndication");
  5314. system("killall Module_EventLogging");
  5315. system("killall Module_PrimaryComm");
  5316. system("killall Module_EvComm");
  5317. system("killall Module_LcmControl");
  5318. system("killall Module_InternalComm");
  5319. KillPowerModuleTask();
  5320. Kill_Ocpp_Service();
  5321. Kill_MaintainOcpp_Service();
  5322. system("killall Module_4g &");
  5323. system("killall Module_Wifi &");
  5324. }
  5325. int TransferImageViaFTP(char *IpAddress, char *SourcePath)
  5326. {
  5327. int isPass = PASS;
  5328. unsigned char ftpcmdbuf[256];
  5329. //Using ftpput command to transfer upgrade image,
  5330. //User name : root
  5331. //User password : y42j/4cj84
  5332. //Destination : as same as source path
  5333. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s %s %s", IpAddress, SourcePath, SourcePath);
  5334. if(system((char*)ftpcmdbuf) != 0)
  5335. {
  5336. isPass = FAIL;
  5337. }
  5338. return isPass;
  5339. }
  5340. void TriggerDispenserUpgrade(unsigned char index, char *FileName)
  5341. {
  5342. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, 0x00, 128);
  5343. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, FileName, strlen(FileName));
  5344. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.FirmwareUpdateRequest = true;
  5345. LOG_INFO("Trigger dispenser %d to update image [%s]", index + 1, FileName);
  5346. }
  5347. BOOL PutImageToAllDispenser(char *SourcePath, char *FileName, char *model_name)
  5348. {
  5349. int isPass = FAIL;
  5350. int isImageOK[GENERAL_GUN_QUANTITY];
  5351. int dispenser_index = 0;
  5352. BOOL transferred = FALSE;
  5353. memset(isImageOK, 0x00, sizeof(isImageOK));
  5354. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5355. {
  5356. isPass = FAIL;
  5357. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  5358. {
  5359. dispenser_index = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex;
  5360. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName, (char *)model_name) != EQUAL)
  5361. {
  5362. LOG_INFO("Dispenser %d model name [%s] update ignore",
  5363. dispenser_index + 1,
  5364. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName);
  5365. continue;
  5366. }
  5367. char DispenserIpAddress[16];
  5368. memset((char*)DispenserIpAddress, 0x00, 16);
  5369. sprintf((char*)DispenserIpAddress, "%d.%d.%d.%d",
  5370. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  5371. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  5372. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  5373. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  5374. LOG_INFO("Transfer Image(%s) to Dispenser %d (IP: %s)", SourcePath, dispenser_index + 1, DispenserIpAddress);
  5375. isPass = TransferImageViaFTP(DispenserIpAddress, SourcePath);
  5376. isImageOK[dispenser_index] = isPass;
  5377. LOG_INFO("Dispenser %d Put Image %s", dispenser_index + 1, isPass == PASS ? "OK" : "Fail");
  5378. }
  5379. }
  5380. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5381. {
  5382. if(isImageOK[i] > 0)
  5383. {
  5384. TriggerDispenserUpgrade(i, FileName);
  5385. transferred = true;
  5386. }
  5387. }
  5388. return transferred;
  5389. }
  5390. BOOL WaitAllDispenserUpgradeCompleted(void)
  5391. {
  5392. BOOL completed = TRUE;
  5393. GetClockTime(&_DispenserUpgrade_time);
  5394. do
  5395. {
  5396. completed = TRUE;
  5397. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5398. {
  5399. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest ||
  5400. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm)
  5401. {
  5402. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted)
  5403. {
  5404. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest = false;
  5405. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm = false;
  5406. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted = false;
  5407. LOG_INFO("Dispenser %d Upgrade Success", i + 1);
  5408. }
  5409. else
  5410. {
  5411. completed = FALSE;
  5412. }
  5413. }
  5414. }
  5415. if(GetTimeoutValue(_DispenserUpgrade_time) / uSEC_VAL >= DISPENSER_UPGRADE_TIME)
  5416. {
  5417. LOG_INFO("Wait Upgrade Timeout");
  5418. completed = FALSE;
  5419. break;
  5420. }
  5421. if(completed == FALSE)
  5422. {
  5423. sleep(1);
  5424. }
  5425. }while(!completed);
  5426. return completed;
  5427. }
  5428. void UpgradePrepare(void)
  5429. {
  5430. int count = 0;
  5431. SetAcContactor(OFF);
  5432. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5433. {
  5434. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5435. {
  5436. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5437. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5438. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5439. {
  5440. count++;
  5441. }
  5442. }
  5443. }
  5444. if(count > 0)
  5445. {
  5446. sleep(3);
  5447. }
  5448. else
  5449. {
  5450. sleep(1);
  5451. }
  5452. }
  5453. void TriggerPCabinetUpgrade(unsigned char index, char *FileName)
  5454. {
  5455. memset(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, 0x00, 128);
  5456. memcpy(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, FileName, strlen(FileName));
  5457. ShmChargerInfo->ParallelCabinet.PCabinet[index].UpdateRequest = YES;
  5458. LOG_INFO("Trigger PCabinet %d to update image [%s]", index + 1, FileName);
  5459. }
  5460. BOOL PutImageToAllParallelCabinet(char *SourcePath, char *FileName)
  5461. {
  5462. int isPass = FAIL;
  5463. BOOL transferred = FALSE;
  5464. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5465. {
  5466. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5467. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5468. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5469. {
  5470. int channel = ShmChargerInfo->ParallelCabinet.PCabinet[i].SocketChannel;
  5471. if(ShmChargerInfo->CabinetConnInfo[channel].Status == _SCONN_MATCHED &&
  5472. ShmChargerInfo->ParallelCabinet.PCUpdateState[i] == Not_Update)
  5473. {
  5474. char PCabinetIpAddress[16];
  5475. memset((char*)PCabinetIpAddress, 0x00, 16);
  5476. sprintf((char*)PCabinetIpAddress, "%d.%d.%d.%d",
  5477. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 0) & 0xFF),
  5478. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 8) & 0xFF),
  5479. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 16) & 0xFF),
  5480. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 24) & 0xFF));
  5481. isPass = TransferImageViaFTP(PCabinetIpAddress, SourcePath);
  5482. if(isPass == PASS)
  5483. {
  5484. TriggerPCabinetUpgrade(i, FileName);
  5485. }
  5486. transferred = true;
  5487. }
  5488. }
  5489. }
  5490. return transferred;
  5491. }
  5492. BOOL WaitAllPCabinetStartUpgrade(void)
  5493. {
  5494. BOOL isStart = true;
  5495. int count = 0;
  5496. struct timespec _waitStart;
  5497. GetClockTime(&_waitStart);
  5498. do
  5499. {
  5500. isStart = true;
  5501. count = 0;
  5502. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5503. {
  5504. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5505. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5506. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5507. {
  5508. count++;
  5509. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updating)
  5510. {
  5511. isStart = false;
  5512. }
  5513. }
  5514. }
  5515. if((GetTimeoutValue(_waitStart) / uSEC_VAL) >= START_UPGRADE_TIME)
  5516. {
  5517. isStart = false;
  5518. break;
  5519. }
  5520. usleep(10000);
  5521. }while(!isStart);
  5522. if(isStart && count > 0)
  5523. {
  5524. LOG_INFO("Total %d PCabinet upgrade start...", count);
  5525. }
  5526. else
  5527. {
  5528. if(count > 0)
  5529. {
  5530. LOG_INFO("Wait PCabinet upgrade timeout");
  5531. }
  5532. else
  5533. {
  5534. LOG_INFO("No PCabinet need to upgrade");
  5535. }
  5536. }
  5537. return isStart;
  5538. }
  5539. BOOL WaitAllPCabinetUpgradeCompleted(void)
  5540. {
  5541. BOOL completed = true;
  5542. int count = 0;
  5543. struct timespec _waitCompleted;
  5544. GetClockTime(&_waitCompleted);
  5545. do
  5546. {
  5547. completed = true;
  5548. count = 0;
  5549. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5550. {
  5551. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5552. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5553. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5554. {
  5555. count++;
  5556. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updated)
  5557. {
  5558. completed = false;
  5559. }
  5560. }
  5561. }
  5562. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= PCABINET_UPGRADE_TIME)
  5563. {
  5564. completed = false;
  5565. break;
  5566. }
  5567. usleep(10000);
  5568. }while(!completed);
  5569. if(completed && count > 0)
  5570. {
  5571. LOG_INFO("Total %d PCabinet upgrade completed!", count);
  5572. }
  5573. else
  5574. {
  5575. if(count > 0)
  5576. {
  5577. LOG_INFO("Wait PCabinet upgrade completed timeout");
  5578. }
  5579. else
  5580. {
  5581. LOG_INFO("Need not to wait PCabinet completed");
  5582. }
  5583. }
  5584. return completed;
  5585. }
  5586. BOOL WaitMCabinetUpgradeCompleted(void)
  5587. {
  5588. BOOL completed = true;
  5589. struct timespec _waitCompleted;
  5590. GetClockTime(&_waitCompleted);
  5591. do
  5592. {
  5593. completed = false;
  5594. if(ShmChargerInfo->SCabinetControl.NeedUpgrade == _R_NoUpgrade)
  5595. {
  5596. completed = true;
  5597. }
  5598. if(ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Idle &&
  5599. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Alarm &&
  5600. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Charging)
  5601. {
  5602. completed = false;
  5603. break;
  5604. }
  5605. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= MCABINET_UPGRADE_TIME)
  5606. {
  5607. completed = false;
  5608. break;
  5609. }
  5610. usleep(10000);
  5611. }while(!completed);
  5612. if(completed)
  5613. {
  5614. LOG_INFO("MCabinet upgrade completed!");
  5615. }
  5616. else
  5617. {
  5618. LOG_INFO("Wait MCabinet upgrade timeout or disconnected...");
  5619. }
  5620. return completed;
  5621. }
  5622. bool IsModelNameMatch(char *model)
  5623. {
  5624. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == model[0] &&
  5625. ShmSysConfigAndInfo->SysConfig.ModelName[1] == model[1] &&
  5626. ShmSysConfigAndInfo->SysConfig.ModelName[7] == model[7] &&
  5627. ShmSysConfigAndInfo->SysConfig.ModelName[8] == model[8] &&
  5628. ShmSysConfigAndInfo->SysConfig.ModelName[9] == model[9] &&
  5629. ShmSysConfigAndInfo->SysConfig.ModelName[11] == model[11] &&
  5630. ShmSysConfigAndInfo->SysConfig.ModelName[12] == model[12] &&
  5631. ShmSysConfigAndInfo->SysConfig.ModelName[13] == model[13])
  5632. {
  5633. return true;
  5634. }
  5635. return false;
  5636. }
  5637. unsigned long long getAvailableMemory(void)
  5638. {
  5639. long pages = sysconf(_SC_AVPHYS_PAGES);
  5640. long page_size = sysconf(_SC_PAGE_SIZE);
  5641. return pages * page_size;
  5642. }
  5643. void ImageTypeParsing(unsigned int type, char *type_str)
  5644. {
  5645. switch(type)
  5646. {
  5647. case 0x10000001:
  5648. sprintf(type_str, "Image_uboot");
  5649. break;
  5650. case 0x10000002:
  5651. sprintf(type_str, "Image_dtb");
  5652. break;
  5653. case 0x10000003:
  5654. sprintf(type_str, "Image_zImage");
  5655. break;
  5656. case 0x10000004:
  5657. sprintf(type_str, "Image_ramdisk");
  5658. break;
  5659. case 0x10000005:
  5660. sprintf(type_str, "Image_Factory");
  5661. break;
  5662. case 0x10000006:
  5663. sprintf(type_str, "Image_Primary");
  5664. break;
  5665. case 0x10000007:
  5666. sprintf(type_str, "Image_CCS_uboot");
  5667. break;
  5668. case 0x10000008:
  5669. sprintf(type_str, "Image_CCS_dtb");
  5670. break;
  5671. case 0x10000009:
  5672. sprintf(type_str, "Image_CCS_zImage");
  5673. break;
  5674. case 0x1000000A:
  5675. sprintf(type_str, "Image_CCS_ramdisk");
  5676. break;
  5677. case 0x1000000B:
  5678. sprintf(type_str, "Image_CAHdeMO");
  5679. break;
  5680. case 0x1000000C:
  5681. sprintf(type_str, "Image_GBT");
  5682. break;
  5683. case 0x1000000D:
  5684. sprintf(type_str, "Image_Relay");
  5685. break;
  5686. case 0x1000000E:
  5687. sprintf(type_str, "Image_Fan");
  5688. break;
  5689. case 0x1000000F:
  5690. sprintf(type_str, "Image_LCM");
  5691. break;
  5692. case 0x10000010:
  5693. sprintf(type_str, "Image_Infy_PFC");
  5694. break;
  5695. case 0x10000011:
  5696. sprintf(type_str, "Image_Infy_DD");
  5697. break;
  5698. case 0x10000014:
  5699. sprintf(type_str, "Image_LED");
  5700. break;
  5701. case 0x10000015:
  5702. sprintf(type_str, "Image_LCM_APK");
  5703. break;
  5704. default:
  5705. sprintf(type_str, "Image_Unknown");
  5706. break;
  5707. }
  5708. }
  5709. char CheckUpdateProcess()
  5710. {
  5711. char _model[32];
  5712. char _type[32];
  5713. bool support = false;
  5714. bool OneImageOnly = false;
  5715. int isPass = true;
  5716. uint8_t retSucc = 0;
  5717. uint8_t retFail = 0;
  5718. byte target = 0;
  5719. char *new_str = NULL;
  5720. unsigned char *ptr = NULL;
  5721. int fd = 0;
  5722. int uartFd = 0;
  5723. unsigned int Type = 0;
  5724. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  5725. DIR *d;
  5726. struct dirent *dir;
  5727. char cmdBuf[128];
  5728. int availableMem = 0;
  5729. memset(_model, 0x00, sizeof(_model));
  5730. memset(_type, 0x00, sizeof(_type));
  5731. availableMem = getAvailableMemory() / (1024 * 1024);
  5732. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5733. // If available memory too small, free memory cache first
  5734. if(availableMem < MIN_AVAILABLE_MEMORY)
  5735. {
  5736. LOG_INFO("Available memory less than %d MBytes, free cache & kill ntpd.", MIN_AVAILABLE_MEMORY);
  5737. system("echo 3 > /proc/sys/vm/drop_caches");
  5738. KillNtpd();
  5739. availableMem = getAvailableMemory() / (1024 * 1024);
  5740. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5741. }
  5742. d = opendir("/mnt/");
  5743. if(d)
  5744. {
  5745. while((dir = readdir(d)) != NULL && !OneImageOnly)
  5746. {
  5747. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  5748. continue;
  5749. }
  5750. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  5751. strcat(new_str, "/mnt/");
  5752. strcat(new_str, dir->d_name);
  5753. fd = open(new_str, O_RDONLY);
  5754. if(fd < 0)
  5755. {
  5756. close(fd);
  5757. free(new_str);
  5758. continue;
  5759. }
  5760. LOG_INFO("%s\n", new_str);
  5761. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  5762. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  5763. //get the image length
  5764. ImageLen = read(fd, ptr, MaxLen);
  5765. if(ImageLen > 20)
  5766. {
  5767. memcpy(_model, ptr, 16);
  5768. Type = (((unsigned int)ptr[16]) << 24 | ((unsigned int)ptr[17]) << 16 | ((unsigned int)ptr[18]) << 8 | ((unsigned int)ptr[19]));
  5769. ImageTypeParsing(Type, _type);
  5770. LOG_INFO("Model: %s, Type: %s", _model, _type);
  5771. }
  5772. free(ptr);
  5773. if (ImageLen > 20)
  5774. {
  5775. support = false;
  5776. availableMem = getAvailableMemory() / (1024 * 1024);
  5777. LOG_INFO("AvailableMemory: %dMByte", availableMem);
  5778. //if(strcmp(model, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == EQUAL)
  5779. if(IsModelNameMatch(_model))
  5780. {
  5781. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5782. {
  5783. PutImageToAllParallelCabinet(new_str, dir->d_name);
  5784. WaitAllPCabinetStartUpgrade();
  5785. }
  5786. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.NeedUpgrade)
  5787. {
  5788. ShmChargerInfo->SCabinetControl.UpgradeState = Updating;
  5789. sleep(1);
  5790. }
  5791. switch (Type)
  5792. {
  5793. case 0x10000001:
  5794. case 0x10000002:
  5795. case 0x10000003:
  5796. case 0x10000004:
  5797. case 0x10000005:
  5798. support = true;
  5799. _UpgradeNeedReboot = TRUE;
  5800. LOG_INFO("Upgrade Flash Start...", Type);
  5801. isPass = Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5802. LOG_INFO("Upgrade Flash End, %s", isPass ? "Pass" : "Fail");
  5803. break;
  5804. case 0x10000006: // primary mcu
  5805. case 0x1000000D: // relay board
  5806. case 0x1000000E: // fan board
  5807. case 0x10000014: // led board
  5808. support = true;
  5809. target = 0x00;
  5810. if (Type == 0x10000006)
  5811. {
  5812. target = UPGRADE_PRI;
  5813. }
  5814. else if (Type == 0x1000000D)
  5815. {
  5816. target = UPGRADE_RB;
  5817. }
  5818. else if (Type == 0x1000000E)
  5819. {
  5820. target = UPGRADE_FAN;
  5821. }
  5822. else if (Type == 0x10000014)
  5823. {
  5824. target = UPGRADE_LED;
  5825. }
  5826. uartFd = InitComPort(target);
  5827. if(target == UPGRADE_RB)
  5828. {
  5829. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC1, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5830. if(isPass == PASS)
  5831. {
  5832. retSucc++;
  5833. }
  5834. else
  5835. {
  5836. retFail++;
  5837. }
  5838. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5839. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  5840. {
  5841. sleep(3);
  5842. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC2, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5843. }
  5844. }
  5845. else
  5846. {
  5847. isPass = Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5848. }
  5849. close(uartFd);
  5850. break;
  5851. }
  5852. if(support)
  5853. {
  5854. if(isPass == PASS)
  5855. {
  5856. retSucc++;
  5857. }
  5858. else
  5859. {
  5860. retFail++;
  5861. }
  5862. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5863. }
  5864. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5865. {
  5866. LOG_INFO("MCabinet upgrade completed");
  5867. WaitAllPCabinetUpgradeCompleted();
  5868. }
  5869. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.UpgradeState == Updating)
  5870. {
  5871. ShmChargerInfo->SCabinetControl.UpgradeState = Updated;
  5872. LOG_INFO("SCabinet upgrade completed");
  5873. WaitMCabinetUpgradeCompleted();
  5874. }
  5875. }
  5876. else
  5877. {
  5878. if(_model[0] == 'D' && (_model[1] == 'D' || _model[1] == 'X'))
  5879. {
  5880. OneImageOnly = true;
  5881. _UpgradeNeedReboot = TRUE;
  5882. LOG_INFO("Start Put Image To Dispenser");
  5883. PutImageToAllDispenser(new_str, dir->d_name, _model);
  5884. WaitAllDispenserUpgradeCompleted();
  5885. LOG_INFO("All Dispenser Upgrade Completed");
  5886. }
  5887. }
  5888. }
  5889. else
  5890. {
  5891. retFail++;
  5892. LOG_INFO("Model Name Is Not Match: %s", _model);
  5893. }
  5894. sprintf(cmdBuf, "rm -f %s", new_str);
  5895. system(cmdBuf);
  5896. close(fd);
  5897. free(new_str);
  5898. }
  5899. }
  5900. free(dir);
  5901. closedir(d);
  5902. if(retFail != 0)
  5903. {
  5904. return FAIL;
  5905. }
  5906. return PASS;
  5907. }
  5908. void TriggerDispenserConnectionTimeoutSetting(void)
  5909. {
  5910. BOOL trigger = FALSE;
  5911. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5912. {
  5913. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0 &&
  5914. !Is_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT))
  5915. {
  5916. trigger = TRUE;
  5917. Set_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT);
  5918. }
  5919. }
  5920. if(trigger)
  5921. {
  5922. LOG_INFO("********** Trigger Dispenser Connector Timeout Value: %d **********", ShmChargerInfo->CabinetMiscValue.ConnectionTimeout);
  5923. }
  5924. }
  5925. void CheckConnectionTimeout(void)
  5926. {
  5927. if(!Is_Ocpp_Task_Alive())
  5928. {
  5929. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5930. }
  5931. else
  5932. {
  5933. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  5934. {
  5935. _connectionTimeout = Get_Ocpp_ConnectionTimeOut();
  5936. if(_connectionTimeout <= 0)
  5937. {
  5938. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5939. }
  5940. }
  5941. else
  5942. {
  5943. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5944. }
  5945. }
  5946. if(_connectionTimeout != ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  5947. {
  5948. ShmChargerInfo->CabinetMiscValue.ConnectionTimeout = _connectionTimeout;
  5949. TriggerDispenserConnectionTimeoutSetting();
  5950. }
  5951. }
  5952. void CreateTimeoutFork()
  5953. {
  5954. pid_t timeoutPid;
  5955. timeoutPid = fork();
  5956. if (timeoutPid == 0)
  5957. {
  5958. GetClockTime(&_cmdSubPriority_time);
  5959. CheckConnectionTimeout();
  5960. LOG_INFO("Timeout Fork PID = %d", getpid());
  5961. while(true)
  5962. {
  5963. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  5964. {
  5965. CheckConnectionTimeout();
  5966. GetClockTime(&_cmdSubPriority_time);
  5967. }
  5968. sleep(1);
  5969. }
  5970. }
  5971. }
  5972. void CheckFactoryConfigFunction(void)
  5973. {
  5974. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  5975. {
  5976. char Buf[256];
  5977. sprintf(Buf, "cd /root;./FactoryConfig -m");
  5978. system(Buf);
  5979. system("rm -f /Storage/OCPP/OCPPConfiguration");
  5980. system("sync");
  5981. sleep(5);
  5982. system("reboot -f");
  5983. sleep(5);
  5984. system("reboot -f");
  5985. }
  5986. }
  5987. void CheckFwUpdateFunction(void)
  5988. {
  5989. char UpdateResult = PASS;
  5990. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  5991. {
  5992. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  5993. UpgradePrepare();
  5994. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  5995. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  5996. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  5997. LOG_INFO("ftp : update start.");
  5998. TryCloseWatchdog();
  5999. //KillTask();
  6000. system("killall Module_PrimaryComm");
  6001. system("killall Module_InternalComm");
  6002. UpdateResult = CheckUpdateProcess();
  6003. if (UpdateResult == PASS)
  6004. {
  6005. LOG_INFO("ftp : update complete.");
  6006. }
  6007. else
  6008. {
  6009. LOG_INFO("ftp : update fail.");
  6010. }
  6011. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  6012. sleep(10);
  6013. if(_UpgradeNeedReboot)
  6014. {
  6015. system("reboot -f");
  6016. }
  6017. else
  6018. {
  6019. KillAllTask();
  6020. system("/usr/bin/run_evse_restart.sh");
  6021. }
  6022. }
  6023. else if(Is_Ocpp_UpdateFirmwareReq())
  6024. {
  6025. Clean_Ocpp_UpdateFirmwareReq();
  6026. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  6027. UpgradePrepare();
  6028. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  6029. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  6030. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  6031. if(Is_Ocpp_UpdateFirmwareStatus("Downloaded"))
  6032. {
  6033. LOG_INFO("Backend : update start.");
  6034. TryCloseWatchdog();
  6035. Set_Ocpp_UpdateFirmwareStatus("Installing");
  6036. Set_Ocpp_FirmwareStatusNotificationReq();
  6037. //KillTask();
  6038. system("killall Module_PrimaryComm");
  6039. system("killall Module_InternalComm");
  6040. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6041. {
  6042. setChargerMode(_index, MODE_UPDATE);
  6043. }
  6044. UpdateResult = CheckUpdateProcess();
  6045. if (UpdateResult == PASS)
  6046. {
  6047. LOG_INFO("Backend : update complete.");
  6048. Set_Ocpp_UpdateFirmwareStatus("Installed");
  6049. }
  6050. else
  6051. {
  6052. LOG_INFO("Backend : update fail.");
  6053. Set_Ocpp_UpdateFirmwareStatus("InstallationFailed");
  6054. }
  6055. Set_Ocpp_FirmwareStatusNotificationReq();
  6056. sleep(10);
  6057. if(_UpgradeNeedReboot)
  6058. {
  6059. system("reboot -f");
  6060. }
  6061. else
  6062. {
  6063. KillAllTask();
  6064. system("/usr/bin/run_evse_restart.sh");
  6065. }
  6066. }
  6067. }
  6068. }
  6069. void ExecuteWriteFlash(void)
  6070. {
  6071. struct SysConfigData config;
  6072. memcpy(&config, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  6073. config.SwitchDebugFlag = NO;
  6074. strcpy((char *) config.UserId, "");
  6075. int result = StoreUsrConfigData(&config);
  6076. if(result != 1)
  6077. {
  6078. LOG_INFO("Store User Config NG");
  6079. }
  6080. else
  6081. {
  6082. LOG_INFO("Store User Config OK");
  6083. }
  6084. }
  6085. struct timespec _WiringInfo_time;
  6086. int CheckFlashUpdate(void)
  6087. {
  6088. if(ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged)
  6089. {
  6090. ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged = false;
  6091. LOG_INFO("Flash Config Changed");
  6092. GetClockTime(&_WiringInfo_time);
  6093. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = true;
  6094. }
  6095. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable)
  6096. {
  6097. // wait for 30s to write flash or write flash immediately
  6098. if((GetTimeoutValue(_WiringInfo_time) / uSEC_VAL) >= WIRING_INFO_DELAY ||
  6099. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashImmediately)
  6100. {
  6101. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = false;
  6102. LOG_INFO("Start Writing Flash");
  6103. pid_t writePid = fork();
  6104. if(writePid == 0)
  6105. {
  6106. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = true;
  6107. ExecuteWriteFlash();
  6108. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = false;
  6109. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset)
  6110. {
  6111. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset = false;
  6112. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  6113. LOG_INFO("Write Flash OK And Need Soft Reset");
  6114. }
  6115. return NO;
  6116. }
  6117. LOG_INFO("Write Flash Conpleted");
  6118. }
  6119. }
  6120. return YES;
  6121. }
  6122. void CheckSoftHardReset(void)
  6123. {
  6124. if(ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset)
  6125. {
  6126. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = false;
  6127. LOG_INFO("********** Need Soft Reset **********");
  6128. sleep(5);
  6129. Kill_Ocpp_Service();
  6130. KillAllTask();
  6131. TryCloseWatchdog();
  6132. system("/usr/bin/run_evse_restart.sh");
  6133. }
  6134. if(ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset)
  6135. {
  6136. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = false;
  6137. LOG_INFO("********** Need Hard Reset **********");
  6138. sleep(5);
  6139. system("reboot -f");
  6140. }
  6141. }
  6142. struct timespec _Reboot_time;
  6143. void TriggerDispenserHardwareReboot(void)
  6144. {
  6145. BOOL trigger = FALSE;
  6146. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6147. {
  6148. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6149. {
  6150. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm == 0 &&
  6151. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  6152. {
  6153. trigger = TRUE;
  6154. Set_Dispenser_MiscCommand(i, MISC_DISP_HARDWARE_REBOOT);
  6155. }
  6156. }
  6157. }
  6158. if(trigger)
  6159. {
  6160. GetClockTime(&_Reboot_time);
  6161. LOG_INFO("********** Trigger Dispenser Hardware Reboot **********");
  6162. }
  6163. }
  6164. BOOL IsDispenserHardwareRebootResponsed(void)
  6165. {
  6166. BOOL response_ok = TRUE;
  6167. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6168. {
  6169. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6170. {
  6171. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  6172. {
  6173. response_ok = FALSE;
  6174. // 10s for reboot request timeout
  6175. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  6176. {
  6177. response_ok = TRUE;
  6178. LOG_INFO("Reboot request timeout");
  6179. }
  6180. }
  6181. }
  6182. }
  6183. return response_ok;
  6184. }
  6185. struct timespec _Reset_time;
  6186. void TriggerDispenserSoftwareReset(void)
  6187. {
  6188. BOOL trigger = FALSE;
  6189. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6190. {
  6191. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6192. {
  6193. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  6194. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  6195. {
  6196. trigger = TRUE;
  6197. Set_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET);
  6198. }
  6199. }
  6200. }
  6201. if(trigger)
  6202. {
  6203. GetClockTime(&_Reset_time);
  6204. LOG_INFO("********** Trigger Dispenser Software Reset **********");
  6205. }
  6206. }
  6207. BOOL IsDispenserSoftwareResetResponsed(void)
  6208. {
  6209. BOOL response_ok = TRUE;
  6210. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6211. {
  6212. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6213. {
  6214. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  6215. {
  6216. response_ok = FALSE;
  6217. // 10s for reset request timeout
  6218. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  6219. {
  6220. response_ok = TRUE;
  6221. LOG_INFO("Reset request timeout");
  6222. }
  6223. }
  6224. }
  6225. }
  6226. return response_ok;
  6227. }
  6228. // return time offset
  6229. int TimeOffsetKeyParsing(char *StringItem)
  6230. {
  6231. int offset = 0;
  6232. int hour = 0, minute = 0, sign = 1;
  6233. char *ptrSave, *ptrToken;
  6234. char strSource[501];
  6235. memset(strSource, 0x00, sizeof(strSource));
  6236. strcpy(strSource, StringItem);
  6237. if(strSource[0] != '+' && strSource[0] != '-')
  6238. {
  6239. return offset;
  6240. }
  6241. sign = strSource[0] == '+' ? (1) : (-1);
  6242. ptrToken = strtok_r(&strSource[1], ":", &ptrSave);
  6243. if(ptrToken != NULL)
  6244. {
  6245. hour = atoi(ptrToken);
  6246. ptrToken = strtok_r(NULL, ":", &ptrSave);
  6247. if(ptrToken != NULL)
  6248. {
  6249. minute = atoi(ptrToken);
  6250. offset = ((hour * 60) + minute) * sign;
  6251. }
  6252. }
  6253. return offset;
  6254. }
  6255. char MyDefaultPriceString[501];
  6256. void DefaultPriceHandler(void)
  6257. {
  6258. char price[501];
  6259. char defaultPrice[512];
  6260. memset(defaultPrice, 0x00, sizeof(defaultPrice));
  6261. Get_Ocpp_DefaultPrice((char *)price);
  6262. if(strcmp(MyDefaultPriceString, (char *)price) != EQUAL)
  6263. {
  6264. float ConnectionFee = 0, OccupancyFee = 0;
  6265. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6266. {
  6267. ConnectionFee = PriceParsing(price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6268. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = PriceParsing(price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6269. OccupancyFee = PriceParsing(price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6270. LOG_INFO("******** Customer Audi Default Price *******");
  6271. LOG_INFO("********** Connection Fee: %4.2f **********", ConnectionFee);
  6272. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6273. LOG_INFO("********** Occupancy Fee: %4.2f **********", OccupancyFee);
  6274. }
  6275. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6276. {
  6277. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6278. ShellPriceParsing(price, defaultPrice);
  6279. LOG_INFO("******* Customer Shell Default Price *******");
  6280. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6281. }
  6282. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6283. {
  6284. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = TccDefaultPriceParsing(price, &ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6285. LOG_INFO("******* Customer Tcc Default Price *******");
  6286. LOG_INFO("*** TimeOfDayPricing: %4.2f", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6287. }
  6288. else
  6289. {
  6290. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6291. strcpy(defaultPrice, price);
  6292. LOG_INFO("********** Standard Default Price **********");
  6293. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6294. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6295. }
  6296. strcpy(MyDefaultPriceString, price);
  6297. }
  6298. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6299. {
  6300. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = GetNowTimePricing(&ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6301. }
  6302. if(ShmChargerInfo->CabinetMiscValue.DefaultPrice != ShmChargerInfo->PriceAndReceiptInfo.CurrentRate)
  6303. {
  6304. ShmChargerInfo->CabinetMiscValue.DefaultPrice = ShmChargerInfo->PriceAndReceiptInfo.CurrentRate;
  6305. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6306. {
  6307. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6308. {
  6309. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE))
  6310. {
  6311. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE);
  6312. }
  6313. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY))
  6314. {
  6315. Set_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY);
  6316. }
  6317. }
  6318. }
  6319. LOG_INFO("********** Trigger Dispenser Default Price Value **********");
  6320. }
  6321. if(strlen(defaultPrice) > 0)
  6322. {
  6323. strcpy(ShmChargerInfo->PriceAndReceiptInfo.DefaultPriceString, defaultPrice);
  6324. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6325. {
  6326. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6327. {
  6328. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING))
  6329. {
  6330. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING);
  6331. }
  6332. }
  6333. }
  6334. LOG_INFO("********** Trigger Dispenser Default Price Value String **********");
  6335. }
  6336. }
  6337. void UserPriceHandler(void)
  6338. {
  6339. char UserId[32];
  6340. char price[500];
  6341. char userPrice[512];
  6342. memset(userPrice, 0x00, sizeof(userPrice));
  6343. Get_Ocpp_UserPrice(UserId, price);
  6344. if(strlen((char *)UserId) > 0 && strlen((char *)price) > 0)
  6345. {
  6346. float ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  6347. float AccountBalance = 0;
  6348. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6349. {
  6350. ConnectionFee = PriceParsing((char *)price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6351. CurrentRate = PriceParsing((char *)price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6352. OccupancyFee = PriceParsing((char *)price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6353. AccountBalance = PriceParsing((char *)price, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6354. LOG_INFO("********* Customer Audi User Price *********");
  6355. LOG_INFO("********* User Id: [%s]", UserId);
  6356. LOG_INFO("********* Connection Fee: [%7.2f]", ConnectionFee);
  6357. LOG_INFO("********* Current Rate: [%7.2f]", CurrentRate);
  6358. LOG_INFO("********* Occupancy Fee: [%7.2f]", OccupancyFee);
  6359. LOG_INFO("********* Account Balance: [%7.2f]", AccountBalance);
  6360. }
  6361. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6362. {
  6363. ShellPriceParsing(price, userPrice);
  6364. LOG_INFO("******** Customer Shell User Price *********");
  6365. LOG_INFO("******** User Id: [%s]", UserId);
  6366. LOG_INFO("******** User Price: [%s]", userPrice);
  6367. }
  6368. else
  6369. {
  6370. CurrentRate = PriceParsing((char *)price, "chargineFee", _PHIHONG_PRICE_SPLIT_KEY);
  6371. OccupancyFee = PriceParsing((char *)price, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6372. strcpy(userPrice, price);
  6373. LOG_INFO("*********** Standard User Price ************");
  6374. LOG_INFO("*********** User Id: [%s]", UserId);
  6375. LOG_INFO("*********** Current Rate: [%7.2f]", CurrentRate);
  6376. LOG_INFO("*********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6377. LOG_INFO("*********** User Price: [%s]", userPrice);
  6378. }
  6379. #if 0
  6380. LOG_INFO("********** User Id: %s **********", UserId);
  6381. LOG_INFO("********** User Price: %s", price);
  6382. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6383. LOG_INFO("********** Current Rate: %7.2f **********", CurrentRate);
  6384. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6385. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6386. #endif
  6387. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6388. {
  6389. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  6390. {
  6391. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  6392. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  6393. {
  6394. LOG_INFO("********** Connector Id %d User Price %4.2f, Account Balance %7.2f **********",
  6395. (i + 1), CurrentRate, AccountBalance);
  6396. }
  6397. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  6398. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  6399. if(strlen(userPrice) > 0)
  6400. {
  6401. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[i][0], userPrice);
  6402. Set_Connector_MiscCommand(i, MISC_CONN_USER_PRICE_STRING);
  6403. }
  6404. if(chargingInfo[i]->SystemStatus == S_AUTHORIZING)
  6405. {
  6406. AnnounceAccountBalance(i, AccountBalance);
  6407. }
  6408. }
  6409. }
  6410. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6411. {
  6412. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthResult == _AuthResult_Valid)
  6413. {
  6414. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthId, UserId) == EQUAL)
  6415. {
  6416. if(strlen(userPrice) > 0)
  6417. {
  6418. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[i][0], userPrice);
  6419. }
  6420. }
  6421. }
  6422. }
  6423. Clean_Ocpp_UserPrice();
  6424. }
  6425. }
  6426. void RunningCostParsing(int gunIndex, char *strCost)
  6427. {
  6428. float ConnectionFee = 0, SessionFee = 0, OccupancyFee = -1, TotalCost = 0, EnergyCost = -1;
  6429. float AccountBalance = 0;
  6430. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6431. {
  6432. ConnectionFee = PriceParsing((char *)strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6433. SessionFee = PriceParsing((char *)strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6434. OccupancyFee = PriceParsing((char *)strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6435. TotalCost = PriceParsing((char *)strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6436. AccountBalance = PriceParsing((char *)strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6437. LOG_INFO("******** Customer Audi Gun %d Running Cost ********", (gunIndex + 1));
  6438. LOG_INFO("******** Connection Fee: [%7.2f]", ConnectionFee);
  6439. LOG_INFO("******** Session Fee: [%7.2f]", SessionFee);
  6440. LOG_INFO("******** Occupancy Fee: [%7.2f]", OccupancyFee);
  6441. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6442. LOG_INFO("******** Account Balance: [%7.2f]", AccountBalance);
  6443. }
  6444. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6445. {
  6446. TotalCost = ShellRunningCostParsing(strCost);
  6447. LOG_INFO("******** Customer Shell Gun %d Running Cost *******", (gunIndex + 1));
  6448. LOG_INFO("******** Gun %d Running Cost", (gunIndex + 1));
  6449. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6450. }
  6451. else
  6452. {
  6453. EnergyCost = PriceParsing((char *)strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6454. TotalCost = EnergyCost;
  6455. OccupancyFee = PriceParsing((char *)strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6456. LOG_INFO("********** Standard Gun %d Running Cost ***********", (gunIndex + 1));
  6457. LOG_INFO("********** Energy Cost: [%7.2f]", EnergyCost);
  6458. LOG_INFO("********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6459. LOG_INFO("********** Total Cost: [%7.2f]", TotalCost);
  6460. }
  6461. #if 0
  6462. LOG_INFO("********** Connector Id: %d Running Cost **********", (gunIndex + 1));
  6463. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6464. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6465. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6466. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6467. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6468. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6469. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance)
  6470. {
  6471. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance %7.2f **********",
  6472. (gunIndex + 1), TotalCost, AccountBalance);
  6473. }
  6474. #endif
  6475. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = TotalCost;
  6476. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = AccountBalance;
  6477. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = EnergyCost;
  6478. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = OccupancyFee;
  6479. }
  6480. void RunningCostHandler(void)
  6481. {
  6482. char strCost[256];
  6483. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6484. {
  6485. Get_Ocpp_RunningCost(i, strCost);
  6486. if(strlen((char *)strCost) > 0)
  6487. {
  6488. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  6489. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_CHARGING)
  6490. {
  6491. RunningCostParsing(i, strCost);
  6492. }
  6493. Clean_Ocpp_RunningCost(i);
  6494. }
  6495. }
  6496. }
  6497. void EnableConnectorBillRequest(int gunIndex, ChargingBillInfoData *bill)
  6498. {
  6499. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6500. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6501. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = bill->TxId;
  6502. memcpy(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, &bill->FeeInfo, sizeof(FeeInfoData));
  6503. }
  6504. void DisableConnectorBill(int gunIndex)
  6505. {
  6506. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Idle;
  6507. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6508. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = 0;
  6509. memset(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, 0x00, sizeof(FeeInfoData));
  6510. }
  6511. void FinalCostParsing(int txId, char *strCost, ChargingBillInfoData *_bill)
  6512. {
  6513. memset(_bill, 0x00, sizeof(ChargingBillInfoData));
  6514. _bill->TxId = txId;
  6515. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6516. {
  6517. _bill->FeeInfo.ConnectionFee = PriceParsing(strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6518. _bill->FeeInfo.SessionFee = PriceParsing(strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6519. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6520. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6521. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6522. }
  6523. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6524. {
  6525. _bill->FeeInfo.TotalCost = ShellFinalCostParsing(strCost);
  6526. GetShellReceiptInfo(strCost, (char *)_bill->Receipt);
  6527. }
  6528. else
  6529. {
  6530. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6531. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6532. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "accountBalance", _PHIHONG_PRICE_SPLIT_KEY);
  6533. _bill->FeeInfo.CostDiscount = PriceParsing(strCost, "couponPoint", _PHIHONG_PRICE_SPLIT_KEY);
  6534. _bill->FeeInfo.RemainAmount = PriceParsing(strCost, "farewellMessage", _PHIHONG_PRICE_SPLIT_KEY);
  6535. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0)
  6536. {
  6537. sprintf(_bill->Receipt, "%s%d",
  6538. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0],
  6539. txId);
  6540. }
  6541. }
  6542. }
  6543. void UpdateCurrentFinalCost(int gunIndex, ChargingBillInfoData *_bill)
  6544. {
  6545. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6546. {
  6547. LOG_INFO("********* Customer Audi Gun %d Final Cost *********", (gunIndex + 1));
  6548. LOG_INFO("********* Connection Fee: [%7.2f]", _bill->FeeInfo.ConnectionFee);
  6549. LOG_INFO("********* Session Fee: [%7.2f]", _bill->FeeInfo.SessionFee);
  6550. LOG_INFO("********* Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6551. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6552. LOG_INFO("********* Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6553. }
  6554. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6555. {
  6556. LOG_INFO("********* Customer Shell Gun %d Final Cost ********", (gunIndex + 1));
  6557. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6558. LOG_INFO("********* Receipt: [%s]", _bill->Receipt);
  6559. }
  6560. else
  6561. {
  6562. LOG_INFO("*********** Standard Gun %d Final Cost ************", (gunIndex + 1));
  6563. LOG_INFO("*********** Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6564. LOG_INFO("*********** Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6565. LOG_INFO("*********** Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6566. LOG_INFO("*********** Coupon Point: [%7.2f]", _bill->FeeInfo.CostDiscount);
  6567. LOG_INFO("*********** Remain Amount: [%7.2f]", _bill->FeeInfo.RemainAmount);
  6568. LOG_INFO("*********** Receipt: [%s]", _bill->Receipt);
  6569. }
  6570. #if 0
  6571. LOG_INFO("********** Connector Id: %d Final Cost **********", (gunIndex + 1));
  6572. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6573. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6574. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6575. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6576. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6577. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6578. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance ||
  6579. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount != CostDiscount)
  6580. {
  6581. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance: %7.2f, Discount: %d.%02d **********",
  6582. (gunIndex + 1), TotalCost, AccountBalance, (CostDiscount / 100), (CostDiscount % 100));
  6583. }
  6584. #endif
  6585. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = _bill->FeeInfo.TotalCost;
  6586. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = _bill->FeeInfo.AccountBalance;
  6587. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount = _bill->FeeInfo.CostDiscount;
  6588. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = _bill->FeeInfo.EnergyCost;
  6589. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = _bill->FeeInfo.ParkingFee;
  6590. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemainAmount = _bill->FeeInfo.RemainAmount;
  6591. Set_Connector_MiscCommand(gunIndex, MISC_CONN_FINAL_COST);
  6592. if(strlen(_bill->Receipt) > 0)
  6593. {
  6594. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], _bill->Receipt);
  6595. Set_Connector_MiscCommand(gunIndex, MISC_CONN_RECEIPT);
  6596. }
  6597. DB_Update_FinalCost_Status(localDb, _bill->TxId, BILL_SATAUS_Bypass_OK);
  6598. }
  6599. bool IsBillStatusChanged(int gunIndex)
  6600. {
  6601. return ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus == ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus ?
  6602. false : true;
  6603. }
  6604. void SyncBillStatus(int gunIndex)
  6605. {
  6606. ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6607. }
  6608. void EnableUndisposedBill(int dispenserIndex)
  6609. {
  6610. LOG_INFO("Enable Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6611. _isUndisposedFinalCost[dispenserIndex] = true;
  6612. }
  6613. void FindUndisposedAndTrigger(int dispenserIndex)
  6614. {
  6615. int txId = 0;
  6616. char strCost[256];
  6617. ChargingBillInfoData _bill;
  6618. bool find = false;
  6619. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6620. {
  6621. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6622. txId = DB_Get_Undisposed_FinalCost(localDb, gun, strCost);
  6623. if(txId >= 0)
  6624. {
  6625. LOG_INFO("Find Gun %d Undisposed ChargingBill, txId: [%d]", gun + 1, txId);
  6626. FinalCostParsing(txId, (char *)strCost, &_bill);
  6627. EnableConnectorBillRequest(gun, &_bill);
  6628. find = true;
  6629. break;
  6630. }
  6631. }
  6632. if(!find)
  6633. {
  6634. LOG_INFO("Dispenser %d Undisposed ChargingBill Not Found", dispenserIndex + 1);
  6635. _isUndisposedFinalCost[dispenserIndex] = false;
  6636. }
  6637. }
  6638. bool IsDispenserBillStatusBusy(int dispenserIndex)
  6639. {
  6640. bool busy = false;
  6641. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6642. {
  6643. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6644. if(ShmChargerInfo->AllBill.GunBill[gun].BillStatus != _BillStatus_Idle)
  6645. {
  6646. busy = true;
  6647. break;
  6648. }
  6649. }
  6650. return busy;
  6651. }
  6652. void DispenserBillRequestProcess(int dispenserIndex)
  6653. {
  6654. if(IsDispenserIdle(dispenserIndex) && !IsDispenserBillStatusBusy(dispenserIndex))
  6655. {
  6656. if(!_canUpdateFinalCost[dispenserIndex])
  6657. {
  6658. _canUpdateFinalCost[dispenserIndex] = true;
  6659. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6660. }
  6661. if(_canUpdateFinalCost[dispenserIndex] && _isUndisposedFinalCost[dispenserIndex])
  6662. {
  6663. if(GetSecTimeoutValue(_DispenserBill_Time[dispenserIndex]) >= FINAL_COST_UPDATE_TIME)
  6664. {
  6665. //LOG_INFO("Try Find Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6666. FindUndisposedAndTrigger(dispenserIndex);
  6667. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6668. }
  6669. }
  6670. }
  6671. else
  6672. {
  6673. _canUpdateFinalCost[dispenserIndex] = false;
  6674. }
  6675. }
  6676. void GunBillRequestProcess(int gunIndex)
  6677. {
  6678. unsigned char _status = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6679. switch(_status)
  6680. {
  6681. case _BillStatus_Idle:
  6682. if(IsBillStatusChanged(gunIndex))
  6683. {
  6684. SyncBillStatus(gunIndex);
  6685. }
  6686. break;
  6687. case _BillStatus_MiscReq:
  6688. if(IsBillStatusChanged(gunIndex))
  6689. {
  6690. Database_INFO("Gun %d ChargingBill MiscRequest", gunIndex + 1);
  6691. SyncBillStatus(gunIndex);
  6692. }
  6693. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_WaitMiscClean;
  6694. Set_Connector_MiscCommand(gunIndex, MISC_CONN_CHARGING_BILL);
  6695. break;
  6696. case _BillStatus_WaitMiscClean:
  6697. if(IsBillStatusChanged(gunIndex))
  6698. {
  6699. SyncBillStatus(gunIndex);
  6700. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6701. }
  6702. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6703. {
  6704. Database_INFO("Gun %d ChargingBill WaitMisc Timeout", gunIndex + 1);
  6705. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6706. }
  6707. break;
  6708. case _BillStatus_MiscClean:
  6709. if(IsBillStatusChanged(gunIndex))
  6710. {
  6711. Database_INFO("Gun %d ChargingBill MiscClean", gunIndex + 1);
  6712. SyncBillStatus(gunIndex);
  6713. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6714. }
  6715. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6716. {
  6717. Database_INFO("Gun %d ChargingBill Wait Dispenser Timeout", gunIndex + 1);
  6718. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt++;
  6719. if(ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt >= MAX_FINAL_COST_MISC_COUNT)
  6720. {
  6721. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6722. }
  6723. else
  6724. {
  6725. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6726. }
  6727. }
  6728. break;
  6729. case _BillStatus_UpdateDone:
  6730. if(IsBillStatusChanged(gunIndex))
  6731. {
  6732. Database_INFO("Gun %d ChargingBill UpdateDone", gunIndex + 1);
  6733. SyncBillStatus(gunIndex);
  6734. }
  6735. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Completed;
  6736. break;
  6737. case _BillStatus_Completed:
  6738. if(IsBillStatusChanged(gunIndex))
  6739. {
  6740. SyncBillStatus(gunIndex);
  6741. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6742. }
  6743. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6744. {
  6745. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_End;
  6746. }
  6747. break;
  6748. case _BillStatus_Fail:
  6749. if(IsBillStatusChanged(gunIndex))
  6750. {
  6751. SyncBillStatus(gunIndex);
  6752. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_FAIL);
  6753. }
  6754. DisableConnectorBill(gunIndex);
  6755. break;
  6756. case _BillStatus_End:
  6757. if(IsBillStatusChanged(gunIndex))
  6758. {
  6759. SyncBillStatus(gunIndex);
  6760. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_OK);
  6761. }
  6762. DisableConnectorBill(gunIndex);
  6763. break;
  6764. default:
  6765. DisableConnectorBill(gunIndex);
  6766. break;
  6767. }
  6768. }
  6769. void FinalCostHandler(void)
  6770. {
  6771. int txId = 0;
  6772. char strCost[256];
  6773. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6774. {
  6775. txId = Get_Ocpp_FinalCostTxId(i);
  6776. Get_Ocpp_FinalCost(i, strCost);
  6777. if(strlen((char *)strCost) > 0)
  6778. {
  6779. DB_Insert_FinalCost(localDb, i, txId, strCost);
  6780. Database_INFO("Insert FinalCost: Gun %d TxId[%d]", i + 1, txId);
  6781. if(ShmChargerInfo->UserTransaction[i].TransactionId == txId)
  6782. {
  6783. ChargingBillInfoData _bill;
  6784. FinalCostParsing(txId, (char *)strCost, &_bill);
  6785. UpdateCurrentFinalCost(i, &_bill);
  6786. }
  6787. else
  6788. {
  6789. EnableUndisposedBill(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex);
  6790. }
  6791. Clean_Ocpp_FinalCost(i);
  6792. }
  6793. }
  6794. if(ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill)
  6795. {
  6796. ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill = false;
  6797. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6798. {
  6799. _isUndisposedFinalCost[i] = true;
  6800. }
  6801. }
  6802. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6803. {
  6804. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  6805. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  6806. {
  6807. DispenserBillRequestProcess(i);
  6808. }
  6809. else
  6810. {
  6811. _canUpdateFinalCost[i] = false;
  6812. }
  6813. }
  6814. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6815. {
  6816. GunBillRequestProcess(i);
  6817. }
  6818. }
  6819. char MyTimeOffsetString[501];
  6820. void TimeOffsetHandler(void)
  6821. {
  6822. char timeOffset[501];
  6823. Get_Ocpp_TimeOffset((char *)timeOffset);
  6824. if(strcmp(MyTimeOffsetString, (char *)timeOffset) != EQUAL)
  6825. {
  6826. BOOL trigger = FALSE;
  6827. int offset = 0;
  6828. offset = TimeOffsetKeyParsing(timeOffset);
  6829. LOG_INFO("********** Time Offset String [%s] -> Value [%d] -> Time [%s%02d:%02d]",
  6830. timeOffset, offset, offset >= 0 ? "+" : "-", (abs(offset) / 60), (abs(offset) % 60));
  6831. ShmChargerInfo->CabinetMiscValue.TimeOffset = offset;
  6832. #if 0
  6833. time_t timep;
  6834. struct tm *tm;
  6835. time(&timep);
  6836. tm = localtime(&timep);
  6837. LOG_INFO(" NowTime: %d/%02d/%02d %02d:%02d:%02d",
  6838. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  6839. timep += (offset * 60);
  6840. tm = localtime(&timep);
  6841. LOG_INFO("OffsetTime: %d/%02d/%02d %02d:%02d:%02d",
  6842. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  6843. #endif
  6844. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6845. {
  6846. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6847. {
  6848. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET))
  6849. {
  6850. trigger = TRUE;
  6851. Set_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET);
  6852. }
  6853. }
  6854. }
  6855. if(trigger)
  6856. {
  6857. LOG_INFO("********** Trigger Dispenser Time Offset **********");
  6858. }
  6859. strcpy(MyTimeOffsetString, timeOffset);
  6860. }
  6861. }
  6862. struct timespec _Cost_Price_time;
  6863. void CheckOcppCostAndPrice(void)
  6864. {
  6865. // 5s for reboot request timeout
  6866. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  6867. {
  6868. GetClockTime(&_Cost_Price_time);
  6869. DefaultPriceHandler();
  6870. UserPriceHandler();
  6871. RunningCostHandler();
  6872. TimeOffsetHandler();
  6873. }
  6874. FinalCostHandler();
  6875. }
  6876. void CheckRemoteStartNoIdRequest(void)
  6877. {
  6878. if(Is_Ocpp_RemoteStartWaitReq())
  6879. {
  6880. if(!ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6881. {
  6882. LOG_INFO("Ocpp remote start without connector id is trigger");
  6883. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = true;
  6884. AnnounceRemoteStartNoID();
  6885. GetClockTime(&_RemoteStartNoId_Time);
  6886. }
  6887. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6888. {
  6889. // to avoid keep RemoteStartNoId state when ocpp is disconnected
  6890. if((GetTimeoutValue(_RemoteStartNoId_Time) / uSEC_VAL) >= (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + CONNECTION_TIME_OFFSET))
  6891. {
  6892. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  6893. }
  6894. }
  6895. }
  6896. else
  6897. {
  6898. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6899. {
  6900. LOG_INFO("Ocpp remote start without connector id is expired");
  6901. }
  6902. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  6903. }
  6904. }
  6905. void CheckOcppPageChangeRequest(void)
  6906. {
  6907. int gunId = 0, pageIndex = 0;
  6908. if(Is_Ocpp_LcdOverideReq(&gunId, &pageIndex))
  6909. {
  6910. Clean_Ocpp_LcdOverideReq();
  6911. LOG_INFO("Gun %d Ocpp Line Status [%d] is trigger", gunId, pageIndex);
  6912. AnnounceLineStatusCode(gunId - 1, pageIndex);
  6913. }
  6914. }
  6915. void CheckLcmPageChangeRequest(void)
  6916. {
  6917. bool isTrigger = false;
  6918. CheckRemoteStartNoIdRequest();
  6919. CheckOcppPageChangeRequest();
  6920. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  6921. {
  6922. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  6923. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  6924. {
  6925. isTrigger = false;
  6926. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  6927. {
  6928. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  6929. if(ShmChargerInfo->ConnectorPageReq[gun].CtrlValue != 0)
  6930. {
  6931. isTrigger = true;
  6932. break;
  6933. }
  6934. }
  6935. if(isTrigger)
  6936. {
  6937. Set_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  6938. }
  6939. else
  6940. {
  6941. Clean_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  6942. }
  6943. }
  6944. }
  6945. }
  6946. void CheckStationInfo(void)
  6947. {
  6948. int station_id = 0;
  6949. char station_name[64];
  6950. if(Is_Ocpp_ChargerStationReq())
  6951. {
  6952. memset(station_name, 0x00, sizeof(station_name));
  6953. Get_Ocpp_ChargerStationInfo(&station_id, station_name);
  6954. if(strcmp(station_name, ShmChargerInfo->StationInfo.StationName) != EQUAL ||
  6955. ShmChargerInfo->StationInfo.StationID != station_id)
  6956. {
  6957. strcpy(ShmChargerInfo->StationInfo.StationName, station_name);
  6958. ShmChargerInfo->StationInfo.StationID = station_id;
  6959. LOG_INFO("Station ID: %d, Name: %s", ShmChargerInfo->StationInfo.StationID, ShmChargerInfo->StationInfo.StationName);
  6960. AnnounceStationInfo();
  6961. }
  6962. Clean_Ocpp_ChargerStationReq();
  6963. }
  6964. }
  6965. void CheckWeatherInfo(void)
  6966. {
  6967. int weatherID = 0;
  6968. float temperature = 0;
  6969. char strWeather[16];
  6970. 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};
  6971. if(Is_Ocpp_WeatherReq())
  6972. {
  6973. Get_Ocpp_WeatherInfo(&weatherID, &temperature);
  6974. if(weatherID != ShmChargerInfo->StationInfo.WeatherID ||
  6975. temperature != ShmChargerInfo->StationInfo.Temperature)
  6976. {
  6977. ShmChargerInfo->StationInfo.WeatherID = weatherID;
  6978. ShmChargerInfo->StationInfo.Temperature = temperature;
  6979. if(ShmChargerInfo->StationInfo.WeatherID <= _WEATHER_Mist)
  6980. {
  6981. strcpy(strWeather, str_weather_type[ShmChargerInfo->StationInfo.WeatherID]);
  6982. }
  6983. else
  6984. {
  6985. sprintf(strWeather, "%d", ShmChargerInfo->StationInfo.WeatherID);
  6986. }
  6987. LOG_INFO("Weather: [%s][%d], Temperature: %4.1f", strWeather, ShmChargerInfo->StationInfo.WeatherID, ShmChargerInfo->StationInfo.Temperature);
  6988. AnnounceStationInfo();
  6989. }
  6990. Clean_Ocpp_WeatherReq();
  6991. }
  6992. }
  6993. bool _WriteDeductBusy[CONNECTOR_QUANTITY] = {0};
  6994. void CheckDeductResultRequest(void)
  6995. {
  6996. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  6997. char cardNo[21];
  6998. char appNo[10];
  6999. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7000. {
  7001. if(Is_DeductResultReq(i))
  7002. {
  7003. if(!_WriteDeductBusy[i])
  7004. {
  7005. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7006. {
  7007. _WriteDeductBusy[i] = true;
  7008. //Set_Ocpp_DeductTransaction(_BackupDeductResult[i].TransactionId);
  7009. //Set_Ocpp_DeductCreditNo((char *)&_BackupDeductResult[i].creditNo);
  7010. //Set_Ocpp_DeductVemData("");
  7011. //Set_Ocpp_DeductAmount(((double)_BackupDeductResult[i].DeductAmount / 100));
  7012. //Set_Ocpp_DeductResult(_BackupDeductResult[i].DeductResult);
  7013. //Set_Ocpp_DonateInvoice(_BackupDeductResult[i].IsDonateInvoice);
  7014. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->DeductResultReq[i]))
  7015. {
  7016. memset(cardNo, 0x00, sizeof(cardNo));
  7017. memcpy(cardNo, (char *)&ShmChargerInfo->DeductResultReq[i].creditNo[0], sizeof(ShmChargerInfo->DeductResultReq[i].creditNo));
  7018. memset(appNo, 0x00, sizeof(appNo));
  7019. memcpy(appNo, (char *)&ShmChargerInfo->DeductResultReq[i].ApprovalNumber[0], sizeof(ShmChargerInfo->DeductResultReq[i].ApprovalNumber));
  7020. Set_Ocpp_ReportCreditDeductReq();
  7021. LOG_INFO("DeductReport Gun %d TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7022. i + 1,
  7023. ShmChargerInfo->DeductResultReq[i].TransactionId,
  7024. cardNo,
  7025. appNo,
  7026. (ShmChargerInfo->DeductResultReq[i].DeductAmount / 100),
  7027. (ShmChargerInfo->DeductResultReq[i].DeductAmount % 100),
  7028. str_ReaderStatus[ShmChargerInfo->DeductResultReq[i].DeductResult],
  7029. ShmChargerInfo->DeductResultReq[i].IsDonateInvoice ? "Yes" : "No");
  7030. if(ShmChargerInfo->DeductResultReq[i].DeductResult == _Deduct_PreAuth_Pass)
  7031. {
  7032. memcpy(&ShmChargerInfo->ReaderStatus[i], &ShmChargerInfo->DeductResultReq[i], sizeof(DeductResultInfoData));
  7033. ShmChargerInfo->ReaderStatus[i].ReaderStatusReq = YES;
  7034. }
  7035. }
  7036. }
  7037. }
  7038. else
  7039. {
  7040. if(!Is_Ocpp_ReportCreditDeductReq())
  7041. {
  7042. Clean_DeductInfo(i);
  7043. _WriteDeductBusy[i] = false;
  7044. }
  7045. }
  7046. }
  7047. else
  7048. {
  7049. _WriteDeductBusy[i] = false;
  7050. }
  7051. }
  7052. }
  7053. bool _WriteReDeductBusy = false;
  7054. void CheckReDeductRequest(void)
  7055. {
  7056. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7057. char cardNo[21];
  7058. char appNo[10];
  7059. if(ShmChargerInfo->ReDeductReq.DeductReq)
  7060. {
  7061. if(!_WriteReDeductBusy)
  7062. {
  7063. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7064. {
  7065. _WriteReDeductBusy = true;
  7066. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->ReDeductReq))
  7067. {
  7068. memset(cardNo, 0x00, sizeof(cardNo));
  7069. memcpy(cardNo, (char *)&ShmChargerInfo->ReDeductReq.creditNo[0], sizeof(ShmChargerInfo->ReDeductReq.creditNo));
  7070. memset(appNo, 0x00, sizeof(appNo));
  7071. memcpy(appNo, (char *)&ShmChargerInfo->ReDeductReq.ApprovalNumber[0], sizeof(ShmChargerInfo->ReDeductReq.ApprovalNumber));
  7072. Set_Ocpp_ReportCreditDeductReq();
  7073. LOG_INFO("ReDeductReport TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7074. ShmChargerInfo->ReDeductReq.TransactionId,
  7075. cardNo,
  7076. appNo,
  7077. (ShmChargerInfo->ReDeductReq.DeductAmount / 100),
  7078. (ShmChargerInfo->ReDeductReq.DeductAmount % 100),
  7079. str_ReaderStatus[ShmChargerInfo->ReDeductReq.DeductResult],
  7080. ShmChargerInfo->ReDeductReq.IsDonateInvoice ? "Yes" : "No");
  7081. if(ShmChargerInfo->ReDeductReq.DeductResult == _Deduct_PreAuth_Pass && ShmChargerInfo->ReDeductReq.ConnectorId != 0xFF)
  7082. {
  7083. int gunIndex = ShmChargerInfo->ReDeductReq.ConnectorId - 1;
  7084. memcpy(&ShmChargerInfo->ReaderStatus[gunIndex], &ShmChargerInfo->ReDeductReq, sizeof(DeductResultInfoData));
  7085. ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq = YES;
  7086. }
  7087. }
  7088. }
  7089. }
  7090. else
  7091. {
  7092. if(!Is_Ocpp_ReportCreditDeductReq())
  7093. {
  7094. memset(&ShmChargerInfo->ReDeductReq, 0x00, sizeof(DeductResultInfoData));
  7095. _WriteReDeductBusy = false;
  7096. }
  7097. }
  7098. }
  7099. else
  7100. {
  7101. _WriteReDeductBusy = false;
  7102. }
  7103. }
  7104. bool _WriteReaderStatusBusy[CONNECTOR_QUANTITY] = {0};
  7105. void CheckReaderStatusRequest(void)
  7106. {
  7107. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7108. char cardNo[21];
  7109. char appNo[10];
  7110. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7111. {
  7112. if(Is_ReaderStatusReq(i))
  7113. {
  7114. if(!_WriteReaderStatusBusy[i])
  7115. {
  7116. if(!Is_Ocpp_TcciReaderStatusReq(i) && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7117. {
  7118. _WriteReaderStatusBusy[i] = true;
  7119. if(Set_Ocpp_TcciReaderStatus(i, &ShmChargerInfo->ReaderStatus[i]))
  7120. {
  7121. memset(cardNo, 0x00, sizeof(cardNo));
  7122. memcpy(cardNo, (char *)&ShmChargerInfo->ReaderStatus[i].creditNo[0], sizeof(ShmChargerInfo->ReaderStatus[i].creditNo));
  7123. memset(appNo, 0x00, sizeof(appNo));
  7124. memcpy(appNo, (char *)&ShmChargerInfo->ReaderStatus[i].ApprovalNumber[0], sizeof(ShmChargerInfo->ReaderStatus[i].ApprovalNumber));
  7125. Set_Ocpp_TcciReaderStatusReq(i);
  7126. LOG_INFO("StatusReport Gun %d: [%s], CreditNo: [%s], ApprovalNo: [%s]",
  7127. i + 1, str_ReaderStatus[ShmChargerInfo->ReaderStatus[i].DeductResult], cardNo, appNo);
  7128. }
  7129. }
  7130. }
  7131. else
  7132. {
  7133. if(!Is_Ocpp_TcciReaderStatusReq(i))
  7134. {
  7135. Clean_ReaderStatusInfo(i);
  7136. _WriteReaderStatusBusy[i] = false;
  7137. }
  7138. }
  7139. }
  7140. else
  7141. {
  7142. _WriteReaderStatusBusy[i] = false;
  7143. }
  7144. }
  7145. }
  7146. void CheckOccupancyPrice(void)
  7147. {
  7148. float _price = 0;
  7149. if(Is_Ocpp_TcciSetOccupancyPriceReq())
  7150. {
  7151. _price = Get_Ocpp_TcciOccupancyPrice();
  7152. if(ShmChargerInfo->CabinetMiscValue.ParkingPrice != _price)
  7153. {
  7154. ShmChargerInfo->CabinetMiscValue.ParkingPrice = _price;
  7155. LOG_INFO("Set OccupancyPrice: [%6.2f]", ShmChargerInfo->CabinetMiscValue.ParkingPrice);
  7156. AnnounceOccupancyPrice();
  7157. }
  7158. Clean_Ocpp_TcciSetOccupancyPriceReq();
  7159. }
  7160. }
  7161. bool IsParkingButtonStatusChanged(int gunIndex)
  7162. {
  7163. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus ?
  7164. false : true;
  7165. }
  7166. void SyncParkingButtonStatus(int gunIndex)
  7167. {
  7168. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7169. }
  7170. int OccupancyButtonSync(int gunIndex)
  7171. {
  7172. int _pass = FAIL;
  7173. unsigned char _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7174. switch(_status)
  7175. {
  7176. case _BillStatus_Idle:
  7177. if(IsParkingButtonStatusChanged(gunIndex))
  7178. {
  7179. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7180. SyncParkingButtonStatus(gunIndex);
  7181. }
  7182. break;
  7183. case _BillStatus_MiscReq:
  7184. if(IsParkingButtonStatusChanged(gunIndex))
  7185. {
  7186. Occupancy_INFO("Gun %d OccupancyButton [MiscRequest]", gunIndex + 1);
  7187. SyncParkingButtonStatus(gunIndex);
  7188. }
  7189. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_WaitMiscClean;
  7190. Set_Connector_MiscCommand(gunIndex, MISC_CONN_PARKING_STATUS);
  7191. break;
  7192. case _BillStatus_WaitMiscClean:
  7193. if(IsParkingButtonStatusChanged(gunIndex))
  7194. {
  7195. SyncParkingButtonStatus(gunIndex);
  7196. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7197. }
  7198. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7199. {
  7200. Occupancy_INFO("Gun %d OccupancyButton [WaitMisc Timeout]", gunIndex + 1);
  7201. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7202. }
  7203. break;
  7204. case _BillStatus_MiscClean:
  7205. if(IsParkingButtonStatusChanged(gunIndex))
  7206. {
  7207. Occupancy_INFO("Gun %d OccupancyButton [MiscClean]", gunIndex + 1);
  7208. SyncParkingButtonStatus(gunIndex);
  7209. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7210. }
  7211. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7212. {
  7213. LOG_INFO("Gun %d OccupancyButton [Wait Timeout]", gunIndex + 1);
  7214. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7215. }
  7216. break;
  7217. case _BillStatus_UpdateDone:
  7218. if(IsParkingButtonStatusChanged(gunIndex))
  7219. {
  7220. Occupancy_INFO("Gun %d OccupancyButton [UpdateDone]", gunIndex + 1);
  7221. SyncParkingButtonStatus(gunIndex);
  7222. }
  7223. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_Idle;
  7224. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7225. _pass = PASS;
  7226. break;
  7227. default:
  7228. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7229. break;
  7230. }
  7231. return _pass;
  7232. }
  7233. int NotifyOccupancyFeeActRequest(int gunIndex, int action)
  7234. {
  7235. char *str_notify_occupancy_act[] = {STR_NOTIFY_NONE, STR_GET_OCCUPANCY_FEE, STR_CANCEL_OCCUPANCY_DEDUCT};
  7236. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_None)
  7237. {
  7238. if(!Is_Ocpp_TcciOccupancyFeeActReq(gunIndex))
  7239. {
  7240. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7241. Set_Ocpp_TcciOccupancyFeeAct(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill, action);
  7242. _notifyOccupancyFeeAct[gunIndex] = action;
  7243. LOG_INFO("Gun %d Set OccupancyFee Action [%s] Request", gunIndex + 1, str_notify_occupancy_act[action]);
  7244. }
  7245. }
  7246. else
  7247. {
  7248. if(!Is_Ocpp_TcciOccupancyFeeActReq(gunIndex))
  7249. {
  7250. if(Is_Ocpp_TcciOccupancyFeeActStatus(gunIndex, "Accepted"))
  7251. {
  7252. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_GetOccupancyFee)
  7253. {
  7254. Get_Ocpp_TcciGetOccupancyFeeResponse(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill);
  7255. LOG_INFO("Gun %d Get Occupancy Fee: [%.2f], Duration: [%d], StartTime: [%s]",
  7256. gunIndex + 1,
  7257. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Fee,
  7258. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Duration,
  7259. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.StartTime);
  7260. }
  7261. if(_notifyOccupancyFeeAct[gunIndex] == _Notify_CancelLocalDeduction)
  7262. {
  7263. LOG_INFO("Gun %d Occupancy Deduction Stopped", gunIndex + 1);
  7264. }
  7265. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7266. return PASS;
  7267. }
  7268. else
  7269. {
  7270. LOG_INFO("Gun %d Notify OccupancyFee Action Rejected", gunIndex + 1);
  7271. return FAIL;
  7272. }
  7273. }
  7274. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7275. {
  7276. // shall not reach here
  7277. Clean_Ocpp_TcciNotifyOccupancyFeeActReq(gunIndex);
  7278. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7279. LOG_INFO("Gun %d OccupancyFee Action [%s] Request Timeout", gunIndex + 1, str_notify_occupancy_act[action]);
  7280. return FAIL;
  7281. }
  7282. }
  7283. return WAIT;
  7284. }
  7285. bool IsParkingStatusChanged(int gunIndex)
  7286. {
  7287. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus ?
  7288. false : true;
  7289. }
  7290. void SyncParkingStatus(int gunIndex)
  7291. {
  7292. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7293. }
  7294. bool Is_Occupancy_Cancel(int gunIndex)
  7295. {
  7296. unsigned char dispenser = 0;
  7297. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParentDispensetIndex;
  7298. if((!ShmSysConfigAndInfo->SysInfo.OcppConnStatus && !ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq) ||
  7299. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_None ||
  7300. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_Timeout)
  7301. {
  7302. return true;
  7303. }
  7304. return false;
  7305. }
  7306. void OccupancyRequest(int gunIndex)
  7307. {
  7308. int _action = 0;
  7309. bool feeRequest = false;
  7310. bool keepCounting = false;
  7311. int res = FAIL;
  7312. char _sn[37];
  7313. unsigned char _status = 0;
  7314. 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};
  7315. _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7316. if(Is_Ocpp_TcciOccupancyDisplayReq(gunIndex))
  7317. {
  7318. memset(_sn, 0x00, sizeof(_sn));
  7319. _action = Get_Ocpp_TcciOccupancyAction(gunIndex, sizeof(_sn) - 1, _sn);
  7320. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ActionId = _action;
  7321. LOG_INFO("Gun %d OccupancyAction is [%s]", gunIndex + 1, str_action[_action]);
  7322. Clean_Ocpp_TcciOccupancyDisplayReq(gunIndex);
  7323. }
  7324. if(ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest)
  7325. {
  7326. feeRequest = true;
  7327. ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest =false;
  7328. }
  7329. if(Is_OccupancyKeepCountingReq(gunIndex))
  7330. {
  7331. if(_status == _Parking_WaitToPay)
  7332. {
  7333. keepCounting = true;
  7334. }
  7335. else
  7336. {
  7337. Clean_OccupancyKeepCountingReq(gunIndex);
  7338. }
  7339. }
  7340. if(_status != _Parking_NoOccupancy && chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7341. {
  7342. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7343. Occupancy_INFO("Gun %d OccupancyStatus [ForceClean]", gunIndex + 1);
  7344. return;
  7345. }
  7346. switch(_status)
  7347. {
  7348. case _Parking_NoOccupancy:
  7349. if(IsParkingStatusChanged(gunIndex))
  7350. {
  7351. Occupancy_INFO("Gun %d OccupancyStatus [Clean]", gunIndex + 1);
  7352. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex], 0x00, sizeof(OccupancyInfoData));
  7353. SyncParkingStatus(gunIndex);
  7354. }
  7355. if(_action == _ParkingAction_DisplayButton)
  7356. {
  7357. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7358. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_SyncOccupancy;
  7359. }
  7360. break;
  7361. case _Parking_SyncOccupancy:
  7362. if(IsParkingStatusChanged(gunIndex))
  7363. {
  7364. Occupancy_INFO("Gun %d OccupancyStatus [Sync]", gunIndex + 1);
  7365. SyncParkingStatus(gunIndex);
  7366. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7367. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7368. }
  7369. if(_action == _ParkingAction_DisplayButton)
  7370. {
  7371. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7372. }
  7373. else if(_action == _ParkingAction_CancelOccupancy ||
  7374. Is_Occupancy_Cancel(gunIndex))
  7375. {
  7376. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7377. }
  7378. else if(_action == _ParkingAction_PaidOnlineOK)
  7379. {
  7380. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7381. }
  7382. else
  7383. {
  7384. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  7385. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  7386. {
  7387. if(OccupancyButtonSync(gunIndex) == PASS)
  7388. {
  7389. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7390. }
  7391. }
  7392. }
  7393. break;
  7394. case _Parking_Occupied:
  7395. if(IsParkingStatusChanged(gunIndex))
  7396. {
  7397. Occupancy_INFO("Gun %d OccupancyStatus [Start]", gunIndex + 1);
  7398. SyncParkingStatus(gunIndex);
  7399. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7400. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7401. }
  7402. if(_action == _ParkingAction_DisplayButton)
  7403. {
  7404. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7405. }
  7406. else if(_action == _ParkingAction_CancelOccupancy ||
  7407. Is_Occupancy_Cancel(gunIndex))
  7408. {
  7409. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7410. }
  7411. else if(_action == _ParkingAction_PaidOnlineOK)
  7412. {
  7413. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7414. }
  7415. else if(_action == _ParkingAction_PaidOnlineFail)
  7416. {
  7417. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7418. }
  7419. else
  7420. {
  7421. if(feeRequest)
  7422. {
  7423. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_RequestFee;
  7424. }
  7425. }
  7426. break;
  7427. case _Parking_RequestFee:
  7428. if(IsParkingStatusChanged(gunIndex))
  7429. {
  7430. Occupancy_INFO("Gun %d OccupancyStatus [RequestFee]", gunIndex + 1);
  7431. SyncParkingStatus(gunIndex);
  7432. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7433. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7434. }
  7435. if(_action == _ParkingAction_CancelOccupancy ||
  7436. Is_Occupancy_Cancel(gunIndex))
  7437. {
  7438. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7439. }
  7440. else if(_action == _ParkingAction_PaidOnlineOK)
  7441. {
  7442. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7443. }
  7444. else if(_action == _ParkingAction_PaidOnlineFail)
  7445. {
  7446. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7447. }
  7448. else
  7449. {
  7450. if(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq)
  7451. {
  7452. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7453. }
  7454. else
  7455. {
  7456. res = NotifyOccupancyFeeActRequest(gunIndex, _Notify_GetOccupancyFee);
  7457. if(res == PASS)
  7458. {
  7459. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7460. }
  7461. else if(res == FAIL)
  7462. {
  7463. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7464. }
  7465. else
  7466. {
  7467. // wait response
  7468. }
  7469. }
  7470. }
  7471. break;
  7472. case _Parking_WaitToPay:
  7473. if(IsParkingStatusChanged(gunIndex))
  7474. {
  7475. Occupancy_INFO("Gun %d OccupancyStatus [WaitToPay]", gunIndex + 1);
  7476. SyncParkingStatus(gunIndex);
  7477. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].BillReqStatus, 0x00, sizeof(MiscRequestStatus));
  7478. _notifyOccupancyFeeAct[gunIndex] = _Notify_None;
  7479. }
  7480. if(_action == _ParkingAction_CancelOccupancy ||
  7481. Is_Occupancy_Cancel(gunIndex))
  7482. {
  7483. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7484. }
  7485. else if(_action == _ParkingAction_PaidOnlineOK)
  7486. {
  7487. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7488. }
  7489. else if(_action == _ParkingAction_PaidOnlineFail)
  7490. {
  7491. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7492. }
  7493. else if(keepCounting)
  7494. {
  7495. res = NotifyOccupancyFeeActRequest(gunIndex, _Notify_CancelLocalDeduction);
  7496. if(res == PASS)
  7497. {
  7498. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7499. Clean_OccupancyKeepCountingReq(gunIndex);
  7500. }
  7501. else if(res == FAIL)
  7502. {
  7503. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7504. Clean_OccupancyKeepCountingReq(gunIndex);
  7505. }
  7506. else
  7507. {
  7508. // wait response
  7509. }
  7510. }
  7511. break;
  7512. case _Parking_PaidOnlineOK:
  7513. if(IsParkingStatusChanged(gunIndex))
  7514. {
  7515. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineOK]", gunIndex + 1);
  7516. SyncParkingStatus(gunIndex);
  7517. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7518. }
  7519. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7520. {
  7521. LOG_INFO("Gun %d Reset OccupancyStatus via PaidOnlineOK", gunIndex + 1);
  7522. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7523. }
  7524. break;
  7525. case _Parking_PaidOnlineFail:
  7526. if(IsParkingStatusChanged(gunIndex))
  7527. {
  7528. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineFail]", gunIndex + 1);
  7529. SyncParkingStatus(gunIndex);
  7530. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7531. }
  7532. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7533. {
  7534. LOG_INFO("Gun %d Return to Occupied via PaidOnlineFail", gunIndex + 1);
  7535. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7536. }
  7537. break;
  7538. default:
  7539. DisableConnectorBill(gunIndex);
  7540. break;
  7541. }
  7542. }
  7543. void CheckOccupancyFee(void)
  7544. {
  7545. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7546. {
  7547. OccupancyRequest(i);
  7548. }
  7549. }
  7550. #define OCCUPANCY_DEDUCT_IDLE 0
  7551. #define OCCUPANCY_DEDUCT_REQUEST 1
  7552. unsigned char _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7553. int _OccupancyReportGunId = 0;
  7554. void CheckOccupancyDeduct(void)
  7555. {
  7556. char cardNo[21];
  7557. char appNo[10];
  7558. if(_OccupancyDeductStatus == OCCUPANCY_DEDUCT_IDLE)
  7559. {
  7560. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7561. {
  7562. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7563. {
  7564. if(Is_OccupancyDeductReq(i))
  7565. {
  7566. if(ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyReq.bits.SelfReq &&
  7567. ShmChargerInfo->AllBill.OccupancyInfo[i].Status == _Parking_WaitToPay)
  7568. {
  7569. ShmChargerInfo->AllBill.OccupancyInfo[i].Status = _Parking_NoOccupancy;
  7570. LOG_INFO("Gun %d Occupancy Concluded!", i + 1);
  7571. break;
  7572. }
  7573. Set_Ocpp_OccupancyDeductResult(&ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyDeduct, YES);
  7574. Set_Ocpp_TcciReportOccupancyDeductReq();
  7575. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_REQUEST;
  7576. _OccupancyReportGunId = i + 1;
  7577. Clean_OccupancyDeductReq(i);
  7578. break;
  7579. }
  7580. }
  7581. if(_OccupancyDeductStatus != OCCUPANCY_DEDUCT_IDLE)
  7582. {
  7583. memset(cardNo, 0x00, sizeof(cardNo));
  7584. memset(appNo, 0x00, sizeof(appNo));
  7585. memcpy(cardNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo));
  7586. memcpy(appNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber));
  7587. LOG_INFO("Gun %d Occupancy Deduct [Request] Sn: [%s], Fee: [%.2f], CreditNo: [%s], ApprovalNo: [%s]",
  7588. _OccupancyReportGunId,
  7589. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Sn,
  7590. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Fee,
  7591. cardNo,
  7592. appNo);
  7593. }
  7594. }
  7595. }
  7596. else
  7597. {
  7598. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7599. {
  7600. if(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status == _Parking_WaitToPay)
  7601. {
  7602. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status = _Parking_NoOccupancy;
  7603. LOG_INFO("Gun %d OccupancyDeduct Request Completed!", _OccupancyReportGunId);
  7604. }
  7605. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7606. }
  7607. }
  7608. }
  7609. //===============================================
  7610. // OCPP
  7611. //===============================================
  7612. void CheckOcppStatus()
  7613. {
  7614. if (Is_Ocpp_BootNotification())
  7615. {
  7616. Clean_Ocpp_BootNotification();
  7617. LOG_INFO("*********************OCPP Boot Notification****************************");
  7618. }
  7619. if (Is_Ocpp_ResetReq())
  7620. {
  7621. bool canReset = true;
  7622. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  7623. {
  7624. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7625. {
  7626. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  7627. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  7628. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  7629. {
  7630. canReset = false;
  7631. //if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  7632. //{
  7633. // ChargingTerminalProcess(_index);
  7634. //}
  7635. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7636. {
  7637. chargingInfo[_index]->ChargingStopFlag.bits.HardResetStop = true;
  7638. }
  7639. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7640. {
  7641. chargingInfo[_index]->ChargingStopFlag.bits.SoftResetStop = true;
  7642. }
  7643. }
  7644. }
  7645. }
  7646. if(canReset)
  7647. {
  7648. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7649. {
  7650. //trigger dispenser to hardware reboot
  7651. TriggerDispenserHardwareReboot();
  7652. if(IsDispenserHardwareRebootResponsed())
  7653. {
  7654. Clean_Ocpp_ResetReq();
  7655. LOG_INFO("****** Hard Reboot ******");
  7656. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7657. {
  7658. setChargerMode(i, MODE_MAINTAIN);
  7659. }
  7660. sleep(3);
  7661. system("reboot -f");
  7662. }
  7663. }
  7664. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7665. {
  7666. //trigger dispenser to software reset
  7667. TriggerDispenserSoftwareReset();
  7668. if(IsDispenserSoftwareResetResponsed())
  7669. {
  7670. Clean_Ocpp_ResetReq();
  7671. LOG_INFO("****** Soft Reboot ******");
  7672. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7673. {
  7674. setChargerMode(i, MODE_MAINTAIN);
  7675. }
  7676. sleep(3);
  7677. Kill_Ocpp_Service();
  7678. KillAllTask();
  7679. TryCloseWatchdog();
  7680. system("/usr/bin/run_evse_restart.sh");
  7681. }
  7682. }
  7683. }
  7684. }
  7685. CheckOcppCostAndPrice();
  7686. CheckLcmPageChangeRequest();
  7687. CheckStationInfo();
  7688. CheckWeatherInfo();
  7689. CheckDeductResultRequest();
  7690. CheckReDeductRequest();
  7691. CheckReaderStatusRequest();
  7692. CheckOccupancyPrice();
  7693. CheckOccupancyFee();
  7694. CheckOccupancyDeduct();
  7695. }
  7696. void CheckoutStartChargingSoc(byte gunIndex, bool force)
  7697. {
  7698. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc)
  7699. {
  7700. if(chargingInfo[gunIndex]->EvBatterySoc > 0 || force)
  7701. {
  7702. chargingInfo[gunIndex]->EvBatteryStartSoc = chargingInfo[gunIndex]->EvBatterySoc;
  7703. LOG_INFO("Gun %d StartSoc = %d", chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->EvBatteryStartSoc);
  7704. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc = true;
  7705. }
  7706. }
  7707. }
  7708. void UpdateChargingTimestamp(char *timestamp)
  7709. {
  7710. struct timeb SeqEndTime;
  7711. struct tm *tm;
  7712. ftime(&SeqEndTime);
  7713. SeqEndTime.time = time(NULL);
  7714. tm = localtime(&SeqEndTime.time);
  7715. 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);
  7716. }
  7717. void OcppStartTransaction(byte gunIndex)
  7718. {
  7719. if(_triggerTransaction[gunIndex])
  7720. {
  7721. return;
  7722. }
  7723. // clean deduct info
  7724. Clean_DeductInfo(gunIndex);
  7725. // set deduct user id
  7726. //Set_Deduct_UserId(gunIndex, (char *)&chargingInfo[gunIndex]->StartUserId);
  7727. // initial start & stop datetime
  7728. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00");
  7729. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, "00:00:00");
  7730. Update_PowerConsumption(gunIndex);
  7731. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  7732. Set_Ocpp_StartTransactionIdTag(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  7733. LOG_INFO("Gun %d Start Transaction IdTag = %s, MeterValue: %.4f",
  7734. chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->StartUserId, chargingInfo[gunIndex]->PowerConsumption);
  7735. Clean_Ocpp_StartTransactionConf(gunIndex);
  7736. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StartCharging);
  7737. Set_Ocpp_StartTransactionReq(gunIndex);
  7738. // set _triggerTransaction
  7739. _triggerTransaction[gunIndex] = true;
  7740. }
  7741. void OcppStopTransaction(byte gunIndex)
  7742. {
  7743. if(!_triggerTransaction[gunIndex])
  7744. {
  7745. return;
  7746. }
  7747. Update_PowerConsumption(gunIndex);
  7748. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  7749. Set_Ocpp_StopTransactionId(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  7750. LOG_INFO("Gun %d Stop Transaction IdTag = %s, StopSoc = %d, MeterValue: %.4f",
  7751. chargingInfo[gunIndex]->Index + 1,
  7752. chargingInfo[gunIndex]->StartUserId,
  7753. chargingInfo[gunIndex]->EvBatterySoc,
  7754. chargingInfo[gunIndex]->PowerConsumption);
  7755. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StopCharging);
  7756. Set_Ocpp_StopTransactionReq(gunIndex);
  7757. // clean _triggerTransaction
  7758. _triggerTransaction[gunIndex] = false;
  7759. }
  7760. bool Is_DispenserStop(byte gunIndex)
  7761. {
  7762. bool _isStop = false;
  7763. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  7764. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  7765. {
  7766. _isStop = true;
  7767. }
  7768. return _isStop;
  7769. }
  7770. void ChkOcppStatus(byte gunIndex)
  7771. {
  7772. Chk_Ocpp_Backup_ErrorCode(gunIndex);
  7773. if(Is_Ocpp_ReserveNowReq(gunIndex))
  7774. {
  7775. Clean_Ocpp_ReserveNowReq(gunIndex);
  7776. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE && !Is_Ocpp_ReserveNowExpired(gunIndex))
  7777. {
  7778. LOG_INFO("***************ChkOcppStatus: Gun %d OcppReservedStatus********************", gunIndex + 1);
  7779. chargingInfo[gunIndex]->ReservationId = Get_Ocpp_ReserveNowId(gunIndex);
  7780. setChargerMode(gunIndex, MODE_RESERVATION);
  7781. }
  7782. Set_Ocpp_ReserveNowConf(gunIndex);
  7783. }
  7784. if(Is_Ocpp_CancelReservationReq(gunIndex))
  7785. {
  7786. Clean_Ocpp_CancelReservationReq(gunIndex);
  7787. if(chargingInfo[gunIndex]->SystemStatus == S_RESERVATION)
  7788. {
  7789. LOG_INFO("***************ChkOcppStatus: Gun %d Cancel OcppReservedStatus********************", gunIndex + 1);
  7790. chargingInfo[gunIndex]->ReservationId = -1;
  7791. setChargerMode(gunIndex, MODE_IDLE);
  7792. }
  7793. Set_Ocpp_CancelReservationConf(gunIndex);
  7794. }
  7795. if (Is_Ocpp_ChangeAvailabilityReq(gunIndex) == YES)
  7796. {
  7797. LOG_INFO("***************ChkOcppStatus: Gun %d OcppChangeAvailability********************", gunIndex + 1);
  7798. Clean_Ocpp_ChangeAvailabilityReq(gunIndex);
  7799. if(Chk_Ocpp_Availability_Type(gunIndex, "Operative"))
  7800. {
  7801. if (isDb_ready)
  7802. DB_Update_Operactive(localDb, gunIndex, true);
  7803. chargingInfo[gunIndex]->IsAvailable = YES;
  7804. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  7805. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  7806. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  7807. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  7808. {
  7809. setChargerMode(gunIndex, MODE_IDLE);
  7810. }
  7811. }
  7812. else if(Chk_Ocpp_Availability_Type(gunIndex, "Inoperative"))
  7813. {
  7814. if (isDb_ready)
  7815. DB_Update_Operactive(localDb, gunIndex, false);
  7816. chargingInfo[gunIndex]->IsAvailable = NO;
  7817. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  7818. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  7819. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  7820. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  7821. {
  7822. setChargerMode(gunIndex, MODE_MAINTAIN);
  7823. }
  7824. }
  7825. }
  7826. if (Is_Ocpp_UnlockConnectorReq(gunIndex))
  7827. {
  7828. Clean_Ocpp_UnlockConnectorReq(gunIndex);
  7829. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  7830. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  7831. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  7832. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7833. {
  7834. // 充電中,需停止充電
  7835. Set_Ocpp_StopReason(gunIndex, "UnlockCommand");
  7836. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_UNLOCK);
  7837. chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop = true;
  7838. LOG_INFO("********** OcppUnlockStop Gun %d **********", gunIndex + 1);
  7839. }
  7840. Set_Ocpp_UnlockConnectorConf(gunIndex);
  7841. }
  7842. if (Is_Ocpp_RemoteStopReq(gunIndex))
  7843. {
  7844. Clean_Ocpp_RemoteStopReq(gunIndex);
  7845. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  7846. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  7847. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  7848. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7849. {
  7850. Set_Ocpp_StopReason(gunIndex, "Remote");
  7851. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_REMOTE_STOP);
  7852. chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop = true;
  7853. LOG_INFO("********** OcppRemoteStop Gun %d **********", gunIndex + 1);
  7854. }
  7855. }
  7856. if(Is_Ocpp_StartTransactionConf(gunIndex))
  7857. {
  7858. char acceptId[128] = {0};
  7859. char status[32] = {0};
  7860. Get_Ocpp_StartTransaction_Status(gunIndex, status);
  7861. Get_Ocpp_TransactionId(gunIndex, acceptId);
  7862. Set_Deduct_TransactionId(gunIndex, atoi(acceptId));
  7863. Set_UserTransactionId(gunIndex, atoi(acceptId));
  7864. LOG_INFO("Gun %d OCPP Start Transaction %s, TransactionId [%s]", gunIndex + 1, status, acceptId);
  7865. if(!Is_Ocpp_StartTransaction_Accepted(gunIndex))
  7866. {
  7867. if(Is_Ocpp_StopOnInvalidId_Enable())
  7868. {
  7869. if(Is_Invalid_StartTransactionId(gunIndex))
  7870. {
  7871. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop = true;
  7872. LOG_INFO("Gun %d OCPP StopOnInvalidId", gunIndex + 1);
  7873. }
  7874. }
  7875. LOG_INFO("Gun %d Transaction [%s] Not Accepted, %s",
  7876. gunIndex + 1, acceptId,
  7877. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop > 0 ? "And Stop Charging" : "But StopOnInvalidId is Disable");
  7878. }
  7879. Clean_Ocpp_StartTransactionConf(gunIndex);
  7880. }
  7881. }
  7882. bool CheckBackendChargingTimeout(byte gunIndex)
  7883. {
  7884. bool result = false;
  7885. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  7886. {
  7887. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  7888. {
  7889. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  7890. result = true;
  7891. }
  7892. }
  7893. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  7894. {
  7895. // 隨插即充電的要看 offline
  7896. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  7897. {
  7898. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  7899. result = true;
  7900. }
  7901. }
  7902. return result;
  7903. }
  7904. bool CheckBackendChargingEnergy(byte gunIndex)
  7905. {
  7906. bool result = false;
  7907. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  7908. {
  7909. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  7910. {
  7911. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  7912. result = true;
  7913. }
  7914. }
  7915. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  7916. {
  7917. // 隨插即充電的要看 offline
  7918. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  7919. {
  7920. if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  7921. result = true;
  7922. }
  7923. }
  7924. return result;
  7925. }
  7926. bool IsReachConfigMaxChargingSoc(byte gunIndex)
  7927. {
  7928. bool result = false;
  7929. if(ShmSysConfigAndInfo->SysConfig.MaxChargingSoc > 0)
  7930. {
  7931. if(chargingInfo[gunIndex]->EvBatterySoc >= ShmSysConfigAndInfo->SysConfig.MaxChargingSoc)
  7932. {
  7933. LOG_INFO("Gun %d MaxChargingSoc: [%d%%] Stop", gunIndex + 1, ShmSysConfigAndInfo->SysConfig.MaxChargingSoc);
  7934. result = true;
  7935. }
  7936. }
  7937. return result;
  7938. }
  7939. bool CheckSessionTargetStop(byte gunIndex)
  7940. {
  7941. bool stop = false;
  7942. struct StructSessionTarget target;
  7943. Get_Ocpp_SessionTarget(gunIndex, &target);
  7944. if(target.targetDuration > 0)
  7945. {
  7946. if(chargingInfo[gunIndex]->PresentChargedDuration >= (target.targetDuration * 60))
  7947. {
  7948. LOG_INFO("Gun %d SessionTarget(Duration) Stop, Present: %d (sec), Target: %d (min)",
  7949. gunIndex + 1,
  7950. chargingInfo[gunIndex]->PresentChargedDuration,
  7951. target.targetDuration);
  7952. stop = true;
  7953. }
  7954. }
  7955. if(target.targetSoc > 0)
  7956. {
  7957. if(chargingInfo[gunIndex]->EvBatterySoc >= target.targetSoc)
  7958. {
  7959. LOG_INFO("Gun %d SessionTarget(SOC) Stop, Present: %d (%%), Target: %d (%%)",
  7960. gunIndex + 1,
  7961. chargingInfo[gunIndex]->EvBatterySoc,
  7962. target.targetSoc);
  7963. stop = true;
  7964. }
  7965. }
  7966. if(target.targetEnergy > 0)
  7967. {
  7968. if(chargingInfo[gunIndex]->PresentChargedEnergy >= target.targetEnergy)
  7969. {
  7970. LOG_INFO("Gun %d SessionTarget(Energy) Stop, Present: %8.3f (kWh), Target: %4d (kWh)",
  7971. gunIndex + 1,
  7972. chargingInfo[gunIndex]->PresentChargedEnergy,
  7973. target.targetEnergy);
  7974. stop = true;
  7975. }
  7976. }
  7977. if(stop)
  7978. {
  7979. chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop = true;
  7980. }
  7981. return stop;
  7982. }
  7983. void InformOcppErrOccur(byte codeType)
  7984. {
  7985. char _error[25];
  7986. switch(codeType)
  7987. {
  7988. case 4: strcpy(_error, "InternalError"); break;
  7989. case 6: strcpy(_error, "NoError"); break;
  7990. case 7: strcpy(_error, "OtherError"); break;
  7991. case 13: strcpy(_error, "UnderVoltage"); break;
  7992. case 14: strcpy(_error, "OverVoltage"); break;
  7993. }
  7994. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  7995. {
  7996. SetOcppErrorCode(gun_index, _error);
  7997. }
  7998. }
  7999. void SetOcppErrorCode(unsigned char _index, char *errCode)
  8000. {
  8001. Set_Ocpp_StatusNotificationErrorCode(_index, errCode);
  8002. strcpy((char *)_BackupStatusNotification[_index].ErrorCode, errCode);
  8003. }
  8004. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode)
  8005. {
  8006. Set_Ocpp_StatusNotificationVendorCode(_index, vendorCode);
  8007. strcpy((char *)_BackupStatusNotification[_index].VendorErrorCode, vendorCode);
  8008. }
  8009. //===============================================
  8010. // SQLite3 related routine
  8011. //===============================================
  8012. int NetworkDB_Open(sqlite3 *db)
  8013. {
  8014. int result = PASS;
  8015. char* errMsg = NULL;
  8016. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  8017. "idx integer primary key AUTOINCREMENT, "
  8018. "occurDatetime text NOT NULL, "
  8019. "isInternet text NOT NULL, "
  8020. "isOcppConnected text NOT NULL, "
  8021. "isEth0Internet text NOT NULL, "
  8022. "isMlan0Internet text NOT NULL, "
  8023. "isPpp0Internet text NOT NULL, "
  8024. "rssiMlan0 text NOT NULL, "
  8025. "rssiPpp0 text NOT NULL"
  8026. ");";
  8027. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  8028. if(sqlite3_open(NETWORK_DB_FILE, &db))
  8029. {
  8030. result = FAIL;
  8031. LOG_ERROR("Can't open database (Net) : %s", sqlite3_errmsg(db));
  8032. sqlite3_close(db);
  8033. }
  8034. else
  8035. {
  8036. LOG_INFO("Local network status record database open successfully.");
  8037. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8038. {
  8039. result = FAIL;
  8040. LOG_ERROR("Create local network status record table error message: %s", errMsg);
  8041. }
  8042. else
  8043. {
  8044. LOG_INFO("Opened local network status record table successfully");
  8045. }
  8046. sqlite3_close(db);
  8047. }
  8048. return result;
  8049. }
  8050. int DB_Open(sqlite3 *db)
  8051. {
  8052. int result = PASS;
  8053. char* errMsg = NULL;
  8054. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  8055. "idx integer primary key AUTOINCREMENT, "
  8056. "reservationId text, "
  8057. "transactionId text, "
  8058. "startMethod text, "
  8059. "userId text, "
  8060. "dateTimeStart text, "
  8061. "dateTimeStop text,"
  8062. "socStart text, "
  8063. "socStop text, "
  8064. "chargeEnergy text, "
  8065. "stopReason text"
  8066. ");";
  8067. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  8068. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8069. "`IsAvailable` TEXT NOT NULL, "
  8070. "`connector` INTEGER NOT NULL, "
  8071. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  8072. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  8073. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8074. "`occurDatetime` TEXT NOT NULL, "
  8075. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  8076. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  8077. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8078. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  8079. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  8080. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8081. "`connector` INTEGER NOT NULL, "
  8082. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8083. char *createGunConsumptionSql = "CREATE TABLE IF NOT EXISTS `gun_consumption` ( "
  8084. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8085. "`connector` INTEGER NOT NULL, "
  8086. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8087. char *createFinalCostSql = "CREATE TABLE IF NOT EXISTS `final_cost` ( "
  8088. "idx integer primary key AUTOINCREMENT, "
  8089. "gun text, "
  8090. "transactionId text, "
  8091. "occurDatetime text, "
  8092. "updateDatetime text, "
  8093. "description text, "
  8094. "updateStatus integer"
  8095. ");";
  8096. if(sqlite3_open(DB_FILE, &db))
  8097. {
  8098. result = FAIL;
  8099. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8100. sqlite3_close(db);
  8101. }
  8102. else
  8103. {
  8104. LOG_INFO( "Local charging record database open successfully.");
  8105. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8106. {
  8107. result = FAIL;
  8108. LOG_ERROR( "Create local charging record table error message: %s", errMsg);
  8109. }
  8110. else
  8111. {
  8112. LOG_INFO( "Opened local charging record table successfully");
  8113. }
  8114. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  8115. {
  8116. result = FAIL;
  8117. LOG_ERROR( "Create local config table error message: %s", errMsg);
  8118. }
  8119. else
  8120. {
  8121. LOG_INFO( "Opened local config table successfully");
  8122. }
  8123. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8124. {
  8125. result = FAIL;
  8126. LOG_ERROR( "Create local record table error message: %s", errMsg);
  8127. }
  8128. else
  8129. {
  8130. LOG_INFO( "Opened local record table successfully");
  8131. }
  8132. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  8133. {
  8134. result = FAIL;
  8135. LOG_ERROR( "Create reboot record table error message: %s", errMsg);
  8136. }
  8137. else
  8138. {
  8139. LOG_INFO( "Opened reboot record table successfully");
  8140. }
  8141. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8142. {
  8143. result = FAIL;
  8144. LOG_ERROR( "Create power consumption record table error message: %s", errMsg);
  8145. }
  8146. else
  8147. {
  8148. LOG_INFO( "Opened power consumption record table successfully");
  8149. }
  8150. if (sqlite3_exec(db, createGunConsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8151. {
  8152. result = FAIL;
  8153. LOG_ERROR( "Create gun consumption record table error message: %s", errMsg);
  8154. }
  8155. else
  8156. {
  8157. LOG_INFO( "Opened gun consumption record table successfully");
  8158. }
  8159. if (sqlite3_exec(db, createFinalCostSql, 0, 0, &errMsg) != SQLITE_OK)
  8160. {
  8161. result = FAIL;
  8162. LOG_ERROR( "Create final cost table error message: %s", errMsg);
  8163. }
  8164. else
  8165. {
  8166. LOG_INFO( "Opened final cost table successfully");
  8167. }
  8168. sqlite3_close(db);
  8169. }
  8170. return result;
  8171. }
  8172. int DB_Insert_Record(sqlite3 *db, int gun_index)
  8173. {
  8174. int result = PASS;
  8175. char* errMsg = NULL;
  8176. char insertSql[1024];
  8177. char transactionId[128];
  8178. char reason[32];
  8179. Get_Ocpp_TransactionId(gun_index, transactionId);
  8180. Get_Ocpp_StopReason(gun_index, reason);
  8181. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  8182. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  8183. chargingInfo[gun_index]->ReservationId,
  8184. transactionId,
  8185. chargingInfo[gun_index]->StartMethod,
  8186. chargingInfo[gun_index]->StartUserId,
  8187. chargingInfo[gun_index]->StartDateTime,
  8188. chargingInfo[gun_index]->StopDateTime,
  8189. chargingInfo[gun_index]->EvBatteryStartSoc,
  8190. chargingInfo[gun_index]->EvBatterySoc,
  8191. chargingInfo[gun_index]->PresentChargedEnergy,
  8192. reason);
  8193. if(sqlite3_open(DB_FILE, &db))
  8194. {
  8195. result = FAIL;
  8196. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8197. sqlite3_close(db);
  8198. }
  8199. else
  8200. {
  8201. //LOG_INFO( "Local charging record database open successfully.");
  8202. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8203. {
  8204. result = FAIL;
  8205. LOG_ERROR("Insert local charging record error message: %s", errMsg);
  8206. }
  8207. else
  8208. {
  8209. //LOG_INFO("Insert local charging record successfully");
  8210. }
  8211. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  8212. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8213. {
  8214. result = FAIL;
  8215. LOG_ERROR("delete local charging error message: %s", errMsg);
  8216. }
  8217. else
  8218. {
  8219. //LOG_INFO("delete local charging record successfully");
  8220. }
  8221. sqlite3_close(db);
  8222. }
  8223. return result;
  8224. }
  8225. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  8226. {
  8227. uint8_t result = false;
  8228. char* errMsg = NULL;
  8229. char sqlStr[1024];
  8230. srand(time(NULL));
  8231. if(sqlite3_open(DB_FILE, &db))
  8232. {
  8233. result = FAIL;
  8234. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8235. sqlite3_close(db);
  8236. }
  8237. else
  8238. {
  8239. LOG_INFO( "Local charging record database open successfully (%d).", IsAvailable);
  8240. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  8241. LOG_INFO("sqlStr= %s", sqlStr);
  8242. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8243. {
  8244. result = FAIL;
  8245. LOG_INFO( "update config error message: %s", errMsg);
  8246. }
  8247. else
  8248. {
  8249. LOG_INFO("update connector-%d config item isOperactive to %d", gun_index, IsAvailable);
  8250. }
  8251. sqlite3_close(db);
  8252. }
  8253. return result;
  8254. }
  8255. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  8256. {
  8257. uint8_t result = true;
  8258. char* errMsg = NULL;
  8259. char sqlStr[1024];
  8260. char **rs;
  8261. int rows, cols;
  8262. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  8263. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  8264. if(sqlite3_open(DB_FILE, &db))
  8265. {
  8266. result = FAIL;
  8267. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8268. sqlite3_close(db);
  8269. }
  8270. else
  8271. {
  8272. LOG_INFO( "Local config query database open successfully.");
  8273. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8274. if(rows>0)
  8275. {
  8276. for(int idxRow=1;idxRow<=rows;idxRow++)
  8277. {
  8278. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  8279. {
  8280. result = false;
  8281. }
  8282. LOG_INFO("Query connector-%d isOperactive: %s", gun_index, rs[(idxRow*cols)+3]);
  8283. }
  8284. }
  8285. else
  8286. {
  8287. LOG_INFO("Query connector-%d fail, set default value to operactive.", gun_index);
  8288. }
  8289. sqlite3_free_table(rs);
  8290. sqlite3_close(db);
  8291. }
  8292. return result;
  8293. }
  8294. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8295. {
  8296. char* errMsg = NULL;
  8297. char sqlStr[1024];
  8298. srand(time(NULL));
  8299. if(sqlite3_open(DB_FILE, &db))
  8300. {
  8301. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8302. sqlite3_close(db);
  8303. }
  8304. else
  8305. {
  8306. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  8307. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8308. {
  8309. LOG_ERROR("update power_consumption error message: %s", errMsg);
  8310. }
  8311. else
  8312. {
  8313. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8314. }
  8315. sqlite3_close(db);
  8316. }
  8317. }
  8318. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8319. {
  8320. char* errMsg = NULL;
  8321. char sqlStr[1024];
  8322. srand(time(NULL));
  8323. if(sqlite3_open(DB_FILE, &db))
  8324. {
  8325. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8326. sqlite3_close(db);
  8327. }
  8328. else
  8329. {
  8330. sprintf(sqlStr, "insert or replace into gun_consumption (connector, val) values(%d, %f);", gun_index, value);
  8331. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8332. {
  8333. LOG_ERROR("update gun_consumption error message: %s", errMsg);
  8334. }
  8335. else
  8336. {
  8337. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8338. }
  8339. sqlite3_close(db);
  8340. }
  8341. }
  8342. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index)
  8343. {
  8344. float result = 0;
  8345. char* errMsg = NULL;
  8346. char sqlStr[1024];
  8347. char **rs;
  8348. int rows, cols;
  8349. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  8350. if (sqlite3_open(DB_FILE, &db))
  8351. {
  8352. result = FAIL;
  8353. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8354. sqlite3_close(db);
  8355. }
  8356. else
  8357. {
  8358. //LOG_INFO("Local config query database open successfully.");
  8359. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8360. if (rows > 0)
  8361. {
  8362. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8363. {
  8364. result = atof(rs[(idxRow * cols) + 2]);
  8365. }
  8366. }
  8367. else
  8368. {
  8369. LOG_INFO("Query connector-%d fail, set default value to consumption.", gun_index);
  8370. }
  8371. sqlite3_free_table(rs);
  8372. sqlite3_close(db);
  8373. }
  8374. return result;
  8375. }
  8376. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index)
  8377. {
  8378. float result = 0;
  8379. char* errMsg = NULL;
  8380. char sqlStr[1024];
  8381. char **rs;
  8382. int rows, cols;
  8383. sprintf(sqlStr, "select * from gun_consumption where connector=%d;", gun_index);
  8384. if (sqlite3_open(DB_FILE, &db))
  8385. {
  8386. result = FAIL;
  8387. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8388. sqlite3_close(db);
  8389. }
  8390. else
  8391. {
  8392. //LOG_INFO("Local config query database open successfully.");
  8393. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8394. if (rows > 0)
  8395. {
  8396. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8397. {
  8398. result = atof(rs[(idxRow * cols) + 2]);
  8399. }
  8400. }
  8401. else
  8402. {
  8403. LOG_INFO("Query gun %d fail, set -1 to gun consumption.", gun_index + 1);
  8404. result = -1;
  8405. }
  8406. sqlite3_free_table(rs);
  8407. sqlite3_close(db);
  8408. }
  8409. return result;
  8410. }
  8411. float Get_PowerConsumption(uint8_t gun_index)
  8412. {
  8413. float value = 0;
  8414. //value = ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0 ?
  8415. // ShmChargerInfo->MeterValue[gun_index].GunConsumption : ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8416. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  8417. {
  8418. if(!_useGunMeterValue[gun_index])
  8419. {
  8420. LOG_INFO("***** Gun %d Use GunMeterValue *****", gun_index + 1);
  8421. }
  8422. _useGunMeterValue[gun_index] = true;
  8423. value = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  8424. }
  8425. else
  8426. {
  8427. value = ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8428. }
  8429. return value;
  8430. }
  8431. void Update_PowerConsumption(uint8_t gun_index)
  8432. {
  8433. chargingInfo[gun_index]->PowerConsumption = Get_PowerConsumption(gun_index);
  8434. }
  8435. int DB_Insert_FinalCost(sqlite3 *db, uint8_t gun_index, int txId, char *description)
  8436. {
  8437. char timeBuf[32];
  8438. int result = PASS;
  8439. char* errMsg = NULL;
  8440. char insertSql[1024];
  8441. getNowDatetime((unsigned char *)timeBuf);
  8442. sprintf(insertSql, "insert into final_cost(gun, transactionId, occurDatetime, updateDatetime, description, updateStatus) "
  8443. "values('%d', '%d', '%s', '%s', '%s', '%d');",
  8444. gun_index + 1,
  8445. txId,
  8446. timeBuf,
  8447. "",
  8448. description,
  8449. BILL_SATAUS_UNDISPOSED);
  8450. if(sqlite3_open(DB_FILE, &db))
  8451. {
  8452. result = FAIL;
  8453. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8454. sqlite3_close(db);
  8455. }
  8456. else
  8457. {
  8458. //LOG_INFO("FinalCost database open successfully.");
  8459. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8460. {
  8461. result = FAIL;
  8462. LOG_ERROR("Insert final_cost error message: %s", errMsg);
  8463. }
  8464. else
  8465. {
  8466. //LOG_INFO("Insert final_cost successfully");
  8467. }
  8468. sprintf(insertSql, "delete from final_cost where idx < (select idx from final_cost order by idx desc limit 1)-2000;");
  8469. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8470. {
  8471. result = FAIL;
  8472. LOG_ERROR("delete final_cost error message: %s", errMsg);
  8473. }
  8474. else
  8475. {
  8476. //LOG_INFO("delete final_cost successfully");
  8477. }
  8478. sqlite3_close(db);
  8479. }
  8480. return result;
  8481. }
  8482. int DB_Update_FinalCost_Status(sqlite3 *db, int txId, int status)
  8483. {
  8484. char timeBuf[32];
  8485. int result = PASS;
  8486. char* errMsg = NULL;
  8487. char updateSql[1024];
  8488. getNowDatetime((unsigned char *)timeBuf);
  8489. sprintf(updateSql, "update final_cost set updateStatus = %d, updateDatetime = '%s' where transactionId = '%d';",
  8490. status,
  8491. timeBuf,
  8492. txId);
  8493. if(sqlite3_open(DB_FILE, &db))
  8494. {
  8495. result = FAIL;
  8496. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8497. sqlite3_close(db);
  8498. }
  8499. else
  8500. {
  8501. if(sqlite3_exec(db, updateSql, 0, 0, &errMsg) != SQLITE_OK)
  8502. {
  8503. LOG_ERROR("update final_cost status error message: %s", errMsg);
  8504. result = FAIL;
  8505. }
  8506. else
  8507. {
  8508. //LOG_INFO("update final_cost txId[%d] status %d ok", txId, status);
  8509. }
  8510. sqlite3_close(db);
  8511. }
  8512. return result;
  8513. }
  8514. int DB_Get_Undisposed_FinalCost(sqlite3 *db, int gunIndex, char *costString)
  8515. {
  8516. int txId = -1;
  8517. char **rs;
  8518. int rows, cols;
  8519. char* errMsg = NULL;
  8520. char sqlString[1024];
  8521. sprintf(sqlString, "select * from final_cost where updateStatus = -1 and gun = '%d' order by idx asc limit 1;",
  8522. gunIndex + 1);
  8523. if(sqlite3_open(DB_FILE, &db))
  8524. {
  8525. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8526. sqlite3_close(db);
  8527. }
  8528. else
  8529. {
  8530. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  8531. if(rows > 0)
  8532. {
  8533. for(int idxRow = 1; idxRow <= rows; idxRow++)
  8534. {
  8535. txId = atoi(rs[(idxRow * cols) + 2]);
  8536. strcpy(costString, rs[(idxRow * cols) + 5]);
  8537. }
  8538. }
  8539. sqlite3_free_table(rs);
  8540. sqlite3_close(db);
  8541. }
  8542. return txId;
  8543. }
  8544. void CheckInProgress(uint8_t gun_index)
  8545. {
  8546. if((ShmChargerInfo->Connector[gun_index].Status > S_IDLE && ShmChargerInfo->Connector[gun_index].Status <= S_FAULT) ||
  8547. (ShmChargerInfo->Connector[gun_index].Status >= S_CCS_PRECHARGE_ST0 && ShmChargerInfo->Connector[gun_index].Status <= S_CCS_PRECHARGE_ST1))
  8548. {
  8549. if(chargingInfo[gun_index]->InProgress == NO)
  8550. {
  8551. LOG_INFO("Gun %d In Progress", gun_index + 1);
  8552. }
  8553. chargingInfo[gun_index]->InProgress = YES;
  8554. }
  8555. else
  8556. {
  8557. if(chargingInfo[gun_index]->InProgress == YES)
  8558. {
  8559. LOG_INFO("Gun %d Real Idle", gun_index + 1);
  8560. }
  8561. chargingInfo[gun_index]->InProgress = NO;
  8562. }
  8563. }
  8564. void CheckDebugMode(uint8_t gun_index)
  8565. {
  8566. if(ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay)
  8567. {
  8568. if(chargingInfo[gun_index]->SystemStatus != S_DEBUG)
  8569. {
  8570. setChargerMode(gun_index, MODE_DEBUG);
  8571. }
  8572. }
  8573. }
  8574. int DB_Reboot_Record(sqlite3 *db)
  8575. {
  8576. int result = PASS;
  8577. char* errMsg = NULL;
  8578. char insertSql[256];
  8579. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  8580. if(sqlite3_open(DB_FILE, &db))
  8581. {
  8582. result = FAIL;
  8583. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8584. sqlite3_close(db);
  8585. }
  8586. else
  8587. {
  8588. LOG_INFO("Local charging record database open successfully.");
  8589. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8590. {
  8591. result = FAIL;
  8592. LOG_ERROR("Insert reboot record error message: %s", errMsg);
  8593. }
  8594. else
  8595. {
  8596. //LOG_INFO("Insert reboot record successfully");
  8597. }
  8598. sprintf(insertSql, "delete from reboot_record where idx < (select idx from reboot_record order by idx desc limit 1)-2000;");
  8599. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8600. {
  8601. result = FAIL;
  8602. LOG_ERROR("delete reboot record error message: %s", errMsg);
  8603. }
  8604. else
  8605. {
  8606. //LOG_INFO("delete reboot record successfully");
  8607. }
  8608. sqlite3_close(db);
  8609. }
  8610. return result;
  8611. }
  8612. //===============================================
  8613. // Config process
  8614. //===============================================
  8615. void AddPlugInTimes(byte gunIndex)
  8616. {
  8617. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  8618. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  8619. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  8620. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  8621. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  8622. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  8623. }
  8624. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  8625. {
  8626. char cmdBuf[32];
  8627. struct timeb csuTime;
  8628. struct tm *tmCSU;
  8629. ftime(&csuTime);
  8630. tmCSU = localtime(&csuTime.time);
  8631. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  8632. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  8633. tmCSU->tm_sec);
  8634. if (isStart)
  8635. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  8636. else
  8637. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  8638. }
  8639. void zipLogFiles()
  8640. {
  8641. const char* logPath = "/Storage/SystemLog";
  8642. // 獲取目錄
  8643. DIR* pDir = opendir(logPath);
  8644. if (pDir != NULL)
  8645. {
  8646. struct timeb csuTime;
  8647. struct tm *tmCSU;
  8648. ftime(&csuTime);
  8649. tmCSU = localtime(&csuTime.time);
  8650. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  8651. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  8652. // tmCSU->tm_sec);
  8653. // Read items inside the folder
  8654. struct dirent* pEntry = NULL;
  8655. while ((pEntry = readdir(pDir)) != NULL)
  8656. {
  8657. if (strcmp(pEntry->d_name, ".") != 0 &&
  8658. strcmp(pEntry->d_name, "..") != 0 &&
  8659. strncmp(pEntry->d_name, "[", 1) == 0 &&
  8660. strstr(pEntry->d_name, "tar") < 0)
  8661. {
  8662. char yearC[5];
  8663. unsigned short year = 0;
  8664. char monthC[3];
  8665. unsigned short month = 0;
  8666. yearC[4] = '\0';
  8667. strncpy(yearC, pEntry->d_name + 1, 4);
  8668. monthC[2] = '\0';
  8669. strncpy(monthC, pEntry->d_name + 6, 2);
  8670. year = atoi(yearC);
  8671. month = atoi(monthC);
  8672. if (year != 0)
  8673. {
  8674. if (year < tmCSU->tm_year + 1900 ||
  8675. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  8676. {
  8677. LOG_INFO("tar file name : %s", pEntry->d_name);
  8678. char file[256];
  8679. memset(file, 0x00, sizeof(file));
  8680. strcat(file, "tar zcvf ");
  8681. strcat(file, logPath);
  8682. strncat(file, "/", 1);
  8683. strcat(file, pEntry->d_name);
  8684. strcat(file, ".tar");
  8685. strncat(file, " ", 1);
  8686. strcat(file, logPath);
  8687. strncat(file, "/", 1);
  8688. strcat(file, pEntry->d_name);
  8689. LOG_INFO("zip = %s \n", file);
  8690. system(file);
  8691. }
  8692. }
  8693. }
  8694. }
  8695. }
  8696. // Close folder
  8697. closedir(pDir);
  8698. }
  8699. void StopProcessingLoop()
  8700. {
  8701. for (;;)
  8702. {
  8703. CheckFactoryConfigFunction();
  8704. CheckFwUpdateFunction();
  8705. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  8706. {
  8707. ChkPrimaryStatus();
  8708. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0)
  8709. {
  8710. LOG_INFO("Soft reboot for retry self-tets (Primary).");
  8711. KillAllTask();
  8712. sleep(3);
  8713. TryCloseWatchdog();
  8714. system("/usr/bin/run_evse_restart.sh");
  8715. return;
  8716. }
  8717. }
  8718. if(Is_Ocpp_ResetReq())
  8719. {
  8720. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  8721. {
  8722. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = true;
  8723. }
  8724. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  8725. {
  8726. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  8727. }
  8728. }
  8729. CheckSoftHardReset();
  8730. sleep(1);
  8731. }
  8732. }
  8733. void CreateWatchdog(void)
  8734. {
  8735. wtdFd = InitWatchDog();
  8736. if(wtdFd < 0)
  8737. {
  8738. LOG_INFO("Watchdog Initial Fail");
  8739. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  8740. }
  8741. else
  8742. {
  8743. LOG_INFO("Watchdog Initial Success");
  8744. }
  8745. }
  8746. void TryCloseWatchdog(void)
  8747. {
  8748. if(wtdFd > 0)
  8749. {
  8750. write(wtdFd, "V", 1);
  8751. close(wtdFd);
  8752. }
  8753. }
  8754. void TryFeedWatchdog(void)
  8755. {
  8756. if(wtdFd > 0)
  8757. {
  8758. write(wtdFd, "a", 1);
  8759. }
  8760. }
  8761. bool IsParallelCabinetIdle(void)
  8762. {
  8763. bool result = true;
  8764. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  8765. {
  8766. result = false;
  8767. }
  8768. return result;
  8769. }
  8770. bool IsStandbyEnable(void)
  8771. {
  8772. bool result = true;
  8773. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  8774. {
  8775. time_t timep;
  8776. struct tm *tm;
  8777. time(&timep);
  8778. timep += (ShmChargerInfo->CabinetMiscValue.TimeOffset * 60);
  8779. tm = localtime(&timep);
  8780. if(tm->tm_hour >= 0 && tm->tm_hour < 8)
  8781. {
  8782. if(!_isStandbyEnable)
  8783. {
  8784. LOG_INFO("[TCC]Customer Enable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  8785. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  8786. }
  8787. _isStandbyEnable = true;
  8788. result = true;
  8789. }
  8790. else
  8791. {
  8792. if(_isStandbyEnable)
  8793. {
  8794. LOG_INFO("[TCC]Customer Disable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  8795. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  8796. }
  8797. _isStandbyEnable = false;
  8798. result = false;
  8799. }
  8800. }
  8801. return result;
  8802. }
  8803. bool IsGunIdle(int gunIndex, int idleType)
  8804. {
  8805. bool idle = true;
  8806. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  8807. chargingInfo[gunIndex]->ConnectorPlugIn &&
  8808. idleType == GUN_IDLE_TYPE_WHOLE)
  8809. {
  8810. idle = false;
  8811. }
  8812. else if (chargingInfo[gunIndex]->SystemStatus != S_IDLE &&
  8813. chargingInfo[gunIndex]->SystemStatus != S_RESERVATION &&
  8814. chargingInfo[gunIndex]->SystemStatus != S_FAULT &&
  8815. chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  8816. {
  8817. idle = false;
  8818. }
  8819. return idle;
  8820. }
  8821. bool IsDispenserIdle(int dispenserIndex)
  8822. {
  8823. bool dispenserIdle = true;
  8824. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  8825. {
  8826. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  8827. if(!IsGunIdle(gun, GUN_IDLE_TYPE_NORMAL))
  8828. {
  8829. dispenserIdle = false;
  8830. break;
  8831. }
  8832. }
  8833. return dispenserIdle;
  8834. }
  8835. bool IsConnectorWholeIdle(void)
  8836. {
  8837. bool result = true;
  8838. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  8839. {
  8840. if(!IsGunIdle(count, GUN_IDLE_TYPE_WHOLE))
  8841. {
  8842. result = false;
  8843. break;
  8844. }
  8845. }
  8846. return result;
  8847. }
  8848. bool IsUpgradeAvailable(void)
  8849. {
  8850. bool result = true;
  8851. for(int count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  8852. {
  8853. if(!IsGunIdle(count, GUN_IDLE_TYPE_NORMAL))
  8854. {
  8855. result = false;
  8856. break;
  8857. }
  8858. }
  8859. return result;
  8860. }
  8861. void ClearAlarmCodeWhenAcOff()
  8862. {
  8863. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  8864. {
  8865. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  8866. }
  8867. }
  8868. //==========================================
  8869. // Check task processing
  8870. //==========================================
  8871. void CheckTask()
  8872. {
  8873. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  8874. {
  8875. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  8876. {
  8877. if(system("pidof -s Module_4g > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  8878. {
  8879. LOG_ERROR("Module_4g not running, restart it.");
  8880. system("/root/Module_4g &");
  8881. }
  8882. }
  8883. }
  8884. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  8885. {
  8886. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  8887. {
  8888. if(system("pidof -s Module_Wifi > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  8889. {
  8890. LOG_ERROR("Module_Wifi not running, restart it.");
  8891. system("/root/Module_Wifi &");
  8892. }
  8893. }
  8894. }
  8895. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  8896. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  8897. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  8898. {
  8899. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = true;
  8900. if((time((time_t*)NULL) - Get_Ocpp_procDogTime()) > 180)
  8901. {
  8902. LOG_ERROR("OcppBackend watch dog timeout task restart.");
  8903. Clean_Ocpp_procDogTime();
  8904. Kill_Ocpp_Service();
  8905. }
  8906. if(!Is_Ocpp_Task_Alive())
  8907. {
  8908. LOG_ERROR("OcppBackend not running, restart it.");
  8909. Run_Ocpp_Service();
  8910. }
  8911. }
  8912. else
  8913. {
  8914. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = false;
  8915. }
  8916. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  8917. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  8918. {
  8919. if(!Is_MaintainOcpp_Task_Alive())
  8920. {
  8921. LOG_ERROR("Maintain Backend not running, restart it.");
  8922. Run_MaintainOcpp_Service();
  8923. }
  8924. }
  8925. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  8926. {
  8927. LOG_ERROR("Module_ProduceUtils not running, restart it.");
  8928. system ("/root/Module_ProduceUtils &");
  8929. }
  8930. if(system("pidof -s Module_EvComm > /dev/null") != 0)
  8931. {
  8932. LOG_ERROR("Module_EvComm not running, restart it.");
  8933. system ("/root/Module_EvComm &");
  8934. }
  8935. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  8936. {
  8937. LOG_ERROR("Module_InternalComm not running, restart it.");
  8938. system ("/root/Module_InternalComm &");
  8939. //ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  8940. }
  8941. if(!Is_WebServer_Alive())
  8942. {
  8943. LOG_ERROR("lighttpd not running, restart it.");
  8944. Run_WebServer_Service();
  8945. }
  8946. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  8947. {
  8948. if(!Is_LocalPowerSharing_Alive())
  8949. {
  8950. LOG_ERROR("Module_PowerSharing not running, restart it.");
  8951. Run_LocalPowerSharing_Service();
  8952. }
  8953. }
  8954. }
  8955. void InitialDHCP()
  8956. {
  8957. char tmpbuf[256];
  8958. memset(tmpbuf,0,256);
  8959. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  8960. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  8961. system(tmpbuf);
  8962. }
  8963. void InitialDispenserDhcpServerConfig(void)
  8964. {
  8965. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  8966. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  8967. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  8968. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  8969. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  8970. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  8971. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  8972. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  8973. usleep(1000);
  8974. }
  8975. void StartDispenserDhcpServer(void)
  8976. {
  8977. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  8978. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  8979. }
  8980. //==========================================
  8981. // Check Smart Charging Profile
  8982. //==========================================
  8983. void Ocpp_ProfileReq_Cmd(byte gunIndex)
  8984. {
  8985. if(!Is_Ocpp_ChargingProfileConf(gunIndex))
  8986. {
  8987. if(!Is_Ocpp_ChargingProfileReq(gunIndex))
  8988. {
  8989. Set_Ocpp_ChargingProfileReq(gunIndex);
  8990. }
  8991. }
  8992. }
  8993. int GetStartScheduleTime(unsigned char *time)
  8994. {
  8995. int result = -1;
  8996. struct tm tmScheduleStart;
  8997. struct timeb tbScheduleStart;
  8998. 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))
  8999. {
  9000. tmScheduleStart.tm_year -= 1900;
  9001. tmScheduleStart.tm_mon -= 1;
  9002. tbScheduleStart.time = mktime(&tmScheduleStart);
  9003. tbScheduleStart.millitm = 0;
  9004. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  9005. }
  9006. return result;
  9007. }
  9008. void Ocpp_ChargingProfile_Process(byte _index)
  9009. {
  9010. int _startCount = NO_DEFINE;
  9011. int _maxCount = 0;
  9012. float _limit = 0;
  9013. int _numberPhases = 0;
  9014. if(Is_Ocpp_ChargingProfileKind(_index, "Absolute") && Is_Ocpp_ChargingProfileId(_index))
  9015. {
  9016. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(_index);
  9017. _startCount = Get_Ocpp_ChargingProfileStartIndex(_index);
  9018. if(_startCount < _maxCount)
  9019. {
  9020. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9021. _numberPhases = Get_Ocpp_ChargingProfileScheduleNumberPhases(_index, _startCount);
  9022. chargingInfo[_index]->ChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9023. if(chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  9024. (int)chargingInfo[_index]->PresentChargingVoltage > 0)
  9025. {
  9026. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  9027. }
  9028. else
  9029. {
  9030. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9031. }
  9032. }
  9033. else
  9034. {
  9035. chargingInfo[_index]->ChargingProfilePower = -1;
  9036. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9037. }
  9038. }
  9039. else
  9040. {
  9041. chargingInfo[_index]->ChargingProfilePower = -1;
  9042. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9043. }
  9044. if((int)_lastProfilePower[_index] != (int)chargingInfo[_index]->ChargingProfilePower ||
  9045. (int)_lastProfileCurrent[_index] != (int)chargingInfo[_index]->ChargingProfileCurrent)
  9046. {
  9047. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9048. LOG_INFO("Gun %d Get Profile - Index = %d, Limit = %f",
  9049. _index + 1, _startCount, _limit);
  9050. LOG_INFO("Max: %d kW, Gun %d ChargingProfile Power = %d, Current = %d",
  9051. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower == -1 ? (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower : ((int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000),
  9052. _index + 1, (int)chargingInfo[_index]->ChargingProfilePower, (int)chargingInfo[_index]->ChargingProfileCurrent);
  9053. }
  9054. _lastProfilePower[_index] = chargingInfo[_index]->ChargingProfilePower;
  9055. _lastProfileCurrent[_index] = chargingInfo[_index]->ChargingProfileCurrent;
  9056. }
  9057. void Ocpp_MaxChargingProfile_Process(void)
  9058. {
  9059. int _startCount = NO_DEFINE;
  9060. int _maxCount = 0;
  9061. float _limit = 0;
  9062. int _numberPhases = 0;
  9063. if(Is_Ocpp_MaxChargingProfileKind("Absolute") && Is_Ocpp_MaxChargingProfileId())
  9064. {
  9065. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  9066. _startCount = Get_Ocpp_MaxProfileStartIndex();
  9067. if(_startCount < _maxCount)
  9068. {
  9069. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9070. _numberPhases = Get_Ocpp_MaxProfileScheduleNumberPhases(_startCount);
  9071. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9072. }
  9073. else
  9074. {
  9075. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9076. }
  9077. }
  9078. else
  9079. {
  9080. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9081. }
  9082. if((int)_lastMaxProfilePower != (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower)
  9083. {
  9084. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9085. LOG_INFO("Charger Get Max Profile - Index = %d, Limit = %f",
  9086. _startCount, _limit);
  9087. LOG_INFO("Charger Max Profile Power = %d", (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower);
  9088. }
  9089. _lastMaxProfilePower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower;
  9090. }
  9091. void CheckSmartChargeProfile(byte _index)
  9092. {
  9093. if(Is_Ocpp_ChargingProfileConf(_index))
  9094. {
  9095. Clean_Ocpp_ChargingProfileConf(_index);
  9096. Ocpp_MaxChargingProfile_Process();
  9097. Ocpp_ChargingProfile_Process(_index);
  9098. }
  9099. }
  9100. void GetLocalPowerLimit(byte gunIndex)
  9101. {
  9102. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9103. {
  9104. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  9105. {
  9106. float _localLimitPower = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gunIndex] * AC_OUTPUT_VOL;
  9107. if(chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 && (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  9108. {
  9109. chargingInfo[gunIndex]->LocalPowerLimitCurrent = _localLimitPower / chargingInfo[gunIndex]->PresentChargingVoltage;
  9110. }
  9111. else
  9112. {
  9113. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9114. }
  9115. }
  9116. else
  9117. {
  9118. chargingInfo[gunIndex]->LocalPowerLimitCurrent = 0;
  9119. }
  9120. }
  9121. else
  9122. {
  9123. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9124. }
  9125. }
  9126. void TheEndCharging(byte gun_index)
  9127. {
  9128. chargingInfo[gun_index]->isRemoteStart = NO;
  9129. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0 && ShmChargerInfo->UserTransaction[gun_index].TransactionId > 0)
  9130. {
  9131. char receiptString[512];
  9132. sprintf(receiptString, "%s%d",
  9133. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0], ShmChargerInfo->UserTransaction[gun_index].TransactionId);
  9134. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gun_index][0], receiptString);
  9135. Set_Connector_MiscCommand(gun_index, MISC_CONN_RECEIPT);
  9136. LOG_INFO("Gun %d ReceiptrURL: [%s]", gun_index + 1, receiptString);
  9137. }
  9138. ChangeStartOrStopDateTime(NO, gun_index);
  9139. DB_Insert_Record(localDb, gun_index);
  9140. }
  9141. void UpdateErrorCodeToOcpp(byte index)
  9142. {
  9143. //printf("ConnectorAlarmCode = %s \n", chargingInfo[index]->ConnectorAlarmCode);
  9144. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL)
  9145. {
  9146. SetOcppErrorCode(index, "InternalError");
  9147. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->ConnectorAlarmCode);
  9148. }
  9149. else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL)
  9150. {
  9151. SetOcppErrorCode(index, "OtherError");
  9152. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->EvConnAlarmCode);
  9153. }
  9154. }
  9155. void CheckMiscCommandRequirement(void)
  9156. {
  9157. BOOL NeedAnnouncement = FALSE;
  9158. unsigned char connector = 0;
  9159. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate != YES)
  9160. {
  9161. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9162. {
  9163. NeedAnnouncement = FALSE;
  9164. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  9165. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  9166. {
  9167. if(ShmChargerInfo->DispenserMiscReq[i].CtrlValue != 0)
  9168. {
  9169. NeedAnnouncement = TRUE;
  9170. }
  9171. for(int j = 0; j < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity; j++)
  9172. {
  9173. connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorID[j] - 1;
  9174. if(ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue != 0)
  9175. {
  9176. NeedAnnouncement = TRUE;
  9177. }
  9178. }
  9179. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement != NeedAnnouncement)
  9180. {
  9181. if(NeedAnnouncement)
  9182. {
  9183. //LOG_INFO("********** Dispenser %d Misc Command Need Announcement **********", i + 1);
  9184. }
  9185. else
  9186. {
  9187. //LOG_INFO("********** Dispenser %d Misc Command Announced **********", i + 1);
  9188. }
  9189. }
  9190. }
  9191. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  9192. }
  9193. }
  9194. }
  9195. void CheckDispenserVersionUpdateRequirement(void)
  9196. {
  9197. }
  9198. bool IsReadyParalleling(void)
  9199. {
  9200. bool ready = true;
  9201. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  9202. {
  9203. if(chargingInfo[i]->SystemStatus != S_IDLE && chargingInfo[i]->SystemStatus != S_MAINTAIN)
  9204. {
  9205. ready = false;
  9206. break;
  9207. }
  9208. }
  9209. return ready;
  9210. }
  9211. void SetParallelingStart(void)
  9212. {
  9213. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9214. {
  9215. if(chargingInfo[i]->SystemStatus == S_IDLE)
  9216. {
  9217. setChargerMode(i, MODE_MAINTAIN);
  9218. }
  9219. }
  9220. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Wait;
  9221. }
  9222. void SetParallelingDone(void)
  9223. {
  9224. unsigned short totalPow = 0;
  9225. totalPow = GetTotalParallelCabinetPower();
  9226. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9227. {
  9228. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9229. }
  9230. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9231. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_None;
  9232. }
  9233. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff)
  9234. {
  9235. unsigned char relay = 0;
  9236. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9237. {
  9238. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9239. {
  9240. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9241. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9242. {
  9243. relay = OnOff == YES ? YES : NO;
  9244. if(ShmChargerInfo->ParallelCabinet.PCabinet[index].OutputRelaySetting[index] != relay)
  9245. {
  9246. LOG_INFO("Set Parallel Cabinet Gun %d Output Relay %s", index + 1, relay == YES ? "ON" : "OFF");
  9247. }
  9248. ShmChargerInfo->ParallelCabinet.PCabinet[i].OutputRelaySetting[index] = relay;
  9249. }
  9250. }
  9251. }
  9252. }
  9253. void SetParallelCabinetAcContactor(unsigned char OnOff)
  9254. {
  9255. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9256. {
  9257. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9258. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9259. {
  9260. ShmChargerInfo->ParallelCabinet.PCabinet[i].AcContactorSetting = OnOff == YES ? YES : NO;
  9261. }
  9262. }
  9263. }
  9264. unsigned short GetTotalParallelCabinetPower(void)
  9265. {
  9266. unsigned short power = 0, slavePow = 0;
  9267. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9268. {
  9269. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  9270. {
  9271. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  9272. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  9273. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  9274. {
  9275. slavePow = ParsingRatingPower((char *)ShmChargerInfo->ParallelCabinet.PCabinet[i].ModelName);
  9276. power += slavePow;
  9277. }
  9278. }
  9279. }
  9280. return power;
  9281. }
  9282. unsigned char preParallelStatus = 0;
  9283. void MCabinetParallelingProcess(void)
  9284. {
  9285. bool PsuReInitial = false;
  9286. unsigned char status = 0;
  9287. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9288. {
  9289. if(IsReadyParalleling())
  9290. {
  9291. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9292. {
  9293. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest)
  9294. {
  9295. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest = false;
  9296. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm = true;
  9297. PsuReInitial = true;
  9298. }
  9299. }
  9300. if(PsuReInitial)
  9301. {
  9302. SetParallelingStart();
  9303. }
  9304. }
  9305. status = ShmChargerInfo->ParallelCabinet.ParallelStatus;
  9306. switch(status)
  9307. {
  9308. case _Parallel_None:
  9309. if(preParallelStatus != status)
  9310. {
  9311. LOG_INFO("MCabinet Parallel Status [None]");
  9312. GetClockTime(&_Parallel_Time);
  9313. }
  9314. unsigned short totalPow = 0;
  9315. totalPow = GetTotalParallelCabinetPower();
  9316. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9317. {
  9318. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9319. }
  9320. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9321. break;
  9322. case _Parallel_Wait:
  9323. if(preParallelStatus != status)
  9324. {
  9325. LOG_INFO("MCabinet Parallel Status [Wait]");
  9326. SetAcContactor(OFF);
  9327. SetParallelCabinetAcContactor(OFF);
  9328. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9329. {
  9330. SetPCabinetOutputRelay(i, OFF);
  9331. }
  9332. GetClockTime(&_Parallel_Time);
  9333. }
  9334. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9335. {
  9336. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Working;
  9337. }
  9338. break;
  9339. case _Parallel_Working:
  9340. if(preParallelStatus != status)
  9341. {
  9342. LOG_INFO("MCabinet Parallel Status [Working]");
  9343. SetAcContactor(ON);
  9344. SetParallelCabinetAcContactor(ON);
  9345. GetClockTime(&_Parallel_Time);
  9346. }
  9347. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  9348. {
  9349. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Done;
  9350. }
  9351. break;
  9352. case _Parallel_Done:
  9353. if(preParallelStatus != status)
  9354. {
  9355. LOG_INFO("MCabinet Parallel Status [Done]");
  9356. GetClockTime(&_Parallel_Time);
  9357. }
  9358. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9359. {
  9360. SetParallelingDone();
  9361. }
  9362. break;
  9363. }
  9364. preParallelStatus = status;
  9365. }
  9366. }
  9367. void SCabinetControllProcess(void)
  9368. {
  9369. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  9370. {
  9371. if(ShmChargerInfo->SCabinetControl.SAcContactor)
  9372. {
  9373. SetAcContactor(ON);
  9374. }
  9375. else
  9376. {
  9377. SetAcContactor(OFF);
  9378. }
  9379. unsigned short _parallelRelay = 0;
  9380. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9381. {
  9382. _parallelRelay = ShmChargerInfo->SCabinetControl.SParallelRelay[i] > 0 ? (1 < i) : 0;
  9383. }
  9384. if(_parallelRelay != ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue)
  9385. {
  9386. LOG_INFO("SCabinet Set Parallel Relay %X -> %X", ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue, _parallelRelay);
  9387. }
  9388. ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue = _parallelRelay;
  9389. }
  9390. }
  9391. void CheckConnectorDisconnectionRecovery(unsigned char connector)
  9392. {
  9393. unsigned char dispenser = 0;
  9394. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  9395. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  9396. {
  9397. LOG_INFO("Connector %d Disconnection Recovery", connector + 1);
  9398. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.Disconnection = false;
  9399. }
  9400. }
  9401. bool ConnectorRequestToCharging(unsigned char connector)
  9402. {
  9403. unsigned char role = 0;
  9404. bool done = false;
  9405. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9406. switch(role)
  9407. {
  9408. case _GROLE_IDLE:
  9409. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest)
  9410. {
  9411. LOG_INFO("Request Gun %d To Start Charging", connector + 1);
  9412. }
  9413. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest = true;
  9414. break;
  9415. case _GROLE_MASTER:
  9416. done = true;
  9417. break;
  9418. case _GROLE_SLAVE:
  9419. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest)
  9420. {
  9421. LOG_INFO("Gun %d Wait For Re-Grouping", connector + 1);
  9422. }
  9423. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest = true;
  9424. break;
  9425. case _GROLE_REQUEST_TO_CHARGING:
  9426. done = true;
  9427. break;
  9428. default:
  9429. break;
  9430. }
  9431. return done;
  9432. }
  9433. bool IsConnectorGroupingCompleted(unsigned char connector)
  9434. {
  9435. unsigned char role = 0;
  9436. bool completed = false;
  9437. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9438. switch(role)
  9439. {
  9440. case _GROLE_MASTER:
  9441. completed = true;
  9442. break;
  9443. default:
  9444. break;
  9445. }
  9446. return completed;
  9447. }
  9448. void SelfTestFailReason(void)
  9449. {
  9450. char reason[50];
  9451. memset(reason, 0x00, sizeof(reason));
  9452. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  9453. {
  9454. StatusCodeCompose(reason, "042251");
  9455. }
  9456. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail ||
  9457. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail ||
  9458. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9459. {
  9460. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail)
  9461. {
  9462. StatusCodeCompose(reason, "042280");
  9463. }
  9464. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9465. {
  9466. StatusCodeCompose(reason, "042281");
  9467. }
  9468. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail)
  9469. {
  9470. StatusCodeCompose(reason, "042282");
  9471. }
  9472. }
  9473. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail ||
  9474. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  9475. {
  9476. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail)
  9477. {
  9478. StatusCodeCompose(reason, "042285");
  9479. }
  9480. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  9481. {
  9482. StatusCodeCompose(reason, "042286");
  9483. }
  9484. }
  9485. else
  9486. {
  9487. StatusCodeCompose(reason, "Self Test Fail");
  9488. }
  9489. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  9490. {
  9491. SetOcppErrorCode(gun_index, "InternalError");
  9492. SetOcppVendorErrorCode(gun_index, reason);
  9493. setChargerMode(gun_index, MODE_MAINTAIN);
  9494. }
  9495. }
  9496. bool IsAvailableBackToIdle(int gun_index)
  9497. {
  9498. if(_plugInHold[gun_index] == false &&
  9499. (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false || chargingInfo[gun_index]->ConnectorPlugIn == false))
  9500. {
  9501. _plugInHold[gun_index] = true;
  9502. GetClockTime(&_PlugInHold_Time[gun_index]);
  9503. LOG_INFO ("Gun %d Plug In Hold Delay", gun_index + 1);
  9504. }
  9505. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle ||
  9506. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false ||
  9507. (!chargingInfo[gun_index]->ConnectorPlugIn && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm) ||
  9508. isResetStopChargeFlag(gun_index) == YES)
  9509. {
  9510. if(!_plugInHold[gun_index])
  9511. {
  9512. _plugInHold[gun_index] = true;
  9513. GetClockTime(&_PlugInHold_Time[gun_index]);
  9514. LOG_INFO ("Force Gun %d Plug In Hold Delay", gun_index + 1);
  9515. }
  9516. if((GetTimeoutValue(_PlugInHold_Time[gun_index]) / uSEC_VAL) >= PLUG_IN_HOLD_TIME)
  9517. {
  9518. return true;
  9519. }
  9520. }
  9521. return false;
  9522. }
  9523. void CheckGunAvailable(void)
  9524. {
  9525. if(!ShmChargerInfo->Control.SysCtrl.bits.PsuInit)
  9526. {
  9527. if(ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  9528. {
  9529. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9530. {
  9531. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  9532. {
  9533. ShmChargerInfo->Control.GunAvailable[i] = YES;
  9534. }
  9535. }
  9536. ShmChargerInfo->Control.SysCtrl.bits.PsuInit = true;
  9537. }
  9538. }
  9539. }
  9540. int presentChargedEnergyClear(unsigned char gun_index)
  9541. {
  9542. int result = FAIL;
  9543. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  9544. memset(chargingInfo[gun_index]->presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod) * sizeof(float));
  9545. result = PASS;
  9546. return result;
  9547. }
  9548. float presentChargedEnergyTotal(unsigned char gun_index)
  9549. {
  9550. float result = 0.0f;
  9551. for(int idx = 0; idx < ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod); idx++)
  9552. {
  9553. result += chargingInfo[gun_index]->presentChargedEnergyPeriod[idx];
  9554. }
  9555. return result;
  9556. }
  9557. int presentChargedEnergyUpdate(unsigned char gun_index, float kwh)
  9558. {
  9559. int result = FAIL;
  9560. time_t CurrentTime;
  9561. struct tm *tm;
  9562. CurrentTime = time(NULL);
  9563. tm=localtime(&CurrentTime);
  9564. chargingInfo[gun_index]->presentChargedEnergyPeriod[tm->tm_hour] += kwh;
  9565. return result;
  9566. }
  9567. void ChargedEnergySummation(unsigned char gun_index)
  9568. {
  9569. unsigned long time = 0;
  9570. time = GetSecTimeoutValue(_ChargedEnergy_Time[gun_index]);
  9571. if(time >= (chargingInfo[gun_index]->PresentChargedDuration + ENERAGY_INTERVAL))
  9572. {
  9573. chargingInfo[gun_index]->PresentChargedDuration++;
  9574. float changedEnergy = 0;
  9575. float _meterEnergy = 0;
  9576. float _calChangingEnergy = (chargingInfo[gun_index]->PresentChargingPower) / 3600;
  9577. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  9578. {
  9579. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= _lsatGunMeterValue[gun_index])
  9580. {
  9581. _meterEnergy = ShmChargerInfo->MeterValue[gun_index].GunConsumption - _lsatGunMeterValue[gun_index];
  9582. }
  9583. else
  9584. {
  9585. _meterEnergy = 0;
  9586. }
  9587. changedEnergy = _meterEnergy;
  9588. _lsatGunMeterValue[gun_index] = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  9589. }
  9590. else
  9591. {
  9592. changedEnergy = _calChangingEnergy;
  9593. }
  9594. if(ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  9595. {
  9596. chargingInfo[gun_index]->ChargingFee += changedEnergy * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  9597. }
  9598. presentChargedEnergyUpdate(gun_index, changedEnergy);
  9599. chargingInfo[gun_index]->PresentChargedEnergy += changedEnergy;
  9600. ShmChargerInfo->MeterValue[gun_index].LocalConsumption += changedEnergy;
  9601. }
  9602. }
  9603. void ChkConnectorAction(void)
  9604. {
  9605. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9606. {
  9607. if(ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel)
  9608. {
  9609. if(ShmChargerInfo->AllBill.OccupancyInfo[i].Status != _Parking_WaitToPay)
  9610. {
  9611. LOG_INFO("Gun %d Charging Canceled", i + 1);
  9612. Clean_Ocpp_TcciSerialNo(i);
  9613. }
  9614. else
  9615. {
  9616. LOG_INFO("Gun %d Occupancy Keep Counting", i + 1);
  9617. Set_OccupancyKeepCountingReq(i);
  9618. }
  9619. ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel = false;
  9620. }
  9621. }
  9622. }
  9623. void Update_DispenserAvailability(int dispenserIndex)
  9624. {
  9625. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  9626. {
  9627. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  9628. Set_Connector_MiscCommand(gun, MISC_CONN_AVAILABILITY);
  9629. }
  9630. }
  9631. void InitialDataBaseValue(void)
  9632. {
  9633. // Local DB
  9634. if(DB_Open(localDb) != PASS)
  9635. {
  9636. LOG_INFO("DB_Open fail.");
  9637. isDb_ready = false;
  9638. }
  9639. else
  9640. {
  9641. isDb_ready = true;
  9642. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9643. {
  9644. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  9645. LOG_INFO("Gun %d IsAvailable Initial: [%s]", _index + 1, chargingInfo[_index]->IsAvailable == YES ? "Enable" : "Disable");
  9646. }
  9647. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9648. {
  9649. ShmChargerInfo->MeterValue[_index].LocalConsumption = DB_Get_LocalConsumption(localDb, _index);
  9650. LOG_INFO("Local Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  9651. }
  9652. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9653. {
  9654. ShmChargerInfo->MeterValue[_index].GunConsumption = DB_Get_GunConsumption(localDb, _index);
  9655. LOG_INFO("Remote Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].GunConsumption);
  9656. }
  9657. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9658. {
  9659. Update_PowerConsumption(_index);
  9660. LOG_INFO("Gun %d Power Consumption Initial: [%.4f kWh]", _index + 1, chargingInfo[_index]->PowerConsumption);
  9661. }
  9662. DB_Reboot_Record(localDb);
  9663. }
  9664. if (NetworkDB_Open(networkDb) != PASS)
  9665. {
  9666. LOG_ERROR("NetworkDB_Open fail.");
  9667. }
  9668. }
  9669. float CalculateMaxOutputRatio(int gunIndex)
  9670. {
  9671. float ratio = 0;
  9672. int inUsingCnt = 0;
  9673. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9674. {
  9675. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  9676. {
  9677. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  9678. }
  9679. }
  9680. if(inUsingCnt != 0)
  9681. {
  9682. ratio = ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].Role == _GROLE_MASTER ?
  9683. ((float)ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].GunPsuQuantity / inUsingCnt) : 0;
  9684. }
  9685. ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio = ratio;
  9686. return ratio;
  9687. }
  9688. // return unit: 0.1kW
  9689. unsigned short GetModelNameLimitPower(int gunIndex)
  9690. {
  9691. unsigned short limitPower = 0;
  9692. float fPower = 0;
  9693. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9694. ShmChargerInfo->Control.ChargerRatingPower != 0)
  9695. {
  9696. fPower = (float)ShmChargerInfo->Control.ChargerRatingPower / 10;
  9697. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9698. {
  9699. fPower += (float)ShmChargerInfo->ParallelCabinet.TotalParallelPower / 10;
  9700. }
  9701. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9702. limitPower = (unsigned short)(fPower * 10);
  9703. }
  9704. return limitPower;
  9705. }
  9706. void GetWebPageConfigPowerLimit(int gunIndex)
  9707. {
  9708. unsigned short limitPower = 0;
  9709. float fPower = 0;
  9710. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9711. ShmSysConfigAndInfo->SysConfig.MaxChargingPower != 0)
  9712. {
  9713. fPower = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingPower;
  9714. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9715. limitPower = (unsigned short)(fPower * 10);
  9716. }
  9717. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower = limitPower;
  9718. }
  9719. void GetWebPageConfigCurrentLimit(int gunIndex)
  9720. {
  9721. unsigned short limitCurrent = 0;
  9722. float fCurrent = 0;
  9723. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9724. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent != 0)
  9725. {
  9726. fCurrent = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent;
  9727. fCurrent = fCurrent * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9728. limitCurrent = (unsigned short)(fCurrent * 10);
  9729. }
  9730. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent = limitCurrent;
  9731. }
  9732. void GetOffLineMaxChargingPowerLimit(int gunIndex)
  9733. {
  9734. unsigned short limitPower = 0;
  9735. float fPower = 0;
  9736. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9737. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower != 0 &&
  9738. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES)
  9739. {
  9740. fPower = ShmChargerInfo->OutputLimit.Offline_MaxChargingPower / 1000;
  9741. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9742. limitPower = (unsigned short)(fPower * 10);
  9743. }
  9744. ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] = limitPower * 100;
  9745. }
  9746. void GetMaxChargingProfileLimit(int gunIndex)
  9747. {
  9748. unsigned short limitPower = 0, limitCurrent = 0;
  9749. float fPower = 0;
  9750. // max charging profile
  9751. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9752. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower >= 0)
  9753. {
  9754. fPower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000;
  9755. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9756. limitPower = (unsigned short)(fPower * 10);
  9757. if (chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 &&
  9758. (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  9759. {
  9760. limitCurrent = (limitPower * 100 / chargingInfo[gunIndex]->PresentChargingVoltage) * 10;
  9761. }
  9762. }
  9763. ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] = limitPower * 100;
  9764. ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] = limitCurrent;
  9765. }
  9766. void GetMaxChargingLimit(int gunIndex)
  9767. {
  9768. GetWebPageConfigPowerLimit(gunIndex);
  9769. GetWebPageConfigCurrentLimit(gunIndex);
  9770. GetOffLineMaxChargingPowerLimit(gunIndex);
  9771. GetMaxChargingProfileLimit(gunIndex);
  9772. }
  9773. void GetGunOtpDerating(int gunIndex)
  9774. {
  9775. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp.isNeedDerating)
  9776. {
  9777. struct DERATING_BY_OTP *_derating;
  9778. _derating = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp;
  9779. if(_derating->deratingIndex < GUN_DERATING_COUNT)
  9780. {
  9781. int _connectorTemp = (int)chargingInfo[gunIndex]->ConnectorTemp - 60;
  9782. int _chillerTemp = (int)chargingInfo[gunIndex]->ChillerTemp - 60;
  9783. if(_connectorTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  9784. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  9785. {
  9786. LOG_INFO("Gun %d GunTemp %d is Derating(%d) to %d A",
  9787. gunIndex + 1,
  9788. _connectorTemp,
  9789. GunDeratingTemp[_derating->deratingIndex],
  9790. (_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  9791. _derating->deratingIndex++;
  9792. }
  9793. else if(_chillerTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  9794. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  9795. {
  9796. LOG_INFO("Gun %d ChillerTemp %d is Derating(%d) to %d A",
  9797. gunIndex + 1,
  9798. _chillerTemp,
  9799. GunDeratingTemp[_derating->deratingIndex],
  9800. (_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  9801. _derating->deratingIndex++;
  9802. }
  9803. }
  9804. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = _derating->deratingTargetCurrent[_derating->deratingIndex];
  9805. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  9806. }
  9807. else
  9808. {
  9809. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = 0;
  9810. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  9811. }
  9812. }
  9813. void CalculateGunAbsMaxPower(int gunIndex)
  9814. {
  9815. int maxValue = 0;
  9816. maxValue = (int)GetModelNameLimitPower(gunIndex);
  9817. ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] = maxValue != 0 ? (maxValue * 100) : 0;
  9818. }
  9819. void CalculateGunAbsMaxVoltage(int gunIndex)
  9820. {
  9821. int maxValue = 0;
  9822. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalVoltage;
  9823. ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] = maxValue;
  9824. }
  9825. void CalculateGunAbsMaxCurrent(int gunIndex)
  9826. {
  9827. int maxValue = 0;
  9828. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalCurrent;
  9829. ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] = maxValue;
  9830. }
  9831. void RestrictChargingCapability(int gunIndex, unsigned short *power, unsigned short *voltage, unsigned short *current)
  9832. {
  9833. unsigned short limitPower = 0;
  9834. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] != 0)
  9835. {
  9836. limitPower = ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] / 100;
  9837. if(limitPower != 0 && *power > limitPower)
  9838. {
  9839. *power = limitPower;
  9840. }
  9841. }
  9842. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] != 0 &&
  9843. *voltage > ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex])
  9844. {
  9845. *voltage = ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex];
  9846. }
  9847. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] != 0 &&
  9848. *current > ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex])
  9849. {
  9850. *current = ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex];
  9851. }
  9852. // restrict from WebPageConfig power
  9853. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower != 0)
  9854. {
  9855. if(*power > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower)
  9856. {
  9857. *power = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower;
  9858. }
  9859. }
  9860. // restrict from WebPageConfig current
  9861. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent != 0)
  9862. {
  9863. if(*current > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent)
  9864. {
  9865. *current = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent;
  9866. }
  9867. }
  9868. // restrict from Offline_MaxChargingPower
  9869. if(ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] != 0)
  9870. {
  9871. limitPower = ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] / 100;
  9872. if(limitPower != 0 && *power > limitPower)
  9873. {
  9874. *power = limitPower;
  9875. }
  9876. }
  9877. // restrict from MaxChargingProfile
  9878. if(ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] != 0)
  9879. {
  9880. limitPower = ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] / 100;
  9881. if(limitPower != 0 && *power > limitPower)
  9882. {
  9883. *power = limitPower;
  9884. }
  9885. }
  9886. // restrict from MaxChargingProfile
  9887. if(ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] != 0)
  9888. {
  9889. if(*current > ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex])
  9890. {
  9891. *current = ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex];
  9892. }
  9893. }
  9894. // restrict from ChargingProfile
  9895. if((chargingInfo[gunIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gunIndex]->SystemStatus <= S_CHARGING) ||
  9896. (chargingInfo[gunIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gunIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  9897. {
  9898. if(chargingInfo[gunIndex]->ChargingProfilePower >= 0 && *power > ((int)chargingInfo[gunIndex]->ChargingProfilePower / 100))
  9899. {
  9900. *power = (int)chargingInfo[gunIndex]->ChargingProfilePower / 100;
  9901. }
  9902. if(chargingInfo[gunIndex]->ChargingProfileCurrent >= 0 && *current > (int)chargingInfo[gunIndex]->ChargingProfileCurrent)
  9903. {
  9904. *current = (int)chargingInfo[gunIndex]->ChargingProfileCurrent;
  9905. }
  9906. }
  9907. // restrict from local power limit
  9908. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9909. {
  9910. if(chargingInfo[gunIndex]->LocalPowerLimitCurrent >= 0 && *current > (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10))
  9911. {
  9912. *current = (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10);
  9913. }
  9914. }
  9915. // restrict after otp
  9916. if(ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] != 0)
  9917. {
  9918. limitPower = ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] / 100;
  9919. if(limitPower != 0 && *power > limitPower)
  9920. {
  9921. *power = limitPower;
  9922. }
  9923. }
  9924. // restrict after otp
  9925. if(ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] != 0 &&
  9926. *current > ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex])
  9927. {
  9928. *current = ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex];
  9929. }
  9930. }
  9931. void UpdateChargingCapability(void)
  9932. {
  9933. unsigned short voltage = 0, current = 0, power = 0;
  9934. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower = Get_Ocpp_OffLineMaxChargingPower();
  9935. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  9936. {
  9937. voltage = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.MaximumChargingVoltage;
  9938. current = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingCurrent;
  9939. power = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingPower;
  9940. CalculateGunAbsMaxPower(i);
  9941. CalculateGunAbsMaxVoltage(i);
  9942. CalculateGunAbsMaxCurrent(i);
  9943. CalculateMaxOutputRatio(i);
  9944. GetMaxChargingLimit(i);
  9945. GetLocalPowerLimit(i);
  9946. GetGunOtpDerating(i);
  9947. RestrictChargingCapability(i, &power, &voltage, &current);
  9948. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityVoltage = voltage;
  9949. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityCurrent = current;
  9950. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityPower = power;
  9951. }
  9952. }
  9953. void GunErrorCollection(void)
  9954. {
  9955. bool _PsuProtectionAlarm = false;
  9956. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  9957. {
  9958. _PsuProtectionAlarm |= ShmChargerInfo->GunError[i].ErrFlag.bits.PsuGroupAllAlarmFault;
  9959. }
  9960. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = _PsuProtectionAlarm;
  9961. }
  9962. void UpdateGunPresentChargingPower(int gunIndex)
  9963. {
  9964. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Enable)
  9965. {
  9966. chargingInfo[gunIndex]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingCurrent))) / 1000 / 100);
  9967. }
  9968. else
  9969. {
  9970. chargingInfo[gunIndex]->PresentChargingPower = ((float)((chargingInfo[gunIndex]->PresentChargingVoltage) * (chargingInfo[gunIndex]->PresentChargingCurrent)) / 1000);
  9971. }
  9972. }
  9973. void CheckPsuLostQuantity(void)
  9974. {
  9975. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  9976. {
  9977. if(ShmChargerInfo->Control.PsuQuantityByModel == ShmPsuData->SystemPresentPsuQuantity)
  9978. {
  9979. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = false;
  9980. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = 0;
  9981. }
  9982. else
  9983. {
  9984. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = ShmChargerInfo->Control.PsuQuantityByModel > ShmPsuData->SystemPresentPsuQuantity ?
  9985. (ShmChargerInfo->Control.PsuQuantityByModel - ShmPsuData->SystemPresentPsuQuantity) : 0;
  9986. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch ||
  9987. _psuLostQuantity != ShmSysConfigAndInfo->SysInfo.PsuLostQuantity)
  9988. {
  9989. LOG_INFO("Psu Quantity is %d, it should be %d, Psu Lost: %d",
  9990. ShmPsuData->SystemPresentPsuQuantity, ShmChargerInfo->Control.PsuQuantityByModel,
  9991. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity);
  9992. }
  9993. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = true;
  9994. _psuLostQuantity = ShmSysConfigAndInfo->SysInfo.PsuLostQuantity;
  9995. }
  9996. }
  9997. }
  9998. int main(void)
  9999. {
  10000. unsigned long time = 0;
  10001. int _dispenser = 0;
  10002. _isStandbyEnable = true;
  10003. if(CreateShareMemory() == 0)
  10004. {
  10005. #ifdef SystemLogMessage
  10006. LOG_ERROR("CreatShareMemory NG");
  10007. #endif
  10008. if(ShmStatusCodeData!=NULL)
  10009. {
  10010. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  10011. }
  10012. sleep(5);
  10013. system("reboot -f");
  10014. sleep(5);
  10015. system("reboot -f");
  10016. }
  10017. LOG_INFO("\n");
  10018. LOG_INFO("===== Boot and Initialization start =====");
  10019. LOG_INFO("Main Fork PID = %d", getpid());
  10020. if (!InitialSystemDefaultConfig())
  10021. {
  10022. LOG_INFO("InitialSystemDefaultConfig NG");
  10023. StopProcessingLoop();
  10024. }
  10025. LOG_INFO("ModelName: [%s], SN: [%s], SW Version: [%s][%s]",
  10026. ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber, fwVersion, subVersion);
  10027. LOG_INFO("InitialShareMemoryInfo");
  10028. InitialShareMemoryInfo();
  10029. LOG_INFO("ChargerType (IEC or UL) = %d", ShmSysConfigAndInfo->SysInfo.ChargerType);
  10030. if(!InitialChargerSetting())
  10031. {
  10032. isModelNameMatch = false;
  10033. }
  10034. Initialization();
  10035. InitialDataBaseValue();
  10036. LOG_INFO("Spawn all Task.");
  10037. SpawnTask();
  10038. if (!isModelNameMatch)
  10039. {
  10040. LOG_INFO("Module Name & HW info none match.");
  10041. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  10042. // Module Name 與硬體對應不正確
  10043. LOG_ERROR("Module Name & HW info none match.");
  10044. sleep(3);
  10045. KillAllTask();
  10046. StopProcessingLoop();
  10047. }
  10048. LOG_INFO("Module Name & HW info correct. Initialize.......");
  10049. CreateTimeoutFork();
  10050. SelfTestRun();
  10051. while(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  10052. {
  10053. sleep(1);
  10054. }
  10055. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  10056. {
  10057. sleep(1);
  10058. SelfTestFailReason();
  10059. }
  10060. else
  10061. {
  10062. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10063. {
  10064. setChargerMode(gun_index, MODE_IDLE);
  10065. if(ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 ||
  10066. !ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  10067. {
  10068. ShmChargerInfo->Control.GunAvailable[gun_index] = YES;
  10069. }
  10070. }
  10071. }
  10072. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE)
  10073. {
  10074. SetAcContactor(ON);
  10075. SpawnOcppService();
  10076. SpawnMaintainOcppService();
  10077. }
  10078. else
  10079. {
  10080. InitSlaveCabinetEthernet();
  10081. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10082. {
  10083. setChargerMode(gun_index, MODE_MAINTAIN);
  10084. }
  10085. }
  10086. sleep(1);
  10087. CreateWatchdog();
  10088. // Main loop
  10089. LOG_INFO("%s \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_COMPLETE ?
  10090. "****************************Main Loop**********************************" :
  10091. "********************** SelfTest Fail **********************");
  10092. GetClockTime(&_cmdMainPriority_time);
  10093. for (;;)
  10094. {
  10095. CheckGunAvailable();
  10096. CheckOcppStatus();
  10097. ChkPrimaryStatus();
  10098. ChkPsuStatus();
  10099. PsuFailureResume();
  10100. ChkConnectorAction();
  10101. if(IsUpgradeAvailable())
  10102. {
  10103. CheckFactoryConfigFunction();
  10104. CheckFwUpdateFunction();
  10105. }
  10106. if(CheckFlashUpdate() != YES)
  10107. {
  10108. // return fork process
  10109. return 0;
  10110. }
  10111. CheckSoftHardReset();
  10112. StandbyCheck();
  10113. // 讀卡邏輯
  10114. //ScannerCardProcess();
  10115. AuthorizeTimeoutProcess();
  10116. // 當 AC 沒有搭上時,清除一些錯誤碼
  10117. ClearAlarmCodeWhenAcOff();
  10118. if ((GetTimeoutValue(_cmdMainPriority_time) / uSEC_VAL) > MAIN_PRIORITY_INTERVAL)
  10119. {
  10120. CheckTask();
  10121. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10122. {
  10123. if((chargingInfo[_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[_index]->SystemStatus <= S_CHARGING) ||
  10124. (chargingInfo[_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10125. {
  10126. if (chargingInfo[_index]->SystemStatus == S_CHARGING && _ocppProfileChkFlag[_index] == 12)
  10127. {
  10128. Ocpp_ProfileReq_Cmd(_index);
  10129. gunOutputVol[_index] = chargingInfo[_index]->PresentChargingVoltage;
  10130. _ocppProfileChkFlag[_index] = 0;
  10131. }
  10132. else if (chargingInfo[_index]->SystemStatus != S_CHARGING)
  10133. {
  10134. Ocpp_ProfileReq_Cmd(_index);
  10135. _ocppProfileChkFlag[_index] = 0;
  10136. }
  10137. else
  10138. {
  10139. _ocppProfileChkFlag[_index]++;
  10140. }
  10141. }
  10142. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  10143. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  10144. {
  10145. DB_Update_LocalConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  10146. if(ShmChargerInfo->MeterValue[_index].GunConsumption >= 0)
  10147. {
  10148. DB_Update_GunConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].GunConsumption);
  10149. }
  10150. }
  10151. }
  10152. CheckPsuLostQuantity();
  10153. GetClockTime(&_cmdMainPriority_time);
  10154. }
  10155. CheckMiscCommandRequirement();
  10156. CheckDispenserVersionUpdateRequirement();
  10157. MCabinetParallelingProcess();
  10158. SCabinetControllProcess();
  10159. UpdateChargingCapability();
  10160. GunErrorCollection();
  10161. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10162. {
  10163. //CheckGpioInStatus();
  10164. CheckErrorOccurStatus(gun_index);
  10165. ChkOcppStatus(gun_index);
  10166. if((chargingInfo[gun_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= S_CHARGING) ||
  10167. (chargingInfo[gun_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10168. {
  10169. if(chargingInfo[gun_index]->SystemStatus == S_CHARGING)
  10170. {
  10171. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  10172. if (chargingInfo[gun_index]->PresentChargingVoltage >= gunOutputVol[gun_index] + 10 ||
  10173. chargingInfo[gun_index]->PresentChargingVoltage <= gunOutputVol[gun_index] - 10)
  10174. {
  10175. _ocppProfileChkFlag[gun_index] = 12;
  10176. gunOutputVol[gun_index] = chargingInfo[gun_index]->PresentChargingVoltage;
  10177. }
  10178. }
  10179. }
  10180. CheckSmartChargeProfile(gun_index);
  10181. Update_PowerConsumption(gun_index);
  10182. CheckInProgress(gun_index);
  10183. CheckDebugMode(gun_index);
  10184. //LOG_INFO("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  10185. switch(chargingInfo[gun_index]->SystemStatus)
  10186. {
  10187. case S_MAINTAIN:
  10188. if(isModeChange(gun_index))
  10189. {
  10190. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10191. {
  10192. LOG_INFO("================== Slave Cabinet %d ==================", ShmChargerInfo->Control.CabinetSwitch - 1);
  10193. }
  10194. LOG_INFO("================== S_MAINTAIN (%x) ================== \n", gun_index + 1);
  10195. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10196. GetClockTime(&_SystemStatus_Time[gun_index]);
  10197. }
  10198. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  10199. if(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE ||
  10200. ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  10201. ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10202. {
  10203. break;
  10204. }
  10205. if(chargingInfo[gun_index]->IsAvailable == YES &&
  10206. ShmChargerInfo->Control.GunAvailable[gun_index] == YES &&
  10207. ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  10208. {
  10209. if(time >= MAINTAIN_RECOVERY_TIME)
  10210. {
  10211. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10212. setChargerMode(gun_index, MODE_IDLE);
  10213. }
  10214. }
  10215. break;
  10216. case S_RESERVATION:
  10217. if(isModeChange(gun_index))
  10218. {
  10219. LOG_INFO("================== S_RESERVATION (%x) ================== \n", gun_index + 1);
  10220. char expiryDate[128];
  10221. Get_Ocpp_ReserveNowExpiryDate(gun_index, expiryDate);
  10222. Get_Ocpp_ReserveNowIdTag(gun_index, &ShmChargerInfo->Control.ResevedIdTag[gun_index][0]);
  10223. Set_Connector_MiscCommand(gun_index, MISC_CONN_RESERVATION);
  10224. LOG_INFO("****** Reservation IdTag: [%s], Expiry: [%s]", &ShmChargerInfo->Control.ResevedIdTag[gun_index][0], expiryDate);
  10225. GetClockTime(&_SystemStatus_Time[gun_index]);
  10226. }
  10227. if(chargingInfo[gun_index]->ConnectorPlugIn)
  10228. {
  10229. SetAcContactor(ON);
  10230. }
  10231. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  10232. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  10233. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  10234. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10235. {
  10236. UpdateErrorCodeToOcpp(gun_index);
  10237. setChargerMode(gun_index, MODE_FAULT);
  10238. continue;
  10239. }
  10240. if(Is_Ocpp_ReserveNowExpired(gun_index))
  10241. {
  10242. chargingInfo[gun_index]->ReservationId = -1;
  10243. LOG_INFO("Gun %d reservation is expired...", gun_index + 1);
  10244. setChargerMode(gun_index, MODE_IDLE);
  10245. }
  10246. else
  10247. {
  10248. if(IsConnectorAuthorizeSuccess(gun_index))
  10249. {
  10250. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10251. {
  10252. LOG_INFO("Gun %d remote start reserve charging", gun_index + 1);
  10253. }
  10254. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  10255. {
  10256. LOG_INFO("Gun %d swipe rfid start reserve charging", gun_index + 1);
  10257. }
  10258. setChargerMode(gun_index, MODE_AUTHORIZING);
  10259. }
  10260. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  10261. {
  10262. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  10263. if(Is_Ocpp_ReserveNowIdTag(gun_index, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId))
  10264. {
  10265. SetAcContactor(ON);
  10266. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  10267. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10268. {
  10269. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  10270. LOG_INFO("Gun %d reserve Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  10271. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  10272. {
  10273. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  10274. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  10275. }
  10276. DispenserAuthInitial(_dispenser);
  10277. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10278. }
  10279. }
  10280. }
  10281. else
  10282. {
  10283. if(chargingInfo[gun_index]->ReservationId == -1)
  10284. {
  10285. LOG_INFO("Gun %d reservation is clean", gun_index + 1);
  10286. setChargerMode(gun_index, MODE_IDLE);
  10287. }
  10288. }
  10289. }
  10290. break;
  10291. case S_IDLE:
  10292. case S_FAULT:
  10293. {
  10294. // clean PermissionRequest flag
  10295. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  10296. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  10297. if(isModeChange(gun_index))
  10298. {
  10299. if (chargingInfo[gun_index]->SystemStatus == S_IDLE)
  10300. {
  10301. LOG_INFO("================== S_IDLE (%x) ================== \n", gun_index + 1);
  10302. chargingInfo[gun_index]->ReservationId = -1;
  10303. _triggerTransaction[gun_index] = false;
  10304. _plugInHold[gun_index] = false;
  10305. chargingInfo[gun_index]->PresentChargedDuration = 0;
  10306. chargingInfo[gun_index]->RemainChargingDuration = 0;
  10307. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  10308. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  10309. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  10310. memset(ShmChargerInfo->Timestamp[gun_index].StartCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StartCharging));
  10311. memset(ShmChargerInfo->Timestamp[gun_index].StopCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StopCharging));
  10312. Clean_Ocpp_StopReason(gun_index);
  10313. ReleaseAlarmCode(gun_index);
  10314. // clean connector information
  10315. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  10316. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  10317. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  10318. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  10319. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  10320. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  10321. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -65535;
  10322. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].CostDiscount = 0;
  10323. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemainAmount = 0;
  10324. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].EnergyCost = -1;
  10325. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParkingFee = -1;
  10326. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  10327. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.GetStartChargingSoc = false;
  10328. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  10329. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  10330. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  10331. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.OutputLimitEnable = false;
  10332. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].GeneralChargingData.deratingByConnOtp.deratingIndex = 0;
  10333. chargingInfo[gun_index]->EvBatterytargetVoltage = 0;
  10334. chargingInfo[gun_index]->EvBatterytargetCurrent = 0;
  10335. chargingInfo[gun_index]->EvBatteryMaxVoltage = 0;
  10336. chargingInfo[gun_index]->EvBatterySoc = 0;
  10337. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetVoltage = 0;
  10338. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetCurrent = 0;
  10339. // clean RemoteStart & RemoteStop flag
  10340. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_START);
  10341. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_STOP);
  10342. Clean_Connector_MiscCommand(gun_index, MISC_CONN_UNLOCK);
  10343. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  10344. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopConfirm = false;
  10345. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopConfirm = false;
  10346. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NormalStopRequest = false;
  10347. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AlarmStopRequest = false;
  10348. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  10349. ShmChargerInfo->GunError[gun_index].ErrFlag.ErrorValue = 0;
  10350. memset(chargingInfo[gun_index]->ConnectorAlarmCode, 0x00, sizeof(chargingInfo[gun_index]->ConnectorAlarmCode));
  10351. Clean_Ocpp_RunningCost(gun_index);
  10352. Clean_Ocpp_FinalCost(gun_index);
  10353. Clean_DeductInfo(gun_index);
  10354. Clean_Ocpp_TcciSerialNo(gun_index);
  10355. Clean_UserTransaction(gun_index);
  10356. Clean_UserPriceAndReceipt(gun_index);
  10357. presentChargedEnergyClear(gun_index);
  10358. chargingInfo[gun_index]->PresentChargingPower = 0;
  10359. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower = 0;
  10360. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent = 0;
  10361. chargingInfo[gun_index]->ChargingProfilePower = -1;
  10362. chargingInfo[gun_index]->ChargingProfileCurrent = -1;
  10363. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  10364. {
  10365. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging = false;
  10366. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger = false;
  10367. // reset dispenser availability
  10368. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  10369. }
  10370. // clean force charging info
  10371. memset(&ShmChargerInfo->Control.FCharging[gun_index], 0x00, sizeof(ForceCharging));
  10372. }
  10373. else if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  10374. {
  10375. LOG_INFO("================== S_FAULT (%x) ================== \n", gun_index + 1);
  10376. }
  10377. GetClockTime(&_SystemStatus_Time[gun_index]);
  10378. }
  10379. if (chargingInfo[gun_index]->IsAvailable == NO ||
  10380. ShmChargerInfo->Control.GunAvailable[gun_index] == NO)
  10381. {
  10382. setChargerMode(gun_index, MODE_MAINTAIN);
  10383. continue;
  10384. }
  10385. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  10386. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  10387. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  10388. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10389. {
  10390. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest)
  10391. {
  10392. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  10393. {
  10394. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemotenAlarmCode, 6);
  10395. }
  10396. }
  10397. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  10398. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10399. {
  10400. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  10401. {
  10402. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042304", 6);
  10403. LOG_INFO("Connector %d Disconnect Occur", gun_index + 1);
  10404. }
  10405. }
  10406. if(chargingInfo[gun_index]->SystemStatus != S_FAULT)
  10407. {
  10408. UpdateErrorCodeToOcpp(gun_index);
  10409. setChargerMode(gun_index, MODE_FAULT);
  10410. }
  10411. else
  10412. {
  10413. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10414. {
  10415. CheckConnectorDisconnectionRecovery(gun_index);
  10416. }
  10417. }
  10418. }
  10419. else
  10420. {
  10421. if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  10422. {
  10423. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  10424. if(time >= FAULT_RELEASE_TIME)
  10425. {
  10426. ReleaseAlarmCode(gun_index);
  10427. setChargerMode(gun_index, MODE_IDLE);
  10428. }
  10429. break;
  10430. }
  10431. // clean stop charge flag
  10432. chargingInfo[gun_index]->StopChargeFlag = false;
  10433. chargingInfo[gun_index]->ChargingStopFlag.ChargingStopValue = 0;
  10434. if(chargingInfo[gun_index]->ConnectorPlugIn)
  10435. {
  10436. SetAcContactor(ON);
  10437. }
  10438. { // Idle 正常程序起點
  10439. // 判斷是否有啟用檢查插槍
  10440. if(IsConnectorAuthorizeSuccess(gun_index))
  10441. {
  10442. if(chargingInfo[gun_index]->IsAvailable &&
  10443. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  10444. {
  10445. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10446. {
  10447. LOG_INFO("Gun %d remote start charging", gun_index + 1);
  10448. Clean_Ocpp_TcciCreditInfo(gun_index);
  10449. }
  10450. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  10451. {
  10452. LOG_INFO("Gun %d swipe rfid start charging", gun_index + 1);
  10453. }
  10454. setChargerMode(gun_index, MODE_AUTHORIZING);
  10455. continue;
  10456. }
  10457. }
  10458. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  10459. {
  10460. SetAcContactor(ON);
  10461. if(chargingInfo[gun_index]->IsAvailable &&
  10462. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  10463. chargingInfo[gun_index]->ConnectorPlugIn &&
  10464. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10465. {
  10466. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  10467. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  10468. LOG_INFO("Gun %d Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  10469. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  10470. {
  10471. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  10472. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  10473. }
  10474. DispenserAuthInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  10475. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10476. continue;
  10477. }
  10478. }
  10479. // only for test & debug purpose
  10480. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging &&
  10481. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging)
  10482. {
  10483. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging = false;
  10484. strcpy((char *)chargingInfo[gun_index]->StartUserId, "ChargingTest");
  10485. LOG_INFO("Gun %d Start Force Charging%s",
  10486. gun_index + 1,
  10487. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger > 0 ? ", Triggered by WebAPI" : "");
  10488. if(ShmChargerInfo->Control.TestCtrl.bits.FChargingReleaseExtend)
  10489. {
  10490. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableReleaseAndExtend = true;
  10491. LOG_INFO("Gun %d Enable ReleaseAndExtend At Force Charging", gun_index + 1);
  10492. }
  10493. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  10494. {
  10495. LOG_INFO("Set Dispenser %d Inoperative",
  10496. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex + 1);
  10497. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  10498. }
  10499. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10500. continue;
  10501. }
  10502. else
  10503. {
  10504. }
  10505. } // Idle 正常程序終點
  10506. }
  10507. }
  10508. break;
  10509. case S_AUTHORIZING:
  10510. if(isModeChange(gun_index))
  10511. {
  10512. LOG_INFO("================== S_AUTHORIZING (%x) ================== \n", gun_index + 1);
  10513. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10514. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  10515. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  10516. //StopSystemTimeoutDet();
  10517. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  10518. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  10519. SetAcContactor(ON);
  10520. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  10521. }
  10522. if(!_triggerTransaction[gun_index] &&
  10523. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC &&
  10524. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10525. {
  10526. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= STARTTRANSATION_TIME)
  10527. {
  10528. OcppStartTransaction(gun_index);
  10529. }
  10530. }
  10531. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  10532. {
  10533. FouceAnnounceAccountBalance(gun_index);
  10534. }
  10535. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm)
  10536. {
  10537. // sync with dispenser
  10538. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  10539. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  10540. LOG_INFO("Sync with Gun %d through remote start", gun_index + 1);
  10541. FouceAnnounceAccountBalance(gun_index);
  10542. }
  10543. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm)
  10544. {
  10545. // sync with dispenser
  10546. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm = false;
  10547. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  10548. LOG_INFO("Sync with Connector %d through swipe rfid", gun_index + 1);
  10549. }
  10550. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  10551. {
  10552. if(!chargingInfo[gun_index]->ConnectorPlugIn)
  10553. {
  10554. LOG_INFO("*********** Gun %d Plug In Timeout ***********", gun_index + 1);
  10555. }
  10556. else
  10557. {
  10558. LOG_INFO("*********** Gun %d Preparing State Timeout ***********", gun_index + 1);
  10559. }
  10560. OcppStopTransaction(gun_index);
  10561. setChargerMode(gun_index, MODE_IDLE);
  10562. }
  10563. else
  10564. {
  10565. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  10566. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10567. {
  10568. AddPlugInTimes(gun_index);
  10569. LOG_INFO("Gun %d CardNumber = [%s]", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  10570. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10571. GunAuthInitial(gun_index);
  10572. }
  10573. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  10574. {
  10575. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  10576. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  10577. LOG_INFO("*********** Gun %d User Stop Plug In ***********", gun_index + 1);
  10578. OcppStopTransaction(gun_index);
  10579. setChargerMode(gun_index, MODE_IDLE);
  10580. }
  10581. else if(isEvBoardStopChargeFlag(gun_index) == YES)
  10582. {
  10583. LOG_INFO("********** Gun %d Alarm Stop (S_AUTHORIZING) **********", gun_index + 1);
  10584. OcppStopTransaction(gun_index);
  10585. setChargerMode(gun_index, MODE_IDLE);
  10586. }
  10587. }
  10588. break;
  10589. case S_REASSIGN_CHECK:
  10590. {
  10591. if (isModeChange(gun_index))
  10592. {
  10593. LOG_INFO("================== S_REASSIGN_CHECK (%x) ================== \n", gun_index + 1);
  10594. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10595. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  10596. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  10597. //StopSystemTimeoutDet();
  10598. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  10599. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy = ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy;
  10600. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration = ShmSysConfigAndInfo->SysConfig.MaxChargingDuration;
  10601. LOG_INFO("Max Total Current %d A, Max Total Power %d kW, Total Energy %d kWh, Total Duration %d",
  10602. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent,
  10603. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  10604. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy,
  10605. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration);
  10606. SetAcContactor(ON);
  10607. SetPCabinetOutputRelay(gun_index, ON);
  10608. GetClockTime(&_SystemStatus_Time[gun_index]);
  10609. }
  10610. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10611. if(ConnectorRequestToCharging(gun_index))
  10612. {
  10613. setChargerMode(gun_index, MODE_REASSIGN);
  10614. }
  10615. else if(time >= WAIT_REASSIGN_TIME)
  10616. {
  10617. if(ShmPsuData->Work_Step != _WORK_CHARGING)
  10618. {
  10619. LOG_INFO("Gun %d Wait PSU Work Step Timeout", gun_index + 1);
  10620. }
  10621. else
  10622. {
  10623. LOG_INFO("Gun %d Request Charging Timeout", gun_index + 1);
  10624. }
  10625. setChargerMode(gun_index, MODE_IDLE);
  10626. }
  10627. }
  10628. break;
  10629. case S_REASSIGN:
  10630. {
  10631. if (isModeChange(gun_index))
  10632. {
  10633. LOG_INFO("================== S_REASSIGN (%x) ================== \n", gun_index + 1);
  10634. //gettimeofday(&_toAverage_time, NULL);
  10635. GetClockTime(&_SystemStatus_Time[gun_index]);
  10636. }
  10637. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10638. if(IsConnectorGroupingCompleted(gun_index))
  10639. {
  10640. setChargerMode(gun_index, MODE_PRECHARGE);
  10641. }
  10642. else if(time >= MAX_REQUEST_CHARGING_TIME)
  10643. {
  10644. LOG_INFO("Gun %d Grouping Timeout", gun_index + 1);
  10645. setChargerMode(gun_index, MODE_IDLE);
  10646. }
  10647. }
  10648. break;
  10649. case S_PREPARNING:
  10650. {
  10651. if (isModeChange(gun_index))
  10652. {
  10653. LOG_INFO("================== S_PREPARNING (%x) ================== \n", gun_index + 1);
  10654. //StopGunInfoTimeoutDet(gun_index);
  10655. //StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  10656. SetAcContactor(ON);
  10657. GetClockTime(&_SystemStatus_Time[gun_index]);
  10658. }
  10659. UpdateGunPresentChargingPower(gun_index);
  10660. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10661. if ((chargingInfo[gun_index]->AvailableChargingPower > 10) && time >= MIN_WAIT_PREPARNING_TIME)
  10662. {
  10663. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  10664. break;;
  10665. }
  10666. if (isEvBoardStopChargeFlag(gun_index) == YES)
  10667. {
  10668. // 板端或後臺要求停止
  10669. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARNING) **********", gun_index + 1);
  10670. Set_Ocpp_StopReason(gun_index, "Local");
  10671. ChargingAlarmProcess(gun_index);
  10672. }
  10673. else if(isNormalStopChargeFlag(gun_index) == YES)
  10674. {
  10675. LOG_INFO("********** Gun %d Normal Stop (S_PREPARNING) %s**********", gun_index + 1,
  10676. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10677. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10678. ChargingTerminalProcess(gun_index);
  10679. }
  10680. else if(isResetStopChargeFlag(gun_index) == YES)
  10681. {
  10682. LOG_INFO("********** Gun %d Reset Stop (S_PREPARNING) %s**********", gun_index + 1,
  10683. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10684. char *reason[16];
  10685. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10686. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10687. ChargingTerminalProcess(gun_index);
  10688. }
  10689. else if(isBackendStopFlag(gun_index))
  10690. {
  10691. LOG_INFO("********** Gun %d Backend Stop (S_PREPARNING) **********", gun_index + 1);
  10692. Set_Ocpp_StopReason(gun_index, "Other");
  10693. ChargingTerminalProcess(gun_index);
  10694. }
  10695. if(time > GUN_PREPARE_TIMEOUT)
  10696. {
  10697. LOG_INFO("********** Gun %d PrepareTimeout **********", gun_index + 1);
  10698. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  10699. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  10700. {
  10701. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "012279", 6);
  10702. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  10703. }
  10704. Set_Ocpp_StopReason(gun_index, "Local");
  10705. ChargingAlarmProcess(gun_index);
  10706. }
  10707. }
  10708. break;
  10709. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  10710. {
  10711. if (isModeChange(gun_index))
  10712. {
  10713. LOG_INFO("================== S_PREPARING_FOR_EV (%x) ================== \n", gun_index + 1);
  10714. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  10715. //StopGunInfoTimeoutDet(gun_index);
  10716. //StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  10717. GetClockTime(&_SystemStatus_Time[gun_index]);
  10718. }
  10719. UpdateGunPresentChargingPower(gun_index);
  10720. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10721. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  10722. {
  10723. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  10724. }
  10725. // only for test & debug purpose
  10726. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10727. {
  10728. LOG_INFO("Gun %d [Force Charging] S_PREPARING_FOR_EV OK", gun_index + 1);
  10729. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = true;
  10730. }
  10731. if (isEvBoardStopChargeFlag(gun_index) == YES)
  10732. {
  10733. // 板端或後臺要求停止
  10734. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  10735. Set_Ocpp_StopReason(gun_index, "Local");
  10736. ChargingAlarmProcess(gun_index);
  10737. }
  10738. else if(isNormalStopChargeFlag(gun_index) == YES)
  10739. {
  10740. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  10741. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10742. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10743. ChargingTerminalProcess(gun_index);
  10744. }
  10745. else if(isResetStopChargeFlag(gun_index) == YES)
  10746. {
  10747. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  10748. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10749. char *reason[16];
  10750. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10751. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10752. ChargingTerminalProcess(gun_index);
  10753. }
  10754. else if(isBackendStopFlag(gun_index))
  10755. {
  10756. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  10757. Set_Ocpp_StopReason(gun_index, "Other");
  10758. ChargingTerminalProcess(gun_index);
  10759. }
  10760. if(time >= GUN_EV_WAIT_TIMEOUT)
  10761. {
  10762. LOG_INFO("********** Gun %d Wait Dispenser Timeout **********", gun_index + 1);
  10763. ChargingTerminalProcess(gun_index);
  10764. }
  10765. }
  10766. break;
  10767. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  10768. {
  10769. if (isModeChange(gun_index))
  10770. {
  10771. LOG_INFO("================== S_PREPARING_FOR_EVSE (%x) ================== \n", gun_index + 1);
  10772. GetClockTime(&_SystemStatus_Time[gun_index]);
  10773. #ifdef STARTTRANSATION_IMMEDIATELY
  10774. OcppStartTransaction(gun_index);
  10775. #endif
  10776. }
  10777. CheckoutStartChargingSoc(gun_index, NO);
  10778. UpdateGunPresentChargingPower(gun_index);
  10779. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10780. // only for test & debug purpose
  10781. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10782. {
  10783. if(time >= FORCE_CHARGING_CMD_DELAY)
  10784. {
  10785. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  10786. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  10787. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  10788. {
  10789. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  10790. }
  10791. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  10792. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  10793. }
  10794. }
  10795. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Charging)
  10796. {
  10797. setChargerMode(gun_index, MODE_CHARGING);
  10798. }
  10799. // only for test & debug purpose
  10800. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10801. {
  10802. if(time >= FORCE_CHARGING_WAIT_EVSE)
  10803. {
  10804. LOG_INFO("Gun %d [Charging Simulation] S_PREPARING_FOR_EVSE OK", gun_index + 1);
  10805. setChargerMode(gun_index, MODE_CHARGING);
  10806. }
  10807. }
  10808. if (isEvBoardStopChargeFlag(gun_index) == YES)
  10809. {
  10810. // 板端或後臺要求停止
  10811. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  10812. Set_Ocpp_StopReason(gun_index, "Local");
  10813. ChargingAlarmProcess(gun_index);
  10814. }
  10815. else if(isNormalStopChargeFlag(gun_index) == YES)
  10816. {
  10817. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  10818. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10819. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10820. ChargingTerminalProcess(gun_index);
  10821. }
  10822. else if(isResetStopChargeFlag(gun_index) == YES)
  10823. {
  10824. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  10825. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10826. char *reason[16];
  10827. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10828. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10829. ChargingTerminalProcess(gun_index);
  10830. }
  10831. else if(isInvalidIdStopFlag(gun_index) == YES)
  10832. {
  10833. LOG_INFO("********** Gun %d InvalidId Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  10834. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  10835. ChargingTerminalProcess(gun_index);
  10836. }
  10837. else if(isBackendStopFlag(gun_index))
  10838. {
  10839. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  10840. Set_Ocpp_StopReason(gun_index, "Other");
  10841. ChargingTerminalProcess(gun_index);
  10842. }
  10843. if(time >= GUN_EV_WAIT_TIMEOUT)
  10844. {
  10845. LOG_INFO("********** Gun %d GFD timeout **********", gun_index + 1);
  10846. ChargingTerminalProcess(gun_index);
  10847. }
  10848. }
  10849. break;
  10850. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  10851. {
  10852. if (isModeChange(gun_index))
  10853. {
  10854. LOG_INFO("================== S_CHARGING (%x) ================== \n", gun_index + 1);
  10855. //StopGunInfoTimeoutDet(gun_index);
  10856. GetClockTime(&_SystemStatus_Time[gun_index]);
  10857. GetClockTime(&_ChargedEnergy_Time[gun_index]);
  10858. CheckoutStartChargingSoc(gun_index, YES);
  10859. chargingInfo[gun_index]->PresentChargedDuration = 0;
  10860. ChangeStartOrStopDateTime(YES, gun_index);
  10861. #ifndef STARTTRANSATION_IMMEDIATELY
  10862. OcppStartTransaction(gun_index);
  10863. #endif
  10864. }
  10865. // only for test & debug purpose
  10866. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10867. {
  10868. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  10869. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  10870. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  10871. {
  10872. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  10873. }
  10874. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  10875. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  10876. }
  10877. // 計算 Power
  10878. //chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  10879. //chargingInfo[gun_index]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent))) / 1000 / 100);
  10880. UpdateGunPresentChargingPower(gun_index);
  10881. ChargedEnergySummation(gun_index);
  10882. CheckSessionTargetStop(gun_index);
  10883. if (isEvBoardStopChargeFlag(gun_index))
  10884. {
  10885. LOG_INFO("********** Gun %d Alarm Stop (S_CHARGING) **********", gun_index + 1);
  10886. Set_Ocpp_StopReason(gun_index, "Local");
  10887. ChargingAlarmProcess(gun_index);
  10888. }
  10889. else if(isNormalStopChargeFlag(gun_index) == YES)
  10890. {
  10891. LOG_INFO("********** Gun %d Normal Stop (S_CHARGING) %s**********", gun_index + 1,
  10892. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10893. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10894. ChargingTerminalProcess(gun_index);
  10895. }
  10896. else if(isResetStopChargeFlag(gun_index) == YES)
  10897. {
  10898. LOG_INFO("********** Gun %d Reset Stop (S_CHARGING) %s**********", gun_index + 1,
  10899. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10900. char *reason[16];
  10901. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10902. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10903. ChargingTerminalProcess(gun_index);
  10904. }
  10905. else if(isInvalidIdStopFlag(gun_index) == YES)
  10906. {
  10907. LOG_INFO("********** Gun %d InvalidId Stop (S_CHARGING) **********", gun_index + 1);
  10908. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  10909. ChargingTerminalProcess(gun_index);
  10910. }
  10911. else if(CheckBackendChargingTimeout(gun_index) ||
  10912. CheckBackendChargingEnergy(gun_index) ||
  10913. IsReachConfigMaxChargingSoc(gun_index))
  10914. {
  10915. LOG_INFO("********** Gun %d Backend Condition Stop (S_CHARGING) **********", gun_index + 1);
  10916. ChargingTerminalProcess(gun_index);
  10917. }
  10918. else if(isSessionTargetStopFlag(gun_index) == YES)
  10919. {
  10920. LOG_INFO("********** Gun %d SessionTarget Stop (S_CHARGING) **********", gun_index + 1);
  10921. Set_Ocpp_StopReason(gun_index, "Other");
  10922. ChargingTerminalProcess(gun_index);
  10923. }
  10924. else if(isBackendStopFlag(gun_index))
  10925. {
  10926. LOG_INFO("********** Gun %d Backend Stop (S_CHARGING) **********", gun_index + 1);
  10927. Set_Ocpp_StopReason(gun_index, "Other");
  10928. ChargingTerminalProcess(gun_index);
  10929. }
  10930. }
  10931. break;
  10932. case S_ALARM:
  10933. case S_TERMINATING:
  10934. {
  10935. if (isModeChange(gun_index))
  10936. {
  10937. Set_Ocpp_StopReason(gun_index, "Other");
  10938. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  10939. {
  10940. LOG_INFO("================== S_ALARM (%x) ================== \n", gun_index + 1);
  10941. UpdateErrorCodeToOcpp(gun_index);
  10942. TheEndCharging(gun_index);
  10943. }
  10944. else
  10945. {
  10946. LOG_INFO("================== S_TERMINATING (%x) ================== \n", gun_index + 1);
  10947. }
  10948. //StopGunInfoTimeoutDet(gun_index);
  10949. GetClockTime(&_SystemStatus_Time[gun_index]);
  10950. }
  10951. UpdateGunPresentChargingPower(gun_index);
  10952. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10953. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  10954. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  10955. {
  10956. OcppStopTransaction(gun_index);
  10957. }
  10958. else
  10959. {
  10960. ChargedEnergySummation(gun_index);
  10961. }
  10962. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  10963. {
  10964. if(time >= GUN_COMP_WAIT_TIMEOUT)
  10965. {
  10966. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  10967. {
  10968. LOG_INFO ("********** Gun %d psu is releasable **********\n", gun_index + 1);
  10969. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  10970. }
  10971. }
  10972. if(IsAvailableBackToIdle(gun_index) == YES &&
  10973. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  10974. {
  10975. ReleaseAlarmCode(gun_index);
  10976. setChargerMode(gun_index, MODE_IDLE);
  10977. }
  10978. }
  10979. else
  10980. {
  10981. setChargerMode(gun_index, MODE_COMPLETE);
  10982. }
  10983. }
  10984. break;
  10985. case S_COMPLETE:
  10986. {
  10987. if (isModeChange(gun_index))
  10988. {
  10989. LOG_INFO("================== S_COMPLETE (%x) ================== \n", gun_index + 1);
  10990. TheEndCharging(gun_index);
  10991. GetClockTime(&_SystemStatus_Time[gun_index]);
  10992. }
  10993. UpdateGunPresentChargingPower(gun_index);
  10994. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10995. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  10996. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  10997. {
  10998. OcppStopTransaction(gun_index);
  10999. }
  11000. else
  11001. {
  11002. ChargedEnergySummation(gun_index);
  11003. }
  11004. if(time >= GUN_COMP_WAIT_TIMEOUT)
  11005. {
  11006. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11007. {
  11008. LOG_INFO ("********** Gun %d psu is releasable **********", gun_index + 1);
  11009. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  11010. }
  11011. }
  11012. if(IsAvailableBackToIdle(gun_index) == YES &&
  11013. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11014. {
  11015. setChargerMode(gun_index, MODE_IDLE);
  11016. }
  11017. }
  11018. break;
  11019. case S_CCS_PRECHARGE_ST0:
  11020. if (isModeChange(gun_index))
  11021. {
  11022. LOG_INFO("CCS Precharge Processing 1....................%x \n", gun_index);
  11023. //StopGunInfoTimeoutDet(gun_index);
  11024. //StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  11025. }
  11026. break;
  11027. case S_CCS_PRECHARGE_ST1:
  11028. if (isModeChange(gun_index))
  11029. {
  11030. LOG_INFO("CCS Precharge Processing 2....................%x \n", gun_index);
  11031. }
  11032. break;
  11033. case S_DEBUG:
  11034. if (isModeChange(gun_index))
  11035. {
  11036. LOG_INFO("================== S_DEBUG (%x) ================== \n", gun_index + 1);
  11037. }
  11038. if(!ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay &&
  11039. !ShmChargerInfo->Control.FanCtrl.bits.AbnormalFan)
  11040. {
  11041. setChargerMode(gun_index, MODE_IDLE);
  11042. }
  11043. break;
  11044. }
  11045. }
  11046. TryFeedWatchdog();
  11047. usleep(whileLoopTime);
  11048. }
  11049. return FAIL;
  11050. }