main.c 242 KB

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