main.c 257 KB

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