main.c 279 KB

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