main.c 261 KB

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