main.c 249 KB

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