main.c 226 KB

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