main.c 261 KB

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