main.c 227 KB

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