main.c 228 KB

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