main.c 282 KB

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