main.c 274 KB

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