main.c 275 KB

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