main.c 259 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848
  1. #include "define.h"
  2. #include "main.h"
  3. //#define CDFA_CERTIFICATE // Only enable for CDFA certificate to send receipt by Phihong back end API
  4. //==========================
  5. // System basic sample constant
  6. //==========================
  7. #define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)
  8. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  9. #define PASS 1
  10. #define FAIL -1
  11. #define YES 1
  12. #define NO 0
  13. #define ON 1
  14. #define OFF 0
  15. #define BUFFER_SIZE 128
  16. //==========================
  17. // Timeout constant define
  18. //==========================
  19. #define TIMEOUT_SPEC_HANDSHAKING 180
  20. #define TIMEOUT_SPEC_AUTH 15
  21. #define TIMEOUT_SPEC_HANDSHAKING_LED 185
  22. #define TIMEOUT_SPEC_LOGPPRINTOUT 30
  23. #define TIMEOUT_SPEC_PROFILE_PREPARE 60
  24. #define TIMEOUT_SPEC_BS_HLC_HANDSHAKE 25
  25. #define TIMEOUT_SPEC_EV_READY 30
  26. #define TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET 10
  27. #define TIMEOUT_SPEC_CCS_HANDSHAKE 120
  28. #define TIMEOUT_SPEC_PWN_CHANGE 5
  29. #define TIMEOUT_SPEC_POWERSAVING_LCD 120
  30. #define TIMEOUT_SPEC_POWERSAVING_RFID 120
  31. #define TIMEOUT_SPEC_POWERSAVING_METER 120
  32. #define TIMEOUT_SPEC_POWERSAVING_LED_STATUS 120
  33. #define TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION 15
  34. #define TIMEOUT_SPEC_RESET_WIFI_MODULE 300
  35. //==========================
  36. // GPIO constant define
  37. //==========================
  38. #define GPIO_OUT_RST_RFID 62
  39. #define GPIO_OUT_RST_4G 114
  40. #define GPIO_OUT_RST_QCA 115
  41. #define GPIO_OUT_RST_ETH 56
  42. #define GPIO_IN_WAKEUP 63
  43. #define GPIO_OUT_RST_4G_WIFI 59
  44. #define MtdBlockSize 0x300000
  45. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  46. //==========================
  47. // Declare method
  48. //==========================
  49. void trim(char *s);
  50. int mystrcmp(char *p1,char *p2);
  51. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  52. void split(char **arr, char *str, const char *del);
  53. int isReachableInternet();
  54. int isRouteFail();
  55. int InitRfidPort(void);
  56. int GetCardSerialNumber();
  57. void setLedMotion(unsigned char gun_index,unsigned char led_mode);
  58. void setRequest(unsigned char gun_index,unsigned char isOn);
  59. void setSpeaker(unsigned char isOn, unsigned char speaker_mode);
  60. //==========================
  61. // Declare RFID module type
  62. //==========================
  63. #define MODULE_EWT 0
  64. int wtdFd = -1;
  65. int rfidFd = -1;
  66. char* rfidPortName = "/dev/ttyS2";
  67. RFID rfid;
  68. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  69. //==========================
  70. // Declare share memory
  71. //==========================
  72. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  73. struct StatusCodeData *ShmStatusCodeData;
  74. struct PsuData *ShmPsuData;
  75. struct CHAdeMOData *ShmCHAdeMOData;
  76. struct CcsData *ShmCcsData;
  77. struct PrimaryMcuData *ShmPrimaryMcuData;
  78. struct FanModuleData *ShmFanModuleData;
  79. struct RelayModuleData *ShmRelayModuleData;
  80. struct OCPP16Data *ShmOCPP16Data;
  81. struct OCPP20Data *ShmOCPP20Data;
  82. struct Charger *ShmCharger;
  83. struct timespec startTime[AC_QUANTITY][TMR_IDX_CNT];
  84. struct timespec startChargingTime[AC_QUANTITY];
  85. struct timespec endChargingTime[AC_QUANTITY];
  86. sqlite3 *localDb;
  87. struct SysConfigData SysConfigOrg;
  88. ParsingRatedCur modelnameInfo={0};
  89. struct PreviousData
  90. {
  91. uint16_t targetCurrent;
  92. uint16_t current_limit;
  93. uint16_t primaryMcuCp_Pwn_Duty;
  94. }previousData[4];
  95. //=================================
  96. // Common routine
  97. //=================================
  98. void trim(char *s)
  99. {
  100. int i=0, j, k, l=0;
  101. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  102. i++;
  103. j = strlen(s)-1;
  104. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  105. j--;
  106. if(i==0 && j==strlen(s)-1) { }
  107. else if(i==0) s[j+1] = '\0';
  108. else {
  109. for(k=i; k<=j; k++) s[l++] = s[k];
  110. s[l] = '\0';
  111. }
  112. }
  113. int mystrcmp(char *p1,char *p2)
  114. {
  115. while(*p1==*p2)
  116. {
  117. if(*p1=='\0' || *p2=='\0')
  118. break;
  119. p1++;
  120. p2++;
  121. }
  122. if(*p1=='\0' && *p2=='\0')
  123. return(PASS);
  124. else
  125. return(FAIL);
  126. }
  127. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  128. {
  129. strncpy(dest, src + start, cnt);
  130. dest[cnt] = 0;
  131. }
  132. void split(char **arr, char *str, const char *del)
  133. {
  134. char *s = strtok(str, del);
  135. while(s != NULL)
  136. {
  137. *arr++ = s;
  138. s = strtok(NULL, del);
  139. }
  140. }
  141. int StoreLogMsg(const char *fmt, ...)
  142. {
  143. char Buf[4096+256];
  144. char buffer[4096];
  145. time_t CurrentTime;
  146. struct tm *tm;
  147. struct timeval tv;
  148. va_list args;
  149. va_start(args, fmt);
  150. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  151. va_end(args);
  152. memset(Buf,0,sizeof(Buf));
  153. CurrentTime = time(NULL);
  154. tm=localtime(&CurrentTime);
  155. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  156. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  157. {
  158. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]%s_%s_SystemLog",
  159. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  160. buffer,
  161. tm->tm_year+1900,tm->tm_mon+1,
  162. ShmSysConfigAndInfo->SysConfig.ModelName,
  163. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  164. }
  165. else
  166. {
  167. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  168. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  169. buffer,
  170. tm->tm_year+1900,tm->tm_mon+1);
  171. }
  172. #ifdef SystemLogMessage
  173. system(Buf);
  174. #endif
  175. #ifdef ConsloePrintLog
  176. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  177. #endif
  178. return rc;
  179. }
  180. void refreshStartTimer(struct timespec *timer)
  181. {
  182. clock_gettime(CLOCK_MONOTONIC, timer);
  183. }
  184. int getDiffSecNow(struct timespec timer)
  185. {
  186. struct timespec timerNow;
  187. clock_gettime(CLOCK_MONOTONIC, &timerNow);
  188. return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000);
  189. }
  190. int getDiffSecBetween(struct timespec start, struct timespec end)
  191. {
  192. return (int)((((unsigned long)(end.tv_sec - start.tv_sec) * 1000) + ((unsigned long)((end.tv_nsec / 1000000) - (start.tv_nsec / 1000000))))/1000);
  193. }
  194. long long DiffTimebWithNow(struct timeb ST)
  195. {
  196. //return milli-second
  197. struct timeb ET;
  198. long long StartTime,StopTime;
  199. ftime(&ET);
  200. StartTime=(long long)ST.time;
  201. StopTime=(long long)ET.time;
  202. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  203. }
  204. long long DiffTimeb(struct timeb ST, struct timeb ET)
  205. {
  206. //return milli-second
  207. long long StartTime,StopTime;
  208. StartTime=(long long)ST.time;
  209. StopTime=(long long)ET.time;
  210. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  211. }
  212. int DiffTimebWithNowSec(struct timeb ST)
  213. {
  214. //return milli-second
  215. struct timeb ET;
  216. unsigned int StartTime,StopTime;
  217. ftime(&ET);
  218. StartTime=(unsigned int)ST.time;
  219. StopTime=(unsigned int)ET.time;
  220. return (StopTime-StartTime);
  221. }
  222. int isOvertNow(uint8_t *start)
  223. {
  224. int result = YES;
  225. struct ParsingResult
  226. {
  227. int result;
  228. int scanedElement;
  229. int year;
  230. int month;
  231. int mday;
  232. int hour;
  233. int min;
  234. int sec;
  235. int tz_hour;
  236. int tz_min;
  237. float minSec;
  238. }parsingResult;
  239. struct tm tmStart;
  240. struct timeb tbStart;
  241. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  242. if(strstr((char*)start, ".") != NULL)
  243. {
  244. // Original data with mini second
  245. if(strstr((char*)start, "Z") != NULL)
  246. {
  247. // Original data with Z
  248. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  249. &parsingResult.year,
  250. &parsingResult.month,
  251. &parsingResult.mday,
  252. &parsingResult.hour,
  253. &parsingResult.min,
  254. &parsingResult.sec,
  255. &parsingResult.minSec);
  256. }
  257. else
  258. {
  259. // Original data without Z
  260. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  261. &parsingResult.year,
  262. &parsingResult.month,
  263. &parsingResult.mday,
  264. &parsingResult.hour,
  265. &parsingResult.min,
  266. &parsingResult.sec,
  267. &parsingResult.minSec,
  268. &parsingResult.tz_hour,
  269. &parsingResult.tz_min);
  270. }
  271. }
  272. else
  273. {
  274. // Original data without mini second
  275. if(strstr((char*)start, "Z") != NULL)
  276. {
  277. // Original data with Z
  278. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  279. &parsingResult.year,
  280. &parsingResult.month,
  281. &parsingResult.mday,
  282. &parsingResult.hour,
  283. &parsingResult.min,
  284. &parsingResult.sec);
  285. }
  286. else
  287. {
  288. // Original data without Z
  289. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  290. &parsingResult.year,
  291. &parsingResult.month,
  292. &parsingResult.mday,
  293. &parsingResult.hour,
  294. &parsingResult.min,
  295. &parsingResult.sec,
  296. &parsingResult.tz_hour,
  297. &parsingResult.tz_min);
  298. }
  299. }
  300. if(parsingResult.scanedElement >= 6)
  301. {
  302. tmStart.tm_year = parsingResult.year - 1900;
  303. tmStart.tm_mon = parsingResult.month - 1;
  304. tmStart.tm_mday = parsingResult.mday;
  305. tmStart.tm_hour = parsingResult.hour;
  306. tmStart.tm_min = parsingResult.min;
  307. tmStart.tm_sec = parsingResult.sec;
  308. tmStart.tm_gmtoff = 0;
  309. tbStart.time = mktime(&tmStart);
  310. tbStart.timezone = 0;
  311. tbStart.millitm = 0;
  312. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  313. if(DiffTimebWithNowSec(tbStart) <= 0)
  314. result = NO;
  315. else
  316. result = YES;
  317. }
  318. else
  319. {
  320. DEBUG_WARN("Start date parsing error.\n");
  321. }
  322. return result;
  323. }
  324. void getDateTimeString(char* result)
  325. {
  326. time_t CurrentTime;
  327. struct tm *tm;
  328. CurrentTime = time(NULL);
  329. tm=localtime(&CurrentTime);
  330. sprintf(result, "%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);
  331. }
  332. int getCurrentYear()
  333. {
  334. int result = 0;
  335. time_t CurrentTime;
  336. struct tm *tm;
  337. CurrentTime = time(NULL);
  338. tm=localtime(&CurrentTime);
  339. result = (tm->tm_year + 1900);
  340. return result;
  341. }
  342. unsigned long long getAvailableMemory()
  343. {
  344. long pages = sysconf(_SC_AVPHYS_PAGES);
  345. long page_size = sysconf(_SC_PAGE_SIZE);
  346. return pages * page_size;
  347. }
  348. unsigned int isKernelSupportNAT()
  349. {
  350. unsigned int result = NO;
  351. unsigned int version = 0;
  352. FILE *fp;
  353. char cmd[256];
  354. char buf[512];
  355. // Get IP address & net mask
  356. strcpy(cmd, "uname -v");
  357. fp = popen(cmd, "r");
  358. if(fp != NULL)
  359. {
  360. if(fgets(buf, sizeof(buf), fp) != NULL)
  361. {
  362. sscanf(buf, "#%d", &version);
  363. //DEBUG_INFO("Kernel version: %d\n", version);
  364. if(version >= 30)
  365. result = YES;
  366. }
  367. }
  368. pclose(fp);
  369. return result;
  370. }
  371. int getEth0MacAddress()
  372. {
  373. int result = PASS;
  374. FILE *fp;
  375. char cmd[256];
  376. char buf[512];
  377. char tmp[512];
  378. strcpy(cmd, "ifconfig eth0");
  379. fp = popen(cmd, "r");
  380. if(fp != NULL)
  381. {
  382. while(fgets(buf, sizeof(buf), fp) != NULL)
  383. {
  384. if(strstr(buf, "eth0") > 0)
  385. {
  386. result = PASS;
  387. }
  388. if(strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
  389. {
  390. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  391. strcpy((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
  392. }
  393. }
  394. }
  395. pclose(fp);
  396. return result;
  397. }
  398. //======================================================
  399. // OCPP routine
  400. //======================================================
  401. void ocpp_process_start()
  402. {
  403. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  404. system("/root/OcppBackend &");
  405. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  406. system("/root/OcppBackend20 &");
  407. }
  408. uint8_t ocpp_get_isRemoteStartWait()
  409. {
  410. uint8_t result = OFF;
  411. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  412. {
  413. result = ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  414. }
  415. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  416. {
  417. result = ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  418. }
  419. return result;
  420. }
  421. uint8_t ocpp_get_connection_status()
  422. {
  423. uint8_t result = OFF;
  424. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  425. {
  426. result = ShmOCPP16Data->OcppConnStatus;
  427. }
  428. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  429. {
  430. result = ShmOCPP20Data->OcppConnStatus;
  431. }
  432. return result;
  433. }
  434. uint16_t ocpp_get_connection_timeout()
  435. {
  436. uint16_t result = TIMEOUT_SPEC_HANDSHAKING;
  437. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  438. {
  439. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  440. {
  441. result = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  442. }
  443. }
  444. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  445. {
  446. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value,"") != 0)
  447. {
  448. result = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  449. }
  450. }
  451. return result;
  452. }
  453. uint8_t ocpp_get_update_firmware_req()
  454. {
  455. uint8_t result = NO;
  456. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  457. {
  458. result = ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq;
  459. }
  460. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  461. {
  462. result = ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq;
  463. }
  464. return result;
  465. }
  466. uint8_t ocpp_get_reset_req()
  467. {
  468. uint8_t result = NO;
  469. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  470. {
  471. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  472. }
  473. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  474. {
  475. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  476. }
  477. return result;
  478. }
  479. void ocpp_boot_info_sync()
  480. {
  481. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  482. {
  483. memcpy((char*)ShmOCPP16Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  484. memcpy((char*)ShmOCPP16Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  485. sprintf((char*)ShmOCPP16Data->BootNotification.CpFwVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  486. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
  487. switch(ShmSysConfigAndInfo->SysConfig.ModelName[3])
  488. {
  489. case 'M':
  490. case 'Z':
  491. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "MID");
  492. break;
  493. case 'P':
  494. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "PTB");
  495. break;
  496. case 'I':
  497. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "TIC");
  498. break;
  499. case 'U':
  500. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "UL");
  501. break;
  502. default:
  503. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "N/A");
  504. break;
  505. }
  506. }
  507. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  508. {
  509. memcpy((char*)ShmOCPP20Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  510. memcpy((char*)ShmOCPP20Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  511. sprintf((char*)ShmOCPP20Data->BootNotification.chargingStation.firmwareVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  512. }
  513. }
  514. void ocpp_set_remotestart(uint8_t gun_index, uint8_t status)
  515. {
  516. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  517. {
  518. if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq != status)
  519. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = status;
  520. }
  521. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  522. {
  523. if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq != status)
  524. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = status;
  525. }
  526. }
  527. void ocpp_set_remotestop(uint8_t gun_index, uint8_t status)
  528. {
  529. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  530. {
  531. if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != status)
  532. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = status;
  533. }
  534. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  535. {
  536. if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq != status)
  537. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = status;
  538. }
  539. }
  540. uint8_t ocpp_get_remotestart(uint8_t gun_index)
  541. {
  542. uint8_t result = OFF;
  543. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  544. {
  545. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
  546. }
  547. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  548. {
  549. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq;
  550. }
  551. return result;
  552. }
  553. void ocpp_copy_userid_from_remotestart(uint8_t gun_index)
  554. {
  555. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  556. {
  557. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  558. }
  559. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  560. {
  561. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  562. }
  563. }
  564. uint8_t ocpp_get_remotestop(uint8_t gun_index)
  565. {
  566. uint8_t result = OFF;
  567. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  568. {
  569. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq;
  570. }
  571. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  572. {
  573. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq;
  574. }
  575. return result;
  576. }
  577. void ocpp_set_auth_req(uint8_t status, ...)
  578. {
  579. va_list args;
  580. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  581. {
  582. if(ShmOCPP16Data->SpMsg.bits.AuthorizeReq != status)
  583. {
  584. if(status == ON)
  585. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, 0x00, ARRAY_SIZE(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  586. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = status;
  587. }
  588. }
  589. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  590. {
  591. if(ShmOCPP20Data->SpMsg.bits.AuthorizeReq != status)
  592. {
  593. if(status == ON)
  594. {
  595. memset(&ShmOCPP20Data->Authorize.Response_idTokenInfo, 0x00, sizeof(struct IdTokenInfoType));
  596. va_start(args, status);
  597. sprintf((char*)ShmOCPP20Data->Authorize.idToken.type, "%s", va_arg(args, char*));
  598. va_end(args);
  599. }
  600. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = status;
  601. }
  602. }
  603. }
  604. uint8_t ocpp_get_auth_req()
  605. {
  606. uint8_t result = OFF;
  607. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  608. {
  609. result = ShmOCPP16Data->SpMsg.bits.AuthorizeReq;
  610. }
  611. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  612. {
  613. result = ShmOCPP20Data->SpMsg.bits.AuthorizeReq;
  614. }
  615. return result;
  616. }
  617. void ocpp_set_auth_conf(uint8_t status)
  618. {
  619. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  620. {
  621. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf != status)
  622. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = status;
  623. }
  624. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  625. {
  626. if(ShmOCPP20Data->SpMsg.bits.AuthorizeConf != status)
  627. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = status;
  628. }
  629. }
  630. uint8_t ocpp_get_auth_conf()
  631. {
  632. uint8_t result = OFF;
  633. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  634. {
  635. result = ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  636. }
  637. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  638. {
  639. result = ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
  640. }
  641. return result;
  642. }
  643. uint8_t ocpp_get_auth_result(uint8_t isValidParent, ...)
  644. {
  645. uint8_t result = OFF;
  646. va_list args;
  647. uint8_t gun_index;
  648. if(isValidParent)
  649. {
  650. va_start(args, isValidParent);
  651. gun_index = va_arg(args, int);
  652. va_end(args);
  653. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  654. {
  655. case SYS_MODE_AUTHORIZING:
  656. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  657. {
  658. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0))
  659. result = PASS;
  660. }
  661. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  662. {
  663. if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0))
  664. result = PASS;
  665. }
  666. break;
  667. case SYS_MODE_CHARGING:
  668. case SYS_MODE_TERMINATING:
  669. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  670. {
  671. /*
  672. DEBUG_INFO("==========================================\n");
  673. DEBUG_INFO("=== OCPP GETTING AUTHORIZE RESULT 1.6 ====\n");
  674. DEBUG_INFO("==========================================\n");
  675. DEBUG_INFO("Authorize.ResponseIdTagInfo.ParentIdTag : %s \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag);
  676. DEBUG_INFO("StartTransaction[%d].ResponseIdTagInfo.ParentIdTag : %s \n", gun_index ,ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag);
  677. */
  678. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) &&
  679. (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag)==0))
  680. result = PASS;
  681. }
  682. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  683. {
  684. /*
  685. DEBUG_INFO("==========================================\n");
  686. DEBUG_INFO("=== OCPP GETTING AUTHORIZE RESULT 2.0 ====\n");
  687. DEBUG_INFO("==========================================\n");
  688. DEBUG_INFO("Authorize.Response_idTokenInfo.groupIdToken.idToken : %s \n", ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken);
  689. DEBUG_INFO("TransactionEvent[%d].Response_idTokenInfo.groupIdToken.idToken : %s \n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.groupIdToken.idToken);
  690. */
  691. if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0) &&
  692. (strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken, (char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.groupIdToken.idToken)==0))
  693. result = PASS;
  694. }
  695. break;
  696. default:
  697. break;
  698. }
  699. }
  700. else
  701. {
  702. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  703. {
  704. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0))
  705. result = PASS;
  706. }
  707. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  708. {
  709. if((strcmp((char*)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted")==0))
  710. result = PASS;
  711. }
  712. }
  713. //DEBUG_INFO("Authorize result : %s \n", ((result == PASS)?"Pass":"Fail"));
  714. return result;
  715. }
  716. uint8_t ocpp_get_unlocker_req(uint8_t gun_index)
  717. {
  718. uint8_t result = OFF;
  719. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  720. {
  721. result = ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor;
  722. }
  723. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  724. {
  725. result = ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor;
  726. }
  727. return result;
  728. }
  729. void ocpp_set_unlocker_req(uint8_t gun_index, uint8_t status)
  730. {
  731. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  732. {
  733. if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor != status)
  734. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = status;
  735. }
  736. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  737. {
  738. if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor != status)
  739. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = status;
  740. }
  741. }
  742. void ocpp_set_starttransaction_req(uint8_t gun_index, uint8_t status)
  743. {
  744. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  745. {
  746. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq != status)
  747. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = status;
  748. }
  749. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  750. {
  751. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
  752. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
  753. }
  754. }
  755. void ocpp_set_starttransaction_conf(uint8_t gun_index, uint8_t status)
  756. {
  757. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  758. {
  759. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf != status)
  760. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = status;
  761. }
  762. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  763. {
  764. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
  765. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
  766. }
  767. }
  768. void ocpp_set_stoptransaction_req(uint8_t gun_index, uint8_t status)
  769. {
  770. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  771. {
  772. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq != status)
  773. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = status;
  774. }
  775. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  776. {
  777. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
  778. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
  779. }
  780. }
  781. void ocpp_set_stoptransaction_conf(uint8_t gun_index, uint8_t status)
  782. {
  783. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  784. {
  785. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf != status)
  786. {
  787. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = status;
  788. }
  789. }
  790. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  791. {
  792. if(ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
  793. {
  794. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
  795. }
  796. }
  797. }
  798. void ocpp_copy_userid_to_starttransaction(uint8_t gun_index)
  799. {
  800. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  801. {
  802. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  803. }
  804. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  805. {
  806. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  807. }
  808. }
  809. uint8_t ocpp_get_starttransaction_result(uint8_t gun_index)
  810. {
  811. uint8_t result = PASS;
  812. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  813. {
  814. if(strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  815. {
  816. if((strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Blocked")==0) ||
  817. (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Expired")==0) ||
  818. (strcmp((char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid")==0))
  819. result = NO;
  820. }
  821. }
  822. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  823. {
  824. if(strstr((char*)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE"))
  825. {
  826. if((strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Blocked")==0) ||
  827. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Expired")==0) ||
  828. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Invalid")==0) ||
  829. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NoCredit")==0) ||
  830. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAllowedTypeEVSE")==0) ||
  831. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisLocation")==0) ||
  832. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisTime")==0) ||
  833. (strcmp((char*)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Unknown")==0))
  834. result = NO;
  835. }
  836. }
  837. return result;
  838. }
  839. uint8_t ocpp_get_maxcharging_profileId()
  840. {
  841. uint8_t result = 0;
  842. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  843. {
  844. result = ShmOCPP16Data->MaxChargingProfile.ChargingProfileId;
  845. }
  846. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  847. {
  848. result = ShmOCPP20Data->MaxChargingProfile.id;
  849. }
  850. return result;
  851. }
  852. uint8_t ocpp_get_smartcharging_profileId(uint8_t gun_index)
  853. {
  854. uint8_t result = 0;
  855. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  856. {
  857. result = ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId;
  858. }
  859. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  860. {
  861. result = ShmOCPP20Data->SmartChargingProfile[gun_index].id;
  862. }
  863. return result;
  864. }
  865. void ocpp_reset_smartcharging_profileId(uint8_t gun_index)
  866. {
  867. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  868. {
  869. ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId = 0;
  870. }
  871. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  872. {
  873. ShmOCPP20Data->SmartChargingProfile[gun_index].id = 0;
  874. }
  875. }
  876. uint8_t ocpp_get_profile_req(uint8_t gun_index)
  877. {
  878. uint8_t result = OFF;
  879. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  880. {
  881. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq;
  882. }
  883. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  884. {
  885. result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq;
  886. }
  887. return result;
  888. }
  889. void ocpp_set_profile_req(uint8_t gun_index, uint8_t status)
  890. {
  891. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  892. {
  893. if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
  894. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
  895. }
  896. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  897. {
  898. if(ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
  899. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
  900. }
  901. }
  902. uint8_t ocpp_get_profile_conf(uint8_t gun_index)
  903. {
  904. uint8_t result = OFF;
  905. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  906. {
  907. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf;
  908. }
  909. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  910. {
  911. result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf;
  912. }
  913. return result;
  914. }
  915. void ocpp_set_profile_conf(uint8_t gun_index, uint8_t status)
  916. {
  917. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  918. {
  919. if(ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
  920. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
  921. }
  922. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  923. {
  924. if(ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
  925. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
  926. }
  927. }
  928. uint8_t ocpp_get_StopTransactionOnEVSideDisconnect()
  929. {
  930. uint8_t result = OFF;
  931. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  932. {
  933. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)
  934. result = ON;
  935. }
  936. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  937. {
  938. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnEVSideDisconnect].variableAttribute[0].value, "TRUE") == 0)
  939. result = ON;
  940. }
  941. return result;
  942. }
  943. uint8_t ocpp_get_cancelreservation_req(uint8_t gun_index)
  944. {
  945. uint8_t result = OFF;
  946. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  947. {
  948. result = ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq;
  949. }
  950. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  951. {
  952. result = ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq;
  953. }
  954. return result;
  955. }
  956. void ocpp_set_cancelreservation_req(uint8_t gun_index, uint8_t status)
  957. {
  958. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  959. {
  960. if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq != status)
  961. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = status;
  962. if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
  963. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  964. }
  965. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  966. {
  967. if(ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq != status)
  968. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = status;
  969. if(ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
  970. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  971. }
  972. }
  973. uint8_t ocpp_compare_reserve_id_with_user(uint8_t gun_index)
  974. {
  975. uint8_t result = OFF;
  976. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  977. {
  978. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  979. result = ON;
  980. }
  981. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  982. {
  983. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
  984. result = ON;
  985. }
  986. return result;
  987. }
  988. uint8_t ocpp_compare_reserve_id_with_remote_user(uint8_t gun_index)
  989. {
  990. uint8_t result = OFF;
  991. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  992. {
  993. if(strcmp((char*)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  994. {
  995. result = ON;
  996. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  997. }
  998. }
  999. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1000. {
  1001. if(strcmp((char*)ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, (char*)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
  1002. {
  1003. result = ON;
  1004. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  1005. }
  1006. }
  1007. return result;
  1008. }
  1009. uint8_t ocpp_isAuthorizeRemoteStart()
  1010. {
  1011. uint8_t result = NO;
  1012. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1013. {
  1014. if(strstr((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE"))
  1015. {
  1016. result = YES;
  1017. }
  1018. }
  1019. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1020. {
  1021. if(strstr((char*)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE"))
  1022. {
  1023. result = YES;
  1024. }
  1025. }
  1026. return result;
  1027. }
  1028. uint8_t ocpp_get_freevend_idtag(uint8_t * userId)
  1029. {
  1030. uint8_t result = NO;
  1031. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1032. {
  1033. strcpy((char*)userId, (char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData);
  1034. result = YES;
  1035. }
  1036. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1037. {
  1038. strcpy((char*)userId, (char*)ShmOCPP20Data->ControllerComponentVariable[ChargingStation_FreeVendIdtag].variableAttribute[0].value);
  1039. result = YES;
  1040. }
  1041. return result;
  1042. }
  1043. //======================================================
  1044. // Check interface status
  1045. //======================================================
  1046. int isInterfaceUp(const char *interface)
  1047. {
  1048. int result = FAIL;
  1049. FILE *fp;
  1050. char cmd[256];
  1051. char buf[512];
  1052. strcpy(cmd, "ifconfig");;
  1053. fp = popen(cmd, "r");
  1054. if(fp != NULL)
  1055. {
  1056. while(fgets(buf, sizeof(buf), fp) != NULL)
  1057. {
  1058. if(strstr(buf, interface) > 0)
  1059. {
  1060. result = PASS;
  1061. }
  1062. }
  1063. }
  1064. pclose(fp);
  1065. return result;
  1066. }
  1067. //======================================================
  1068. // Create all share memory
  1069. //======================================================
  1070. int CreatShareMemory()
  1071. {
  1072. int result = PASS;
  1073. int MeterSMId;
  1074. //creat ShmSysConfigAndInfo
  1075. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  1076. {
  1077. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1078. result = FAIL;
  1079. }
  1080. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1081. {
  1082. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  1083. result = FAIL;
  1084. }
  1085. memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
  1086. //creat ShmStatusCodeData
  1087. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  1088. {
  1089. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1090. result = FAIL;
  1091. }
  1092. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1093. {
  1094. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1095. result = FAIL;
  1096. }
  1097. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  1098. //creat ShmPsuData
  1099. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  1100. {
  1101. DEBUG_ERROR("shmget ShmPsuData NG\n");
  1102. result = FAIL;
  1103. }
  1104. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1105. {
  1106. DEBUG_ERROR("shmat ShmPsuData NG\n");
  1107. result = FAIL;
  1108. }
  1109. memset(ShmPsuData,0,sizeof(struct PsuData));
  1110. //creat ShmCHAdeMOData
  1111. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  1112. {
  1113. DEBUG_ERROR("shmget ShmCHAdeMOData NG1\n");
  1114. result = FAIL;
  1115. }
  1116. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1117. {
  1118. DEBUG_ERROR("shmat ShmCHAdeMOData NG2\n");
  1119. result = FAIL;
  1120. }
  1121. memset(ShmCHAdeMOData,0,sizeof(struct CHAdeMOData));
  1122. //creat ShmCcsData
  1123. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  1124. {
  1125. DEBUG_ERROR("shmget ShmCcsData NG\n");
  1126. result = FAIL;
  1127. }
  1128. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1129. {
  1130. DEBUG_ERROR("shmat ShmCcsData NG\n");
  1131. result = FAIL;
  1132. }
  1133. memset(ShmCcsData,0,sizeof(struct CcsData));
  1134. //creat ShmPrimaryMcuData
  1135. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  1136. {
  1137. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1138. result = FAIL;
  1139. }
  1140. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1141. {
  1142. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1143. result = FAIL;
  1144. }
  1145. memset(ShmPrimaryMcuData,0,sizeof(struct PrimaryMcuData));
  1146. /*
  1147. //creat ShmFanModuleData
  1148. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  1149. {
  1150. DEBUG_ERROR("shmget ShmFanModuleData NG\n");
  1151. result = FAIL;
  1152. }
  1153. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1154. {
  1155. DEBUG_ERROR("shmat ShmFanModuleData NG\n");
  1156. result = FAIL;
  1157. }
  1158. memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
  1159. //creat ShmRelayModuleData
  1160. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  1161. {
  1162. DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
  1163. result = FAIL;
  1164. }
  1165. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1166. {
  1167. DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
  1168. result = FAIL;
  1169. }
  1170. memset(ShmRelayModuleData,0,sizeof(struct RelayModuleData));*/
  1171. //creat ShmOCPP16Data
  1172. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  1173. {
  1174. DEBUG_ERROR("shmget ShmOCPP16Data NG\n");
  1175. result = FAIL;
  1176. }
  1177. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1178. {
  1179. DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
  1180. result = FAIL;
  1181. }
  1182. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  1183. //creat ShmOCPP20Data
  1184. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  1185. {
  1186. DEBUG_ERROR("shmget OCPP20Data NG\n");
  1187. result = FAIL;
  1188. }
  1189. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1190. {
  1191. DEBUG_ERROR("shmat OCPP20Data NG\n");
  1192. result = FAIL;
  1193. }
  1194. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  1195. //creat ShmCharger
  1196. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
  1197. {
  1198. DEBUG_ERROR("shmget ShmCharger NG\n");
  1199. result = FAIL;
  1200. }
  1201. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1202. {
  1203. DEBUG_ERROR("shmat ShmCharger NG\n");
  1204. result = FAIL;
  1205. }
  1206. memset(ShmCharger,0,sizeof(struct Charger));
  1207. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  1208. return result;
  1209. }
  1210. //======================================================
  1211. // Call WEBAPI to mail receipt (Only for CDFA verify)
  1212. //======================================================
  1213. #ifdef CDFA_CERTIFICATE
  1214. int sendReceiptMail(char *startDateTime, char *stopDateTime, char *receiptInfo)
  1215. {
  1216. int result = FAIL;
  1217. char reciever[128];
  1218. char subject[128];
  1219. char content[8192];
  1220. char priceDetail[4098]={0};
  1221. char cmdBuf[8192];
  1222. double totalEnergy;
  1223. double totalPrice;
  1224. json_object *detailPrice = json_tokener_parse(receiptInfo);
  1225. if(!is_error(detailPrice))
  1226. {
  1227. if(json_object_object_get(detailPrice, "totalChargedEnergy") != NULL)
  1228. totalEnergy = json_object_get_double(json_object_object_get(detailPrice, "totalChargedEnergy"));
  1229. if(json_object_object_get(detailPrice, "totalPrice") != NULL)
  1230. totalPrice = json_object_get_double(json_object_object_get(detailPrice, "totalPrice"));
  1231. if(json_object_object_get(detailPrice, "receiptDetail") != NULL)
  1232. {
  1233. for(int idx=0;idx<json_object_array_length(json_object_object_get(detailPrice, "receiptDetail"));idx++)
  1234. {
  1235. sprintf(priceDetail, "%s---%02d:00, %.4f KWH @ $ %.2f = $ %.2f<br/>\n", priceDetail
  1236. , json_object_get_int(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "hour"))
  1237. , json_object_get_double(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "chargedEnergy"))
  1238. , json_object_get_double(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "unitPrice"))
  1239. , json_object_get_double(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "periodPrice")));
  1240. }
  1241. }
  1242. }
  1243. json_object_put(detailPrice);
  1244. // Generate mail content
  1245. sprintf(reciever, "folus_wen@phihong.com.tw");
  1246. sprintf(subject, "Charging Receipt From %s", ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  1247. sprintf(content, "<img src=\"https://www.phihong.com/images/logo.png\"/><br/>\n"
  1248. "<h1>Receipt of charging on %s</h1><br/>\n"
  1249. "<b>Location Name:</b> CDFA Lab<br/>\n"
  1250. "<b>Location Address:</b> xxxxxxxxxxx<br/>\n"
  1251. "<b>EVSE ID:</b> %s<br/>\n"
  1252. "<b>Maximum Rate of Energy Transfer:</b> 11.5 KW AC<br/>\n"
  1253. "<b>Charging start date time:</b> %s<br/>\n"
  1254. "<b>Charging stop date time:</b> %s<br/>\n"
  1255. "<b>Energy Delivered:</b> %.4f KWH<br/>\n"
  1256. "<b>Cost of Charging:</b><br/>\n"
  1257. "%s"
  1258. "***Total charged cost: $ %.2f<br/>\n"
  1259. "<br/>\n"
  1260. "<br/>\n"
  1261. "%s<br/>\n"
  1262. "47800 Fremont Blvd.<br/>\n"
  1263. "Fremont, CA 94538, USA<br/>\n", startDateTime
  1264. , ShmSysConfigAndInfo->SysConfig.ChargeBoxId
  1265. , startDateTime
  1266. , stopDateTime
  1267. , totalEnergy
  1268. , priceDetail
  1269. , totalPrice
  1270. , ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  1271. json_object *post = json_object_new_object();
  1272. json_object_object_add(post, "Receiver", json_object_new_string(reciever));
  1273. json_object_object_add(post, "Subject", json_object_new_string(subject));
  1274. json_object_object_add(post, "Body", json_object_new_string(content));
  1275. json_object_object_add(post, "UseHtml", json_object_new_boolean(1));
  1276. // Call WEBAPI
  1277. sprintf(cmdBuf, "curl -X POST -k -d '%s' -H \"Content-Type: application/json\" https://ocpp.phihong.com.tw:5014/api/Notification/ &", json_object_to_json_string_ext(post, JSON_C_TO_STRING_PLAIN));
  1278. system(cmdBuf);
  1279. //DEBUG_INFO("cmdBuf: %s\n", cmdBuf);
  1280. json_object_put(post);
  1281. return result;
  1282. }
  1283. #endif /* CDFA_CERTIFICATE */
  1284. //======================================================
  1285. // SQLite3 related routine
  1286. //======================================================
  1287. int getReceiptInfo(uint8_t gun_index, char *receiptInfo)
  1288. {
  1289. int result = PASS;
  1290. double totalEnergy=0.0;
  1291. double totalPrice=0.0;
  1292. json_object *receipt = json_object_new_object();
  1293. json_object *receiptAry = json_object_new_array();
  1294. for(uint8_t idxHour=0;idxHour<ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod);idxHour++)
  1295. {
  1296. json_object *receiptDetail = json_object_new_object();
  1297. json_object_object_add(receiptDetail, "hour", json_object_new_int(idxHour));
  1298. json_object_object_add(receiptDetail, "chargedEnergy", json_object_new_double(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idxHour]));
  1299. json_object_object_add(receiptDetail, "unitPrice", json_object_new_double(ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour]));
  1300. json_object_object_add(receiptDetail, "periodPrice", json_object_new_double(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idxHour]*ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour]));
  1301. totalEnergy += ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idxHour];
  1302. totalPrice += ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idxHour]*ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour];
  1303. json_object_array_add(receiptAry, receiptDetail);
  1304. }
  1305. json_object_object_add(receipt, "receiptDetail", receiptAry);
  1306. json_object_object_add(receipt, "totalChargedEnergy", json_object_new_double(totalEnergy));
  1307. json_object_object_add(receipt, "totalPrice", json_object_new_double(totalPrice));
  1308. json_object_object_add(receipt, "serviceProvider", json_object_new_string((char*)ShmSysConfigAndInfo->SysConfig.chargePointVendor));
  1309. json_object_object_add(receipt, "serviceProviderAddress", json_object_new_string("Address"));
  1310. json_object_object_add(receipt, "serviceProviderTel", json_object_new_string("TEL"));
  1311. json_object_object_add(receipt, "chargerBoxId", json_object_new_string((char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1312. sprintf(receiptInfo, "%s", json_object_to_json_string_ext(receipt, JSON_C_TO_STRING_PLAIN));
  1313. json_object_put(receipt);
  1314. //DEBUG_INFO("receiptInfo: %s\n", receiptInfo);
  1315. return result;
  1316. }
  1317. int DB_Open(sqlite3 *db)
  1318. {
  1319. int result = PASS;
  1320. char* errMsg = NULL;
  1321. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  1322. "idx integer primary key AUTOINCREMENT, "
  1323. "reservationId text, "
  1324. "transactionId text, "
  1325. "startMethod text, "
  1326. "userId text, "
  1327. "dateTimeStart text, "
  1328. "dateTimeStop text,"
  1329. "socStart text, "
  1330. "socStop text, "
  1331. "chargeEnergy text, "
  1332. "stopReason text, "
  1333. "finalCost text"
  1334. ");";
  1335. char* createRecordBufSql="CREATE TABLE IF NOT EXISTS charging_record_buffer("
  1336. "reservationId text, "
  1337. "transactionId text, "
  1338. "startMethod text, "
  1339. "userId text, "
  1340. "dateTimeStart text, "
  1341. "dateTimeStop text,"
  1342. "socStart text, "
  1343. "socStop text, "
  1344. "chargeEnergy text, "
  1345. "stopReason text, "
  1346. "finalCost text"
  1347. ");";
  1348. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  1349. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  1350. "`item` TEXT NOT NULL, "
  1351. "`connector` INTEGER NOT NULL, "
  1352. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  1353. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  1354. if(sqlite3_open(DB_FILE, &db))
  1355. {
  1356. result = FAIL;
  1357. DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
  1358. sqlite3_close(db);
  1359. }
  1360. else
  1361. {
  1362. DEBUG_INFO( "Local charging record database open successfully.\n");
  1363. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  1364. {
  1365. result = FAIL;
  1366. DEBUG_ERROR( "Create local charging record table error message: %s\n", errMsg);
  1367. }
  1368. else
  1369. {
  1370. DEBUG_INFO( "Opened local charging record table successfully\n");
  1371. }
  1372. if (sqlite3_exec(db, createRecordBufSql, 0, 0, &errMsg) != SQLITE_OK)
  1373. {
  1374. result = FAIL;
  1375. DEBUG_ERROR( "Create local charging record buffer table error message: %s\n", errMsg);
  1376. }
  1377. else
  1378. {
  1379. DEBUG_INFO( "Opened local charging record buffer table successfully\n");
  1380. }
  1381. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  1382. {
  1383. result = FAIL;
  1384. DEBUG_ERROR( "Create local config table error message: %s\n", errMsg);
  1385. }
  1386. else
  1387. {
  1388. DEBUG_INFO( "Opened local config table successfully\n");
  1389. }
  1390. sqlite3_close(db);
  1391. }
  1392. return result;
  1393. }
  1394. int DB_Check_Record_Buf(sqlite3 *db, int gun_index)
  1395. {
  1396. int result = PASS;
  1397. char* errMsg = NULL;
  1398. char sqlStr[4096];
  1399. char **rs;
  1400. int rows, cols;
  1401. if(sqlite3_open(DB_FILE, &db))
  1402. {
  1403. result = FAIL;
  1404. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1405. sqlite3_close(db);
  1406. }
  1407. else
  1408. {
  1409. sqlite3_get_table(db, "select * from charging_record_buffer", &rs, &rows, &cols, &errMsg);
  1410. if(rows>0)
  1411. {
  1412. // Copy record buffer
  1413. for(int idxRow=1;idxRow<=rows;idxRow++)
  1414. {
  1415. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  1416. {
  1417. result = false;
  1418. }
  1419. sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1420. "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
  1421. rs[(idxRow*cols)+0],
  1422. rs[(idxRow*cols)+1],
  1423. rs[(idxRow*cols)+2],
  1424. rs[(idxRow*cols)+3],
  1425. rs[(idxRow*cols)+4],
  1426. rs[(idxRow*cols)+5],
  1427. rs[(idxRow*cols)+6],
  1428. rs[(idxRow*cols)+7],
  1429. rs[(idxRow*cols)+8],
  1430. rs[(idxRow*cols)+9],
  1431. rs[(idxRow*cols)+10]);
  1432. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1433. {
  1434. result = FAIL;
  1435. DEBUG_INFO( "Copy local charging record buffer error message: %s\n", errMsg);
  1436. }
  1437. else
  1438. {
  1439. DEBUG_INFO( "Copy local charging record buffer successfully\n");
  1440. }
  1441. #ifdef CDFA_CERTIFICATE
  1442. // Only for CDFA certificate
  1443. sendReceiptMail(rs[(idxRow*cols)+4],
  1444. rs[(idxRow*cols)+5],
  1445. rs[(idxRow*cols)+10]);
  1446. #endif /* CDFA_CERTIFICATE */
  1447. }
  1448. // Delete buffer
  1449. if (sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
  1450. {
  1451. result = FAIL;
  1452. DEBUG_INFO( "Delete local charging record buffer error message: %s\n", errMsg);
  1453. }
  1454. else
  1455. {
  1456. DEBUG_INFO( "Delete local charging record buffer successfully\n");
  1457. }
  1458. }
  1459. else
  1460. {
  1461. DEBUG_INFO("There is not any charging record buffer.\n", gun_index);
  1462. }
  1463. sqlite3_free_table(rs);
  1464. sqlite3_close(db);
  1465. }
  1466. return result;
  1467. }
  1468. int DB_Update_Record_Buf(sqlite3 *db, int gun_index)
  1469. {
  1470. int result = PASS;
  1471. char* errMsg = NULL;
  1472. char sqlStr[4096];
  1473. char receiptInfo[2048];
  1474. getReceiptInfo(gun_index, receiptInfo);
  1475. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1476. {
  1477. sprintf(sqlStr, "insert into charging_record_buffer(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1478. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1479. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1480. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  1481. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1482. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1483. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1484. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1485. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1486. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1487. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1488. "Power Loss",
  1489. #ifdef CDFA_CERTIFICATE
  1490. receiptInfo);// Only for CDFA certificate
  1491. #else
  1492. ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  1493. #endif /* CDFA_CERTIFICATE */
  1494. }
  1495. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1496. {
  1497. sprintf(sqlStr, "insert into charging_record_buffer(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1498. #ifdef CDFA_CERTIFICATE
  1499. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1500. #else
  1501. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%f');",
  1502. #endif /* CDFA_CERTIFICATE */
  1503. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1504. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  1505. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1506. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1507. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1508. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1509. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1510. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1511. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1512. "Power Loss",
  1513. #ifdef CDFA_CERTIFICATE
  1514. //receiptInfo);// Only for CDFA certificate
  1515. #else
  1516. ShmOCPP20Data->CostUpdated.totalCost);
  1517. #endif /* CDFA_CERTIFICATE */
  1518. }
  1519. if(sqlite3_open(DB_FILE, &db))
  1520. {
  1521. result = FAIL;
  1522. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1523. sqlite3_close(db);
  1524. }
  1525. else
  1526. {
  1527. // Delete buffer
  1528. if (sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
  1529. {
  1530. result = FAIL;
  1531. DEBUG_INFO( "Delete local charging record buffer error message: %s\n", errMsg);
  1532. }
  1533. else
  1534. {
  1535. //DEBUG_INFO( "Delete local charging record buffer successfully\n");
  1536. }
  1537. // Insert record buffer
  1538. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1539. {
  1540. result = FAIL;
  1541. DEBUG_INFO( "Insert local charging record buffer error message: %s\n", errMsg);
  1542. }
  1543. else
  1544. {
  1545. //DEBUG_INFO( "Insert local charging record buffer successfully\n");
  1546. }
  1547. sqlite3_close(db);
  1548. }
  1549. return result;
  1550. }
  1551. int DB_Insert_Record(sqlite3 *db, int gun_index)
  1552. {
  1553. int result = PASS;
  1554. char* errMsg = NULL;
  1555. char sqlStr[4096];
  1556. char receiptInfo[2048];
  1557. getReceiptInfo(gun_index, receiptInfo);
  1558. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1559. {
  1560. sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1561. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1562. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1563. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  1564. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1565. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1566. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1567. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1568. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1569. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1570. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1571. ShmOCPP16Data->StopTransaction[gun_index].StopReason,
  1572. #ifdef CDFA_CERTIFICATE
  1573. receiptInfo);// Only for CDFA certificate
  1574. #else
  1575. ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  1576. #endif /* CDFA_CERTIFICATE */
  1577. }
  1578. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1579. {
  1580. sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1581. #ifdef CDFA_CERTIFICATE
  1582. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1583. #else
  1584. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%f');",
  1585. #endif /* CDFA_CERTIFICATE */
  1586. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  1587. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  1588. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  1589. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  1590. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1591. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1592. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1593. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  1594. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  1595. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason,
  1596. #ifdef CDFA_CERTIFICATE
  1597. receiptInfo);// Only for CDFA certificate
  1598. #else
  1599. ShmOCPP20Data->CostUpdated.totalCost);
  1600. #endif /* CDFA_CERTIFICATE */
  1601. }
  1602. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1603. if(sqlite3_open(DB_FILE, &db))
  1604. {
  1605. result = FAIL;
  1606. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1607. sqlite3_close(db);
  1608. }
  1609. else
  1610. {
  1611. // Delete buffer
  1612. if (sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
  1613. {
  1614. result = FAIL;
  1615. DEBUG_INFO( "Delete local charging record buffer error message: %s\n", errMsg);
  1616. }
  1617. else
  1618. {
  1619. DEBUG_INFO( "Delete local charging record buffer successfully\n");
  1620. }
  1621. // Insert charging record
  1622. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1623. {
  1624. result = FAIL;
  1625. DEBUG_INFO( "Insert local charging record error message: %s\n", errMsg);
  1626. }
  1627. else
  1628. {
  1629. DEBUG_INFO( "Insert local charging record successfully\n");
  1630. }
  1631. sprintf(sqlStr, "delete from charging_record where (idx < (select idx from charging_record order by idx desc limit 1)-2000) and (dateTimeStop < '%04d.01.01 00:00:00');", (getCurrentYear()-3));
  1632. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1633. {
  1634. result = FAIL;
  1635. DEBUG_INFO( "delete local charging error message: %s\n", errMsg);
  1636. }
  1637. else
  1638. {
  1639. DEBUG_INFO( "delete local charging record successfully\n");
  1640. }
  1641. sqlite3_close(db);
  1642. }
  1643. #ifdef CDFA_CERTIFICATE
  1644. // Only for CDFA certificate
  1645. sendReceiptMail((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  1646. (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  1647. receiptInfo);
  1648. #endif
  1649. return result;
  1650. }
  1651. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t isOperactive)
  1652. {
  1653. uint8_t result = false;
  1654. char* errMsg = NULL;
  1655. char sqlStr[1024];
  1656. srand(time(NULL));
  1657. if(sqlite3_open(DB_FILE, &db))
  1658. {
  1659. result = FAIL;
  1660. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1661. sqlite3_close(db);
  1662. }
  1663. else
  1664. {
  1665. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('isOperactive', %d, %d);", gun_index, isOperactive);
  1666. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1667. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1668. {
  1669. result = FAIL;
  1670. DEBUG_INFO( "update config error message: %s\n", errMsg);
  1671. }
  1672. else
  1673. {
  1674. DEBUG_INFO("update connector-%d config item isOperactive to %d\n", gun_index, isOperactive);
  1675. }
  1676. sqlite3_close(db);
  1677. }
  1678. return result;
  1679. }
  1680. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  1681. {
  1682. uint8_t result = true;
  1683. char* errMsg = NULL;
  1684. char sqlStr[1024];
  1685. char **rs;
  1686. int rows, cols;
  1687. sprintf(sqlStr, "select * from config where item='isOperactive' and connector=%d;", gun_index);
  1688. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1689. if(sqlite3_open(DB_FILE, &db))
  1690. {
  1691. result = FAIL;
  1692. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1693. sqlite3_close(db);
  1694. }
  1695. else
  1696. {
  1697. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1698. if(rows>0)
  1699. {
  1700. for(int idxRow=1;idxRow<=rows;idxRow++)
  1701. {
  1702. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  1703. {
  1704. result = false;
  1705. }
  1706. DEBUG_INFO("Query connector-%d isOperactive: %s\n", gun_index, rs[(idxRow*cols)+3]);
  1707. }
  1708. }
  1709. else
  1710. {
  1711. DEBUG_INFO("Query connector-%d fail, default value as operactive.\n", gun_index);
  1712. }
  1713. sqlite3_free_table(rs);
  1714. sqlite3_close(db);
  1715. }
  1716. return result;
  1717. }
  1718. //======================================================
  1719. // Peripheral initial
  1720. //======================================================
  1721. int InitWatchDog()
  1722. {
  1723. int fd;
  1724. int timeout = 180;
  1725. system("/usr/bin/fuser -k /dev/watchdog");
  1726. sleep(1);
  1727. system("echo V > /dev/watchdog");
  1728. sleep(1);
  1729. fd=open("/dev/watchdog", O_RDWR);
  1730. if(fd<=0)
  1731. {
  1732. DEBUG_ERROR("System watch dog initial fail.\n");
  1733. }
  1734. ioctl(fd, _IOWR('W', 6, int), &timeout);
  1735. return fd;
  1736. }
  1737. void InitGPIO()
  1738. {
  1739. /*****************0~3, 4 bank, bank x 32+ num*********************/
  1740. /***************************************************************/
  1741. /*************** INPUT PIN ***************************************/
  1742. /***************************************************************/
  1743. /***************************************************************/
  1744. /*************** OUTPUT PIN ************************************/
  1745. /***************************************************************/
  1746. /*MCU request:GPIO3_20 => H:ON; L:OFF*/
  1747. system("echo 116 > /sys/class/gpio/export");
  1748. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  1749. system("echo 0 > /sys/class/gpio/gpio116/value");
  1750. /*Rfid:GPIO0_19 => Reset_PING H:ON; L:OFF*/
  1751. system("echo 19 > /sys/class/gpio/export");
  1752. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  1753. system("echo 1 > /sys/class/gpio/gpio19/value");
  1754. /*Speaker:GPIO2_1 => H:ON; L:OFF*/
  1755. system("echo 65 > /sys/class/gpio/export");
  1756. system("echo \"out\" > /sys/class/gpio/gpio65/direction");
  1757. system("echo 0 > /sys/class/gpio/gpio65/value");
  1758. /*Ethernet RST:GPIO1_24 => H:ON; L:OFF*/
  1759. system("echo 56 > /sys/class/gpio/export");
  1760. system("echo \"out\" > /sys/class/gpio/gpio56/direction");
  1761. system("echo 0 > /sys/class/gpio/gpio56/value");
  1762. sleep(3);
  1763. system("echo 1 > /sys/class/gpio/gpio56/value");
  1764. /*4G POWER RST:GPIO3_18 => H:ON; L:OFF*/
  1765. system("echo 114 > /sys/class/gpio/export");
  1766. system("echo \"out\" > /sys/class/gpio/gpio114/direction");
  1767. system("echo 1 > /sys/class/gpio/gpio114/value");
  1768. sleep(3);
  1769. system("echo 0 > /sys/class/gpio/gpio114/value");
  1770. /*4G/WIFI POWER RST:GPIO1_27 => H:OFF L:ON*/
  1771. system("echo 59 > /sys/class/gpio/export");
  1772. system("echo \"out\" > /sys/class/gpio/gpio59/direction");
  1773. system("echo 1 > /sys/class/gpio/gpio59/value");
  1774. sleep(3);
  1775. system("echo 0 > /sys/class/gpio/gpio59/value");
  1776. /*RFID ICC:GPIO0_20 => H:ON; L:OFF*/
  1777. system("echo 20 > /sys/class/gpio/export");
  1778. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  1779. /*QCA7000 interrupt:GPIO2_00 => H:ON; L:OFF*/
  1780. system("echo 64 > /sys/class/gpio/export");
  1781. system("echo \"in\" > /sys/class/gpio/gpio64/direction");
  1782. /*QCA7000 Reset:GPIO3_19 => H:ON; L:OFF*/
  1783. system("echo 115 > /sys/class/gpio/export");
  1784. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  1785. system("echo 0 > /sys/class/gpio/gpio115/value");
  1786. sleep(3);
  1787. system("echo 1 > /sys/class/gpio/gpio115/value");
  1788. /*RFID RST: GPIO1_30 => H:OFF; L:ON*/
  1789. system("echo 62 > /sys/class/gpio/export");
  1790. system("echo \"out\" > /sys/class/gpio/gpio62/direction");
  1791. system("echo 0 > /sys/class/gpio/gpio62/value");
  1792. /*Wake up button GPIO1_31 => H:OFF; L:ON*/
  1793. system("echo 63 > /sys/class/gpio/export");
  1794. system("echo \"in\" > /sys/class/gpio/gpio64/direction");
  1795. sleep(1);
  1796. DEBUG_INFO("Initial GPIO OK\n");
  1797. }
  1798. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  1799. {
  1800. int fd,wrd;
  1801. unsigned char *buf;
  1802. unsigned int ChkSum,ChkSumOrg;
  1803. if((buf=malloc(MtdBlockSize))==NULL)
  1804. {
  1805. DEBUG_ERROR("malloc buffer NG,rebooting..\n");
  1806. if(ShmStatusCodeData!=NULL)
  1807. {
  1808. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1809. }
  1810. sleep(5);
  1811. system("reboot -f");
  1812. sleep(5);
  1813. system("reboot -f");
  1814. }
  1815. memset(buf, 0, MtdBlockSize);
  1816. //================================================
  1817. // Load configuration from mtdblock10
  1818. //================================================
  1819. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  1820. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1821. if (fd < 0)
  1822. {
  1823. free(buf);
  1824. DEBUG_ERROR("open mtdblock10 NG,rebooting..\n");
  1825. if(ShmStatusCodeData!=NULL)
  1826. {
  1827. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1828. }
  1829. sleep(5);
  1830. system("reboot -f");
  1831. sleep(5);
  1832. system("reboot -f");
  1833. }
  1834. wrd=read(fd, buf, MtdBlockSize);
  1835. close(fd);
  1836. if(wrd<MtdBlockSize)
  1837. {
  1838. free(buf);
  1839. DEBUG_ERROR("read SysConfigData data NG,rebooting..\n");
  1840. if(ShmStatusCodeData!=NULL)
  1841. {
  1842. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1843. }
  1844. sleep(5);
  1845. system("reboot -f");
  1846. sleep(5);
  1847. system("reboot -f");
  1848. }
  1849. ChkSum=0;
  1850. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1851. {
  1852. ChkSum+=buf[wrd];
  1853. }
  1854. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1855. memcpy(&ptr->ModelName,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)),ARRAY_SIZE(ptr->ModelName));
  1856. memcpy(&ptr->SerialNumber,buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)),ARRAY_SIZE(ptr->SerialNumber));
  1857. //================================================
  1858. // Load configuration from mtdblock11
  1859. //================================================
  1860. if(ChkSum!=ChkSumOrg)
  1861. {
  1862. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  1863. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1864. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1865. if (fd < 0)
  1866. {
  1867. free(buf);
  1868. DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\n");
  1869. if(ShmStatusCodeData!=NULL)
  1870. {
  1871. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1872. }
  1873. sleep(5);
  1874. system("reboot -f");
  1875. sleep(5);
  1876. system("reboot -f");
  1877. }
  1878. memset(buf, 0, MtdBlockSize);
  1879. wrd=read(fd, buf,MtdBlockSize);
  1880. close(fd);
  1881. if(wrd<MtdBlockSize)
  1882. {
  1883. free(buf);
  1884. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\n");
  1885. if(ShmStatusCodeData!=NULL)
  1886. {
  1887. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1888. }
  1889. sleep(5);
  1890. system("reboot -f");
  1891. sleep(5);
  1892. system("reboot -f");
  1893. }
  1894. ChkSum=0;
  1895. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1896. {
  1897. ChkSum+=buf[wrd];
  1898. }
  1899. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1900. //================================================
  1901. // Load configuration from mtdblock12 (Factory default)
  1902. //================================================
  1903. if(ChkSum!=ChkSumOrg)
  1904. {
  1905. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\n");
  1906. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  1907. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1908. if (fd < 0)
  1909. {
  1910. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\n");
  1911. free(buf);
  1912. if(ShmStatusCodeData!=NULL)
  1913. {
  1914. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1915. }
  1916. sleep(5);
  1917. system("reboot -f");
  1918. sleep(5);
  1919. system("reboot -f");
  1920. }
  1921. memset(buf, 0, MtdBlockSize);
  1922. wrd=read(fd, buf,MtdBlockSize);
  1923. close(fd);
  1924. if(wrd<MtdBlockSize)
  1925. {
  1926. DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\n");
  1927. free(buf);
  1928. if(ShmStatusCodeData!=NULL)
  1929. {
  1930. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1931. }
  1932. sleep(5);
  1933. system("reboot -f");
  1934. sleep(5);
  1935. system("reboot -f");
  1936. }
  1937. ChkSum=0;
  1938. for(wrd=ARRAY_SIZE(ptr->CsuBootLoadFwRev);wrd<MtdBlockSize-4;wrd++)
  1939. {
  1940. ChkSum+=buf[wrd];
  1941. }
  1942. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  1943. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1944. memcpy(buf+(ARRAY_SIZE(ptr->CsuBootLoadFwRev)+ARRAY_SIZE(ptr->ModelName)+ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1945. if(ChkSum!=ChkSumOrg)
  1946. {
  1947. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\n");
  1948. free(buf);
  1949. system("cd /root;./Module_FactoryConfig -m");
  1950. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1951. system("sync");
  1952. sleep(5);
  1953. system("reboot -f");
  1954. sleep(5);
  1955. system("reboot -f");
  1956. return FAIL;
  1957. }
  1958. }
  1959. }
  1960. //load OK
  1961. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  1962. free(buf);
  1963. system("rm -f /mnt/EvseConfig.bin");
  1964. // SysConfig in flash is empty (0xffffffff)
  1965. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1966. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1967. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1968. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1969. {
  1970. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1971. {
  1972. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1973. }
  1974. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1975. {
  1976. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1977. }
  1978. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1979. {
  1980. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1981. }
  1982. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1983. {
  1984. DEBUG_INFO("Ethernet dhcp config is null.\n");
  1985. }
  1986. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) == 0x00)
  1987. {
  1988. DEBUG_INFO("Model name over length.\n");
  1989. }
  1990. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) == 0x00)
  1991. {
  1992. DEBUG_INFO("Model serial number over length.\n");
  1993. }
  1994. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) == 0x00)
  1995. {
  1996. DEBUG_INFO("SystemId over length.\n");
  1997. }
  1998. system("cd /root;./Module_FactoryConfig -m");
  1999. sleep(3);
  2000. system("/usr/bin/run_evse_restart.sh");
  2001. }
  2002. DEBUG_INFO("Load SysConfigData OK\n");
  2003. ShmCharger->isCcsEnable = OFF;
  2004. for(uint8_t idx=0;idx<3;idx++)
  2005. {
  2006. if(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == '7')
  2007. ShmCharger->isCcsEnable = ON;
  2008. }
  2009. #ifdef ENABLE_CCS
  2010. ShmCharger->isCcsEnable = ON;
  2011. #endif
  2012. DEBUG_INFO("Is CCS Enable: %s \n",((ShmCharger->isCcsEnable == ON)?"YES":"NO"));
  2013. RatedCurrentParsing((char*)ShmSysConfigAndInfo->SysConfig.ModelName, &modelnameInfo);
  2014. return PASS;
  2015. }
  2016. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2017. {
  2018. int result = PASS;
  2019. int fd,wrd;
  2020. unsigned int i,Chk;
  2021. unsigned char *ptr, *BufTmp;
  2022. Chk=0;
  2023. ptr=(unsigned char *)UsrData;
  2024. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  2025. {
  2026. memset(BufTmp,0,MtdBlockSize);
  2027. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  2028. for(i=0;i<MtdBlockSize-4;i++)
  2029. Chk+=*(BufTmp+i);
  2030. memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
  2031. // Output configuration to file.
  2032. fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
  2033. if (fd < 0)
  2034. {
  2035. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  2036. free(BufTmp);
  2037. return 0;
  2038. }
  2039. wrd=write(fd, BufTmp, MtdBlockSize);
  2040. close(fd);
  2041. if(wrd<MtdBlockSize)
  2042. {
  2043. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  2044. free(BufTmp);
  2045. return 0;
  2046. }
  2047. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  2048. DEBUG_INFO("Erase /dev/mtd10.\n");
  2049. runShellCmd("flash_erase /dev/mtd10 0 0");
  2050. DEBUG_INFO("Write /dev/mtd10.\n");
  2051. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  2052. DEBUG_INFO("Erase /dev/mtd11.\n");
  2053. runShellCmd("flash_erase /dev/mtd11 0 0");
  2054. DEBUG_INFO("Write /dev/mtd11.\n");
  2055. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  2056. system("rm -f /mnt/EvseConfig.bin");
  2057. DEBUG_INFO("EvseConfig write to flash OK\n");
  2058. }
  2059. else
  2060. {
  2061. DEBUG_ERROR("alloc BlockSize NG\r\n");
  2062. result = FAIL;
  2063. }
  2064. if(BufTmp!=NULL)
  2065. free(BufTmp);
  2066. return result;
  2067. }
  2068. void InitEthernet()
  2069. {
  2070. pid_t pid;
  2071. uint8_t cnt_pingDNS_Fail;
  2072. char tmpbuf[256];
  2073. unsigned int natInterface = 0;
  2074. //Init Eth0 for internet
  2075. memset(tmpbuf,0,256);
  2076. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  2077. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2078. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2079. DEBUG_INFO("eth0 config as ip: %s, netmask: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2080. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2081. system(tmpbuf);
  2082. memset(tmpbuf,0,256);
  2083. sprintf(tmpbuf,"route add default gw %s eth0 &",
  2084. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2085. system(tmpbuf);
  2086. system("ifconfig lo up &");
  2087. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  2088. //system("/sbin/ethtool -s eth0 speed 10 duplex full autoneg off");
  2089. //Run DHCP client if enabled
  2090. system("killall udhcpc");
  2091. system("rm -rf /etc/resolv.conf");
  2092. system("echo nameserver 8.8.8.8 >> /etc/resolv.conf"); //Google DNS server
  2093. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  2094. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  2095. {
  2096. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  2097. system(tmpbuf);
  2098. }
  2099. // Upgrade system id to /etc/hostname
  2100. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  2101. system(tmpbuf);
  2102. // Ethernet MAC address
  2103. getEth0MacAddress();
  2104. //check internet status
  2105. pid = fork();
  2106. if(pid == 0)
  2107. {
  2108. for(;;)
  2109. {
  2110. if(isRouteFail())
  2111. {
  2112. //DEBUG_ERROR("eth0 not in route, restart eth0.\n");
  2113. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  2114. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  2115. {
  2116. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2117. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  2118. system(tmpbuf);
  2119. }
  2120. else
  2121. {
  2122. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2123. memset(tmpbuf,0,256);
  2124. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  2125. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2126. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2127. system(tmpbuf);
  2128. memset(tmpbuf,0,256);
  2129. sprintf(tmpbuf,"route add default gw %s eth0 &",
  2130. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2131. system(tmpbuf);
  2132. }
  2133. }
  2134. if(isReachableInternet() == PASS)
  2135. {
  2136. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  2137. cnt_pingDNS_Fail = 0;
  2138. }
  2139. else
  2140. {
  2141. if(cnt_pingDNS_Fail >= 3)
  2142. {
  2143. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  2144. if(!ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  2145. {
  2146. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  2147. {
  2148. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2149. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  2150. system(tmpbuf);
  2151. }
  2152. else
  2153. {
  2154. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2155. memset(tmpbuf,0,256);
  2156. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  2157. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2158. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2159. system(tmpbuf);
  2160. memset(tmpbuf,0,256);
  2161. sprintf(tmpbuf,"route add default gw %s eth0 &",
  2162. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2163. system(tmpbuf);
  2164. }
  2165. cnt_pingDNS_Fail = 0;
  2166. }
  2167. }
  2168. else
  2169. {
  2170. cnt_pingDNS_Fail++;
  2171. }
  2172. }
  2173. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet &&
  2174. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
  2175. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi) &&
  2176. (ShmOCPP16Data->OcppConnStatus != PASS))
  2177. {
  2178. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  2179. }
  2180. else
  2181. {
  2182. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  2183. }
  2184. //============================================================
  2185. // Priority for internet 0 : First / 1 : Second / 2: Third
  2186. //============================================================
  2187. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  2188. {
  2189. system("/sbin/ifmetric eth0 0");
  2190. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2191. {
  2192. system("/sbin/ifmetric mlan0 1");
  2193. }
  2194. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2195. {
  2196. system("/sbin/ifmetric ppp0 2");
  2197. }
  2198. if(isKernelSupportNAT() == YES)
  2199. {
  2200. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2201. {
  2202. if(natInterface != 1)
  2203. {
  2204. system("/sbin/iptables -t nat -F");
  2205. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o eth0 -j MASQUERADE");
  2206. natInterface = 1;
  2207. }
  2208. }
  2209. else
  2210. {
  2211. system("/sbin/iptables -t nat -F");
  2212. }
  2213. }
  2214. }
  2215. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  2216. {
  2217. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2218. {
  2219. system("/sbin/ifmetric eth0 1");
  2220. system("/sbin/ifmetric mlan0 0");
  2221. }
  2222. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2223. {
  2224. system("/sbin/ifmetric ppp0 2");
  2225. }
  2226. }
  2227. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  2228. {
  2229. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2230. {
  2231. system("/sbin/ifmetric mlan0 2");
  2232. }
  2233. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2234. {
  2235. system("/sbin/ifmetric eth0 1");
  2236. system("/sbin/ifmetric ppp0 0");
  2237. }
  2238. if(isKernelSupportNAT() == YES)
  2239. {
  2240. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2241. {
  2242. if(natInterface != 2)
  2243. {
  2244. system("/sbin/iptables -t nat -F");
  2245. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o ppp0 -j MASQUERADE");
  2246. natInterface = 2;
  2247. }
  2248. }
  2249. else
  2250. {
  2251. system("/sbin/iptables -t nat -F");
  2252. }
  2253. }
  2254. }
  2255. else
  2256. {
  2257. if(isKernelSupportNAT() == YES)
  2258. {
  2259. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2260. {
  2261. system("/sbin/iptables -t nat -F");
  2262. natInterface = 0;
  2263. }
  2264. }
  2265. }
  2266. // Check is need to reset WIFI/4G module power
  2267. if(!ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  2268. ((system("pidof -s Module_Wifi > /dev/null") != 0) ||
  2269. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) ||
  2270. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 1) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == ON)) ||
  2271. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi == ON))) &&
  2272. ((system("pidof -s Module_4g > /dev/null") != 0) ||
  2273. (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) ||
  2274. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 1) && (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == ON))))
  2275. {
  2276. if(getDiffSecNow(startTime[0][TMR_IDX_RESET_WIFI]) > TIMEOUT_SPEC_RESET_WIFI_MODULE)
  2277. {
  2278. if((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != 0) || (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled != 0))
  2279. {
  2280. DEBUG_WARN("WIFI/4G module reset.\n");
  2281. gpio_set_value(GPIO_OUT_RST_4G_WIFI, ON);
  2282. sleep(1);
  2283. gpio_set_value(GPIO_OUT_RST_4G_WIFI, OFF);
  2284. }
  2285. refreshStartTimer(&startTime[0][TMR_IDX_RESET_WIFI]);
  2286. }
  2287. }
  2288. else
  2289. {
  2290. refreshStartTimer(&startTime[0][TMR_IDX_RESET_WIFI]);
  2291. }
  2292. sleep(5);
  2293. }
  2294. }
  2295. DEBUG_INFO("Initial Ethernet OK\n");
  2296. }
  2297. int SpawnTask(uint8_t gun_index)
  2298. {
  2299. if(gun_index == 0)
  2300. {
  2301. system ("pkill Module_");
  2302. system ("pkill OcppBackend");
  2303. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  2304. {
  2305. system("/root/Module_4g &");
  2306. }
  2307. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2308. {
  2309. system("/root/Module_Wifi &");
  2310. }
  2311. system("/root/Module_EventLogging &");
  2312. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  2313. {
  2314. ocpp_process_start();
  2315. }
  2316. else
  2317. {
  2318. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  2319. ocpp_process_start();
  2320. }
  2321. system ("/root/Module_AlarmDetect &");
  2322. system ("/root/Module_InternalComm &");
  2323. system ("/root/Module_Speaker &");
  2324. system ("/root/Module_ProduceUtils &");
  2325. system ("/root/Module_LcmControl &");
  2326. }
  2327. return PASS;
  2328. }
  2329. int InitQca7000()
  2330. {
  2331. int result = PASS;
  2332. system("/sbin/rmmod qcaspi");
  2333. if(isKernelSupportNAT() == YES)
  2334. system("/sbin/insmod /lib/qcaspi_nat.ko");
  2335. else
  2336. system("/sbin/insmod /lib/qcaspi.ko");
  2337. sleep(2);
  2338. /*+++ vern, for CCS +++*/
  2339. //system("/sbin/ifconfig eth1 192.168.253.11 netmask 255.255.255.0 up");
  2340. system("/sbin/ifconfig eth1 192.168.0.11 netmask 255.255.255.0 up");
  2341. /*--- vern, for CCS ---*/
  2342. sleep(1);
  2343. return result;
  2344. }
  2345. int Initialization(uint8_t gun_index)
  2346. {
  2347. int result = PASS;
  2348. if(gun_index == 0)
  2349. {
  2350. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2351. InitGPIO();
  2352. InitEthernet();
  2353. if(InitQca7000() != PASS)
  2354. {
  2355. DEBUG_ERROR("QCA7000 initial fail.\n");
  2356. result = FAIL;
  2357. }
  2358. if(DB_Open(localDb) != PASS)
  2359. {
  2360. DEBUG_ERROR("Local database initial fail.\n");
  2361. result = FAIL;
  2362. }
  2363. if((rfidFd = InitRfidPort()) == FAIL)
  2364. {
  2365. DEBUG_ERROR("RFID port initial fail.\n");
  2366. result = FAIL;
  2367. }
  2368. if((wtdFd = InitWatchDog()) == FAIL)
  2369. {
  2370. DEBUG_ERROR("Watchdog initial fail.\n");
  2371. result = FAIL;
  2372. }
  2373. if(result == PASS)
  2374. DEBUG_INFO("Initialization OK.\n");
  2375. else
  2376. DEBUG_INFO("Initialization Fail.\n");
  2377. }
  2378. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2379. return result;
  2380. }
  2381. //=====================================================
  2382. // Common routine
  2383. //=====================================================
  2384. char* getSystemModeName(unsigned char mode)
  2385. {
  2386. char* result;
  2387. switch(mode)
  2388. {
  2389. case SYS_MODE_BOOTING:
  2390. result = "booting";
  2391. break;
  2392. case SYS_MODE_IDLE:
  2393. result = "idle";
  2394. break;
  2395. case SYS_MODE_AUTHORIZING:
  2396. result = "authorizing";
  2397. break;
  2398. case SYS_MODE_PREPARING:
  2399. result = "preparing";
  2400. break;
  2401. case SYS_MODE_CHARGING:
  2402. result = "charging";
  2403. break;
  2404. case SYS_MODE_TERMINATING:
  2405. result = "terminating";
  2406. break;
  2407. case SYS_MODE_COMPLETE:
  2408. result = "complete";
  2409. break;
  2410. case SYS_MODE_ALARM:
  2411. result = "alarm";
  2412. break;
  2413. case SYS_MODE_FAULT:
  2414. result = "fault";
  2415. break;
  2416. case SYS_MODE_MAINTAIN:
  2417. result = "maintain";
  2418. break;
  2419. case SYS_MODE_RESERVATION:
  2420. result = "reservation";
  2421. break;
  2422. case SYS_MODE_BOOKING:
  2423. result = "booking";
  2424. break;
  2425. case SYS_MODE_DEBUG:
  2426. result = "debug";
  2427. break;
  2428. case SYS_MODE_UPDATE:
  2429. result = "upgrade";
  2430. break;
  2431. default:
  2432. result = "unknown";
  2433. break;
  2434. }
  2435. return result;
  2436. }
  2437. void setChargerMode(unsigned char gun_index, unsigned char mode)
  2438. {
  2439. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  2440. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  2441. DEBUG_INFO("Gun-%02d mode switch from %s to %s\n", gun_index, getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus), getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus));
  2442. }
  2443. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  2444. {
  2445. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  2446. }
  2447. unsigned char isModeChange(unsigned char gun_index)
  2448. {
  2449. unsigned char result = NO;
  2450. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  2451. {
  2452. result = YES;
  2453. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  2454. }
  2455. return result;
  2456. }
  2457. void gpio_set_value(unsigned int gpio, unsigned int value)
  2458. {
  2459. int fd;
  2460. char buf[256];
  2461. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2462. fd = open(buf, O_WRONLY);
  2463. if (fd < 0)
  2464. {
  2465. DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
  2466. return;
  2467. }
  2468. if (value)
  2469. write(fd, "1", 2);
  2470. else
  2471. write(fd, "0", 2);
  2472. close(fd);
  2473. }
  2474. int gpio_get_value(unsigned int gpio)
  2475. {
  2476. int fd;
  2477. char buf[256];
  2478. char ch;
  2479. int8_t result = FAIL;
  2480. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2481. fd = open(buf, O_RDONLY);
  2482. if (fd < 0)
  2483. {
  2484. DEBUG_ERROR("GPIO-%d get fail\n", gpio);
  2485. return result;
  2486. }
  2487. read(fd, &ch, 1);
  2488. if (ch != '0')
  2489. result = 1;
  2490. else
  2491. result = 0;
  2492. close(fd);
  2493. return result;
  2494. }
  2495. int presentChargedEnergyClear(unsigned char gun_index)
  2496. {
  2497. int result = FAIL;
  2498. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2499. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod)*sizeof(float));
  2500. result = PASS;
  2501. return result;
  2502. }
  2503. float presentChargedEnergyTotal(unsigned char gun_index)
  2504. {
  2505. float result = 0.0f;
  2506. for(int idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod);idx++)
  2507. {
  2508. //result += ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idx];
  2509. result += (((int)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[idx]*10000))/(float)10000);
  2510. }
  2511. return result;
  2512. }
  2513. int presentChargedEnergyUpdate(unsigned char gun_index)
  2514. {
  2515. int result = FAIL;
  2516. time_t CurrentTime;
  2517. struct tm *tm;
  2518. CurrentTime = time(NULL);
  2519. tm=localtime(&CurrentTime);
  2520. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  2521. {
  2522. // Resolution: 0.0001 kwh
  2523. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[tm->tm_hour] += (((float)(ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption - ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start))/10000.0) - presentChargedEnergyTotal(gun_index);
  2524. }
  2525. else
  2526. {
  2527. // Resolution: 0.0001 kwh
  2528. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].presentChargedEnergyPeriod[tm->tm_hour] += ((((float)(ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start))/10000.0) +
  2529. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start))/10000.0) +
  2530. (((float)(ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start))/10000.0))
  2531. - presentChargedEnergyTotal(gun_index);
  2532. }
  2533. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = presentChargedEnergyTotal(gun_index);
  2534. return result;
  2535. }
  2536. //===============================================
  2537. // Get firmware version
  2538. //===============================================
  2539. void get_firmware_version(unsigned char gun_index)
  2540. {
  2541. FILE *fp;
  2542. char cmd[512];
  2543. char buf[512];
  2544. // Get CSU hardware version
  2545. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  2546. // Get CSU boot loader version
  2547. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  2548. // Get CSU kernel version
  2549. sprintf(cmd, "/bin/uname -r");
  2550. fp = popen(cmd, "r");
  2551. if(fp == NULL)
  2552. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  2553. else
  2554. {
  2555. while(fgets(buf, sizeof(buf), fp) != NULL)
  2556. {
  2557. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  2558. }
  2559. }
  2560. // Get MCU firmware version
  2561. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2562. // Get CSU root file system version
  2563. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "B0.62.00.0000.00");
  2564. // Get AC connector type from model name
  2565. for(uint8_t idx=0;idx<3;idx++)
  2566. {
  2567. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  2568. {
  2569. case '1':
  2570. // J1772 Plug
  2571. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  2572. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  2573. break;
  2574. case '2':
  2575. // J1772 Socket
  2576. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  2577. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  2578. break;
  2579. case '3':
  2580. // CE Plug
  2581. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  2582. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  2583. break;
  2584. case '4':
  2585. // CE Socket
  2586. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  2587. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  2588. break;
  2589. case '5':
  2590. // GB Plug
  2591. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  2592. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  2593. break;
  2594. case '6':
  2595. // GB Socket
  2596. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  2597. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  2598. break;
  2599. }
  2600. }
  2601. // Get network option from model name
  2602. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  2603. {
  2604. case 'B':
  2605. case 'U':
  2606. //Blue tooth
  2607. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  2608. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  2609. break;
  2610. case 'W':
  2611. // WIFI
  2612. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  2613. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  2614. break;
  2615. case 'T':
  2616. // 3G/4G
  2617. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  2618. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  2619. break;
  2620. case 'D':
  2621. // WIFI + 4G
  2622. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  2623. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
  2624. break;
  2625. default:
  2626. // LAN
  2627. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  2628. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  2629. break;
  2630. }
  2631. // Get rating power from model name
  2632. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2633. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  2634. // Get vender code from model name
  2635. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2636. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  2637. DEBUG_INFO("========================================\n");
  2638. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  2639. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  2640. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  2641. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  2642. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  2643. DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2644. DEBUG_INFO("========================================\n");
  2645. }
  2646. //===============================================
  2647. // Upgrade firmware
  2648. //===============================================
  2649. int upgrade_check()
  2650. {
  2651. int result = PASS;
  2652. int fd;
  2653. DIR *dir;
  2654. struct dirent *file;
  2655. char cmd[512];
  2656. long int MaxLen=48*1024*1024, ImageLen=0, wrd;
  2657. if ((dir = opendir ("/mnt")) != NULL)
  2658. {
  2659. /* print all the files and directories within directory */
  2660. while ((file = readdir (dir)) != NULL)
  2661. {
  2662. if((strlen(file->d_name)>2))
  2663. {
  2664. // Wait for MCU upgrade finish.
  2665. while(ShmCharger->gun_info[0].mcuFlag.isMcuUpgradeReq || (AC_QUANTITY>1?ShmCharger->gun_info[1].mcuFlag.isMcuUpgradeReq:FALSE))sleep(1);
  2666. // Wait for LCM upgrade finish.
  2667. while(ShmCharger->isUpgradeLcmReq)sleep(1);
  2668. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  2669. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  2670. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  2671. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  2672. {
  2673. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2674. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2675. ImageLen = read(fd, ptr, MaxLen);
  2676. close(fd);
  2677. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  2678. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  2679. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  2680. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  2681. if((ShmCharger->fwUpgradeInfo.modelName[0] == ShmSysConfigAndInfo->SysConfig.ModelName[0]) &&
  2682. (ShmCharger->fwUpgradeInfo.modelName[1] == ShmSysConfigAndInfo->SysConfig.ModelName[1]) &&
  2683. (ShmCharger->fwUpgradeInfo.modelName[7] == ShmSysConfigAndInfo->SysConfig.ModelName[7]) &&
  2684. (ShmCharger->fwUpgradeInfo.modelName[8] == ShmSysConfigAndInfo->SysConfig.ModelName[8]) &&
  2685. (ShmCharger->fwUpgradeInfo.modelName[9] == ShmSysConfigAndInfo->SysConfig.ModelName[9]) &&
  2686. (ShmCharger->fwUpgradeInfo.modelName[11] == ShmSysConfigAndInfo->SysConfig.ModelName[11]) &&
  2687. (ShmCharger->fwUpgradeInfo.modelName[12] == ShmSysConfigAndInfo->SysConfig.ModelName[12]) &&
  2688. (ShmCharger->fwUpgradeInfo.modelName[13] == ShmSysConfigAndInfo->SysConfig.ModelName[13]) &&
  2689. (ShmCharger->fwUpgradeInfo.fwType>0))
  2690. {
  2691. switch(ShmCharger->fwUpgradeInfo.fwType)
  2692. {
  2693. case CSU_MLO:
  2694. case CSU_BOOTLOADER:
  2695. case CSU_KERNEL_CONFIGURATION:
  2696. case CSU_KERNEL_IMAGE:
  2697. case CSU_ROOT_FILE_SYSTEM:
  2698. case CSU_USER_CONFIGURATION:
  2699. case CSU_PRIMARY_CONTROLLER:
  2700. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  2701. {
  2702. result = FAIL;
  2703. }
  2704. else
  2705. {
  2706. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  2707. {
  2708. DEBUG_INFO("Restore model name & serial number.\n");
  2709. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2710. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2711. {
  2712. DEBUG_INFO("Re-load configuration fail.\n");
  2713. result = FAIL;
  2714. }
  2715. else
  2716. {
  2717. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  2718. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  2719. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  2720. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2721. {
  2722. DEBUG_INFO("Re-write configuration fail.\n");
  2723. result = FAIL;
  2724. }
  2725. else
  2726. DEBUG_INFO("Re-write configuration OK.\n");
  2727. }
  2728. }
  2729. }
  2730. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2731. system(cmd);
  2732. break;
  2733. case AC_WALLMOUNT_CONTROLLER:
  2734. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2735. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  2736. sleep(10);
  2737. break;
  2738. case LCM:
  2739. fd = open("/mnt/lcm.zip", O_RDWR | O_CREAT | O_EXCL);
  2740. if (fd < 0)
  2741. {
  2742. DEBUG_ERROR("Can not create lcm.zip file.\n");
  2743. result = FAIL;
  2744. }
  2745. else
  2746. {
  2747. // Write image to flash
  2748. DEBUG_INFO("Writing data to lcm.zip file...\n");
  2749. wrd=write(fd, ptr+48, ImageLen-48);
  2750. close(fd);
  2751. DEBUG_INFO(">> lcm.zip Written length: 0x%x\n", wrd);
  2752. if(wrd != (ImageLen-48))
  2753. {
  2754. result = FAIL;
  2755. }
  2756. else
  2757. {
  2758. runShellCmd("mkdir -p /mnt/lcd");
  2759. runShellCmd("unzip /mnt/lcm.zip -d /mnt/lcd");
  2760. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2761. system(cmd);
  2762. system("rm -f /mnt/lcm.zip");
  2763. result = PASS;
  2764. }
  2765. }
  2766. ShmCharger->isUpgradeLcmReq = ON;
  2767. sleep(10);
  2768. break;
  2769. default:
  2770. result = FAIL;
  2771. DEBUG_WARN("Image file is unknown type.\n");
  2772. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2773. system(cmd);
  2774. break;
  2775. }
  2776. }
  2777. else
  2778. {
  2779. result = FAIL;
  2780. DEBUG_ERROR("Model name and Firmware type error.\n");
  2781. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2782. system(cmd);
  2783. }
  2784. free(ptr);
  2785. }
  2786. else
  2787. {
  2788. result = FAIL;
  2789. DEBUG_ERROR("New firmware open error.\n");
  2790. }
  2791. }
  2792. else
  2793. {
  2794. if(strlen(file->d_name) >= 3)
  2795. {
  2796. result = FAIL;
  2797. DEBUG_ERROR("File name error.\n");
  2798. }
  2799. else
  2800. {
  2801. DEBUG_ERROR("Searching file.\n");
  2802. }
  2803. }
  2804. }
  2805. closedir (dir);
  2806. }
  2807. else
  2808. {
  2809. result = FAIL;
  2810. DEBUG_ERROR("/mnt does not valid.\n");
  2811. }
  2812. return result;
  2813. }
  2814. //===============================================
  2815. // Check RFID is match with start user
  2816. //===============================================
  2817. int isMatchStartUser(unsigned char gun_index)
  2818. {
  2819. uint8_t tmpUser[32];
  2820. uint8_t isPrintLogOut = OFF;
  2821. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2822. {
  2823. // Big endian
  2824. switch(rfid.snType)
  2825. {
  2826. case RFID_SN_TYPE_6BYTE:
  2827. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  2828. break;
  2829. case RFID_SN_TYPE_7BYTE:
  2830. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  2831. break;
  2832. case RFID_SN_TYPE_10BYTE:
  2833. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  2834. break;
  2835. case RFID_SN_TYPE_4BYTE:
  2836. default:
  2837. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2838. break;
  2839. }
  2840. }
  2841. else
  2842. {
  2843. // Little endian
  2844. switch(rfid.snType)
  2845. {
  2846. case RFID_SN_TYPE_6BYTE:
  2847. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2848. break;
  2849. case RFID_SN_TYPE_7BYTE:
  2850. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2851. break;
  2852. case RFID_SN_TYPE_10BYTE:
  2853. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2854. break;
  2855. case RFID_SN_TYPE_4BYTE:
  2856. default:
  2857. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2858. break;
  2859. }
  2860. }
  2861. if(isPrintLogOut == OFF)
  2862. {
  2863. DEBUG_INFO("==== isMatchStartUser ==== \n");
  2864. DEBUG_INFO("tmpUser : %s \n", tmpUser);
  2865. DEBUG_INFO("StartUserId : %s \n", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId);
  2866. DEBUG_INFO("========================== \n");
  2867. isPrintLogOut = ON;
  2868. }
  2869. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  2870. }
  2871. int isMatchPresentUser()
  2872. {
  2873. uint8_t tmpUser[32];
  2874. uint8_t isPrintLogOut = OFF;
  2875. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2876. {
  2877. // Big endian
  2878. switch(rfid.snType)
  2879. {
  2880. case RFID_SN_TYPE_6BYTE:
  2881. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  2882. break;
  2883. case RFID_SN_TYPE_7BYTE:
  2884. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  2885. break;
  2886. case RFID_SN_TYPE_10BYTE:
  2887. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  2888. break;
  2889. case RFID_SN_TYPE_4BYTE:
  2890. default:
  2891. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2892. break;
  2893. }
  2894. }
  2895. else
  2896. {
  2897. // Little endian
  2898. switch(rfid.snType)
  2899. {
  2900. case RFID_SN_TYPE_6BYTE:
  2901. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2902. break;
  2903. case RFID_SN_TYPE_7BYTE:
  2904. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2905. break;
  2906. case RFID_SN_TYPE_10BYTE:
  2907. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2908. break;
  2909. case RFID_SN_TYPE_4BYTE:
  2910. default:
  2911. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2912. break;
  2913. }
  2914. }
  2915. if(isPrintLogOut == OFF)
  2916. {
  2917. DEBUG_INFO("==== isMatchPresentUser ==== \n");
  2918. DEBUG_INFO("tmpUser : %s \n", tmpUser);
  2919. DEBUG_INFO("UserId : %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  2920. DEBUG_INFO("============================ \n");
  2921. isPrintLogOut = ON;
  2922. }
  2923. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysConfig.UserId)==0)?YES:NO);
  2924. }
  2925. //===============================================
  2926. // Read RFID Serial Number
  2927. //===============================================
  2928. int GetCardSerialNumber()
  2929. {
  2930. int isSuccess = FAIL;
  2931. int module_type = MODULE_EWT;
  2932. if(getRequestCardSN(rfidFd,module_type,&rfid))
  2933. {
  2934. if(rfid.cardType == ISO14443A)
  2935. {
  2936. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  2937. {
  2938. isSuccess = PASS;
  2939. }
  2940. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  2941. {
  2942. isSuccess = PASS;
  2943. }
  2944. sethaltCard(rfidFd,module_type);
  2945. }
  2946. else if(rfid.cardType == IS014443B)
  2947. {
  2948. isSuccess = PASS;
  2949. }
  2950. else if(rfid.cardType == FELICA)
  2951. {
  2952. isSuccess = PASS;
  2953. }
  2954. else
  2955. {}
  2956. }
  2957. else
  2958. {}
  2959. return isSuccess;
  2960. }
  2961. //===============================================
  2962. // Set led motion
  2963. //===============================================
  2964. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  2965. {
  2966. switch(led_mode)
  2967. {
  2968. case LED_ACTION_INIT:
  2969. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  2970. break;
  2971. case LED_ACTION_IDLE:
  2972. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  2973. break;
  2974. case LED_ACTION_AUTHED:
  2975. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  2976. break;
  2977. case LED_ACTION_CONNECTED:
  2978. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  2979. break;
  2980. case LED_ACTION_CHARGING:
  2981. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  2982. break;
  2983. case LED_ACTION_STOP:
  2984. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  2985. break;
  2986. case LED_ACTION_ALARM:
  2987. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  2988. break;
  2989. case LED_ACTION_MAINTAIN:
  2990. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  2991. break;
  2992. case LED_ACTION_RFID_PASS:
  2993. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  2994. break;
  2995. case LED_ACTION_RFID_FAIL:
  2996. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  2997. break;
  2998. case LED_ACTION_BLE_CONNECT:
  2999. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  3000. break;
  3001. case LED_ACTION_BLE_DISABLE:
  3002. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  3003. break;
  3004. case LED_ACTION_DEBUG:
  3005. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  3006. break;
  3007. case LED_ACTION_ALL_OFF:
  3008. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  3009. break;
  3010. case LED_RELAY_ON:
  3011. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  3012. break;
  3013. case LED_RELAY_OFF:
  3014. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  3015. break;
  3016. case LED_ACTION_HANDSHAKE_FAIL:
  3017. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  3018. break;
  3019. case LED_ACTION_INTERNET_DISCONNECT:
  3020. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  3021. break;
  3022. case LED_ACTION_RESTORE_SETTING:
  3023. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RESTORE_SETTING;
  3024. break;
  3025. case LED_ACTION_IDLE_BACKEND_CONNECTED:
  3026. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_CONNECTED;
  3027. break;
  3028. case LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP:
  3029. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP;
  3030. break;
  3031. case LED_ACTION_IDLE_BACKEND_DISCONNECTED:
  3032. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_DISCONNECTED;
  3033. break;
  3034. case LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP:
  3035. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP;
  3036. break;
  3037. case LED_ACTION_RESERVATION_MODE:
  3038. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RESERVATION_MODE;
  3039. break;
  3040. }
  3041. }
  3042. //===============================================
  3043. // Request on/off set
  3044. //===============================================
  3045. void setRequest(unsigned char gun_index,unsigned char isOn)
  3046. {
  3047. if(isOn == ON)
  3048. {
  3049. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  3050. {
  3051. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  3052. DEBUG_INFO("Gun-%d permission request: ON \n",gun_index);
  3053. }
  3054. }
  3055. else
  3056. {
  3057. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  3058. {
  3059. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  3060. DEBUG_INFO("Gun-%d permission request: OFF \n",gun_index);
  3061. }
  3062. }
  3063. }
  3064. //===============================================
  3065. // Request on/off get
  3066. //===============================================
  3067. int getRequest(unsigned char gun_index)
  3068. {
  3069. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  3070. }
  3071. //===============================================
  3072. // Relay on/off set
  3073. //===============================================
  3074. void setRelay(unsigned char gun_index,unsigned char isOn)
  3075. {
  3076. if(isOn == ON)
  3077. {
  3078. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  3079. {
  3080. switch(ShmCharger->gun_info[gun_index].chargingMode)
  3081. {
  3082. case CHARGING_MODE_BS:
  3083. case CHARGING_MODE_HLC:
  3084. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3085. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  3086. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  3087. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  3088. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3089. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_BS] \n",gun_index);
  3090. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3091. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_HLC] \n",gun_index);
  3092. break;
  3093. case CHARGING_MODE_SOCKETE:
  3094. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3095. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] = 0x01;
  3096. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_SOCKETE] \n",gun_index);
  3097. break;
  3098. default:
  3099. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3100. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  3101. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  3102. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  3103. DEBUG_INFO("Gun-%d Output relay status: ON. [DEFFAULT] \n",gun_index);
  3104. break;
  3105. }
  3106. }
  3107. }
  3108. else
  3109. {
  3110. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  3111. {
  3112. switch(ShmCharger->gun_info[gun_index].chargingMode)
  3113. {
  3114. case CHARGING_MODE_BS:
  3115. case CHARGING_MODE_HLC:
  3116. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3117. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  3118. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  3119. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  3120. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3121. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_BS] \n",gun_index);
  3122. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3123. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_HLC] \n",gun_index);
  3124. break;
  3125. case CHARGING_MODE_SOCKETE:
  3126. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3127. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] = 0;
  3128. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_SOCKETE] \n",gun_index);
  3129. break;
  3130. default:
  3131. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3132. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  3133. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  3134. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  3135. DEBUG_INFO("Gun-%d Output relay status: OFF. [DEFFAULT] \n",gun_index);
  3136. break;
  3137. }
  3138. }
  3139. }
  3140. }
  3141. //===============================================
  3142. // Relay on/off get
  3143. //===============================================
  3144. int getRelay(unsigned char gun_index)
  3145. {
  3146. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  3147. }
  3148. //===============================================
  3149. // Set speaker on/off request
  3150. //===============================================
  3151. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  3152. {
  3153. if(isOn == ON)
  3154. {
  3155. ShmCharger->isSpeakerOn = ON;
  3156. ShmCharger->speaker_type = speaker_mode;
  3157. }
  3158. }
  3159. //===============================================
  3160. // Initialization RFID communication port
  3161. //===============================================
  3162. int InitRfidPort()
  3163. {
  3164. int uartO2 = open(rfidPortName, O_RDWR);
  3165. struct termios tios;
  3166. if (uartO2 != FAIL)
  3167. {
  3168. ioctl (uartO2, TCGETS, &tios);
  3169. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  3170. tios.c_lflag = 0;
  3171. tios.c_iflag = 0;
  3172. tios.c_oflag = 0;
  3173. tios.c_cc[VMIN] = 0;
  3174. tios.c_cc[VTIME] = (unsigned char) 1;
  3175. tios.c_lflag = 0;
  3176. tcflush(uartO2, TCIFLUSH);
  3177. ioctl(uartO2, TCSETS, &tios);
  3178. }
  3179. if (uartO2 < 0)
  3180. {
  3181. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  3182. }
  3183. return uartO2;
  3184. }
  3185. //===============================================
  3186. // Check internet access status
  3187. //===============================================
  3188. int isReachableInternet()
  3189. {
  3190. int result = FAIL;
  3191. FILE *fp;
  3192. char cmd[256];
  3193. char buf[512];
  3194. char tmp[512];
  3195. // Get ip address & net mask
  3196. strcpy(cmd, "ifconfig eth0");
  3197. fp = popen(cmd, "r");
  3198. if (fp != NULL)
  3199. {
  3200. while(fgets(buf, sizeof(buf), fp) != NULL)
  3201. {
  3202. if (strstr(buf, "inet addr:") > 0)
  3203. {
  3204. sscanf(buf, "%*s%s", tmp);
  3205. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  3206. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != 0)
  3207. {
  3208. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  3209. }
  3210. sscanf(buf, "%*s%*s%*s%s", tmp);
  3211. substr(tmp, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  3212. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress) != 0)
  3213. {
  3214. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp);
  3215. }
  3216. }
  3217. }
  3218. }
  3219. pclose(fp);
  3220. memset(buf, 0x00, sizeof(buf));
  3221. // Get gateway
  3222. fp = popen("ip route", "r");
  3223. if(fp == NULL)
  3224. result = FAIL;
  3225. else
  3226. {
  3227. while(fgets(buf, sizeof(buf), fp) != NULL)
  3228. {
  3229. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  3230. break;
  3231. }
  3232. if(strstr(buf, "default") != NULL)
  3233. {
  3234. sscanf(buf, "%*s%*s%s", tmp);
  3235. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  3236. }
  3237. }
  3238. pclose(fp);
  3239. memset(buf, 0x00, sizeof(buf));
  3240. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  3241. {
  3242. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  3243. fp = popen(cmd, "r");
  3244. if(fp != NULL)
  3245. {
  3246. while(fgets(buf, sizeof(buf), fp) != NULL)
  3247. {
  3248. if(strstr(buf, "transmitted") > 0)
  3249. {
  3250. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  3251. if(strstr(buf,"100%") != NULL)
  3252. {
  3253. }
  3254. else
  3255. {
  3256. result = PASS;
  3257. }
  3258. //DEBUG_INFO("%s",buf);
  3259. //DEBUG_INFO("%s\n",tmp);
  3260. }
  3261. }
  3262. }
  3263. pclose(fp);
  3264. }
  3265. return result;
  3266. }
  3267. int isRouteFail()
  3268. {
  3269. int result = YES;
  3270. FILE *fp;
  3271. char buf[512];
  3272. fp = popen("route -n", "r");
  3273. if(fp != NULL)
  3274. {
  3275. while(fgets(buf, sizeof(buf), fp) != NULL)
  3276. {
  3277. if(strstr(buf, "eth0") != NULL)
  3278. result = NO;
  3279. }
  3280. }
  3281. pclose(fp);
  3282. return result;
  3283. }
  3284. //===============================================
  3285. // Check reservation date is expired
  3286. //===============================================
  3287. int isReservationExpired(unsigned char gun_index)
  3288. {
  3289. int result = NO;
  3290. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3291. {
  3292. if(isOvertNow(ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate))
  3293. result = YES;
  3294. }
  3295. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3296. {
  3297. if(isOvertNow(ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime))
  3298. result = YES;
  3299. }
  3300. return result;
  3301. }
  3302. //===============================================
  3303. // Check charging profile related date routine
  3304. //===============================================
  3305. int isProfileValid(uint8_t gun_index)
  3306. {
  3307. int result = NO;
  3308. struct tm tmFrom, tmTo;
  3309. struct timeb tbFrom, tbTo;
  3310. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3311. {
  3312. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom, "%4d-%2d-%2dT%2d:%2d:%2d", &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6) &&
  3313. (sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo, "%4d-%2d-%2dT%2d:%2d:%2d", &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
  3314. {
  3315. tmFrom.tm_year -= 1900;
  3316. tmFrom.tm_mon -= 1;
  3317. tbFrom.time = mktime(&tmFrom);
  3318. tmTo.tm_year -= 1900;
  3319. tmTo.tm_mon -= 1;
  3320. tbTo.time = mktime(&tmTo);
  3321. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3322. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3323. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  3324. {
  3325. result = YES;
  3326. }
  3327. }
  3328. else
  3329. {
  3330. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3331. }
  3332. }
  3333. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3334. {
  3335. if((sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom, "%4d-%2d-%2dT%2d:%2d:%2d", &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6) &&
  3336. (sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validTo, "%4d-%2d-%2dT%2d:%2d:%2d", &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
  3337. {
  3338. tmFrom.tm_year -= 1900;
  3339. tmFrom.tm_mon -= 1;
  3340. tbFrom.time = mktime(&tmFrom);
  3341. tmTo.tm_year -= 1900;
  3342. tmTo.tm_mon -= 1;
  3343. tbTo.time = mktime(&tmTo);
  3344. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3345. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3346. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  3347. {
  3348. result = YES;
  3349. }
  3350. }
  3351. else
  3352. {
  3353. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3354. }
  3355. }
  3356. return result;
  3357. }
  3358. int getMaxScheduleStart()
  3359. {
  3360. int result = -1;
  3361. struct tm tmScheduleStart;;
  3362. struct timeb tbScheduleStart;
  3363. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3364. {
  3365. if((sscanf((char*)ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule, "%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))
  3366. {
  3367. tmScheduleStart.tm_year -= 1900;
  3368. tmScheduleStart.tm_mon -= 1;
  3369. tbScheduleStart.time = mktime(&tmScheduleStart);
  3370. tbScheduleStart.millitm = 0;
  3371. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3372. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  3373. }
  3374. else
  3375. {
  3376. DEBUG_WARN("Max schedule start date parsing error.\n");
  3377. }
  3378. }
  3379. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3380. {
  3381. if((sscanf((char*)ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule, "%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))
  3382. {
  3383. tmScheduleStart.tm_year -= 1900;
  3384. tmScheduleStart.tm_mon -= 1;
  3385. tbScheduleStart.time = mktime(&tmScheduleStart);
  3386. tbScheduleStart.millitm = 0;
  3387. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3388. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  3389. }
  3390. else
  3391. {
  3392. DEBUG_WARN("Max schedule start date parsing error.\n");
  3393. }
  3394. }
  3395. return result;
  3396. }
  3397. int getScheduleStart(int gun_index)
  3398. {
  3399. int result = -1;
  3400. struct tm tmScheduleStart;;
  3401. struct timeb tbScheduleStart;
  3402. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3403. {
  3404. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule, "%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))
  3405. {
  3406. tmScheduleStart.tm_year -= 1900;
  3407. tmScheduleStart.tm_mon -= 1;
  3408. tbScheduleStart.time = mktime(&tmScheduleStart);
  3409. tbScheduleStart.millitm = 0;
  3410. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3411. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3412. }
  3413. else
  3414. {
  3415. DEBUG_WARN("Schedule start date parsing error.\n");
  3416. }
  3417. }
  3418. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3419. {
  3420. if((sscanf((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule, "%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))
  3421. {
  3422. tmScheduleStart.tm_year -= 1900;
  3423. tmScheduleStart.tm_mon -= 1;
  3424. tbScheduleStart.time = mktime(&tmScheduleStart);
  3425. tbScheduleStart.millitm = 0;
  3426. result = DiffTimebWithNow(tbScheduleStart)/1000;
  3427. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3428. }
  3429. else
  3430. {
  3431. DEBUG_WARN("Schedule start date parsing error.\n");
  3432. }
  3433. }
  3434. return result;
  3435. }
  3436. int getStartSinceToday()
  3437. {
  3438. int result = -1;
  3439. time_t t;
  3440. struct tm *tmStartToday;
  3441. struct timeb tbStartToday;
  3442. t=time(NULL);
  3443. tmStartToday=localtime(&t);
  3444. tmStartToday->tm_hour = 0;
  3445. tmStartToday->tm_min = 0;
  3446. tmStartToday->tm_sec = 0;
  3447. tbStartToday.time = mktime(tmStartToday);
  3448. result = DiffTimebWithNow(tbStartToday)/1000;
  3449. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  3450. return result;
  3451. }
  3452. int getStartSinceWeek()
  3453. {
  3454. int result = -1;
  3455. time_t t;
  3456. struct tm *tmStartWeek;
  3457. struct timeb tbStartWeek;
  3458. t=time(NULL);
  3459. tmStartWeek=localtime(&t);
  3460. t-=86400*tmStartWeek->tm_wday;
  3461. tmStartWeek=localtime(&t);
  3462. tmStartWeek->tm_hour = 0;
  3463. tmStartWeek->tm_min = 0;
  3464. tmStartWeek->tm_sec = 0;
  3465. tbStartWeek.time = mktime(tmStartWeek);
  3466. result = DiffTimebWithNow(tbStartWeek)/1000;
  3467. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  3468. return result;
  3469. }
  3470. //===============================================
  3471. // Valid from local white list
  3472. //===============================================
  3473. int isValidLocalWhiteCard()
  3474. {
  3475. uint8_t result = FAIL;
  3476. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  3477. {
  3478. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  3479. {
  3480. result = PASS;
  3481. }
  3482. }
  3483. return result;
  3484. }
  3485. //==========================================
  3486. // Check routine
  3487. //==========================================
  3488. void checkTask()
  3489. {
  3490. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3491. {
  3492. if(system("pidof -s Module_4g > /dev/null") != 0)
  3493. {
  3494. DEBUG_INFO("Module_4g not running, restart it.\n");
  3495. system("/root/Module_4g &");
  3496. }
  3497. }
  3498. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  3499. {
  3500. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  3501. {
  3502. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  3503. system("/root/Module_Wifi &");
  3504. }
  3505. }
  3506. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  3507. {
  3508. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  3509. system("/root/Module_EventLogging &");
  3510. }
  3511. if((strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0) && !ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing)
  3512. {
  3513. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3514. {
  3515. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  3516. {
  3517. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  3518. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  3519. system("pkill OcppBackend");
  3520. sleep(3);
  3521. ocpp_process_start();
  3522. }
  3523. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3524. {
  3525. DEBUG_INFO("OcppBackend not running, restart it.\n");
  3526. ocpp_process_start();
  3527. }
  3528. }
  3529. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3530. {
  3531. if((time((time_t*)NULL) - ShmOCPP20Data->procDogTime) > 180)
  3532. {
  3533. DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
  3534. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  3535. system("pkill OcppBackend20");
  3536. sleep(3);
  3537. ocpp_process_start();
  3538. }
  3539. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  3540. {
  3541. DEBUG_INFO("OcppBackend20 not running, restart it.\n");
  3542. ocpp_process_start();
  3543. }
  3544. }
  3545. }
  3546. if((strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL,"") != 0) && !ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing)
  3547. {
  3548. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  3549. {
  3550. DEBUG_INFO("OcppBackendPH not running, restart it.\n");
  3551. system("/root/OcppBackendPH &");
  3552. }
  3553. }
  3554. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  3555. {
  3556. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  3557. system("/root/Module_AlarmDetect &");
  3558. }
  3559. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  3560. {
  3561. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  3562. system("/root/Module_InternalComm &");
  3563. }
  3564. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  3565. {
  3566. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  3567. system("/root/Module_Speaker &");
  3568. }
  3569. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  3570. {
  3571. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  3572. system ("/root/Module_ProduceUtils &");
  3573. }
  3574. if(system("pidof -s Module_LcmControl > /dev/null") != 0)
  3575. {
  3576. DEBUG_INFO("Module_LcmControl not running, restart it.\n");
  3577. system ("/root/Module_LcmControl &");
  3578. }
  3579. if((system("pidof -s Module_PowerSharing > /dev/null") != 0) &&
  3580. ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing)
  3581. {
  3582. DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
  3583. system ("/root/Module_PowerSharing &");
  3584. }
  3585. if(ShmCharger->isCcsEnable == ON)
  3586. {
  3587. if((system("pidof -s CsuComm > /dev/null") != 0))
  3588. {
  3589. DEBUG_INFO("CsuComm not running, restart it.\n");
  3590. system ("/root/CsuComm &");
  3591. }
  3592. if((system("pidof -s SeccComm > /dev/null") != 0))
  3593. {
  3594. sleep(3);
  3595. DEBUG_INFO("SeccComm not running, restart it.\n");
  3596. system ("/root/SeccComm &");
  3597. }
  3598. }
  3599. }
  3600. void checkConnectionTimeout()
  3601. {
  3602. if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  3603. {
  3604. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  3605. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING);
  3606. }
  3607. else
  3608. {
  3609. ShmCharger->timeoutSpec.Setting_Timeout_Spec = ocpp_get_connection_timeout();
  3610. if((ShmCharger->timeoutSpec.Setting_Timeout_Spec) < TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  3611. {
  3612. ShmCharger->timeoutSpec.Present_Timeout_Spec = (TIMEOUT_SPEC_BS_HLC_HANDSHAKE+10);
  3613. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  3614. }
  3615. else
  3616. {
  3617. ShmCharger->timeoutSpec.Present_Timeout_Spec = ShmCharger->timeoutSpec.Setting_Timeout_Spec;
  3618. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  3619. }
  3620. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3621. }
  3622. }
  3623. void checkReset()
  3624. {
  3625. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3626. {
  3627. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3628. {
  3629. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3630. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  3631. {
  3632. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  3633. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3634. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  3635. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  3636. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3637. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3638. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  3639. {
  3640. system("sync");
  3641. sleep(10);
  3642. system("reboot -f");
  3643. sleep(10);
  3644. system("reboot -f");
  3645. }
  3646. else
  3647. {
  3648. sleep(10);
  3649. close(wtdFd);
  3650. system("/usr/bin/run_evse_restart.sh");
  3651. }
  3652. }
  3653. }
  3654. }
  3655. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3656. {
  3657. if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  3658. {
  3659. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  3660. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):TRUE))
  3661. {
  3662. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  3663. sprintf((char*)ShmOCPP20Data->Reset.Response_status, "Accepted");
  3664. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  3665. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  3666. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  3667. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3668. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate") == 0)
  3669. {
  3670. system("sync");
  3671. sleep(10);
  3672. system("reboot -f");
  3673. sleep(10);
  3674. system("reboot -f");
  3675. }
  3676. else
  3677. {
  3678. sleep(10);
  3679. close(wtdFd);
  3680. system("/usr/bin/run_evse_restart.sh");
  3681. }
  3682. }
  3683. }
  3684. }
  3685. }
  3686. void checkReservation(uint8_t gun_index)
  3687. {
  3688. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3689. {
  3690. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  3691. {
  3692. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3693. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3694. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3695. {
  3696. sleep(5);
  3697. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  3698. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3699. }
  3700. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3701. }
  3702. }
  3703. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3704. {
  3705. if(ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  3706. {
  3707. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3708. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3709. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3710. {
  3711. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  3712. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3713. }
  3714. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3715. }
  3716. }
  3717. }
  3718. void checkUnlocker(uint8_t gun_index)
  3719. {
  3720. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3721. {
  3722. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3723. {
  3724. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3725. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  3726. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  3727. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  3728. }
  3729. }
  3730. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3731. {
  3732. if(ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3733. {
  3734. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3735. sprintf((char*)ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].Response_status, "UnlockFailed");
  3736. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].UnlockConnectorConf = ON;
  3737. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor = ON;
  3738. }
  3739. }
  3740. }
  3741. void checkAvailability(uint8_t gun_index)
  3742. {
  3743. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3744. {
  3745. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3746. {
  3747. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  3748. {
  3749. DB_Update_Operactive(localDb, gun_index, true);
  3750. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3751. }
  3752. else
  3753. {
  3754. DB_Update_Operactive(localDb, gun_index, false);
  3755. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3756. }
  3757. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3758. }
  3759. }
  3760. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3761. {
  3762. if(ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3763. {
  3764. if(strcmp((char*)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  3765. {
  3766. DB_Update_Operactive(localDb, gun_index, true);
  3767. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3768. }
  3769. else
  3770. {
  3771. DB_Update_Operactive(localDb, gun_index, false);
  3772. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  3773. }
  3774. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  3775. }
  3776. }
  3777. }
  3778. void checkChargingProfileLimit(uint8_t gun_index, uint8_t system_mode)
  3779. {
  3780. uint16_t MaxChargingProfileChargingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3781. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3782. {
  3783. // Get profile charging profile limit
  3784. if((ocpp_get_smartcharging_profileId(gun_index) > 0))
  3785. {
  3786. // Checking profile kind
  3787. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  3788. {
  3789. // Checking limitation
  3790. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3791. {
  3792. if((getScheduleStart(gun_index) >= ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3793. ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3794. )
  3795. {
  3796. ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit,"W")==PASS?ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/(220*ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases):ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
  3797. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3798. }
  3799. else
  3800. break;
  3801. }
  3802. }
  3803. }
  3804. else
  3805. {
  3806. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3807. }
  3808. // Get max charging profile limit
  3809. if((ocpp_get_maxcharging_profileId() > 0))
  3810. {
  3811. // Absolute profile
  3812. if((mystrcmp((char*)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == PASS))
  3813. {
  3814. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  3815. {
  3816. if((getMaxScheduleStart() >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  3817. ((idx_period == 0) || (ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0))
  3818. )
  3819. {
  3820. MaxChargingProfileChargingCurrent = (mystrcmp((char*)ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingRateUnit,"W")==PASS?ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/(220*ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases):ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
  3821. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3822. }
  3823. else
  3824. break;
  3825. }
  3826. }
  3827. }
  3828. // Compare target current is over max current for EVSE limit
  3829. if(gun_index == 0)
  3830. {
  3831. if(ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  3832. {
  3833. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  3834. }
  3835. }
  3836. else
  3837. {
  3838. if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > MaxChargingProfileChargingCurrent)
  3839. {
  3840. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
  3841. }
  3842. }
  3843. }
  3844. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3845. {
  3846. // Get profile charging profile limit
  3847. if((ocpp_get_smartcharging_profileId(gun_index) > 0))
  3848. {
  3849. // Checking profile kind
  3850. if((mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  3851. {
  3852. // Checking limitation
  3853. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3854. {
  3855. if((getScheduleStart(gun_index) >= ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3856. ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3857. )
  3858. {
  3859. ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingRateUnit,"W")==PASS?ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit/(220*ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].numberPhases):ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit);
  3860. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3861. }
  3862. else
  3863. break;
  3864. }
  3865. }
  3866. }
  3867. else
  3868. {
  3869. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  3870. }
  3871. // Get max charging profile limit
  3872. if((ocpp_get_maxcharging_profileId() > 0))
  3873. {
  3874. // Absolute profile
  3875. if((mystrcmp((char*)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, "Absolute") == PASS))
  3876. {
  3877. // Checking limitation
  3878. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);idx_period++)
  3879. {
  3880. if((getMaxScheduleStart() >= ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod) &&
  3881. ((idx_period == 0) || (ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0))
  3882. )
  3883. {
  3884. MaxChargingProfileChargingCurrent = (mystrcmp((char*)ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingRateUnit,"W")==PASS?ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit/(220*ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].numberPhases):ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit);
  3885. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  3886. }
  3887. else
  3888. break;
  3889. }
  3890. }
  3891. }
  3892. // Compare target current is over max current for EVSE limit
  3893. if(gun_index == 0)
  3894. {
  3895. if(ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  3896. {
  3897. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  3898. }
  3899. }
  3900. else
  3901. {
  3902. if((ShmCharger->gun_info[gun_index-1].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent) > MaxChargingProfileChargingCurrent)
  3903. {
  3904. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent - ShmCharger->gun_info[gun_index-1].targetCurrent);
  3905. }
  3906. }
  3907. }
  3908. switch(system_mode)
  3909. {
  3910. case SYS_MODE_IDLE:
  3911. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3912. {
  3913. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3914. if(ShmCharger->isCcsEnable)
  3915. {
  3916. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  3917. }
  3918. else
  3919. {
  3920. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3921. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
  3922. }
  3923. }
  3924. else
  3925. {
  3926. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3927. if(ShmCharger->isCcsEnable)
  3928. {
  3929. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  3930. }
  3931. else
  3932. {
  3933. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3934. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
  3935. }
  3936. }
  3937. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3938. break;
  3939. case SYS_MODE_PREPARING:
  3940. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3941. {
  3942. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3943. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3944. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
  3945. }
  3946. else
  3947. {
  3948. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3949. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3950. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
  3951. }
  3952. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3953. break;
  3954. case SYS_MODE_CHARGING:
  3955. case SYS_MODE_TERMINATING:
  3956. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  3957. {
  3958. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3959. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  3960. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  3961. {
  3962. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3963. {
  3964. if(getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  3965. {
  3966. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3967. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
  3968. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3969. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3970. }
  3971. }
  3972. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3973. {
  3974. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3975. ShmCharger->gun_info[gun_index].targetCurrent = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)?6:ShmCharger->gun_info[gun_index].targetCurrent);
  3976. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float)ShmCharger->gun_info[gun_index].targetCurrent;
  3977. //ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float)((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  3978. }
  3979. }
  3980. }
  3981. else
  3982. {
  3983. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3984. if((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current) ||
  3985. (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  3986. {
  3987. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3988. {
  3989. if(getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  3990. {
  3991. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  3992. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
  3993. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  3994. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  3995. }
  3996. }
  3997. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3998. {
  3999. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4000. ShmCharger->gun_info[gun_index].targetCurrent = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)?6:ShmCharger->gun_info[gun_index].targetCurrent);
  4001. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float)ShmCharger->gun_info[gun_index].targetCurrent;
  4002. //ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float)((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  4003. }
  4004. }
  4005. }
  4006. if(previousData[gun_index].primaryMcuCp_Pwn_Duty != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  4007. {
  4008. DEBUG_INFO("==================================================\n");
  4009. DEBUG_INFO("gun-%02d primaryMcuCp_Pwn_Duty: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  4010. DEBUG_INFO("==================================================\n");
  4011. previousData[gun_index].primaryMcuCp_Pwn_Duty = ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current;
  4012. }
  4013. if(ShmCharger->gun_info[gun_index].targetCurrent != previousData[gun_index].targetCurrent)
  4014. {
  4015. DEBUG_INFO("==================================================\n");
  4016. DEBUG_INFO("gun-%02d targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4017. DEBUG_INFO("MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4018. DEBUG_INFO("==================================================\n");
  4019. previousData[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].targetCurrent;
  4020. }
  4021. if(ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != previousData[gun_index].current_limit)
  4022. {
  4023. DEBUG_INFO("==================================================\n");
  4024. DEBUG_INFO("gun-%02d mcu current_limit: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  4025. DEBUG_INFO("==================================================\n");
  4026. previousData[gun_index].current_limit = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
  4027. }
  4028. break;
  4029. }
  4030. }
  4031. void checkStopReason(uint8_t gun_index)
  4032. {
  4033. sleep(2);
  4034. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4035. {
  4036. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4037. if(!ocpp_get_starttransaction_result(gun_index))
  4038. {
  4039. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  4040. }
  4041. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  4042. {
  4043. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  4044. }
  4045. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  4046. {
  4047. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4048. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4049. }
  4050. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  4051. {
  4052. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  4053. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  4054. else
  4055. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  4056. }
  4057. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  4058. {
  4059. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  4060. if(!isMatchStartUser(gun_index))
  4061. {
  4062. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4063. }
  4064. else
  4065. {
  4066. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4067. }
  4068. DEBUG_INFO("Gun-[%d] : IdTag [ %s ].\n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  4069. }
  4070. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  4071. {
  4072. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  4073. }
  4074. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  4075. {
  4076. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  4077. }
  4078. else
  4079. {
  4080. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  4081. }
  4082. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4083. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  4084. presentChargedEnergyUpdate(gun_index);
  4085. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  4086. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  4087. }
  4088. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4089. {
  4090. memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  4091. if(!ocpp_get_starttransaction_result(gun_index))
  4092. {
  4093. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  4094. }
  4095. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  4096. {
  4097. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  4098. }
  4099. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  4100. {
  4101. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  4102. }
  4103. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  4104. {
  4105. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  4106. }
  4107. else if(ShmOCPP20Data->MsMsg.bits.ResetReq)
  4108. {
  4109. if(strcmp((char*)ShmOCPP20Data->Reset.type, "Immediate")==0)
  4110. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  4111. else
  4112. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OnIdle");
  4113. }
  4114. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  4115. {
  4116. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  4117. if(!isMatchStartUser(gun_index))
  4118. {
  4119. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  4120. }
  4121. else
  4122. {
  4123. memcpy((char*)ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  4124. }
  4125. DEBUG_INFO("Gun-[%d] : idToken [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
  4126. }
  4127. else if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L1_OVER_CURRENT) ||
  4128. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L2_OVER_CURRENT) ||
  4129. (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L3_OVER_CURRENT))
  4130. {
  4131. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  4132. }
  4133. else if(ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  4134. {
  4135. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  4136. }
  4137. else if(ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].isUnlockerConnetor == ON)
  4138. {
  4139. sprintf((char*)ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId-1].transactionInfo.stoppedReason, "UnlockCommand");
  4140. }
  4141. else
  4142. {
  4143. sprintf((char*)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  4144. }
  4145. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n", gun_index, ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4146. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  4147. presentChargedEnergyUpdate(gun_index);
  4148. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n",gun_index ,ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
  4149. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  4150. }
  4151. }
  4152. void checkRemoteUpgradeStatus()
  4153. {
  4154. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4155. {
  4156. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  4157. {
  4158. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4159. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4160. ShmCharger->isUpdateSuccess = NO;
  4161. }
  4162. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  4163. {
  4164. DEBUG_INFO("Firmware remote upgrading...\n");
  4165. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4166. sleep(1);
  4167. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4168. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4169. int result = upgrade_check();
  4170. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  4171. if(result == PASS)
  4172. {
  4173. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4174. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4175. ShmCharger->isUpdateSuccess = YES;
  4176. DEBUG_INFO("Remote update success...\n");
  4177. }
  4178. else
  4179. {
  4180. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4181. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4182. ShmCharger->isUpdateSuccess = NO;
  4183. DEBUG_INFO("Remote update unsuccess...\n");
  4184. }
  4185. }
  4186. else
  4187. {}
  4188. }
  4189. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4190. {
  4191. if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed")==0)
  4192. {
  4193. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4194. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4195. ShmCharger->isUpdateSuccess = NO;
  4196. }
  4197. else if(strcmp((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded")==0)
  4198. {
  4199. DEBUG_INFO("Firmware remote upgrading...\n");
  4200. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4201. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4202. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4203. int result = upgrade_check();
  4204. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  4205. if(result == PASS)
  4206. {
  4207. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4208. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4209. ShmCharger->isUpdateSuccess = YES;
  4210. DEBUG_INFO("Remote update success...\n");
  4211. }
  4212. else
  4213. {
  4214. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4215. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4216. ShmCharger->isUpdateSuccess = NO;
  4217. DEBUG_INFO("Remote update unsuccess...\n");
  4218. }
  4219. }
  4220. else
  4221. {}
  4222. }
  4223. }
  4224. void checkHandshakeCountdown(uint8_t gun_index)
  4225. {
  4226. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  4227. {
  4228. case SYS_MODE_IDLE:
  4229. if(ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
  4230. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4231. else
  4232. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4233. if(!ShmCharger->isAuthrizing)
  4234. {
  4235. if(ocpp_get_isRemoteStartWait())
  4236. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4237. else
  4238. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4239. }
  4240. break;
  4241. case SYS_MODE_PREPARING:
  4242. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]));
  4243. break;
  4244. case SYS_MODE_RESERVATION:
  4245. if(ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
  4246. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4247. else
  4248. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4249. break;
  4250. }
  4251. }
  4252. void checkRfidAuthrize()
  4253. {
  4254. static uint8_t isCheckdResult = FALSE;
  4255. uint8_t isStartByWrongReservation = NO;
  4256. if(!ShmCharger->isAuthrizing)
  4257. {
  4258. // Read RFID
  4259. if(GetCardSerialNumber() != FAIL)
  4260. {
  4261. uint8_t isSnStart = FALSE;
  4262. uint8_t bufferRFID[ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId)];
  4263. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4264. {
  4265. // Big endian
  4266. switch(rfid.snType)
  4267. {
  4268. case RFID_SN_TYPE_6BYTE:
  4269. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  4270. break;
  4271. case RFID_SN_TYPE_7BYTE:
  4272. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  4273. break;
  4274. case RFID_SN_TYPE_10BYTE:
  4275. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  4276. break;
  4277. case RFID_SN_TYPE_4BYTE:
  4278. default:
  4279. sprintf((char*)bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  4280. break;
  4281. }
  4282. }
  4283. else
  4284. {
  4285. // Little endian
  4286. switch(rfid.snType)
  4287. {
  4288. case RFID_SN_TYPE_6BYTE:
  4289. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4290. break;
  4291. case RFID_SN_TYPE_7BYTE:
  4292. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4293. break;
  4294. case RFID_SN_TYPE_10BYTE:
  4295. sprintf((char*)bufferRFID, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4296. break;
  4297. case RFID_SN_TYPE_4BYTE:
  4298. default:
  4299. sprintf((char*)bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  4300. break;
  4301. }
  4302. }
  4303. DEBUG_INFO("Authorize request User Id : %s\n", bufferRFID);
  4304. //DEBUG_INFO("Authorize request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4305. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4306. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  4307. // Check SN already start
  4308. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4309. {
  4310. if(isMatchStartUser(gun_index) || (isMatchPresentUser(gun_index) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING)))
  4311. {
  4312. DEBUG_INFO("%s running on connector-%02d.\n", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, gun_index);
  4313. isSnStart = TRUE;
  4314. ShmCharger->gun_info[gun_index].rfidReq = ON;
  4315. ocpp_set_auth_conf(ON);
  4316. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4317. setSpeaker(ON, SPEAKER_SHORT);
  4318. sleep(3);
  4319. break;
  4320. }
  4321. }
  4322. // Request authorize if isSnStart is false
  4323. if(!isSnStart)
  4324. {
  4325. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, bufferRFID, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4326. refreshStartTimer(&startTime[0][TMR_IDX_AUTH]);
  4327. ocpp_set_auth_conf(OFF);
  4328. ocpp_set_auth_req(ON, "ISO14443");
  4329. setLedMotion(0,LED_ACTION_AUTHED);
  4330. ShmCharger->isAuthrizing = TRUE;
  4331. ShmCharger->isGetAuthResult = FALSE;
  4332. isCheckdResult = FALSE;
  4333. }
  4334. }
  4335. }
  4336. else
  4337. {
  4338. // Wait authorize result
  4339. if(!ocpp_get_auth_conf() && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH))
  4340. {
  4341. // Authorization timeout process.
  4342. DEBUG_WARN("Authorize timeout !!!\n");
  4343. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4344. {
  4345. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4346. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4347. }
  4348. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4349. sleep(3);
  4350. ShmCharger->isAuthrizing = FALSE;
  4351. }
  4352. else
  4353. {
  4354. if(ocpp_get_auth_conf() ||
  4355. (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  4356. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > 2))
  4357. )
  4358. {
  4359. if(ocpp_get_auth_result(NO) ||
  4360. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  4361. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  4362. {
  4363. if(!isCheckdResult)
  4364. {
  4365. DEBUG_INFO("Authorize pass.\n");
  4366. setSpeaker(ON, SPEAKER_SHORT);
  4367. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4368. ShmCharger->isGetAuthResult = TRUE;
  4369. isCheckdResult = TRUE;
  4370. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  4371. {
  4372. ocpp_set_auth_conf(ON);
  4373. }
  4374. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4375. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  4376. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[ShmCharger->gun_selectd].SystemStatus == SYS_MODE_RESERVATION) && (strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[ShmCharger->gun_selectd].IdTag) != 0))
  4377. {
  4378. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4379. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4380. isStartByWrongReservation = YES;
  4381. }
  4382. }
  4383. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4384. checkHandshakeCountdown(gun_index);
  4385. if((getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < (ocpp_get_connection_timeout())) && (isStartByWrongReservation != YES))
  4386. {
  4387. if(GetCardSerialNumber() != FAIL)
  4388. {
  4389. if(isMatchPresentUser())
  4390. {
  4391. DEBUG_INFO("Cancel present user.\n");
  4392. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4393. {
  4394. setLedMotion(gun_index,LED_ACTION_IDLE);
  4395. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4396. }
  4397. ShmCharger->isAuthrizing = FALSE;
  4398. }
  4399. }
  4400. else
  4401. {
  4402. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4403. {
  4404. if(getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < 3)
  4405. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  4406. else
  4407. setLedMotion(gun_index,LED_ACTION_AUTHED);
  4408. if((((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_RESERVATION)) && (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) || (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON))) ||
  4409. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) && (ShmCharger->gun_selectd == gun_index)) ||
  4410. (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING)) && (ShmCharger->gun_selectd == gun_index))
  4411. )
  4412. {
  4413. DEBUG_INFO("Connector-%02d action detect.\n", gun_index);
  4414. ShmCharger->isAuthrizing = FALSE;
  4415. ShmCharger->gun_info[gun_index].rfidReq = ON;
  4416. break;
  4417. }
  4418. }
  4419. }
  4420. }
  4421. else
  4422. {
  4423. DEBUG_INFO("Connector action detect timeout.\n");
  4424. ShmCharger->isAuthrizing = FALSE;
  4425. ShmCharger->isGetAuthResult = TRUE;
  4426. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4427. {
  4428. setLedMotion(gun_index,LED_ACTION_HANDSHAKE_FAIL);
  4429. sleep(6);
  4430. setLedMotion(gun_index,LED_ACTION_IDLE);
  4431. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4432. }
  4433. }
  4434. }
  4435. else
  4436. {
  4437. DEBUG_INFO("Authorize fail.\n");
  4438. ShmCharger->isAuthrizing = FALSE;
  4439. ShmCharger->isGetAuthResult = TRUE;
  4440. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4441. {
  4442. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4443. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  4444. }
  4445. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  4446. sleep(3);
  4447. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4448. {
  4449. setLedMotion(gun_index,LED_ACTION_IDLE);
  4450. }
  4451. }
  4452. }
  4453. }
  4454. }
  4455. }
  4456. //======================================================
  4457. // Main process
  4458. //======================================================
  4459. int main(void)
  4460. {
  4461. //==================================================
  4462. // Create all share memory
  4463. //==================================================
  4464. if(CreatShareMemory()==0)
  4465. {
  4466. DEBUG_ERROR("CreatShareMemory NG\n");
  4467. if(ShmStatusCodeData!=NULL)
  4468. {
  4469. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  4470. }
  4471. sleep(5);
  4472. system("reboot -f");
  4473. sleep(5);
  4474. system("reboot -f");
  4475. }
  4476. else
  4477. {
  4478. DEBUG_INFO("CreatShareMemory OK\n");
  4479. }
  4480. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4481. {
  4482. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  4483. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  4484. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  4485. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  4486. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  4487. }
  4488. //==================================================
  4489. // Main loop
  4490. //==================================================
  4491. for(;;)
  4492. {
  4493. //==============================================
  4494. // Synchronize share memory from OCPP struct
  4495. //==============================================
  4496. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  4497. //==============================================
  4498. // Period check for 10 seconds
  4499. //==============================================
  4500. if((getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) > 10) || (getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) < 0))
  4501. {
  4502. //==============================================
  4503. // Check task processing
  4504. //==============================================
  4505. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  4506. checkTask();
  4507. //==============================================
  4508. // Check connection timeout specification
  4509. //==============================================
  4510. checkConnectionTimeout();
  4511. refreshStartTimer(&startTime[0][TMR_IDX_CHECK_TASK]);
  4512. }
  4513. //==============================================
  4514. // Something need run in Idle mode
  4515. //==============================================
  4516. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_DEBUG)) &&
  4517. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_DEBUG)):true))
  4518. {
  4519. // Check restore factory setting request
  4520. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  4521. {
  4522. // Set led to initial
  4523. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4524. {
  4525. setLedMotion(gun_index,LED_ACTION_INIT);
  4526. }
  4527. sleep(5);
  4528. system("cd /root;./Module_FactoryConfig -m");
  4529. system("rm -f /Storage/OCPP/OCPPConfiguration");
  4530. system("sync");
  4531. sleep(5);
  4532. system("reboot -f");
  4533. sleep(5);
  4534. system("reboot -f");
  4535. }
  4536. // Check upgrade firmware request
  4537. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  4538. ocpp_get_update_firmware_req())
  4539. {
  4540. // If available memory too small, free memory cache first
  4541. if(getAvailableMemory() < (200*1024*1024))
  4542. {
  4543. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n", getAvailableMemory()/(1024*1024.0));
  4544. system("echo 3 > /proc/sys/vm/drop_caches");
  4545. }
  4546. ShmCharger->isUpdateSuccess = NO;
  4547. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4548. {
  4549. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  4550. setChargerMode(gun_index, SYS_MODE_UPDATE);
  4551. }
  4552. }
  4553. }
  4554. //==============================================
  4555. // Check remote reset request
  4556. //==============================================
  4557. checkReset();
  4558. //==============================================
  4559. // Check RFID authorization
  4560. //==============================================
  4561. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4562. {
  4563. // The user is not allowed to use RFID when the system change to under those modes.
  4564. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM) ||
  4565. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_FAULT) ||
  4566. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_DEBUG) ||
  4567. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE) ||
  4568. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_BOOTING) ||
  4569. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_COMPLETE) ||
  4570. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_MAINTAIN) ||
  4571. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE))
  4572. {
  4573. }
  4574. else
  4575. {
  4576. checkRfidAuthrize();
  4577. }
  4578. }
  4579. //==============================================
  4580. // Connector loop
  4581. //==============================================
  4582. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  4583. {
  4584. //==========================================
  4585. // Power saving logic
  4586. //==========================================
  4587. /*
  4588. * TODO:
  4589. * 1. Power saving logic
  4590. */
  4591. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == OFF))
  4592. {
  4593. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  4594. }
  4595. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM) ||
  4596. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_FAULT) ||
  4597. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE) ||
  4598. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_MAINTAIN) ||
  4599. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING) ||
  4600. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING) ||
  4601. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_COMPLETE))
  4602. {
  4603. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4604. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  4605. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  4606. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  4607. }
  4608. else
  4609. {
  4610. if((ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES) ||
  4611. (gpio_get_value(GPIO_IN_WAKEUP) == OFF) ||
  4612. (ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON) ||
  4613. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus != ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus) ||
  4614. (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_RESERVATION)) &&
  4615. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)) &&
  4616. (getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]) < 10)))
  4617. {
  4618. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4619. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  4620. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  4621. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  4622. if(((gpio_get_value(GPIO_IN_WAKEUP) == OFF) || (ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON)) &&
  4623. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) &&
  4624. (getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_CHECK_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
  4625. {
  4626. ShmCharger->gun_info[gun_index].isCheckPowerConsumption = YES;
  4627. refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
  4628. }
  4629. }
  4630. else
  4631. {
  4632. refreshStartTimer(&startTime[gun_index][TMR_IDX_CHECK_POWER_CONSUMPTION]);
  4633. if((getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_LCM_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
  4634. {
  4635. ShmCharger->gun_info[gun_index].isCheckPowerConsumption = NO;
  4636. }
  4637. }
  4638. }
  4639. if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) > TIMEOUT_SPEC_POWERSAVING_LCD)
  4640. {
  4641. if(ShmCharger->isLcdOn == ON)
  4642. {
  4643. DEBUG_INFO("LCD into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
  4644. ShmCharger->isLcdOn = OFF;
  4645. }
  4646. }
  4647. else
  4648. {
  4649. if(ShmCharger->isLcdOn == OFF)
  4650. {
  4651. DEBUG_INFO("LCD exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
  4652. ShmCharger->isLcdOn = ON;
  4653. }
  4654. }
  4655. if(getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]) > TIMEOUT_SPEC_POWERSAVING_RFID)
  4656. {
  4657. if(gpio_get_value(GPIO_OUT_RST_RFID) == ON)
  4658. {
  4659. DEBUG_INFO("RFID into power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  4660. gpio_set_value(GPIO_OUT_RST_RFID, OFF);
  4661. }
  4662. }
  4663. else
  4664. {
  4665. if(gpio_get_value(GPIO_OUT_RST_RFID) == OFF)
  4666. {
  4667. DEBUG_INFO("RFID exit power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
  4668. gpio_set_value(GPIO_OUT_RST_RFID, ON);
  4669. }
  4670. }
  4671. if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]) > TIMEOUT_SPEC_POWERSAVING_METER)
  4672. {
  4673. if(ShmCharger->gun_info[gun_index].isMeterOn)
  4674. {
  4675. DEBUG_INFO("Meter into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  4676. ShmCharger->gun_info[gun_index].isMeterOn = OFF;
  4677. }
  4678. }
  4679. else
  4680. {
  4681. if(!ShmCharger->gun_info[gun_index].isMeterOn)
  4682. {
  4683. DEBUG_INFO("Meter exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
  4684. ShmCharger->gun_info[gun_index].isMeterOn = ON;
  4685. }
  4686. }
  4687. if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]) > TIMEOUT_SPEC_POWERSAVING_LED_STATUS)
  4688. {
  4689. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  4690. {
  4691. DEBUG_INFO("LED status into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
  4692. ShmCharger->gun_info[gun_index].isSleepOn = YES;
  4693. }
  4694. }
  4695. else
  4696. {
  4697. if(ShmCharger->gun_info[gun_index].isSleepOn == YES)
  4698. {
  4699. DEBUG_INFO("LED status exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
  4700. ShmCharger->gun_info[gun_index].isSleepOn = NO;
  4701. }
  4702. }
  4703. //==========================================
  4704. // Synchronize current rating value from MCU
  4705. //==========================================
  4706. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  4707. //==========================================
  4708. // Assign connector location index for OCPP
  4709. //==========================================
  4710. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  4711. //==========================================
  4712. // Synchronize present charging power
  4713. //==========================================
  4714. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  4715. {
  4716. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  4717. {
  4718. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  4719. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower <= 1.0)
  4720. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4721. }
  4722. else
  4723. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4724. }
  4725. else
  4726. {
  4727. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  4728. {
  4729. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000) +
  4730. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL2*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2)/1000) +
  4731. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL3*ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3)/1000));
  4732. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower <= 1.0)
  4733. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4734. }
  4735. else
  4736. {
  4737. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  4738. }
  4739. }
  4740. //==========================================
  4741. // Check initialization "PASS" or "FAIL"
  4742. //==========================================
  4743. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  4744. {
  4745. // Alarm event check
  4746. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0) || (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode>0) || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
  4747. {
  4748. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  4749. {
  4750. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_UPDATE)
  4751. {
  4752. setChargerMode(gun_index, SYS_MODE_ALARM);
  4753. }
  4754. }
  4755. }
  4756. }
  4757. //==========================================
  4758. // Reservation request check
  4759. //==========================================
  4760. checkReservation(gun_index);
  4761. //==========================================
  4762. // Change availability check
  4763. //==========================================
  4764. checkAvailability(gun_index);
  4765. if(ShmCharger->gun_info[gun_index].isOperactive)
  4766. {
  4767. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  4768. {
  4769. setChargerMode(gun_index, SYS_MODE_IDLE);
  4770. }
  4771. }
  4772. else
  4773. {
  4774. if(isMode(gun_index, SYS_MODE_IDLE))
  4775. {
  4776. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  4777. }
  4778. }
  4779. //==========================================
  4780. // Unlock Connector signal check
  4781. //==========================================
  4782. checkUnlocker(gun_index);
  4783. //==========================================
  4784. // Connector process
  4785. //==========================================
  4786. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  4787. {
  4788. case SYS_MODE_BOOTING:
  4789. if(isModeChange(gun_index))
  4790. {
  4791. setLedMotion(gun_index,LED_ACTION_INIT);
  4792. //CSU Initialization & task spawn
  4793. if((Initialization(gun_index) != PASS) ||
  4794. (SpawnTask(gun_index) != PASS))
  4795. {
  4796. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  4797. }
  4798. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4799. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  4800. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  4801. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  4802. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  4803. refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
  4804. }
  4805. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  4806. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  4807. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  4808. {
  4809. ShmCharger->gun_selectd = 0;
  4810. // Firmware version
  4811. get_firmware_version(gun_index);
  4812. // OCPP BootNotification info set
  4813. DEBUG_INFO("==========================================\n");
  4814. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  4815. DEBUG_INFO("==========================================\n");
  4816. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  4817. ocpp_boot_info_sync();
  4818. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  4819. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  4820. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  4821. DEBUG_INFO("==========================================\n");
  4822. DEBUG_INFO("========== Set Wifi information ==========\n");
  4823. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  4824. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  4825. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  4826. DEBUG_INFO("==========================================\n");
  4827. // Set max current to rating current
  4828. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  4829. // Default Ethernet / Wifi / 4G to 1:disconnected
  4830. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  4831. {
  4832. case 'E':
  4833. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4834. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  4835. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  4836. break;
  4837. case 'W':
  4838. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4839. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  4840. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  4841. break;
  4842. case 'T':
  4843. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4844. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  4845. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  4846. break;
  4847. case 'D':
  4848. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  4849. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  4850. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  4851. break;
  4852. }
  4853. // Default LCM brightness to 100
  4854. ShmCharger->isLcdOn = ON;
  4855. // Defaule led status to not sleep mode
  4856. ShmCharger->gun_info[gun_index].isSleepOn = NO;
  4857. // If Web Server OPCC URL is empty kill Module_OcppBackend
  4858. if((strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0) || ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing)
  4859. {
  4860. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  4861. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  4862. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing)
  4863. DEBUG_INFO("Local power sharing enable kill Module_OcppBackend...\n");
  4864. system ("pkill OcppBackend");
  4865. }
  4866. // If rotate switch equal zero, the system needs to change Debug mode
  4867. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  4868. setChargerMode(gun_index, SYS_MODE_DEBUG);
  4869. else
  4870. setChargerMode(gun_index, SYS_MODE_IDLE);
  4871. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = ((ShmSysConfigAndInfo->SysConfig.ModelName[2]=='Y') ||
  4872. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='D') ||
  4873. (ShmSysConfigAndInfo->SysConfig.ModelName[2]=='W')
  4874. ?3:1);
  4875. }
  4876. break;
  4877. case SYS_MODE_IDLE:
  4878. if(isModeChange(gun_index))
  4879. {
  4880. setLedMotion(gun_index,LED_ACTION_IDLE);
  4881. setRelay(gun_index, OFF);
  4882. setRequest(gun_index,OFF);
  4883. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  4884. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  4885. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4886. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  4887. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  4888. ocpp_set_remotestart(gun_index, OFF);
  4889. ocpp_set_remotestop(gun_index, OFF);
  4890. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  4891. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  4892. presentChargedEnergyClear(gun_index);
  4893. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  4894. ocpp_set_unlocker_req(gun_index, OFF);
  4895. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  4896. // Response StopTransactionConf
  4897. ocpp_set_stoptransaction_conf(gun_index, OFF);
  4898. memset(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId));
  4899. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4900. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_IDLE;
  4901. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  4902. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  4903. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  4904. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = OFF;
  4905. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4906. //if(ShmCharger->isCcsEnable)system("pkill Module_CCS");
  4907. DB_Check_Record_Buf(localDb, gun_index);
  4908. // Default previousData every time
  4909. previousData[gun_index].primaryMcuCp_Pwn_Duty = 0;
  4910. previousData[gun_index].targetCurrent = 0;
  4911. previousData[gun_index].current_limit = 0;
  4912. }
  4913. checkHandshakeCountdown(gun_index);
  4914. // LED status in Idle mode
  4915. if(!ShmCharger->isAuthrizing)
  4916. {
  4917. if(ocpp_get_isRemoteStartWait())
  4918. {
  4919. ShmCharger->gun_info[gun_index].isSleepOn = OFF;
  4920. setLedMotion(gun_index, LED_ACTION_AUTHED);
  4921. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  4922. //DEBUG_INFO("Remote Start Transaction without connector Id.\n");
  4923. }
  4924. else
  4925. {
  4926. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus == ON)
  4927. {
  4928. if(ShmCharger->gun_info[gun_index].rfidReq == OFF)
  4929. {
  4930. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  4931. setLedMotion(gun_index,LED_ACTION_IDLE_BACKEND_CONNECTED);
  4932. else
  4933. setLedMotion(gun_index,LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP);
  4934. }
  4935. }
  4936. else
  4937. {
  4938. if(ShmCharger->gun_info[gun_index].rfidReq == OFF)
  4939. {
  4940. if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
  4941. setLedMotion(gun_index,LED_ACTION_IDLE_BACKEND_DISCONNECTED);
  4942. else
  4943. setLedMotion(gun_index,LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP);
  4944. }
  4945. }
  4946. }
  4947. }
  4948. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  4949. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  4950. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)) ||
  4951. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  4952. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  4953. (ocpp_get_remotestart(gun_index) == ON) ||
  4954. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  4955. {
  4956. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  4957. {
  4958. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  4959. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  4960. DEBUG_INFO("Start Method : RFID...\n");
  4961. }
  4962. else if(ocpp_get_remotestart(gun_index))
  4963. {
  4964. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  4965. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  4966. ocpp_copy_userid_from_remotestart(gun_index);
  4967. setSpeaker(ON, SPEAKER_SHORT);
  4968. DEBUG_INFO("Start Method : BACKEND...\n");
  4969. }
  4970. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  4971. {
  4972. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  4973. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Local;
  4974. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  4975. setSpeaker(ON, SPEAKER_SHORT);
  4976. DEBUG_INFO("Start Method : BLE...\n");
  4977. }
  4978. else
  4979. {
  4980. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  4981. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_NoAuthorization;
  4982. //memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  4983. ocpp_get_freevend_idtag(ShmSysConfigAndInfo->SysConfig.UserId);
  4984. setSpeaker(ON, SPEAKER_SHORT);
  4985. DEBUG_INFO("Start Method : FREE...\n");
  4986. }
  4987. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  4988. ocpp_set_remotestart(gun_index, OFF);
  4989. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  4990. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  4991. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  4992. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  4993. // Get target current
  4994. ocpp_set_profile_req(gun_index, ON);
  4995. sleep(1);
  4996. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  4997. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  4998. }
  4999. else
  5000. {}
  5001. break;
  5002. case SYS_MODE_AUTHORIZING:
  5003. if(isModeChange(gun_index))
  5004. {
  5005. refreshStartTimer(&startTime[gun_index][TMR_IDX_AUTH]);
  5006. if(ocpp_isAuthorizeRemoteStart() && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND))
  5007. {
  5008. DEBUG_INFO("Remote start request authorize.\n");
  5009. ocpp_set_auth_req(ON, "ISO14443");
  5010. }
  5011. }
  5012. if(getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  5013. {
  5014. // Authorization timeout process.
  5015. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  5016. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  5017. sleep(3);
  5018. setChargerMode(gun_index, SYS_MODE_IDLE);
  5019. DEBUG_WARN("Authorize timeout !!!\n");
  5020. }
  5021. else
  5022. {
  5023. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  5024. {
  5025. case START_METHOD_BACKEND:
  5026. if(ocpp_isAuthorizeRemoteStart())
  5027. {
  5028. if(ocpp_get_auth_conf())
  5029. {
  5030. if(ocpp_get_auth_result(gun_index))
  5031. {
  5032. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5033. DEBUG_INFO("Authorize pass.\n");
  5034. setSpeaker(ON,SPEAKER_SHORT);
  5035. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  5036. #if 0
  5037. if(ShmCharger->isCcsEnable)
  5038. {
  5039. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5040. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5041. sleep(4);
  5042. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5043. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5044. //system("/root/Module_CCS &");
  5045. }
  5046. else
  5047. #endif
  5048. sleep(4);
  5049. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5050. }
  5051. else
  5052. {
  5053. DEBUG_INFO("Authorize fail.\n");
  5054. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  5055. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  5056. sleep(3);
  5057. setChargerMode(gun_index, SYS_MODE_IDLE);
  5058. }
  5059. ocpp_set_auth_conf(OFF);
  5060. }
  5061. }
  5062. else
  5063. {
  5064. if(ShmCharger->isCcsEnable)
  5065. {
  5066. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5067. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5068. //sleep(4);
  5069. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5070. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5071. //system("/root/Module_CCS &");
  5072. }
  5073. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5074. }
  5075. break;
  5076. case START_METHOD_RFID:
  5077. case START_METHOD_BLE:
  5078. case START_METHOD_FREE:
  5079. default:
  5080. if(ShmCharger->isCcsEnable)
  5081. {
  5082. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5083. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5084. //sleep(4);
  5085. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5086. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5087. //system("/root/Module_CCS &");
  5088. }
  5089. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5090. break;
  5091. }
  5092. }
  5093. break;
  5094. case SYS_MODE_PREPARING:
  5095. if(isModeChange(gun_index))
  5096. {
  5097. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5098. setLedMotion(gun_index,LED_ACTION_AUTHED);
  5099. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5100. if(ShmCharger->isCcsEnable)
  5101. {
  5102. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5;
  5103. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_DUTY_5\n");
  5104. }
  5105. else
  5106. {
  5107. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5108. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5109. setRequest(gun_index, ON);
  5110. }
  5111. }
  5112. // If control pilot detect Bx, skip watch dog time out.
  5113. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) ||
  5114. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  5115. {
  5116. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  5117. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5118. switch(ShmCharger->gun_info[gun_index].ccsHandshakeState)
  5119. {
  5120. case HANDSHAKE_DUTY_5:
  5121. if(!getRequest(gun_index))
  5122. {
  5123. setRequest(gun_index, ON);
  5124. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  5125. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5126. DEBUG_INFO("Set Request On.\n");
  5127. //Let CCS task start to negotiate
  5128. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = ON;
  5129. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_NONE;
  5130. }
  5131. // Set CCS 5% PWM duty
  5132. if(ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_5)//set 5% by SeccComm.c
  5133. {
  5134. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  5135. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK;
  5136. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5137. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK\n");
  5138. }
  5139. break;
  5140. case HANDSHAKE_DUTY_5_CHECK:
  5141. if((ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF))
  5142. {
  5143. //2 secs timeout
  5144. refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  5145. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CCS;
  5146. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_CCS\n");
  5147. }
  5148. break;
  5149. case HANDSHAKE_CCS:
  5150. //CCS handshake timeout
  5151. if(getDiffSecNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  5152. {
  5153. if(ShmCharger->isCcsEnable)
  5154. {
  5155. DEBUG_INFO("TIMEOUT_SPEC_BS_HLC_HANDSHAKE.\n");
  5156. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5157. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5158. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5159. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5160. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5161. break;
  5162. }
  5163. else
  5164. {
  5165. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5166. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5167. DEBUG_INFO("BS/HLC %d secs handshake timeout.\n", TIMEOUT_SPEC_BS_HLC_HANDSHAKE);
  5168. }
  5169. }
  5170. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  5171. {
  5172. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  5173. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5174. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5175. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5176. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5177. }
  5178. //CCS status check
  5179. #if 0
  5180. if((16 < ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 254))
  5181. {
  5182. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
  5183. DEBUG_INFO("Enter HLC Mode charging.\n");
  5184. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
  5185. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5186. }
  5187. #else
  5188. if((16 <= ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 253))
  5189. {
  5190. //chang PWM duty to BS
  5191. if((ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)&&(ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current!=CCS_PWM_DUTY_5))
  5192. {
  5193. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =CCS_PWM_DUTY_5; //((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  5194. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5195. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_CCS (set 5% duty)\n");
  5196. }
  5197. //ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
  5198. //ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
  5199. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5200. }
  5201. if((37 <= ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus < 49))
  5202. {
  5203. //chang PWM duty to BS
  5204. if((ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)&&(ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current!=CCS_PWM_DUTY_5))
  5205. {
  5206. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =CCS_PWM_DUTY_5; //((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  5207. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5208. }
  5209. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
  5210. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
  5211. //refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5212. DEBUG_INFO("ccsHandshakeState = HANDSHAKE_HLC_MODE\n");
  5213. }
  5214. else if((54 < ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus <= 255))
  5215. {
  5216. DEBUG_INFO("ccsHandshakeState = CHARGING_MODE_BS, CCS terminated\n");
  5217. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5218. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5219. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5220. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5221. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5222. }
  5223. #endif
  5224. break;
  5225. case HANDSHAKE_CP_STATE_E:
  5226. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  5227. {
  5228. if(ShmCharger->isCcsEnable)
  5229. {
  5230. DEBUG_INFO("Change to CP STATE E for 4 secs.\n");
  5231. //CP STATE E for 4 secs
  5232. sleep(4);
  5233. }
  5234. //restore normal CP PWM duty
  5235. // Determine max charging current to MCU
  5236. DEBUG_INFO("Determine max charging current to MCU.\n");
  5237. ocpp_set_profile_req(gun_index, ON);
  5238. sleep(1);
  5239. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  5240. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
  5241. refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  5242. }
  5243. break;
  5244. case HANDSHAKE_SET_MAX_CURRENT:
  5245. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  5246. {
  5247. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_BS_MODE;
  5248. DEBUG_INFO("Enter BS Mode charging.\n");
  5249. //for EV READY 30 secs didn't start charging to STATE E
  5250. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5251. }
  5252. break;
  5253. case HANDSHAKE_BS_MODE:
  5254. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5255. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  5256. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) && ((ShmCharger->gun_info[gun_index].targetCurrent != 0)))
  5257. {
  5258. setRelay(gun_index,ON);
  5259. }
  5260. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) || (ShmCharger->gun_info[gun_index].targetCurrent == 0))
  5261. {
  5262. ocpp_set_unlocker_req(gun_index, OFF);
  5263. presentChargedEnergyClear(gun_index);
  5264. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  5265. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  5266. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  5267. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  5268. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  5269. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  5270. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5271. else
  5272. {
  5273. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5274. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  5275. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  5276. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  5277. }
  5278. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5279. ocpp_copy_userid_to_starttransaction(gun_index);
  5280. ocpp_set_starttransaction_req(gun_index, ON);
  5281. refreshStartTimer(&startChargingTime[gun_index]);
  5282. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5283. }
  5284. //EV READY CHECK
  5285. /*
  5286. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_EV_READY) && (ShmCharger->gun_info[gun_index].isEvReady2StateE == OFF))
  5287. {
  5288. if(ShmCharger->gun_info[gun_index].isDoEvReadyOnce == OFF)
  5289. {
  5290. DEBUG_INFO("EV READY STATE E 4sec.\n");
  5291. ShmCharger->gun_info[gun_index].isEvReady2StateE = ON;
  5292. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_E;
  5293. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5294. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_E;
  5295. }
  5296. }
  5297. if(ShmCharger->gun_info[gun_index].isEvReady2StateE == ON)
  5298. {
  5299. switch(ShmCharger->gun_info[gun_index].evReadyState)
  5300. {
  5301. case EV_READY_STAT_E:
  5302. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  5303. {
  5304. sleep(4);
  5305. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5306. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5307. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_STAT_C;
  5308. }
  5309. break;
  5310. case EV_READY_STAT_C:
  5311. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  5312. {
  5313. usleep(500000);
  5314. DEBUG_INFO("EV READY Determine max charging current to MCU.\n");
  5315. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  5316. {
  5317. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  5318. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  5319. {
  5320. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  5321. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5322. }
  5323. }
  5324. else
  5325. {
  5326. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  5327. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  5328. {
  5329. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  5330. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5331. }
  5332. }
  5333. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5334. ShmCharger->gun_info[gun_index].evReadyState = EV_READY_SET_MAX_CURRENT;
  5335. }
  5336. break;
  5337. case EV_READY_SET_MAX_CURRENT:
  5338. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  5339. {
  5340. ShmCharger->gun_info[gun_index].isEvReady2StateE = OFF;
  5341. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = ON;
  5342. }
  5343. break;
  5344. default:
  5345. break;
  5346. }
  5347. }*/
  5348. break;
  5349. case HANDSHAKE_HLC_MODE:
  5350. if(ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE)//powerDelivery
  5351. {
  5352. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  5353. {
  5354. setRelay(gun_index,ON);
  5355. ShmCharger->gun_info[gun_index].isCCSStartTransation = ON;
  5356. DEBUG_INFO("HANDSHAKE_HLC_MODE:setRelay ON\n");
  5357. }
  5358. }
  5359. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) &&
  5360. (ShmCharger->gun_info[gun_index].isCCSStartTransation == ON))
  5361. {
  5362. DEBUG_INFO("HANDSHAKE_HLC_MODE:Relay ON done\n");
  5363. ocpp_set_unlocker_req(gun_index, OFF);
  5364. presentChargedEnergyClear(gun_index);
  5365. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  5366. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  5367. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  5368. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  5369. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  5370. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  5371. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5372. else
  5373. {
  5374. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5375. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  5376. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  5377. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  5378. }
  5379. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5380. ocpp_copy_userid_to_starttransaction(gun_index);
  5381. ocpp_set_starttransaction_req(gun_index, ON);
  5382. ShmCharger->gun_info[gun_index].isCCSStartTransation = OFF;
  5383. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5384. DEBUG_INFO("set SYS_MODE_CHARGING\n");
  5385. refreshStartTimer(&startChargingTime[gun_index]);
  5386. refreshStartTimer(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  5387. }
  5388. //120 sec timeout
  5389. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
  5390. {
  5391. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  5392. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5)
  5393. {
  5394. DEBUG_INFO("CCS 120 secs handshake timeout, change to BS Mode...\n");
  5395. //Cancel CCS task negotiating
  5396. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5397. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5398. //ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5399. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  5400. {
  5401. DEBUG_INFO("Wait CCS give up negotiagting.\n");
  5402. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_F;
  5403. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5404. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5405. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5406. }
  5407. }
  5408. }
  5409. break;
  5410. default:
  5411. break;
  5412. }
  5413. }
  5414. else if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode)
  5415. {
  5416. DEBUG_INFO("Enter Socket-E Mode charging.\n");
  5417. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_SOCKETE;
  5418. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  5419. setRelay(gun_index,ON);
  5420. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5421. ocpp_copy_userid_to_starttransaction(gun_index);
  5422. ocpp_set_starttransaction_req(gun_index, ON);
  5423. refreshStartTimer(&startChargingTime[gun_index]);
  5424. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5425. }
  5426. else if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  5427. {
  5428. setRequest(gun_index, ON);
  5429. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5430. }
  5431. // Unplug charging gun to Idle mode
  5432. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  5433. {
  5434. DEBUG_INFO("Charging gun is plugged before.\n");
  5435. //Cancel CCS task negotiating
  5436. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5437. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5438. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5439. }
  5440. // Use RFID card to stop handshaking
  5441. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchPresentUser(gun_index))
  5442. {
  5443. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  5444. //Cancel CCS task negotiating
  5445. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5446. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5447. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5448. }
  5449. else
  5450. {
  5451. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5452. }
  5453. checkHandshakeCountdown(gun_index);
  5454. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  5455. {
  5456. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  5457. if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5))
  5458. {
  5459. DEBUG_INFO("Handshaking timeout...\n");
  5460. //Cancel CCS task negotiating
  5461. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5462. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5463. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5464. }
  5465. }
  5466. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5)
  5467. {
  5468. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5469. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5470. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  5471. setChargerMode(gun_index, SYS_MODE_IDLE);
  5472. }
  5473. break;
  5474. case SYS_MODE_CHARGING:
  5475. if(isModeChange(gun_index))
  5476. {
  5477. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5478. //ftime(&startChargingTime[gun_index]);
  5479. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  5480. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5481. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  5482. ocpp_set_auth_req(OFF);
  5483. ocpp_reset_smartcharging_profileId(gun_index);
  5484. ocpp_set_profile_req(gun_index, ON);
  5485. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5486. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5487. }
  5488. //if time up, clear CCS MSG count
  5489. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  5490. (getDiffSecNow(startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
  5491. {
  5492. refreshStartTimer(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  5493. /*if(ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat > 0)
  5494. {
  5495. ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat = 0;
  5496. }
  5497. else
  5498. {
  5499. DEBUG_INFO("CCS could not get MSG from car.\n");
  5500. //setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5501. //setRelay(gun_index, OFF);
  5502. }*/
  5503. }
  5504. // If unplug the connector during a session, the system must stop charging and stop session.
  5505. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS) || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC))
  5506. {
  5507. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A))
  5508. {
  5509. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  5510. DEBUG_INFO("The connector was unplugged under charging mode. [CHARGING_MODE_BS / CHARGING_MODE_HLC]. \n");
  5511. }
  5512. }
  5513. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5514. {
  5515. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn != ON)
  5516. {
  5517. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  5518. DEBUG_INFO("The socket e was unplugged under charging mode. [CHARGING_MODE_SOCKETE]. \n");
  5519. }
  5520. }
  5521. else
  5522. {}
  5523. if((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES) ||
  5524. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  5525. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  5526. ocpp_get_remotestop(gun_index) ||
  5527. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  5528. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  5529. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) ||
  5530. ocpp_get_reset_req() ||
  5531. ocpp_get_unlocker_req(gun_index) ||
  5532. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  5533. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)) ||
  5534. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE)) ||
  5535. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE)) ||
  5536. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && ((49 <= ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus) && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus <= 255)))
  5537. )
  5538. {
  5539. //printf("EVChargeProgress=0x%x, PresentMsgFlowStatus=0x%x\n",ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress,ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus);
  5540. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5541. }
  5542. else
  5543. {
  5544. // Charging session info calculation
  5545. setLedMotion(gun_index,LED_ACTION_CHARGING);
  5546. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount==1)
  5547. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5548. else
  5549. {
  5550. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0);
  5551. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0);
  5552. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0);
  5553. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
  5554. }
  5555. refreshStartTimer(&endChargingTime[gun_index]);
  5556. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  5557. presentChargedEnergyUpdate(gun_index);
  5558. // Response StartTransactionConf
  5559. ocpp_set_starttransaction_conf(gun_index, OFF);
  5560. // Charging profile preparation
  5561. if((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  5562. {
  5563. if(!ocpp_get_profile_conf(gun_index))
  5564. {
  5565. ocpp_set_profile_req(gun_index, ON);
  5566. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5567. }
  5568. else
  5569. {
  5570. ocpp_set_profile_conf(gun_index, OFF);
  5571. }
  5572. }
  5573. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  5574. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  5575. // Charging session target current check if OCPP disconnect and power sharing server connected
  5576. if(!ocpp_get_connection_status() && ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing)
  5577. {
  5578. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  5579. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent;
  5580. else
  5581. {
  5582. if(ShmCharger->gun_info[gun_index].targetCurrent != 0)
  5583. DEBUG_WARN("Disconnect from power sharing server, target current set to 0.\n");
  5584. ShmCharger->gun_info[gun_index].targetCurrent = 0;
  5585. }
  5586. }
  5587. // Charging session local limit condition check
  5588. if(ocpp_get_connection_status())
  5589. {
  5590. // On-line max condition check
  5591. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  5592. {
  5593. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5594. {
  5595. setRelay(gun_index, OFF);
  5596. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5597. }
  5598. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5599. {
  5600. setRelay(gun_index, OFF);
  5601. }
  5602. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5603. {
  5604. setRelay(gun_index, OFF);
  5605. }
  5606. else
  5607. {}
  5608. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5609. DEBUG_INFO("Connector-%d charging duration(%d) already over max duration(%d) in second.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  5610. }
  5611. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  5612. {
  5613. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5614. {
  5615. setRelay(gun_index, OFF);
  5616. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5617. }
  5618. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5619. {
  5620. setRelay(gun_index, OFF);
  5621. }
  5622. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5623. {
  5624. setRelay(gun_index, OFF);
  5625. }
  5626. else
  5627. {}
  5628. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5629. DEBUG_INFO("Connector-%d charging energy(%.2f) already over max energy(%.2f) in KWH.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy));
  5630. }
  5631. else
  5632. {
  5633. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5634. {
  5635. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5636. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  5637. setRelay(gun_index, ON);
  5638. else
  5639. setRelay(gun_index, OFF);
  5640. }
  5641. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5642. {
  5643. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  5644. setRelay(gun_index, ON);
  5645. }
  5646. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5647. {
  5648. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  5649. setRelay(gun_index, ON);
  5650. else
  5651. setRelay(gun_index, OFF);
  5652. }
  5653. else
  5654. {}
  5655. }
  5656. }
  5657. else
  5658. {
  5659. // Off-line max condition check
  5660. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  5661. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  5662. {
  5663. if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))))
  5664. {
  5665. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5666. {
  5667. setRelay(gun_index, OFF);
  5668. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5669. }
  5670. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5671. {
  5672. setRelay(gun_index, OFF);
  5673. }
  5674. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5675. {
  5676. setRelay(gun_index, OFF);
  5677. }
  5678. else
  5679. {}
  5680. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5681. DEBUG_INFO("Connector-%d charging duration(%d) already over off-line max duration(%d) in second.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  5682. }
  5683. else if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
  5684. {
  5685. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5686. {
  5687. setRelay(gun_index, OFF);
  5688. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  5689. }
  5690. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5691. {
  5692. setRelay(gun_index, OFF);
  5693. }
  5694. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5695. {
  5696. setRelay(gun_index, OFF);
  5697. }
  5698. else
  5699. {}
  5700. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5701. DEBUG_INFO("Connector-%d charging energy(%.2f) already over off-line max energy(%.2f) in KWH.\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy));
  5702. }
  5703. else
  5704. {
  5705. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5706. {
  5707. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5708. if(ShmCharger->gun_info[gun_index].targetCurrent>0)
  5709. setRelay(gun_index, ON);
  5710. else
  5711. setRelay(gun_index, OFF);
  5712. }
  5713. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5714. {
  5715. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  5716. setRelay(gun_index, ON);
  5717. }
  5718. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5719. {
  5720. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  5721. setRelay(gun_index, ON);
  5722. else
  5723. setRelay(gun_index, OFF);
  5724. }
  5725. else
  5726. {}
  5727. }
  5728. }
  5729. else
  5730. {
  5731. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state > 0)
  5732. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  5733. setRelay(gun_index, OFF);
  5734. }
  5735. }
  5736. // Debug information
  5737. if((getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
  5738. {
  5739. /*
  5740. DEBUG_INFO("==============================================================\n");
  5741. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  5742. DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  5743. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  5744. DEBUG_INFO("==============================================================\n");
  5745. */
  5746. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  5747. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  5748. DB_Update_Record_Buf(localDb, gun_index);
  5749. }
  5750. }
  5751. break;
  5752. case SYS_MODE_TERMINATING:
  5753. if(isModeChange(gun_index))
  5754. {
  5755. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  5756. {
  5757. setLedMotion(gun_index, LED_ACTION_STOP);
  5758. }
  5759. else
  5760. {}
  5761. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  5762. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5763. if(ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)
  5764. setRelay(gun_index, OFF);
  5765. }
  5766. refreshStartTimer(&endChargingTime[gun_index]);
  5767. //if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  5768. //{
  5769. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  5770. //}
  5771. // End authorize pass
  5772. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  5773. ((ShmCharger->gun_info[gun_index].rfidReq == ON) && ocpp_get_auth_result(YES, gun_index)) ||
  5774. (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) && (ShmCharger->gun_info[gun_index].rfidReq == ON)) ||
  5775. (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (ShmCharger->gun_info[gun_index].rfidReq == ON)) ||
  5776. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  5777. ocpp_get_remotestop(gun_index) ||
  5778. (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index)) ||
  5779. ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)) && ocpp_get_StopTransactionOnEVSideDisconnect()) ||
  5780. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && ((!ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn) || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))) ||
  5781. ocpp_get_reset_req() ||
  5782. ocpp_get_unlocker_req(gun_index) ||
  5783. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON) ||
  5784. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
  5785. {
  5786. setLedMotion(gun_index, LED_ACTION_STOP);
  5787. if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  5788. {
  5789. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5790. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5791. sleep(1);
  5792. checkStopReason(gun_index);
  5793. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5794. }
  5795. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5796. {
  5797. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5798. //Cancel CCS task negotiating
  5799. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5800. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5801. setRelay(gun_index,OFF);
  5802. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5803. }
  5804. else
  5805. {
  5806. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5807. }
  5808. }
  5809. else
  5810. {
  5811. // If the charger stops charging, the led should be stoped blink
  5812. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  5813. {
  5814. setLedMotion(gun_index, LED_ACTION_STOP);
  5815. }
  5816. // Charging profile preparation
  5817. if((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  5818. {
  5819. if(!ocpp_get_profile_conf(gun_index))
  5820. {
  5821. ocpp_set_profile_req(gun_index, ON);
  5822. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5823. }
  5824. else
  5825. {
  5826. ocpp_set_profile_conf(gun_index, OFF);
  5827. }
  5828. }
  5829. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  5830. checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
  5831. setRelay(gun_index, OFF);
  5832. // Debug information
  5833. if((getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
  5834. {
  5835. /*
  5836. DEBUG_INFO("=============================================================\n");
  5837. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  5838. DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  5839. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  5840. DEBUG_INFO("=============================================================\n");
  5841. */
  5842. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  5843. }
  5844. if(!ocpp_get_auth_result(YES, gun_index))
  5845. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5846. if((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES) &&
  5847. (((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn)) &&
  5848. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  5849. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  5850. !ocpp_get_remotestop(gun_index) &&
  5851. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  5852. !ocpp_get_reset_req() &&
  5853. ocpp_get_starttransaction_result(gun_index) &&
  5854. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  5855. !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  5856. !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))) &&
  5857. !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))) &&
  5858. (((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE)) || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS) || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE))
  5859. )
  5860. {
  5861. sleep(1);
  5862. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5863. }
  5864. }
  5865. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
  5866. (ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) &&
  5867. (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
  5868. {
  5869. DEBUG_INFO("Set PWM duty 100%% go to SYS_MODE_TERMINATING.\n");
  5870. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5871. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5872. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  5873. sleep(1);
  5874. checkStopReason(gun_index);
  5875. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  5876. }
  5877. break;
  5878. case SYS_MODE_COMPLETE:
  5879. if(isModeChange(gun_index))
  5880. {
  5881. setLedMotion(gun_index, LED_ACTION_STOP);
  5882. sleep(6);
  5883. setRelay(gun_index, OFF);
  5884. setRequest(gun_index, OFF);
  5885. sleep(13);
  5886. }
  5887. if(((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)) ||
  5888. ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE) && (!ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn)) ||
  5889. ocpp_get_reset_req())
  5890. {
  5891. sleep(2);
  5892. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5893. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  5894. ocpp_set_remotestop(gun_index, OFF);
  5895. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  5896. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5897. DB_Insert_Record(localDb, gun_index);
  5898. setChargerMode(gun_index, SYS_MODE_IDLE);
  5899. }
  5900. break;
  5901. case SYS_MODE_ALARM:
  5902. setLedMotion(gun_index,LED_ACTION_ALARM);
  5903. setRelay(gun_index, OFF);
  5904. if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS) || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC))
  5905. {
  5906. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A))
  5907. {
  5908. setRequest(gun_index,OFF);
  5909. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  5910. }
  5911. }
  5912. else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5913. {
  5914. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn != ON)
  5915. {
  5916. setRequest(gun_index,OFF);
  5917. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  5918. }
  5919. }
  5920. else
  5921. {}
  5922. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0) && !ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
  5923. {
  5924. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  5925. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  5926. {
  5927. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  5928. }
  5929. else
  5930. {
  5931. setChargerMode(gun_index, SYS_MODE_IDLE);
  5932. }
  5933. }
  5934. break;
  5935. case SYS_MODE_FAULT:
  5936. if(isModeChange(gun_index))
  5937. {}
  5938. break;
  5939. case SYS_MODE_MAINTAIN:
  5940. if(isModeChange(gun_index))
  5941. {
  5942. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5943. }
  5944. break;
  5945. case SYS_MODE_UPDATE:
  5946. if(isModeChange(gun_index))
  5947. {
  5948. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  5949. }
  5950. //======================================
  5951. // Check local upgrade firmware request
  5952. //======================================
  5953. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  5954. {
  5955. DEBUG_INFO("Firmware local upgrading...\n");
  5956. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  5957. int value = upgrade_check();
  5958. DEBUG_INFO("Local update Value: %s... \n",((value == PASS)?"Pass": "Fail"));
  5959. if(value == PASS)
  5960. {
  5961. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  5962. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  5963. ShmCharger->isUpdateSuccess = YES;
  5964. DEBUG_INFO("Local update success...\n");
  5965. }
  5966. else
  5967. {
  5968. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  5969. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  5970. ShmCharger->isUpdateSuccess = NO;
  5971. DEBUG_INFO("Local update unsuccess...\n");
  5972. }
  5973. }
  5974. else if(ocpp_get_update_firmware_req())
  5975. {
  5976. //======================================
  5977. // Check remote upgrade firmware request
  5978. //======================================
  5979. checkRemoteUpgradeStatus();
  5980. }
  5981. else
  5982. {
  5983. //======================================
  5984. // Upgrade complete reboot system
  5985. //======================================
  5986. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES) && !ShmCharger->isUpgradeLcmReq)
  5987. {
  5988. if(ShmCharger->isUpdateSuccess == YES)
  5989. {
  5990. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  5991. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5992. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  5993. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  5994. DEBUG_WARN("Firmware upgrade success.\n");
  5995. sleep(5);
  5996. DEBUG_INFO("Firmware upgraded, reboot...\n");
  5997. system("reboot -f");
  5998. sleep(5);
  5999. system("reboot -f");
  6000. }
  6001. else
  6002. {
  6003. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  6004. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6005. sprintf((char*)ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  6006. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6007. DEBUG_WARN("Firmware upgrade fail.\n");
  6008. sleep(5);
  6009. system("rm -rvf /mnt/* ");
  6010. close(wtdFd);
  6011. system("/usr/bin/run_evse_restart.sh");
  6012. }
  6013. }
  6014. }
  6015. break;
  6016. case SYS_MODE_RESERVATION:
  6017. if(isModeChange(gun_index))
  6018. {}
  6019. setLedMotion(gun_index,LED_ACTION_RESERVATION_MODE);
  6020. if(isReservationExpired(gun_index))
  6021. {
  6022. DEBUG_INFO("Reservation: Time's up...\n");
  6023. setChargerMode(gun_index, SYS_MODE_IDLE);
  6024. }
  6025. else if(ocpp_get_cancelreservation_req(gun_index))
  6026. {
  6027. DEBUG_INFO("Reservation: Cancel reservation...\n");
  6028. setChargerMode(gun_index, SYS_MODE_IDLE);
  6029. ocpp_set_cancelreservation_req(gun_index, OFF);
  6030. }
  6031. else
  6032. {
  6033. // Check is there RFID or back end request start
  6034. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  6035. ocpp_get_remotestart(gun_index))
  6036. {
  6037. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  6038. {
  6039. if(ocpp_compare_reserve_id_with_user(gun_index))
  6040. {
  6041. DEBUG_INFO("Start Method in reservation : RFID...\n");
  6042. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  6043. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  6044. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_ISO14443;
  6045. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  6046. }
  6047. else
  6048. {
  6049. DEBUG_INFO("It's not reserve user id.\n");
  6050. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  6051. }
  6052. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  6053. }
  6054. else if(ocpp_get_remotestart(gun_index))
  6055. {
  6056. if(ocpp_compare_reserve_id_with_remote_user(gun_index))
  6057. {
  6058. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  6059. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  6060. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartIdType = IdTokenType_Central;
  6061. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  6062. }
  6063. ocpp_set_remotestop(gun_index, OFF);
  6064. }
  6065. }
  6066. }
  6067. break;
  6068. case SYS_MODE_BOOKING:
  6069. if(isModeChange(gun_index))
  6070. {}
  6071. break;
  6072. case SYS_MODE_DEBUG:
  6073. if(isModeChange(gun_index))
  6074. {
  6075. setLedMotion(gun_index,LED_ACTION_DEBUG);
  6076. }
  6077. break;
  6078. }
  6079. }
  6080. // System watch dog reset
  6081. write(wtdFd, "a", 1);
  6082. usleep(50000);
  6083. }
  6084. return FAIL;
  6085. }