main.c 229 KB

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