main.c 202 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293
  1. /*
  2. * main.c
  3. *
  4. * Created on: 2020年9月10日
  5. * Author: Wendell
  6. */
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/time.h>
  10. #include <sys/timeb.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/mman.h>
  17. #include <linux/wireless.h>
  18. #include <arpa/inet.h>
  19. #include <netinet/in.h>
  20. #include <unistd.h>
  21. #include <stdarg.h>
  22. #include <stdio.h> /*標準輸入輸出定義*/
  23. #include <stdlib.h> /*標準函數庫定義*/
  24. #include <unistd.h> /*Unix 標準函數定義*/
  25. #include <fcntl.h> /*檔控制定義*/
  26. #include <termios.h> /*PPSIX 終端控制定義*/
  27. #include <errno.h> /*錯誤號定義*/
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <math.h>
  35. #include "../../define.h"
  36. #include "Config.h"
  37. #include <stdbool.h>
  38. #include <dirent.h>
  39. #include "timeout.h"
  40. #include "Module_EvComm.h"
  41. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  42. #define PASS 1
  43. #define FAIL -1
  44. #define BUFFER_SIZE 128
  45. #define YES 1
  46. #define NO 0
  47. #define NORMAL 0
  48. #define ABNORMAL 1
  49. #define EQUAL 0
  50. #define BTN_RELEASE 0
  51. #define BTN_PRESS 1
  52. #define MAX_BUF 64
  53. #define MtdBlockSize 0x600000
  54. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  55. #define UPGRADE_FAN 0x02
  56. #define UPGRADE_RB 0x03
  57. #define UPGRADE_PRI 0x04
  58. #define UPGRADE_AC 0x05
  59. #define UPGRADE_LED 0x06
  60. #define SYSTEM_MIN_VOL 150
  61. #define MIN_OUTPUT_CUR 0
  62. #define AC_OUTPUT_VOL 220
  63. #define NO_DEFINE 255
  64. #define DEFAULT_AC_INDEX 2
  65. #define PSU_MIN_CUR 100
  66. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  67. #define uSEC_VAL 1000000
  68. #define SELFTEST_TIMEOUT 45
  69. #define AUTHORIZE_TIMEOUT 30
  70. #define AUTHORIZE_COMP_TIMEOUT 1
  71. #define AUTHORIZE_FAIL_TIMEOUT 3
  72. #define AUTHORIZE_STOP_TIMEOUT 30
  73. #define RETURN_TO_CHARGING_PAGE 30
  74. #define GUN_PREPARE_TIMEOUT 30
  75. #define GUN_EV_WAIT_TIMEOUT 120
  76. #define GUN_EVSE_WAIT_TIMEOUT 60
  77. #define GUN_COMP_WAIT_TIMEOUT 10
  78. #define GUN_PRECHARGING_TIMEOUT 60
  79. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  80. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  81. int whileLoopTime = 10000; // 10 ms
  82. int wtdFd = -1;
  83. byte _authorizeIndex = NO_DEFINE;
  84. bool IsAuthorizingMode();
  85. void ClearAuthorizedFlag();
  86. bool isDetectPlugin();
  87. void ClearDetectPluginFlag();
  88. int mystrcmp(unsigned char *p1, unsigned char *p2);
  89. long long DiffTimebWithNow(struct timeb ST);
  90. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  91. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  92. void ChargingTerminalProcess(byte gunIndex);
  93. void ChkPrimaryStatus();
  94. void StartSystemTimeoutDet(unsigned char flag);
  95. void StopSystemTimeoutDet();
  96. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag);
  97. void StopGunInfoTimeoutDet(unsigned char gunIndex);
  98. int StoreLogMsg_1(const char *fmt, ...);
  99. unsigned long GetTimeoutValue(struct timeval _sour_time);
  100. void gpio_set_value(unsigned int gpio, unsigned int value);
  101. void PRINTF_FUNC(char *string, ...);
  102. void ChangeGunSelectByIndex(byte sel);
  103. void InformOcppErrOccur(byte codeType);
  104. void RecordAlarmCode(byte gunIndex, char *code);
  105. void RecordWarningCode(byte gunIndex, char *code);
  106. void ReleaseWarningCodeByString(byte gunIndex, char *code);
  107. void ReleaseAlarmCode(byte gunIndex);
  108. int DB_Open(sqlite3 *db);
  109. int DB_Insert_Record(sqlite3 *db, int gun_index);
  110. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  111. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  112. void InitialDHCP();
  113. void InitialDispenserDhcpServerConfig(void);
  114. void StartDispenserDhcpServer(void);
  115. void _SelfTestTimeout(void);
  116. void CheckConnectionTimeout(void);
  117. #define DEBUG_INFO_MSG(format, args...) PRINTF_FUNC("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  118. #define DEBUG_WARN_MSG(format, args...) PRINTF_FUNC("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  119. #define DEBUG_ERROR_MSG(format, args...) PRINTF_FUNC("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  120. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  121. struct StatusCodeData *ShmStatusCodeData;
  122. struct PsuData *ShmPsuData;
  123. struct CHAdeMOData *ShmCHAdeMOData;
  124. struct GBTData *ShmGBTData;
  125. struct CcsData *ShmCcsData;
  126. struct PrimaryMcuData *ShmPrimaryMcuData;
  127. struct FanModuleData *ShmFanModuleData;
  128. struct RelayModuleData *ShmRelayModuleData[2];
  129. struct LedModuleData *ShmLedModuleData;
  130. struct OCPP16Data *ShmOCPP16Data;
  131. struct ChargingInfoData *chargingInfo[CONNECTOR_QUANTITY];
  132. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  133. struct timeb startChargingTime[CONNECTOR_QUANTITY];
  134. struct timeb endChargingTime[CONNECTOR_QUANTITY];
  135. struct timeval _ConnectorAuthorizing_Time[CONNECTOR_QUANTITY];
  136. struct timeval _DispenserAuthorizing_Time[CONNECTOR_QUANTITY];
  137. int chargingTime[CONNECTOR_QUANTITY];
  138. // for initial index to check EV board type is correct
  139. byte _gunIndex = 0;
  140. byte _acgunIndex = 0;
  141. byte _chademoIndex = 0;
  142. byte _ccsIndex = 0;
  143. byte _gb_Index = 0;
  144. byte _ac_Index = 0;
  145. //byte bd0_1_status = 0;
  146. //byte bd0_2_status = 0;
  147. //byte bd1_1_status = 0;
  148. //byte bd1_2_status = 0;
  149. bool isCardScan = false;
  150. bool isModelNameMatch = true;
  151. //int rfidFd = -1;
  152. //char* rfidPortName = "/dev/ttyS2";
  153. char* fwVersion = "D0.04.00.0000.00";
  154. sqlite3 *localDb;
  155. bool isDb_ready;
  156. //================================================
  157. // initial can-bus
  158. //================================================
  159. int InitCanBus()
  160. {
  161. int s0,nbytes;
  162. struct timeval tv;
  163. struct ifreq ifr0;
  164. struct sockaddr_can addr0;
  165. system("/sbin/ip link set can0 down");
  166. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  167. system("/sbin/ip link set can0 up");
  168. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  169. tv.tv_sec = 0;
  170. tv.tv_usec = 10000;
  171. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  172. {
  173. #ifdef SystemLogMessage
  174. DEBUG_ERROR_MSG("Set SO_RCVTIMEO NG");
  175. #endif
  176. }
  177. nbytes=40960;
  178. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  179. {
  180. #ifdef SystemLogMessage
  181. DEBUG_ERROR_MSG("Set SO_RCVBUF NG");
  182. #endif
  183. }
  184. nbytes=40960;
  185. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  186. {
  187. #ifdef SystemLogMessage
  188. DEBUG_ERROR_MSG("Set SO_SNDBUF NG");
  189. #endif
  190. }
  191. strcpy(ifr0.ifr_name, "can0" );
  192. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  193. addr0.can_family = AF_CAN;
  194. addr0.can_ifindex = ifr0.ifr_ifindex;
  195. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  196. return s0;
  197. }
  198. //================================================
  199. // initial uart port
  200. //================================================
  201. char *_priPortName = "/dev/ttyS1";
  202. char *_485PortName = "/dev/ttyS5";
  203. int InitComPort(byte target)
  204. {
  205. int fd;
  206. struct termios tios;
  207. if(target == UPGRADE_PRI)
  208. fd = open(_priPortName, O_RDWR);
  209. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  210. fd = open(_485PortName, O_RDWR);
  211. if(fd<=0)
  212. {
  213. #ifdef SystemLogMessage
  214. DEBUG_ERROR_MSG("open 407 Communication port NG \n");
  215. #endif
  216. return -1;
  217. }
  218. ioctl (fd, TCGETS, &tios);
  219. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  220. tios.c_lflag = 0;
  221. tios.c_iflag = 0;
  222. tios.c_oflag = 0;
  223. tios.c_cc[VMIN]=0;
  224. tios.c_cc[VTIME]=(unsigned char)1;
  225. tios.c_lflag=0;
  226. tcflush(fd, TCIFLUSH);
  227. ioctl (fd, TCSETS, &tios);
  228. return fd;
  229. }
  230. //=================================
  231. // Common routine
  232. //=================================
  233. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  234. {
  235. strncpy(dest, src + start, cnt);
  236. dest[cnt] = 0;
  237. }
  238. int InitWatchDog()
  239. {
  240. int fd;
  241. system("/usr/bin/fuser -k /dev/watchdog");
  242. sleep(1);
  243. system("echo V > /dev/watchdog");
  244. sleep(1);
  245. fd=open("/dev/watchdog", O_RDWR);
  246. if(fd<=0)
  247. {
  248. DEBUG_ERROR_MSG("System watch dog initial fail.\r\n");
  249. }
  250. return fd;
  251. }
  252. int StoreLogMsg_1(const char *fmt, ...)
  253. {
  254. char Buf[4096+256];
  255. char buffer[4096];
  256. va_list args;
  257. struct timeb SeqEndTime;
  258. struct tm *tm;
  259. va_start(args, fmt);
  260. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  261. va_end(args);
  262. memset(Buf,0,sizeof(Buf));
  263. ftime(&SeqEndTime);
  264. SeqEndTime.time = time(NULL);
  265. tm=localtime(&SeqEndTime.time);
  266. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  267. {
  268. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  269. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  270. printf("%s \n", Buf);
  271. }
  272. else
  273. {
  274. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  275. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  276. buffer,
  277. tm->tm_year+1900,tm->tm_mon+1);
  278. system(Buf);
  279. }
  280. return rc;
  281. }
  282. unsigned long GetTimeoutValue(struct timeval _sour_time)
  283. {
  284. struct timeval _end_time;
  285. gettimeofday(&_end_time, NULL);
  286. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  287. }
  288. int mystrcmp(unsigned char *p1, unsigned char *p2)
  289. {
  290. while(*p1==*p2)
  291. {
  292. if(*p1=='\0' || *p2=='\0')
  293. break;
  294. p1++;
  295. p2++;
  296. }
  297. if(*p1=='\0' && *p2=='\0')
  298. return(PASS);
  299. else
  300. return(FAIL);
  301. }
  302. int DiffTimeb(struct timeb ST, struct timeb ET)
  303. {
  304. //return milli-second
  305. unsigned int StartTime,StopTime;
  306. StartTime=(unsigned int)ST.time;
  307. StopTime=(unsigned int)ET.time;
  308. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  309. return (StopTime-StartTime);
  310. }
  311. bool CheckTimeOut(struct timeb ST)
  312. {
  313. struct timeb ET;
  314. unsigned int StartTime, StopTime;
  315. ftime(&ET);
  316. StartTime = (unsigned int) ST.time;
  317. StopTime = (unsigned int) ET.time;
  318. return (StopTime > StartTime)? YES : NO;
  319. }
  320. void setChargerMode(byte gun_index, byte mode)
  321. {
  322. chargingInfo[gun_index]->SystemStatus = mode;
  323. }
  324. void PRINTF_FUNC(char *string, ...)
  325. {
  326. va_list args;
  327. char buffer[4096];
  328. va_start(args, string);
  329. vsnprintf(buffer, sizeof(buffer), string, args);
  330. va_end(args);
  331. printf("%s \n", buffer);
  332. }
  333. long long DiffTimebWithNow(struct timeb ST)
  334. {
  335. //return milli-second
  336. struct timeb ET;
  337. long long StartTime,StopTime;
  338. ftime(&ET);
  339. StartTime=(long long)ST.time;
  340. StopTime=(long long)ET.time;
  341. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  342. }
  343. /*
  344. //==========================================
  345. // Check interface status
  346. //==========================================
  347. int isInterfaceUp(const char *interface)
  348. {
  349. int result = FAIL;
  350. FILE *fp;
  351. char cmd[256];
  352. char buf[512];
  353. strcpy(cmd, "ifconfig");
  354. fp = popen(cmd, "r");
  355. if(fp != NULL)
  356. {
  357. while(fgets(buf, sizeof(buf), fp) != NULL)
  358. {
  359. if(strstr(buf, interface) > 0)
  360. {
  361. result = PASS;
  362. }
  363. }
  364. }
  365. pclose(fp);
  366. return result;
  367. }
  368. */
  369. //=================================
  370. // Create all share memory
  371. //=================================
  372. int CreateShareMemory()
  373. {
  374. int MeterSMId;
  375. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  376. {
  377. #ifdef SystemLogMessage
  378. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG \n");
  379. #endif
  380. return 0;
  381. }
  382. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  383. {
  384. #ifdef SystemLogMessage
  385. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG \n");
  386. #endif
  387. return 0;
  388. }
  389. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  390. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  391. {
  392. #ifdef SystemLogMessage
  393. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmStatusCodeData NG \n");
  394. #endif
  395. return 0;
  396. }
  397. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  398. {
  399. #ifdef SystemLogMessage
  400. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmStatusCodeData NG \n");
  401. #endif
  402. return 0;
  403. }
  404. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  405. //creat ShmPsuData
  406. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  407. {
  408. #ifdef SystemLogMessage
  409. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPsuData NG \n");
  410. #endif
  411. return 0;
  412. }
  413. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  414. {
  415. #ifdef SystemLogMessage
  416. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPsuData NG \n");
  417. #endif
  418. return 0;
  419. }
  420. memset(ShmPsuData, 0, sizeof(struct PsuData));
  421. /*
  422. if(CHAdeMO_QUANTITY > 0)
  423. {
  424. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  425. {
  426. #ifdef SystemLogMessage
  427. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCHAdeMOData NG \n");
  428. #endif
  429. return 0;
  430. }
  431. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  432. {
  433. #ifdef SystemLogMessage
  434. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCHAdeMOData NG \n");
  435. #endif
  436. return 0;
  437. }
  438. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  439. }
  440. if(GB_QUANTITY > 0)
  441. {
  442. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  443. {
  444. #ifdef SystemLogMessage
  445. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmGBTData NG \n");
  446. #endif
  447. return 0;
  448. }
  449. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  450. {
  451. #ifdef SystemLogMessage
  452. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmGBTData NG \n");
  453. #endif
  454. return 0;
  455. }
  456. memset(ShmGBTData, 0, sizeof(struct GBTData));
  457. }
  458. //creat ShmCcsData
  459. if(CCS_QUANTITY > 0)
  460. {
  461. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  462. {
  463. #ifdef SystemLogMessage
  464. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmCcsData NG \n");
  465. #endif
  466. return 0;
  467. }
  468. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  469. {
  470. #ifdef SystemLogMessage
  471. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmCcsData NG \n");
  472. #endif
  473. return 0;
  474. }
  475. memset(ShmCcsData, 0, sizeof(struct CcsData));
  476. }
  477. */
  478. //creat ShmPrimaryMcuData
  479. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  480. {
  481. #ifdef SystemLogMessage
  482. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmPrimaryMcuData NG \n");
  483. #endif
  484. return 0;
  485. }
  486. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  487. {
  488. #ifdef SystemLogMessage
  489. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG \n");
  490. #endif
  491. return 0;
  492. }
  493. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  494. //creat ShmFanModuleData
  495. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  496. {
  497. #ifdef SystemLogMessage
  498. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmFanModuleData NG \n");
  499. #endif
  500. return 0;
  501. }
  502. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  503. {
  504. #ifdef SystemLogMessage
  505. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmFanModuleData NG \n");
  506. #endif
  507. return 0;
  508. }
  509. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  510. //creat ShmRelayModuleData
  511. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  512. {
  513. #ifdef SystemLogMessage
  514. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData[0] NG \n");
  515. #endif
  516. return 0;
  517. }
  518. else if ((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  519. {
  520. #ifdef SystemLogMessage
  521. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData[0] NG \n");
  522. #endif
  523. return 0;
  524. }
  525. memset(ShmRelayModuleData[0], 0, sizeof(struct RelayModuleData));
  526. // DO360 RC2
  527. if ((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  528. {
  529. #ifdef SystemLogMessage
  530. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmRelayModuleData[1] NG \n");
  531. #endif
  532. return 0;
  533. }
  534. else if ((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  535. {
  536. #ifdef SystemLogMessage
  537. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmRelayModuleData[1] NG \n");
  538. #endif
  539. return 0;
  540. }
  541. memset(ShmRelayModuleData[1], 0, sizeof(struct RelayModuleData));
  542. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  543. {
  544. #ifdef SystemLogMessage
  545. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmLedModuleData NG \n");
  546. #endif
  547. return 0;
  548. }
  549. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  550. {
  551. #ifdef SystemLogMessage
  552. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmLedModuleData NG \n");
  553. #endif
  554. return 0;
  555. }
  556. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  557. //creat ShmOCPP16Data
  558. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  559. {
  560. #ifdef SystemLogMessage
  561. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmget ShmOCPP16Data NG \n");
  562. #endif
  563. return 0;
  564. }
  565. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  566. {
  567. #ifdef SystemLogMessage
  568. DEBUG_ERROR_MSG("[main]CreatShareMemory:shmat ShmOCPP16Data NG \n");
  569. #endif
  570. return 0;
  571. }
  572. // memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  573. return 1;
  574. }
  575. //=================================
  576. // LCM Page
  577. //=================================
  578. void ChangeLcmByIndex(byte page_index)
  579. {
  580. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2 ||
  581. page_index == _LCM_COMPLETE || page_index == _LCM_FIX)
  582. {
  583. ShmSysConfigAndInfo->SysInfo.PageIndex = page_index;
  584. }
  585. }
  586. //======================================================
  587. // Peripheral initial
  588. //======================================================
  589. void InitGPIO()
  590. {
  591. /*****************0~3, 4 bank, bank x 32+ num*********************/
  592. /***************************************************************/
  593. /*************** GPIO 0 ***************************************/
  594. /***************************************************************/
  595. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/ // no use
  596. system("echo 22 > /sys/class/gpio/export");
  597. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  598. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/ // no use
  599. system("echo 23 > /sys/class/gpio/export");
  600. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  601. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/ // no use
  602. system("echo 26 > /sys/class/gpio/export");
  603. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  604. system("echo 1 > /sys/class/gpio/gpio26/value");
  605. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/ // no use
  606. system("echo 27 > /sys/class/gpio/export");
  607. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  608. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  609. system("echo 29 > /sys/class/gpio/export");
  610. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  611. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/ // no use
  612. system("echo 19 > /sys/class/gpio/export");
  613. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  614. system("echo 1 > /sys/class/gpio/gpio19/value");
  615. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/ // no use
  616. system("echo 20 > /sys/class/gpio/export");
  617. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  618. /***************************************************************/
  619. /*************** GPIO 1 ***************************************/
  620. /***************************************************************/
  621. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/ // no use
  622. system("echo 44 > /sys/class/gpio/export");
  623. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  624. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/ // no use
  625. system("echo 45 > /sys/class/gpio/export");
  626. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  627. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/ // no use
  628. system("echo 46 > /sys/class/gpio/export");
  629. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  630. system("echo 0 > /sys/class/gpio/gpio46/value");
  631. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/ // no use
  632. system("echo 47 > /sys/class/gpio/export");
  633. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  634. /***************************************************************/
  635. /*************** GPIO 2 ***************************************/
  636. /***************************************************************/
  637. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  638. system("echo 89 > /sys/class/gpio/export");
  639. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  640. system("echo 1 > /sys/class/gpio/gpio89/value");
  641. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  642. system("echo 87 > /sys/class/gpio/export");
  643. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  644. system("echo 0 > /sys/class/gpio/gpio87/value");
  645. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/ // no use
  646. system("echo 88 > /sys/class/gpio/export");
  647. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  648. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/ // no use
  649. system("echo 86 > /sys/class/gpio/export");
  650. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  651. /***************************************************************/
  652. /*************** GPIO 3 ***************************************/
  653. /***************************************************************/
  654. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  655. system("echo 111 > /sys/class/gpio/export");
  656. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  657. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  658. system("echo 114 > /sys/class/gpio/export");
  659. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  660. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  661. system("echo 113 > /sys/class/gpio/export");
  662. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  663. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  664. system("echo 110 > /sys/class/gpio/export");
  665. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  666. system("echo 0 > /sys/class/gpio/gpio110/value");
  667. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/ // no use
  668. system("echo 115 > /sys/class/gpio/export");
  669. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  670. system("echo 0 > /sys/class/gpio/gpio115/value");
  671. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  672. system("echo 112 > /sys/class/gpio/export");
  673. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  674. system("echo 1 > /sys/class/gpio/gpio112/value");
  675. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/ // no use
  676. system("echo 116 > /sys/class/gpio/export");
  677. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  678. system("echo 0 > /sys/class/gpio/gpio116/value");
  679. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  680. system("echo 103 > /sys/class/gpio/export");
  681. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  682. system("echo 0 > /sys/class/gpio/gpio103/value");
  683. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  684. system("echo 104 > /sys/class/gpio/export");
  685. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  686. system("echo 0 > /sys/class/gpio/gpio104/value");
  687. #ifdef SystemLogMessage
  688. DEBUG_INFO_MSG("[main]InitGPIO: Initial GPIO OK");
  689. #endif
  690. }
  691. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  692. {
  693. int fd,wrd;
  694. unsigned char *buf;
  695. unsigned int ChkSum,ChkSumOrg;
  696. if((buf=malloc(MtdBlockSize))==NULL)
  697. {
  698. DEBUG_ERROR_MSG("malloc buffer NG,rebooting..\r\n");
  699. if(ShmStatusCodeData!=NULL)
  700. {
  701. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  702. }
  703. sleep(5);
  704. system("reboot -f");
  705. sleep(5);
  706. system("reboot -f");
  707. }
  708. memset(buf, 0, MtdBlockSize);
  709. //================================================
  710. // Load configuration from mtdblock10
  711. //================================================
  712. fd = open("/dev/mtdblock10", O_RDWR);
  713. if (fd < 0)
  714. {
  715. free(buf);
  716. DEBUG_ERROR_MSG("open mtdblock10 NG,rebooting..\r\n");
  717. if(ShmStatusCodeData!=NULL)
  718. {
  719. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  720. }
  721. sleep(5);
  722. system("reboot -f");
  723. sleep(5);
  724. system("reboot -f");
  725. }
  726. wrd=read(fd, buf, MtdBlockSize);
  727. close(fd);
  728. if(wrd<MtdBlockSize)
  729. {
  730. free(buf);
  731. DEBUG_ERROR_MSG("read SysConfigData data NG,rebooting..\r\n");
  732. if(ShmStatusCodeData!=NULL)
  733. {
  734. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  735. }
  736. sleep(5);
  737. system("reboot -f");
  738. sleep(5);
  739. system("reboot -f");
  740. }
  741. ChkSum=0;
  742. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  743. {
  744. ChkSum+=buf[wrd];
  745. }
  746. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  747. //================================================
  748. // Load configuration from mtdblock11
  749. //================================================
  750. if(ChkSum!=ChkSumOrg)
  751. {
  752. DEBUG_ERROR_MSG("Primary SysConfigData checksum NG, read backup\r\n");
  753. fd = open("/dev/mtdblock11", O_RDWR);
  754. if (fd < 0)
  755. {
  756. free(buf);
  757. DEBUG_ERROR_MSG("open mtdblock11 (backup) NG,rebooting..\r\n");
  758. if(ShmStatusCodeData!=NULL)
  759. {
  760. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  761. }
  762. sleep(5);
  763. system("reboot -f");
  764. sleep(5);
  765. system("reboot -f");
  766. }
  767. memset(buf, 0, MtdBlockSize);
  768. wrd=read(fd, buf,MtdBlockSize);
  769. close(fd);
  770. if(wrd<MtdBlockSize)
  771. {
  772. free(buf);
  773. DEBUG_ERROR_MSG("read backup SysConfigData data NG,rebooting..\r\n");
  774. if(ShmStatusCodeData!=NULL)
  775. {
  776. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  777. }
  778. sleep(5);
  779. system("reboot -f");
  780. sleep(5);
  781. system("reboot -f");
  782. }
  783. ChkSum=0;
  784. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  785. {
  786. ChkSum+=buf[wrd];
  787. }
  788. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  789. //================================================
  790. // Load configuration from mtdblock12 (Factory default)
  791. //================================================
  792. if(ChkSum!=ChkSumOrg)
  793. {
  794. DEBUG_ERROR_MSG("backup SysConfigData checksum NG, read Factory default\r\n");
  795. fd = open("/dev/mtdblock12", O_RDWR);
  796. if (fd < 0)
  797. {
  798. free(buf);
  799. DEBUG_ERROR_MSG("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  800. if(ShmStatusCodeData!=NULL)
  801. {
  802. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  803. }
  804. sleep(5);
  805. system("reboot -f");
  806. sleep(5);
  807. system("reboot -f");
  808. }
  809. memset(buf, 0, MtdBlockSize);
  810. wrd=read(fd, buf,MtdBlockSize);
  811. close(fd);
  812. if(wrd<MtdBlockSize)
  813. {
  814. free(buf);
  815. DEBUG_ERROR_MSG("read factory default SysConfigData data NG,rebooting..\r\n");
  816. if(ShmStatusCodeData!=NULL)
  817. {
  818. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  819. }
  820. sleep(5);
  821. system("reboot -f");
  822. sleep(5);
  823. system("reboot -f");
  824. }
  825. ChkSum=0;
  826. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  827. {
  828. ChkSum+=buf[wrd];
  829. }
  830. memcpy(&ChkSumOrg,buf+(0x00600000-4),sizeof(ChkSumOrg));
  831. if(ChkSum!=ChkSumOrg)
  832. {
  833. DEBUG_ERROR_MSG("factory default SysConfigData checksum NG, restore factory default\r\n");
  834. free(buf);
  835. system("cd /root;./FactoryConfig -m");
  836. system("sync");
  837. sleep(5);
  838. system("reboot -f");
  839. sleep(5);
  840. system("reboot -f");
  841. return FAIL;
  842. }
  843. }
  844. }
  845. //load OK
  846. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  847. free(buf);
  848. DEBUG_INFO_MSG("Load SysConfigData OK\r\n");
  849. return PASS;
  850. }
  851. /*
  852. byte GetCurEthUsage()
  853. {
  854. byte result = _ETHERNET_USAGE_NONE;
  855. FILE *fp;
  856. char cmd[256];
  857. char buf[512];
  858. byte targetC = 2;
  859. strcpy(cmd, "route -n");
  860. fp = popen(cmd, "r");
  861. if (fp != NULL)
  862. {
  863. while(fgets(buf, sizeof(buf), fp) != NULL)
  864. {
  865. if (targetC-- == 0)
  866. {
  867. if (strstr(buf, "eth0") > 0)
  868. result = _ETHERNET_USAGE_LAN;
  869. else if (strstr(buf, "mlan0") > 0)
  870. result = _ETHERNET_USAGE_WIFI;
  871. else if (strstr(buf, "ppp0") > 0)
  872. result = _ETHERNET_USAGE_3G_4g;
  873. break;
  874. }
  875. // if (strstr(buf, "inet addr:") > 0)
  876. // {
  877. // }
  878. }
  879. }
  880. pclose(fp);
  881. memset(buf, 0x00, sizeof(buf));
  882. return result;
  883. }
  884. */
  885. int isRouteFail()
  886. {
  887. int result = YES;
  888. FILE *fp;
  889. char buf[512];
  890. fp = popen("route -n", "r");
  891. if(fp != NULL)
  892. {
  893. while(fgets(buf, sizeof(buf), fp) != NULL)
  894. {
  895. if(strstr(buf, "eth0") != NULL)
  896. result = NO;
  897. }
  898. }
  899. pclose(fp);
  900. return result;
  901. }
  902. int isReachableInternet()
  903. {
  904. int result = FAIL;
  905. FILE *fp;
  906. char cmd[256];
  907. char buf[512];
  908. char tmp[512];
  909. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  910. // {
  911. // result = PASS;
  912. // }
  913. // else
  914. {
  915. strcpy(cmd, "ifconfig eth0");
  916. fp = popen(cmd, "r");
  917. if (fp != NULL)
  918. {
  919. while(fgets(buf, sizeof(buf), fp) != NULL)
  920. {
  921. if (strstr(buf, "inet addr:") > 0)
  922. {
  923. sscanf(buf, "%*s%s", tmp);
  924. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  925. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  926. {
  927. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  928. }
  929. }
  930. }
  931. }
  932. pclose(fp);
  933. memset(buf, 0x00, sizeof(buf));
  934. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  935. {
  936. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  937. fp = popen(cmd, "r");
  938. if(fp != NULL)
  939. {
  940. while(fgets(buf, sizeof(buf), fp) != NULL)
  941. {
  942. if(strstr(buf, "transmitted") > 0)
  943. {
  944. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  945. if(strstr(buf,"100%") != NULL)
  946. {
  947. }
  948. else
  949. {
  950. result = PASS;
  951. }
  952. //DEBUG_INFO("%s",buf);
  953. //DEBUG_INFO("%s\n",tmp);
  954. }
  955. }
  956. }
  957. pclose(fp);
  958. }
  959. }
  960. return result;
  961. }
  962. void InitEthernet()
  963. {
  964. char tmpbuf[256];
  965. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  966. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  967. sleep(2);
  968. //Init Eth0 for internet
  969. memset(tmpbuf,0,256);
  970. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  971. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  972. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  973. system(tmpbuf);
  974. memset(tmpbuf,0,256);
  975. sprintf(tmpbuf,"route add default gw %s eth0 ",
  976. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  977. system(tmpbuf);
  978. //system("ifconfig lo up");
  979. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  980. //Init Eth1 for administrator tool
  981. memset(tmpbuf,0,256);
  982. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  983. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  984. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  985. system(tmpbuf);
  986. //Run DHCP client if enabled
  987. system("killall udhcpc");
  988. system("rm -rf /etc/resolv.conf");
  989. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  990. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  991. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  992. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  993. {
  994. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  995. system(tmpbuf);
  996. }
  997. //Upgrade system id to /etc/hostname
  998. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  999. system(tmpbuf);
  1000. // DO360 DHCP Server
  1001. InitialDispenserDhcpServerConfig();
  1002. StartDispenserDhcpServer();
  1003. pid_t pid = fork();
  1004. uint8_t cnt_pingDNS_Fail;
  1005. if(pid == 0)
  1006. {
  1007. for(;;)
  1008. {
  1009. if (isRouteFail())
  1010. {
  1011. PRINTF_FUNC("eth0 not in route, restart eht0. \n");
  1012. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1013. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1014. {
  1015. InitialDHCP();
  1016. }
  1017. }
  1018. if(isReachableInternet() == PASS)
  1019. {
  1020. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet= NO;
  1021. cnt_pingDNS_Fail = 0;
  1022. }
  1023. else
  1024. {
  1025. if(++cnt_pingDNS_Fail > 3)
  1026. {
  1027. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1028. }
  1029. }
  1030. bool ethResult = YES;
  1031. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  1032. ethResult = NO;
  1033. else
  1034. {
  1035. system("/sbin/ifmetric eth0 0");
  1036. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') ||
  1037. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1038. system("/sbin/ifmetric mlan0 1");
  1039. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') ||
  1040. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1041. system("/sbin/ifmetric ppp0 2");
  1042. }
  1043. if (!ethResult &&
  1044. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1045. {
  1046. ethResult = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi;
  1047. if (ethResult)
  1048. {
  1049. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1050. {
  1051. system("/sbin/ifmetric eth0 1");
  1052. system("/sbin/ifmetric mlan0 0");
  1053. }
  1054. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1055. system("/sbin/ifmetric ppp0 2");
  1056. }
  1057. }
  1058. if (!ethResult &&
  1059. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1060. {
  1061. ethResult = !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi;
  1062. if (ethResult)
  1063. {
  1064. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1065. system("/sbin/ifmetric mlan0 2");
  1066. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1067. {
  1068. system("/sbin/ifmetric eth0 1");
  1069. system("/sbin/ifmetric ppp0 0");
  1070. }
  1071. }
  1072. }
  1073. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1074. ShmStatusCodeData->InfoCode.InfoEvents.bits.WiFiDisable = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == false ? true : false;
  1075. ShmStatusCodeData->InfoCode.InfoEvents.bits.Telocom4GModuleDisable = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == false ? true : false;
  1076. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet = ShmOCPP16Data->OcppConnStatus == false ? true : false;
  1077. sleep(5);
  1078. }
  1079. }
  1080. #ifdef SystemLogMessage
  1081. DEBUG_INFO_MSG("[main]InitEthernet: Initial Ethernet OK. \n");
  1082. #endif
  1083. }
  1084. /*
  1085. int InitialRfidPort()
  1086. {
  1087. int uartO2 = open(rfidPortName, O_RDWR);
  1088. struct termios tios;
  1089. if (uartO2 != FAIL)
  1090. {
  1091. ioctl (uartO2, TCGETS, &tios);
  1092. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1093. tios.c_lflag = 0;
  1094. tios.c_iflag = 0;
  1095. tios.c_oflag = 0;
  1096. tios.c_cc[VMIN] = 0;
  1097. tios.c_cc[VTIME] = (unsigned char) 1;
  1098. tios.c_lflag = 0;
  1099. tcflush(uartO2, TCIFLUSH);
  1100. ioctl(uartO2, TCSETS, &tios);
  1101. }
  1102. if (uartO2 < 0)
  1103. {
  1104. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1105. }
  1106. return uartO2;
  1107. }
  1108. */
  1109. void GetMacAddress()
  1110. {
  1111. for (byte index = 0; index < 2; index++)
  1112. {
  1113. int fd;
  1114. struct ifreq ifr;
  1115. char tarEth[5];
  1116. char Mac[18];
  1117. sprintf(tarEth,"eth%d",index);
  1118. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1119. ifr.ifr_addr.sa_family = AF_INET;
  1120. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1121. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1122. close(fd);
  1123. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1124. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1125. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1126. if (index == 0)
  1127. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1128. else
  1129. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1130. }
  1131. }
  1132. void GetFirmwareVersion()
  1133. {
  1134. // Get CSU root file system version
  1135. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1136. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1137. for(uint8_t idx=0;idx<3;idx++)
  1138. {
  1139. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J')
  1140. {
  1141. chademo++;
  1142. count++;
  1143. }
  1144. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1145. {
  1146. gb++;
  1147. count++;
  1148. }
  1149. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  1150. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E')
  1151. {
  1152. ccs++;
  1153. count++;
  1154. }
  1155. }
  1156. if (count == 1)
  1157. {
  1158. if (chademo > 0)
  1159. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1160. else if (ccs > 0)
  1161. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1162. else if (gb > 0)
  1163. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1164. }
  1165. else
  1166. {
  1167. if (chademo > 0 && ccs > 0)
  1168. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1169. else if (chademo > 0 && gb > 0)
  1170. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1171. else if (ccs > 0 && gb > 0)
  1172. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1173. }
  1174. // Get network option from model name
  1175. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1176. {
  1177. case 'B':
  1178. case 'U':
  1179. //Blue tooth
  1180. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1181. break;
  1182. case 'W':
  1183. // WIFI
  1184. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1185. break;
  1186. case 'T':
  1187. // 3G/4G
  1188. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1189. break;
  1190. default:
  1191. // LAN
  1192. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1193. break;
  1194. }
  1195. // Get rating power from model name
  1196. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1197. // Get IEC or UL
  1198. char _buf[3] = {0};
  1199. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  1200. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL)
  1201. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1202. else if (strcmp(_buf, "WU") == EQUAL)
  1203. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1204. }
  1205. void InitialShareMemoryInfo()
  1206. {
  1207. FILE *fp;
  1208. char cmd[512];
  1209. char buf[512];
  1210. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1211. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1212. sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1213. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1214. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1215. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1216. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1217. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1218. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1219. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1220. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1221. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1222. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1223. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1224. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1225. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1226. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1227. sprintf(cmd, "/bin/uname -r");
  1228. fp = popen(cmd, "r");
  1229. if(fp == NULL)
  1230. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1231. else
  1232. {
  1233. while(fgets(buf, sizeof(buf), fp) != NULL)
  1234. {
  1235. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1236. }
  1237. }
  1238. // 雙槍 CCS + Chademo
  1239. GetFirmwareVersion();
  1240. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1241. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1242. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1243. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1244. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1245. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1246. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1247. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1248. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1249. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1250. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1251. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1252. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1253. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleHwRev, " "); //DO360 RC2
  1254. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, " "); //DO360 RC2
  1255. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1256. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, " ");
  1257. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector1FwRev, " ");
  1258. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector2FwRev, " ");
  1259. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1260. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1261. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1262. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1263. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  1264. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  1265. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1266. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1267. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1268. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1269. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1270. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1271. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1272. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1273. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1274. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1275. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1276. memset(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev)); //DO360 RC2
  1277. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1278. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  1279. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  1280. ShmFanModuleData->SelfTest_Comp = NO;
  1281. ShmLedModuleData->SelfTest_Comp = NO;
  1282. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1283. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1284. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1285. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1286. ShmFanModuleData->TestFanSpeed = 0;
  1287. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1288. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1289. char EvsePower[2];
  1290. EvsePower[2] = '\0';
  1291. unsigned short buf_pow = 0;
  1292. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 6)
  1293. {
  1294. strncpy(EvsePower, (char *)(ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2);
  1295. if (strcmp(EvsePower, "15") == EQUAL)
  1296. buf_pow = 150;
  1297. else if (strcmp(EvsePower, "30") == EQUAL)
  1298. buf_pow = 30;
  1299. else if (strcmp(EvsePower, "60") == EQUAL)
  1300. buf_pow = 60;
  1301. else if (strcmp(EvsePower, "12") == EQUAL)
  1302. buf_pow = 120;
  1303. else if (strcmp(EvsePower, "18") == EQUAL)
  1304. buf_pow = 180;
  1305. else if (strcmp(EvsePower, "36") == EQUAL)
  1306. buf_pow = 360;
  1307. ShmSysConfigAndInfo->SysConfig.RatingCurrent = (buf_pow / 30) * PSU_MIN_CUR;
  1308. if(ShmSysConfigAndInfo->SysConfig.MaxChargingPower == 0 ||
  1309. ShmSysConfigAndInfo->SysConfig.MaxChargingPower > buf_pow)
  1310. ShmSysConfigAndInfo->SysConfig.MaxChargingPower = buf_pow;
  1311. }
  1312. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1313. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1314. }
  1315. int Initialization()
  1316. {
  1317. // 初始化卡號驗證的 Flag
  1318. ClearAuthorizedFlag();
  1319. // 初始化插槍驗證的 Flag
  1320. ClearDetectPluginFlag();
  1321. // UART 2 for Rfid
  1322. //rfidFd = InitialRfidPort();
  1323. // int pinOut[2] = { 116, 115 };
  1324. for (byte count = 0; count < CONNECTOR_QUANTITY; count++)
  1325. {
  1326. chargingInfo[count]->RemoteStartFlag = NO;
  1327. //
  1328. // if (chargingInfo[count]->Type == _Type_Chademo)
  1329. // {
  1330. // gpio_set_value(pinOut[count], 0x00);
  1331. // ShmCHAdeMOData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1332. // }
  1333. // else if (chargingInfo[count]->Type == _Type_GB)
  1334. // {
  1335. // gpio_set_value(pinOut[count], 0x00);
  1336. // ShmGBTData->evse[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1337. // }
  1338. // else if (chargingInfo[count]->Type == _Type_CCS_2)
  1339. // {
  1340. // if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1341. // {
  1342. // if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1343. // gpio_set_value(pinOut[1], 0x01);
  1344. // else
  1345. // gpio_set_value(pinOut[count], 0x01);
  1346. // ShmCcsData->V2GMessage_DIN70121[chargingInfo[count]->type_index].SelfTest_Comp = NO;
  1347. // }
  1348. // }
  1349. strcpy((char *)ShmOCPP16Data->StatusNotification[count].ErrorCode, "NoError");
  1350. }
  1351. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  1352. {
  1353. ac_chargingInfo[count]->RemoteStartFlag = NO;
  1354. if (ac_chargingInfo[count]->Type == _Type_AC)
  1355. {
  1356. ac_chargingInfo[count]->SelfTest_Comp = NO;
  1357. strcpy((char *)ShmOCPP16Data->StatusNotification[count + ShmSysConfigAndInfo->SysConfig.TotalConnectorCount].ErrorCode, "NoError");
  1358. }
  1359. }
  1360. PRINTF_FUNC("Initialization OK \n");
  1361. return PASS;
  1362. }
  1363. bool InitialSystemDefaultConfig()
  1364. {
  1365. bool result = true;
  1366. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1367. InitGPIO();
  1368. InitEthernet();
  1369. GetMacAddress();
  1370. // system("echo 1 > /sys/class/gpio/gpio110/value"); //reset PHY
  1371. // sleep(3);
  1372. // system("/sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down");
  1373. // sleep(1);
  1374. // system("/sbin/ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up");
  1375. return result;
  1376. }
  1377. /*
  1378. bool DisplaySelfTestFailReason()
  1379. {
  1380. bool result = false;
  1381. // RB、FB、407、EV 小板中有些板子無回應
  1382. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1383. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = YES; result = true; }
  1384. if (ShmFanModuleData->SelfTest_Comp == NO)
  1385. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = YES; result = true; }
  1386. if (ShmPrimaryMcuData->SelfTest_Comp == NO)
  1387. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = YES; result = true; }
  1388. // if (ShmLedModuleData->SelfTest_Comp == NO)
  1389. // { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES; }
  1390. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1391. {
  1392. if (chargingInfo[index]->Type == _Type_Chademo)
  1393. {
  1394. if (ShmCHAdeMOData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1395. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = YES; result = true; }
  1396. }
  1397. else if (chargingInfo[index]->Type == _Type_GB)
  1398. {
  1399. if (ShmGBTData->evse[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1400. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtboardStestFail = YES; result = true; }
  1401. }
  1402. else if (chargingInfo[index]->Type == _Type_CCS_2)
  1403. {
  1404. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1405. {
  1406. if (ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp == NO)
  1407. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = YES; result = true; }
  1408. }
  1409. }
  1410. }
  1411. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++)
  1412. {
  1413. // 先借 GBT 顯示
  1414. if (ac_chargingInfo[index]->SelfTest_Comp == NO)
  1415. { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcConnectorStestFail = YES; result = true; }
  1416. }
  1417. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == NO)
  1418. {
  1419. // AC Contact 未搭上
  1420. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
  1421. result = true;
  1422. }
  1423. else if (ShmPsuData->SystemAvailablePower <= 0 && ShmPsuData->SystemAvailableCurrent <= 0)
  1424. {
  1425. // PSU 通訊問題
  1426. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = YES;
  1427. result = true;
  1428. }
  1429. return result;
  1430. }
  1431. */
  1432. void SelfTestRun()
  1433. {
  1434. struct timeval _selfTest_time;
  1435. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  1436. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  1437. ShmFanModuleData->SelfTest_Comp = NO;
  1438. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1439. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_VERSION;
  1440. gettimeofday(&_selfTest_time, NULL);
  1441. while (ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  1442. {
  1443. ChkPrimaryStatus();
  1444. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  1445. {
  1446. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1447. PRINTF_FUNC("Self Test Fail: WarningInfo Level 2");
  1448. return;
  1449. }
  1450. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0)
  1451. {
  1452. if (ShmPsuData->Work_Step == _NO_WORKING ||
  1453. ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL)
  1454. {
  1455. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1456. PRINTF_FUNC("Self Test Fail: PSU No Working");
  1457. return;
  1458. }
  1459. switch(ShmSysConfigAndInfo->SysInfo.SelfTestSeq)
  1460. {
  1461. case _STEST_VERSION:
  1462. {
  1463. if (ShmFanModuleData->SelfTest_Comp &&
  1464. ShmRelayModuleData[0]->SelfTest_Comp && ShmRelayModuleData[0]->SelfTest_Comp &&
  1465. ShmPrimaryMcuData->SelfTest_Comp)
  1466. {
  1467. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  1468. PRINTF_FUNC("Self test version check ok");
  1469. }
  1470. }
  1471. break;
  1472. case _STEST_AC_CONTACTOR:
  1473. {
  1474. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  1475. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  1476. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  1477. {
  1478. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1479. PRINTF_FUNC("Communication board pass. \n");
  1480. }
  1481. }
  1482. break;
  1483. case _STEST_PSU_DETECT:
  1484. {
  1485. if (ShmPsuData->Work_Step >= GET_SYS_CAP)
  1486. {
  1487. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1488. }
  1489. }
  1490. break;
  1491. case _STEST_PSU_CAP:
  1492. {
  1493. // 此測試是要確認當前總輸出能力
  1494. // 如果沒有 PSU 模組請 bypass
  1495. if (ShmPsuData->Work_Step == BOOTING_COMPLETE)
  1496. {
  1497. sleep(1);
  1498. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1499. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1500. }
  1501. }
  1502. break;
  1503. }
  1504. }
  1505. else
  1506. break;
  1507. usleep(100000);
  1508. if(GetTimeoutValue(_selfTest_time) / uSEC_VAL >= SELFTEST_TIMEOUT)
  1509. {
  1510. _SelfTestTimeout();
  1511. break;
  1512. }
  1513. }
  1514. }
  1515. int SpawnTask()
  1516. {
  1517. sleep(2);
  1518. system("/root/Module_EventLogging &");
  1519. system("/root/Module_PrimaryComm &");
  1520. system("/root/Module_EvComm &");
  1521. //system("/root/Module_LcmControl &");
  1522. system("/root/Module_InternalComm &");
  1523. system("/root/Module_PsuComm &");
  1524. system("/root/Module_ProduceUtils &");
  1525. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  1526. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  1527. {
  1528. system("/root/OcppBackend &");
  1529. }
  1530. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1531. {
  1532. system("/root/Module_4g &");
  1533. }
  1534. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1535. {
  1536. system("/root/Module_Wifi &");
  1537. }
  1538. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  1539. {
  1540. system("/root/Module_4g &");
  1541. system("/root/Module_Wifi &");
  1542. }
  1543. return PASS;
  1544. }
  1545. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1546. {
  1547. int result = PASS;
  1548. int fd,wrd;
  1549. unsigned int i,Chk;
  1550. unsigned char *ptr, *BufTmp;
  1551. Chk=0;
  1552. ptr=(unsigned char *)UsrData;
  1553. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  1554. {
  1555. memset(BufTmp, 0, MtdBlockSize);
  1556. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  1557. for(i=0; i<MtdBlockSize-4; i++)
  1558. Chk+=*(ptr+i);
  1559. memcpy(BufTmp + MtdBlockSize-4, &Chk, 4);
  1560. fd = open("/dev/mtdblock10", O_RDWR);
  1561. if (fd > 0)
  1562. {
  1563. wrd=write(fd, BufTmp, MtdBlockSize);
  1564. close(fd);
  1565. if(wrd >= MtdBlockSize)
  1566. {
  1567. fd = open("/dev/mtdblock11", O_RDWR);
  1568. if (fd > 0)
  1569. {
  1570. wrd=write(fd, BufTmp, MtdBlockSize);
  1571. close(fd);
  1572. if(wrd < MtdBlockSize)
  1573. {
  1574. DEBUG_ERROR_MSG("write /dev/mtdblock11(backup) NG\r\n");
  1575. result = FAIL;
  1576. }
  1577. }
  1578. else
  1579. {
  1580. DEBUG_ERROR_MSG("open /dev/mtdblock11(backup) NG\r\n");
  1581. result = FAIL;
  1582. }
  1583. }
  1584. else
  1585. {
  1586. DEBUG_ERROR_MSG("write /dev/mtdblock10 NG\r\n");
  1587. result = FAIL;
  1588. }
  1589. }
  1590. else
  1591. {
  1592. DEBUG_ERROR_MSG("open /dev/mtdblock10 NG\r\n");
  1593. result = FAIL;
  1594. }
  1595. }
  1596. else
  1597. {
  1598. DEBUG_ERROR_MSG("alloc BlockSize NG\r\n");
  1599. result = FAIL;
  1600. }
  1601. if(BufTmp != NULL)
  1602. free(BufTmp);
  1603. return result;
  1604. }
  1605. //===============================================
  1606. // Common Detect Chk - Stop Charging ?
  1607. //===============================================
  1608. bool isEvBoardStopChargeFlag(byte gunIndex)
  1609. {
  1610. return chargingInfo[gunIndex]->StopChargeFlag;
  1611. }
  1612. //===============================================
  1613. // 掃描插槍狀況
  1614. //===============================================
  1615. void ClearDetectPluginFlag()
  1616. {
  1617. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  1618. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  1619. {
  1620. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  1621. chargingInfo[gun_index]->RemoteStartFlag = NO;
  1622. }
  1623. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  1624. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1625. }
  1626. void DetectPluginStart()
  1627. {
  1628. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  1629. }
  1630. bool isDetectPlugin()
  1631. {
  1632. if(ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES)
  1633. return YES;
  1634. return NO;
  1635. }
  1636. //===============================================
  1637. // Common Detect Chk - Chademo
  1638. //===============================================
  1639. bool isEvGunLocked_chademo(byte gunIndex)
  1640. {
  1641. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1642. }
  1643. bool isEvContactorWelding_chademo(byte gunIndex)
  1644. {
  1645. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  1646. }
  1647. bool isEvStopReq_chademo(byte gunIndex)
  1648. {
  1649. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  1650. }
  1651. bool isEvStopCharging_chademo(byte gunIndex)
  1652. {
  1653. if (isEvGunLocked_chademo(gunIndex) == NO)
  1654. {
  1655. // 無鎖槍 = 停止
  1656. PRINTF_FUNC("gun locked none (%d) \n", gunIndex);
  1657. return YES;
  1658. }
  1659. return NO;
  1660. }
  1661. byte isPrechargeStatus_chademo(byte gunIndex)
  1662. {
  1663. byte result = 0x00;
  1664. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1665. return result;
  1666. }
  1667. //===============================================
  1668. // Common Detect Chk - GB
  1669. //===============================================
  1670. bool isEvGunLocked_gb(byte gunIndex)
  1671. {
  1672. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1673. }
  1674. bool isEvStopCharging_gb(byte gunIndex)
  1675. {
  1676. if (isEvGunLocked_gb(gunIndex) == NO)
  1677. {
  1678. // 無鎖槍 = 停止
  1679. PRINTF_FUNC("gun locked none. \n");
  1680. return YES;
  1681. }
  1682. return NO;
  1683. }
  1684. byte isPrechargeStatus_gb(byte gunIndex)
  1685. {
  1686. byte result = 0x00;
  1687. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1688. return result;
  1689. }
  1690. //===============================================
  1691. // Common Detect Chk - CCS
  1692. //===============================================
  1693. bool isEvGunLocked_ccs(byte gunIndex)
  1694. {
  1695. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0)? NO : YES;
  1696. }
  1697. byte isPrechargeStatus_ccs(byte gunIndex)
  1698. {
  1699. byte result = 0x00;
  1700. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  1701. {
  1702. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1703. }
  1704. return result;
  1705. }
  1706. bool isEvStopCharging_ccs(byte gunIndex)
  1707. {
  1708. if (isEvGunLocked_ccs(gunIndex) == NO)
  1709. {
  1710. // 無鎖槍 = 停止
  1711. PRINTF_FUNC("gun locked none. \n");
  1712. return YES;
  1713. }
  1714. return NO;
  1715. }
  1716. //===============================================
  1717. // Callback
  1718. //===============================================
  1719. void DisplayChargingInfo()
  1720. {
  1721. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  1722. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  1723. {
  1724. if (chargingInfo[i]->SystemStatus != S_IDLE &&
  1725. chargingInfo[i]->SystemStatus != S_RESERVATION)
  1726. {
  1727. ChangeGunSelectByIndex(i);
  1728. return;
  1729. }
  1730. }
  1731. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  1732. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  1733. ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE)
  1734. {
  1735. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  1736. }
  1737. usleep(50000);
  1738. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1739. }
  1740. void _AutoReturnTimeout()
  1741. {
  1742. PRINTF_FUNC("*********** _AutoReturnTimeout %d*********** \n", ShmSysConfigAndInfo->SysInfo.PageIndex);
  1743. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  1744. {
  1745. ClearDetectPluginFlag();
  1746. }
  1747. // else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  1748. // {
  1749. // DetectPluginStart();
  1750. // }
  1751. usleep(50000);
  1752. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1753. }
  1754. void _SelfTestTimeout(void)
  1755. {
  1756. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  1757. {
  1758. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  1759. {
  1760. setChargerMode(gun_index, MODE_ALARM);
  1761. }
  1762. }
  1763. ShmPsuData->Work_Step = _NO_WORKING;
  1764. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1765. PRINTF_FUNC("Self test timeout. \n");
  1766. }
  1767. void _AuthorizedTimeout()
  1768. {
  1769. if(IsAuthorizingMode())
  1770. {
  1771. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  1772. isCardScan = false;
  1773. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1774. ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1775. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1776. ClearAuthorizedFlag();
  1777. }
  1778. }
  1779. void _DetectPlugInTimeout()
  1780. {
  1781. PRINTF_FUNC("*********** _DetectPlugInTimeout *********** \n");
  1782. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1783. ClearDetectPluginFlag();
  1784. usleep(50000);
  1785. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1786. }
  1787. void _DetectEvChargingEnableTimeout(byte gunIndex)
  1788. {
  1789. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1790. {
  1791. if(!isEvGunLocked_chademo(gunIndex))
  1792. {
  1793. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  1794. }
  1795. }
  1796. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  1797. {
  1798. if(!isEvGunLocked_ccs(gunIndex))
  1799. {
  1800. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  1801. }
  1802. }
  1803. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  1804. {
  1805. if(!isEvGunLocked_ccs(gunIndex))
  1806. {
  1807. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  1808. }
  1809. }
  1810. ChargingTerminalProcess(gunIndex);
  1811. _AutoReturnTimeout();
  1812. }
  1813. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  1814. {
  1815. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  1816. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  1817. {
  1818. setChargerMode(gunIndex, MODE_IDLE);
  1819. _AutoReturnTimeout();
  1820. }
  1821. }
  1822. void _PrepareTimeout(byte gunIndex)
  1823. {
  1824. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  1825. setChargerMode(gunIndex, MODE_IDLE);
  1826. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  1827. _AutoReturnTimeout();
  1828. }
  1829. void _CcsPrechargeTimeout(byte gunIndex)
  1830. {
  1831. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  1832. setChargerMode(gunIndex, MODE_IDLE);
  1833. }
  1834. //===============================================
  1835. // 取得卡號與卡號驗證
  1836. //===============================================
  1837. bool canStartCharging()
  1838. {
  1839. char buf2[16] = "";
  1840. memset(buf2, 0, ARRAY_SIZE(buf2));
  1841. for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++)
  1842. {
  1843. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  1844. }
  1845. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  1846. // 因為無法得知實際的長度,所以只能用搜尋的方式
  1847. if(strcmp(buf2, "Accepted") == EQUAL)
  1848. return true;
  1849. else
  1850. {
  1851. }
  1852. return false;
  1853. }
  1854. void AuthorizingStart()
  1855. {
  1856. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1857. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  1858. }
  1859. void ClearAuthorizedFlag()
  1860. {
  1861. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1862. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  1863. }
  1864. bool isAuthorizedComplete()
  1865. {
  1866. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf == NO)
  1867. return false;
  1868. return true;
  1869. }
  1870. bool IsAuthorizingMode()
  1871. {
  1872. if(ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO)
  1873. return false;
  1874. return true;
  1875. }
  1876. //===============================================
  1877. // 紀錄 Alarm Code
  1878. //===============================================
  1879. void RecordAlarmCode(byte gunIndex, char *code)
  1880. {
  1881. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6);
  1882. if (strcmp(code, "012234") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES;
  1883. if (strcmp(code, "012235") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES;
  1884. if (strcmp(code, "012236") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES;
  1885. if (strcmp(code, "012288") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES;
  1886. if (strcmp(code, "012289") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES;
  1887. if (strcmp(code, "012290") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES;
  1888. }
  1889. void RecordWarningCode(byte gunIndex, char *code)
  1890. {
  1891. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, code, 6);
  1892. if (strcmp(code, "012296") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES;
  1893. if (strcmp(code, "012297") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES;
  1894. if (strcmp(code, "012298") == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES;
  1895. }
  1896. void ReleaseAlarmCode(byte gunIndex)
  1897. {
  1898. bool isCleanCheck = false;
  1899. char code[7];
  1900. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  1901. {
  1902. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012234", 6) == EQUAL &&
  1903. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  1904. {
  1905. memcpy(code, "012234", 6);
  1906. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1907. isCleanCheck = true;
  1908. }
  1909. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012289", 6) == EQUAL &&
  1910. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail == YES)
  1911. {
  1912. memcpy(code, "012289", 6);
  1913. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1914. isCleanCheck = true;
  1915. }
  1916. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == YES)
  1917. {
  1918. ReleaseWarningCodeByString(gunIndex, "012296");
  1919. }
  1920. }
  1921. else if (chargingInfo[gunIndex]->Type == _Type_GB)
  1922. {
  1923. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012236", 6) == EQUAL &&
  1924. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES)
  1925. {
  1926. memcpy(code, "012236", 6);
  1927. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1928. isCleanCheck = true;
  1929. }
  1930. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012290", 6) == EQUAL &&
  1931. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail == YES)
  1932. {
  1933. memcpy(code, "012290", 6);
  1934. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1935. isCleanCheck = true;
  1936. }
  1937. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES)
  1938. {
  1939. ReleaseWarningCodeByString(gunIndex, "012298");
  1940. }
  1941. }
  1942. else if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  1943. {
  1944. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012235", 6) == EQUAL &&
  1945. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  1946. {
  1947. memcpy(code, "012235", 6);
  1948. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1949. isCleanCheck = true;
  1950. }
  1951. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012288", 6) == EQUAL &&
  1952. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail == YES)
  1953. {
  1954. memcpy(code, "012288", 6);
  1955. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1956. isCleanCheck = true;
  1957. }
  1958. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning == YES)
  1959. {
  1960. ReleaseWarningCodeByString(gunIndex, "012297");
  1961. }
  1962. }
  1963. if (isCleanCheck)
  1964. {
  1965. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  1966. {
  1967. if (index != gunIndex || ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  1968. {
  1969. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, code, 6) != EQUAL)
  1970. {
  1971. if (strncmp(code, "012234", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO;
  1972. if (strncmp(code, "012289", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO;
  1973. if (strncmp(code, "012236", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO;
  1974. if (strncmp(code, "012290", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO;
  1975. if (strncmp(code, "012235", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO;
  1976. if (strncmp(code, "012288", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO;
  1977. }
  1978. }
  1979. }
  1980. }
  1981. }
  1982. void ReleaseWarningCodeByString(byte gunIndex, char *code)
  1983. {
  1984. bool isCleanCheck = false;
  1985. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1986. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == YES)
  1987. {
  1988. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1989. isCleanCheck = true;
  1990. }
  1991. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1992. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning == YES)
  1993. {
  1994. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  1995. isCleanCheck = true;
  1996. }
  1997. else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  1998. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES)
  1999. {
  2000. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  2001. isCleanCheck = true;
  2002. }
  2003. if (isCleanCheck)
  2004. {
  2005. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  2006. {
  2007. if (index != gunIndex || ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  2008. {
  2009. if (strncmp((char *)chargingInfo[index]->ConnectorWarningCode, code, 6) != EQUAL)
  2010. {
  2011. if (strncmp(code, "012296", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO;
  2012. if (strncmp(code, "012297", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO;
  2013. if (strncmp(code, "012298", 6) == EQUAL) ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO;
  2014. }
  2015. }
  2016. }
  2017. }
  2018. }
  2019. //===============================================
  2020. // EmergencyStop and Charging Stop
  2021. //===============================================
  2022. void ChargingTerminalProcess(byte gunIndex)
  2023. {
  2024. setChargerMode(gunIndex, MODE_TERMINATING);
  2025. }
  2026. void AcChargingTerminalProcess()
  2027. {
  2028. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2029. }
  2030. void StopChargingProcessByString(byte level)
  2031. {
  2032. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2033. {
  2034. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2035. }
  2036. }
  2037. void ReleaseChargingProcessByString(byte level)
  2038. {
  2039. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2040. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2041. }
  2042. // 一般錯誤停止充電處理函式
  2043. void BoardErrOccurByString(byte index, char *code)
  2044. {
  2045. byte level = 1;
  2046. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2047. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2048. {
  2049. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2050. {
  2051. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2052. }
  2053. ChargingTerminalProcess(index);
  2054. }
  2055. StopChargingProcessByString(level);
  2056. }
  2057. // 急停狀況的停止充電處理函式
  2058. void EmcOccureByString(char *code)
  2059. {
  2060. byte level = 2;
  2061. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2062. // 其錯誤等級為 2
  2063. if (strncmp(code, "012251", 6) == EQUAL || strncmp(code, "012252", 6) == EQUAL ||
  2064. strncmp(code, "012238", 6) == EQUAL)
  2065. {
  2066. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2067. {
  2068. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2069. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2070. {
  2071. ChargingTerminalProcess(gun);
  2072. }
  2073. }
  2074. StopChargingProcessByString(level);
  2075. InformOcppErrOccur(4);
  2076. }
  2077. }
  2078. void ReleaseBoardErrOccurByString(byte index, char *code)
  2079. {
  2080. bool isTrigger = false;
  2081. byte level = 1;
  2082. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES)
  2083. {
  2084. isTrigger = true;
  2085. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  2086. }
  2087. if (isTrigger)
  2088. {
  2089. ReleaseChargingProcessByString(level);
  2090. }
  2091. }
  2092. void ReleaseEmsOccureByString(byte index, char *code)
  2093. {
  2094. bool isTrigger = false;
  2095. byte level = 2;
  2096. if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2097. {
  2098. isTrigger = true;
  2099. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2100. }
  2101. else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2102. {
  2103. isTrigger = true;
  2104. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2105. }
  2106. else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2107. {
  2108. isTrigger = true;
  2109. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2110. }
  2111. else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2112. {
  2113. isTrigger = true;
  2114. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2115. }
  2116. if (isTrigger)
  2117. {
  2118. ReleaseChargingProcessByString(level);
  2119. InformOcppErrOccur(6);
  2120. }
  2121. }
  2122. //===============================================
  2123. // 確認硬體 (按鈕) 狀態
  2124. //===============================================
  2125. bool leftBtnPush = false;
  2126. bool rightBtnPush = false;
  2127. void ChkPrimaryStatus()
  2128. {
  2129. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2130. {
  2131. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2132. EmcOccureByString("012251");
  2133. }
  2134. else
  2135. ReleaseEmsOccureByString(0, "012251");
  2136. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2137. {
  2138. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2139. EmcOccureByString("012238");
  2140. }
  2141. else
  2142. ReleaseEmsOccureByString(0, "012238");
  2143. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2144. {
  2145. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2146. }
  2147. else
  2148. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2149. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2150. {
  2151. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2152. EmcOccureByString("012252");
  2153. }
  2154. else
  2155. ReleaseEmsOccureByString(0, "012252");
  2156. /*
  2157. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush)
  2158. {
  2159. if(!leftBtnPush)
  2160. {
  2161. leftBtnPush = true;
  2162. PRINTF_FUNC("left btn down............................... \n");
  2163. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2164. {
  2165. switch(ac_chargingInfo[0]->SystemStatus)
  2166. {
  2167. case S_IDLE:
  2168. {
  2169. if(isDetectPlugin())
  2170. {
  2171. _DetectPlugInTimeout();
  2172. StopSystemTimeoutDet();
  2173. }
  2174. }
  2175. break;
  2176. case S_REASSIGN_CHECK:
  2177. case S_REASSIGN:
  2178. case S_PREPARNING:
  2179. case S_PREPARING_FOR_EV:
  2180. case S_PREPARING_FOR_EVSE:
  2181. case S_CCS_PRECHARGE_ST0:
  2182. case S_CCS_PRECHARGE_ST1:
  2183. {
  2184. // 取消充電
  2185. AcChargingTerminalProcess();
  2186. }
  2187. break;
  2188. case S_CHARGING:
  2189. {
  2190. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  2191. {
  2192. // 停止充電
  2193. AcChargingTerminalProcess();
  2194. }
  2195. }
  2196. break;
  2197. case S_COMPLETE:
  2198. {}
  2199. break;
  2200. }
  2201. }
  2202. switch(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  2203. {
  2204. case S_IDLE:
  2205. {
  2206. if(isDetectPlugin())
  2207. {
  2208. _DetectPlugInTimeout();
  2209. StopSystemTimeoutDet();
  2210. }
  2211. }
  2212. break;
  2213. case S_REASSIGN_CHECK:
  2214. case S_REASSIGN:
  2215. case S_PREPARNING:
  2216. case S_PREPARING_FOR_EV:
  2217. case S_PREPARING_FOR_EVSE:
  2218. case S_CCS_PRECHARGE_ST0:
  2219. case S_CCS_PRECHARGE_ST1:
  2220. {
  2221. // 取消充電
  2222. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  2223. AcChargingTerminalProcess();
  2224. else
  2225. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2226. }
  2227. break;
  2228. case S_CHARGING:
  2229. {
  2230. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  2231. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->isRemoteStart == NO)
  2232. {
  2233. // 停止充電
  2234. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2235. }
  2236. }
  2237. break;
  2238. case S_COMPLETE:
  2239. {
  2240. // 回 IDLE
  2241. //PRINTF_FUNC("right btn down.................S_COMPLETE \n");
  2242. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_IDLE;
  2243. }
  2244. break;
  2245. }
  2246. }
  2247. }
  2248. else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE)
  2249. {
  2250. if(leftBtnPush)
  2251. {
  2252. leftBtnPush = false;
  2253. PRINTF_FUNC("left btn up............................... \n");
  2254. }
  2255. }
  2256. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush)
  2257. {
  2258. if(!rightBtnPush)
  2259. {
  2260. rightBtnPush = true;
  2261. PRINTF_FUNC("right btn down............................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2262. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2263. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  2264. {
  2265. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  2266. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2267. }
  2268. else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2269. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)
  2270. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2271. else if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2272. {
  2273. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  2274. {
  2275. if (chargingInfo[_index]->SystemStatus != S_BOOTING &&
  2276. chargingInfo[_index]->SystemStatus != S_IDLE &&
  2277. chargingInfo[_index]->SystemStatus != S_RESERVATION)
  2278. {
  2279. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  2280. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2281. return;
  2282. }
  2283. }
  2284. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2285. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2286. }
  2287. else
  2288. {
  2289. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2290. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2291. }
  2292. }
  2293. }
  2294. else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE)
  2295. {
  2296. if(rightBtnPush)
  2297. {
  2298. rightBtnPush = false;
  2299. PRINTF_FUNC("right btn up............................... \n");
  2300. }
  2301. }
  2302. */
  2303. }
  2304. //===============================================
  2305. // 確認各小板偵測的錯誤狀況
  2306. //===============================================
  2307. void CheckErrorOccurStatus(byte index)
  2308. {
  2309. // 小板
  2310. if (chargingInfo[index]->Type == _Type_Chademo)
  2311. {
  2312. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
  2313. BoardErrOccurByString(index, "011012");
  2314. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  2315. // BoardErrOccurByString(index, "012234");
  2316. }
  2317. else if (chargingInfo[index]->Type == _Type_GB)
  2318. {
  2319. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault == YES)
  2320. BoardErrOccurByString(index, "011016");
  2321. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES)
  2322. // BoardErrOccurByString(index, "012236");
  2323. }
  2324. else if (chargingInfo[index]->Type == _Type_CCS_2)
  2325. {
  2326. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
  2327. BoardErrOccurByString(index, "011014");
  2328. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  2329. // BoardErrOccurByString(index, "012235");
  2330. }
  2331. // RB
  2332. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2333. {
  2334. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2335. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2336. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2337. {
  2338. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2339. {
  2340. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2341. StopChargingProcessByString(2);
  2342. InformOcppErrOccur(13);
  2343. }
  2344. }
  2345. else
  2346. {
  2347. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2348. {
  2349. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2350. ReleaseChargingProcessByString(2);
  2351. InformOcppErrOccur(6);
  2352. }
  2353. }
  2354. }
  2355. else
  2356. {
  2357. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2358. {
  2359. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2360. ReleaseChargingProcessByString(2);
  2361. InformOcppErrOccur(6);
  2362. }
  2363. }
  2364. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2365. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2366. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2367. {
  2368. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2369. {
  2370. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2371. StopChargingProcessByString(2);
  2372. InformOcppErrOccur(14);
  2373. }
  2374. }
  2375. else
  2376. {
  2377. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2378. {
  2379. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2380. ReleaseChargingProcessByString(2);
  2381. InformOcppErrOccur(6);
  2382. }
  2383. }
  2384. }
  2385. /*
  2386. //===============================================
  2387. // 確認 GPIO 狀態
  2388. //===============================================
  2389. void gpio_set_value(unsigned int gpio, unsigned int value)
  2390. {
  2391. int fd;
  2392. char buf[MAX_BUF];
  2393. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2394. fd = open(buf, O_WRONLY);
  2395. if (fd < 0)
  2396. {
  2397. perror("gpio/set-value");
  2398. return;
  2399. }
  2400. if (value)
  2401. write(fd, "1", 2);
  2402. else
  2403. write(fd, "0", 2);
  2404. close(fd);
  2405. }
  2406. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2407. {
  2408. int fd;
  2409. char buf[MAX_BUF];
  2410. char ch;
  2411. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2412. fd = open(buf, O_RDONLY);
  2413. if (fd < 0) {
  2414. perror("gpio/get-value");
  2415. return fd;
  2416. }
  2417. read(fd, &ch, 1);
  2418. if (ch != '0') {
  2419. *value = 1;
  2420. } else {
  2421. *value = 0;
  2422. }
  2423. close(fd);
  2424. return 0;
  2425. }
  2426. void CheckGunTypeFromHw()
  2427. {
  2428. int pinIn[4] = { 22, 23, 44, 45 };
  2429. unsigned int gpioValue = 0;
  2430. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  2431. gpio_get_value(pinIn[i], &gpioValue);
  2432. {
  2433. switch (pinIn[i])
  2434. {
  2435. case 22:
  2436. bd1_1_status = gpioValue;
  2437. break;
  2438. case 23:
  2439. bd1_2_status = gpioValue;
  2440. break;
  2441. case 44:
  2442. bd0_1_status = gpioValue;
  2443. break;
  2444. case 45:
  2445. bd0_2_status = gpioValue;
  2446. break;
  2447. }
  2448. }
  2449. }
  2450. }
  2451. void CheckGpioInStatus()
  2452. {
  2453. int pinIn[2] = { 27, 47 };
  2454. unsigned int gpioValue = 0;
  2455. for (int i = 0; i < ARRAY_SIZE(pinIn); i++)
  2456. {
  2457. gpio_get_value(pinIn[i], &gpioValue);
  2458. if (gpioValue == 0x01)
  2459. {
  2460. switch(pinIn[i])
  2461. {
  2462. // 小板緊急停止
  2463. case 47:
  2464. {
  2465. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2466. {
  2467. if (chargingInfo[i]->slotsIndex == 1)
  2468. {
  2469. if (chargingInfo[i]->Type == _Type_Chademo)
  2470. BoardErrOccurByString(i, "023730");
  2471. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2472. BoardErrOccurByString(i, "013627");
  2473. break;
  2474. }
  2475. }
  2476. }
  2477. break;
  2478. case 27:
  2479. {
  2480. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2481. {
  2482. if (chargingInfo[i]->slotsIndex == 3)
  2483. {
  2484. if (chargingInfo[i]->Type == _Type_Chademo)
  2485. BoardErrOccurByString(i, "023730");
  2486. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2487. BoardErrOccurByString(i, "013627");
  2488. break;
  2489. }
  2490. }
  2491. }
  2492. break;
  2493. }
  2494. }
  2495. else
  2496. {
  2497. switch (pinIn[i])
  2498. {
  2499. // 小板解除緊急停止
  2500. case 47:
  2501. {
  2502. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2503. {
  2504. if (chargingInfo[i]->slotsIndex == 1)
  2505. {
  2506. if (chargingInfo[i]->Type == _Type_Chademo)
  2507. ReleaseBoardErrOccurByString(i, "023730");
  2508. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2509. ReleaseBoardErrOccurByString(i, "013627");
  2510. break;
  2511. }
  2512. }
  2513. }
  2514. break;
  2515. case 27:
  2516. {
  2517. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2518. {
  2519. if (chargingInfo[i]->slotsIndex == 3)
  2520. {
  2521. if (chargingInfo[i]->Type == _Type_Chademo)
  2522. ReleaseBoardErrOccurByString(i, "023730");
  2523. else if (chargingInfo[i]->Type == _Type_CCS_2)
  2524. ReleaseBoardErrOccurByString(i, "013627");
  2525. break;
  2526. }
  2527. }
  2528. }
  2529. break;
  2530. }
  2531. }
  2532. }
  2533. }
  2534. */
  2535. //===============================================
  2536. // Main process
  2537. //===============================================
  2538. // 檢查 Byte 中某個 Bit 的值
  2539. // _byte : 欲改變的 byte
  2540. // _bit : 該 byte 的第幾個 bit
  2541. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  2542. {
  2543. return ( _byte & mask_table[_bit] ) != 0x00;
  2544. }
  2545. // 設定 Byte 中某個 Bit的值
  2546. // _byte : 欲改變的 byte
  2547. // _bit : 該 byte 的第幾個 bit
  2548. // value : 修改的值為 0 or 1
  2549. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  2550. {
  2551. if(value == 1)
  2552. *_byte |= (1 << _bit);
  2553. else if (value == 0)
  2554. *_byte ^= (1 << _bit);
  2555. }
  2556. void UserScanFunction()
  2557. {
  2558. bool idleReq = false;
  2559. unsigned char idleIndex = 255;
  2560. // unsigned char stopReq = 255;
  2561. // 當前非驗證的狀態
  2562. if(!IsAuthorizingMode())
  2563. {
  2564. // 先判斷現在是否可以提供刷卡
  2565. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  2566. // 2. 停止充電
  2567. // if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX)
  2568. // {
  2569. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2570. // return;
  2571. // }
  2572. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  2573. {
  2574. // if (chargingInfo[i]->SystemStatus == S_CHARGING)
  2575. // {
  2576. // stopReq = i;
  2577. // }
  2578. // if ((chargingInfo[i]->SystemStatus == S_IDLE && chargingInfo[i]->IsAvailable == YES) ||
  2579. // (_acgunIndex > 0 && ac_chargingInfo[0]->SystemStatus == S_IDLE && ac_chargingInfo[0]->IsAvailable == YES))
  2580. // {
  2581. // idleReq = true;
  2582. // idleIndex = i;
  2583. // }
  2584. if(chargingInfo[i]->SystemStatus == S_IDLE && chargingInfo[i]->IsAvailable == YES)
  2585. {
  2586. idleReq = true;
  2587. idleIndex = i;
  2588. }
  2589. }
  2590. // if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  2591. // ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  2592. // {
  2593. // stopReq = DEFAULT_AC_INDEX;
  2594. // }
  2595. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0)
  2596. {
  2597. // if (_acgunIndex > 0 && stopReq == DEFAULT_AC_INDEX && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  2598. // {
  2599. // char value[32];
  2600. // PRINTF_FUNC("ac stop charging \n");
  2601. // PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  2602. // ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2603. // memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId,
  2604. // ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  2605. // if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  2606. // {
  2607. // AcChargingTerminalProcess();
  2608. // }
  2609. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2610. // }
  2611. /*
  2612. if (stopReq < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2613. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_CHARGING &&
  2614. (_acgunIndex <= 0 || (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE)))
  2615. {
  2616. char value[32];
  2617. PRINTF_FUNC("stop charging \n");
  2618. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  2619. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2620. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  2621. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  2622. // 同一張卡直接停掉
  2623. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL)
  2624. {
  2625. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2626. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2627. }
  2628. else
  2629. {
  2630. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2631. // // 進驗證
  2632. // if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX)
  2633. // {
  2634. // _authorizeIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc;
  2635. // }
  2636. // else
  2637. // {
  2638. // _authorizeIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  2639. // }
  2640. //
  2641. // StartSystemTimeoutDet(Timeout_AuthorizingForStop);
  2642. // AuthorizingStart();
  2643. }
  2644. }
  2645. */
  2646. if (idleReq)
  2647. {
  2648. // if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  2649. // stopReq != 255 &&
  2650. // ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  2651. // {
  2652. // idleReq = false;
  2653. // strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2654. // }
  2655. if ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  2656. (idleIndex != 255 && chargingInfo[idleIndex]->SystemStatus == S_IDLE))
  2657. {
  2658. PRINTF_FUNC("// LCM => Authorizing \n");
  2659. // LCM => Authorizing
  2660. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  2661. // 進入確認卡號狀態
  2662. AuthorizingStart();
  2663. }
  2664. else
  2665. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2666. }
  2667. else
  2668. {
  2669. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2670. }
  2671. }
  2672. }
  2673. else
  2674. {
  2675. // 透過後臺停止充電的判斷
  2676. if (isAuthorizedComplete() ||
  2677. (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  2678. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  2679. {
  2680. // 判斷後台回覆狀態
  2681. if(canStartCharging() ||
  2682. (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  2683. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING))
  2684. {
  2685. if (_authorizeIndex != NO_DEFINE)
  2686. {
  2687. // 先找 AC
  2688. if (_authorizeIndex == DEFAULT_AC_INDEX)
  2689. {
  2690. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  2691. strcmp((char *)chargingInfo[_authorizeIndex]->StartUserId, "") != EQUAL)
  2692. {
  2693. AcChargingTerminalProcess();
  2694. }
  2695. }
  2696. else
  2697. {
  2698. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  2699. strcmp((char *)chargingInfo[_authorizeIndex]->StartUserId, "") != EQUAL)
  2700. {
  2701. ChargingTerminalProcess(_authorizeIndex);
  2702. }
  2703. }
  2704. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2705. _authorizeIndex = NO_DEFINE;
  2706. }
  2707. }
  2708. else
  2709. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2710. PRINTF_FUNC("ClearAuthorizedFlag");
  2711. ClearAuthorizedFlag();
  2712. }
  2713. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  2714. {
  2715. // 白名單驗證
  2716. for (int i = 0; i < 10; i++)
  2717. {
  2718. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  2719. {
  2720. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  2721. {
  2722. ChargingTerminalProcess(_authorizeIndex);
  2723. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2724. ClearAuthorizedFlag();
  2725. break;
  2726. }
  2727. }
  2728. }
  2729. }
  2730. }
  2731. }
  2732. unsigned char isModeChange(unsigned char gun_index)
  2733. {
  2734. unsigned char result = NO;
  2735. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  2736. {
  2737. result = YES;
  2738. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  2739. }
  2740. return result;
  2741. }
  2742. /*
  2743. void ScannerCardProcess()
  2744. {
  2745. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != 2 &&
  2746. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  2747. {
  2748. isCardScan = true;
  2749. // 處理刷卡及驗證卡號的動作
  2750. UserScanFunction();
  2751. }
  2752. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING)
  2753. {
  2754. StartSystemTimeoutDet(Timeout_Authorizing);
  2755. // 確認驗證卡號完成沒
  2756. if (isAuthorizedComplete() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2757. {
  2758. StopSystemTimeoutDet();
  2759. // 判斷後台回覆狀態
  2760. if(canStartCharging() || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2761. {
  2762. // LCM => Authorize complete
  2763. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2764. PRINTF_FUNC("User ID Authorized Completed");
  2765. }
  2766. else
  2767. {
  2768. // LCM => Authorize fail
  2769. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2770. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2771. }
  2772. ClearAuthorizedFlag();
  2773. }
  2774. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  2775. {
  2776. // 白名單驗證
  2777. for (int i = 0; i < 10; i++)
  2778. {
  2779. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  2780. {
  2781. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  2782. {
  2783. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2784. ClearAuthorizedFlag();
  2785. break;
  2786. }
  2787. }
  2788. }
  2789. }
  2790. }
  2791. else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL)
  2792. {
  2793. StartSystemTimeoutDet(Timeout_VerifyFail);
  2794. isCardScan = false;
  2795. }
  2796. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP)
  2797. {
  2798. StartSystemTimeoutDet(Timeout_VerifyComp);
  2799. }
  2800. else if(ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG)
  2801. {
  2802. StartSystemTimeoutDet(Timeout_WaitPlug);
  2803. }
  2804. else
  2805. isCardScan = false;
  2806. }
  2807. */
  2808. struct timeval _StartAuthorizing_Time;
  2809. void PowerCabinetAuthorizingSettingInitial(void)
  2810. {
  2811. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize = false;
  2812. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = false;
  2813. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeTargetIndex = 0;
  2814. }
  2815. void AuthorizingSettingInitial(unsigned char index, unsigned char AuthorizeSrc)
  2816. {
  2817. char *str_auth_src[] = {AUTHORIZE_SRC_NONE_DEV, AUTHORIZE_SRC_LOCAL_DEV, AUTHORIZE_SRC_REMOTE_DEV};
  2818. PRINTF_FUNC("*********** %s id %d Authorize Initial ***********", str_auth_src[AuthorizeSrc], index + 1);
  2819. switch(AuthorizeSrc)
  2820. {
  2821. case _AuthorizeSrc_Local:
  2822. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].UserId, 0x00, 32);
  2823. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthorizeStatus = _AuthorizeStatus_Idle;
  2824. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.AuthorizeRequest = false;
  2825. for(int i = 0; i < 2; i++)
  2826. {
  2827. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] != 0)
  2828. {
  2829. unsigned char ConnectorIndex = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  2830. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AuthorizeRequestType != _AuthorizeSrc_None)
  2831. {
  2832. PRINTF_FUNC("*********** Connector id %d AuthorizeReqClean ***********", ConnectorIndex + 1);
  2833. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AuthorizeRequestType = _AuthorizeSrc_None;
  2834. }
  2835. }
  2836. }
  2837. break;
  2838. case _AuthorizeSrc_Remote:
  2839. memset(ShmOCPP16Data->RemoteStartTransaction[index].IdTag, 0x00, 20);
  2840. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthorizeStatus = _AuthorizeStatus_Idle;
  2841. ShmOCPP16Data->CsMsg.bits[index].RemoteStartTransactionReq = false;
  2842. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AuthorizeRequestType != _AuthorizeSrc_None)
  2843. {
  2844. PRINTF_FUNC("*********** Connector id %d AuthorizeReqClean ***********", index + 1);
  2845. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AuthorizeRequestType = _AuthorizeSrc_None;
  2846. }
  2847. break;
  2848. }
  2849. }
  2850. void SetAuthorizingTarget(unsigned char index, unsigned char AuthorizeSrc)
  2851. {
  2852. BOOL valid = false;
  2853. switch(AuthorizeSrc)
  2854. {
  2855. case _AuthorizeSrc_Local:
  2856. valid = true;
  2857. PRINTF_FUNC("*********** Dispenser id %d Local Authorize ***********", index + 1);
  2858. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, 32);
  2859. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].UserId, 32);
  2860. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthorizeStatus = _AuthorizeStatus_Wait;
  2861. break;
  2862. case _AuthorizeSrc_Remote:
  2863. valid = true;
  2864. PRINTF_FUNC("*********** Connector id %d Remote Authorize ***********", index + 1);
  2865. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, 32);
  2866. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[index].IdTag, 20);
  2867. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthorizeStatus = _AuthorizeStatus_Wait;
  2868. break;
  2869. }
  2870. if(valid)
  2871. {
  2872. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize = true;
  2873. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = false;
  2874. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeTargetIndex = index;
  2875. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc = AuthorizeSrc;
  2876. }
  2877. }
  2878. void SetConnectorAuthorizeOK(unsigned char index, unsigned char AuthorizeSrc)
  2879. {
  2880. switch(AuthorizeSrc)
  2881. {
  2882. case _AuthorizeSrc_Local:
  2883. for(int i = 0; i < 2; i++)
  2884. {
  2885. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] != 0)
  2886. {
  2887. unsigned char ConnectorIndex = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  2888. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].GeneralChargingData.SystemStatus == S_IDLE)
  2889. {
  2890. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AuthorizeRequestType = _AuthorizeSrc_Local;
  2891. memcpy(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].GeneralChargingData.StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
  2892. PRINTF_FUNC("*********** Connector id %d LocalAuthorize OK ***********", ConnectorIndex + 1);
  2893. }
  2894. }
  2895. }
  2896. break;
  2897. case _AuthorizeSrc_Remote:
  2898. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData.SystemStatus == S_IDLE)
  2899. {
  2900. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AuthorizeRequestType = _AuthorizeSrc_Remote;
  2901. memcpy(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData.StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, 32);
  2902. PRINTF_FUNC("*********** Connector id %d RemoteAuthorize OK***********", index + 1);
  2903. }
  2904. break;
  2905. }
  2906. }
  2907. void OCPPAuthorizingStart(void)
  2908. {
  2909. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  2910. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  2911. }
  2912. void AuthorizeTimeoutProcess(void)
  2913. {
  2914. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  2915. {
  2916. switch(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthorizeStatus)
  2917. {
  2918. case _AuthorizeStatus_Pass:
  2919. if(GetTimeoutValue(_ConnectorAuthorizing_Time[index]) / uSEC_VAL >= _connectionTimeout)
  2920. {
  2921. PRINTF_FUNC("*********** Connector id %d Plug In Timeout ***********\n", index + 1);
  2922. AuthorizingSettingInitial(index, _AuthorizeSrc_Remote);
  2923. }
  2924. break;
  2925. case _AuthorizeStatus_Fail:
  2926. if(GetTimeoutValue(_ConnectorAuthorizing_Time[index]) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT)
  2927. {
  2928. PRINTF_FUNC("*********** Connector id %d Authorized Return ***********", index + 1);
  2929. AuthorizingSettingInitial(index, _AuthorizeSrc_Remote);
  2930. }
  2931. break;
  2932. default:
  2933. // do nothing
  2934. break;
  2935. }
  2936. switch(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthorizeStatus)
  2937. {
  2938. case _AuthorizeStatus_Pass:
  2939. if(GetTimeoutValue(_DispenserAuthorizing_Time[index]) / uSEC_VAL >= _connectionTimeout)
  2940. {
  2941. PRINTF_FUNC("*********** Dispenser id %d Plug In Timeout ***********\n", index + 1);
  2942. AuthorizingSettingInitial(index, _AuthorizeSrc_Local);
  2943. }
  2944. break;
  2945. case _AuthorizeStatus_Fail:
  2946. if(GetTimeoutValue(_DispenserAuthorizing_Time[index]) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT)
  2947. {
  2948. PRINTF_FUNC("*********** Dispenser id %d Authorized Return ***********", index + 1);
  2949. AuthorizingSettingInitial(index, _AuthorizeSrc_Local);
  2950. }
  2951. break;
  2952. default:
  2953. // do nothing
  2954. break;
  2955. }
  2956. }
  2957. }
  2958. void PowerCabinetAuthorizeProcess(void)
  2959. {
  2960. char *str_auth_src[] = {AUTHORIZE_SRC_NONE_DEV, AUTHORIZE_SRC_LOCAL_DEV, AUTHORIZE_SRC_REMOTE_DEV};
  2961. BOOL FindRemoteStartRequest = false;
  2962. AuthorizeTimeoutProcess();
  2963. if(!ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize)
  2964. {
  2965. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  2966. {
  2967. if(ShmOCPP16Data->CsMsg.bits[index].RemoteStartTransactionReq == true)
  2968. {
  2969. SetAuthorizingTarget(index, _AuthorizeSrc_Remote);
  2970. ShmOCPP16Data->CsMsg.bits[index].RemoteStartTransactionReq = false;
  2971. FindRemoteStartRequest = true;
  2972. break;
  2973. }
  2974. }
  2975. if(!FindRemoteStartRequest)
  2976. {
  2977. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  2978. {
  2979. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthorizeStatus == _AuthorizeStatus_Idle)
  2980. {
  2981. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.AuthorizeRequest &&
  2982. strlen((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].UserId) > 0)
  2983. {
  2984. SetAuthorizingTarget(index, _AuthorizeSrc_Local);
  2985. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.AuthorizeRequest = false;
  2986. break;
  2987. }
  2988. }
  2989. }
  2990. }
  2991. }
  2992. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize == true &&
  2993. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted == false)
  2994. {
  2995. unsigned char target = ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeTargetIndex;
  2996. unsigned char *AuthorizeStatus;
  2997. switch(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc)
  2998. {
  2999. case _AuthorizeSrc_Local:
  3000. AuthorizeStatus = &ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[target].AuthorizeStatus;
  3001. break;
  3002. case _AuthorizeSrc_Remote:
  3003. AuthorizeStatus = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[target].AuthorizeStatus;
  3004. break;
  3005. default:
  3006. break;
  3007. }
  3008. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc != _AuthorizeSrc_None)
  3009. {
  3010. switch(*AuthorizeStatus)
  3011. {
  3012. case _AuthorizeStatus_Wait:
  3013. *AuthorizeStatus = _AuthorizeStatus_Busy;
  3014. PRINTF_FUNC("*********** %s id %d Start Authorizing ***********",
  3015. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3016. if(ShmOCPP16Data->OcppConnStatus)
  3017. {
  3018. PRINTF_FUNC("*********** %s id %d OPCC AuthorizeReq ***********",
  3019. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3020. OCPPAuthorizingStart();
  3021. }
  3022. // update start authorizing timeout
  3023. gettimeofday(&_StartAuthorizing_Time, NULL);
  3024. break;
  3025. case _AuthorizeStatus_Busy:
  3026. if(GetTimeoutValue(_StartAuthorizing_Time) / uSEC_VAL >= AUTHORIZE_TIMEOUT)
  3027. {
  3028. PRINTF_FUNC("*********** %s ID %d Authorize Timeout ***********",
  3029. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3030. *AuthorizeStatus = _AuthorizeStatus_Fail;
  3031. }
  3032. else
  3033. {
  3034. if(ShmOCPP16Data->OcppConnStatus)
  3035. {
  3036. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf)
  3037. {
  3038. BOOL accept = false;
  3039. if(strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted") == EQUAL)
  3040. {
  3041. accept = true;
  3042. }
  3043. PRINTF_FUNC("*********** %s id %d OCPP Authorize %s ***********",
  3044. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1, accept ? "OK" : "NG");
  3045. *AuthorizeStatus = accept ? _AuthorizeStatus_Pass : _AuthorizeStatus_Fail;
  3046. }
  3047. }
  3048. else
  3049. {
  3050. if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3051. {
  3052. PRINTF_FUNC("*********** %s id %d No Charging ***********",
  3053. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3054. *AuthorizeStatus = _AuthorizeStatus_Fail;
  3055. }
  3056. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  3057. {
  3058. // set authorize pass when offline policy is free charge
  3059. PRINTF_FUNC("*********** %s id %d Free Charging OK ***********",
  3060. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3061. *AuthorizeStatus = _AuthorizeStatus_Pass;
  3062. }
  3063. else if(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST)
  3064. {
  3065. BOOL find = false;
  3066. // check white list
  3067. for(int i = 0; i < 10; i++)
  3068. {
  3069. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL)
  3070. {
  3071. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL)
  3072. {
  3073. find = true;
  3074. break;
  3075. }
  3076. }
  3077. }
  3078. PRINTF_FUNC("*********** %s id %d White Card %s ***********",
  3079. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1, find ? "OK" : "NG");
  3080. *AuthorizeStatus = find ? _AuthorizeStatus_Pass : _AuthorizeStatus_Fail;
  3081. }
  3082. else
  3083. {
  3084. PRINTF_FUNC("*********** %s id %d Invalid Policy ***********",
  3085. str_auth_src[ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc], target + 1);
  3086. *AuthorizeStatus = _AuthorizeStatus_Fail;
  3087. }
  3088. }
  3089. }
  3090. if(*AuthorizeStatus != _AuthorizeStatus_Busy)
  3091. {
  3092. // authorize completed
  3093. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = true;
  3094. if(*AuthorizeStatus == _AuthorizeStatus_Pass)
  3095. {
  3096. SetConnectorAuthorizeOK(target, ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc);
  3097. }
  3098. switch(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizeSrc)
  3099. {
  3100. case _AuthorizeSrc_Local:
  3101. // update dispenser authorizing timeout (local start)
  3102. gettimeofday(&_DispenserAuthorizing_Time[target], NULL);
  3103. break;
  3104. case _AuthorizeSrc_Remote:
  3105. // update connector authorizing timeout (remote start)
  3106. gettimeofday(&_ConnectorAuthorizing_Time[target], NULL);
  3107. break;
  3108. default:
  3109. break;
  3110. }
  3111. // update authorizing completed timeout
  3112. gettimeofday(&_StartAuthorizing_Time, NULL);
  3113. CheckConnectionTimeout();
  3114. }
  3115. break;
  3116. case _AuthorizeStatus_Pass:
  3117. case _AuthorizeStatus_Fail:
  3118. break;
  3119. default:
  3120. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.StartAuthorize = false;
  3121. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = false;
  3122. break;
  3123. }
  3124. }
  3125. else
  3126. {
  3127. PRINTF_FUNC("*********** PowerCabinet Authorize None Src ***********");
  3128. // authorize completed
  3129. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted = true;
  3130. // update authorizing completed timeout
  3131. gettimeofday(&_StartAuthorizing_Time, NULL);
  3132. }
  3133. }
  3134. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.AuthorizingCompleted)
  3135. {
  3136. if(GetTimeoutValue(_StartAuthorizing_Time) / uSEC_VAL >= AUTHORIZE_COMP_TIMEOUT)
  3137. {
  3138. PRINTF_FUNC("*********** PowerCabinet Authorize Completed ***********");
  3139. PowerCabinetAuthorizingSettingInitial();
  3140. }
  3141. else
  3142. {
  3143. // do nothing
  3144. }
  3145. }
  3146. }
  3147. BOOL IsConnectorAuthorizeSuccess(unsigned char ConnectorIndex)
  3148. {
  3149. BOOL success = false;
  3150. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[ConnectorIndex].Parameter.bits.AuthorizeRequestType != _AuthorizeSrc_None)
  3151. {
  3152. success = true;
  3153. }
  3154. return success;
  3155. }
  3156. /*
  3157. bool AddGunInfoByConnector(byte typeValue, byte slots)
  3158. {
  3159. bool result = true;
  3160. switch (typeValue)
  3161. {
  3162. case '0': // none
  3163. break;
  3164. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  3165. break;
  3166. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  3167. break;
  3168. case '3': // IEC 62196-2 Type 2 Plug
  3169. case '4': // IEC 62196-2 Type 2 Socket
  3170. if (AC_QUANTITY > _ac_Index)
  3171. {
  3172. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  3173. // AC 固定 index
  3174. ac_chargingInfo[_acgunIndex]->Index = 0;
  3175. ac_chargingInfo[_acgunIndex]->ReservationId = -1;
  3176. ac_chargingInfo[_acgunIndex]->SystemStatus = S_IDLE;
  3177. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  3178. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  3179. _ac_Index++;
  3180. _acgunIndex++;
  3181. }
  3182. else
  3183. result = false;
  3184. break;
  3185. case '5': // GB/T AC Plug
  3186. break;
  3187. case '6': // GB/T AC Socket
  3188. break;
  3189. case 'J': // CHAdeMO
  3190. {
  3191. if (CHAdeMO_QUANTITY > _chademoIndex)
  3192. {
  3193. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  3194. chargingInfo[_gunIndex]->Index = _gunIndex;
  3195. chargingInfo[_gunIndex]->ReservationId = -1;
  3196. chargingInfo[_gunIndex]->slotsIndex = slots;
  3197. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3198. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  3199. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  3200. chargingInfo[_gunIndex]->IsAvailable = YES;
  3201. _chademoIndex++;
  3202. _gunIndex++;
  3203. }
  3204. else
  3205. result = false;
  3206. }
  3207. break;
  3208. case 'U': // CCS1 combo
  3209. case 'E': // CCS2 combo
  3210. {
  3211. if (CCS_QUANTITY > _ccsIndex)
  3212. {
  3213. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  3214. chargingInfo[_gunIndex]->Index = _gunIndex;
  3215. chargingInfo[_gunIndex]->ReservationId = -1;
  3216. chargingInfo[_gunIndex]->slotsIndex = slots;
  3217. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3218. chargingInfo[_gunIndex]->Type = _Type_CCS_2;
  3219. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  3220. chargingInfo[_gunIndex]->IsAvailable = YES;
  3221. // 現階段預設為走 DIN70121
  3222. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  3223. _ccsIndex++;
  3224. _gunIndex++;
  3225. }
  3226. else
  3227. result = false;
  3228. }
  3229. break;
  3230. case 'G': // GBT DC
  3231. {
  3232. if (GB_QUANTITY > _gb_Index)
  3233. {
  3234. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  3235. chargingInfo[_gunIndex]->Index = _gunIndex;
  3236. chargingInfo[_gunIndex]->ReservationId = -1;
  3237. chargingInfo[_gunIndex]->slotsIndex = slots;
  3238. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  3239. chargingInfo[_gunIndex]->Type = _Type_GB;
  3240. chargingInfo[_gunIndex]->type_index = _gb_Index;
  3241. chargingInfo[_gunIndex]->IsAvailable = YES;
  3242. _gb_Index++;
  3243. _gunIndex++;
  3244. }
  3245. else
  3246. result = false;
  3247. }
  3248. break;
  3249. case 'D': // GBT DC x 2
  3250. break;
  3251. }
  3252. return result;
  3253. }
  3254. */
  3255. bool CheckConnectorTypeStatus()
  3256. {
  3257. bool result = true;
  3258. // PRINTF_FUNC("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
  3259. // bd0_1_status, bd0_2_status, bd1_1_status, bd1_2_status);
  3260. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9)
  3261. {
  3262. // byte slots = 1;
  3263. // for (byte typeIndex = 7; typeIndex <= 9; typeIndex++)
  3264. // {
  3265. // if(!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots))
  3266. // {
  3267. // return false;
  3268. // }
  3269. // slots++;
  3270. // }
  3271. // AC index 接在 DC 後面
  3272. // if (AC_QUANTITY > 0)
  3273. // ac_chargingInfo[0]->Index += _gunIndex;
  3274. // DO360 for Audi, two gun only
  3275. _gunIndex = 2;
  3276. _acgunIndex = 0;
  3277. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3278. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  3279. PRINTF_FUNC("DC _gunCount = %d, AC _gunCount = %d \n",
  3280. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  3281. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  3282. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  3283. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0)
  3284. result = false;
  3285. //DO360 charging info default setting
  3286. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  3287. {
  3288. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  3289. chargingInfo[i]->Index = 0xFF;
  3290. chargingInfo[i]->Evboard_id = 0x00;
  3291. chargingInfo[i]->ReservationId = -1;
  3292. chargingInfo[i]->slotsIndex = 0;
  3293. chargingInfo[i]->SystemStatus = S_BOOTING;
  3294. chargingInfo[i]->Type = _Type_Unknown;
  3295. chargingInfo[i]->type_index = 0;
  3296. chargingInfo[i]->IsAvailable = YES;
  3297. }
  3298. /*
  3299. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3300. {
  3301. chargingInfo[0]->Evboard_id = 0x01;
  3302. PRINTF_FUNC("index = %d, Type = %d, Evboard_id = %d \n", 0, chargingInfo[0]->Type, chargingInfo[0]->Evboard_id);
  3303. }
  3304. else
  3305. {
  3306. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  3307. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  3308. {
  3309. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1)
  3310. {
  3311. // 與硬體相同 type : Chademo
  3312. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3313. {
  3314. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3315. }
  3316. }
  3317. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0)
  3318. {
  3319. // 與硬體相同 type : CCS
  3320. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  3321. {
  3322. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3323. }
  3324. }
  3325. else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1)
  3326. {
  3327. // 與硬體相同 type : GB
  3328. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3329. {
  3330. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3331. }
  3332. }
  3333. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1)
  3334. {
  3335. // 與硬體相同 type : Chademo
  3336. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  3337. {
  3338. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3339. }
  3340. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3341. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3342. }
  3343. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0)
  3344. {
  3345. // 與硬體相同 type : CCS
  3346. if (chargingInfo[gunIndex]->Type == _Type_CCS_2)
  3347. {
  3348. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3349. }
  3350. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3351. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3352. }
  3353. else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1)
  3354. {
  3355. // 與硬體相同 type : GB
  3356. if (chargingInfo[gunIndex]->Type == _Type_GB)
  3357. {
  3358. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3359. }
  3360. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1)
  3361. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3362. }
  3363. PRINTF_FUNC("index = %d, Type = %d, Evboard_id = %d \n", gunIndex, chargingInfo[gunIndex]->Type, chargingInfo[gunIndex]->Evboard_id);
  3364. if (chargingInfo[gunIndex]->Evboard_id == 0x00)
  3365. result = false;
  3366. }
  3367. }
  3368. */
  3369. }
  3370. else
  3371. {
  3372. // Module Name 不正確 - 告警
  3373. result = false;
  3374. }
  3375. return result;
  3376. }
  3377. void KillTask()
  3378. {
  3379. ChangeLcmByIndex(_LCM_FIX);
  3380. system("killall Module_EventLogging");
  3381. system("killall Module_PrimaryComm");
  3382. system("killall Module_EvComm");
  3383. system("killall Module_LcmControl");
  3384. system("killall Module_InternalComm");
  3385. system("killall Module_PsuComm");
  3386. system("killall OcppBackend &");
  3387. system("killall Module_4g &");
  3388. system("killall Module_Wifi &");
  3389. }
  3390. char CheckUpdateProcess()
  3391. {
  3392. DIR *d;
  3393. struct dirent *dir;
  3394. d = opendir("/mnt/");
  3395. if (d)
  3396. {
  3397. long int MaxLen=48*1024*1024, ImageLen = 0;
  3398. while ((dir = readdir(d)) != NULL)
  3399. {
  3400. char *new_str;
  3401. new_str = malloc(strlen("/mnt/")+strlen(dir->d_name)+1);
  3402. new_str[0] = '\0';
  3403. strcat(new_str, "/mnt/");
  3404. strcat(new_str, dir->d_name);
  3405. int fd = open(new_str, O_RDONLY);
  3406. if (fd < 0)
  3407. {
  3408. return FAIL;
  3409. }
  3410. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  3411. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  3412. //get the image length
  3413. ImageLen = read(fd, ptr, MaxLen);
  3414. if (ImageLen > 20)
  3415. {
  3416. unsigned int Type = (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19]));
  3417. PRINTF_FUNC("Typed...%x \r\n", Type);
  3418. switch (Type)
  3419. {
  3420. case 0x10000001:
  3421. case 0x10000002:
  3422. case 0x10000003:
  3423. case 0x10000004:
  3424. case 0x10000005:
  3425. {
  3426. if (Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3427. return PASS;
  3428. else
  3429. return FAIL;
  3430. }
  3431. break;
  3432. case 0x10000007:
  3433. case 0x10000008:
  3434. case 0x10000009:
  3435. case 0x1000000A:
  3436. {
  3437. bool isPass = true;
  3438. int CanFd = InitCanBus();
  3439. if (CanFd > 0)
  3440. {
  3441. for(byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  3442. {
  3443. if (!isPass)
  3444. break;
  3445. if (chargingInfo[index]->Type == _Type_CCS_2)
  3446. {
  3447. if (Upgrade_CCS(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == FAIL)
  3448. {
  3449. isPass = false;
  3450. }
  3451. }
  3452. }
  3453. }
  3454. else
  3455. {
  3456. printf("Upgrade CCS open CAN FD fail.\n");
  3457. isPass = false;
  3458. }
  3459. return isPass;
  3460. }
  3461. break;
  3462. case 0x10000006:
  3463. case 0x1000000D:
  3464. case 0x1000000E:
  3465. case 0x20000002:
  3466. case 0x10000014:
  3467. {
  3468. // CSU_PRIMARY_CONTROLLER : 0x10000006
  3469. byte target = 0x00;
  3470. if (Type == 0x10000006)
  3471. target = UPGRADE_PRI;
  3472. else if (Type == 0x1000000D)
  3473. target = UPGRADE_RB;
  3474. else if (Type == 0x1000000E)
  3475. target = UPGRADE_FAN;
  3476. else if (Type == 0x20000002)
  3477. target = UPGRADE_AC;
  3478. else if (Type == 0x10000014)
  3479. target = UPGRADE_LED;
  3480. int fd = InitComPort(target);
  3481. if (Upgrade_UART(fd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3482. return PASS;
  3483. else
  3484. return FAIL;
  3485. close(fd);
  3486. }
  3487. break;
  3488. case 0x1000000B:
  3489. case 0x1000000C:
  3490. {
  3491. // CHAdeMO_BOARD : 0x1000000B, GBT : 0x1000000C
  3492. bool isPass = true;
  3493. int CanFd = InitCanBus();
  3494. if (CanFd > 0)
  3495. {
  3496. for(byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  3497. {
  3498. if (!isPass)
  3499. break;
  3500. if ((Type == 0x1000000B && chargingInfo[index]->Type == _Type_Chademo) ||
  3501. (Type == 0x1000000C && chargingInfo[index]->Type == _Type_GB))
  3502. {
  3503. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  3504. return PASS;
  3505. else
  3506. return FAIL;
  3507. }
  3508. }
  3509. }
  3510. else
  3511. {
  3512. PRINTF_FUNC("Upgrad FD fail. \n");
  3513. isPass = false;
  3514. }
  3515. return isPass;
  3516. break;
  3517. }
  3518. }
  3519. }
  3520. free(new_str);
  3521. free(ptr);
  3522. }
  3523. }
  3524. free(dir);
  3525. closedir(d);
  3526. return FAIL;
  3527. }
  3528. /*
  3529. void CreateRfidFork()
  3530. {
  3531. pid_t rfidRecPid;
  3532. rfidRecPid = fork();
  3533. if (rfidRecPid == 0)
  3534. {
  3535. while(true)
  3536. {
  3537. // 刷卡判斷
  3538. RFID rfid;
  3539. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  3540. !ShmSysConfigAndInfo->SysConfig.isRFID)
  3541. {}
  3542. else if(getRequestCardSN(rfidFd, 0, &rfid))
  3543. {
  3544. PRINTF_FUNC("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3545. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0)
  3546. {
  3547. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE)
  3548. {
  3549. switch (rfid.snType)
  3550. {
  3551. case RFID_SN_TYPE_6BYTE:
  3552. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3553. "%02X%02X%02X%02X%02X%02X",
  3554. rfid.currentCard[0], rfid.currentCard[1],
  3555. rfid.currentCard[2], rfid.currentCard[3],
  3556. rfid.currentCard[4], rfid.currentCard[5]);
  3557. break;
  3558. case RFID_SN_TYPE_7BYTE:
  3559. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3560. "%02X%02X%02X%02X%02X%02X%02X",
  3561. rfid.currentCard[0], rfid.currentCard[1],
  3562. rfid.currentCard[2], rfid.currentCard[3],
  3563. rfid.currentCard[4], rfid.currentCard[5],
  3564. rfid.currentCard[6]);
  3565. break;
  3566. case RFID_SN_TYPE_10BYTE:
  3567. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3568. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3569. rfid.currentCard[0], rfid.currentCard[1],
  3570. rfid.currentCard[2], rfid.currentCard[3],
  3571. rfid.currentCard[4], rfid.currentCard[5],
  3572. rfid.currentCard[6], rfid.currentCard[7],
  3573. rfid.currentCard[8], rfid.currentCard[9]);
  3574. break;
  3575. case RFID_SN_TYPE_4BYTE:
  3576. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3577. "%02X%02X%02X%02X",
  3578. rfid.currentCard[0], rfid.currentCard[1],
  3579. rfid.currentCard[2], rfid.currentCard[3]);
  3580. break;
  3581. }
  3582. }
  3583. else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG)
  3584. {
  3585. switch (rfid.snType)
  3586. {
  3587. case RFID_SN_TYPE_6BYTE:
  3588. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3589. "%02X%02X%02X%02X%02X%02X",
  3590. rfid.currentCard[5], rfid.currentCard[4],
  3591. rfid.currentCard[3], rfid.currentCard[2],
  3592. rfid.currentCard[1], rfid.currentCard[0]);
  3593. break;
  3594. case RFID_SN_TYPE_7BYTE:
  3595. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3596. "%02X%02X%02X%02X%02X%02X%02X",
  3597. rfid.currentCard[6], rfid.currentCard[5],
  3598. rfid.currentCard[4], rfid.currentCard[3],
  3599. rfid.currentCard[2], rfid.currentCard[1],
  3600. rfid.currentCard[0]);
  3601. break;
  3602. case RFID_SN_TYPE_10BYTE:
  3603. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3604. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3605. rfid.currentCard[9], rfid.currentCard[8],
  3606. rfid.currentCard[7], rfid.currentCard[6],
  3607. rfid.currentCard[5], rfid.currentCard[4],
  3608. rfid.currentCard[3], rfid.currentCard[2],
  3609. rfid.currentCard[1], rfid.currentCard[0]);
  3610. break;
  3611. case RFID_SN_TYPE_4BYTE:
  3612. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3613. "%02X%02X%02X%02X",
  3614. rfid.currentCard[3], rfid.currentCard[2],
  3615. rfid.currentCard[1], rfid.currentCard[0]);
  3616. break;
  3617. }
  3618. }
  3619. PRINTF_FUNC("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3620. }
  3621. }
  3622. sleep(1);
  3623. }
  3624. }
  3625. }
  3626. */
  3627. void StartSystemTimeoutDet(unsigned char flag)
  3628. {
  3629. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag)
  3630. {
  3631. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3632. }
  3633. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  3634. }
  3635. void StopSystemTimeoutDet()
  3636. {
  3637. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3638. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  3639. }
  3640. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  3641. {
  3642. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  3643. {
  3644. if (chargingInfo[gunIndex]->TimeoutFlag != flag)
  3645. {
  3646. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  3647. }
  3648. chargingInfo[gunIndex]->TimeoutFlag = flag;
  3649. }
  3650. }
  3651. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  3652. {
  3653. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount)
  3654. {
  3655. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  3656. }
  3657. }
  3658. void CheckConnectionTimeout(void)
  3659. {
  3660. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3661. {
  3662. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3663. }
  3664. else
  3665. {
  3666. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  3667. {
  3668. _connectionTimeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3669. if(_connectionTimeout <= 0)
  3670. {
  3671. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3672. }
  3673. }
  3674. else
  3675. {
  3676. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3677. }
  3678. }
  3679. }
  3680. void CreateTimeoutFork()
  3681. {
  3682. pid_t timeoutPid;
  3683. timeoutPid = fork();
  3684. if (timeoutPid > 0)
  3685. {
  3686. gettimeofday(&_cmdSubPriority_time, NULL);
  3687. CheckConnectionTimeout();
  3688. while(true)
  3689. {
  3690. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  3691. {
  3692. CheckConnectionTimeout();
  3693. gettimeofday(&_cmdSubPriority_time, NULL);
  3694. }
  3695. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  3696. // 系統
  3697. switch(ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag)
  3698. {
  3699. // case Timeout_SelftestChk:
  3700. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= SELFTEST_TIMEOUT)
  3701. // {
  3702. // _SelfTestTimeout();
  3703. // StopSystemTimeoutDet();
  3704. // }
  3705. // break;
  3706. // case Timeout_Authorizing:
  3707. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_TIMEOUT)
  3708. // {
  3709. // _AuthorizedTimeout();
  3710. // StopSystemTimeoutDet();
  3711. // }
  3712. // break;
  3713. // case Timeout_VerifyFail:
  3714. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT)
  3715. // {
  3716. // _AutoReturnTimeout();
  3717. // StopSystemTimeoutDet();
  3718. // }
  3719. // break;
  3720. // case Timeout_VerifyComp:
  3721. // if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_COMP_TIMEOUT)
  3722. // {
  3723. // _AutoReturnTimeout();
  3724. // StopSystemTimeoutDet();
  3725. // }
  3726. // break;
  3727. case Timeout_WaitPlug:
  3728. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= _connectionTimeout)
  3729. {
  3730. _DetectPlugInTimeout();
  3731. StopSystemTimeoutDet();
  3732. }
  3733. break;
  3734. case Timeout_ReturnToChargingGunDet:
  3735. {
  3736. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= RETURN_TO_CHARGING_PAGE)
  3737. {
  3738. DisplayChargingInfo();
  3739. StopSystemTimeoutDet();
  3740. }
  3741. }
  3742. break;
  3743. case Timeout_AuthorizingForStop:
  3744. {
  3745. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT)
  3746. {
  3747. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3748. ClearAuthorizedFlag();
  3749. StopSystemTimeoutDet();
  3750. }
  3751. }
  3752. break;
  3753. }
  3754. // 各槍
  3755. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  3756. {
  3757. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  3758. switch(chargingInfo[gun_index]->TimeoutFlag)
  3759. {
  3760. case Timeout_Preparing:
  3761. {
  3762. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PREPARE_TIMEOUT)
  3763. {
  3764. _PrepareTimeout(gun_index);
  3765. StopGunInfoTimeoutDet(gun_index);
  3766. }
  3767. }
  3768. break;
  3769. case Timeout_EvChargingDet:
  3770. {
  3771. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EV_WAIT_TIMEOUT)
  3772. {
  3773. _DetectEvChargingEnableTimeout(gun_index);
  3774. StopGunInfoTimeoutDet(gun_index);
  3775. }
  3776. }
  3777. break;
  3778. case Timeout_EvseChargingDet:
  3779. {
  3780. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EVSE_WAIT_TIMEOUT)
  3781. {
  3782. _DetectEvseChargingEnableTimeout(gun_index);
  3783. StopGunInfoTimeoutDet(gun_index);
  3784. }
  3785. }
  3786. break;
  3787. case Timeout_EvseCompleteDet:
  3788. {
  3789. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_COMP_WAIT_TIMEOUT)
  3790. {
  3791. StopGunInfoTimeoutDet(gun_index);
  3792. }
  3793. }
  3794. break;
  3795. case Timeout_ForCcsPrechargeDet:
  3796. {
  3797. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PRECHARGING_TIMEOUT)
  3798. {
  3799. _CcsPrechargeTimeout(gun_index);
  3800. StopGunInfoTimeoutDet(gun_index);
  3801. }
  3802. }
  3803. break;
  3804. }
  3805. }
  3806. sleep(1);
  3807. }
  3808. }
  3809. }
  3810. void GetSystemTime()
  3811. {
  3812. struct timeb csuTime;
  3813. struct tm *tmCSU;
  3814. ftime(&csuTime);
  3815. tmCSU = localtime(&csuTime.time);
  3816. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  3817. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3818. tmCSU->tm_sec);
  3819. // byte date[14];
  3820. //
  3821. //
  3822. // //sprintf(&date, "%d", );
  3823. //
  3824. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  3825. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  3826. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  3827. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  3828. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  3829. //
  3830. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  3831. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  3832. //
  3833. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  3834. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  3835. //
  3836. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  3837. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  3838. //
  3839. // date[10] = (tmCSU->tm_min) / 10 % 10;
  3840. // date[11] = (tmCSU->tm_min) / 1 % 10;
  3841. //
  3842. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  3843. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  3844. // PRINTF_FUNC("%x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x \n", date[0], date[1], date[2], date[3],
  3845. // date[4], date[5], date[6], date[7],
  3846. // date[8], date[9], date[10], date[11],
  3847. // date[12], date[13]);
  3848. }
  3849. void CheckFactoryConfigFunction()
  3850. {
  3851. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  3852. {
  3853. system("cd /root;./FactoryConfig -m");
  3854. system("sync");
  3855. sleep(5);
  3856. system("reboot -f");
  3857. sleep(5);
  3858. system("reboot -f");
  3859. }
  3860. }
  3861. void CheckFwUpdateFunction()
  3862. {
  3863. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  3864. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  3865. {
  3866. DEBUG_INFO_MSG("ftp : update start. \n");
  3867. KillTask();
  3868. if (CheckUpdateProcess() == PASS)
  3869. DEBUG_INFO_MSG("ftp : update complete. \n");
  3870. else
  3871. DEBUG_INFO_MSG("ftp : update fail. \n");
  3872. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  3873. sleep(5);
  3874. system("reboot -f");
  3875. }
  3876. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES)
  3877. {
  3878. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  3879. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL)
  3880. {
  3881. DEBUG_INFO_MSG("Backend : update start. \n");
  3882. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3883. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3884. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3885. KillTask();
  3886. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  3887. {
  3888. setChargerMode(_index, MODE_UPDATE);
  3889. }
  3890. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++)
  3891. {
  3892. ac_chargingInfo[_index]->SystemStatus = MODE_UPDATE;
  3893. }
  3894. if (CheckUpdateProcess() == PASS)
  3895. {
  3896. DEBUG_INFO_MSG("Backend : update complete. \n");
  3897. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3898. }
  3899. else
  3900. {
  3901. DEBUG_INFO_MSG("Backend : update fail. \n");
  3902. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  3903. }
  3904. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3905. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3906. sleep(5);
  3907. system("reboot -f");
  3908. }
  3909. }
  3910. }
  3911. //===============================================
  3912. // Check reservation date is expired
  3913. //===============================================
  3914. int isReservationExpired(unsigned char gun_index)
  3915. {
  3916. int result = NO;
  3917. struct tm expiredDate;
  3918. struct timeb expiredTime;
  3919. if (sscanf((char*) ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate,
  3920. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  3921. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  3922. &expiredDate.tm_min, &expiredDate.tm_sec) == 6)
  3923. {
  3924. expiredDate.tm_year -= 1900;
  3925. expiredDate.tm_mon -= 1;
  3926. expiredTime.time = mktime(&expiredDate);
  3927. if (!CheckTimeOut(expiredTime))
  3928. {
  3929. result = YES;
  3930. }
  3931. }
  3932. return result;
  3933. }
  3934. //===============================================
  3935. // OCPP
  3936. //===============================================
  3937. void CheckOcppStatus()
  3938. {
  3939. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES)
  3940. {
  3941. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  3942. PRINTF_FUNC("*********************OCPP Boot Notification****************************\n");
  3943. }
  3944. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES)
  3945. {
  3946. bool canReset = true;
  3947. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  3948. {
  3949. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  3950. {
  3951. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  3952. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  3953. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  3954. {
  3955. canReset = false;
  3956. if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  3957. {
  3958. ChargingTerminalProcess(_index);
  3959. }
  3960. }
  3961. }
  3962. }
  3963. if (canReset)
  3964. {
  3965. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  3966. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3967. if(strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL)
  3968. {
  3969. DEBUG_ERROR_MSG("****** Hard Reboot ****** \n");
  3970. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  3971. sleep(3);
  3972. system("reboot -f");
  3973. }
  3974. else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL)
  3975. {
  3976. DEBUG_ERROR_MSG("****** Soft Reboot ****** \n");
  3977. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  3978. sleep(3);
  3979. system("killall OcppBackend &");
  3980. KillTask();
  3981. system("/usr/bin/run_evse_restart.sh");
  3982. }
  3983. }
  3984. }
  3985. }
  3986. void OcppStartTransation(byte gunIndex)
  3987. {
  3988. byte _OcppGunIndex = gunIndex;
  3989. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  3990. // if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  3991. // _OcppGunIndex = 2;
  3992. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  3993. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  3994. else
  3995. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  3996. PRINTF_FUNC("Gun: %d, OCPP Start Transation Index: %d, IdTag = %s \n", chargingInfo[gunIndex]->Index, _OcppGunIndex, ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  3997. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  3998. }
  3999. void OcppStopTransation(byte gunIndex)
  4000. {
  4001. byte _OcppGunIndex = gunIndex;
  4002. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  4003. // if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1)
  4004. // _OcppGunIndex = 2;
  4005. if(strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL)
  4006. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  4007. else
  4008. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  4009. PRINTF_FUNC("OCPP Start Transation Index: %d, IdTag = %s \n", _OcppGunIndex, ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  4010. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  4011. }
  4012. bool OcppRemoteStop(byte gunIndex)
  4013. {
  4014. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4015. // 有 AC 槍的話
  4016. if (acDirIndex > 0 && gunIndex > 0)
  4017. {
  4018. gunIndex += acDirIndex;
  4019. }
  4020. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  4021. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  4022. {
  4023. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  4024. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  4025. }
  4026. return result;
  4027. }
  4028. void OcppRemoteStartChk()
  4029. {
  4030. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  4031. {}
  4032. else if(!isDetectPlugin())
  4033. {
  4034. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  4035. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  4036. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index++)
  4037. {
  4038. if (ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq == YES)
  4039. {
  4040. if (ac_chargingInfo[ac_index]->SystemStatus == S_IDLE ||
  4041. ac_chargingInfo[ac_index]->SystemStatus == S_RESERVATION)
  4042. {
  4043. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  4044. ac_chargingInfo[ac_index]->RemoteStartFlag = YES;
  4045. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  4046. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  4047. ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  4048. DetectPluginStart();
  4049. return;
  4050. }
  4051. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  4052. }
  4053. }
  4054. byte threeGunIndex = 0;
  4055. byte dcIndex = 0;
  4056. bool isGunUsingStatus = false;
  4057. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4058. {
  4059. // 如果有 AC 槍,且 DC 槍也有兩把
  4060. if (acDirIndex == 1 && _index == 1)
  4061. threeGunIndex = 1;
  4062. if (ShmOCPP16Data->CsMsg.bits[_index + threeGunIndex].RemoteStartTransactionReq == YES)
  4063. dcIndex = _index;
  4064. if (chargingInfo[_index]->SystemStatus != S_IDLE)
  4065. {
  4066. isGunUsingStatus = true;
  4067. }
  4068. }
  4069. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  4070. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  4071. {
  4072. if (dcIndex == 0)
  4073. threeGunIndex = 0;
  4074. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4075. return;
  4076. }
  4077. if (dcIndex == 0)
  4078. threeGunIndex = 0;
  4079. if (ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq == YES)
  4080. {
  4081. if (chargingInfo[dcIndex]->SystemStatus == S_IDLE ||
  4082. chargingInfo[dcIndex]->SystemStatus == S_RESERVATION)
  4083. {
  4084. chargingInfo[dcIndex]->RemoteStartFlag = YES;
  4085. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  4086. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  4087. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4088. DetectPluginStart();
  4089. }
  4090. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  4091. }
  4092. }
  4093. }
  4094. void ChkOcppStatus(byte gunIndex)
  4095. {
  4096. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  4097. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES)
  4098. {
  4099. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  4100. if (isReservationExpired(gunIndex))
  4101. {
  4102. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4103. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  4104. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  4105. chargingInfo[gunIndex]->SystemStatus = S_RESERVATION;
  4106. }
  4107. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  4108. }
  4109. if (chargingInfo[gunIndex]->SystemStatus == S_RESERVATION &&
  4110. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES)
  4111. {
  4112. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  4113. if (isReservationExpired(gunIndex))
  4114. {
  4115. PRINTF_FUNC("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  4116. DEBUG_ERROR_MSG("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  4117. chargingInfo[gunIndex]->ReservationId = 0;
  4118. chargingInfo[gunIndex]->SystemStatus = S_IDLE;
  4119. }
  4120. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationConf = YES;
  4121. }
  4122. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES)
  4123. {
  4124. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4125. DEBUG_ERROR_MSG("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  4126. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  4127. if(strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL)
  4128. {
  4129. if (isDb_ready)
  4130. DB_Update_Operactive(localDb, gunIndex, true);
  4131. chargingInfo[gunIndex]->IsAvailable = YES;
  4132. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  4133. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  4134. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  4135. {
  4136. setChargerMode(gunIndex, MODE_IDLE);
  4137. }
  4138. }
  4139. else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL)
  4140. {
  4141. if (isDb_ready)
  4142. DB_Update_Operactive(localDb, gunIndex, false);
  4143. chargingInfo[gunIndex]->IsAvailable = NO;
  4144. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  4145. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  4146. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  4147. {
  4148. setChargerMode(gunIndex, MODE_MAINTAIN);
  4149. }
  4150. }
  4151. }
  4152. if (ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq == YES)
  4153. {
  4154. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq = NO;
  4155. if (chargingInfo[gunIndex]->SystemStatus >= S_REASSIGN_CHECK &&
  4156. chargingInfo[gunIndex]->SystemStatus <= S_CHARGING)
  4157. {
  4158. // 充電中,需停止充電
  4159. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "UnlockCommand");
  4160. ChargingTerminalProcess(gunIndex);
  4161. }
  4162. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex].ResponseStatus, "Unlocked");
  4163. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorConf = YES;
  4164. }
  4165. }
  4166. bool CheckBackendChargingTimeout(byte gunIndex)
  4167. {
  4168. bool result = false;
  4169. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  4170. {
  4171. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  4172. {
  4173. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  4174. result = true;
  4175. }
  4176. }
  4177. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  4178. {
  4179. // 隨插即充電的要看 offline
  4180. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  4181. {
  4182. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  4183. result = true;
  4184. }
  4185. }
  4186. return result;
  4187. }
  4188. bool CheckBackendChargingEnergy(byte gunIndex)
  4189. {
  4190. bool result = false;
  4191. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  4192. {
  4193. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  4194. {
  4195. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  4196. result = true;
  4197. }
  4198. }
  4199. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  4200. {
  4201. // 隨插即充電的要看 offline
  4202. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  4203. {
  4204. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  4205. result = true;
  4206. }
  4207. }
  4208. return result;
  4209. }
  4210. void InformOcppErrOccur(byte codeType)
  4211. {
  4212. char _error[25];
  4213. switch(codeType)
  4214. {
  4215. case 4: strcpy(_error, "InternalError"); break;
  4216. case 6: strcpy(_error, "NoError"); break;
  4217. case 7: strcpy(_error, "OtherError"); break;
  4218. case 13: strcpy(_error, "UnderVoltage"); break;
  4219. case 14: strcpy(_error, "OverVoltage"); break;
  4220. }
  4221. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4222. {
  4223. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, _error);
  4224. }
  4225. }
  4226. //===============================================
  4227. // SQLite3 related routine
  4228. //===============================================
  4229. int DB_Open(sqlite3 *db)
  4230. {
  4231. int result = PASS;
  4232. char* errMsg = NULL;
  4233. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  4234. "idx integer primary key AUTOINCREMENT, "
  4235. "reservationId text, "
  4236. "transactionId text, "
  4237. "startMethod text, "
  4238. "userId text, "
  4239. "dateTimeStart text, "
  4240. "dateTimeStop text,"
  4241. "socStart text, "
  4242. "socStop text, "
  4243. "chargeEnergy text, "
  4244. "stopReason text"
  4245. ");";
  4246. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  4247. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  4248. "`IsAvailable` TEXT NOT NULL, "
  4249. "`connector` INTEGER NOT NULL, "
  4250. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  4251. if(sqlite3_open(DB_FILE, &db))
  4252. {
  4253. result = FAIL;
  4254. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4255. sqlite3_close(db);
  4256. }
  4257. else
  4258. {
  4259. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  4260. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  4261. {
  4262. result = FAIL;
  4263. PRINTF_FUNC( "Create local charging record table error message: %s\n", errMsg);
  4264. }
  4265. else
  4266. {
  4267. PRINTF_FUNC( "Opened local charging record table successfully\n");
  4268. }
  4269. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  4270. {
  4271. result = FAIL;
  4272. PRINTF_FUNC( "Create local config table error message: %s\n", errMsg);
  4273. }
  4274. else
  4275. {
  4276. PRINTF_FUNC( "Opened local config table successfully\n");
  4277. }
  4278. sqlite3_close(db);
  4279. }
  4280. return result;
  4281. }
  4282. int DB_Insert_Record(sqlite3 *db, int gun_index)
  4283. {
  4284. int result = PASS;
  4285. char* errMsg = NULL;
  4286. char insertSql[1024];
  4287. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  4288. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  4289. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  4290. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  4291. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  4292. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  4293. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  4294. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  4295. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  4296. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  4297. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  4298. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4299. if(sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db))
  4300. {
  4301. result = FAIL;
  4302. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4303. sqlite3_close(db);
  4304. }
  4305. else
  4306. {
  4307. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  4308. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  4309. {
  4310. result = FAIL;
  4311. PRINTF_FUNC( "Insert local charging record error message: %s\n", errMsg);
  4312. }
  4313. else
  4314. {
  4315. PRINTF_FUNC( "Insert local charging record successfully\n");
  4316. }
  4317. sqlite3_close(db);
  4318. }
  4319. return result;
  4320. }
  4321. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  4322. {
  4323. uint8_t result = false;
  4324. char* errMsg = NULL;
  4325. char sqlStr[1024];
  4326. srand(time(NULL));
  4327. if(sqlite3_open(DB_FILE, &db))
  4328. {
  4329. result = FAIL;
  4330. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4331. sqlite3_close(db);
  4332. }
  4333. else
  4334. {
  4335. PRINTF_FUNC( "Local charging record database open successfully (%d).\r\n", IsAvailable);
  4336. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  4337. PRINTF_FUNC("sqlStr= %s\r\n", sqlStr);
  4338. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  4339. {
  4340. result = FAIL;
  4341. PRINTF_FUNC( "update config error message: %s\n", errMsg);
  4342. }
  4343. else
  4344. {
  4345. PRINTF_FUNC("update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable);
  4346. }
  4347. sqlite3_close(db);
  4348. }
  4349. return result;
  4350. }
  4351. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  4352. {
  4353. uint8_t result = true;
  4354. char* errMsg = NULL;
  4355. char sqlStr[1024];
  4356. char **rs;
  4357. int rows, cols;
  4358. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  4359. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  4360. if(sqlite3_open(DB_FILE, &db))
  4361. {
  4362. result = FAIL;
  4363. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4364. sqlite3_close(db);
  4365. }
  4366. else
  4367. {
  4368. PRINTF_FUNC( "Local config query database open successfully.\r\n");
  4369. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  4370. if(rows>0)
  4371. {
  4372. for(int idxRow=1;idxRow<=rows;idxRow++)
  4373. {
  4374. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  4375. {
  4376. result = false;
  4377. }
  4378. PRINTF_FUNC("Query connector-%d isOperactive: %s\r\n", gun_index, rs[(idxRow*cols)+3]);
  4379. }
  4380. }
  4381. else
  4382. {
  4383. PRINTF_FUNC("Query connector-%d fail, set default value to operactive.\r\n", gun_index);
  4384. }
  4385. sqlite3_free_table(rs);
  4386. sqlite3_close(db);
  4387. }
  4388. return result;
  4389. }
  4390. //===============================================
  4391. // Config process
  4392. //===============================================
  4393. void AddPlugInTimes(byte gunIndex)
  4394. {
  4395. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  4396. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  4397. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  4398. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  4399. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  4400. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  4401. }
  4402. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  4403. {
  4404. char cmdBuf[32];
  4405. struct timeb csuTime;
  4406. struct tm *tmCSU;
  4407. ftime(&csuTime);
  4408. tmCSU = localtime(&csuTime.time);
  4409. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  4410. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4411. tmCSU->tm_sec);
  4412. if (isStart)
  4413. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  4414. else
  4415. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  4416. }
  4417. void zipLogFiles()
  4418. {
  4419. const char* logPath = "/Storage/SystemLog";
  4420. // 獲取目錄
  4421. DIR* pDir = opendir(logPath);
  4422. if (pDir != NULL)
  4423. {
  4424. struct timeb csuTime;
  4425. struct tm *tmCSU;
  4426. ftime(&csuTime);
  4427. tmCSU = localtime(&csuTime.time);
  4428. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  4429. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4430. // tmCSU->tm_sec);
  4431. // Read items inside the folder
  4432. struct dirent* pEntry = NULL;
  4433. while ((pEntry = readdir(pDir)) != NULL)
  4434. {
  4435. if (strcmp(pEntry->d_name, ".") != 0 &&
  4436. strcmp(pEntry->d_name, "..") != 0 &&
  4437. strncmp(pEntry->d_name, "[", 1) == 0 &&
  4438. strstr(pEntry->d_name, "tar") < 0)
  4439. {
  4440. char yearC[5];
  4441. unsigned short year = 0;
  4442. char monthC[3];
  4443. unsigned short month = 0;
  4444. yearC[4] = '\0';
  4445. strncpy(yearC, pEntry->d_name + 1, 4);
  4446. monthC[2] = '\0';
  4447. strncpy(monthC, pEntry->d_name + 6, 2);
  4448. year = atoi(yearC);
  4449. month = atoi(monthC);
  4450. if (year != 0)
  4451. {
  4452. if (year < tmCSU->tm_year + 1900 ||
  4453. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  4454. {
  4455. DEBUG_INFO_MSG("tar file name : %s \n", pEntry->d_name);
  4456. char file[256];
  4457. memset(file, 0x00, sizeof(file));
  4458. strcat(file, "tar zcvf ");
  4459. strcat(file, logPath);
  4460. strncat(file, "/", 1);
  4461. strcat(file, pEntry->d_name);
  4462. strcat(file, ".tar");
  4463. strncat(file, " ", 1);
  4464. strcat(file, logPath);
  4465. strncat(file, "/", 1);
  4466. strcat(file, pEntry->d_name);
  4467. PRINTF_FUNC("zip = %s \n", file);
  4468. system(file);
  4469. }
  4470. }
  4471. }
  4472. }
  4473. }
  4474. // Close folder
  4475. closedir(pDir);
  4476. }
  4477. void ChangeGunSelectByIndex(byte sel)
  4478. {
  4479. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  4480. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  4481. }
  4482. void CheckIsAlternatvieByModelName()
  4483. {
  4484. // 黑白機 ?
  4485. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWWU301J0UT1PH") == EQUAL ||
  4486. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYE301J0ET1PH") == EQUAL ||
  4487. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DSYE301J3EW2PH") == EQUAL ||
  4488. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYC301J0UW1PH") == EQUAL ||
  4489. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYC301J0UD1PH") == EQUAL)
  4490. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  4491. else
  4492. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  4493. }
  4494. void StopProcessingLoop()
  4495. {
  4496. for (;;)
  4497. {
  4498. CheckFactoryConfigFunction();
  4499. CheckFwUpdateFunction();
  4500. sleep(1);
  4501. }
  4502. }
  4503. void CreateWatchdog()
  4504. {
  4505. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  4506. {
  4507. wtdFd = InitWatchDog();
  4508. if (wtdFd < 0)
  4509. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  4510. }
  4511. }
  4512. bool IsConnectorWholeIdle()
  4513. {
  4514. bool result = true;
  4515. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  4516. {
  4517. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  4518. chargingInfo[count]->SystemStatus != S_RESERVATION)
  4519. {
  4520. result = false;
  4521. break;
  4522. }
  4523. }
  4524. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++)
  4525. {
  4526. if (ac_chargingInfo[count]->SystemStatus != S_IDLE &&
  4527. ac_chargingInfo[count]->IsErrorOccur == NO)
  4528. {
  4529. result = false;
  4530. break;
  4531. }
  4532. }
  4533. return result;
  4534. }
  4535. void ClearAlarmCodeWhenAcOff()
  4536. {
  4537. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  4538. {
  4539. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  4540. }
  4541. }
  4542. //==========================================
  4543. // Check task processing
  4544. //==========================================
  4545. void CheckTask()
  4546. {
  4547. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  4548. {
  4549. if(system("pidof -s Module_4g > /dev/null") != 0)
  4550. {
  4551. DEBUG_ERROR_MSG("Module_4g not running, restart it.\r\n");
  4552. system("/root/Module_4g &");
  4553. }
  4554. }
  4555. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  4556. {
  4557. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  4558. {
  4559. DEBUG_ERROR_MSG("Module_Wifi not running, restart it.\r\n");
  4560. system("/root/Module_Wifi &");
  4561. }
  4562. }
  4563. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  4564. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  4565. {
  4566. if(system("pidof -s OcppBackend > /dev/null") != 0)
  4567. {
  4568. DEBUG_ERROR_MSG("OcppBackend not running, restart it.\r\n");
  4569. system("/root/OcppBackend &");
  4570. }
  4571. }
  4572. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  4573. {
  4574. DEBUG_ERROR_MSG("Module_ProduceUtils not running, restart it.\r\n");
  4575. system ("/root/Module_ProduceUtils &");
  4576. }
  4577. }
  4578. void InitialDHCP()
  4579. {
  4580. char tmpbuf[256];
  4581. memset(tmpbuf,0,256);
  4582. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  4583. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  4584. system(tmpbuf);
  4585. }
  4586. void InitialDispenserDhcpServerConfig(void)
  4587. {
  4588. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  4589. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  4590. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  4591. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  4592. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  4593. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  4594. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  4595. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  4596. usleep(1000);
  4597. }
  4598. void StartDispenserDhcpServer(void)
  4599. {
  4600. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  4601. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  4602. }
  4603. //==========================================
  4604. // Check Smart Charging Profile
  4605. //==========================================
  4606. int GetStartScheduleTime(unsigned char *time)
  4607. {
  4608. int result = -1;
  4609. struct tm tmScheduleStart;
  4610. struct timeb tbScheduleStart;
  4611. if((sscanf((char *)time, "%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))
  4612. {
  4613. tmScheduleStart.tm_year -= 1900;
  4614. tmScheduleStart.tm_mon -= 1;
  4615. tbScheduleStart.time = mktime(&tmScheduleStart);
  4616. tbScheduleStart.millitm = 0;
  4617. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  4618. }
  4619. return result;
  4620. }
  4621. void CheckSmartChargeProfile(byte _index)
  4622. {
  4623. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == NO)
  4624. {
  4625. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq == NO)
  4626. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq = YES;
  4627. }
  4628. else
  4629. {
  4630. // Get Charging Profile
  4631. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf = NO;
  4632. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileKind, "Absolute") == EQUAL)
  4633. {
  4634. int _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  4635. byte _startCount = NO_DEFINE;
  4636. byte _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod);
  4637. for (byte _count = 0; _count < _maxCount; _count++)
  4638. {
  4639. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4640. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod &&
  4641. ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR)
  4642. {
  4643. _startCount = _count;
  4644. }
  4645. }
  4646. PRINTF_FUNC("_startCount = %d \n", _startCount);
  4647. if (_startCount < _maxCount)
  4648. {
  4649. PRINTF_FUNC("*********Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  4650. chargingInfo[_index]->ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * 10;
  4651. chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * chargingInfo[_index]->EvBatterytargetVoltage / 100;
  4652. //chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  4653. // if ((chargingInfo[_index]->EvBatterytargetVoltage * 10) > 0)
  4654. // {
  4655. // chargingInfo[_index]->ChargingProfileCurrent = chargingInfo[_index]->ChargingProfilePower / (chargingInfo[_index]->EvBatterytargetVoltage * 10);
  4656. // }
  4657. }
  4658. else
  4659. {
  4660. chargingInfo[_index]->ChargingProfilePower = -1;
  4661. chargingInfo[_index]->ChargingProfileCurrent = -1;
  4662. }
  4663. PRINTF_FUNC("ChargingProfilePower = %f \n", chargingInfo[_index]->ChargingProfilePower);
  4664. PRINTF_FUNC("ChargingProfileCurrent = %f \n", chargingInfo[_index]->ChargingProfileCurrent);
  4665. }
  4666. //
  4667. // printf("-------------Schedule------------\n");
  4668. // printf("index = %d \n", _index);
  4669. // printf("StartSchedule = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  4670. // printf("ChargingRateUnit = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingRateUnit);
  4671. // printf("----------SchedulePeriod---------\n");
  4672. // for (int v = 0; v < 10; v++)
  4673. // {
  4674. // printf("StartPeriod = %d \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].StartPeriod);
  4675. // printf("Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].Limit);
  4676. // }
  4677. // printf("---------------------------------\n");
  4678. }
  4679. }
  4680. void CheckReturnToChargingConn()
  4681. {
  4682. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  4683. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  4684. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  4685. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  4686. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG)
  4687. {
  4688. bool isReturnTimeout = false;
  4689. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  4690. {
  4691. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  4692. if (count == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  4693. {
  4694. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  4695. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  4696. {
  4697. isReturnTimeout = false;
  4698. break;
  4699. }
  4700. }
  4701. else if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  4702. {
  4703. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  4704. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  4705. {
  4706. isReturnTimeout = true;
  4707. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  4708. }
  4709. }
  4710. }
  4711. // AC 槍
  4712. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0)
  4713. {
  4714. // 沒有選中 AC,且 AC 在充電中
  4715. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  4716. (ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE))
  4717. {
  4718. // 當前 DC 充電槍在 idle 狀態
  4719. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_IDLE ||
  4720. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_RESERVATION)
  4721. {
  4722. isReturnTimeout = true;
  4723. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  4724. }
  4725. }
  4726. else if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  4727. ((chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= S_COMPLETE) ||
  4728. (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= S_CCS_PRECHARGE_ST1)))
  4729. {
  4730. // 當前 DC 充電槍在 idle 狀態
  4731. if (ac_chargingInfo[0]->SystemStatus == S_IDLE ||
  4732. ac_chargingInfo[0]->SystemStatus == S_RESERVATION)
  4733. {
  4734. isReturnTimeout = true;
  4735. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  4736. }
  4737. }
  4738. }
  4739. if (!isReturnTimeout)
  4740. StopSystemTimeoutDet();
  4741. }
  4742. }
  4743. bool GetStartChargingByAlterMode(byte _gun)
  4744. {
  4745. bool result = true;
  4746. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2 &&
  4747. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES)
  4748. {
  4749. for (byte _select = 0; _select < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _select++)
  4750. {
  4751. if (_select != _gun)
  4752. {
  4753. if (chargingInfo[_select]->SystemStatus != S_IDLE &&
  4754. chargingInfo[_select]->SystemStatus != S_RESERVATION)
  4755. {
  4756. result = false;
  4757. break;
  4758. }
  4759. }
  4760. }
  4761. }
  4762. return result;
  4763. }
  4764. struct timeval _dispenserDisconnection_time;
  4765. int main(void)
  4766. {
  4767. if(CreateShareMemory() == 0)
  4768. {
  4769. #ifdef SystemLogMessage
  4770. DEBUG_ERROR_MSG("CreatShareMemory NG \n");
  4771. #endif
  4772. if(ShmStatusCodeData!=NULL)
  4773. {
  4774. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  4775. }
  4776. sleep(5);
  4777. system("reboot -f");
  4778. sleep(5);
  4779. system("reboot -f");
  4780. }
  4781. if (!InitialSystemDefaultConfig())
  4782. {
  4783. DEBUG_ERROR_MSG("InitialSystemDefaultConfig NG \n");
  4784. StopProcessingLoop();
  4785. }
  4786. PRINTF_FUNC("SW Version = %s \n", fwVersion);
  4787. PRINTF_FUNC("Chademo = %d, CCS = %d, GB = %d, AC = %d, General: %d\n",
  4788. CHAdeMO_QUANTITY, CCS_QUANTITY, GB_QUANTITY, AC_QUANTITY, GENERAL_GUN_QUANTITY);
  4789. PRINTF_FUNC("CheckConnectorTypeStatus. \n");
  4790. // CheckGunTypeFromHw();
  4791. PRINTF_FUNC("CheckIsAlternatvie, ModelName = %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  4792. // CheckIsAlternatvieByModelName();
  4793. PRINTF_FUNC("InitialShareMemoryInfo \n");
  4794. InitialShareMemoryInfo();
  4795. PRINTF_FUNC("ChargerType (IEC or UL) = %d \n", ShmSysConfigAndInfo->SysInfo.ChargerType);
  4796. ChangeLcmByIndex(_LCM_INIT);
  4797. if (!CheckConnectorTypeStatus())
  4798. isModelNameMatch = false;
  4799. Initialization();
  4800. PRINTF_FUNC("Spawn all Task. \n");
  4801. SpawnTask();
  4802. if (!isModelNameMatch)
  4803. {
  4804. PRINTF_FUNC("Module Name & HW info none match. \n");
  4805. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  4806. ChangeLcmByIndex(_LCM_FIX);
  4807. // Module Name 與硬體對應不正確
  4808. DEBUG_ERROR_MSG("Module Name & HW info none match. \n");
  4809. sleep(3);
  4810. KillTask();
  4811. StopProcessingLoop();
  4812. }
  4813. PRINTF_FUNC("Module Name & HW info correct. Initialize.......\n");
  4814. CreateTimeoutFork();
  4815. PRINTF_FUNC("Self test. \n");
  4816. ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv = true; // green led
  4817. ShmPrimaryMcuData->OutputDrv.bits.SystemLed2Drv = true; // red led
  4818. SelfTestRun();
  4819. // StopSystemTimeoutDet();
  4820. PRINTF_FUNC("SelfTestSeq = %d, Work_Step = %d \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq, ShmPsuData->Work_Step);
  4821. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  4822. ShmPsuData->Work_Step == _NO_WORKING)
  4823. {
  4824. /*
  4825. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  4826. {
  4827. if (!DisplaySelfTestFailReason())
  4828. {
  4829. PRINTF_FUNC("Soft reboot for retry self-tets. \n");
  4830. sleep(3);
  4831. system("killall OcppBackend &");
  4832. KillTask();
  4833. system("/usr/bin/run_evse_restart.sh");
  4834. }
  4835. }
  4836. */
  4837. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4838. {
  4839. setChargerMode(gun_index, MODE_ALARM);
  4840. }
  4841. ChangeLcmByIndex(_LCM_FIX);
  4842. sleep(3);
  4843. KillTask();
  4844. StopProcessingLoop();
  4845. }
  4846. else
  4847. {
  4848. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4849. {
  4850. setChargerMode(gun_index, MODE_IDLE);
  4851. }
  4852. }
  4853. // Local DB
  4854. if(DB_Open(localDb) != PASS)
  4855. {
  4856. PRINTF_FUNC("DB_Open fail. \n");
  4857. isDb_ready = false;
  4858. }
  4859. else
  4860. {
  4861. isDb_ready = true;
  4862. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  4863. {
  4864. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  4865. }
  4866. }
  4867. ChangeLcmByIndex(_LCM_IDLE);
  4868. sleep(1);
  4869. //***** 須新增的偵測 *****//
  4870. // 1. Thernal - 控制風扇轉速
  4871. // 2. ouput fuse - 控制風扇轉速
  4872. // CreateRfidFork();
  4873. // Create Watchdog
  4874. //CreateWatchdog();
  4875. ShmPrimaryMcuData->OutputDrv.bits.SystemLed2Drv = false;
  4876. // Main loop
  4877. PRINTF_FUNC("****************************Main Loop********************************** \n");
  4878. gettimeofday(&_cmdMainPriority_time, NULL);
  4879. for (;;)
  4880. {
  4881. CheckOcppStatus();
  4882. ChkPrimaryStatus();
  4883. if ((IsConnectorWholeIdle() || ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX) &&
  4884. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_ReturnToChargingGunDet)
  4885. {
  4886. CheckFactoryConfigFunction();
  4887. CheckFwUpdateFunction();
  4888. }
  4889. // OCPP 邏輯
  4890. //OcppRemoteStartChk();
  4891. // 讀卡邏輯
  4892. //ScannerCardProcess();
  4893. PowerCabinetAuthorizeProcess();
  4894. // 當 AC 沒有搭上時,清除一些錯誤碼
  4895. ClearAlarmCodeWhenAcOff();
  4896. // 確認是否要回到充電中的槍畫面邏輯判斷
  4897. CheckReturnToChargingConn();
  4898. // if (_acgunIndex > 0 && isDetectPlugin() && !isCardScan)
  4899. // {
  4900. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  4901. // }
  4902. if ((GetTimeoutValue(_cmdMainPriority_time) / 1000) > 5000)
  4903. {
  4904. CheckTask();
  4905. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  4906. {
  4907. if (chargingInfo[_index]->SystemStatus == S_CHARGING)
  4908. CheckSmartChargeProfile(_index);
  4909. }
  4910. gettimeofday(&_cmdMainPriority_time, NULL);
  4911. PRINTF_FUNC("Gun 1(%d): %d(%s), Gun 2(%d): %d(%s), OCPP: %s",
  4912. chargingInfo[0]->Index, chargingInfo[0]->SystemStatus, chargingInfo[0]->IsAvailable ? "Operative" : "Inoperative",
  4913. chargingInfo[1]->Index, chargingInfo[1]->SystemStatus, chargingInfo[1]->IsAvailable ? "Operative" : "Inoperative",
  4914. ShmOCPP16Data->OcppConnStatus ? "On" : "Off");
  4915. // PRINTF_FUNC("Gun 1: %d(%d), Gun 2: %d(%d), Gun 3: %d(%d), Gun 4: %d(%d)",
  4916. // chargingInfo[0]->Index, chargingInfo[0]->Type,
  4917. // chargingInfo[1]->Index, chargingInfo[1]->Type,
  4918. // chargingInfo[2]->Index, chargingInfo[2]->Type,
  4919. // chargingInfo[3]->Index, chargingInfo[3]->Type);
  4920. }
  4921. if(ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  4922. {
  4923. ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv = false;
  4924. ShmPrimaryMcuData->OutputDrv.bits.SystemLed2Drv = true;
  4925. }
  4926. else
  4927. {
  4928. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.DispenserQuantity == 0)
  4929. {
  4930. if((GetTimeoutValue(_dispenserDisconnection_time) / 1000) >= 1000)
  4931. {
  4932. ShmPrimaryMcuData->OutputDrv.bits.SystemLed2Drv = ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv ? false : true;
  4933. ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv = ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv ? false : true;
  4934. gettimeofday(&_dispenserDisconnection_time, NULL);
  4935. }
  4936. }
  4937. else
  4938. {
  4939. ShmPrimaryMcuData->OutputDrv.bits.SystemLed1Drv = true;
  4940. ShmPrimaryMcuData->OutputDrv.bits.SystemLed2Drv = false;
  4941. }
  4942. }
  4943. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  4944. {
  4945. //CheckGpioInStatus();
  4946. CheckErrorOccurStatus(gun_index);
  4947. ChkOcppStatus(gun_index);
  4948. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false)
  4949. {
  4950. setChargerMode(gun_index, MODE_IDLE);
  4951. }
  4952. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  4953. switch(chargingInfo[gun_index]->SystemStatus)
  4954. {
  4955. case S_IDLE:
  4956. case S_RESERVATION:
  4957. case S_MAINTAIN:
  4958. case S_ALARM:
  4959. {
  4960. // clean PermissionRequest flag
  4961. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  4962. if (chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  4963. isModeChange(gun_index))
  4964. {
  4965. PRINTF_FUNC("S_IDLE================================== %x \n", gun_index);
  4966. chargingInfo[gun_index]->PresentChargedDuration = 0;
  4967. chargingInfo[gun_index]->RemainChargingDuration = 0;
  4968. chargingTime[gun_index] = 0;
  4969. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  4970. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  4971. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  4972. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  4973. }
  4974. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION &&
  4975. isModeChange(gun_index))
  4976. {
  4977. PRINTF_FUNC("S_RESERVATION....................%x \n", gun_index);
  4978. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = YES;
  4979. }
  4980. if (chargingInfo[gun_index]->IsAvailable == NO)
  4981. {
  4982. setChargerMode(gun_index, MODE_MAINTAIN);
  4983. }
  4984. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  4985. {
  4986. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected)
  4987. {
  4988. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  4989. }
  4990. ClearDetectPluginFlag();
  4991. setChargerMode(gun_index, MODE_ALARM);
  4992. }
  4993. else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  4994. {
  4995. // 不給充電
  4996. }
  4997. else
  4998. {
  4999. if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_FIX)
  5000. {
  5001. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5002. for (byte g_index = 0; g_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; g_index++)
  5003. setChargerMode(g_index, MODE_IDLE);
  5004. }
  5005. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  5006. {
  5007. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  5008. {
  5009. // 均充 -> 最大充
  5010. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  5011. {
  5012. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE)
  5013. {
  5014. PRINTF_FUNC("=============Smart Charging============= Step 11 \n");
  5015. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  5016. }
  5017. }
  5018. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  5019. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  5020. {
  5021. PRINTF_FUNC("=============Smart Charging============= Step 14 \n");
  5022. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  5023. }
  5024. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  5025. {
  5026. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  5027. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5028. }
  5029. }
  5030. else
  5031. {
  5032. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  5033. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5034. }
  5035. }
  5036. else
  5037. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5038. // clean stop charge flag
  5039. chargingInfo[gun_index]->StopChargeFlag = false;
  5040. { // Idle 正常程序起點
  5041. // 判斷是否有啟用檢查插槍
  5042. //if(isDetectPlugin())
  5043. if(IsConnectorAuthorizeSuccess(gun_index))
  5044. {
  5045. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  5046. chargingInfo[gun_index]->IsAvailable &&
  5047. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  5048. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  5049. {
  5050. AddPlugInTimes(gun_index);
  5051. ChangeGunSelectByIndex(gun_index);
  5052. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  5053. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5054. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AuthorizeRequestType == _AuthorizeSrc_Local)
  5055. {
  5056. AuthorizingSettingInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex, _AuthorizeSrc_Local);
  5057. }
  5058. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AuthorizeRequestType == _AuthorizeSrc_Remote)
  5059. {
  5060. AuthorizingSettingInitial(gun_index, _AuthorizeSrc_Remote);
  5061. }
  5062. continue;
  5063. }
  5064. /*
  5065. // 卡號驗證成功後,等待充電槍插入充電車
  5066. if (chargingInfo[gun_index]->RemoteStartFlag == YES)
  5067. {
  5068. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  5069. chargingInfo[gun_index]->IsAvailable)
  5070. {
  5071. PRINTF_FUNC("-----------------1----------------- %d \n", gun_index);
  5072. chargingInfo[gun_index]->RemoteStartFlag = NO;
  5073. chargingInfo[gun_index]->isRemoteStart = YES;
  5074. ChangeGunSelectByIndex(gun_index);
  5075. AddPlugInTimes(gun_index);
  5076. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5077. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  5078. ClearDetectPluginFlag();
  5079. continue;
  5080. }
  5081. }
  5082. else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE)
  5083. {
  5084. if (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable &&
  5085. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  5086. {
  5087. PRINTF_FUNC("-----------------2----------------- \n");
  5088. ChangeGunSelectByIndex(gun_index);
  5089. AddPlugInTimes(gun_index);
  5090. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  5091. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  5092. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  5093. // 當前操作的槍號,進入 Preparing
  5094. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5095. ClearDetectPluginFlag();
  5096. continue;
  5097. }
  5098. }
  5099. */
  5100. // if (!isCardScan)
  5101. // {
  5102. // LCM => Waiting for plugging
  5103. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  5104. // }
  5105. }
  5106. else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION)
  5107. {
  5108. if (!isReservationExpired(gun_index))
  5109. {
  5110. chargingInfo[gun_index]->SystemStatus = S_IDLE;
  5111. }
  5112. }
  5113. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  5114. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable))
  5115. {
  5116. PRINTF_FUNC("-----------------3----------------- \n");
  5117. bool isCanStartChargingFlag = GetStartChargingByAlterMode(gun_index);
  5118. if (isCanStartChargingFlag)
  5119. {
  5120. ChangeGunSelectByIndex(gun_index);
  5121. AddPlugInTimes(gun_index);
  5122. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5123. ClearDetectPluginFlag();
  5124. continue;
  5125. }
  5126. }
  5127. else
  5128. {
  5129. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5130. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  5131. }
  5132. } // Idle 正常程序終點
  5133. }
  5134. ReleaseAlarmCode(gun_index);
  5135. }
  5136. break;
  5137. case S_REASSIGN_CHECK:
  5138. {
  5139. if (isModeChange(gun_index))
  5140. {
  5141. PRINTF_FUNC("S_REASSIGN_CHECK================================== %x \n", gun_index);
  5142. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5143. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  5144. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  5145. StopSystemTimeoutDet();
  5146. }
  5147. bool isRessign = false;
  5148. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  5149. {
  5150. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  5151. {
  5152. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++)
  5153. {
  5154. // 有其他槍已經分配好 psu 模塊
  5155. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
  5156. chargingInfo[index]->SystemStatus >= S_PREPARNING &&
  5157. chargingInfo[index]->SystemStatus != S_MAINTAIN)
  5158. {
  5159. PRINTF_FUNC("=============Smart Charging============= Step 1 \n");
  5160. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_M_TO_A;
  5161. isRessign = true;
  5162. break;
  5163. }
  5164. }
  5165. }
  5166. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  5167. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE)
  5168. {
  5169. // 如果在切換最大充的過程中,需等待最大充切換完成後,在走均充流程
  5170. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES)
  5171. {
  5172. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  5173. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING)
  5174. {
  5175. PRINTF_FUNC("=============Smart Charging============= Step 14 \n");
  5176. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  5177. }
  5178. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP)
  5179. {
  5180. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  5181. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5182. continue;
  5183. }
  5184. }
  5185. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5186. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5187. continue;
  5188. }
  5189. }
  5190. if (isRessign)
  5191. setChargerMode(gun_index, MODE_REASSIGN);
  5192. else
  5193. setChargerMode(gun_index, MODE_PRECHARGE);
  5194. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5195. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5196. }
  5197. break;
  5198. case S_REASSIGN:
  5199. {
  5200. if (isModeChange(gun_index))
  5201. {
  5202. PRINTF_FUNC("S_REASSIGN================================== %x \n", gun_index);
  5203. gettimeofday(&_toAverage_time, NULL);
  5204. }
  5205. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  5206. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  5207. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
  5208. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  5209. {
  5210. if (ShmSysConfigAndInfo->SysInfo.CanAverageCharging)
  5211. setChargerMode(gun_index, MODE_PRECHARGE);
  5212. else
  5213. setChargerMode(gun_index, MODE_IDLE);
  5214. }
  5215. else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY_M_TO_A &&
  5216. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO)
  5217. {
  5218. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 6 \n");
  5219. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  5220. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5221. }
  5222. //PRINTF_FUNC("CurGunSelected = %d, gun_index = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected, gun_index);
  5223. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5224. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5225. }
  5226. break;
  5227. case S_PREPARNING:
  5228. {
  5229. if (isModeChange(gun_index))
  5230. {
  5231. PRINTF_FUNC("S_PREPARNING================================== %x \n", gun_index);
  5232. StopGunInfoTimeoutDet(gun_index);
  5233. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  5234. }
  5235. if (ShmPsuData->SystemPresentPsuQuantity > 0 && ShmPsuData->SystemAvailablePower > 10 &&
  5236. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 5000000)
  5237. {
  5238. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  5239. }
  5240. if (isEvBoardStopChargeFlag(gun_index) == YES || OcppRemoteStop(gun_index) == YES)
  5241. {
  5242. // 板端或後臺要求停止
  5243. PRINTF_FUNC("EvBoardStop: %d, OcppStop: %d \n", isEvBoardStopChargeFlag(gun_index), OcppRemoteStop(gun_index));
  5244. ChargingTerminalProcess(gun_index);
  5245. }
  5246. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5247. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5248. }
  5249. break;
  5250. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  5251. {
  5252. if (isModeChange(gun_index))
  5253. {
  5254. PRINTF_FUNC("S_PREPARING_FOR_EV================================== %x \n", gun_index);
  5255. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  5256. StopGunInfoTimeoutDet(gun_index);
  5257. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  5258. }
  5259. /*
  5260. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  5261. {
  5262. // 檢查車端的槍鎖是否為鎖上
  5263. if (isEvGunLocked_chademo(gun_index) == YES)
  5264. {
  5265. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  5266. }
  5267. }
  5268. else if (chargingInfo[gun_index]->Type == _Type_GB)
  5269. {
  5270. // 檢查車端的 charging enable 是否為 1
  5271. if (isEvGunLocked_gb(gun_index) == YES)
  5272. {
  5273. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  5274. }
  5275. }
  5276. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  5277. {
  5278. // 檢查車端的 charging enable 是否為 1
  5279. if (isEvGunLocked_ccs(gun_index) == YES)
  5280. {
  5281. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  5282. }
  5283. }
  5284. */
  5285. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  5286. {
  5287. setChargerMode(gun_index, MODE_CHARGING);
  5288. }
  5289. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  5290. OcppRemoteStop(gun_index) == YES)
  5291. {
  5292. // 板端或後臺要求停止
  5293. ChargingTerminalProcess(gun_index);
  5294. }
  5295. // LCM => Pre-charging
  5296. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5297. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5298. }
  5299. break;
  5300. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  5301. {
  5302. if (isModeChange(gun_index))
  5303. {
  5304. PRINTF_FUNC("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
  5305. StopGunInfoTimeoutDet(gun_index);
  5306. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  5307. }
  5308. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  5309. {
  5310. // 檢查樁端的 GFD 結果
  5311. if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8)
  5312. {
  5313. // 當前操作的槍號,進入 Charging
  5314. setChargerMode(gun_index, MODE_CHARGING);
  5315. }
  5316. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5317. {
  5318. // GFD 錯誤停止
  5319. RecordAlarmCode(gun_index, "012234");
  5320. }
  5321. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5322. {
  5323. // GFD 警告
  5324. RecordWarningCode(gun_index, "012296");
  5325. }
  5326. }
  5327. else if (chargingInfo[gun_index]->Type == _Type_GB)
  5328. {
  5329. // 檢查樁端的 GFD 結果
  5330. if (isPrechargeStatus_gb(gun_index) > 5 && isPrechargeStatus_gb(gun_index) < 9)
  5331. {
  5332. setChargerMode(gun_index, MODE_CHARGING);
  5333. }
  5334. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5335. {
  5336. // GFD 錯誤停止
  5337. RecordAlarmCode(gun_index, "012236");
  5338. }
  5339. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5340. {
  5341. // GFD 警告
  5342. RecordWarningCode(gun_index, "012298");
  5343. }
  5344. }
  5345. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  5346. {
  5347. // 檢查樁端的 GFD 結果
  5348. if ((chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  5349. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING))
  5350. {
  5351. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
  5352. }
  5353. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5354. {
  5355. // GFD 錯誤停止
  5356. RecordAlarmCode(gun_index, "012235");
  5357. }
  5358. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5359. {
  5360. // GFD 警告
  5361. RecordWarningCode(gun_index, "012297");
  5362. }
  5363. }
  5364. if (isEvBoardStopChargeFlag(gun_index) == YES ||
  5365. OcppRemoteStop(gun_index) == YES)
  5366. {
  5367. // 板端或後臺要求停止
  5368. ChargingTerminalProcess(gun_index);
  5369. }
  5370. // LCM => Pre-charging
  5371. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5372. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5373. }
  5374. break;
  5375. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  5376. {
  5377. if (isModeChange(gun_index))
  5378. {
  5379. PRINTF_FUNC("S_CHARGING================================== %x \n", gun_index);
  5380. StopGunInfoTimeoutDet(gun_index);
  5381. ftime(&startChargingTime[gun_index]);
  5382. ChangeStartOrStopDateTime(YES, gun_index);
  5383. OcppStartTransation(gun_index);
  5384. }
  5385. if (ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf)
  5386. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = NO;
  5387. ftime(&endChargingTime[gun_index]);
  5388. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  5389. // 計算 Power
  5390. chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  5391. if (chargingTime[gun_index] == 0 || chargingTime[gun_index] > chargingInfo[gun_index]->PresentChargedDuration)
  5392. {
  5393. chargingTime[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  5394. }
  5395. else
  5396. {
  5397. int passTime = chargingInfo[gun_index]->PresentChargedDuration - chargingTime[gun_index];
  5398. if(passTime > 0)
  5399. {
  5400. float changingPow = (chargingInfo[gun_index]->PresentChargingPower) * passTime / 3600;
  5401. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  5402. {
  5403. chargingInfo[gun_index]->ChargingFee += changingPow * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  5404. }
  5405. chargingInfo[gun_index]->PresentChargedEnergy += changingPow;
  5406. chargingInfo[gun_index]->PowerConsumption += changingPow;
  5407. chargingTime[gun_index] = chargingInfo[gun_index]->PresentChargedDuration;
  5408. }
  5409. }
  5410. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  5411. {
  5412. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5413. {
  5414. // GFD 錯誤停止
  5415. RecordAlarmCode(gun_index, "012234");
  5416. }
  5417. //else if (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  5418. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))
  5419. //{
  5420. // UVP
  5421. // RecordAlarmCode(gun_index, "012289");
  5422. // ChargingTerminalProcess(gun_index);
  5423. //}
  5424. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5425. {
  5426. // GFD 警告
  5427. RecordWarningCode(gun_index, "012296");
  5428. }
  5429. }
  5430. else if (chargingInfo[gun_index]->Type == _Type_GB)
  5431. {
  5432. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5433. {
  5434. // GFD 錯誤停止
  5435. RecordAlarmCode(gun_index, "012236");
  5436. }
  5437. //else if (isPrechargeStatus_gb(gun_index) == 10 &&
  5438. // (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  5439. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL)))
  5440. //{
  5441. // UVP
  5442. // RecordAlarmCode(gun_index, "012290");
  5443. // ChargingTerminalProcess(gun_index);
  5444. //}
  5445. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5446. {
  5447. // GFD 警告
  5448. RecordWarningCode(gun_index, "012298");
  5449. }
  5450. }
  5451. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  5452. {
  5453. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5454. {
  5455. // GFD 錯誤停止
  5456. RecordAlarmCode(gun_index, "012235");
  5457. }
  5458. //else if (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  5459. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))
  5460. //{
  5461. // UVP
  5462. // RecordAlarmCode(gun_index, "012288");
  5463. // ChargingTerminalProcess(gun_index);
  5464. //}
  5465. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5466. {
  5467. // GFD 警告
  5468. RecordWarningCode(gun_index, "012297");
  5469. }
  5470. }
  5471. if (isEvBoardStopChargeFlag(gun_index) ||
  5472. OcppRemoteStop(gun_index) ||
  5473. CheckBackendChargingTimeout(gun_index) ||
  5474. CheckBackendChargingEnergy(gun_index) ||
  5475. strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid") == EQUAL)
  5476. {
  5477. PRINTF_FUNC("S_CHARGING=======Stop=========================== %f \n", (chargingInfo[gun_index]->EvBatterytargetVoltage * 10));
  5478. // 板端或後臺要求停止
  5479. ChargingTerminalProcess(gun_index);
  5480. }
  5481. // LCM => Charging
  5482. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5483. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  5484. }
  5485. break;
  5486. case S_TERMINATING:
  5487. {
  5488. if (isModeChange(gun_index))
  5489. {
  5490. PRINTF_FUNC ("terminating......................... %x \n", gun_index);
  5491. StopGunInfoTimeoutDet(gun_index);
  5492. }
  5493. setChargerMode(gun_index, MODE_COMPLETE);
  5494. /*
  5495. if (chargingInfo[gun_index]->Type == _Type_Chademo)
  5496. {
  5497. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  5498. if (isEvStopCharging_chademo(gun_index) == YES)
  5499. {
  5500. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  5501. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  5502. }
  5503. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5504. {
  5505. // GFD 錯誤停止
  5506. RecordAlarmCode(gun_index, "012234");
  5507. }
  5508. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5509. {
  5510. // GFD 警告
  5511. RecordWarningCode(gun_index, "012296");
  5512. }
  5513. if (isEvStopCharging_chademo(gun_index) == YES ||
  5514. isPrechargeStatus_chademo(gun_index) <= 0)
  5515. {
  5516. setChargerMode(gun_index, MODE_COMPLETE);
  5517. }
  5518. }
  5519. else if (chargingInfo[gun_index]->Type == _Type_GB)
  5520. {
  5521. PRINTF_FUNC("************ GB lock Status = %d, status = %d \n", isEvStopCharging_gb(gun_index), isPrechargeStatus_gb(gun_index));
  5522. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  5523. if (isEvStopCharging_chademo(gun_index) == YES)
  5524. {
  5525. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  5526. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  5527. }
  5528. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5529. {
  5530. // GFD 錯誤停止
  5531. RecordAlarmCode(gun_index, "012236");
  5532. }
  5533. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5534. {
  5535. // GFD 警告
  5536. RecordWarningCode(gun_index, "012298");
  5537. }
  5538. if (isEvStopCharging_gb(gun_index) == YES ||
  5539. isPrechargeStatus_gb(gun_index) <= 0)
  5540. {
  5541. setChargerMode(gun_index, MODE_COMPLETE);
  5542. }
  5543. }
  5544. else if (chargingInfo[gun_index]->Type == _Type_CCS_2)
  5545. {
  5546. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  5547. if (isEvStopCharging_chademo(gun_index) == YES)
  5548. {
  5549. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  5550. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  5551. }
  5552. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5553. {
  5554. // GFD 錯誤停止
  5555. RecordAlarmCode(gun_index, "012235");
  5556. }
  5557. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)
  5558. {
  5559. // GFD 警告
  5560. RecordWarningCode(gun_index, "012297");
  5561. }
  5562. if (isEvStopCharging_ccs(gun_index) == YES &&
  5563. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  5564. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14))
  5565. {
  5566. setChargerMode(gun_index, MODE_COMPLETE);
  5567. }
  5568. }
  5569. // 車端的停止
  5570. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  5571. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  5572. */
  5573. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5574. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  5575. }
  5576. break;
  5577. case S_COMPLETE:
  5578. {
  5579. if (isModeChange(gun_index))
  5580. {
  5581. PRINTF_FUNC ("complete......................... %x \n", gun_index);
  5582. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  5583. {
  5584. OcppStopTransation(gun_index);
  5585. }
  5586. ftime(&endChargingTime[gun_index]);
  5587. if (chargingInfo[gun_index]->PresentChargedDuration != 0)
  5588. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  5589. chargingInfo[gun_index]->isRemoteStart = NO;
  5590. StopGunInfoTimeoutDet(gun_index);
  5591. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  5592. ChangeStartOrStopDateTime(NO, gun_index);
  5593. }
  5594. //if(chargingInfo[gun_index]->ConnectorPlugIn == NO &&
  5595. if(GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)
  5596. {
  5597. setChargerMode(gun_index, MODE_IDLE);
  5598. }
  5599. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index)
  5600. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  5601. }
  5602. break;
  5603. /*
  5604. case S_CCS_PRECHARGE_ST0:
  5605. {
  5606. if (isModeChange(gun_index))
  5607. {
  5608. PRINTF_FUNC("CCS Precharge Processing 1....................%x \n", gun_index);
  5609. StopGunInfoTimeoutDet(gun_index);
  5610. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  5611. }
  5612. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5613. {
  5614. // GFD 錯誤停止
  5615. RecordAlarmCode(gun_index, "012235");
  5616. }
  5617. if (isEvBoardStopChargeFlag(gun_index) == YES)
  5618. {
  5619. // 板端要求停止
  5620. ChargingTerminalProcess(gun_index);
  5621. }
  5622. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  5623. // 切換 D+ Relay to Precharge Relay
  5624. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40)
  5625. {
  5626. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  5627. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  5628. {
  5629. PRINTF_FUNC("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
  5630. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  5631. }
  5632. }
  5633. else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46)
  5634. {
  5635. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
  5636. }
  5637. break;
  5638. }
  5639. case S_CCS_PRECHARGE_ST1:
  5640. {
  5641. if (isModeChange(gun_index))
  5642. {
  5643. PRINTF_FUNC("CCS Precharge Processing 2....................%x \n", gun_index);
  5644. }
  5645. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL)
  5646. {
  5647. // GFD 錯誤停止
  5648. RecordAlarmCode(gun_index, "012235");
  5649. }
  5650. if (isEvBoardStopChargeFlag(gun_index) == YES)
  5651. {
  5652. // 板端要求停止
  5653. ChargingTerminalProcess(gun_index);
  5654. }
  5655. // 等待小板通知進入充電
  5656. // 切換 D+ Relay to Precharge Relay
  5657. if (chargingInfo[gun_index]->RelayK1K2Status == YES)
  5658. {
  5659. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  5660. setChargerMode(gun_index, MODE_CHARGING);
  5661. }
  5662. break;
  5663. }
  5664. */
  5665. }
  5666. }
  5667. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE)
  5668. {
  5669. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  5670. }
  5671. else
  5672. {
  5673. bool dcPageRun = true;
  5674. // if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE)
  5675. // {
  5676. // if (ac_chargingInfo[0]->SystemStatus == S_IDLE)
  5677. // ChangeLcmByIndex(_LCM_IDLE);
  5678. // else if (ac_chargingInfo[0]->SystemStatus == S_PREPARNING)
  5679. // ChangeLcmByIndex(_LCM_PRE_CHARGE);
  5680. // else if (ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  5681. // ChangeLcmByIndex(_LCM_CHARGING);
  5682. // else if (ac_chargingInfo[0]->SystemStatus == S_TERMINATING ||
  5683. // ac_chargingInfo[0]->SystemStatus == S_COMPLETE)
  5684. // ChangeLcmByIndex(_LCM_COMPLETE);
  5685. // else
  5686. // dcPageRun = true;
  5687. // }
  5688. // else
  5689. // dcPageRun = true;
  5690. if (dcPageRun)
  5691. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  5692. }
  5693. write(wtdFd, "a", 1);
  5694. usleep(whileLoopTime);
  5695. }
  5696. return FAIL;
  5697. }