main.c 246 KB

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