main.c 283 KB

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