SeccComm.c 652 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353
  1. /*===========================================================================
  2. Combined Charging System (CCS): SECC
  3. EVComm.c
  4. initiated by Vern, Joseph
  5. (since 2019/07/19)
  6. =============================================================================*/
  7. #include <netinet/ip.h>
  8. #include <arpa/inet.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <linux/termios.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include <stdlib.h>
  17. #include <sys/ipc.h>
  18. #include <sys/shm.h>
  19. #include <sys/mman.h>
  20. #include <linux/sockios.h>
  21. #include <linux/socket.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <sys/time.h>
  25. #include <sys/timeb.h>
  26. #include <math.h>//for pow
  27. #include <unistd.h>
  28. #include <linux/if_packet.h>
  29. #include <netinet/if_ether.h>
  30. #include <net/if.h>
  31. #include <netinet/ether.h>
  32. #include <errno.h>
  33. #include <linux/can.h>
  34. #include <linux/can/raw.h>
  35. #include "define.h"
  36. #include "SeccComm.h"
  37. #include "NidNmk.h"
  38. #include "SeccComm.h"
  39. #include "CsuComm.h"
  40. #include "./exi_engine/api/api.h"
  41. #include "./exi_engine/appHandshake/appHandEXIDatatypes.h"
  42. #include "./exi_engine/transport/v2gtp.h"
  43. int MeterSMId[8];
  44. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  45. struct StatusCodeData *ShmStatusCodeData;
  46. struct CcsData *ShmCcsData;
  47. struct InternalComm *ShmInternalComm;
  48. struct InternalCommAC *ShmInternalCommAC;
  49. struct appHandEXIDocument ccs_handshake;
  50. struct dinEXIDocument ccs_exi_doc_DIN;
  51. struct iso1EXIDocument ccs_exi_doc_ISO1;
  52. struct iso2EXIDocument ccs_exi_doc_ISO2;
  53. unsigned char V2GTP_Tx_buf[V2GTP_MSG_TX_BUFFER_SIZE]; //64*1024 ﺿ65,536 = 65.5KB
  54. unsigned char buf_log_evcomm[SIZE_OF_LOG_BUFFER];
  55. unsigned char buf_log_evcomm_fork1[SIZE_OF_LOG_BUFFER];
  56. unsigned char buf_log_evcomm_fork2[SIZE_OF_LOG_BUFFER];
  57. unsigned char buf_log_evcomm_fork3[SIZE_OF_LOG_BUFFER];
  58. pid_t CP_Detection_Pid = 0;
  59. pid_t PP_Detection_Pid = 0;
  60. pid_t Error_Monitor_Pid = 0;
  61. int TcpAcceptFd;
  62. enum MsgFlowStatus V2gFlowStatus;
  63. int RawSock,UdpSock,TcpSock;
  64. unsigned char *V2gtpMsgRxBuf, *V2gtpMsgTxBuf;
  65. unsigned short Aag[64];
  66. struct MmeHeader SendMmePacket;
  67. int SendMmePacketSize;
  68. unsigned char CsuMac[6],QcaMac[6],EvMac[6],SlacRunId[8];
  69. struct sockaddr_ll DestSocketAddress;
  70. struct ifreq Req;
  71. unsigned int PwmStartTime;
  72. struct timeb SeqStartTime, SeqEndTime;
  73. unsigned char AagGroupsNum, MnbcSoundNum, AttenProfileCnt;
  74. unsigned char NewNmkKey[16], Nid[7];
  75. /*===========================================================================
  76. FUNCTION: Check_V2G_Flow_Status
  77. DESCRIPTION:
  78. PRE-CONDITION:
  79. INPUT:
  80. OUTPUT:
  81. GLOBAL VARIABLES:
  82. =============================================================================*/
  83. unsigned char Check_V2G_Flow_Status()
  84. {
  85. unsigned char result = 0;
  86. switch (ShmCcsData->CommProtocol)
  87. {
  88. case V2GT_MSG_PROTOCOL_DIN70121: //0
  89. {
  90. result = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus;
  91. break;
  92. }
  93. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  94. {
  95. result = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus;
  96. break;
  97. }
  98. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  99. {
  100. result = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus;
  101. break;
  102. }
  103. default:
  104. break;
  105. }
  106. return result;
  107. }
  108. /*===========================================================================
  109. FUNCTION: StoreLogMsg
  110. DESCRIPTION:
  111. PRE-CONDITION:
  112. INPUT:
  113. OUTPUT:
  114. GLOBAL VARIABLES:
  115. =============================================================================*/
  116. #if SAVE_SYS_LOG_MSG_EVCOMM_SWITCH == ENABLE
  117. int StoreLogMsg(unsigned char *DataString)
  118. {
  119. static unsigned char Buf[1024];
  120. static time_t CurrentTime;
  121. static struct tm *tm;
  122. static struct timeval tv;
  123. memset(Buf, 0, sizeof(Buf));
  124. CurrentTime = time(NULL);
  125. tm = localtime(&CurrentTime);
  126. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  127. sprintf(Buf, "echo \"[%04d%02d%02d: %02d:%02d:%02d.%06d][SECC][%d][%02d]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  128. tm->tm_year + 1900,
  129. tm->tm_mon + 1,
  130. tm->tm_mday,
  131. tm->tm_hour,
  132. tm->tm_min,
  133. tm->tm_sec,
  134. tv.tv_usec,
  135. EVCOMM_SYS_INFO.CpState,
  136. Check_V2G_Flow_Status(),
  137. DataString,
  138. tm->tm_year + 1900,
  139. tm->tm_mon + 1);
  140. system(Buf);
  141. DEBUG_PRINTF_EVCOMM_SYSTEM_LOG("[%02d:%02d:%02d.%06d][SECC][%d][%02d]%s \n",
  142. tm->tm_hour,
  143. tm->tm_min,
  144. tm->tm_sec,
  145. tv.tv_usec,
  146. EVCOMM_SYS_INFO.CpState,
  147. Check_V2G_Flow_Status(),
  148. DataString);
  149. //Reset the buf_log_evcomm Buffer, i.e. DataString
  150. memset(buf_log_evcomm, 0, SIZE_OF_LOG_BUFFER);
  151. }
  152. #endif
  153. /*===========================================================================
  154. FUNCTION: CAN_Tx_MSG
  155. DESCRIPTION:
  156. PRE-CONDITION:
  157. INPUT:
  158. OUTPUT:
  159. GLOBAL VARIABLES:
  160. =============================================================================*/
  161. int CAN_Tx_MSG(int Fd, unsigned int MsgId, unsigned char SlaveAddress, unsigned char DataLength, unsigned char *SendData)
  162. {
  163. struct can_frame frame;
  164. struct timeb StartTime, EndTime;
  165. unsigned int tmp = 0;
  166. int nbytes = 0;
  167. int i = 0;
  168. //Protection: To avoid unexpected length for CAN bus payload.
  169. if (DataLength > 8)
  170. {
  171. DataLength = 8;
  172. }
  173. memset(&frame, 0, sizeof(struct can_frame));
  174. frame.can_id = 0x80000000 | CAN_SEND_DIRECTION | MsgId | SlaveAddress; //0x80000000: extension ID format
  175. frame.can_dlc = DataLength;
  176. memcpy(frame.data, SendData, DataLength);
  177. nbytes = write(Fd, &frame, sizeof(struct can_frame));
  178. #if 0
  179. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][CAN_Tx_MSG] <%X> ", frame.can_id);
  180. for (i = 0; i < frame.can_dlc; i++)
  181. {
  182. DEBUG_PRINTF_EVCOMM_DETAIL("%02X ", frame.data[i]);
  183. }
  184. DEBUG_PRINTF_EVCOMM_DETAIL("(%d Bytes)\n", frame.can_dlc);
  185. #endif
  186. return nbytes;
  187. }
  188. /*===========================================================================
  189. FUNCTION: Proc_EVStopRes
  190. DESCRIPTION:
  191. PRE-CONDITION:
  192. INPUT:
  193. OUTPUT:
  194. GLOBAL VARIABLES:
  195. =============================================================================*/
  196. int Proc_EVStopRes(int Fd)
  197. {
  198. int nbytes;
  199. unsigned char Buffer[8];
  200. memset(Buffer, 0, sizeof(Buffer));
  201. if (CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency == TRUE)
  202. {
  203. Buffer[0] = EV_EMERGENCY_STOP; //2
  204. }
  205. else
  206. {
  207. Buffer[0] = EV_NORMAL_STOP; //1
  208. }
  209. /*
  210. if(strlen(ShmStatusCodeData->PresentStatusCode[0]) > 0)
  211. {
  212. memcpy(Buffer + 2, ShmStatusCodeData->PresentStatusCode[0], 6);
  213. }
  214. */
  215. Buffer[1] = ShmStatusCodeData->PresentStatusCode[0][0];
  216. Buffer[2] = ShmStatusCodeData->PresentStatusCode[0][1];
  217. Buffer[3] = ShmStatusCodeData->PresentStatusCode[0][2];
  218. Buffer[4] = ShmStatusCodeData->PresentStatusCode[0][3];
  219. Buffer[5] = ShmStatusCodeData->PresentStatusCode[0][4];
  220. Buffer[6] = ShmStatusCodeData->PresentStatusCode[0][5];
  221. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_EV_STOP_EVENT, ShmInternalComm->SlaveAddress, 7, Buffer);
  222. //system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"); //for test only
  223. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  224. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_EVStopRes] Sending STOP Command to CSU");
  225. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = FALSE;
  226. return nbytes;
  227. }
  228. /*===========================================================================
  229. FUNCTION: Sniffer_Candump
  230. DESCRIPTION:
  231. 1. Only used in End_Process(), which is in fork2 of SeccComm task.
  232. PRE-CONDITION:
  233. INPUT:
  234. OUTPUT:
  235. 0: accept
  236. -1: invalid
  237. GLOBAL VARIABLES:
  238. =============================================================================*/
  239. int Sniffer_Candump(char cmd)
  240. {
  241. #if (CANDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  242. if (cmd == ENABLE)
  243. {
  244. SAVE_SYS_LOG_MSG_EVCOMM("[candump]init");
  245. system("cd /mnt/;rm -rf candump/");
  246. system("cd /mnt/;mkdir candump");
  247. SAVE_SYS_LOG_MSG_EVCOMM("[candump]on");
  248. system("cd /mnt/candump;candump -l can0 &");
  249. return 0;
  250. }
  251. else if (cmd == DISABLE)
  252. {
  253. SAVE_SYS_LOG_MSG_EVCOMM("[candump]off");
  254. system("killall candump");
  255. SAVE_SYS_LOG_MSG_EVCOMM("[candump]save");
  256. system("cd /;cp -rfv /mnt/candump /Storage/SystemLog/");
  257. return 0;
  258. }
  259. else
  260. {
  261. sprintf(buf_log_evcomm_fork2, "[candump]unexpected cmd(%d)", cmd);
  262. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork2);
  263. return -1;
  264. }
  265. #endif
  266. }
  267. /*===========================================================================
  268. FUNCTION: Sniffer_Tcpdump
  269. DESCRIPTION:
  270. PRE-CONDITION:
  271. INPUT:
  272. OUTPUT:
  273. GLOBAL VARIABLES:
  274. =============================================================================*/
  275. int Sniffer_Tcpdump(char cmd)
  276. {
  277. #if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  278. if (cmd == ENABLE)
  279. {
  280. SAVE_SYS_LOG_MSG_EVCOMM("[tcpdump]init...");
  281. system("cd /mnt/;rm -rf tcpdump/");
  282. system("cd /mnt/;mkdir tcpdump");
  283. unsigned char buf[256];
  284. time_t CurrentTime;
  285. struct tm *tm;
  286. struct timeval tv;
  287. memset(buf, 0, sizeof(buf));
  288. CurrentTime = time(NULL);
  289. tm = localtime(&CurrentTime);
  290. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  291. sprintf(buf,
  292. "tcpdump -i eth1 -w /mnt/tcpdump/[%s][%04d%02d%02d-%02d:%02d:%02d.%06d]packets.pcap&",
  293. FIRMWARE_VERSION,
  294. tm->tm_year + 1900,
  295. tm->tm_mon + 1,
  296. tm->tm_mday,
  297. tm->tm_hour,
  298. tm->tm_min,
  299. tm->tm_sec,
  300. tv.tv_usec
  301. );
  302. SAVE_SYS_LOG_MSG_EVCOMM("[tcpdump]on");
  303. system(buf);
  304. return 0;
  305. }
  306. else if (cmd == DISABLE)
  307. {
  308. SAVE_SYS_LOG_MSG_EVCOMM("[tcpdump]off");
  309. system("killall tcpdump");
  310. SAVE_SYS_LOG_MSG_EVCOMM("[tcpdump]wait: 2s");
  311. sleep(1);
  312. SAVE_SYS_LOG_MSG_EVCOMM("[tcpdump]wait: 1s");
  313. sleep(1);
  314. SAVE_SYS_LOG_MSG_EVCOMM("[tcpdump]save");
  315. system("cd /;cp -rfv /mnt/tcpdump /Storage/SystemLog/");
  316. return 0;
  317. }
  318. else
  319. {
  320. sprintf(buf_log_evcomm, "[tcpdump]unexpected cmd(%d)", cmd);
  321. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  322. return -1;
  323. }
  324. #endif
  325. }
  326. /*===========================================================================
  327. FUNCTION: Check_V2G_Flow_Status_pre
  328. DESCRIPTION:
  329. PRE-CONDITION:
  330. INPUT:
  331. OUTPUT:
  332. GLOBAL VARIABLES:
  333. =============================================================================*/
  334. unsigned char Check_V2G_Flow_Status_pre()
  335. {
  336. unsigned char result = 0;
  337. switch (ShmCcsData->CommProtocol)
  338. {
  339. case V2GT_MSG_PROTOCOL_DIN70121: //0
  340. {
  341. result = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus_pre;
  342. break;
  343. }
  344. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  345. {
  346. result = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus_pre;
  347. break;
  348. }
  349. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  350. {
  351. result = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus_pre;
  352. break;
  353. }
  354. default:
  355. break;
  356. }
  357. return result;
  358. }
  359. /*===========================================================================
  360. FUNCTION: Update_V2G_Flow_Status
  361. DESCRIPTION:
  362. PRE-CONDITION:
  363. INPUT:
  364. OUTPUT:
  365. 1. V2gFlowStatus
  366. GLOBAL VARIABLES:
  367. 1. V2gFlowStatus
  368. =============================================================================*/
  369. int Update_V2G_Flow_Status(unsigned int state_new)
  370. {
  371. unsigned char state_now;
  372. state_now = Check_V2G_Flow_Status();
  373. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE ||
  374. CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused == TRUE)
  375. {
  376. if (state_now != Performance_Timeout && //253
  377. state_now != Sequence_Timeout && //254
  378. state_now != Other_Fault) //255
  379. {
  380. sprintf(buf_log_evcomm,
  381. "[Warning]state(%d) change: ignored (End_Process_inused:%d, FW_Update_Task_inused:%d)",
  382. state_new,
  383. EVCOMM_SYS_INFO.End_Process_inused,
  384. CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused
  385. );
  386. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  387. return -1;
  388. }
  389. }
  390. if (state_now != state_new)
  391. {
  392. //Step 1: Check if Error Occours
  393. if (state_now == Performance_Timeout || //253
  394. state_now == Sequence_Timeout || //254
  395. state_now == Other_Fault) //255
  396. {
  397. if (state_new != IDLE)
  398. {
  399. if (EVCOMM_SYS_INFO.State_Change_Ignored_Notice == FALSE)
  400. {
  401. EVCOMM_SYS_INFO.State_Change_Ignored_Notice = TRUE;
  402. sprintf(buf_log_evcomm,
  403. "[Warning]state(%d) change: ignored(now in error state)",
  404. state_new,
  405. state_now);
  406. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  407. }
  408. return -1;
  409. }
  410. }
  411. //Updating the state formally.
  412. sprintf(buf_log_evcomm,
  413. "[stt]%02d >> %02d",
  414. state_now,
  415. state_new
  416. );
  417. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  418. V2gFlowStatus = state_new;
  419. state_now = state_new;
  420. switch (ShmCcsData->CommProtocol)
  421. {
  422. case V2GT_MSG_PROTOCOL_DIN70121: //0
  423. {
  424. ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus;
  425. ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus = V2gFlowStatus;
  426. //break;
  427. }
  428. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  429. {
  430. ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus;
  431. ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus = V2gFlowStatus;
  432. //break;
  433. }
  434. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  435. {
  436. ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus;
  437. ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus = V2gFlowStatus;
  438. //break;
  439. }
  440. default:
  441. break;
  442. }
  443. }
  444. }
  445. double DiffTimeb(struct timeb ST, struct timeb ET)
  446. {
  447. //return milli-second
  448. double StartTime, EndTime;
  449. double t_diff;
  450. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  451. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  452. t_diff = EndTime - StartTime;
  453. if (t_diff < 0)
  454. {
  455. #if 0
  456. if (t_diff < -1000) //1000ms
  457. {
  458. sprintf(buf_log_evcomm,
  459. "[Warning]StartTime(%.02lf) > EndTime(%.02lf), d(%.02lf)",
  460. StartTime,
  461. EndTime,
  462. t_diff);
  463. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  464. }
  465. #endif
  466. return -1;
  467. }
  468. return t_diff;
  469. }
  470. double DiffTimeb_fork1_Error_Monitor(struct timeb ST, struct timeb ET)
  471. {
  472. //return milli-second
  473. static double StartTime, EndTime;
  474. static double t_diff;
  475. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  476. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  477. t_diff = EndTime - StartTime;
  478. if (t_diff < 0)
  479. {
  480. #if 0
  481. if (t_diff < -1000) //1000ms
  482. {
  483. sprintf(buf_log_evcomm_fork2,
  484. "[fork1][Warning]S(%.02lf) > E(%.02lf), d(%.02lf)",
  485. StartTime,
  486. EndTime,
  487. t_diff);
  488. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork2);
  489. }
  490. #endif
  491. return -1;
  492. }
  493. return t_diff;
  494. }
  495. double DiffTimeb_fork2_Error_Monitor(struct timeb ST, struct timeb ET)
  496. {
  497. //return milli-second
  498. static double StartTime, EndTime;
  499. static double t_diff;
  500. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  501. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  502. t_diff = EndTime - StartTime;
  503. if (t_diff < 0)
  504. {
  505. #if 0
  506. if (t_diff < -1000) //1000ms
  507. {
  508. sprintf(buf_log_evcomm_fork2,
  509. "[fork2][Warning]S(%.02lf) > E(%.02lf), d(%.02lf)",
  510. StartTime,
  511. EndTime,
  512. t_diff);
  513. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork2);
  514. }
  515. #endif
  516. return -1;
  517. }
  518. return t_diff;
  519. }
  520. int CreateShareMemory()
  521. {
  522. int MeterSMId;
  523. //create ShmSysConfigAndInfo
  524. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  525. {
  526. SAVE_SYS_LOG_MSG_EVCOMM("[main]CreateShareMemory:shmget ShmSysConfigAndInfo NG");
  527. return 0;
  528. }
  529. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  530. {
  531. SAVE_SYS_LOG_MSG_EVCOMM("[main]CreateShareMemory:shmat ShmSysConfigAndInfo NG");
  532. return 0;
  533. }
  534. memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
  535. //create ShmStatusCodeData
  536. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  537. {
  538. SAVE_SYS_LOG_MSG_EVCOMM("[main]CreateShareMemory:shmget ShmStatusCodeData NG");
  539. return 0;
  540. }
  541. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  542. {
  543. SAVE_SYS_LOG_MSG_EVCOMM("[main]CreateShareMemory:shmat ShmStatusCodeData NG");
  544. return 0;
  545. }
  546. memset(ShmStatusCodeData,0,sizeof(struct StatusCodeData));
  547. //create ShmCcsData
  548. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  549. {
  550. SAVE_SYS_LOG_MSG_EVCOMM("[main]CreateShareMemory:shmget ShmCcsData NG");
  551. return 0;
  552. }
  553. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  554. {
  555. SAVE_SYS_LOG_MSG_EVCOMM("[main]CreateShareMemory:shmat ShmCcsData NG");
  556. return 0;
  557. }
  558. memset(ShmCcsData,0,sizeof(struct CcsData));
  559. return 1;
  560. }
  561. void DetachShareMemory()
  562. {
  563. if(MeterSMId[0]>0)
  564. {
  565. shmdt(ShmSysConfigAndInfo);
  566. shmctl(MeterSMId[0], IPC_RMID, 0);
  567. }
  568. if(MeterSMId[1]>0)
  569. {
  570. shmdt(ShmStatusCodeData);
  571. shmctl(MeterSMId[1], IPC_RMID, 0);
  572. }
  573. if(MeterSMId[2]>0)
  574. {
  575. shmdt(ShmCcsData);
  576. shmctl(MeterSMId[2], IPC_RMID, 0);
  577. }
  578. if(MeterSMId[3]>0)
  579. {
  580. shmdt(ShmInternalComm);
  581. shmctl(MeterSMId[3], IPC_RMID, 0);
  582. }
  583. if(MeterSMId[4]>0)
  584. {
  585. shmdt(ShmInternalCommAC);
  586. shmctl(MeterSMId[4], IPC_RMID, 0);
  587. }
  588. }
  589. /*===========================================================================
  590. FUNCTION: ShareMemory_Init
  591. DESCRIPTION:
  592. PRE-CONDITION:
  593. INPUT:
  594. OUTPUT:
  595. GLOBAL VARIABLES:
  596. =============================================================================*/
  597. int ShareMemory_Init()
  598. {
  599. memset(MeterSMId,0,sizeof(MeterSMId));
  600. //create ShmSysConfigAndInfo
  601. if ((MeterSMId[0] = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  602. {
  603. SAVE_SYS_LOG_MSG_EVCOMM("ShareMemory_Init:shmget ShmSysConfigAndInfo NG");
  604. return 0;
  605. }
  606. else if ((ShmSysConfigAndInfo = shmat(MeterSMId[0], NULL, 0)) == (void *) -1)
  607. {
  608. SAVE_SYS_LOG_MSG_EVCOMM("ShareMemory_Init:shmat ShmSysConfigAndInfo NG");
  609. return 0;
  610. }
  611. //create ShmStatusCodeData
  612. if ((MeterSMId[1] = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  613. {
  614. SAVE_SYS_LOG_MSG_EVCOMM("ShareMemory_Init:shmget ShmStatusCodeData NG");
  615. return 0;
  616. }
  617. else if ((ShmStatusCodeData = shmat(MeterSMId[1], NULL, 0)) == (void *) -1)
  618. {
  619. SAVE_SYS_LOG_MSG_EVCOMM("ShareMemory_Init:shmat ShmStatusCodeData NG");
  620. return 0;
  621. }
  622. //create ShmCcsData
  623. if ((MeterSMId[2] = shmget(ShmCcsCommKey, sizeof(struct CcsData), 0777)) < 0)
  624. {
  625. SAVE_SYS_LOG_MSG_EVCOMM("ShareMemory_Init:shmget ShmCcsData NG");
  626. return 0;
  627. }
  628. else if ((ShmCcsData = shmat(MeterSMId[2], NULL, 0)) == (void *) -1)
  629. {
  630. SAVE_SYS_LOG_MSG_EVCOMM("ShareMemory_Init:shmat ShmCcsData NG");
  631. return 0;
  632. }
  633. //create ShmInternalComm
  634. if ((MeterSMId[3] = shmget(ShmInternalCommKey, sizeof(struct InternalComm), 0777)) < 0)
  635. {
  636. SAVE_SYS_LOG_MSG_EVCOMM("ShareMemory_Init:shmget ShmInternalComm NG");
  637. return 0;
  638. }
  639. else if ((ShmInternalComm = shmat(MeterSMId[3], NULL, 0)) == (void *) -1)
  640. {
  641. SAVE_SYS_LOG_MSG_EVCOMM("ShareMemory_Init:shmat ShmInternalComm NG");
  642. return 0;
  643. }
  644. //create ShmInternalCommAC
  645. if ((MeterSMId[4] = shmget(ShmInternalCommACKey, sizeof(struct InternalCommAC), 0777)) < 0)
  646. {
  647. SAVE_SYS_LOG_MSG_EVCOMM("ShareMemory_Init:shmget ShmInternalCommAC NG");
  648. return 0;
  649. }
  650. else if ((ShmInternalCommAC = shmat(MeterSMId[4], NULL, 0)) == (void *) -1)
  651. {
  652. SAVE_SYS_LOG_MSG_EVCOMM("ShareMemory_Init:shmat ShmInternalCommAC NG");
  653. return 0;
  654. }
  655. //[To-Do] The initialization here is reduntant and should be removed partially.
  656. //SHM_Init_supportedAppProtocolRes(ShmCcsData);
  657. //SHM_Init_din_SessionSetupRes(ShmCcsData);
  658. //SHM_Init_din_ServiceDiscoveryRes(ShmCcsData);
  659. //SHM_Init_din_ServiceAndPaymentSelectionRes(ShmCcsData);
  660. //SHM_Init_din_ContractAuthenticationRes(ShmCcsData);
  661. SHM_Init_din_ChargeParameterDiscoveryRes(ShmCcsData);
  662. SHM_Init_din_CableCheckRes(ShmCcsData);
  663. SHM_Init_din_PreChargeRes(ShmCcsData);
  664. SHM_Init_din_PowerDeliveryRes(ShmCcsData);
  665. SHM_Init_din_CurrentDemandRes(ShmCcsData);
  666. SHM_Init_din_WeldingDetectionRes(ShmCcsData);
  667. SHM_Init_din_SessionStopRes(ShmCcsData);
  668. return 1;
  669. }
  670. /*===========================================================================
  671. FUNCTION: GetEthMac
  672. DESCRIPTION:
  673. 1. Accessing current CsuMac address, if Eth = eth1
  674. 2. The address of eth1(for QCA7000) is a random number, which will be modified
  675. after each time of booting up system.
  676. PRE-CONDITION:
  677. INPUT:
  678. OUTPUT:
  679. GLOBAL VARIABLES:
  680. =============================================================================*/
  681. int GetEthMac(unsigned char *Eth, unsigned char *mac)
  682. {
  683. //Parameters:MAC,IP,Mask,Gateway
  684. int fd, rd = 0;
  685. unsigned char addr[18], Buffer[128];
  686. memset(Buffer, 0, sizeof(Buffer));
  687. sprintf(Buffer, "cat /sys/class/net/%s/address > /mnt/GetEthInfo", Eth); //CsuMac (Eth = eth1)
  688. system(Buffer);
  689. fd = open("/mnt/GetEthInfo", O_RDONLY);
  690. if(fd < 0)
  691. {
  692. system("rm -f /mnt/GetEthInfo");
  693. SAVE_SYS_LOG_MSG_EVCOMM("GetEthMac: MAC Address open error");
  694. return 0;
  695. }
  696. memset(mac, 0, 6);
  697. memset(addr, 0, sizeof(addr));
  698. rd = read(fd, addr, 17);
  699. close(fd);
  700. system("rm -f /mnt/GetEthInfo");
  701. sscanf(addr, "%02x:%02x:%02x:%02x:%02x:%02x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); //CsuMac (Eth = eth1)
  702. sprintf(buf_log_evcomm,
  703. "[GetEthMac]EVSE MACAddr(CsuMac): %02x:%02x:%02x:%02x:%02x:%02x",
  704. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); //CsuMac (Eth = eth1)
  705. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  706. return 1;
  707. }
  708. /*===========================================================================
  709. FUNCTION: ReadAdcVolt
  710. DESCRIPTION:
  711. 1. fork1
  712. PRE-CONDITION:
  713. INPUT:
  714. OUTPUT:
  715. GLOBAL VARIABLES:
  716. =============================================================================*/
  717. float ReadAdcVolt(unsigned char AdcChannel)
  718. {
  719. //AIN0=CCS GUN Temp 1
  720. //AIN1=CCS GUN Temp 2
  721. //AIN2=CCS_Proximity/2
  722. //AIN3=pilot voltage
  723. if(AdcChannel == 3)
  724. {
  725. int fd,count,AvgTimes;
  726. unsigned char SampleBuf[4];
  727. float TmpVolt, MinSample, AvgSample = 0;
  728. fd = open("/sys/bus/iio/devices/iio\:device0/in_voltage3_raw", O_RDONLY);
  729. if(fd > 0)
  730. {
  731. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  732. for(AvgTimes = 0; AvgTimes < 3; AvgTimes++) //period = 60~91 ms(renice -10, , CurrentDemand()) /*+++ 20200909, vern, extend detection time for interference ---*/
  733. {
  734. count = 0;
  735. MinSample = 2306;
  736. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  737. while(count < 40) //period = 21~42ms (renice -10, CurrentDemand())/*+++ 20200909, vern, extend detection time for interference ---*/
  738. {
  739. //re-sampling period = 3~13ms (renice -10, SLAC())
  740. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  741. read(fd, SampleBuf, 4); //period = 3.2~10ms (renice -10, SLAC())
  742. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  743. TmpVolt = atoi(SampleBuf);
  744. if((TmpVolt < 2306) && (TmpVolt > 0))//positive voltage
  745. {
  746. if(TmpVolt < MinSample)
  747. {
  748. MinSample = TmpVolt;
  749. }
  750. count++;
  751. }
  752. lseek(fd, 0, SEEK_SET);
  753. }
  754. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  755. AvgSample += MinSample;
  756. }
  757. AvgSample /= AvgTimes;
  758. close(fd);
  759. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  760. return ((0.954-(1.8*AvgSample/4095))/0.06);
  761. }
  762. else
  763. {
  764. return -1;
  765. }
  766. }
  767. else
  768. {
  769. FILE *fp;
  770. unsigned char str[64];
  771. unsigned char AdcValue[8];
  772. if(AdcChannel > 7)
  773. {
  774. return -1;
  775. }
  776. memset(str,0,sizeof(str));
  777. memset(AdcValue,0,sizeof(AdcValue));
  778. sprintf(str, "cat /sys/bus/iio/devices/iio\\:device0/in_voltage%d_raw", AdcChannel);
  779. fp=popen(str, "r");
  780. if(fgets(AdcValue,sizeof(AdcValue),fp) == NULL)
  781. {
  782. pclose(fp);
  783. return -1;
  784. }
  785. pclose(fp);
  786. //Vin = Vref *D / (2^n - 1)
  787. return ((float)1.8*(float)atoi(AdcValue))/4095;
  788. }
  789. }
  790. /*===========================================================================
  791. FUNCTION: ReadAdcVolt
  792. DESCRIPTION:
  793. //AIN0=CCS GUN Temp 1
  794. //AIN1=CCS GUN Temp 2
  795. //AIN2=CCS_Proximity/2
  796. //AIN3=pilot voltage
  797. PRE-CONDITION:
  798. INPUT:
  799. OUTPUT:
  800. GLOBAL VARIABLES:
  801. =============================================================================*/
  802. float ReadAdcVolt_PP_fork3()
  803. {
  804. int fd, AvgTimes;
  805. unsigned char SampleBuf[4];
  806. float TmpVolt = 0;
  807. float AvgSample = 0;
  808. float V_pp = 0;
  809. fd = open("/sys/bus/iio/devices/iio\:device0/in_voltage2_raw", O_RDONLY); //PP
  810. if(fd > 0)
  811. {
  812. for(AvgTimes = 0; AvgTimes < 5; AvgTimes++) //get 5 samples
  813. {
  814. read(fd, SampleBuf, 4);
  815. TmpVolt = atoi(SampleBuf);
  816. lseek(fd, 0, SEEK_SET);
  817. AvgSample += TmpVolt;
  818. }
  819. close(fd);
  820. AvgSample /= AvgTimes;
  821. V_pp = (3.6*AvgSample)/4095; //PP
  822. #if 0
  823. printf("PP_adc = %.02f, PP_v = %.02f\n", AvgSample, V_pp);
  824. usleep(3000000); //300ms
  825. #endif
  826. usleep(20000); //20ms
  827. return V_pp;
  828. }
  829. else
  830. {
  831. return -1;
  832. }
  833. }
  834. /*===========================================================================
  835. FUNCTION: Qca7kPowerReset
  836. DESCRIPTION:
  837. PRE-CONDITION:
  838. INPUT:
  839. OUTPUT:
  840. GLOBAL VARIABLES:
  841. =============================================================================*/
  842. void Qca7kPowerReset()
  843. {
  844. //printf("\n[CCS Board]Reset QCA7000...\n");
  845. system("echo 1 > /sys/class/gpio/gpio88/value");
  846. usleep(500000);
  847. system("echo 0 > /sys/class/gpio/gpio88/value");
  848. }
  849. /*===========================================================================
  850. FUNCTION: SwitchCpStateE
  851. DESCRIPTION:
  852. PRE-CONDITION:
  853. INPUT:
  854. OUTPUT:
  855. GLOBAL VARIABLES:
  856. =============================================================================*/
  857. int SwitchCpStateE(unsigned char OnOff)
  858. {
  859. if((OnOff != ENABLE) && (OnOff != DISABLE))
  860. {
  861. return -1;
  862. }
  863. struct ChargingInfoData *ccs;
  864. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  865. ccs->SwitchCpStateE_status = OnOff;
  866. //OnOff = 1 => switch State to E
  867. //OnOff = 0 => return noraml
  868. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  869. {
  870. if(OnOff == DISABLE)
  871. {
  872. //SAVE_SYS_LOG_MSG_EVCOMM("SwitchCpStateE: released\n");
  873. CSUCOMMAC_SHM.CpSetStateE = DISABLE;
  874. }
  875. else
  876. {
  877. OutputCpPwmDuty(100); //set CP duty as 100, firstly.
  878. CSUCOMMAC_SHM.CpSetStateE = ENABLE;
  879. //SAVE_SYS_LOG_MSG_EVCOMM("SwitchCpStateE: enabled!\n");
  880. }
  881. }
  882. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  883. {
  884. if(OnOff == DISABLE)
  885. {
  886. //SAVE_SYS_LOG_MSG_EVCOMM("SwitchCpStateE: released\n");
  887. system("echo 0 > /sys/class/gpio/gpio86/value");
  888. }
  889. else
  890. {
  891. OutputCpPwmDuty(100); //set CP duty as 100, firstly.
  892. system("echo 1 > /sys/class/gpio/gpio86/value");
  893. //SAVE_SYS_LOG_MSG_EVCOMM("SwitchCpStateE: enabled!\n");
  894. }
  895. }
  896. #endif
  897. if (ccs->SwitchCpStateE_status_pre != ccs->SwitchCpStateE_status)
  898. {
  899. sprintf(buf_log_evcomm,
  900. "SwitchCpStateE: %d >> %d",
  901. ccs->SwitchCpStateE_status_pre,
  902. ccs->SwitchCpStateE_status
  903. );
  904. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  905. ccs->SwitchCpStateE_status_pre = ccs->SwitchCpStateE_status;
  906. }
  907. }
  908. /*===========================================================================
  909. FUNCTION: OutputCpPwmDuty
  910. DESCRIPTION:
  911. PRE-CONDITION:
  912. INPUT:
  913. OUTPUT:
  914. GLOBAL VARIABLES:
  915. =============================================================================*/
  916. int OutputCpPwmDuty(unsigned char Duty)
  917. {
  918. int DutyInNanoSec;
  919. struct ChargingInfoData *ccs;
  920. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  921. if((Duty < 0)||(Duty > 100))
  922. {
  923. return -1;
  924. }
  925. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  926. {
  927. CSUCOMMAC_SHM.CpSetPWMDuty = Duty;
  928. }
  929. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  930. {
  931. DutyInNanoSec = 10000 * Duty;
  932. sprintf(buf_log_evcomm, "echo %d > /sys/class/pwm/pwmchip0/pwm0/duty_cycle", DutyInNanoSec);//nanoseconds
  933. system(buf_log_evcomm);
  934. }
  935. #endif
  936. ccs->CpDuty = Duty;
  937. //CP Duty
  938. if (ccs->CpDuty != ccs->CpDuty_pre)
  939. {
  940. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  941. sprintf(buf_log_evcomm, "CP Duty: %d\% >> %d\%", ccs->CpDuty_pre, ccs->CpDuty);
  942. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  943. ccs->CpDuty_pre = ccs->CpDuty;
  944. }
  945. return 0;
  946. }
  947. /*===========================================================================
  948. FUNCTION: Check_Plugin_Status_Update_fork1
  949. DESCRIPTION:
  950. PRE-CONDITION:
  951. INPUT:
  952. OUTPUT:
  953. GLOBAL VARIABLES:
  954. =============================================================================*/
  955. //#if SAVE_SYS_LOG_MSG_EVCOMM_SWITCH == ENABLE
  956. void Check_Plugin_Status_Update_fork1(struct ChargingInfoData *ccs)
  957. {
  958. if (ccs->ConnectorPlugIn != ccs->ConnectorPlugIn_new)
  959. {
  960. sprintf(buf_log_evcomm_fork1, "[fork1]Plugin: %d >> %d (CP=%.2fV, PP=%.2fV)",
  961. ccs->ConnectorPlugIn,
  962. ccs->ConnectorPlugIn_new,
  963. ccs->CpVoltage,
  964. ccs->PpVoltage
  965. );
  966. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork1);
  967. ccs->ConnectorPlugIn_pre = ccs->ConnectorPlugIn;
  968. ccs->ConnectorPlugIn = ccs->ConnectorPlugIn_new;
  969. //ccs->CpVoltage_pre = ccs->CpVoltage;
  970. }
  971. }
  972. //#endif
  973. /*===========================================================================
  974. FUNCTION: ConnectorPlugIn
  975. DESCRIPTION:
  976. PRE-CONDITION:
  977. INPUT:
  978. OUTPUT:
  979. GLOBAL VARIABLES:
  980. =============================================================================*/
  981. int CheckConnectorPlugIn()
  982. {
  983. //return 0 => unplug
  984. //return 1 => plug-in
  985. //float TmpProximity;
  986. //unsigned char Rtn = 0;
  987. static struct ChargingInfoData *ccs;
  988. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  989. #if (CP_PROTECTION_MECHANISM == ENABLE)
  990. return (int)ccs->ConnectorPlugIn;
  991. #else
  992. return TRUE;
  993. #endif
  994. /*
  995. // PP Detection
  996. TmpProximity = ReadAdcVolt(ADC_CHANNEL_AIN2_PP);
  997. //[CAUTION] This function takes a period of time to deal with.
  998. //Designers should avoid to call it in main function, repeatly.
  999. if(((FIRMWARE_VERSION & 0x0000FF00) >> 8) == 0x01)
  1000. {
  1001. //UL version: judge with CP and proximity
  1002. //TmpProximity = ReadAdcVolt(ADC_CHANNEL_AIN2_PP);
  1003. //SAVE_SYS_LOG_MSG_EVCOMM("Connector: UL");
  1004. if((ccs->CpState >= 2) &&
  1005. (ccs->CpState <= 5) &&
  1006. (TmpProximity >= 0.4) &&
  1007. (TmpProximity <= 0.9))
  1008. {
  1009. Rtn = 1;
  1010. }
  1011. }
  1012. else
  1013. {
  1014. //SAVE_SYS_LOG_MSG_EVCOMM("Connector: CE");
  1015. //CE version: judge with CP only
  1016. if((ccs->CpState >=2 ) &&
  1017. (ccs->CpState <=5 ))
  1018. {
  1019. Rtn = 1;
  1020. }
  1021. }
  1022. */
  1023. //CE version: judge with CP only
  1024. /*
  1025. if((ccs->CpState >= 2 ) &&
  1026. (ccs->CpState <= 5 ))
  1027. {
  1028. Rtn = TRUE;
  1029. }
  1030. ccs->ConnectorPlugIn = Rtn;
  1031. //ccs->PpVoltage = TmpProximity;
  1032. return Rtn;
  1033. */
  1034. }
  1035. /*===========================================================================
  1036. FUNCTION: Check_CP_State_Error_fork1
  1037. DESCRIPTION:
  1038. PRE-CONDITION:
  1039. INPUT:
  1040. OUTPUT:
  1041. GLOBAL VARIABLES:
  1042. =============================================================================*/
  1043. int Check_CP_State_Error_fork1(struct ChargingInfoData *ccs)
  1044. {
  1045. #if (CP_PROTECTION_MECHANISM == DISABLE)
  1046. {
  1047. return -1;
  1048. }
  1049. #endif
  1050. unsigned char state = 0;
  1051. double time_diff = 0;
  1052. state = Check_V2G_Flow_Status();
  1053. //SLAC, SLAAC, SDP, ..., ChargeParameterDiscovery
  1054. if (state >= CM_SLAC_PARM_CONF && //by considering 15118(SLAC first)
  1055. state < ChargeParameterDiscoveryRequest &&
  1056. state != IDLE &&
  1057. state != CM_SET_KEY_REQ &&
  1058. state != CM_SET_KEY_CNF &&
  1059. state != CM_VALIDATE_REQ &&
  1060. state != CM_VALIDATE_CNF)
  1061. {
  1062. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  1063. {
  1064. if((ccs->CpState != 2) && (ccs->CpState != 3))
  1065. {
  1066. if (ccs->CpState_err == FALSE)
  1067. {
  1068. SAVE_SYS_LOG_MSG_EVCOMM("[fork1][CP][Error]before CPD"); //CPD: ChargeParameterDiscoveryRequest
  1069. //CPD: ChargeParameterDiscovery
  1070. }
  1071. ccs->CpState_err = TRUE;
  1072. }
  1073. }
  1074. #else
  1075. {
  1076. if(ccs->CpState != 3)
  1077. {
  1078. if (ccs->CpState_err == FALSE)
  1079. {
  1080. SAVE_SYS_LOG_MSG_EVCOMM("[fork1][CP][Error]before CPD"); //CPD: ChargeParameterDiscoveryRequest
  1081. //CPD: ChargeParameterDiscovery
  1082. }
  1083. ccs->CpState_err = TRUE;
  1084. }
  1085. }
  1086. #endif
  1087. }
  1088. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  1089. {
  1090. //ChargeParameterDiscovery
  1091. if(state >= ChargeParameterDiscoveryRequest && //35
  1092. state <= ChargeParameterDiscoveryResponse) //36
  1093. {
  1094. if(ccs->CpState != 3)
  1095. {
  1096. if (ccs->CpState_err == FALSE)
  1097. {
  1098. SAVE_SYS_LOG_MSG_EVCOMM("[CP][Error]CPD");
  1099. //PRC: Precharge
  1100. //CUD: CurrentDemand
  1101. }
  1102. ccs->CpState_err = TRUE;
  1103. }
  1104. }
  1105. //ChargingStatus
  1106. if(state >= ChargingStatusRequest && //43
  1107. state <= ChargingStatusResponse) //44
  1108. {
  1109. if(ccs->CpState != 4 && ccs->CpState != 5)
  1110. {
  1111. if (ccs->CpState_err == FALSE)
  1112. {
  1113. SAVE_SYS_LOG_MSG_EVCOMM("[CP][Error]CGS");
  1114. //PRC: Precharge
  1115. //CUD: CurrentDemand
  1116. }
  1117. ccs->CpState_err = TRUE;
  1118. }
  1119. }
  1120. }
  1121. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  1122. {
  1123. //ChargeParameterDiscovery, CableCheck
  1124. if (state >= ChargeParameterDiscoveryRequest && //35
  1125. state <= CableCheckResponse) //38
  1126. {
  1127. if(ccs->CableCheckPreCountDownDone == FALSE &&
  1128. state >= CableCheckRequest)
  1129. {
  1130. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  1131. time_diff = DiffTimeb_fork1_Error_Monitor(EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start, EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  1132. if(time_diff >= V2G_SECC_CP_Timeout_CableCheck) //2 sec
  1133. {
  1134. sprintf(buf_log_evcomm_fork1,
  1135. "[fork1][CP]check C(4),D(5): ON (%.02lf of %dms)",
  1136. time_diff,
  1137. V2G_SECC_CP_Timeout_CableCheck);
  1138. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork1);
  1139. ccs->CableCheckPreCountDownDone = TRUE;
  1140. }
  1141. }
  1142. if(ccs->CableCheckPreCountDownDone == TRUE)
  1143. {
  1144. if(ccs->CpState != 4 && ccs->CpState != 5)
  1145. {
  1146. if (ccs->CpState_err == FALSE)
  1147. {
  1148. SAVE_SYS_LOG_MSG_EVCOMM("[CP][Error]CPD and CCK (after CNT)");
  1149. //CPD: ChargeParameterDiscovery
  1150. //CCK: CableCheck
  1151. //CNT: count down
  1152. }
  1153. ccs->CpState_err = TRUE;
  1154. }
  1155. }
  1156. else
  1157. {
  1158. if(ccs->CpState != 3 && ccs->CpState != 4 && ccs->CpState != 5)
  1159. {
  1160. if (ccs->CpState_err == FALSE)
  1161. {
  1162. SAVE_SYS_LOG_MSG_EVCOMM("[CP][Error]CPD and CCK (before CNT)");
  1163. //CPD: ChargeParameterDiscovery
  1164. //CCK: CableCheck
  1165. //CNT: count down
  1166. }
  1167. ccs->CpState_err = TRUE;
  1168. }
  1169. }
  1170. }
  1171. //Precharge, CurrentDemand
  1172. if(state >= PreChargeRequest && //39
  1173. state <= CurrentDemandResponse) //46
  1174. {
  1175. if(ccs->CpState != 4 && ccs->CpState != 5)
  1176. {
  1177. if (ccs->CpState_err == FALSE)
  1178. {
  1179. SAVE_SYS_LOG_MSG_EVCOMM("[CP][Error]PRC and CUD");
  1180. //PRC: Precharge
  1181. //CUD: CurrentDemand
  1182. }
  1183. ccs->CpState_err = TRUE;
  1184. }
  1185. }
  1186. }
  1187. #endif
  1188. if(ccs->CpState_err == TRUE && ccs->CpState_err_logged == FALSE)
  1189. {
  1190. OutputCpPwmDuty(100);
  1191. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  1192. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1193. //CCS_SECC_CP_State_Error (023889)
  1194. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1195. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1196. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1197. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1198. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1199. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1200. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1201. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1202. sprintf(buf_log_evcomm_fork1, "[fork1][Erorr]CP(%d) Error in state %d(%d)",
  1203. ccs->CpState,
  1204. state,
  1205. ccs->CableCheckPreCountDownDone
  1206. );
  1207. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork1);
  1208. ccs->CpState_err_logged = TRUE;
  1209. }
  1210. return 0;
  1211. }
  1212. /*===========================================================================
  1213. FUNCTION: Check_CP_State_Update_fork1
  1214. DESCRIPTION:
  1215. PRE-CONDITION:
  1216. INPUT:
  1217. OUTPUT:
  1218. GLOBAL VARIABLES:
  1219. =============================================================================*/
  1220. //#if SAVE_SYS_LOG_MSG_EVCOMM_SWITCH == ENABLE
  1221. void Check_CP_State_Update_fork1(struct ChargingInfoData *ccs)
  1222. {
  1223. //CP Voltage
  1224. /*
  1225. //Issues waiting to debug
  1226. //[CAUTION] This function shall not be used in charging state.
  1227. if (((int)(ccs->CpVoltage * 10)) != ((int)(ccs->CpVoltage_pre * 10)))
  1228. {
  1229. memset(buf, 0, sizeof(buf));
  1230. sprintf(buf, "CP Voltage: %.2lf >> %.2lf (%d >> %d)",
  1231. ccs->CpVoltage_pre,
  1232. ccs->CpVoltage,
  1233. (int)(ccs->CpVoltage_pre * 10),
  1234. (int)(ccs->CpVoltage * 10)
  1235. );
  1236. SAVE_SYS_LOG_MSG_EVCOMM(buf);
  1237. ccs->CpVoltage_pre = ccs->CpVoltage;
  1238. }
  1239. */
  1240. //CP State
  1241. if (ccs->CpState != ccs->CpState_pre)
  1242. {
  1243. sprintf(buf_log_evcomm_fork1, "[fork1]CP State: %d >> %d (%.2fV)",
  1244. ccs->CpState_pre,
  1245. ccs->CpState,
  1246. ccs->CpVoltage
  1247. );
  1248. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork1);
  1249. ccs->CpState_pre = ccs->CpState;
  1250. }
  1251. }
  1252. //#endif
  1253. /*===========================================================================
  1254. FUNCTION: CP_Detection
  1255. DESCRIPTION:
  1256. 1. In order to detect CP in efficient response time, we creat an independent
  1257. thead for this procedure.
  1258. 2. The priority of this thread is set as the same as other tasks.
  1259. 3. fork1
  1260. PRE-CONDITION:
  1261. INPUT:
  1262. OUTPUT:
  1263. GLOBAL VARIABLES:
  1264. =============================================================================*/
  1265. void CP_Detection()
  1266. {
  1267. pid_t tmp = 0;
  1268. // struct timeb StartTime, EndTime;
  1269. unsigned char Statetmp;
  1270. float TotalTmpVolt;
  1271. struct ChargingInfoData *ccs;
  1272. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1273. if(CP_Detection_Pid == 0)
  1274. {
  1275. tmp = fork();
  1276. if(tmp > 0)
  1277. {
  1278. CP_Detection_Pid = tmp;
  1279. #if 1
  1280. unsigned char buf[64];
  1281. memset(buf, 0, sizeof(buf));
  1282. //sprintf(buf, "renice -20 -p %d", tmp);
  1283. sprintf(buf, "renice -10 -p %d", tmp);
  1284. system(buf);
  1285. #endif
  1286. return;
  1287. }
  1288. }
  1289. while(1)
  1290. {
  1291. //ADC_Voltage = (1+RE62/RE63)*0.9 - (RE62/RE63)*Pilot_Voltage, RE62/RE63=0.06
  1292. //=>Pilot_Voltage=(0.954-ADC_Voltage)/0.06
  1293. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  1294. {
  1295. ccs->CpVoltage = ShmInternalComm->AC_CpPositiveVoltage;
  1296. ccs->CpState = ShmInternalComm->AC_CpPresentState;
  1297. }
  1298. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  1299. {
  1300. TotalTmpVolt = ReadAdcVolt(3);
  1301. ccs->CpVoltage = TotalTmpVolt;
  1302. if (ccs->CpVoltage_pre != ccs->CpVoltage)
  1303. {
  1304. ccs->CpVoltage_pre = ccs->CpVoltage;
  1305. }
  1306. //printf("TotalTmpVolt = %.02f\n", TotalTmpVolt);
  1307. //If CP voltage is higer than 13.5V
  1308. if(TotalTmpVolt >= 13.5)
  1309. {
  1310. //Pilot Error
  1311. if((ccs->CpState == 1) &&
  1312. (TotalTmpVolt < 13.75))
  1313. {
  1314. //V_cp = 13.5 ~ 13.75
  1315. Statetmp = 1;
  1316. }
  1317. else
  1318. {
  1319. //V_cp = 13.5 ~
  1320. Statetmp = 8;
  1321. }
  1322. }
  1323. else if((TotalTmpVolt >= 10.5) && (TotalTmpVolt < 13.5)) //V_cp = 10.5 ~ 13.5
  1324. {
  1325. //State A (12V)
  1326. if((ccs->CpState >= 2) &&
  1327. (ccs->CpState <= 3) &&
  1328. (TotalTmpVolt < 10.75))
  1329. {
  1330. if((ccs->CpDuty >= 5) &&
  1331. (ccs->CpDuty < 100))
  1332. {
  1333. Statetmp = 3;
  1334. }
  1335. else
  1336. {
  1337. Statetmp = 2;
  1338. }
  1339. }
  1340. else if((ccs->CpState == 8) &&
  1341. (TotalTmpVolt >= 13.25))
  1342. {
  1343. Statetmp = 8;
  1344. }
  1345. else
  1346. {
  1347. Statetmp = 1;
  1348. }
  1349. }
  1350. else if((TotalTmpVolt >= 7.5) && (TotalTmpVolt < 10.5))
  1351. {
  1352. //State B (9V)
  1353. if((ccs->CpState == 4) && (TotalTmpVolt < 7.75))
  1354. {
  1355. Statetmp = 4;
  1356. }
  1357. else if((ccs->CpState == 1) && (TotalTmpVolt >= 10.25))
  1358. {
  1359. Statetmp = 1;
  1360. }
  1361. else
  1362. {
  1363. if((ccs->CpDuty >= 5) && (ccs->CpDuty < 100))
  1364. {
  1365. Statetmp = 3;
  1366. }
  1367. else
  1368. {
  1369. Statetmp = 2;
  1370. }
  1371. }
  1372. }
  1373. else if((TotalTmpVolt >= 4.5) && (TotalTmpVolt < 7.5))
  1374. {
  1375. //State C (6V)
  1376. if((ccs->CpState == 5) && (TotalTmpVolt < 4.75))
  1377. {
  1378. Statetmp = 5;
  1379. }
  1380. else if((ccs->CpState >= 2) && (ccs->CpState <= 3) && (TotalTmpVolt >= 7.25))
  1381. {
  1382. if((ccs->CpDuty >= 5)&&(ccs->CpDuty < 100))
  1383. {
  1384. Statetmp = 3;
  1385. }
  1386. else
  1387. {
  1388. Statetmp = 2;
  1389. }
  1390. }
  1391. else
  1392. {
  1393. Statetmp = 4;
  1394. }
  1395. }
  1396. else if((TotalTmpVolt >= 1.5) && (TotalTmpVolt < 4.5))
  1397. {
  1398. //State D (3V)
  1399. if((ccs->CpState == 6)&&(TotalTmpVolt < 1.75))
  1400. {
  1401. Statetmp = 6;
  1402. }
  1403. else if((ccs->CpState == 4)&&(TotalTmpVolt >= 4.25))
  1404. {
  1405. Statetmp = 4;
  1406. }
  1407. else
  1408. {
  1409. Statetmp = 5;
  1410. }
  1411. }
  1412. else if((TotalTmpVolt >= -1.5) && (TotalTmpVolt < 1.5)) //V_cp = -1.5V ~ 1.5V
  1413. {
  1414. //State E (0V)
  1415. if((ccs->CpState == 8) &&
  1416. (TotalTmpVolt < -1.25))
  1417. {
  1418. Statetmp = 8;
  1419. }
  1420. else if((ccs->CpState == 5) &&
  1421. (TotalTmpVolt >= 1.25))
  1422. {
  1423. Statetmp = 5;
  1424. }
  1425. else
  1426. {
  1427. Statetmp = 6;
  1428. }
  1429. }
  1430. else if((TotalTmpVolt >= -13.5) && (TotalTmpVolt < -10.5)) //V_cp = -10.5V ~ -13.5V
  1431. {
  1432. //State F (-12V)
  1433. if((ccs->CpState == 8) &&
  1434. (TotalTmpVolt >= -10.75))
  1435. {
  1436. Statetmp = 8;
  1437. }
  1438. else
  1439. {
  1440. Statetmp = 7;
  1441. }
  1442. }
  1443. else
  1444. {
  1445. //null
  1446. }
  1447. ccs->CpState = Statetmp;
  1448. }
  1449. #endif
  1450. Check_CP_State_Update_fork1(ccs);
  1451. Check_CP_State_Error_fork1(ccs);
  1452. //Updating Plugin status
  1453. #if (PP_PROTECTION_MECHANISM == ENABLE)
  1454. if((ccs->CpState >= 2 ) && (ccs->CpState <= 5 ) && (ccs->PpVoltage >= 1.0)) //PP >= 1.0V
  1455. #else
  1456. if((ccs->CpState >= 2 ) && (ccs->CpState <= 5 ))
  1457. #endif
  1458. {
  1459. ccs->ConnectorPlugIn_new = TRUE;
  1460. }
  1461. else
  1462. {
  1463. ccs->ConnectorPlugIn_new = FALSE;
  1464. OutputCpPwmDuty(100);
  1465. }
  1466. Check_Plugin_Status_Update_fork1(ccs);
  1467. //ftime(&EndTime);
  1468. //DEBUG_PRINTF_EVCOMM_DETAIL("duration:%.02lf\n", DiffTimeb(StartTime, EndTime));
  1469. }//while
  1470. }
  1471. /*===========================================================================
  1472. FUNCTION: PP_Detection
  1473. DESCRIPTION:
  1474. 0. SeccComm fork3
  1475. 1. In order to detect CP in efficient response time, we creat an independent
  1476. thead for this procedure.
  1477. 2. The priority of this thread is set as the same as other tasks.
  1478. PRE-CONDITION:
  1479. INPUT:
  1480. OUTPUT:
  1481. GLOBAL VARIABLES:
  1482. =============================================================================*/
  1483. void PP_Detection()
  1484. {
  1485. pid_t tmp = 0;
  1486. // struct timeb StartTime, EndTime;
  1487. unsigned char Statetmp;
  1488. float TotalTmpVolt;
  1489. if(PP_Detection_Pid == 0)
  1490. {
  1491. tmp = fork();
  1492. if(tmp > 0)
  1493. {
  1494. PP_Detection_Pid = tmp;
  1495. #if 0
  1496. unsigned char buf[64];
  1497. memset(buf, 0, sizeof(buf));
  1498. sprintf(buf, "renice -10 -p %d", tmp);
  1499. system(buf);
  1500. #endif
  1501. return;
  1502. }
  1503. }
  1504. TotalTmpVolt = ReadAdcVolt_PP_fork3();
  1505. while(1)
  1506. {
  1507. TotalTmpVolt = ReadAdcVolt_PP_fork3();
  1508. EVCOMM_SYS_INFO.PpVoltage = TotalTmpVolt;
  1509. if (EVCOMM_SYS_INFO.PpVoltage_pre != EVCOMM_SYS_INFO.PpVoltage)
  1510. {
  1511. if ((EVCOMM_SYS_INFO.PpVoltage_pre < 0.5 && EVCOMM_SYS_INFO.PpVoltage >= 1.0) ||
  1512. (EVCOMM_SYS_INFO.PpVoltage_pre >= 1.0 && EVCOMM_SYS_INFO.PpVoltage < 0.5))
  1513. {
  1514. // plug-in : > 1.0
  1515. //unplug : <0.5
  1516. sprintf(buf_log_evcomm_fork3,
  1517. "[fork3]PP(%.2f >> %.2fV)",
  1518. EVCOMM_SYS_INFO.PpVoltage_pre,
  1519. EVCOMM_SYS_INFO.PpVoltage
  1520. );
  1521. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork3);
  1522. EVCOMM_SYS_INFO.PpVoltage_pre = EVCOMM_SYS_INFO.PpVoltage;
  1523. }
  1524. }
  1525. #if 0
  1526. sleep(5);
  1527. printf("V_pp = %.02f\n", TotalTmpVolt);
  1528. #endif
  1529. }//while
  1530. }
  1531. /*===========================================================================
  1532. FUNCTION: Error_Monitor
  1533. DESCRIPTION:
  1534. 1. This function only works in SeccComm fork2.
  1535. PRE-CONDITION:
  1536. INPUT:
  1537. OUTPUT:
  1538. GLOBAL VARIABLES:
  1539. =============================================================================*/
  1540. void Error_Monitor()
  1541. {
  1542. pid_t tmp = 0;
  1543. double time_diff = 0;
  1544. unsigned char status = 0;
  1545. struct ChargingInfoData *ccs;
  1546. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1547. if(Error_Monitor_Pid == 0)
  1548. {
  1549. tmp = fork(); //SeccComm fork2
  1550. if(tmp > 0)
  1551. {
  1552. Error_Monitor_Pid = tmp;
  1553. #if 0
  1554. unsigned char buf[64];
  1555. memset(buf, 0, sizeof(buf));
  1556. sprintf(buf, "renice -20 -p %d", tmp);
  1557. system(buf);
  1558. #endif
  1559. return;
  1560. }
  1561. }
  1562. while(1)
  1563. {
  1564. //Step 0
  1565. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  1566. {
  1567. //If the End_Process is in processing, disable Error_Monitor.
  1568. continue;
  1569. }
  1570. //Step1 1: Check and Response to Plugin Status
  1571. if(CheckConnectorPlugIn() == FALSE)
  1572. {
  1573. status = Check_V2G_Flow_Status();
  1574. if (status > IDLE &&
  1575. status < Performance_Timeout &&
  1576. status != CM_SET_KEY_REQ &&
  1577. status != CM_SET_KEY_CNF &&
  1578. EVCOMM_SYS_INFO.End_Process_inused == FALSE)
  1579. {
  1580. #if (CP_PROTECTION_MECHANISM == ENABLE)
  1581. //SAVE_SYS_LOG_MSG_EVCOMM("[fork2] Emergency Stop (due to Connector is plugged out during communication.)");
  1582. SAVE_SYS_LOG_MSG_EVCOMM("[fork2]Plug out Error => End_Process");
  1583. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1584. //CCS_SECC_CP_State_Error (023889)
  1585. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1586. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1587. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1588. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1589. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1590. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1591. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1592. End_Process();
  1593. #else
  1594. //SAVE_SYS_LOG_MSG_EVCOMM("CP_PROTECTION_MECHANISM is disabled. Emergency Stop: skipped" );
  1595. #endif
  1596. }
  1597. }
  1598. //Step 2: Check for V2G_SECC_Sequence_Timeout
  1599. //#if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  1600. #if 1
  1601. status = Check_V2G_Flow_Status();
  1602. if (status >= SupportedAppProtocolRequest &&
  1603. status < SessionStopRequest)
  1604. {
  1605. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_End);
  1606. time_diff = DiffTimeb_fork2_Error_Monitor(EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start, EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_End);
  1607. if(time_diff > V2G_SECC_Sequence_Timeout) //60s
  1608. {
  1609. sprintf(buf_log_evcomm_fork2,
  1610. "[fork2]V2G_SECC_Sequence_Timeout in state %d - (%.02lf of %d ms)\n",
  1611. status,
  1612. time_diff,
  1613. V2G_SECC_Sequence_Timeout);
  1614. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork2);
  1615. Update_V2G_Flow_Status(Sequence_Timeout);
  1616. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1617. //CCS_SECC_TIMEOUT_V2G_Sequence_Time (023844)
  1618. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1619. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1620. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1621. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1622. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  1623. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  1624. End_Process();
  1625. break;
  1626. }
  1627. else if (time_diff > 4000) //4s
  1628. {
  1629. //Check for CSU command of "Stop by EVSE"
  1630. if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_Shutdown)
  1631. {
  1632. SAVE_SYS_LOG_MSG_EVCOMM("[Error_Monitor]EVSE_Shutdown");
  1633. Update_V2G_Flow_Status(Other_Fault);
  1634. }
  1635. else if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_EmergencyShutdown)
  1636. {
  1637. SAVE_SYS_LOG_MSG_EVCOMM("[Error_Monitor]EVSE_EmergencyShutdown");
  1638. Update_V2G_Flow_Status(Other_Fault);
  1639. }
  1640. else if (ShmInternalComm->ChargingPermission == FALSE)
  1641. {
  1642. if (status >= ChargeParameterDiscoveryRequest) //&& status < SessionStopRequest
  1643. {
  1644. SAVE_SYS_LOG_MSG_EVCOMM("[Error_Monitor]ChargingPermission = FALSE");
  1645. Update_V2G_Flow_Status(Other_Fault);
  1646. }
  1647. }
  1648. else
  1649. {
  1650. //null
  1651. }
  1652. }
  1653. else
  1654. {
  1655. //null
  1656. }
  1657. }
  1658. #endif
  1659. //Step 3: Check and Response to Error V2gFlowStatus
  1660. status = Check_V2G_Flow_Status();
  1661. if (status == Performance_Timeout ||
  1662. status == Sequence_Timeout ||
  1663. status == Other_Fault)
  1664. {
  1665. sprintf(buf_log_evcomm_fork2,
  1666. "[fork2]Timeout or Fault State(%d) => End_Process",
  1667. status);
  1668. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork2);
  1669. //Normal Stop
  1670. //alarm and duty to 100%
  1671. //OutputCpPwmDuty(100);
  1672. //SwitchCpStateE(ENABLE); //Set CP duty as 100% and set CP state as E (0V).
  1673. //[Joseph/ To-Do] Set State E (0V) via GPIO setting
  1674. End_Process();
  1675. }
  1676. //Step 4: Check and Response to CP State Error
  1677. if(ccs->CpState_err == TRUE)
  1678. {
  1679. SAVE_SYS_LOG_MSG_EVCOMM("[fork2]CP Error => End_Process");
  1680. Update_V2G_Flow_Status(Other_Fault);
  1681. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1682. //CCS_SECC_CP_State_Error (023889)
  1683. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1684. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1685. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1686. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1687. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1688. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1689. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1690. End_Process();
  1691. }
  1692. //Step 5: Check and Response to Shutdown Commnad from CSU
  1693. if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_Shutdown ||
  1694. EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_EmergencyShutdown)
  1695. {
  1696. if (Check_V2G_Flow_Status() <= SLACC_SDP_TCP_Connection)
  1697. {
  1698. sprintf(buf_log_evcomm_fork2,
  1699. "[fork2]EVSE Shutdown(%d) => End_Process",
  1700. EVCOMM_SYS_INFO.DC_EVSEStatus);
  1701. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork2);
  1702. Update_V2G_Flow_Status(Other_Fault);
  1703. End_Process();
  1704. }
  1705. }
  1706. //Step 6: Check and Response to SessionStop
  1707. status = Check_V2G_Flow_Status();
  1708. if (status == SessionStopResponse)
  1709. {
  1710. SAVE_SYS_LOG_MSG_EVCOMM("[fork2]SessionStopResponse => End_Process");
  1711. End_Process();
  1712. }
  1713. //Step 7: Check for ChargingPermission from TRUE to FALSE before V2G Messages
  1714. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  1715. if ((ShmInternalComm->ChargingPermission == FALSE) &&
  1716. (ShmInternalComm->ChargingPermission_pre >=1) &&
  1717. (ccs->CpState >= 3) && (ccs->CpState <=5))
  1718. {
  1719. if (status >= CM_SLAC_PARM_REQ &&
  1720. status != CM_SET_KEY_REQ &&
  1721. status != CM_SET_KEY_CNF &&
  1722. status <= SLACC_SDP_TCP_Connection)
  1723. {
  1724. sprintf(buf_log_evcomm_fork2,
  1725. "[fork2]Permission OFF before V2G msg(%d) => End_Process",
  1726. ShmInternalComm->ChargingPermission);
  1727. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork2);
  1728. Update_V2G_Flow_Status(Other_Fault);
  1729. End_Process();
  1730. }
  1731. }
  1732. //Step 8: DC OVP Protection
  1733. if (CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED &&
  1734. status > CableCheckResponse &&
  1735. status <= SessionStopRequest &&
  1736. status != ChargingStatusRequest &&
  1737. status != ChargingStatusResponse &&
  1738. EVCOMM_SYS_INFO.EvBatteryMaxVoltage != 0)
  1739. {
  1740. //Part A: OVP Protection
  1741. if (EVCOMM_SYS_INFO.PresentChargingVoltage >= (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02)) // 2%
  1742. {
  1743. sprintf(buf_log_evcomm_fork2,
  1744. "[fork2][Error]OVP => End_Process (%.02f > %.02f)",
  1745. EVCOMM_SYS_INFO.PresentChargingVoltage,
  1746. (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02));
  1747. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork2);
  1748. Update_V2G_Flow_Status(Other_Fault);
  1749. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1750. //System CCS output OVP (012219)
  1751. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1752. ShmStatusCodeData->PresentStatusCode[0][1] = 1;
  1753. ShmStatusCodeData->PresentStatusCode[0][2] = 2;
  1754. ShmStatusCodeData->PresentStatusCode[0][3] = 2;
  1755. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  1756. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1757. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1758. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1759. End_Process();
  1760. }
  1761. //Part B: Over Voltage Request Protection
  1762. if (EVCOMM_SYS_INFO.EvBatterytargetVoltage >= (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02))
  1763. {
  1764. sprintf(buf_log_evcomm_fork2,
  1765. "[fork2]Over V Req => End_Process (%.02f > %.02f)",
  1766. EVCOMM_SYS_INFO.EvBatterytargetVoltage,
  1767. (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02)
  1768. );
  1769. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm_fork2);
  1770. Update_V2G_Flow_Status(Other_Fault);
  1771. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1772. //System CCS output OVP (012219)
  1773. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1774. ShmStatusCodeData->PresentStatusCode[0][1] = 1;
  1775. ShmStatusCodeData->PresentStatusCode[0][2] = 2;
  1776. ShmStatusCodeData->PresentStatusCode[0][3] = 2;
  1777. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  1778. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1779. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1780. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1781. End_Process();
  1782. }
  1783. }
  1784. //Step 9: Check 60V
  1785. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  1786. if (CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED &&
  1787. ShmInternalComm->ChargingPermission >=1 &&
  1788. status < CableCheckRequest)
  1789. {
  1790. if (EVCOMM_SYS_INFO.PresentChargingVoltage >= 60) //60V
  1791. {
  1792. SAVE_SYS_LOG_MSG_EVCOMM("[fork2]DC Output Voltage is over 60V => End_Process");
  1793. Update_V2G_Flow_Status(Other_Fault);
  1794. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1795. //CCS_SECC_Unexpected_60V_Before_Charing_Error (023890)
  1796. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1797. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1798. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1799. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1800. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  1801. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  1802. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1803. //Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1804. End_Process();
  1805. }
  1806. }
  1807. //Step 10: Check if the connector is unplug from plugin
  1808. if (EVCOMM_SYS_INFO.ConnectorPlugIn_pre == TRUE && EVCOMM_SYS_INFO.ConnectorPlugIn == FALSE)
  1809. {
  1810. SAVE_SYS_LOG_MSG_EVCOMM("[fork2]Unplug Reset => End_Process");
  1811. Update_V2G_Flow_Status(Other_Fault);
  1812. End_Process();
  1813. }
  1814. }//while
  1815. }
  1816. /*===========================================================================
  1817. FUNCTION: SendSetKey
  1818. DESCRIPTION:
  1819. PRE-CONDITION:
  1820. INPUT:
  1821. OUTPUT:
  1822. GLOBAL VARIABLES:
  1823. =============================================================================*/
  1824. int SendSetKey()
  1825. {
  1826. int i = 0;
  1827. unsigned char nRandValue = 0x0;
  1828. unsigned char ConstString[16] = "PhihongKey000000";
  1829. memset(&SendMmePacket,0,sizeof(struct MmeHeader));
  1830. memcpy(SendMmePacket.ODA,QcaMac,6);
  1831. memcpy(SendMmePacket.OSA,CsuMac,6);
  1832. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  1833. SendMmePacket.MMV = 0x01;
  1834. SendMmePacket.MMTYPE = MMTYPE_CM_SET_KEY_REQ;
  1835. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  1836. SendMmePacketSize = 0;
  1837. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x01;//Fixed value (0x01) to indicate ��NMK��
  1838. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,4);//My Nonce, Fixed value(0x00000000), encrypted payload not used
  1839. SendMmePacketSize+=4;
  1840. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,4);//Your Nonce, Fixed value(0x00000000), encrypted payload not used
  1841. SendMmePacketSize+=4;
  1842. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x04;//PID, Fixed value (0x04) to indicate ��HLE protocol��
  1843. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,2);//PRN, Fixed value(0x00), encrypted payload not used
  1844. SendMmePacketSize+=2;
  1845. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00;//PMN, Fixed value(0x00) encrypted payload not used
  1846. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x01;//CCo Capablility
  1847. srand(time(NULL));
  1848. for (i = 10; i < 16; i++)
  1849. {
  1850. nRandValue = (rand()%62) + 1;
  1851. if((nRandValue>=0)&&(nRandValue<=9)) // 0 ~ 9
  1852. {
  1853. ConstString[i]= nRandValue + 0x30;
  1854. }
  1855. else if((nRandValue>=10)&&(nRandValue<=35)) // A ~ Z
  1856. {
  1857. ConstString[i]= nRandValue -10 + 0x41;
  1858. }
  1859. else if((nRandValue>=36)&&(nRandValue<=61)) // a ~ z
  1860. {
  1861. ConstString[i]= nRandValue -37 + 0x61;
  1862. }
  1863. else
  1864. {
  1865. ConstString[i]= 0x30;
  1866. }
  1867. }
  1868. memset(NewNmkKey,0,sizeof(NewNmkKey));
  1869. memset(Nid,0,sizeof(Nid));
  1870. HPAVKeyNMK(NewNmkKey, ConstString);
  1871. HPAVKeyNID(Nid,NewNmkKey,DEFAULT_LEVEL);
  1872. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,Nid,sizeof(Nid)); //NID, 54 LSBs contain the NID 2 MSBs = 0b00
  1873. SendMmePacketSize+=sizeof(Nid);
  1874. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x01;//NewEKS,Fixed value (0x01)to indicate ��NMK��
  1875. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,NewNmkKey,sizeof(NewNmkKey));//NewKey
  1876. SendMmePacketSize += sizeof(NewNmkKey);
  1877. SendMmePacketSize += 19; //the size before MMENTRY
  1878. SAVE_SYS_LOG_MSG_EVCOMM("[QCA7K][Tx]CM_SET_KEY_REQ");
  1879. i = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  1880. Update_V2G_Flow_Status(CM_SET_KEY_REQ);
  1881. DEBUG_PRINTF_EVCOMM_DETAIL("SendSetKey: send size =%d\n",i);
  1882. }
  1883. /*===========================================================================
  1884. FUNCTION: GetQca7kMac
  1885. DESCRIPTION:
  1886. PRE-CONDITION:
  1887. INPUT:
  1888. OUTPUT:
  1889. GLOBAL VARIABLES:
  1890. =============================================================================*/
  1891. int GetQca7kMac()
  1892. {
  1893. int i = 0;
  1894. struct QcaVendorMmeHeader SendPacket;
  1895. memset(&SendPacket, 0, sizeof(struct QcaVendorMmeHeader));
  1896. memset(SendPacket.ODA, 0xFF, 6); //broadcast
  1897. memcpy(SendPacket.OSA, CsuMac, 6);
  1898. SendPacket.MTYPE = htons(EtherType_HomePlug);
  1899. SendPacket.MMV = 0x00;
  1900. SendPacket.MMTYPE = MMTYPE_VENDOR_VS_NW_INFO;
  1901. SendPacket.OUI[0] = 0x00;
  1902. SendPacket.OUI[1] = 0xB0;
  1903. SendPacket.OUI[2] = 0x52;
  1904. i = sendto(RawSock, &SendPacket, 20, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  1905. DEBUG_PRINTF_EVCOMM_DETAIL("GetQca7kMac: send size =%d\n", i);
  1906. SAVE_SYS_LOG_MSG_EVCOMM("[QCA7K][Tx][VENDOR_VS_NW_INFO]Req for QCA7K MacAddr");
  1907. }
  1908. /*===========================================================================
  1909. FUNCTION: Array_Check_All_Zero
  1910. DESCRIPTION:
  1911. PRE-CONDITION:
  1912. INPUT:
  1913. OUTPUT:
  1914. result:
  1915. (1) TRUE: all zero
  1916. (2) FALSE: not all zero
  1917. GLOBAL VARIABLES:
  1918. =============================================================================*/
  1919. int Array_Check_All_Zero(unsigned char *ptr, int size)
  1920. {
  1921. int result = TRUE;
  1922. int i = 0;
  1923. for (i = 0; i < size; i++)
  1924. {
  1925. if (ptr[i] != 0)
  1926. {
  1927. result = FALSE;
  1928. break;
  1929. }
  1930. }
  1931. return result;
  1932. }
  1933. /*===========================================================================
  1934. FUNCTION: Array_Compare_Identity
  1935. DESCRIPTION:
  1936. PRE-CONDITION:
  1937. INPUT:
  1938. OUTPUT:
  1939. result = FALSE (not identical)
  1940. result = TRUE (identical)
  1941. GLOBAL VARIABLES:
  1942. =============================================================================*/
  1943. int Array_Compare_Identity(unsigned char *ptrA, unsigned char *ptrB, int size)
  1944. {
  1945. int result = TRUE;
  1946. int i = 0;
  1947. for (i = 0; i < size; i++)
  1948. {
  1949. if (ptrA[i] != ptrB[i])
  1950. {
  1951. result = FALSE;
  1952. #if 0
  1953. sprintf(buf_log_evcomm,
  1954. "[Array_Compare_Identity]%02X%02X%02X%02X%02X%02X,%02X%02X%02X%02X%02X%02X(%d)\n",
  1955. ptrA[0], ptrA[1], ptrA[2], ptrA[3], ptrA[4], ptrA[5],
  1956. ptrB[0], ptrB[1], ptrB[2], ptrB[3], ptrB[4], ptrB[5],
  1957. result);
  1958. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  1959. #endif
  1960. break;
  1961. }
  1962. }
  1963. return result;
  1964. }
  1965. /*===========================================================================
  1966. FUNCTION: SLAC_DB_Search_EvMac_idx
  1967. DESCRIPTION:
  1968. PRE-CONDITION:
  1969. INPUT:
  1970. OUTPUT:
  1971. idx = -1 (not found)
  1972. GLOBAL VARIABLES:
  1973. =============================================================================*/
  1974. int SLAC_DB_Search_EvMac_idx(struct EVCC_SLAC_DATA_TYPE *evcc, unsigned char *EvMac_in)
  1975. {
  1976. int idx = -1;
  1977. int i = 0;
  1978. if (evcc->arrayLen == 0)
  1979. {
  1980. //printf("[SLAC_DB_Search_EvMac_idx]arrayLen is empty (%d)\n", evcc->arrayLen);
  1981. //no need to search
  1982. }
  1983. else if (evcc->arrayLen > EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE)
  1984. {
  1985. //error
  1986. sprintf(buf_log_evcomm,
  1987. "[ERROR][SLAC_DB_Search_EvMac_idx]DB length(%d) > %d",
  1988. evcc->arrayLen,
  1989. EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE);
  1990. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  1991. evcc->arrayLen = 0; //reset
  1992. }
  1993. else
  1994. {
  1995. //start searching
  1996. for (i = 0; i < evcc->arrayLen; i++)
  1997. {
  1998. //printf("[SLAC_DB_Search_EvMac_idx]checking DB[%d]...\n", i);
  1999. if (Array_Compare_Identity(evcc->array[i].EvMac, EvMac_in, SLAC_EVMAC_LENGTH) == TRUE)
  2000. {
  2001. //printf("[SLAC_DB_Search_EvMac_idx]identical at DB[%d]...\n", i);
  2002. idx = i;
  2003. break;
  2004. }
  2005. }
  2006. }
  2007. //printf("[SLAC_DB_Search_EvMac_idx]return = %d\n", idx);
  2008. return idx;
  2009. }
  2010. /*===========================================================================
  2011. FUNCTION: SLAC_DB_Check_EvMac_RunID_Matching
  2012. DESCRIPTION:
  2013. PRE-CONDITION:
  2014. INPUT:
  2015. OUTPUT:
  2016. res = FALSE (unmatched)
  2017. res = TRUE (matched)
  2018. GLOBAL VARIABLES:
  2019. =============================================================================*/
  2020. int SLAC_DB_Check_EvMac_RunID_Matching(struct EVCC_SLAC_DATA_TYPE *evcc, unsigned char *EvMac_in, unsigned char *RunID_in)
  2021. {
  2022. int res = FALSE;
  2023. int idx = -1;
  2024. idx = SLAC_DB_Search_EvMac_idx(evcc, EvMac_in);
  2025. if (idx >= 0)
  2026. {
  2027. res = Array_Compare_Identity(evcc->array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH);
  2028. }
  2029. else
  2030. {
  2031. //not found the EvMac data in DB
  2032. res = FALSE;
  2033. }
  2034. return res;
  2035. }
  2036. /*===========================================================================
  2037. FUNCTION: SLAC_DB_Add
  2038. DESCRIPTION:
  2039. 1. Saving each CM_SLAC_PARM_REQ data into EVCC database if its
  2040. EvMac and RunID data are different, respectively.
  2041. PRE-CONDITION:
  2042. INPUT:
  2043. OUTPUT:
  2044. idx = saved index (must be a positive value)
  2045. GLOBAL VARIABLES:
  2046. =============================================================================*/
  2047. int SLAC_DB_Add(unsigned char *EvMac_in, unsigned char *RunID_in)
  2048. {
  2049. int idx = -1;
  2050. //Search if this EvMac and RunID already exists
  2051. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2052. if (idx < 0) //not exist, yet.
  2053. {
  2054. if (SLAC_INFO.arrayLen < EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE) //20
  2055. {
  2056. sprintf(buf_log_evcomm,
  2057. "[SLAC_DB_Add]data does not exist => added to %d-th",
  2058. SLAC_INFO.arrayLen);
  2059. DEBUG_PRINTF_EVCOMM_DETAIL(buf_log_evcomm);
  2060. if (SLAC_INFO.arrayLen >= 0)
  2061. {
  2062. memset(&SLAC_INFO.array[SLAC_INFO.arrayLen], 0, sizeof(struct EVCC_SLAC_DATA_ARRAY_TYPE));
  2063. memcpy(SLAC_INFO.array[SLAC_INFO.arrayLen].EvMac, EvMac_in, SLAC_EVMAC_LENGTH);
  2064. memcpy(SLAC_INFO.array[SLAC_INFO.arrayLen].RunID, RunID_in, SLAC_RUNID_LENGTH);
  2065. idx = SLAC_INFO.arrayLen;
  2066. SLAC_INFO.arrayLen++;
  2067. }
  2068. else
  2069. {
  2070. sprintf(buf_log_evcomm,
  2071. "[ERROR][SLAC_DB_Add]arrayLen: unexpected(%d)",
  2072. SLAC_INFO.arrayLen);
  2073. DEBUG_PRINTF_EVCOMM_DETAIL(buf_log_evcomm);
  2074. SLAC_INFO.arrayLen = 0;
  2075. }
  2076. }
  2077. else
  2078. {
  2079. //DB is full
  2080. sprintf(buf_log_evcomm,
  2081. "[SLAC_DB_Add]DB is full(%d) => bypass",
  2082. SLAC_INFO.arrayLen);
  2083. DEBUG_PRINTF_EVCOMM_DETAIL(buf_log_evcomm);
  2084. }
  2085. }
  2086. else
  2087. {
  2088. #if 0
  2089. sprintf(buf_log_evcomm,
  2090. "[SLAC_DB_Add]EvMac: existed (%d)",
  2091. idx);
  2092. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2093. #endif
  2094. //Check RunID
  2095. if (Array_Compare_Identity(SLAC_INFO.array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH) == TRUE)
  2096. {
  2097. //RunID is the same
  2098. //SAVE_SYS_LOG_MSG_EVCOMM("[SLAC_DB_Add]RunID: same");
  2099. }
  2100. else
  2101. {
  2102. SAVE_SYS_LOG_MSG_EVCOMM("[SLAC_DB_Add]RunID: diff");
  2103. }
  2104. //Reset all corresponding parameters
  2105. #if 0
  2106. sprintf(buf_log_evcomm,
  2107. "[SLAC_DB_Add]EvMac: reset para(%d)",
  2108. idx);
  2109. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2110. #endif
  2111. memset(&SLAC_INFO.array[idx], 0, sizeof(struct EVCC_SLAC_DATA_ARRAY_TYPE));
  2112. memcpy(SLAC_INFO.array[idx].EvMac, EvMac_in, SLAC_EVMAC_LENGTH);
  2113. memcpy(SLAC_INFO.array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH);
  2114. }
  2115. return idx;
  2116. }
  2117. /*===========================================================================
  2118. FUNCTION: SLAC_DB_Reset
  2119. DESCRIPTION:
  2120. PRE-CONDITION:
  2121. INPUT:
  2122. OUTPUT:
  2123. GLOBAL VARIABLES:
  2124. =============================================================================*/
  2125. int SLAC_DB_Reset()
  2126. {
  2127. memset(&SLAC_INFO, 0, sizeof(struct EVCC_SLAC_DATA_TYPE));
  2128. SAVE_SYS_LOG_MSG_EVCOMM("[SLAC_DB_Reset]DONE");
  2129. }
  2130. /*===========================================================================
  2131. FUNCTION: MmeProcess
  2132. DESCRIPTION:
  2133. PRE-CONDITION:
  2134. INPUT:
  2135. OUTPUT:
  2136. GLOBAL VARIABLES:
  2137. =============================================================================*/
  2138. int MmeProcess(unsigned char *Buffer, int DataLength)
  2139. {
  2140. //struct ethhdr *EthPacket;
  2141. struct MmeHeader *MmePacket;
  2142. static unsigned char counter;
  2143. unsigned char state = 0;
  2144. unsigned char *EvMac_in;
  2145. unsigned char *RunID_in;
  2146. int Rtn = 0;
  2147. int idx = 0;
  2148. MmePacket = (struct MmeHeader *)Buffer;
  2149. state = Check_V2G_Flow_Status();
  2150. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2151. {
  2152. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***********************************\n");
  2153. DEBUG_PRINTF_EVCOMM_DETAIL("***** Received MME Packet *****\n");
  2154. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  2155. DEBUG_PRINTF_EVCOMM_DETAIL("DataLength=%d\n",DataLength);
  2156. DEBUG_PRINTF_EVCOMM_DETAIL("ODA: %02x:%02x:%02x:%02x:%02x:%02x\n", //Destination MAC Address
  2157. MmePacket->ODA[0], MmePacket->ODA[1],
  2158. MmePacket->ODA[2], MmePacket->ODA[3],
  2159. MmePacket->ODA[4], MmePacket->ODA[5]);
  2160. DEBUG_PRINTF_EVCOMM_DETAIL("OSA: %02x:%02x:%02x:%02x:%02x:%02x\n", //Source MAC Address (EV MAC)
  2161. MmePacket->OSA[0], MmePacket->OSA[1],
  2162. MmePacket->OSA[2], MmePacket->OSA[3],
  2163. MmePacket->OSA[4], MmePacket->OSA[5]);
  2164. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(MmePacket->MTYPE));
  2165. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", MmePacket->MMV);
  2166. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", MmePacket->MMTYPE);
  2167. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n",
  2168. MmePacket->FMI[0],MmePacket->FMI[1]);
  2169. }
  2170. #endif
  2171. #if (SLAC_FIRST_RESPONSE_METHOD == SET_NO_PWM_IF_NOT_GET_PERMISSION)
  2172. {
  2173. //Check CP as 5%
  2174. if (EVCOMM_SYS_INFO.CpState != 3 &&
  2175. EVCOMM_SYS_INFO.CpState != 4 &&
  2176. MmePacket->MMTYPE != MMTYPE_CM_SET_KEY_CNF &&
  2177. MmePacket->MMTYPE != MMTYPE_VENDOR_VS_HOST_ACTION &&
  2178. MmePacket->MMTYPE != MMTYPE_VENDOR_ATTEN_CHAR &&
  2179. MmePacket->MMTYPE != MMTYPE_VENDOR_VS_NW_INFO_CNF
  2180. )
  2181. {
  2182. //SAVE_SYS_LOG_MSG_EVCOMM("[SLAC]ignored(wrong CP state)");
  2183. return 0;
  2184. }
  2185. }
  2186. #endif
  2187. //[To-Do] Adding a pre-check filter mechanism for Source and Destination MAC Address
  2188. //check if the destination address is broadcast or EVSE itself, it can be ignore if not belong to itself.
  2189. switch(MmePacket->MMTYPE)
  2190. {
  2191. case MMTYPE_CM_SET_KEY_CNF:
  2192. {
  2193. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SET_KEY_CNF ---\n");
  2194. DEBUG_PRINTF_EVCOMM_DETAIL("Result: 0x%x\n", MmePacket->MMENTRY[0]);
  2195. SAVE_SYS_LOG_MSG_EVCOMM("[QCA7K][Rx]CM_SET_KEY_CNF (SetKey: DONE)");
  2196. Update_V2G_Flow_Status(CM_SET_KEY_CNF);
  2197. break;
  2198. }
  2199. case MMTYPE_CM_SLAC_PARM_REQ:
  2200. {
  2201. //Check QCA7000 status
  2202. if (EVCOMM_SYS_INFO.QCA7K_SetKeyDone == FALSE)
  2203. {
  2204. //SAVE_SYS_LOG_MSG_EVCOMM("[SLAC][Rx]CM_SLAC_PARM_REQ: ignored(QCA7K init...)");
  2205. break;
  2206. }
  2207. //Check error state
  2208. state = Check_V2G_Flow_Status();
  2209. if (state == Performance_Timeout || //253
  2210. state == Sequence_Timeout || //254
  2211. state == Other_Fault) //255
  2212. {
  2213. SAVE_SYS_LOG_MSG_EVCOMM("[SLAC][Rx]CM_SLAC_PARM_REQ: ignored(in error state)");
  2214. break;
  2215. }
  2216. //Printing EV MAC Address
  2217. SAVE_SYS_LOG_MSG_EVCOMM("[SLAC][Rx]CM_SLAC_PARM_REQ");
  2218. //Avoid Coupled SLAC_PARM_REQ
  2219. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2220. {
  2221. #if 0
  2222. sprintf(buf_log_evcomm,
  2223. "[WARNING][SLAC][Rx]CM_SLAC_PARM_REQ: ignored(coupled SLAC,%d)",
  2224. CheckConnectorPlugIn());
  2225. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2226. #endif
  2227. break;
  2228. }
  2229. //[TC_SECC_VTB_CmSlacParm_003] SECURITY_TYPE needs to be 0x00 (no security)
  2230. if (MmePacket->MMENTRY[1] != 0)
  2231. {
  2232. sprintf(buf_log_evcomm,
  2233. "[WARNING][SLAC][Rx]CM_SLAC_PARM_REQ: ignored(invalid SECURITY_TYPE,%d)", //Source MAC Address (EV MAC)
  2234. MmePacket->MMENTRY[1]);
  2235. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2236. break;
  2237. }
  2238. //=================== Legal CM_SLAC_PARM_REQ Zone =================
  2239. Update_V2G_Flow_Status(CM_SLAC_PARM_REQ);
  2240. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_RX_CM_SLAC_PARM_REQ)
  2241. {
  2242. //Once receiving CM_SLAC_PARM_REQ, set PWM as 5%
  2243. SwitchCpStateE(DISABLE);
  2244. OutputCpPwmDuty(5);
  2245. }
  2246. #endif
  2247. //Checking if this SLAC Req comes before 5% PWM (for 15118)
  2248. if(ShmInternalComm->ChargingPermission == FALSE)
  2249. {
  2250. //Sniffer_Tcpdump(ENABLE);
  2251. sprintf(buf_log_evcomm, "Check Permission: %d (SLAC first => START)", ShmInternalComm->ChargingPermission);
  2252. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2253. }
  2254. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2255. {
  2256. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SLAC_PARM_REQ ---\n");
  2257. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2258. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2259. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2260. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2261. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2262. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7],
  2263. MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2264. DEBUG_PRINTF_EVCOMM_DETAIL("CipherSuiteSetSize: 0x%x\n", MmePacket->MMENTRY[10]);
  2265. DEBUG_PRINTF_EVCOMM_DETAIL("CipherSuite [1]: 0x%x,0x%x\n",
  2266. MmePacket->MMENTRY[11], MmePacket->MMENTRY[12]);
  2267. }
  2268. #endif
  2269. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2270. {
  2271. //Saving each CM_SLAC_PARM_REQ data into EVCC database
  2272. EvMac_in = &MmePacket->OSA[0];
  2273. RunID_in = &MmePacket->MMENTRY[2];
  2274. idx = SLAC_DB_Add(EvMac_in, RunID_in); //(EvMac, RunID)
  2275. if (idx < 0)
  2276. {
  2277. sprintf(buf_log_evcomm,
  2278. "[WARNING][CM_SLAC_PARM_REQ]DB is full or errors occour(%d) => ignore",
  2279. idx);
  2280. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2281. break;
  2282. }
  2283. //Select the 1st EV MAC address
  2284. if (SLAC_INFO.arrayLen == 1) //1st Req
  2285. {
  2286. #if 1
  2287. sprintf(buf_log_evcomm,
  2288. "[SLAC][Rx]CM_SLAC_PARM_REQ[%d](%02X:%02X:%02X:%02X:%02X:%02X,%02X%02X%02X%02X%02X%02X%02X%02X):selected",
  2289. (idx + 1),
  2290. MmePacket->OSA[0], MmePacket->OSA[1], MmePacket->OSA[2], MmePacket->OSA[3], MmePacket->OSA[4], MmePacket->OSA[5],
  2291. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3], MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2292. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7], MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2293. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2294. #endif
  2295. /*
  2296. sprintf(buf_log_evcomm,
  2297. "[SLAC][EVMAC][%d-th]%02X:%02X:%02X:%02X:%02X:%02X (selected)", //Source MAC Address (EV MAC)
  2298. SLAC_INFO.arrayLen,
  2299. MmePacket->OSA[0], MmePacket->OSA[1],
  2300. MmePacket->OSA[2], MmePacket->OSA[3],
  2301. MmePacket->OSA[4], MmePacket->OSA[5]);
  2302. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2303. sprintf(buf_log_evcomm,
  2304. "[SLAC][RunID][%d-th]%02X%02X%02X%02X%02X%02X%02X%02X (selected)", //RunID (from EVCC)
  2305. SLAC_INFO.arrayLen,
  2306. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2307. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2308. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7],
  2309. MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2310. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2311. */
  2312. }
  2313. else //2nd Req
  2314. {
  2315. #if 1
  2316. sprintf(buf_log_evcomm,
  2317. "[SLAC][Rx]CM_SLAC_PARM_REQ[%d](%02X:%02X:%02X:%02X:%02X:%02X,%02X%02X%02X%02X%02X%02X%02X%02X):not selected",
  2318. (idx + 1),
  2319. MmePacket->OSA[0], MmePacket->OSA[1], MmePacket->OSA[2], MmePacket->OSA[3], MmePacket->OSA[4], MmePacket->OSA[5],
  2320. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3], MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2321. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7], MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2322. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2323. #endif
  2324. /*
  2325. sprintf(buf_log_evcomm,
  2326. "[SLAC][EVMAC][%d-th]%02X:%02X:%02X:%02X:%02X:%02X (not selected)", //Source MAC Address (EV MAC)
  2327. SLAC_INFO.arrayLen,
  2328. MmePacket->OSA[0], MmePacket->OSA[1],
  2329. MmePacket->OSA[2], MmePacket->OSA[3],
  2330. MmePacket->OSA[4], MmePacket->OSA[5]);
  2331. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2332. sprintf(buf_log_evcomm,
  2333. "[SLAC][RunID][%d-th]%02X%02X%02X%02X%02X%02X%02X%02X (not selected)", //RunID (from EVCC)
  2334. SLAC_INFO.arrayLen,
  2335. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2336. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2337. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7],
  2338. MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2339. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2340. */
  2341. }
  2342. memcpy(EvMac, MmePacket->OSA, sizeof(EvMac));
  2343. memcpy(DestSocketAddress.sll_addr, MmePacket->OSA, SLAC_EVMAC_LENGTH);
  2344. memcpy(SlacRunId, MmePacket->MMENTRY + 2, SLAC_RUNID_LENGTH);
  2345. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  2346. memcpy(SendMmePacket.ODA, MmePacket->OSA, 6);
  2347. memcpy(SendMmePacket.OSA, CsuMac, 6);
  2348. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  2349. SendMmePacket.MMV = MmePacket->MMV;
  2350. SendMmePacket.MMTYPE = MMTYPE_CM_SLAC_PARM_CNF;
  2351. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  2352. SendMmePacketSize = 0;
  2353. memset(SendMmePacket.MMENTRY, 0xFF, 6); //M-SOUND_TARGET(6-byte:0xFFFFFFFFFFFF): Fixed value indicating that M-Sounds to be sent as Ethernet broadcast
  2354. SendMmePacketSize += 6;
  2355. SendMmePacket.MMENTRY[SendMmePacketSize++] = C_EV_match_MNBC; //NUM_SOUNDS(0x0A): Number of M-Sounds to be transmitted by the EV GP Station during the SLAC process
  2356. SendMmePacket.MMENTRY[SendMmePacketSize++] = 6; //Time_Out(0x06): unit = 100ms
  2357. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x01; //RESP_TYPE(0x01): Fixed value indicating Other GP station, 0x00 V HLE of the STA, 0x01 V Another GP STA, 0x02 V 0xFF V Reserved
  2358. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, SLAC_INFO.array[idx].EvMac, 6); //FORWARDING_STA(MAC Address of the EV HLE)
  2359. SendMmePacketSize += 6;
  2360. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00): 0x00(PEV-EVSE Association), 0x01-0xFF(Reserved)
  2361. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00): 0x00(No Security), 0x01(Public Key Signature), 0x02-0xFF(Reserved)
  2362. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, SlacRunId, SLAC_RUNID_LENGTH); //RunID (8-byte)
  2363. SendMmePacketSize += SLAC_RUNID_LENGTH;
  2364. SendMmePacketSize += 19; //the size before MMENTRY
  2365. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2366. {
  2367. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response MME Packet *****\n");
  2368. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2369. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  2370. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2371. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  2372. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  2373. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  2374. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  2375. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  2376. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_PARM_CNF ---\n");
  2377. DEBUG_PRINTF_EVCOMM_DETAIL("M-SOUND_TARGET: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2378. SendMmePacket.MMENTRY[0],SendMmePacket.MMENTRY[1],SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],
  2379. SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5]);
  2380. DEBUG_PRINTF_EVCOMM_DETAIL("NUM_SOUNDS: 0x%x\n", SendMmePacket.MMENTRY[6]);
  2381. DEBUG_PRINTF_EVCOMM_DETAIL("Time_Out: 0x%x\n", SendMmePacket.MMENTRY[7]);
  2382. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_TYPE: 0x%x\n", SendMmePacket.MMENTRY[8]);
  2383. DEBUG_PRINTF_EVCOMM_DETAIL("M-FORWARDING_STA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2384. SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],SendMmePacket.MMENTRY[12],
  2385. SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14]);
  2386. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[15]);
  2387. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[16]);
  2388. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2389. SendMmePacket.MMENTRY[17],SendMmePacket.MMENTRY[18],SendMmePacket.MMENTRY[19],SendMmePacket.MMENTRY[20],
  2390. SendMmePacket.MMENTRY[21],SendMmePacket.MMENTRY[22],SendMmePacket.MMENTRY[23],SendMmePacket.MMENTRY[24]);
  2391. }
  2392. #endif
  2393. Update_V2G_Flow_Status(CM_SLAC_PARM_CONF);
  2394. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  2395. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  2396. ftime(&SeqStartTime);
  2397. break;
  2398. }
  2399. #else
  2400. {
  2401. memcpy(EvMac, MmePacket->OSA, sizeof(EvMac));
  2402. memcpy(DestSocketAddress.sll_addr, MmePacket->OSA, sizeof(EvMac));
  2403. memcpy(SlacRunId,MmePacket->MMENTRY + 2, sizeof(SlacRunId));
  2404. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  2405. memcpy(SendMmePacket.ODA, MmePacket->OSA, 6);
  2406. memcpy(SendMmePacket.OSA, CsuMac, 6);
  2407. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  2408. SendMmePacket.MMV = MmePacket->MMV;
  2409. SendMmePacket.MMTYPE = MMTYPE_CM_SLAC_PARM_CNF;
  2410. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  2411. SendMmePacketSize = 0;
  2412. memset(SendMmePacket.MMENTRY, 0xFF, 6); //M-SOUND_TARGET(6-byte:0xFFFFFFFFFFFF): Fixed value indicating that M-Sounds to be sent as Ethernet broadcast
  2413. SendMmePacketSize += 6;
  2414. SendMmePacket.MMENTRY[SendMmePacketSize++] = C_EV_match_MNBC; //NUM_SOUNDS(0x0A): Number of M-Sounds to be transmitted by the EV GP Station during the SLAC process
  2415. SendMmePacket.MMENTRY[SendMmePacketSize++] = 6; //Time_Out(0x06): unit = 100ms
  2416. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x01; //RESP_TYPE(0x01): Fixed value indicating Other GP station, 0x00 V HLE of the STA, 0x01 V Another GP STA, 0x02 V 0xFF V Reserved
  2417. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac, 6); //FORWARDING_STA(MAC Address of the EV HLE)
  2418. SendMmePacketSize += 6;
  2419. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00): 0x00(PEV-EVSE Association), 0x01-0xFF(Reserved)
  2420. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00): 0x00(No Security), 0x01(Public Key Signature), 0x02-0xFF(Reserved)
  2421. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, SlacRunId, sizeof(SlacRunId)); //RunID (8-byte)
  2422. SendMmePacketSize += sizeof(SlacRunId);
  2423. SendMmePacketSize += 19; //the size before MMENTRY
  2424. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2425. {
  2426. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response MME Packet *****\n");
  2427. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2428. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  2429. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2430. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  2431. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  2432. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  2433. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  2434. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  2435. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_PARM_CNF ---\n");
  2436. DEBUG_PRINTF_EVCOMM_DETAIL("M-SOUND_TARGET: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2437. SendMmePacket.MMENTRY[0],SendMmePacket.MMENTRY[1],SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],
  2438. SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5]);
  2439. DEBUG_PRINTF_EVCOMM_DETAIL("NUM_SOUNDS: 0x%x\n", SendMmePacket.MMENTRY[6]);
  2440. DEBUG_PRINTF_EVCOMM_DETAIL("Time_Out: 0x%x\n", SendMmePacket.MMENTRY[7]);
  2441. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_TYPE: 0x%x\n", SendMmePacket.MMENTRY[8]);
  2442. DEBUG_PRINTF_EVCOMM_DETAIL("M-FORWARDING_STA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2443. SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],SendMmePacket.MMENTRY[12],
  2444. SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14]);
  2445. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[15]);
  2446. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[16]);
  2447. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2448. SendMmePacket.MMENTRY[17],SendMmePacket.MMENTRY[18],SendMmePacket.MMENTRY[19],SendMmePacket.MMENTRY[20],
  2449. SendMmePacket.MMENTRY[21],SendMmePacket.MMENTRY[22],SendMmePacket.MMENTRY[23],SendMmePacket.MMENTRY[24]);
  2450. }
  2451. #endif
  2452. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  2453. Update_V2G_Flow_Status(CM_SLAC_PARM_CONF);
  2454. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  2455. ftime(&SeqStartTime);
  2456. counter = 0;
  2457. break;
  2458. }
  2459. #endif
  2460. }
  2461. case MMTYPE_CM_START_ATTEN_CHAR_IND:
  2462. {
  2463. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2464. {
  2465. SAVE_SYS_LOG_MSG_EVCOMM("[WARNING][SLAC][Rx]CM_START_ATTEN_CHAR_IND: ignored(timeup)");
  2466. break;
  2467. }
  2468. //Avoid Coupled CM_START_ATTEN_CHAR_IND
  2469. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2470. {
  2471. sprintf(buf_log_evcomm,
  2472. "[WARNING][SLAC][Rx]CM_START_ATTEN_CHAR_IND: ignored(coupled SLAC,%d)",
  2473. CheckConnectorPlugIn());
  2474. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2475. break;
  2476. }
  2477. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2478. {
  2479. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_START_ATTEN_CHAR_IND (counter : %d/3 ) ---\n",counter + 1);
  2480. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2481. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2482. DEBUG_PRINTF_EVCOMM_DETAIL("NUM_SOUNDS: 0x%x\n", MmePacket->MMENTRY[2]);
  2483. DEBUG_PRINTF_EVCOMM_DETAIL("Time_Out 0x%x\n", MmePacket->MMENTRY[3]);
  2484. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_TYPE 0x%x\n", MmePacket->MMENTRY[4]); //Fixed value (0x01) indicating other Green PHY station
  2485. DEBUG_PRINTF_EVCOMM_DETAIL("FORWARDING_STA: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2486. MmePacket->MMENTRY[5],MmePacket->MMENTRY[6],MmePacket->MMENTRY[7],MmePacket->MMENTRY[8],
  2487. MmePacket->MMENTRY[9],MmePacket->MMENTRY[10]);
  2488. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2489. MmePacket->MMENTRY[11],MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],MmePacket->MMENTRY[14],
  2490. MmePacket->MMENTRY[15],MmePacket->MMENTRY[16],MmePacket->MMENTRY[17],MmePacket->MMENTRY[18]);
  2491. }
  2492. #endif
  2493. //New SLAC architecture designed by Joseph
  2494. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2495. {
  2496. EvMac_in = &MmePacket->OSA[0];
  2497. RunID_in = &MmePacket->MMENTRY[11];
  2498. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2499. if (idx >= 0)
  2500. {
  2501. Update_V2G_Flow_Status(CM_START_ATTEN_CHAR_IND);
  2502. //[TC_SECC_VTB_AttenuationCharacterization_013]
  2503. if (MmePacket->MMENTRY[0] != 0) //APPLICATION_TYPE must be 0x00(EV-EVSE Matching)
  2504. {
  2505. sprintf(buf_log_evcomm,
  2506. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]APPLICATION_TYPE(%d): invalid => ignore Req",
  2507. MmePacket->MMENTRY[0]);
  2508. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2509. break;
  2510. }
  2511. //[TC_SECC_VTB_AttenuationCharacterization_014]
  2512. if (MmePacket->MMENTRY[1] != 0) //SECURITY_TYPE must be 0x00(No Security)
  2513. {
  2514. sprintf(buf_log_evcomm,
  2515. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]SECURITY_TYPE(%d): invalid => ignore Req",
  2516. MmePacket->MMENTRY[1]);
  2517. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2518. break;
  2519. }
  2520. //[TC_SECC_VTB_AttenuationCharacterization_017]
  2521. if (MmePacket->MMENTRY[4] != 0x01) //RESP_TYPE must be 0x01(Send to another GP STA(EV))
  2522. {
  2523. sprintf(buf_log_evcomm,
  2524. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]RESP_TYPE(%d): invalid => ignore Req",
  2525. MmePacket->MMENTRY[4]);
  2526. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2527. break;
  2528. }
  2529. //[TC_SECC_VTB_AttenuationCharacterization_021]
  2530. //FORWARDING_STA: MAC Address of the EV HLE (station which the measurement results shall be sent to)
  2531. if (Array_Compare_Identity(EvMac_in, &MmePacket->MMENTRY[5], SLAC_EVMAC_LENGTH) == FALSE)
  2532. {
  2533. sprintf(buf_log_evcomm,
  2534. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]FORWARDING_STA(%02X:%02X:%02X:%02X:%02X:%02X): invalid => ignore Req",
  2535. MmePacket->MMENTRY[5], MmePacket->MMENTRY[6],
  2536. MmePacket->MMENTRY[7], MmePacket->MMENTRY[8],
  2537. MmePacket->MMENTRY[9], MmePacket->MMENTRY[10]);
  2538. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2539. SLAC_INFO.array[idx].StartAttenCharErr = TRUE;
  2540. break;
  2541. }
  2542. //Check RunID
  2543. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE)
  2544. {
  2545. SLAC_INFO.array[idx].MnbcSoundNum = MmePacket->MMENTRY[2];
  2546. SLAC_INFO.array[idx].StartAttenCharCnt++;
  2547. #if 0
  2548. sprintf(buf_log_evcomm,
  2549. "[SLAC][Rx]CM_START_ATTEN_CHAR_IND[%d]:%d-th",
  2550. (idx + 1),
  2551. SLAC_INFO.array[idx].StartAttenCharCnt);
  2552. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2553. #endif
  2554. if (SLAC_INFO.array[idx].StartAttenCharCnt == 1)
  2555. {
  2556. memset(SLAC_INFO.array[idx].AAG, 0, sizeof(SLAC_INFO.array[idx].AAG));
  2557. SLAC_INFO.array[idx].AttenProfileCnt = 0;
  2558. ftime(&SeqStartTime); //start TT_EVSE_match_MNBC
  2559. }
  2560. else if (SLAC_INFO.array[idx].StartAttenCharCnt >= 3)
  2561. {
  2562. sprintf(buf_log_evcomm,
  2563. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]counter(%d): unexpected",
  2564. SLAC_INFO.array[idx].StartAttenCharCnt);
  2565. }
  2566. else
  2567. {
  2568. //null
  2569. }
  2570. }
  2571. else
  2572. {
  2573. //This RunID is not matched with this EvMac,
  2574. //or this RunID is not found in DB.
  2575. sprintf(buf_log_evcomm,
  2576. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]EvMac-RunID: unmatched => ignore Req");
  2577. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2578. //Response: ignore
  2579. }
  2580. }
  2581. else
  2582. {
  2583. //this msg source is not in database
  2584. //ignore
  2585. }
  2586. break;
  2587. }
  2588. #else //Old SLAC architecture designed by Vern
  2589. {
  2590. MnbcSoundNum = MmePacket->MMENTRY[2];
  2591. Update_V2G_Flow_Status(CM_START_ATTEN_CHAR_IND);
  2592. counter++;
  2593. if(counter == 1)
  2594. {
  2595. memset(Aag, 0, sizeof(Aag));
  2596. AttenProfileCnt = 0;
  2597. ftime(&SeqStartTime); //start TT_EVSE_match_MNBC
  2598. }
  2599. else if(counter >= 3)
  2600. {
  2601. counter = 0;
  2602. }
  2603. break;
  2604. }
  2605. #endif
  2606. }
  2607. case MMTYPE_CM_MNBC_SOUND_IND:
  2608. {
  2609. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2610. {
  2611. SAVE_SYS_LOG_MSG_EVCOMM("[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND: ignored(timeup)");
  2612. break;
  2613. }
  2614. //Avoid Coupled CM_MNBC_SOUND_IND
  2615. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2616. {
  2617. sprintf(buf_log_evcomm,
  2618. "[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND: ignored(coupled SLAC,%d)",
  2619. CheckConnectorPlugIn());
  2620. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2621. break;
  2622. }
  2623. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == TRUE)
  2624. {
  2625. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_MNBC_SOUND_IND (counter : %d/%d) ---\n",counter + 1 , MnbcSoundNum);
  2626. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2627. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2628. DEBUG_PRINTF_EVCOMM_DETAIL("SenderID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2629. MmePacket->MMENTRY[2],MmePacket->MMENTRY[3],MmePacket->MMENTRY[4],MmePacket->MMENTRY[5],
  2630. MmePacket->MMENTRY[6],MmePacket->MMENTRY[7],MmePacket->MMENTRY[8],MmePacket->MMENTRY[9],
  2631. MmePacket->MMENTRY[10],MmePacket->MMENTRY[11],MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],
  2632. MmePacket->MMENTRY[14],MmePacket->MMENTRY[15],MmePacket->MMENTRY[16],MmePacket->MMENTRY[17],
  2633. MmePacket->MMENTRY[18]);
  2634. DEBUG_PRINTF_EVCOMM_DETAIL("Cnt: 0x%x\n", MmePacket->MMENTRY[19]);
  2635. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2636. MmePacket->MMENTRY[20],MmePacket->MMENTRY[21],MmePacket->MMENTRY[22],MmePacket->MMENTRY[23],
  2637. MmePacket->MMENTRY[24],MmePacket->MMENTRY[25],MmePacket->MMENTRY[26],MmePacket->MMENTRY[27]);
  2638. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2639. MmePacket->MMENTRY[28],MmePacket->MMENTRY[29],MmePacket->MMENTRY[30],MmePacket->MMENTRY[31],
  2640. MmePacket->MMENTRY[32],MmePacket->MMENTRY[33],MmePacket->MMENTRY[34],MmePacket->MMENTRY[35]);
  2641. DEBUG_PRINTF_EVCOMM_DETAIL("Rnd: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2642. MmePacket->MMENTRY[36],MmePacket->MMENTRY[37],MmePacket->MMENTRY[38],MmePacket->MMENTRY[39],
  2643. MmePacket->MMENTRY[40],MmePacket->MMENTRY[41],MmePacket->MMENTRY[42],MmePacket->MMENTRY[43],
  2644. MmePacket->MMENTRY[44],MmePacket->MMENTRY[45],MmePacket->MMENTRY[46],MmePacket->MMENTRY[47],
  2645. MmePacket->MMENTRY[48],MmePacket->MMENTRY[49],MmePacket->MMENTRY[50],MmePacket->MMENTRY[51]);
  2646. }
  2647. #endif
  2648. //New SLAC architecture designed by Joseph
  2649. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2650. {
  2651. EvMac_in = &MmePacket->OSA[0];
  2652. RunID_in = &MmePacket->MMENTRY[20];
  2653. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2654. if (idx >= 0)
  2655. {
  2656. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2657. //Check for RunID
  2658. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE)
  2659. {
  2660. SLAC_INFO.array[idx].MnbcSoundCnt++;
  2661. /*
  2662. sprintf(buf_log_evcomm,
  2663. "[SLAC][Rx]CM_MNBC_SOUND_IND[%d]:%d-th",
  2664. (idx + 1),
  2665. SLAC_INFO.array[idx].MnbcSoundCnt);
  2666. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2667. */
  2668. }
  2669. else
  2670. {
  2671. //RunID is not matched or does not exist.
  2672. sprintf(buf_log_evcomm,
  2673. "[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND]EvMac-RunID: unmatched");
  2674. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2675. }
  2676. }
  2677. else
  2678. {
  2679. //ignore
  2680. sprintf(buf_log_evcomm,
  2681. "[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND]EvMac does not exist");
  2682. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2683. }
  2684. break;
  2685. }
  2686. #else //Old SLAC architecture designed by Vern
  2687. {
  2688. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2689. counter++;
  2690. break;
  2691. }
  2692. #endif
  2693. }
  2694. case MMTYPE_CM_ATTEN_PROFILE_IND:
  2695. {
  2696. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2697. {
  2698. SAVE_SYS_LOG_MSG_EVCOMM("[WARNING][SLAC][Rx]CM_ATTEN_PROFILE_IND: ignore(timeup)");
  2699. break;
  2700. }
  2701. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == TRUE)
  2702. {
  2703. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_ATTEN_PROFILE_IND (counter : %d/%d) ---\n", counter, MnbcSoundNum);
  2704. DEBUG_PRINTF_EVCOMM_DETAIL("EV MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2705. MmePacket->MMENTRY[0], MmePacket->MMENTRY[1], MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2706. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5]);
  2707. DEBUG_PRINTF_EVCOMM_DETAIL("NumGroups: 0x%x\n", MmePacket->MMENTRY[6]); //NumGroups, Number of AAG Groups (ex: 0x3A = 58)
  2708. //NumGroups: Number of OFDM carrier groups used for the SLAC signal characterization.
  2709. //AAG[i]: Average Attenuation of Group i (i = 1 ~ 58)
  2710. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: 0x%x\n", MmePacket->MMENTRY[7]);
  2711. DEBUG_PRINTF_EVCOMM_DETAIL("AAG: \n");
  2712. for(Rtn = 0; Rtn < MmePacket->MMENTRY[6]; Rtn++)
  2713. {
  2714. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[8 + Rtn]);
  2715. }
  2716. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  2717. }
  2718. #endif
  2719. //New SLAC architecture designed by Joseph
  2720. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2721. {
  2722. EvMac_in = &MmePacket->MMENTRY[0]; //[CAUTION] The EvMac address is not &MmePacket->OSA[0]
  2723. //[CAUTION] There is no RunID information of CM_ATTEN_PROFILE_IND
  2724. // packet, which means SECC cannot use the RunID to
  2725. // distinguish those SLAC request with identical EvMac
  2726. // but with different RunID.
  2727. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2728. /*
  2729. printf("%d, %d, %d, %d\n",
  2730. idx,
  2731. SLAC_INFO.array[idx].AagGroupsNum,
  2732. MmePacket->MMENTRY[6],
  2733. SLAC_INFO.array[idx].AttenProfileCnt);
  2734. */
  2735. if (idx >= 0)
  2736. {
  2737. SLAC_INFO.array[idx].AttenProfileCnt++;
  2738. /*
  2739. sprintf(buf_log_evcomm,
  2740. "[SLAC][Rx]CM_ATTEN_PROFILE_IND[%d]:%d-th",
  2741. (idx + 1),
  2742. SLAC_INFO.array[idx].AttenProfileCnt);
  2743. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2744. */
  2745. //printf("%d, %d\n", SLAC_INFO.array[idx].AagGroupsNum, MmePacket->MMENTRY[6]);
  2746. SLAC_INFO.array[idx].AagGroupsNum = MmePacket->MMENTRY[6];
  2747. ////NumGroups, Number of AAG Groups (ex: 0x3A = 58)
  2748. for(Rtn = 0; Rtn < SLAC_INFO.array[idx].AagGroupsNum; Rtn++) //ex:58
  2749. {
  2750. SLAC_INFO.array[idx].AAG[Rtn] += MmePacket->MMENTRY[8 + Rtn];
  2751. }
  2752. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2753. break;
  2754. }
  2755. else
  2756. {
  2757. //The EvMac is not in the database
  2758. //ignore
  2759. sprintf(buf_log_evcomm,
  2760. "[SLAC][Rx][CM_ATTEN_PROFILE_IND]EvMac(%02X%02X%02X%02X%02X%02X): not exist => ignore",
  2761. EvMac_in[0], EvMac_in[1], EvMac_in[2], EvMac_in[3], EvMac_in[4], EvMac_in[5]);
  2762. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2763. break;
  2764. }
  2765. }
  2766. #else //Old SLAC architecture designed by Vern
  2767. {
  2768. AagGroupsNum = MmePacket->MMENTRY[6];
  2769. for(Rtn = 0; Rtn < MmePacket->MMENTRY[6]; Rtn++)
  2770. {
  2771. Aag[Rtn] += MmePacket->MMENTRY[8 + Rtn];
  2772. }
  2773. AttenProfileCnt++;
  2774. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2775. break;
  2776. }
  2777. #endif
  2778. }
  2779. case MMTYPE_CM_ATTEN_CHAR_RSP:
  2780. {
  2781. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2782. {
  2783. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_ATTEN_CHAR_RSP ---\n");
  2784. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2785. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2786. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2787. MmePacket->MMENTRY[2],MmePacket->MMENTRY[3],MmePacket->MMENTRY[4],MmePacket->MMENTRY[5],
  2788. MmePacket->MMENTRY[6],MmePacket->MMENTRY[7]);
  2789. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2790. MmePacket->MMENTRY[8],MmePacket->MMENTRY[9],MmePacket->MMENTRY[10],MmePacket->MMENTRY[11],
  2791. MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],MmePacket->MMENTRY[14],MmePacket->MMENTRY[15]);
  2792. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ID: \n");
  2793. for(Rtn = 0; Rtn < 17; Rtn++)
  2794. {
  2795. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[16 + Rtn]);
  2796. }
  2797. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  2798. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_ID: \n");
  2799. for(Rtn = 0; Rtn < 17; Rtn++)
  2800. {
  2801. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[33 + Rtn]);
  2802. }
  2803. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  2804. DEBUG_PRINTF_EVCOMM_DETAIL("Result: 0x%x\n", MmePacket->MMENTRY[50]); //Fixed value of 0x00 indicates a successful SLAC process
  2805. }
  2806. #endif
  2807. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2808. {
  2809. //Check ODA (Destination Address)
  2810. if (Array_Compare_Identity(CsuMac, MmePacket->ODA, SLAC_EVSE_MAC_LENGTH) == FALSE)
  2811. {
  2812. SAVE_SYS_LOG_MSG_EVCOMM("[SLAC][Rx][CM_ATTEN_CHAR_RSP]wrong ODA: ignore");
  2813. break;
  2814. }
  2815. EvMac_in = &MmePacket->OSA[0];
  2816. RunID_in = &MmePacket->MMENTRY[8];
  2817. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2818. //Check Parameters
  2819. if ((MmePacket->MMENTRY[0] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_005] applicationType must be 0x00
  2820. (MmePacket->MMENTRY[1] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_006] securityType must be 0x00
  2821. (SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in) < 0) || //[TC_SECC_VTB_AttenuationCharacterization_007] sourceAddress cannot not be all zero
  2822. (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_008] check for invalid runID
  2823. (Array_Check_All_Zero(&MmePacket->MMENTRY[16], 17) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_009] SourceID(17 bytes) must be all zero.
  2824. (Array_Check_All_Zero(&MmePacket->MMENTRY[33], 17) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_009] RESP_ID (17 bytes) must be all zero.
  2825. (MmePacket->MMENTRY[50] != 0) //Result: must be 0x00(Success)
  2826. )
  2827. {
  2828. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry++;
  2829. sprintf(buf_log_evcomm,
  2830. "[WARNING][SLAC][Rx]CM_ATTEN_CHAR_RSP:invalid para(%d,%d,%d,%d,%d) => %d-th retry",
  2831. MmePacket->MMENTRY[0], //applicationType must be 0x00
  2832. MmePacket->MMENTRY[1], //securityType must be 0x00
  2833. SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in),
  2834. SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in),
  2835. MmePacket->MMENTRY[50],
  2836. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry
  2837. );
  2838. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2839. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry <= 2) //limited to 2 retries
  2840. {
  2841. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2842. ftime(&SeqStartTime);
  2843. break;
  2844. }
  2845. else
  2846. {
  2847. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR][SLAC][Rx]CM_ATTEN_CHAR_RSP:invalid para => 2-retry fail => End_Process");
  2848. Update_V2G_Flow_Status(Other_Fault);
  2849. break;
  2850. }
  2851. }
  2852. else
  2853. {
  2854. //The CM_ATTEN_CHAR_IND is legal
  2855. SLAC_INFO.array[idx].AttenCharRspCnt++;
  2856. sprintf(buf_log_evcomm,
  2857. "[SLAC][Rx]CM_ATTEN_CHAR_RSP[%d]:%d-th",
  2858. (idx + 1),
  2859. SLAC_INFO.array[idx].AttenCharRspCnt);
  2860. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2861. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry = 0;
  2862. Update_V2G_Flow_Status(CM_ATTEN_CHAR_RSP);
  2863. ftime(&SeqStartTime);
  2864. break;
  2865. }
  2866. }
  2867. #else
  2868. {
  2869. //Check Parameters
  2870. if ((MmePacket->MMENTRY[0] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_005] applicationType must be 0x00
  2871. (MmePacket->MMENTRY[1] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_006] securityType must be 0x00
  2872. (Array_Compare_Identity(SLAC_INFO.array[0].EvMac, &MmePacket->MMENTRY[2], 6) == TRUE) || //[TC_SECC_VTB_AttenuationCharacterization_007] sourceAddress cannot not be all zero
  2873. (Array_Compare_Identity(SLAC_INFO.array[0].RunID, &MmePacket->MMENTRY[8], 8) == TRUE) //[TC_SECC_VTB_AttenuationCharacterization_008] check for invalid runID
  2874. )
  2875. {
  2876. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry++;
  2877. sprintf(buf_log_evcomm,
  2878. "[WARNING][SLAC][Rx]CM_ATTEN_CHAR_RSP: invalid parameters(%d-th)",
  2879. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry);
  2880. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2881. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry <= 2) //limited to 2 retries
  2882. {
  2883. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2884. ftime(&SeqStartTime);
  2885. break;
  2886. }
  2887. else
  2888. {
  2889. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]CM_ATTEN_CHAR_RSP: invalid parameters => End_Process");
  2890. Update_V2G_Flow_Status(Other_Fault);
  2891. ftime(&SeqStartTime);
  2892. break;
  2893. }
  2894. }
  2895. else
  2896. {
  2897. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry = 0;
  2898. Update_V2G_Flow_Status(CM_ATTEN_CHAR_RSP);
  2899. ftime(&SeqStartTime);
  2900. break;
  2901. }
  2902. }
  2903. #endif
  2904. }
  2905. case MMTYPE_CM_VALIDATE_REQ: //BCB Toggle
  2906. {
  2907. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2908. {
  2909. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_VALIDATE_REQ ---\n");
  2910. DEBUG_PRINTF_EVCOMM_DETAIL("Signal Type: 0x%x\n", MmePacket->MMENTRY[0]); //Fixed value (0x00) to indicate ��PEV S2 toggles on control pilot line��
  2911. DEBUG_PRINTF_EVCOMM_DETAIL("Timer: 0x%x\n", MmePacket->MMENTRY[1]); //Fixed value In the first VALIDATE Request-Response exchange, the Timer field shall be set to zero.
  2912. DEBUG_PRINTF_EVCOMM_DETAIL("Result: 0x%x\n", MmePacket->MMENTRY[2]); //Fixed value In the first VALIDATE Request-Response exchange, the Result field shall be set to 0x01 = ��ready��.
  2913. }
  2914. #endif
  2915. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2916. {
  2917. counter = 0;
  2918. EvMac_in = &MmePacket->OSA[0];
  2919. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2920. if (idx >= 0)
  2921. {
  2922. Update_V2G_Flow_Status(CM_VALIDATE_CNF);
  2923. SLAC_INFO.array[idx].ValidateReqCnt++;
  2924. sprintf(buf_log_evcomm,
  2925. "[SLAC][Rx]CM_VALIDATE_REQ[%d]:%d-th",
  2926. (idx + 1),
  2927. SLAC_INFO.array[idx].ValidateReqCnt);
  2928. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  2929. //[To-Do] Protection
  2930. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  2931. memcpy(SendMmePacket.ODA, EvMac_in, SLAC_EVMAC_LENGTH);
  2932. memcpy(SendMmePacket.OSA, CsuMac, 6);
  2933. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  2934. SendMmePacket.MMV = 0x01;
  2935. SendMmePacket.MMTYPE = MMTYPE_CM_VALIDATE_CNF;
  2936. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  2937. SendMmePacketSize = 0;
  2938. if(counter == 0)
  2939. {
  2940. //The First MMTYPE_CM_VALIDATE_REQ because Unicast
  2941. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SignalType(0x00): Fixed value to indicate "EV S2 toggles on control pilot line"
  2942. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //ToggleNum(0x00): Fixed value. In the first VALIDATE Request-Response exchange, the ToggleNum field shall be set to zero.
  2943. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  2944. SendMmePacket.MMENTRY[SendMmePacketSize++] = 1; //0x01 = Ready
  2945. #else
  2946. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  2947. #endif
  2948. }
  2949. else
  2950. {
  2951. //The Second MMTYPE_CM_VALIDATE_REQ because Broadcast
  2952. unsigned char PreStatus = 3;
  2953. unsigned char ToggleNum = 0;
  2954. ftime(&SeqStartTime);
  2955. while(1)
  2956. {
  2957. if((EVCOMM_SYS_INFO.CpState == 4) && (PreStatus == 3))
  2958. {
  2959. ToggleNum++;
  2960. PreStatus = 4;
  2961. }
  2962. else
  2963. {
  2964. PreStatus = 3;
  2965. }
  2966. ftime(&SeqEndTime);
  2967. if(DiffTimeb(SeqStartTime, SeqEndTime) >= (SendMmePacket.MMENTRY[1]*100 + 100))
  2968. {
  2969. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value to indicate "PEV S2 toggles on control pilot line"
  2970. SendMmePacket.MMENTRY[SendMmePacketSize++] = ToggleNum;
  2971. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  2972. SendMmePacket.MMENTRY[SendMmePacketSize++] = 2; //0x02 = Success
  2973. #else
  2974. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  2975. #endif
  2976. break;
  2977. }
  2978. }
  2979. }
  2980. SendMmePacketSize += 19; //the size before MMENTRY
  2981. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  2982. ftime(&SeqStartTime);
  2983. }
  2984. else
  2985. {
  2986. //EvMac does not exist.
  2987. //ignore
  2988. }
  2989. break;
  2990. }
  2991. #else
  2992. {
  2993. counter = 0;
  2994. for(Rtn = 0; Rtn < 6; Rtn++)
  2995. {
  2996. if(MmePacket->ODA[Rtn] != CsuMac[Rtn])
  2997. {
  2998. counter = 1;
  2999. break;
  3000. }
  3001. }
  3002. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  3003. memcpy(SendMmePacket.ODA, EvMac,6);
  3004. memcpy(SendMmePacket.OSA, CsuMac,6);
  3005. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  3006. SendMmePacket.MMV = 0x01;
  3007. SendMmePacket.MMTYPE = MMTYPE_CM_VALIDATE_CNF;
  3008. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  3009. SendMmePacketSize = 0;
  3010. if(counter == 0)
  3011. {
  3012. //The First MMTYPE_CM_VALIDATE_REQ because Unicast
  3013. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value to indicate PEV S2 toggles on control pilot line
  3014. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value In the first VALIDATE Request-Response exchange, the ToggleNum field shall be set to zero.
  3015. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  3016. SendMmePacket.MMENTRY[SendMmePacketSize++] = 1; //0x01 = Ready
  3017. #else
  3018. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  3019. #endif
  3020. }
  3021. else
  3022. {
  3023. //The Second MMTYPE_CM_VALIDATE_REQ because Broadcast
  3024. unsigned char PreStatus = 3, ToggleNum = 0;
  3025. ftime(&SeqStartTime);
  3026. while(1)
  3027. {
  3028. ftime(&SeqEndTime);
  3029. if((EVCOMM_SYS_INFO.CpState == 4) && (PreStatus == 3))
  3030. {
  3031. ToggleNum++;
  3032. PreStatus = 4;
  3033. }
  3034. else
  3035. {
  3036. PreStatus = 3;
  3037. }
  3038. if(DiffTimeb(SeqStartTime, SeqEndTime) >= (SendMmePacket.MMENTRY[1]*100 + 100))
  3039. {
  3040. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value to indicate ��PEV S2 toggles on control pilot line��
  3041. SendMmePacket.MMENTRY[SendMmePacketSize++] = ToggleNum;
  3042. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  3043. SendMmePacket.MMENTRY[SendMmePacketSize++] = 2; //0x02 = Success
  3044. #else
  3045. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  3046. #endif
  3047. break;
  3048. }
  3049. }
  3050. }
  3051. SendMmePacketSize += 19; //the size before MMENTRY
  3052. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3053. Update_V2G_Flow_Status(CM_VALIDATE_CNF);
  3054. ftime(&SeqStartTime);
  3055. break;
  3056. }
  3057. #endif
  3058. }
  3059. case MMTYPE_CM_SLAC_MATCH_REQ:
  3060. {
  3061. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  3062. {
  3063. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SLAC_MATCH_REQ ---\n");
  3064. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  3065. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  3066. DEBUG_PRINTF_EVCOMM_DETAIL("MVFLength: 0x%x, 0x%x\n", MmePacket->MMENTRY[2],MmePacket->MMENTRY[3]); //Fixed value (0x3E) for matching
  3067. DEBUG_PRINTF_EVCOMM_DETAIL("PEV ID: \n");
  3068. for(Rtn=0; Rtn<17; Rtn++)
  3069. {
  3070. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[4+Rtn]);
  3071. }
  3072. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3073. DEBUG_PRINTF_EVCOMM_DETAIL("PEV MAC: \n");
  3074. for(Rtn=0; Rtn<6; Rtn++)
  3075. {
  3076. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[21+Rtn]);
  3077. }
  3078. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3079. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE ID: \n");
  3080. for(Rtn=0; Rtn<17; Rtn++)
  3081. {
  3082. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[27+Rtn]);
  3083. }
  3084. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3085. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE MAC: \n");
  3086. for(Rtn=0; Rtn<6; Rtn++)
  3087. {
  3088. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[44+Rtn]);
  3089. }
  3090. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3091. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: \n");
  3092. for(Rtn=0; Rtn<8; Rtn++)
  3093. {
  3094. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[50+Rtn]);
  3095. }
  3096. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3097. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: \n");
  3098. for(Rtn=0; Rtn<8; Rtn++)
  3099. {
  3100. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[58+Rtn]);
  3101. }
  3102. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3103. }
  3104. #endif
  3105. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  3106. {
  3107. //SAVE_SYS_LOG_MSG_EVCOMM("[SLAC][Rx]CM_SLAC_MATCH_REQ");
  3108. //Check ODA (Destination Address)
  3109. if (Array_Compare_Identity(CsuMac, MmePacket->ODA, SLAC_EVSE_MAC_LENGTH) == FALSE)
  3110. {
  3111. SAVE_SYS_LOG_MSG_EVCOMM("[SLAC][Rx][CM_SLAC_MATCH_REQ]wrong ODA: ignore");
  3112. //break; //patch for DEKRA I2SE EVCC (another EVSE MAC Address protection comes as below.)
  3113. }
  3114. EvMac_in = &MmePacket->OSA[0];
  3115. RunID_in = &MmePacket->MMENTRY[50];
  3116. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  3117. if (idx >= 0)
  3118. {
  3119. Update_V2G_Flow_Status(CM_SLAC_MATCH_REQ);
  3120. SLAC_INFO.array[idx].MatchReqNum++;
  3121. sprintf(buf_log_evcomm,
  3122. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:%d-th",
  3123. (idx + 1),
  3124. SLAC_INFO.array[idx].MatchReqNum);
  3125. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3126. //[TC_SECC_VTB_CmSlacMatch_007] APPLICATION_TYPE must be 0x00(EV-EVSE Matching)
  3127. //[TC_SECC_VTB_CmSlacMatch_008]
  3128. if (MmePacket->MMENTRY[0] != 0)
  3129. {
  3130. sprintf(buf_log_evcomm,
  3131. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:wrong APPLICATION_TYPE(%d)",
  3132. (idx + 1),
  3133. MmePacket->MMENTRY[0]);
  3134. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3135. break;
  3136. }
  3137. //[TC_SECC_VTB_CmSlacMatch_009] SECURITY_TYPE must be 0x00(No Security)
  3138. //[TC_SECC_VTB_CmSlacMatch_010]
  3139. if (MmePacket->MMENTRY[1] != 0)
  3140. {
  3141. sprintf(buf_log_evcomm,
  3142. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:wrong SECURITY_TYPE(%d)",
  3143. (idx + 1),
  3144. MmePacket->MMENTRY[1]);
  3145. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3146. break;
  3147. }
  3148. //[TC_SECC_VTB_CmSlacMatch_011] MVFLength must be 0x3E(MatchVarField Length)
  3149. //[TC_SECC_VTB_CmSlacMatch_012]
  3150. if (MmePacket->MMENTRY[2] != 0x3E || MmePacket->MMENTRY[3] != 0x00)
  3151. {
  3152. sprintf(buf_log_evcomm,
  3153. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:wrong MVFLength(%d,%d)",
  3154. (idx + 1),
  3155. MmePacket->MMENTRY[2],
  3156. MmePacket->MMENTRY[3]);
  3157. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3158. break;
  3159. }
  3160. //[TC_SECC_VTB_CmSlacMatch_013] EV ID (muxt be all zero)
  3161. //[TC_SECC_VTB_CmSlacMatch_014]
  3162. if (Array_Check_All_Zero(&MmePacket->MMENTRY[4], 17) == FALSE)
  3163. {
  3164. sprintf(buf_log_evcomm,
  3165. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EV ID",
  3166. (idx + 1));
  3167. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3168. break;
  3169. }
  3170. //[TC_SECC_VTB_CmSlacMatch_015] EV MAC
  3171. //[TC_SECC_VTB_CmSlacMatch_016]
  3172. if (Array_Compare_Identity(EvMac_in, &MmePacket->MMENTRY[21], SLAC_EVMAC_LENGTH) == FALSE)
  3173. {
  3174. sprintf(buf_log_evcomm,
  3175. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EV MAC(%02X:%02X:%02X:%02X:%02X:%02X)",
  3176. (idx + 1),
  3177. MmePacket->MMENTRY[21], MmePacket->MMENTRY[22], MmePacket->MMENTRY[23],
  3178. MmePacket->MMENTRY[24], MmePacket->MMENTRY[25], MmePacket->MMENTRY[26]);
  3179. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3180. break;
  3181. }
  3182. //[TC_SECC_VTB_CmSlacMatch_017] EVSE ID should be all zero
  3183. //[TC_SECC_VTB_CmSlacMatch_018]
  3184. if (Array_Check_All_Zero(&MmePacket->MMENTRY[27], 17) == FALSE)
  3185. {
  3186. sprintf(buf_log_evcomm,
  3187. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EVSE ID",
  3188. (idx + 1));
  3189. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3190. break;
  3191. }
  3192. //[TC_SECC_VTB_CmSlacMatch_019] EVSE MAC
  3193. //[TC_SECC_VTB_CmSlacMatch_020]
  3194. if (Array_Compare_Identity(CsuMac, &MmePacket->MMENTRY[44], SLAC_EVSE_MAC_LENGTH) == FALSE)
  3195. {
  3196. sprintf(buf_log_evcomm,
  3197. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EVSE MAC(%02X:%02X:%02X:%02X:%02X:%02X)",
  3198. (idx + 1),
  3199. MmePacket->MMENTRY[44], MmePacket->MMENTRY[45], MmePacket->MMENTRY[46],
  3200. MmePacket->MMENTRY[47], MmePacket->MMENTRY[48], MmePacket->MMENTRY[49]);
  3201. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3202. break;
  3203. }
  3204. //[TC_SECC_VTB_CmSlacMatch_021]runID: Check RunID
  3205. //[TC_SECC_VTB_CmSlacMatch_022]
  3206. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE &&
  3207. (idx == 0)) //only setup a successful link establishment with the first SLAC Req instance
  3208. {
  3209. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  3210. memcpy(SendMmePacket.ODA, MmePacket->OSA, 6);
  3211. memcpy(SendMmePacket.OSA, CsuMac, 6);
  3212. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  3213. SendMmePacket.MMV = MmePacket->MMV;
  3214. SendMmePacket.MMTYPE = MMTYPE_CM_SLAC_MATCH_CNF;
  3215. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  3216. SendMmePacketSize = 0;
  3217. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //APPLICATION_TYPE: Fixed value (0x00: EV-EVSE matching)
  3218. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //SECURITY_TYPE: Fixed value (0x00: No Security)
  3219. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x56; //MVFLength: MatchVarField Length (Fixed value: 0x0056)
  3220. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //MVFLength: MatchVarField Length (Fixed value: 0x0056)
  3221. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //EV ID
  3222. SendMmePacketSize += 17;
  3223. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac_in, SLAC_EVMAC_LENGTH); //EV MAC
  3224. SendMmePacketSize += 6;
  3225. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //EVSE ID
  3226. SendMmePacketSize += 17;
  3227. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, CsuMac, 6); //EVSE MAC
  3228. SendMmePacketSize += 6;
  3229. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, RunID_in, SLAC_RUNID_LENGTH);
  3230. SendMmePacketSize += SLAC_RUNID_LENGTH;
  3231. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 8); //RSVD
  3232. SendMmePacketSize += 8;
  3233. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, Nid, sizeof(Nid)); //NID: Network ID given by the CCo(EVSE)
  3234. SendMmePacketSize += sizeof(Nid); //NID caculated from the random NMK that will be set.
  3235. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //RSVD
  3236. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, NewNmkKey, sizeof(NewNmkKey)); //NMK: Random value
  3237. SendMmePacketSize += sizeof(NewNmkKey); //NMK: Private NMK of the EVSE (random value)
  3238. SendMmePacketSize += 19; //the size before MMENTRY
  3239. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  3240. {
  3241. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response MME Packet *****\n");
  3242. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3243. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  3244. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3245. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  3246. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  3247. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  3248. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  3249. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  3250. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_MATCH_CNF ---\n");
  3251. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  3252. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  3253. DEBUG_PRINTF_EVCOMM_DETAIL("MVFLength: 0x%x, 0x%x\n", SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3]);
  3254. DEBUG_PRINTF_EVCOMM_DETAIL("PEV ID: \n");
  3255. for(Rtn=0; Rtn<17; Rtn++)
  3256. {
  3257. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[4+Rtn]);
  3258. }
  3259. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3260. DEBUG_PRINTF_EVCOMM_DETAIL("PEV MAC: \n");
  3261. for(Rtn=0; Rtn<6; Rtn++)
  3262. {
  3263. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[21+Rtn]);
  3264. }
  3265. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3266. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE ID: \n");
  3267. for(Rtn=0; Rtn<17; Rtn++)
  3268. {
  3269. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[27+Rtn]);
  3270. }
  3271. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3272. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE MAC: \n");
  3273. for(Rtn=0; Rtn<6; Rtn++)
  3274. {
  3275. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[44+Rtn]);
  3276. }
  3277. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3278. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: \n");
  3279. for(Rtn=0; Rtn<8; Rtn++)
  3280. {
  3281. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[50+Rtn]);
  3282. }
  3283. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3284. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: \n");
  3285. for(Rtn=0; Rtn<8; Rtn++)
  3286. {
  3287. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[58+Rtn]);
  3288. }
  3289. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3290. DEBUG_PRINTF_EVCOMM_DETAIL("NID: \n");
  3291. for(Rtn=0; Rtn<7; Rtn++)
  3292. {
  3293. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[66+Rtn]);
  3294. }
  3295. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3296. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: 0x%x\n", SendMmePacket.MMENTRY[73]);
  3297. DEBUG_PRINTF_EVCOMM_DETAIL("NMK: \n");
  3298. for(Rtn=0; Rtn<16; Rtn++)
  3299. {
  3300. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[74+Rtn]);
  3301. }
  3302. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3303. }
  3304. #endif
  3305. Update_V2G_Flow_Status(CM_SLAC_MATCH_CNF);
  3306. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3307. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  3308. sprintf(buf_log_evcomm,
  3309. "[SLAC][Tx]CM_SLAC_MATCH_CNF[%d]",
  3310. (idx + 1));
  3311. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3312. ftime(&SeqStartTime);
  3313. }
  3314. else
  3315. {
  3316. //RunID does not match and it's not the first SLAC request
  3317. //Reset the SLAC database to embrace SLAC retry
  3318. SAVE_SYS_LOG_MSG_EVCOMM("[CM_SLAC_MATCH_REQ]No Real MATCH_REQ");
  3319. SLAC_DB_Reset();
  3320. }
  3321. }
  3322. else
  3323. {
  3324. //OSA(EvMac) does not exist
  3325. }
  3326. break;
  3327. }
  3328. #else
  3329. {
  3330. Update_V2G_Flow_Status(CM_SLAC_MATCH_REQ);
  3331. SAVE_SYS_LOG_MSG_EVCOMM("[SLAC][Rx]CM_SLAC_MATCH_REQ");
  3332. memset(&SendMmePacket,0,sizeof(struct MmeHeader));
  3333. memcpy(SendMmePacket.ODA,MmePacket->OSA,6);
  3334. memcpy(SendMmePacket.OSA,CsuMac,6);
  3335. SendMmePacket.MTYPE=htons(EtherType_HomePlug);
  3336. SendMmePacket.MMV=MmePacket->MMV;
  3337. SendMmePacket.MMTYPE=MMTYPE_CM_SLAC_MATCH_CNF;
  3338. SendMmePacket.FMI[0]=SendMmePacket.FMI[1]=0;
  3339. SendMmePacketSize=0;
  3340. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //Fixed value (0x00) indicating PEV-EVSE matching
  3341. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //Fixed value (0x00) indicating No Security
  3342. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x56; //Fixed value (0x0056) for matching
  3343. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //Fixed value (0x0056) for matching
  3344. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,17); //PEV ID
  3345. SendMmePacketSize+=17;
  3346. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,EvMac,6);
  3347. SendMmePacketSize+=6;
  3348. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,17); //EVSE ID
  3349. SendMmePacketSize+=17;
  3350. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,CsuMac,6);
  3351. SendMmePacketSize+=6;
  3352. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,SlacRunId,sizeof(SlacRunId));
  3353. SendMmePacketSize+=sizeof(SlacRunId);
  3354. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,8); //RSVD
  3355. SendMmePacketSize+=8;
  3356. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,Nid,sizeof(Nid));
  3357. SendMmePacketSize+=sizeof(Nid);
  3358. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //RSVD
  3359. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,NewNmkKey,sizeof(NewNmkKey));
  3360. SendMmePacketSize+=sizeof(NewNmkKey);
  3361. SendMmePacketSize+=19; //the size before MMENTRY
  3362. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  3363. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response MME Packet *****\n");
  3364. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3365. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  3366. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3367. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  3368. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  3369. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  3370. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  3371. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  3372. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_MATCH_CNF ---\n");
  3373. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  3374. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  3375. DEBUG_PRINTF_EVCOMM_DETAIL("MVFLength: 0x%x, 0x%x\n", SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3]);
  3376. DEBUG_PRINTF_EVCOMM_DETAIL("PEV ID: \n");
  3377. for(Rtn=0; Rtn<17; Rtn++)
  3378. {
  3379. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[4+Rtn]);
  3380. }
  3381. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3382. DEBUG_PRINTF_EVCOMM_DETAIL("PEV MAC: \n");
  3383. for(Rtn=0; Rtn<6; Rtn++)
  3384. {
  3385. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[21+Rtn]);
  3386. }
  3387. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3388. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE ID: \n");
  3389. for(Rtn=0; Rtn<17; Rtn++)
  3390. {
  3391. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[27+Rtn]);
  3392. }
  3393. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3394. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE MAC: \n");
  3395. for(Rtn=0; Rtn<6; Rtn++)
  3396. {
  3397. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[44+Rtn]);
  3398. }
  3399. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3400. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: \n");
  3401. for(Rtn=0; Rtn<8; Rtn++)
  3402. {
  3403. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[50+Rtn]);
  3404. }
  3405. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3406. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: \n");
  3407. for(Rtn=0; Rtn<8; Rtn++)
  3408. {
  3409. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[58+Rtn]);
  3410. }
  3411. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3412. DEBUG_PRINTF_EVCOMM_DETAIL("NID: \n");
  3413. for(Rtn=0; Rtn<7; Rtn++)
  3414. {
  3415. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[66+Rtn]);
  3416. }
  3417. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3418. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: 0x%x\n", SendMmePacket.MMENTRY[73]);
  3419. DEBUG_PRINTF_EVCOMM_DETAIL("NMK: \n");
  3420. for(Rtn=0; Rtn<16; Rtn++)
  3421. {
  3422. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[74+Rtn]);
  3423. }
  3424. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3425. #endif
  3426. Update_V2G_Flow_Status(CM_SLAC_MATCH_CNF);
  3427. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3428. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  3429. SAVE_SYS_LOG_MSG_EVCOMM("[SLAC][Tx]CM_SLAC_MATCH_CNF");
  3430. ftime(&SeqStartTime);
  3431. break;
  3432. }
  3433. #endif
  3434. }
  3435. case MMTYPE_VENDOR_VS_HOST_ACTION:
  3436. {
  3437. struct QcaVendorMmeHeader *RecvPacket;
  3438. RecvPacket = (struct QcaVendorMmeHeader *)Buffer;
  3439. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_VS_HOST_ACTION ---\n");
  3440. switch (RecvPacket->MBODY[0])
  3441. {
  3442. case 0x00:
  3443. //Loader (Device Softloader or Bootloader) ready
  3444. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Loader Ready\n");
  3445. break;
  3446. case 0x01:
  3447. //Firmware Upgrade Ready
  3448. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Firmware Upgrade Ready\n");
  3449. break;
  3450. case 0x02:
  3451. //PIB Update Ready
  3452. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: PIB Update Ready\n");
  3453. break;
  3454. case 0x03:
  3455. //Firmware Upgrade and PIB Update ready
  3456. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Firmware Upgrade and PIB Update ready\n");
  3457. break;
  3458. case 0x04:
  3459. //Loader (Bootloader) ready to receive SDRAM configuration.
  3460. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Loader ready to receive SDRAM configuration\n");
  3461. break;
  3462. case 0x05:
  3463. //Reset to Factory Defaults.
  3464. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Reset to Factory Defaults\n");
  3465. break;
  3466. default:
  3467. //Reserved
  3468. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Reserved\n");
  3469. break;
  3470. }
  3471. break;
  3472. }
  3473. case MMTYPE_VENDOR_ATTEN_CHAR:
  3474. {
  3475. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_ATTEN_CHAR ---\n");
  3476. break;
  3477. }
  3478. case MMTYPE_VENDOR_VS_NW_INFO_CNF:
  3479. {
  3480. struct QcaVendorMmeHeader *RecvPacket;
  3481. RecvPacket = (struct QcaVendorMmeHeader *)Buffer;
  3482. memcpy(QcaMac, RecvPacket->OSA, 6);
  3483. sprintf(buf_log_evcomm,
  3484. "[QCA7K][Rx][VENDOR_VS_NW_INFO_CNF]Got QCA7K MacAddr:%02X:%02X:%02X:%02X:%02X:%02X (comm:OK)",
  3485. QcaMac[0], QcaMac[1], QcaMac[2], QcaMac[3], QcaMac[4], QcaMac[5]);
  3486. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3487. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_VS_NW_INFO_CNF ---\n");
  3488. DEBUG_PRINTF_EVCOMM_DETAIL("QcaMac: %02x:%02x:%02x:%02x:%02x:%02x\n",
  3489. QcaMac[0],
  3490. QcaMac[1],
  3491. QcaMac[2],
  3492. QcaMac[3],
  3493. QcaMac[4],
  3494. QcaMac[5]);
  3495. Update_V2G_Flow_Status(CM_SET_KEY_REQ);
  3496. ftime(&SeqStartTime);
  3497. break;
  3498. }
  3499. case MMTYPE_VENDOR_VS_PL_LNK_STATUS_CNF:
  3500. {
  3501. struct QcaVendorMmeHeader *RecvPacket;
  3502. RecvPacket = (struct QcaVendorMmeHeader *)Buffer;
  3503. if(RecvPacket->MBODY[1]==0)
  3504. {
  3505. //PLC disconnected
  3506. sprintf(buf_log_evcomm,
  3507. "[QCA7K][Rx][MMTYPE_VENDOR_VS_PL_LNK_STATUS_CNF]Got PLC Link Status:%d",
  3508. RecvPacket->MBODY[1]);
  3509. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3510. Update_V2G_Flow_Status(Other_Fault);
  3511. }
  3512. else
  3513. Update_V2G_Flow_Status(CM_SET_KEY_REQ);
  3514. ftime(&SeqStartTime);
  3515. break;
  3516. }
  3517. default:
  3518. {
  3519. break;
  3520. }
  3521. }
  3522. }
  3523. /*===========================================================================
  3524. FUNCTION: SlacComm
  3525. DESCRIPTION:
  3526. PRE-CONDITION:
  3527. INPUT:
  3528. OUTPUT:
  3529. GLOBAL VARIABLES:
  3530. =============================================================================*/
  3531. int SlacComm()
  3532. {
  3533. static unsigned char qca7k_comm_retry = 0;
  3534. unsigned char *EvMac_in;
  3535. unsigned char *RunID_in;
  3536. double t_diff = 0;
  3537. int packet_size = 0;
  3538. int count = 0;
  3539. int idx = 0;
  3540. int i = 0;
  3541. if(RawSock >= 0)
  3542. {
  3543. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  3544. packet_size = recvfrom(RawSock, V2gtpMsgRxBuf, V2GTP_MSG_RX_BUFFER_SIZE, 0, NULL, NULL);
  3545. if(packet_size > 0)
  3546. {
  3547. /*#ifdef Debug
  3548. DEBUG_PRINTF_EVCOMM_DETAIL("Raw Data: ");
  3549. for(count=0;count<packet_size;count++)
  3550. DEBUG_PRINTF_EVCOMM_DETAIL("0x%x, ",V2gtpMsgRxBuf[count]);
  3551. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3552. #endif*/
  3553. MmeProcess(V2gtpMsgRxBuf, packet_size);
  3554. }
  3555. }
  3556. switch(Check_V2G_Flow_Status())
  3557. {
  3558. case IDLE:
  3559. {
  3560. if(RawSock < 0)
  3561. {
  3562. RawSock = socket(PF_PACKET, SOCK_RAW, htons(EtherType_HomePlug));
  3563. sprintf(buf_log_evcomm, "[RawSock]opened(%d)", RawSock);
  3564. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3565. if(RawSock == -1)
  3566. {
  3567. SAVE_SYS_LOG_MSG_EVCOMM("SlacComm:Failed to create socket");
  3568. Update_V2G_Flow_Status(Other_Fault);
  3569. return -1;
  3570. }
  3571. if (setsockopt(RawSock, SOL_SOCKET, SO_BINDTODEVICE, QcaInterface, 4) < 0)
  3572. {
  3573. SAVE_SYS_LOG_MSG_EVCOMM("SlacComm:Set SO_BINDTODEVICE NG");
  3574. Update_V2G_Flow_Status(Other_Fault);
  3575. return -1;
  3576. }
  3577. struct timeval tv;
  3578. tv.tv_sec = 0;
  3579. tv.tv_usec = 100000; //100ms (Rx timeout)
  3580. if (setsockopt(RawSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  3581. {
  3582. SAVE_SYS_LOG_MSG_EVCOMM("SlacComm:Set SO_RCVTIMEO NG");
  3583. Update_V2G_Flow_Status(Other_Fault);
  3584. return -1;
  3585. }
  3586. tv.tv_usec = 100000; //100ms (Tx timeout)
  3587. if (setsockopt(RawSock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  3588. {
  3589. SAVE_SYS_LOG_MSG_EVCOMM("SlacComm:Set SO_SNDTIMEO NG");
  3590. Update_V2G_Flow_Status(Other_Fault);
  3591. return -1;
  3592. }
  3593. memset(&Req, 0, sizeof(struct ifreq));
  3594. strcpy( (char*)Req.ifr_name, QcaInterface);
  3595. if (ioctl(RawSock, SIOCGIFINDEX, &Req) < 0)
  3596. {
  3597. SAVE_SYS_LOG_MSG_EVCOMM("SlacComm: ioctl NG");
  3598. Update_V2G_Flow_Status(Other_Fault);
  3599. return -1;
  3600. }
  3601. memset( &DestSocketAddress, 0, sizeof(struct sockaddr_ll));
  3602. DestSocketAddress.sll_ifindex = Req.ifr_ifindex;
  3603. DestSocketAddress.sll_halen = ETH_ALEN;
  3604. PwmStartTime = 0;
  3605. EVCOMM_SYS_INFO.QCA7K_SetKeyDone = FALSE;
  3606. SAVE_SYS_LOG_MSG_EVCOMM("[QCA7K]connecting...");
  3607. //Get QCA7K MAC address
  3608. GetQca7kMac();
  3609. ftime(&SeqStartTime);
  3610. break;
  3611. }
  3612. else //RawSock: opened
  3613. {
  3614. if(EVCOMM_SYS_INFO.QCA7K_SetKeyDone == FALSE)
  3615. {
  3616. ftime(&SeqEndTime);
  3617. t_diff = DiffTimeb(SeqStartTime, SeqEndTime);
  3618. if (t_diff > V2G_SECC_QCA7000_GET_MAC_ADDR_REQ_RETRY_PERIOD) //3 secs
  3619. {
  3620. qca7k_comm_retry++;
  3621. sprintf(buf_log_evcomm,
  3622. "[QCA7K]re-try connecting...(%.02lf/%dms)",
  3623. t_diff,
  3624. V2G_SECC_QCA7000_GET_MAC_ADDR_REQ_RETRY_PERIOD);
  3625. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3626. GetQca7kMac(); //re-send req
  3627. ftime(&SeqStartTime);
  3628. break;
  3629. }
  3630. else
  3631. {
  3632. //null
  3633. }
  3634. //Retry by 3 times
  3635. if (qca7k_comm_retry >= 3)
  3636. {
  3637. sprintf(buf_log_evcomm,
  3638. "[QCA7K][Error]comm: fail (retry by %d times)",
  3639. qca7k_comm_retry);
  3640. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3641. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3642. //CCS_SECCC_TIMEOUT_QCA7000_COMM (023892): The firmware code of QCA7000 may not be installed, yet
  3643. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3644. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3645. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3646. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3647. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  3648. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  3649. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3650. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3651. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  3652. Update_V2G_Flow_Status(Sequence_Timeout);
  3653. qca7k_comm_retry = 0;
  3654. break;
  3655. }
  3656. }
  3657. else //RawSock: opened; Set Key: DONE
  3658. {
  3659. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  3660. if((CheckConnectorPlugIn() == TRUE) && (ShmInternalComm->ChargingPermission >=1))
  3661. {
  3662. if(PwmStartTime <= 0)
  3663. {
  3664. //Sniffer_Tcpdump(ENABLE);
  3665. //#if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  3666. //sleep(1); //wait for tcpdump to be ready.
  3667. //#endif
  3668. SwitchCpStateE(DISABLE);
  3669. OutputCpPwmDuty(5);
  3670. PwmStartTime = time(NULL);
  3671. }
  3672. else
  3673. {
  3674. if((time(NULL) - PwmStartTime) > TT_EVSE_SLAC_init)
  3675. {
  3676. sprintf(buf_log_evcomm,
  3677. "SlacComm: Wait CM_SLAC_PARM_REQ Timeout - TT_EVSE_SLAC_init, NowTime(%d)-PwmStartTime(%d)>%d (sec)",
  3678. time(NULL),
  3679. PwmStartTime,
  3680. TT_EVSE_SLAC_init);
  3681. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3682. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3683. //CCS_SECC_TIMEOUT_SLAC_TT_EVSE_SLAC_init (023809)
  3684. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3685. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3686. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3687. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3688. ShmStatusCodeData->PresentStatusCode[0][4] = 0;
  3689. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  3690. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  3691. Update_V2G_Flow_Status(Sequence_Timeout);
  3692. PwmStartTime = 0;
  3693. return -1;
  3694. }
  3695. else
  3696. {
  3697. //waiting for CM_SLAC_PARM_REQ
  3698. }
  3699. }
  3700. }
  3701. else
  3702. {
  3703. PwmStartTime = 0;
  3704. }
  3705. }
  3706. }
  3707. break;
  3708. }
  3709. case CM_SET_KEY_REQ: //13
  3710. {
  3711. //CM_SET_KEY_REQ
  3712. //SAVE_SYS_LOG_MSG_EVCOMM("[QCA7K][Rx]CM_SET_KEY_REQ");
  3713. ftime(&SeqEndTime);
  3714. t_diff = DiffTimeb(SeqStartTime, SeqEndTime);
  3715. if (t_diff > V2G_SECC_QCA7000_COMM_TIMEOUT) //10 seconds
  3716. {
  3717. sprintf(buf_log_evcomm,
  3718. "[QCA7K]Failed on SetKey => End_Process (%.02lf/%dms)",
  3719. t_diff,
  3720. V2G_SECC_QCA7000_COMM_TIMEOUT);
  3721. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3722. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3723. //CCS_SECCC_TIMEOUT_QCA7000_COMM (023892): The firmware code of QCA7000 may not be installed, yet
  3724. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3725. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3726. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3727. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3728. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  3729. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  3730. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3731. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3732. Update_V2G_Flow_Status(Sequence_Timeout);
  3733. }
  3734. else if (t_diff > V2G_SECC_QCA7000_SEND_SET_KEY_PERIOD) //2 seconds
  3735. {
  3736. sprintf(buf_log_evcomm,
  3737. "[QCA7K]SetKey: proceed (%.02lf/%dms)",
  3738. t_diff,
  3739. V2G_SECC_QCA7000_SEND_SET_KEY_PERIOD);
  3740. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3741. SendSetKey();
  3742. ftime(&SeqStartTime);
  3743. }
  3744. else
  3745. {
  3746. //null
  3747. }
  3748. break;
  3749. }
  3750. case CM_SET_KEY_CNF: //14
  3751. {
  3752. sprintf(buf_log_evcomm,
  3753. "[SECCReady]Wait: plugin(%d), matached(%d), permission(%d)...",
  3754. CheckConnectorPlugIn(),
  3755. CSUCOMMDC_TASK_FLAG.matched,
  3756. ShmInternalComm->ChargingPermission
  3757. );
  3758. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3759. EVCOMM_SYS_INFO.QCA7K_SetKeyDone = TRUE;
  3760. PwmStartTime = 0;
  3761. Update_V2G_Flow_Status(IDLE);
  3762. break;
  3763. }
  3764. case CM_SLAC_PARM_CONF:
  3765. {
  3766. ftime(&SeqEndTime);
  3767. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_sequence)
  3768. {
  3769. sprintf(buf_log_evcomm,
  3770. "SlacComm: Wait CM_START_ATTEN_CHAR_IND Timeout - TT_match_sequence (%.02lf of %dms)",
  3771. DiffTimeb(SeqStartTime, SeqEndTime),
  3772. TT_match_sequence);
  3773. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3774. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3775. //CCS_SECC_TIMEOUT_CM_START_ATTEN_CHAR_IND (023811):
  3776. //[Possible Reason] CCS Board image might not be updated properly. The 2nd reboot process is not finished.
  3777. //[Verification Method] By tying reboot command to the terminal and see if it could be executed immediately.
  3778. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3779. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3780. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3781. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3782. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  3783. ShmStatusCodeData->PresentStatusCode[0][5] = 1;
  3784. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3785. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3786. Update_V2G_Flow_Status(Sequence_Timeout);
  3787. return -1;
  3788. }
  3789. break;
  3790. }
  3791. case CM_START_ATTEN_CHAR_IND:
  3792. {
  3793. ftime(&SeqEndTime);
  3794. if(DiffTimeb(SeqStartTime, SeqEndTime) > (TP_EV_batch_msg_interval)) //one more time interval for tolerance
  3795. {
  3796. sprintf(buf_log_evcomm,
  3797. "SlacComm: Wait CM_MNBC_SOUND_IND Timeout - TP_EV_batch_msg_interval (%.02lf of %dms)",
  3798. DiffTimeb(SeqStartTime, SeqEndTime),
  3799. TP_EV_batch_msg_interval);
  3800. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3801. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3802. //CCS_SECC_TIMEOUT_SLAC_CM_MNBC_SOUND_IND (023818):
  3803. //[Possible Reason] CCS Board image might not be updated properly. The 2nd reboot process is not finished.
  3804. //[Verification Method] By tying reboot command to the terminal and see if it could be executed immediately.
  3805. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3806. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3807. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3808. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3809. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  3810. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  3811. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3812. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3813. Update_V2G_Flow_Status(Sequence_Timeout);
  3814. return -1;
  3815. }
  3816. break;
  3817. }
  3818. case CM_MNBC_SOUND_IND:
  3819. {
  3820. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  3821. {
  3822. ftime(&SeqEndTime);
  3823. t_diff = DiffTimeb(SeqStartTime, SeqEndTime);
  3824. //printf("time:%.02lf, profilNum:%d\n",DiffTimeb(SeqStartTime, SeqEndTime),AttenProfileCnt); //added by Vern
  3825. if(t_diff > TT_EVSE_match_MNBC ||
  3826. (SLAC_INFO.array[0].AttenProfileCnt >= SLAC_INFO.array[0].MnbcSoundNum) ||
  3827. (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0)) //TC_SECC_VTB_AttenuationCharacterization_003
  3828. {
  3829. //Wait for other SLAC Req sets
  3830. if ((SLAC_INFO.arrayLen >= 2) &&
  3831. (t_diff < TT_EVSE_match_MNBC) &&
  3832. (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry == 0)) //not a retry
  3833. {
  3834. break;
  3835. }
  3836. //Check if it is a timeup response
  3837. if (t_diff > TT_EVSE_match_MNBC)
  3838. {
  3839. sprintf(buf_log_evcomm,
  3840. "[WARNING][SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: timeup(%.2f/%dms) => send",
  3841. (i + 1),
  3842. t_diff,
  3843. TT_EVSE_match_MNBC);
  3844. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3845. }
  3846. //Sending all CM_ATTEN_CHAR_IND according to all corresponding SLAC Req sets
  3847. for (i = 0; i < SLAC_INFO.arrayLen; i++)
  3848. {
  3849. if ((SLAC_INFO.array[i].AttenProfileCnt == 0) ||
  3850. (SLAC_INFO.array[i].AagGroupsNum == 0) ||
  3851. (SLAC_INFO.array[i].StartAttenCharCnt == 0) ||
  3852. (SLAC_INFO.array[i].MnbcSoundNum != 10) || //received in CM_START_ATTEN_CHAR_IND
  3853. (SLAC_INFO.array[i].StartAttenCharErr == TRUE)
  3854. )
  3855. {
  3856. //Ignoring those SLAC request sets without sending CM_MNBC_SOUND_IND(CM_ATTEN_PROFILE_IND)
  3857. sprintf(buf_log_evcomm,
  3858. "[WARNING][SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: para err(%d,%d,%d,%d,%d) => canceled",
  3859. (i + 1),
  3860. SLAC_INFO.array[i].AttenProfileCnt,
  3861. SLAC_INFO.array[i].AagGroupsNum,
  3862. SLAC_INFO.array[i].StartAttenCharCnt,
  3863. SLAC_INFO.array[i].MnbcSoundNum,
  3864. SLAC_INFO.array[i].StartAttenCharErr);
  3865. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3866. continue;
  3867. }
  3868. //In CM_ATTEN_CHAR_IND retry process, here only re-send this message according to the 1st coming SLAC request
  3869. if ((EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0) && (i != 0))
  3870. {
  3871. sprintf(buf_log_evcomm,
  3872. "[NOTE][SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: canceled",
  3873. (i + 1));
  3874. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3875. break;
  3876. }
  3877. EvMac_in = SLAC_INFO.array[i].EvMac;
  3878. RunID_in = SLAC_INFO.array[i].RunID;
  3879. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  3880. memcpy(SendMmePacket.ODA, EvMac_in, SLAC_EVMAC_LENGTH);
  3881. memcpy(SendMmePacket.OSA, CsuMac, 6);
  3882. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  3883. SendMmePacket.MMV = 0x01;
  3884. SendMmePacket.MMTYPE = MMTYPE_CM_ATTEN_CHAR_IND;
  3885. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  3886. SendMmePacketSize = 0;
  3887. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00: EV-EVSE Matching)
  3888. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00: No Security)
  3889. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac_in, SLAC_EVMAC_LENGTH); //SOURCE_ADDRESS, MAC address of the EV Host
  3890. SendMmePacketSize += SLAC_EVMAC_LENGTH;
  3891. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, RunID_in, SLAC_RUNID_LENGTH);
  3892. SendMmePacketSize += SLAC_RUNID_LENGTH;
  3893. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //SOURCE_ID(0x00)
  3894. SendMmePacketSize += 17;
  3895. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //RESP_ID(0x00)
  3896. SendMmePacketSize += 17;
  3897. SendMmePacket.MMENTRY[SendMmePacketSize++] = SLAC_INFO.array[i].AttenProfileCnt; //NumSounds: Number of M-Sounds used for generation of the ATTEN_PROFILE
  3898. SendMmePacket.MMENTRY[SendMmePacketSize++] = SLAC_INFO.array[i].AagGroupsNum; //NumGroups
  3899. for(count = 0; count < SLAC_INFO.array[i].AagGroupsNum; count++)
  3900. {
  3901. unsigned char TmpAag;
  3902. TmpAag = ((SLAC_INFO.array[i].AAG[count] / SLAC_INFO.array[i].AttenProfileCnt) & 0xFF);
  3903. SLAC_INFO.array[i].AAG_quality_ori += (float) TmpAag; //original signal quality
  3904. #if (SUDO_PSD_PARAMETER_MECHANISM == ENABLE) //default: ENABLE
  3905. #if 1
  3906. //TC_SECC_VTB_AttenuationCharacterization_019
  3907. TmpAag = TmpAag >> 1; //new method proposed by Joseph (divided by 2)
  3908. #else
  3909. if(TmpAag >= 39) //original method proposed by Vern
  3910. {
  3911. TmpAag = 37;
  3912. }
  3913. #endif
  3914. #endif
  3915. SendMmePacket.MMENTRY[SendMmePacketSize++] = TmpAag;
  3916. SLAC_INFO.array[i].AAG_quality_refined += (float) TmpAag; //refined signal quality
  3917. }
  3918. SendMmePacketSize += 19; //the size before MMENTRY
  3919. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == TRUE)
  3920. {
  3921. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Send MME Packet *****\n");
  3922. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3923. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  3924. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3925. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  3926. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  3927. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  3928. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  3929. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  3930. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_ATTEN_CHAR_IND ---\n");
  3931. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  3932. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  3933. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
  3934. SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5],
  3935. SendMmePacket.MMENTRY[6],SendMmePacket.MMENTRY[7]);
  3936. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  3937. SendMmePacket.MMENTRY[8],SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],
  3938. SendMmePacket.MMENTRY[12],SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14],SendMmePacket.MMENTRY[15]);
  3939. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ID: \n");
  3940. for(count=0; count<17; count++)
  3941. {
  3942. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[16+count]);
  3943. }
  3944. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3945. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_ID: \n");
  3946. for(count=0; count<17; count++)
  3947. {
  3948. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[33+count]);
  3949. }
  3950. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3951. DEBUG_PRINTF_EVCOMM_DETAIL("NumSounds: 0x%x\n", SendMmePacket.MMENTRY[50]);
  3952. DEBUG_PRINTF_EVCOMM_DETAIL("ATTEN_PROFILE: \n");
  3953. for(count=0; count<AagGroupsNum; count++)
  3954. {
  3955. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[51+count]);
  3956. }
  3957. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3958. }
  3959. #endif
  3960. count = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3961. SLAC_INFO.array[i].AAG_quality_ori /= SLAC_INFO.array[i].AagGroupsNum;
  3962. SLAC_INFO.array[i].AAG_quality_refined /= SLAC_INFO.array[i].AagGroupsNum;
  3963. //Print log if this CM_ATTEN_CHAR_IND belongs to RETRY Tx message.
  3964. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0)
  3965. {
  3966. sprintf(buf_log_evcomm,
  3967. "[SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: %d-th resend (Q=%.2f/%.2f)(%d/%d)",
  3968. (i + 1),
  3969. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry,
  3970. SLAC_INFO.array[i].AAG_quality_refined,
  3971. SLAC_INFO.array[i].AAG_quality_ori,
  3972. SLAC_INFO.array[i].AttenProfileCnt,
  3973. SLAC_INFO.array[i].MnbcSoundNum);
  3974. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3975. }
  3976. else if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry == 0)
  3977. {
  3978. sprintf(buf_log_evcomm,
  3979. "[SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: Q=%.2f/%.2f(%d/%d)",
  3980. (i + 1),
  3981. SLAC_INFO.array[i].AAG_quality_refined,
  3982. SLAC_INFO.array[i].AAG_quality_ori,
  3983. SLAC_INFO.array[i].AttenProfileCnt,
  3984. SLAC_INFO.array[i].MnbcSoundNum);
  3985. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3986. }
  3987. else
  3988. {
  3989. sprintf(buf_log_evcomm,
  3990. "[SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: unexpected CM_ATTEN_CHAR_IND_retry(%d))",
  3991. i,
  3992. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry);
  3993. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  3994. }
  3995. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n", SendMmePacketSize,count);
  3996. } //end of for loop
  3997. Update_V2G_Flow_Status(CM_ATTEN_CHAR_IND);
  3998. ftime(&SeqStartTime);
  3999. }
  4000. break;
  4001. }
  4002. #else
  4003. {
  4004. ftime(&SeqEndTime);
  4005. //printf("time:%.02lf, profilNum:%d\n",DiffTimeb(SeqStartTime, SeqEndTime),AttenProfileCnt); //added by Vern
  4006. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_EVSE_match_MNBC || (AttenProfileCnt >= MnbcSoundNum) || (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0))
  4007. {
  4008. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  4009. memcpy(SendMmePacket.ODA, EvMac, 6);
  4010. memcpy(SendMmePacket.OSA, CsuMac, 6);
  4011. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  4012. SendMmePacket.MMV = 0x01;
  4013. SendMmePacket.MMTYPE = MMTYPE_CM_ATTEN_CHAR_IND;
  4014. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  4015. SendMmePacketSize = 0;
  4016. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00: EV-EVSE Matching)
  4017. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00: No Security)
  4018. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac, 6); //SOURCE_ADDRESS, MAC address of the EV Host
  4019. SendMmePacketSize += 6;
  4020. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize, SlacRunId, sizeof(SlacRunId));
  4021. SendMmePacketSize += sizeof(SlacRunId);
  4022. memset(SendMmePacket.MMENTRY+SendMmePacketSize, 0, 17); //SOURCE_ID
  4023. SendMmePacketSize += 17;
  4024. memset(SendMmePacket.MMENTRY+SendMmePacketSize, 0, 17); //RESP_ID
  4025. SendMmePacketSize += 17;
  4026. SendMmePacket.MMENTRY[SendMmePacketSize++] = AttenProfileCnt; //NumSounds
  4027. SendMmePacket.MMENTRY[SendMmePacketSize++] = AagGroupsNum; //NumGroups
  4028. for(count=0; count < AagGroupsNum; count++)
  4029. {
  4030. unsigned char TmpAag;
  4031. TmpAag = ((Aag[count] / AttenProfileCnt) & 0xFF);
  4032. #if (SUDO_PSD_PARAMETER_MECHANISM == ENABLE) //default: ENABLE
  4033. #if 1
  4034. //TC_SECC_VTB_AttenuationCharacterization_019
  4035. TmpAag = TmpAag >> 1; //new method proposed by Joseph (divided by 2)
  4036. #else
  4037. if(TmpAag >= 39) //original method proposed by Vern
  4038. {
  4039. /*
  4040. // [To-do] If this statement is enabled, SLAC will fail due to timeout.
  4041. {
  4042. unsigned char TmpBuf[64];
  4043. memset(TmpBuf,0,sizeof(TmpBuf));
  4044. sprintf(TmpBuf,"SlacComm: bad Aag[%d]=%d",count,TmpAag);
  4045. SAVE_SYS_LOG_MSG_EVCOMM(TmpBuf);
  4046. }
  4047. */
  4048. printf("%d,", TmpAag);
  4049. TmpAag = 37;
  4050. }
  4051. #endif
  4052. #endif
  4053. SendMmePacket.MMENTRY[SendMmePacketSize++] = TmpAag;
  4054. }
  4055. SendMmePacketSize += 19; //the size before MMENTRY
  4056. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == TRUE)
  4057. {
  4058. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Send MME Packet *****\n");
  4059. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  4060. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  4061. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  4062. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  4063. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  4064. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  4065. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  4066. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  4067. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_ATTEN_CHAR_IND ---\n");
  4068. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  4069. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  4070. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
  4071. SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5],
  4072. SendMmePacket.MMENTRY[6],SendMmePacket.MMENTRY[7]);
  4073. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  4074. SendMmePacket.MMENTRY[8],SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],
  4075. SendMmePacket.MMENTRY[12],SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14],SendMmePacket.MMENTRY[15]);
  4076. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ID: \n");
  4077. for(count=0; count<17; count++)
  4078. {
  4079. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[16+count]);
  4080. }
  4081. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  4082. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_ID: \n");
  4083. for(count=0; count<17; count++)
  4084. {
  4085. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[33+count]);
  4086. }
  4087. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  4088. DEBUG_PRINTF_EVCOMM_DETAIL("NumSounds: 0x%x\n", SendMmePacket.MMENTRY[50]);
  4089. DEBUG_PRINTF_EVCOMM_DETAIL("ATTEN_PROFILE: \n");
  4090. for(count=0; count<AagGroupsNum; count++)
  4091. {
  4092. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[51+count]);
  4093. }
  4094. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  4095. }
  4096. #endif
  4097. Update_V2G_Flow_Status(CM_ATTEN_CHAR_IND);
  4098. count = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  4099. //Print log if this CM_ATTEN_CHAR_IND belongs to RETRY Tx message.
  4100. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0)
  4101. {
  4102. sprintf(buf_log_evcomm,
  4103. "[SLAC][Tx]CM_ATTEN_CHAR_IND: resend(%d-th)",
  4104. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry);
  4105. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4106. }
  4107. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n", SendMmePacketSize,count);
  4108. ftime(&SeqStartTime);
  4109. }
  4110. break;
  4111. }
  4112. #endif
  4113. }
  4114. case CM_ATTEN_CHAR_IND:
  4115. {
  4116. ftime(&SeqEndTime);
  4117. //if(DiffTimeb(SeqStartTime, SeqEndTime)>TT_match_response)
  4118. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_response) //extended to 400ms due to the response of CM_ATTEN_CHAR.RSP of some EVCC is slower than 200ms.
  4119. {
  4120. sprintf(buf_log_evcomm,
  4121. "SlacComm: Wait CM_ATTEN_CHAR_RSP Timeout - TT_match_response (%.02lf of %dms)",
  4122. DiffTimeb(SeqStartTime, SeqEndTime),
  4123. TT_match_response);
  4124. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4125. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4126. //CCS_SECC_TIMEOUT_SLAC_CM_ATTEN_CHAR_RSP (023814):
  4127. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  4128. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4129. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4130. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4131. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  4132. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  4133. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  4134. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  4135. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4136. Update_V2G_Flow_Status(Sequence_Timeout);
  4137. return -1;
  4138. }
  4139. break;
  4140. }
  4141. case CM_ATTEN_CHAR_RSP:
  4142. {
  4143. ftime(&SeqEndTime);
  4144. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_EVSE_match_session)
  4145. {
  4146. sprintf(buf_log_evcomm,
  4147. "SlacComm: Wait CM_VALIDATE_REQ_1ST or CM_SLAC_MATCH_REQ Timeout - TT_EVSE_match_session (%.02lf of %dms)",
  4148. DiffTimeb(SeqStartTime, SeqEndTime),
  4149. TT_EVSE_match_session);
  4150. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4151. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4152. //CCS_SECC_TIMEOUT_SLAC_CM_VALIDATE_REQ_1ST__CM_SLAC_MATCH_REQ (023815):
  4153. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  4154. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4155. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4156. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4157. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  4158. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  4159. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  4160. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  4161. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4162. Update_V2G_Flow_Status(Sequence_Timeout);
  4163. return -1;
  4164. }
  4165. break;
  4166. }
  4167. case CM_VALIDATE_CNF:
  4168. {
  4169. ftime(&SeqEndTime);
  4170. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_sequence)
  4171. {
  4172. sprintf(buf_log_evcomm,
  4173. "SlacComm: Wait CM_VALIDATE_REQ_2ND or CM_SLAC_MATCH_REQ Timeout - TT_match_sequence (%.02lf of %dms)",
  4174. DiffTimeb(SeqStartTime, SeqEndTime),
  4175. TT_match_sequence);
  4176. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4177. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4178. //CCS_SECC_TIMEOUT_SLAC_CM_VALIDATE_REQ_2ND__CM_SLAC_MATCH_REQ (023819):
  4179. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  4180. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4181. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4182. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4183. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  4184. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  4185. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  4186. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  4187. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4188. Update_V2G_Flow_Status(Sequence_Timeout);
  4189. return -1;
  4190. }
  4191. break;
  4192. }
  4193. case CM_SLAC_MATCH_CNF:
  4194. {
  4195. if(UdpSock > 0)
  4196. {
  4197. close(UdpSock);
  4198. UdpSock = -1;
  4199. }
  4200. if(TcpSock > 0)
  4201. {
  4202. close(TcpSock);
  4203. TcpSock = -1;
  4204. }
  4205. ftime(&SeqStartTime);
  4206. V2gTcpConnected();
  4207. Update_V2G_Flow_Status(SLACC_SDP_UDP_Connection);
  4208. SAVE_SYS_LOG_MSG_EVCOMM("SLAAC,SDP,UDP: connecting...");
  4209. break;
  4210. }
  4211. defaudlt:
  4212. {
  4213. break;
  4214. }
  4215. }
  4216. return 0;
  4217. }
  4218. /*===========================================================================
  4219. FUNCTION: V2gMsgDecoder
  4220. DESCRIPTION:
  4221. 1. Decode the V2GTP messages inside "msg" and save the decoded
  4222. messages in ccs_exi_doc_DIN, ccs_exi_doc_ISO1, and ccs_exi_doc_ISO2,
  4223. respectively.
  4224. 2. After decoding, V2gMsg_Process() could then use
  4225. ccs_exi_doc_DIN, ccs_exi_doc_ISO1, or ccs_exi_doc_ISO2
  4226. to deal with the corresponding Response messages, respectively.
  4227. PRE-CONDITION:
  4228. 1. msg_length > 0
  4229. INPUT:
  4230. 1. msg
  4231. 2. msg_length
  4232. OUTPUT:
  4233. 1. ccs_exi_doc_DIN //global variable
  4234. ccs_exi_doc_ISO1
  4235. ccs_exi_doc_ISO2
  4236. 2. v2g_state //Status Flag
  4237. //indicating the V2gMsg_Process_din to proceed
  4238. the next process.
  4239. 3. return value // < 0: ERROR
  4240. // > 0: Message Type
  4241. GLOBAL VARIABLES:
  4242. =============================================================================*/
  4243. int V2gMsgDecoder(unsigned char *msg, unsigned int msg_length, unsigned int v2g_state)
  4244. {
  4245. int errn = 0;
  4246. //Checking the minimum Header size requirement
  4247. if(msg_length < V2GTP_MSG_HEADER_LENGTH) //The minimum requirement should be 8-byte header.
  4248. {
  4249. errn = -1;
  4250. return errn;
  4251. }
  4252. //Decode the 1st V2GMSG: AppProtocol
  4253. if(v2g_state == SupportedAppProtocolRequest) //17
  4254. {
  4255. if (errn = API_V2GMSG_EXI_Decoder_AppProtocol(msg, msg_length, &ccs_handshake) < 0)
  4256. {
  4257. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR = %d (DEC)]V2gMsgDecoder: SupportedAppProtocolRequest()\n", errn);
  4258. }
  4259. else //decoded successfully.
  4260. {
  4261. //PRINT_XML_DOC_supportedAppProtocolReq(&ccs_handshake);
  4262. }
  4263. }
  4264. //Decode the subsequent V2GMSG (DIN 70121, ISO 15118-2, ISO 15118-20)
  4265. else if(v2g_state > SupportedAppProtocolRequest && v2g_state <= SessionStopResponse)
  4266. {
  4267. //Decoding according to its own protocol
  4268. switch (ShmCcsData->CommProtocol)
  4269. {
  4270. case V2GT_MSG_PROTOCOL_DIN70121: //0
  4271. {
  4272. //DIN
  4273. if(errn = API_V2GMSG_EXI_Decoder_DIN(msg, msg_length, &ccs_exi_doc_DIN) < 0)
  4274. {
  4275. sprintf(buf_log_evcomm,
  4276. "[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_DIN()",
  4277. errn);
  4278. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4279. }
  4280. break;
  4281. }
  4282. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  4283. {
  4284. //ISO1
  4285. if(errn = API_V2GMSG_EXI_Decoder_ISO1(msg, msg_length, &ccs_exi_doc_ISO1) < 0)
  4286. {
  4287. sprintf(buf_log_evcomm,
  4288. "[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_ISO1()",
  4289. errn);
  4290. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4291. }
  4292. break;
  4293. }
  4294. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  4295. {
  4296. //ISO2
  4297. if(errn = API_V2GMSG_EXI_Decoder_ISO2(msg, msg_length, &ccs_exi_doc_ISO2) < 0)
  4298. {
  4299. sprintf(buf_log_evcomm,
  4300. "[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_ISO2()",
  4301. errn);
  4302. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4303. }
  4304. break;
  4305. }
  4306. default:
  4307. break;
  4308. }
  4309. }
  4310. else
  4311. {
  4312. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR = %d (DEC)]V2gMsgDecoder: Unexpected v2g_state\n", errn);
  4313. errn = -1;
  4314. }
  4315. return errn;
  4316. }
  4317. /*===========================================================================
  4318. FUNCTION: encode_din_V2GTP_stream
  4319. DESCRIPTION:
  4320. PRE-CONDITION:
  4321. INPUT:
  4322. OUTPUT:
  4323. GLOBAL VARIABLES:
  4324. =============================================================================*/
  4325. int encode_din_V2GTP_stream(bitstream_t *v2g_tx_stream, struct dinEXIDocument *ccs_exi_doc_DIN)
  4326. {
  4327. int errn = 0;
  4328. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  4329. errn = encode_dinExiDocument(v2g_tx_stream, ccs_exi_doc_DIN);
  4330. if (errn == 0)
  4331. {
  4332. //successfully encoded
  4333. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  4334. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  4335. if (errn != 0)
  4336. {
  4337. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_din_V2GTP_stream]write_v2gtpHeader(): %d (DEC)\n", errn);
  4338. }
  4339. }
  4340. else
  4341. {
  4342. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_din_V2GTP_stream]encode_dinExiDocument(): %d (DEC)\n", errn);
  4343. }
  4344. return errn;
  4345. }
  4346. /*===========================================================================
  4347. FUNCTION: encode_iso1_V2GTP_stream
  4348. DESCRIPTION:
  4349. PRE-CONDITION:
  4350. INPUT:
  4351. OUTPUT:
  4352. GLOBAL VARIABLES:
  4353. =============================================================================*/
  4354. int encode_iso1_V2GTP_stream(bitstream_t *v2g_tx_stream, struct iso1EXIDocument *ccs_exi_doc_ISO1)
  4355. {
  4356. int errn = 0;
  4357. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  4358. errn = encode_iso1ExiDocument(v2g_tx_stream, ccs_exi_doc_ISO1);
  4359. if (errn == 0)
  4360. {
  4361. //successfully encoded
  4362. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  4363. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  4364. if (errn != 0)
  4365. {
  4366. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso1_V2GTP_stream]write_v2gtpHeader(): %d (DEC)\n", errn);
  4367. }
  4368. }
  4369. else
  4370. {
  4371. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso1_V2GTP_stream]encode_iso1ExiDocument(): %d (DEC)\n", errn);
  4372. }
  4373. return errn;
  4374. }
  4375. /*===========================================================================
  4376. FUNCTION: encode_iso2_V2GTP_stream
  4377. DESCRIPTION:
  4378. PRE-CONDITION:
  4379. INPUT:
  4380. OUTPUT:
  4381. GLOBAL VARIABLES:
  4382. =============================================================================*/
  4383. int encode_iso2_V2GTP_stream(bitstream_t *v2g_tx_stream, struct iso2EXIDocument *ccs_exi_doc_ISO2)
  4384. {
  4385. int errn = 0;
  4386. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  4387. errn = encode_iso2ExiDocument(v2g_tx_stream, ccs_exi_doc_ISO2);
  4388. if (errn == 0)
  4389. {
  4390. //successfully encoded
  4391. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  4392. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  4393. if (errn != 0)
  4394. {
  4395. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso2_V2GTP_stream]write_v2gtpHeader(): %d (DEC)\n", errn);
  4396. }
  4397. }
  4398. else
  4399. {
  4400. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso2_V2GTP_stream]encode_iso2ExiDocument(): %d (DEC)\n", errn);
  4401. }
  4402. return errn;
  4403. }
  4404. /*===========================================================================
  4405. FUNCTION: send_encoded_din_V2GTP_Stream
  4406. DESCRIPTION:
  4407. PRE-CONDITION:
  4408. INPUT:
  4409. OUTPUT:
  4410. GLOBAL VARIABLES:
  4411. =============================================================================*/
  4412. int send_encoded_din_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct dinEXIDocument *ccs_exi_doc_DIN)
  4413. {
  4414. int errn = 0;
  4415. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  4416. errn = encode_din_V2GTP_stream(v2g_tx_stream, ccs_exi_doc_DIN);
  4417. // STEP 2: =========== Send Response Packet ===========
  4418. int rtn = 0;
  4419. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  4420. if (rtn == v2g_tx_stream->size)
  4421. {
  4422. /*
  4423. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][send_encoded_din_V2GTP_Stream] Response Message is sent ( %d / %d ). (Bytes, DEC): OK\n",
  4424. rtn, v2g_tx_stream->size);
  4425. */
  4426. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  4427. }
  4428. else if (rtn >= 0)
  4429. {
  4430. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][send_encoded_din_V2GTP_Stream]Imcomplete Tx ( %d / %d ). (Bytes, DEC): FAIL\n",
  4431. rtn, v2g_tx_stream->size);
  4432. }
  4433. else
  4434. {
  4435. errn = rtn;
  4436. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][send_encoded_din_V2GTP_Stream]send(): %d (DEC)\n", errn);
  4437. }
  4438. //STEP 3: ========= Reset V2G MSG Flags ==========
  4439. init_dinBodyType(&ccs_exi_doc_DIN->V2G_Message.Body);
  4440. return errn;
  4441. }
  4442. /*===========================================================================
  4443. FUNCTION: send_encoded_iso1_V2GTP_Stream
  4444. DESCRIPTION:
  4445. PRE-CONDITION:
  4446. INPUT:
  4447. OUTPUT:
  4448. GLOBAL VARIABLES:
  4449. =============================================================================*/
  4450. int send_encoded_iso1_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct iso1EXIDocument *ccs_exi_doc_ISO1)
  4451. {
  4452. int errn = 0;
  4453. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  4454. errn = encode_iso1_V2GTP_stream(v2g_tx_stream, ccs_exi_doc_ISO1);
  4455. // STEP 2: =========== Send Response Packet ===========
  4456. int rtn = 0;
  4457. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  4458. if (rtn == v2g_tx_stream->size)
  4459. {
  4460. /*
  4461. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][send_encoded_iso1_V2GTP_Stream] Response Message is sent ( %d / %d ). (Bytes, DEC): OK\n",
  4462. rtn, v2g_tx_stream->size);
  4463. */
  4464. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  4465. }
  4466. else if (rtn >= 0)
  4467. {
  4468. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][send_encoded_iso1_V2GTP_Stream]Imcomplete Tx ( %d / %d ). (Bytes, DEC): FAIL\n",
  4469. rtn, v2g_tx_stream->size);
  4470. }
  4471. else
  4472. {
  4473. errn = rtn;
  4474. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][send_encoded_iso1_V2GTP_Stream]send(): %d (DEC)\n", errn);
  4475. }
  4476. //STEP 3: ========= Reset V2G MSG Flags ==========
  4477. init_iso1BodyType(&ccs_exi_doc_ISO1->V2G_Message.Body);
  4478. return errn;
  4479. }
  4480. /*===========================================================================
  4481. FUNCTION: send_encoded_iso2_V2GTP_Stream
  4482. DESCRIPTION:
  4483. PRE-CONDITION:
  4484. INPUT:
  4485. OUTPUT:
  4486. GLOBAL VARIABLES:
  4487. =============================================================================*/
  4488. int send_encoded_iso2_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct iso2EXIDocument *ccs_exi_doc_ISO2)
  4489. {
  4490. int errn = 0;
  4491. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  4492. errn = encode_iso2_V2GTP_stream(v2g_tx_stream, ccs_exi_doc_ISO2);
  4493. // STEP 2: =========== Send Response Packet ===========
  4494. int rtn = 0;
  4495. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  4496. if (rtn == v2g_tx_stream->size)
  4497. {
  4498. /*
  4499. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][send_encoded_iso2_V2GTP_Stream] Response Message is sent ( %d / %d ). (Bytes, DEC): OK\n",
  4500. rtn, v2g_tx_stream->size);
  4501. */
  4502. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  4503. }
  4504. else if (rtn >= 0)
  4505. {
  4506. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][send_encoded_iso2_V2GTP_Stream]Imcomplete Tx ( %d / %d ). (Bytes, DEC): FAIL\n",
  4507. rtn, v2g_tx_stream->size);
  4508. }
  4509. else
  4510. {
  4511. errn = rtn;
  4512. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][send_encoded_iso2_V2GTP_Stream]send(): %d (DEC)\n", errn);
  4513. }
  4514. //STEP 3: ========= Reset V2G MSG Flags ==========
  4515. init_iso2BodyType(&ccs_exi_doc_ISO2->V2G_Message.Body);
  4516. return errn;
  4517. }
  4518. /*===========================================================================
  4519. FUNCTION: Check_din_V2G_Rx_MSG_SessionID
  4520. DESCRIPTION:
  4521. PRE-CONDITION:
  4522. INPUT:
  4523. OUTPUT:
  4524. GLOBAL VARIABLES:
  4525. =============================================================================*/
  4526. int Check_din_V2G_Rx_MSG_SessionID(struct dinEXIDocument *exi_doc_DIN)
  4527. {
  4528. int i = 0;
  4529. int leng = 0;
  4530. int errn = 0;
  4531. leng = exi_doc_DIN->V2G_Message.Header.SessionID.bytesLen;
  4532. //Step 1: Check SessionID Length
  4533. if (leng != 8) //8-byte
  4534. {
  4535. sprintf(buf_log_evcomm, "[ERROR]SessionID: incorrect length(%d)", leng);
  4536. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4537. errn = -1;
  4538. }
  4539. else
  4540. {
  4541. //Step 2-1: Check SessionID content
  4542. for (i = 0; i < leng; i++)
  4543. {
  4544. if (exi_doc_DIN->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  4545. {
  4546. errn = -2;
  4547. break;
  4548. }
  4549. }
  4550. }
  4551. //Step 2-2: Print Incorrect ID
  4552. if (errn == -2) //incorrect ID
  4553. {
  4554. sprintf(buf_log_evcomm,
  4555. "[ERROR]SessionID: incorrect ID(RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)",
  4556. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[0],
  4557. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[1],
  4558. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[2],
  4559. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[3],
  4560. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[4],
  4561. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[5],
  4562. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[6],
  4563. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[7],
  4564. EVCOMM_SYS_INFO.SessionID[0],
  4565. EVCOMM_SYS_INFO.SessionID[1],
  4566. EVCOMM_SYS_INFO.SessionID[2],
  4567. EVCOMM_SYS_INFO.SessionID[3],
  4568. EVCOMM_SYS_INFO.SessionID[4],
  4569. EVCOMM_SYS_INFO.SessionID[5],
  4570. EVCOMM_SYS_INFO.SessionID[6],
  4571. EVCOMM_SYS_INFO.SessionID[7]
  4572. );
  4573. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4574. }
  4575. //Step 3: Correct SessionID for Res Message
  4576. if (errn != 0)
  4577. {
  4578. exi_doc_DIN->V2G_Message.Header.SessionID.bytesLen = 8;
  4579. memset(exi_doc_DIN->V2G_Message.Header.SessionID.bytes, 0, 8);
  4580. memcpy(exi_doc_DIN->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4581. }
  4582. return errn;
  4583. }
  4584. /*===========================================================================
  4585. FUNCTION: Check_iso1_V2G_Rx_MSG_SessionID
  4586. DESCRIPTION:
  4587. PRE-CONDITION:
  4588. INPUT:
  4589. OUTPUT:
  4590. GLOBAL VARIABLES:
  4591. =============================================================================*/
  4592. int Check_iso1_V2G_Rx_MSG_SessionID(struct iso1EXIDocument *exi_doc_ISO1)
  4593. {
  4594. int i = 0;
  4595. int leng = 0;
  4596. int errn = 0;
  4597. leng = exi_doc_ISO1->V2G_Message.Header.SessionID.bytesLen;
  4598. //Step 1: Check SessionID Length
  4599. if (leng != 8) //8-byte
  4600. {
  4601. sprintf(buf_log_evcomm, "[ERROR]SessionID: incorrect length(%d)", leng);
  4602. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4603. errn = -1;
  4604. }
  4605. else
  4606. {
  4607. //Step 2-1: Check SessionID content
  4608. for (i = 0; i < leng; i++)
  4609. {
  4610. if (exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  4611. {
  4612. errn = -2;
  4613. break;
  4614. }
  4615. }
  4616. }
  4617. //Step 2-2: Print Incorrect ID
  4618. if (errn == -2) //incorrect ID
  4619. {
  4620. sprintf(buf_log_evcomm,
  4621. "[ERROR]SessionID: incorrect ID(RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)",
  4622. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[0],
  4623. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[1],
  4624. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[2],
  4625. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[3],
  4626. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[4],
  4627. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[5],
  4628. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[6],
  4629. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[7],
  4630. EVCOMM_SYS_INFO.SessionID[0],
  4631. EVCOMM_SYS_INFO.SessionID[1],
  4632. EVCOMM_SYS_INFO.SessionID[2],
  4633. EVCOMM_SYS_INFO.SessionID[3],
  4634. EVCOMM_SYS_INFO.SessionID[4],
  4635. EVCOMM_SYS_INFO.SessionID[5],
  4636. EVCOMM_SYS_INFO.SessionID[6],
  4637. EVCOMM_SYS_INFO.SessionID[7]
  4638. );
  4639. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4640. }
  4641. //Step 3: Correct SessionID for Res Message
  4642. if (errn != 0)
  4643. {
  4644. exi_doc_ISO1->V2G_Message.Header.SessionID.bytesLen = 8;
  4645. memset(exi_doc_ISO1->V2G_Message.Header.SessionID.bytes, 0, 8);
  4646. memcpy(exi_doc_ISO1->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4647. }
  4648. return errn;
  4649. }
  4650. /*===========================================================================
  4651. FUNCTION: Check_iso2_V2G_Rx_MSG_SessionID
  4652. DESCRIPTION:
  4653. PRE-CONDITION:
  4654. INPUT:
  4655. OUTPUT:
  4656. GLOBAL VARIABLES:
  4657. =============================================================================*/
  4658. int Check_iso2_V2G_Rx_MSG_SessionID(struct iso2EXIDocument *exi_doc_ISO2)
  4659. {
  4660. int i = 0;
  4661. int leng = 0;
  4662. int errn = 0;
  4663. leng = exi_doc_ISO2->V2G_Message.Header.SessionID.bytesLen;
  4664. //Step 1: Check SessionID Length
  4665. if (leng != 8) //8-byte
  4666. {
  4667. sprintf(buf_log_evcomm, "[ERROR]SessionID: incorrect length(%d)", leng);
  4668. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4669. errn = -1;
  4670. }
  4671. else
  4672. {
  4673. //Step 2-1: Check SessionID content
  4674. for (i = 0; i < leng; i++)
  4675. {
  4676. if (exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  4677. {
  4678. errn = -2;
  4679. break;
  4680. }
  4681. }
  4682. }
  4683. //Step 2-2: Print Incorrect ID
  4684. if (errn == -2) //incorrect ID
  4685. {
  4686. sprintf(buf_log_evcomm,
  4687. "[ERROR]SessionID: incorrect ID(RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)",
  4688. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[0],
  4689. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[1],
  4690. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[2],
  4691. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[3],
  4692. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[4],
  4693. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[5],
  4694. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[6],
  4695. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[7],
  4696. EVCOMM_SYS_INFO.SessionID[0],
  4697. EVCOMM_SYS_INFO.SessionID[1],
  4698. EVCOMM_SYS_INFO.SessionID[2],
  4699. EVCOMM_SYS_INFO.SessionID[3],
  4700. EVCOMM_SYS_INFO.SessionID[4],
  4701. EVCOMM_SYS_INFO.SessionID[5],
  4702. EVCOMM_SYS_INFO.SessionID[6],
  4703. EVCOMM_SYS_INFO.SessionID[7]
  4704. );
  4705. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4706. }
  4707. //Step 3: Correct SessionID for Res Message
  4708. if (errn != 0)
  4709. {
  4710. exi_doc_ISO2->V2G_Message.Header.SessionID.bytesLen = 8;
  4711. memset(exi_doc_ISO2->V2G_Message.Header.SessionID.bytes, 0, 8);
  4712. memcpy(exi_doc_ISO2->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4713. }
  4714. return errn;
  4715. }
  4716. /*===========================================================================
  4717. FUNCTION: GetSchemaID_of_Protocol
  4718. DESCRIPTION:
  4719. 1. Get the SchemaID accroding to the input target
  4720. *V2GT_MSG_PROTOCOL_DIN70121: choose DIN 70121
  4721. *V2GT_MSG_PROTOCOL_ISO15118_2014: choose ISO 15118-2 (ed1)
  4722. *V2GT_MSG_PROTOCOL_ISO15118_2018: choose ISO 15118-20 (ed2)
  4723. *V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY: choose the one with the highest priority
  4724. 2. [To-do] Checking Major and Minor version
  4725. 3. The parsing method will not support those private protocols,
  4726. such as "usr:tesla...," since "tesla" is 5 bytes but "din" and "iso" are 3 bytes.
  4727. 4. [To-do] If the target is selected as DIN," but there is no DIN.
  4728. However, if EV and EVSE all support ISO, how to use ISO instead?
  4729. PRE-CONDITION:
  4730. INPUT:
  4731. 1. target: target protocol
  4732. OUTPUT:
  4733. 1. id: SchemaID of selected protocol by EVSE
  4734. 2. ShmCcsData->CommProtocol (selected protocol)
  4735. 3. SupportedAppProtocol_result
  4736. (1) appHandresponseCodeType_OK_SuccessfulNegotiation = 0,
  4737. (2) appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation = 1,
  4738. (3) appHandresponseCodeType_Failed_NoNegotiation = 2
  4739. GLOBAL VARIABLES:
  4740. 1. ccs_handshake
  4741. 2. ShmCcsData
  4742. =============================================================================*/
  4743. int GetSchemaID_of_Protocol(unsigned char target)
  4744. {
  4745. int i = 0;
  4746. int ii = 0;
  4747. int id = 0;
  4748. unsigned char tmp = 0;
  4749. unsigned char pri = 20; //priority = 1(highest)~20(lowerest)
  4750. char num[10];
  4751. //struct CCS_ProtocolNamespacestructCharacters pro;
  4752. //Choose the 1st protocol as default.
  4753. //id = CCS_HANDSHAKE_PROTOCOLS.array[0].SchemaID;
  4754. id = -1;
  4755. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  4756. for(i = 0; i < CCS_HANDSHAKE_PROTOCOLS.arrayLen; i++)
  4757. {
  4758. //Checking for <ProtocolNamespace>urn:din:70121:2012:MsgDef</ProtocolNamespace>
  4759. //[To-Do] Ignoring the priority from EV and force DIN 70121 as our only option. (for temp)
  4760. //[CAUTION] The parsing method will not support those private protocols, such as "usr:tesla..."
  4761. num[0] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[8];
  4762. num[1] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[9];
  4763. num[2] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[10];
  4764. num[3] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[11];
  4765. num[4] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[12];
  4766. num[5] = '\0';
  4767. if (atoi(num) == 70121)
  4768. {
  4769. sprintf(buf_log_evcomm,
  4770. "[GetSchemaID_of_Protocol]supported(%d-th/%d): DIN 70121(%d:v%d.%d;id=%d,pri=%d)",
  4771. (i+1),
  4772. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4773. atoi(num),
  4774. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4775. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4776. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4777. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4778. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4779. if (target == V2GT_MSG_PROTOCOL_DIN70121)
  4780. {
  4781. sprintf(buf_log_evcomm,
  4782. "[GetSchemaID_of_Protocol]selected(%d-th/%d): DIN 70121(%d:v%d.%d;id=%d,pri=%d)",
  4783. (i+1),
  4784. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4785. atoi(num),
  4786. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4787. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4788. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4789. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4790. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4791. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == DIN_SPEC_70121_2012_VersionNumberMajor)
  4792. {
  4793. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == DIN_SPEC_70121_2012_VersionNumberMinor)
  4794. {
  4795. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  4796. }
  4797. else
  4798. { //[TC_SECC_VTB_SupportedAppProtocol_005]
  4799. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  4800. }
  4801. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4802. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  4803. return id;
  4804. }
  4805. else
  4806. {
  4807. //keep looking for the suitable protocol
  4808. }
  4809. }
  4810. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  4811. {
  4812. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  4813. {
  4814. ii = i;
  4815. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4816. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  4817. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  4818. }
  4819. else
  4820. {
  4821. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  4822. }
  4823. }
  4824. else
  4825. {
  4826. //null
  4827. }
  4828. }
  4829. else if (atoi(num) == 15118)
  4830. {
  4831. //urn:din:70121:2012:MsgDef
  4832. //urn:iso:15118:2:2013:MsgDef
  4833. memset(num, 0, sizeof(num));
  4834. num[0] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[16];
  4835. num[1] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[17];
  4836. num[2] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[18];
  4837. num[3] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[19];
  4838. num[4] = '\0';
  4839. if (atoi(num) < 2018 && atoi(num) >= 2010)
  4840. {
  4841. sprintf(buf_log_evcomm,
  4842. "[GetSchemaID_of_Protocol]supported(%d-th/%d): ISO 15118-2(ed1,%d:v%d.%d;id=%d,pri=%d)",
  4843. (i+1),
  4844. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4845. atoi(num),
  4846. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4847. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4848. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4849. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4850. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4851. if (target == V2GT_MSG_PROTOCOL_ISO15118_2014)
  4852. {
  4853. sprintf(buf_log_evcomm,
  4854. "[GetSchemaID_of_Protocol]selected(%d-th/%d): ISO 15118-2,ed1(%d:v%d.%d;id=%d,pri=%d)",
  4855. (i+1),
  4856. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4857. atoi(num),
  4858. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4859. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4860. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4861. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4862. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4863. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == ISO1_15118_2013_VersionNumberMajor)
  4864. {
  4865. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == ISO1_15118_2013_VersionNumberMinor)
  4866. {
  4867. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  4868. }
  4869. else
  4870. {
  4871. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  4872. }
  4873. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4874. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2014;
  4875. return id;
  4876. }
  4877. else
  4878. {
  4879. //keep looking for the suitable protocol
  4880. }
  4881. }
  4882. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  4883. {
  4884. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  4885. {
  4886. ii = i;
  4887. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4888. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  4889. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2014;
  4890. }
  4891. else
  4892. {
  4893. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  4894. }
  4895. }
  4896. else
  4897. {
  4898. //null
  4899. }
  4900. }
  4901. else if (atoi(num) >= 2018 && atoi(num) <= 2100) // >= 2018
  4902. {
  4903. sprintf(buf_log_evcomm,
  4904. "[GetSchemaID_of_Protocol]supported(%d-th/%d): ISO 15118-20(ed2,%d:v%d.%d;id=%d,pri=%d)",
  4905. (i+1),
  4906. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4907. atoi(num),
  4908. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4909. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4910. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4911. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4912. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4913. if (target == V2GT_MSG_PROTOCOL_ISO15118_2018)
  4914. {
  4915. sprintf(buf_log_evcomm,
  4916. "[GetSchemaID_of_Protocol]selected(%d-th/%d): ISO 15118-20,ed2(%d:v%d.%d;id=%d,pri=%d)",
  4917. (i+1),
  4918. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4919. atoi(num),
  4920. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4921. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4922. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4923. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4924. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4925. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == ISO2_15118_2018_VersionNumberMajor)
  4926. {
  4927. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == ISO2_15118_2018_VersionNumberMinor)
  4928. {
  4929. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  4930. }
  4931. else
  4932. {
  4933. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  4934. }
  4935. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4936. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2018;
  4937. return id;
  4938. }
  4939. else
  4940. {
  4941. //keep looking for the suitable protocol
  4942. }
  4943. }
  4944. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  4945. {
  4946. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  4947. {
  4948. ii = i;
  4949. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4950. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  4951. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2018;
  4952. }
  4953. else
  4954. {
  4955. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  4956. }
  4957. }
  4958. else
  4959. {
  4960. //null
  4961. }
  4962. }
  4963. else
  4964. {
  4965. //Unexpected Year
  4966. sprintf(buf_log_evcomm,
  4967. "[GetSchemaID_of_Protocol]unsupported(%d-th/%d): ISO 15118-X(unexpected year:%d,v%d.%d),id=%d,pri=%d",
  4968. (i+1),
  4969. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4970. atoi(num),
  4971. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4972. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4973. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4974. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4975. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4976. //return -1;
  4977. }
  4978. }
  4979. else
  4980. {
  4981. sprintf(buf_log_evcomm,
  4982. "[GetSchemaID_of_Protocol]unsupported protocol(%d-th/%d)(%d:v%d.%d;id=%d,pri=%d)",
  4983. (i+1),
  4984. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4985. atoi(num),
  4986. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4987. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4988. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4989. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4990. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  4991. //return -1;
  4992. }
  4993. }
  4994. //The final result of highest priority protocol
  4995. sprintf(buf_log_evcomm,
  4996. "[GetSchemaID_of_Protocol]selected(%d-th/%d): pro=%d(0:DIN,1:ISO1,2:ISO2);id=%d,pri=%d",
  4997. (ii+1),
  4998. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4999. ShmCcsData->CommProtocol,
  5000. id,
  5001. pri);
  5002. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5003. if (id < 0)
  5004. {
  5005. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_Failed_NoNegotiation;
  5006. }
  5007. return id;
  5008. }
  5009. /*===========================================================================
  5010. FUNCTION: Proc_supportedAppProtocolRes
  5011. DESCRIPTION:
  5012. PRE-CONDITION:
  5013. INPUT:
  5014. OUTPUT:
  5015. GLOBAL VARIABLES:
  5016. =============================================================================*/
  5017. int Proc_supportedAppProtocolRes(int AcceptFd)
  5018. {
  5019. int errn = 0;
  5020. bitstream_t v2g_tx_stream;
  5021. static struct ChargingInfoData *sys;
  5022. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5023. //STEP 1: =========== Setting the Response Message ===========
  5024. init_appHandEXIDocument(&ccs_handshake);
  5025. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_OK_SuccessfulNegotiation;
  5026. ccs_handshake.supportedAppProtocolRes_isUsed = 1u;
  5027. //select the 1st one as the default
  5028. ccs_handshake.supportedAppProtocolRes.SchemaID = CCS_HANDSHAKE_PROTOCOLS.array[0].SchemaID;
  5029. ccs_handshake.supportedAppProtocolRes.SchemaID_isUsed = 1u;
  5030. int id = 0;
  5031. /*+++ 20200808, vern, support both DIN and ISO +++*/
  5032. // id = GetSchemaID_of_Protocol(V2GT_MSG_PROTOCOL_PREFERENCE); //output: EVCOMM_SYS_INFO.SupportedAppProtocol_result
  5033. id = GetSchemaID_of_Protocol(V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY); //output: EVCOMM_SYS_INFO.SupportedAppProtocol_result
  5034. /*--- 20200808, vern, support both DIN and ISO ---*/
  5035. ccs_handshake.supportedAppProtocolRes.ResponseCode = EVCOMM_SYS_INFO.SupportedAppProtocol_result; //updating the response code
  5036. if (id < 0)
  5037. {
  5038. sprintf(buf_log_evcomm,
  5039. "[Error]No avalible CCS protocol (id = %d, preference = %d)",
  5040. id,
  5041. V2GT_MSG_PROTOCOL_PREFERENCE);
  5042. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5043. }
  5044. else
  5045. {
  5046. //selected SchemaID
  5047. ccs_handshake.supportedAppProtocolRes.SchemaID = (unsigned char) id;
  5048. }
  5049. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  5050. {
  5051. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_supportedAppProtocolRes]EVSE_Shutdown => End_Process");
  5052. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5053. errn = -1;
  5054. }
  5055. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5056. {
  5057. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_supportedAppProtocolRes]EVSE_EmergencyShutdown => End_Process");
  5058. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5059. errn = -1;
  5060. }
  5061. else
  5062. {
  5063. //null
  5064. }
  5065. //Check for Permission Changing from TRUE to FALSE
  5066. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  5067. if (ShmInternalComm->ChargingPermission_pre >=1 &&
  5068. ShmInternalComm->ChargingPermission == FALSE)
  5069. {
  5070. SAVE_SYS_LOG_MSG_EVCOMM("[Error][DIN][supportedAppProtocolRes]Permission OFF");
  5071. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5072. //errn = -1;
  5073. }
  5074. #if (CP_PROTECTION_MECHANISM == ENABLE)
  5075. {
  5076. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  5077. {
  5078. //Detect for CP State should be 9V with 5% PWM or 100%PWM (State B1, B2)
  5079. if (sys->CpState != 2 && sys->CpState != 3) //State B1, B2
  5080. {
  5081. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5082. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5083. Update_V2G_Flow_Status(Other_Fault);
  5084. sprintf(buf_log_evcomm,
  5085. "[Proc_supportedAppProtocolRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  5086. sys->CpState,
  5087. sys->CpVoltage);
  5088. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5089. }
  5090. }
  5091. #else
  5092. {
  5093. //Detect for CP State should be 9V (State B)
  5094. if (sys->CpState != 3) //B2
  5095. {
  5096. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5097. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5098. Update_V2G_Flow_Status(Other_Fault);
  5099. sprintf(buf_log_evcomm,
  5100. "[Proc_supportedAppProtocolRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  5101. sys->CpState,
  5102. sys->CpVoltage);
  5103. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5104. }
  5105. }
  5106. #endif
  5107. }
  5108. #endif
  5109. //STEP 2: =========== Encode into EXI ===========
  5110. if (errn = API_V2GMSG_EXI_Encoder_AppProtocol(&ccs_handshake, &v2g_tx_stream) !=0)
  5111. {
  5112. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Error]API_V2GMSG_EXI_Encoder_AppProtocol\n");
  5113. return errn;
  5114. }
  5115. //STEP 3: =========== Send Response Packet ===========
  5116. int Rtn = 0;
  5117. Rtn = send(AcceptFd, v2g_tx_stream.data, v2g_tx_stream.size, 0);
  5118. //Rtn = send(6, v2g_tx_stream.data, v2g_tx_stream.size, 0);
  5119. DEBUG_PRINTF_EVCOMM_DETAIL("Send V2GTP Packet Size = %d, Rtn = %d (Bytes, DEC)\n", v2g_tx_stream.size, Rtn);
  5120. if (Rtn < 0)
  5121. {
  5122. return Rtn;
  5123. }
  5124. //STEP 4: =========== Save into Share Memory =========
  5125. //[Joseph, To-Do] Adding a mechanism to choose DIN 70121 as our 1st priority. (for multiple protocols)
  5126. //STEP 5: =========== Updating the Flow State Flag =========
  5127. if (id < 0)
  5128. {
  5129. errn = -1;
  5130. }
  5131. //STEP 6: =========== Reset Flags ============
  5132. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  5133. init_appHandEXIDocument(&ccs_handshake);
  5134. return errn;
  5135. }
  5136. /*===========================================================================
  5137. FUNCTION: Proc_supportedAppProtocolReq
  5138. DESCRIPTION:
  5139. PRE-CONDITION:
  5140. INPUT:
  5141. OUTPUT:
  5142. GLOBAL VARIABLES:
  5143. =============================================================================*/
  5144. int Proc_supportedAppProtocolReq(int AcceptFd)
  5145. {
  5146. //[Joseph, To-Do] analysis on Req message and choose the prefered protocol
  5147. //Default: DIN 70121 (find SchemaID)
  5148. int errn = 0;
  5149. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]supportedAppProtocolReq");
  5150. SHM_Save_din_supportedAppProtocolReq(ShmCcsData, &ccs_handshake, ShmSysConfigAndInfo);
  5151. errn = Proc_supportedAppProtocolRes(AcceptFd);
  5152. if (errn == 0)
  5153. {
  5154. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Tx]supportedAppProtocolRes");
  5155. }
  5156. else
  5157. {
  5158. DEBUG_PRINTF_EVCOMM_DETAIL("[Error]Proc_supportedAppProtocolRes(): %d (DEC)\n", errn);
  5159. }
  5160. return errn;
  5161. }
  5162. /*===========================================================================
  5163. FUNCTION: Proc_din_SessionSetupRes
  5164. DESCRIPTION:
  5165. PRE-CONDITION:
  5166. INPUT:
  5167. OUTPUT:
  5168. GLOBAL VARIABLES:
  5169. =============================================================================*/
  5170. int Proc_din_SessionSetupRes(int AcceptFd)
  5171. {
  5172. int i = 0;
  5173. int errn = 0;
  5174. bitstream_t v2g_tx_stream;
  5175. static struct ChargingInfoData *sys;
  5176. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5177. size_t pos = 0;
  5178. v2g_tx_stream.pos = &pos;
  5179. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5180. v2g_tx_stream.data = V2GTP_Tx_buf;
  5181. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  5182. init_dinSessionSetupResType(&ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes);
  5183. // ====== [BODY (1/2) ResponseCode ======
  5184. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  5185. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_OK_NewSessionEstablished;
  5186. //[HEADER] Assign Res SessionID
  5187. ccs_exi_doc_DIN.V2G_Message.Header.SessionID.bytesLen = 8;
  5188. memset(ccs_exi_doc_DIN.V2G_Message.Header.SessionID.bytes, 0, 8);
  5189. memcpy(ccs_exi_doc_DIN.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  5190. //Check for SequenceError
  5191. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5192. {
  5193. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError;
  5194. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  5195. errn = -1;
  5196. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5197. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5198. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5199. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5200. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5201. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5202. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5203. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5204. }
  5205. //#if PARAMETER_NORMAL_MODE == ENABLE
  5206. //SHM_Read_din_V2GMSG_Header(&ccs_exi_doc_DIN, ShmCcsData);
  5207. //#endif
  5208. //Detect for CP State should be 9V (State B)
  5209. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5210. {
  5211. #if CP_PROTECTION_MECHANISM == ENABLE
  5212. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  5213. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5214. Update_V2G_Flow_Status(Other_Fault);
  5215. errn = -1;
  5216. sprintf(buf_log_evcomm,
  5217. "[Proc_din_SessionSetupRes]Emergency Stop by CP Error (%d)\n",
  5218. sys->CpState);
  5219. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5220. #else
  5221. sprintf(buf_log_evcomm,
  5222. "[Proc_din_SessionSetupRes]Emergency Stop by CP Error (%d): ignored\n",
  5223. sys->CpState);
  5224. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5225. #endif
  5226. }
  5227. //Check for shutdown commands from EVSE(DC Main Board)
  5228. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5229. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5230. {
  5231. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  5232. sprintf(buf_log_evcomm, "[Proc_din_SessionSetupRes]Stop by EVSE(%d:normal, %d:emergency): (%d)\n",
  5233. EVSE_Shutdown,
  5234. EVSE_EmergencyShutdown,
  5235. sys->DC_EVSEStatus);
  5236. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5237. errn = -1;
  5238. }
  5239. //Check for Permission Changing from TRUE to FALSE
  5240. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  5241. if (ShmInternalComm->ChargingPermission_pre >=1 &&
  5242. ShmInternalComm->ChargingPermission == FALSE)
  5243. {
  5244. SAVE_SYS_LOG_MSG_EVCOMM("[Error][DIN][SessionSetupRes]Permission OFF");
  5245. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  5246. errn = -1;
  5247. }
  5248. // ====== [BODY (2/3) EVSEID ======
  5249. //EVSEID = all zero
  5250. memset(ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytes, 0, sizeof(ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytes));
  5251. //vern, should be encode by SN
  5252. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytesLen = 1; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  5253. // ====== [BODY (3/3) DateTimeNow ======
  5254. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.DateTimeNow_isUsed = 1u;
  5255. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.DateTimeNow = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  5256. #if PARAMETER_NORMAL_MODE == ENABLE
  5257. ///////////SHM_Read_din_SessionSetupRes(&ccs_exi_doc_DIN, ShmCcsData);
  5258. #endif
  5259. // ============ Encode and Send Response Message ===========
  5260. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  5261. {
  5262. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_SessionSetupRes][Error]Tx encoded msg error");
  5263. errn = -1;
  5264. }
  5265. return errn;
  5266. }
  5267. /*===========================================================================
  5268. FUNCTION: Proc_iso1_SessionSetupRes
  5269. DESCRIPTION:
  5270. PRE-CONDITION:
  5271. INPUT:
  5272. OUTPUT:
  5273. GLOBAL VARIABLES:
  5274. =============================================================================*/
  5275. int Proc_iso1_SessionSetupRes(int AcceptFd)
  5276. {
  5277. int i = 0;
  5278. int errn = 0;
  5279. bitstream_t v2g_tx_stream;
  5280. static struct ChargingInfoData *sys;
  5281. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5282. size_t pos = 0;
  5283. v2g_tx_stream.pos = &pos;
  5284. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5285. v2g_tx_stream.data = V2GTP_Tx_buf;
  5286. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  5287. init_iso1SessionSetupResType(&ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes);
  5288. // ====== [BODY (1/2) ResponseCode ======
  5289. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  5290. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_OK_NewSessionEstablished;
  5291. //[HEADER] Assign Res SessionID
  5292. ccs_exi_doc_ISO1.V2G_Message.Header.SessionID.bytesLen = 8;
  5293. memset(ccs_exi_doc_ISO1.V2G_Message.Header.SessionID.bytes, 0, 8);
  5294. memcpy(ccs_exi_doc_ISO1.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  5295. //Check for SequenceError
  5296. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5297. {
  5298. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  5299. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  5300. errn = -1;
  5301. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5302. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5303. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5304. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5305. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5306. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5307. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5308. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5309. }
  5310. //#if PARAMETER_NORMAL_MODE == ENABLE
  5311. //SHM_Read_iso1_V2GMSG_Header(&ccs_exi_doc_ISO1, ShmCcsData);
  5312. //#endif
  5313. //Detect for CP State should be 9V (State B)
  5314. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5315. {
  5316. #if CP_PROTECTION_MECHANISM == ENABLE
  5317. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  5318. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5319. Update_V2G_Flow_Status(Other_Fault);
  5320. errn = -1;
  5321. sprintf(buf_log_evcomm,
  5322. "[Proc_iso1_SessionSetupRes]Emergency Stop by CP Error (%d)\n",
  5323. sys->CpState);
  5324. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5325. #else
  5326. sprintf(buf_log_evcomm,
  5327. "[Proc_iso1_SessionSetupRes]Emergency Stop by CP Error (%d): ignored\n",
  5328. sys->CpState);
  5329. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5330. #endif
  5331. }
  5332. //Check for shutdown commands from EVSE(DC Main Board)
  5333. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5334. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5335. {
  5336. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  5337. sprintf(buf_log_evcomm, "[Proc_iso1_SessionSetupRes]Stop by EVSE(%d:normal, %d:emergency): (%d)\n",
  5338. EVSE_Shutdown,
  5339. EVSE_EmergencyShutdown,
  5340. sys->DC_EVSEStatus);
  5341. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5342. errn = -1;
  5343. }
  5344. //Check for Permission Changing from TRUE to FALSE
  5345. if (ShmInternalComm->ChargingPermission_pre >=1 &&
  5346. ShmInternalComm->ChargingPermission == FALSE)
  5347. {
  5348. SAVE_SYS_LOG_MSG_EVCOMM("[Error][ISO1][SessionSetupRes]Permission OFF");
  5349. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  5350. errn = -1;
  5351. }
  5352. // ====== [BODY (2/3) EVSEID ======
  5353. //EVSEID = all zero
  5354. memset(ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters, 0, sizeof(ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters));
  5355. /*+++ 20200808, vern, set default EVSEID +++*/
  5356. //vern, should be encoded by SN
  5357. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[0]='Z';
  5358. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[1]='Z';
  5359. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[2]='0';
  5360. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[3]='0';
  5361. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[4]='0';
  5362. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[5]='0';
  5363. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[6]='0';
  5364. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 7; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  5365. /*--- 20200808, vern, set default EVSEID ---*/
  5366. // ====== [BODY (3/3) DateTimeNow ======
  5367. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSETimeStamp_isUsed = 1u;
  5368. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSETimeStamp = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  5369. #if PARAMETER_NORMAL_MODE == ENABLE
  5370. ///////////SHM_Read_iso1_SessionSetupRes(&ccs_exi_doc_ISO1, ShmCcsData);
  5371. #endif
  5372. // ============ Encode and Send Response Message ===========
  5373. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  5374. {
  5375. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_SessionSetupRes][Error]Tx encoded msg error");
  5376. errn = -1;
  5377. }
  5378. return errn;
  5379. }
  5380. /*===========================================================================
  5381. FUNCTION: Proc_iso2_SessionSetupRes
  5382. DESCRIPTION:
  5383. PRE-CONDITION:
  5384. INPUT:
  5385. OUTPUT:
  5386. GLOBAL VARIABLES:
  5387. =============================================================================*/
  5388. int Proc_iso2_SessionSetupRes(int AcceptFd)
  5389. {
  5390. int i = 0;
  5391. int errn = 0;
  5392. bitstream_t v2g_tx_stream;
  5393. static struct ChargingInfoData *sys;
  5394. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5395. size_t pos = 0;
  5396. v2g_tx_stream.pos = &pos;
  5397. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5398. v2g_tx_stream.data = V2GTP_Tx_buf;
  5399. init_iso2BodyType(&ccs_exi_doc_ISO2.V2G_Message.Body);
  5400. init_iso2SessionSetupResType(&ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes);
  5401. // ====== [BODY (1/2) ResponseCode ======
  5402. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  5403. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_OK_NewSessionEstablished;
  5404. //[HEADER] Assign Res SessionID
  5405. ccs_exi_doc_ISO2.V2G_Message.Header.SessionID.bytesLen = 8;
  5406. memset(ccs_exi_doc_ISO2.V2G_Message.Header.SessionID.bytes, 0, 8);
  5407. memcpy(ccs_exi_doc_ISO2.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  5408. //Check for SequenceError
  5409. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5410. {
  5411. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  5412. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  5413. errn = -1;
  5414. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5415. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5416. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5417. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5418. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5419. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5420. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5421. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5422. }
  5423. //#if PARAMETER_NORMAL_MODE == ENABLE
  5424. //SHM_Read_iso2_V2GMSG_Header(&ccs_exi_doc_ISO2, ShmCcsData);
  5425. //#endif
  5426. //Detect for CP State should be 9V (State B)
  5427. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5428. {
  5429. #if CP_PROTECTION_MECHANISM == ENABLE
  5430. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  5431. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5432. Update_V2G_Flow_Status(Other_Fault);
  5433. errn = -1;
  5434. sprintf(buf_log_evcomm,
  5435. "[Proc_iso2_SessionSetupRes]Emergency Stop by CP Error (%d)",
  5436. sys->CpState);
  5437. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5438. #else
  5439. sprintf(buf_log_evcomm,
  5440. "[Proc_iso2_SessionSetupRes]Emergency Stop by CP Error (%d): ignored",
  5441. sys->CpState);
  5442. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5443. #endif
  5444. }
  5445. //Check for shutdown commands from EVSE(DC Main Board)
  5446. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5447. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5448. {
  5449. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  5450. sprintf(buf_log_evcomm, "[Proc_iso2_SessionSetupRes]Stop by EVSE(%d:normal, %d:emergency): (%d)\n",
  5451. EVSE_Shutdown,
  5452. EVSE_EmergencyShutdown,
  5453. sys->DC_EVSEStatus);
  5454. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5455. errn = -1;
  5456. }
  5457. //Check for Permission Changing from TRUE to FALSE
  5458. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  5459. if (ShmInternalComm->ChargingPermission_pre >=1 &&
  5460. ShmInternalComm->ChargingPermission == FALSE)
  5461. {
  5462. SAVE_SYS_LOG_MSG_EVCOMM("[Error][ISO2][SessionSetupRes]Permission OFF");
  5463. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  5464. errn = -1;
  5465. }
  5466. // ====== [BODY (2/3) EVSEID ======
  5467. //EVSEID = all zero
  5468. memset(ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.characters, 0, sizeof(ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.characters));
  5469. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 15; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  5470. // ====== [BODY (3/3) DateTimeNow ======
  5471. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSETimeStamp_isUsed = 1u;
  5472. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSETimeStamp = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  5473. #if PARAMETER_NORMAL_MODE == ENABLE
  5474. ///////////SHM_Read_iso2_SessionSetupRes(&ccs_exi_doc_ISO2, ShmCcsData);
  5475. #endif
  5476. // ============ Encode and Send Response Message ===========
  5477. if (send_encoded_iso2_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO2) != 0)
  5478. {
  5479. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso2_SessionSetupRes][Error]Tx encoded msg error");
  5480. errn = -1;
  5481. }
  5482. return errn;
  5483. }
  5484. /*===========================================================================
  5485. FUNCTION: Proc_din_SessionSetupReq
  5486. DESCRIPTION:
  5487. PRE-CONDITION:
  5488. INPUT:
  5489. OUTPUT:
  5490. GLOBAL VARIABLES:
  5491. 2. ccs_exi_doc_DIN
  5492. =============================================================================*/
  5493. int Proc_din_SessionSetupReq(int AcceptFd)
  5494. {
  5495. int errn = 0;
  5496. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Proc_din_SessionSetupReq]Got SessionSetupReq\n");
  5497. //Print the decoded XML Document
  5498. PRINT_XML_DOC_DIN_SessionSetupReq(&ccs_exi_doc_DIN);
  5499. //Save into Share Memory
  5500. SHM_Save_din_SessionSetupReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  5501. errn = Proc_din_SessionSetupRes(AcceptFd);
  5502. if (errn == 0)
  5503. {
  5504. //successfully send response.
  5505. SAVE_SYS_LOG_MSG_EVCOMM("[DIN][V2G][Tx]SessionSetupRes");
  5506. }
  5507. else
  5508. {
  5509. sprintf(buf_log_evcomm,
  5510. "[Error]Proc_din_SessionSetupRes(): %d (DEC)",
  5511. errn);
  5512. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5513. }
  5514. return errn;
  5515. }
  5516. /*===========================================================================
  5517. FUNCTION: Proc_iso1_SessionSetupReq
  5518. DESCRIPTION:
  5519. PRE-CONDITION:
  5520. INPUT:
  5521. OUTPUT:
  5522. GLOBAL VARIABLES:
  5523. 2. ccs_exi_doc_ISO1
  5524. =============================================================================*/
  5525. int Proc_iso1_SessionSetupReq(int AcceptFd)
  5526. {
  5527. int errn = 0;
  5528. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Proc_iso1_SessionSetupReq]Got SessionSetupReq\n");
  5529. //Print the decoded XML Document
  5530. PRINT_XML_DOC_ISO1_SessionSetupReq(&ccs_exi_doc_ISO1);
  5531. //Save into Share Memory
  5532. SHM_Save_iso1_SessionSetupReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  5533. errn = Proc_iso1_SessionSetupRes(AcceptFd);
  5534. if (errn == 0)
  5535. {
  5536. //successfully send response.
  5537. SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]SessionSetupRes");
  5538. }
  5539. else
  5540. {
  5541. sprintf(buf_log_evcomm,
  5542. "[Error]Proc_iso1_SessionSetupRes(): %d (DEC)",
  5543. errn);
  5544. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5545. }
  5546. return errn;
  5547. }
  5548. /*===========================================================================
  5549. FUNCTION: Proc_iso2_SessionSetupReq
  5550. DESCRIPTION:
  5551. PRE-CONDITION:
  5552. INPUT:
  5553. OUTPUT:
  5554. GLOBAL VARIABLES:
  5555. 2. ccs_exi_doc_ISO2
  5556. =============================================================================*/
  5557. int Proc_iso2_SessionSetupReq(int AcceptFd)
  5558. {
  5559. int errn = 0;
  5560. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Proc_iso2_SessionSetupReq]Got SessionSetupReq\n");
  5561. //Print the decoded XML Document
  5562. PRINT_XML_DOC_ISO2_SessionSetupReq(&ccs_exi_doc_ISO2);
  5563. //Save into Share Memory
  5564. SHM_Save_iso2_SessionSetupReq(ShmCcsData, &ccs_exi_doc_ISO2, ShmSysConfigAndInfo);
  5565. errn = Proc_iso2_SessionSetupRes(AcceptFd);
  5566. if (errn == 0)
  5567. {
  5568. //successfully send response.
  5569. SAVE_SYS_LOG_MSG_EVCOMM("[ISO2][V2G][Tx]SessionSetupRes");
  5570. }
  5571. else
  5572. {
  5573. sprintf(buf_log_evcomm,
  5574. "[Error]Proc_iso2_SessionSetupRes(): %d (DEC)",
  5575. errn);
  5576. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5577. }
  5578. return errn;
  5579. }
  5580. /*===========================================================================
  5581. FUNCTION: Proc_din_ServiceDiscoveryRes
  5582. DESCRIPTION:
  5583. PRE-CONDITION:
  5584. INPUT:
  5585. OUTPUT:
  5586. GLOBAL VARIABLES:
  5587. =============================================================================*/
  5588. int Proc_din_ServiceDiscoveryRes(int AcceptFd)
  5589. {
  5590. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  5591. //if it is not the same, the packet should be ignored.
  5592. int i = 0;
  5593. int errn = 0;
  5594. bitstream_t v2g_tx_stream;
  5595. struct ChargingInfoData *sys;
  5596. struct ServiceDiscoveryRequest_DIN70121 *req;
  5597. struct ServiceDiscoveryResponse_DIN70121 *res;
  5598. size_t pos = 0;
  5599. v2g_tx_stream.pos = &pos;
  5600. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5601. v2g_tx_stream.data = V2GTP_Tx_buf;
  5602. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5603. req = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryRequest;
  5604. res = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryResponse;
  5605. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  5606. init_dinServiceDiscoveryResType(&ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes);
  5607. //[1/4] Response Code
  5608. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes_isUsed = 1u;
  5609. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_OK;
  5610. //[HEADER] Check Req SessionID
  5611. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  5612. {
  5613. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  5614. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  5615. errn = -1;
  5616. }
  5617. //Check for SequenceError
  5618. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5619. {
  5620. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  5621. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  5622. errn = -1;
  5623. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5624. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5625. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5626. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5627. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5628. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5629. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5630. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5631. }
  5632. //Detect for CP State should be 9V (State B)
  5633. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5634. {
  5635. #if CP_PROTECTION_MECHANISM == ENABLE
  5636. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  5637. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5638. Update_V2G_Flow_Status(Other_Fault);
  5639. errn = -1;
  5640. sprintf(buf_log_evcomm,
  5641. "[Proc_din_ServiceDiscoveryRes]Emergency Stop by CP Error (%d)",
  5642. sys->CpState);
  5643. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5644. #else
  5645. sprintf(buf_log_evcomm,
  5646. "[Proc_din_ServiceDiscoveryRes]Emergency Stop by CP Error (%d): ignored",
  5647. sys->CpState);
  5648. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5649. #endif
  5650. }
  5651. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5652. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5653. {
  5654. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  5655. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_ServiceDiscoveryRes]shutdown by EVSE");
  5656. errn = -1;
  5657. }
  5658. //Check for Permission Changing from TRUE to FALSE
  5659. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  5660. if (ShmInternalComm->ChargingPermission_pre >=1 &&
  5661. ShmInternalComm->ChargingPermission == FALSE)
  5662. {
  5663. SAVE_SYS_LOG_MSG_EVCOMM("[Error][DIN][ServiceDiscoveryRes]Permission OFF");
  5664. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  5665. errn = -1;
  5666. }
  5667. //[2/4] PaymentOptions
  5668. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptions.PaymentOption.arrayLen = 1u;
  5669. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptions.PaymentOption.array[0] = dinpaymentOptionType_ExternalPayment; //1
  5670. //[3/4] Charge Service
  5671. res->ChargeService_DIN70121.Services.ServiceTag.ServiceID = 1;
  5672. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceTag.ServiceID = (unsigned short) res->ChargeService_DIN70121.Services.ServiceTag.ServiceID;
  5673. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceTag.ServiceCategory = dinserviceCategoryType_EVCharging;
  5674. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.FreeService = 1; //[Joseph] for test
  5675. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.EnergyTransferType = dinEVSESupportedEnergyTransferType_DC_extended;
  5676. //[4/4] Service List (null, not be uesed for now.)
  5677. //#if PARAMETER_NORMAL_MODE == ENABLE
  5678. ///////////////SHM_Read_din_ServiceDiscoveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  5679. //#endif
  5680. // ============ Encode and Send Response Message ===========
  5681. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  5682. {
  5683. errn = -1;
  5684. }
  5685. return errn;
  5686. }
  5687. /*===========================================================================
  5688. FUNCTION: Proc_iso1_ServiceDiscoveryRes
  5689. DESCRIPTION:
  5690. PRE-CONDITION:
  5691. INPUT:
  5692. OUTPUT:
  5693. GLOBAL VARIABLES:
  5694. =============================================================================*/
  5695. int Proc_iso1_ServiceDiscoveryRes(int AcceptFd)
  5696. {
  5697. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  5698. //if it is not the same, the packet should be ignored.
  5699. int i = 0;
  5700. int errn = 0;
  5701. bitstream_t v2g_tx_stream;
  5702. struct ChargingInfoData *sys;
  5703. struct ServiceDiscoveryRequest_ISO15118_2014 *req;
  5704. struct ServiceDiscoveryResponse_ISO15118_2014 *res;
  5705. size_t pos = 0;
  5706. v2g_tx_stream.pos = &pos;
  5707. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5708. v2g_tx_stream.data = V2GTP_Tx_buf;
  5709. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5710. req = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryRequest;
  5711. res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryResponse;
  5712. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  5713. init_iso1ServiceDiscoveryResType(&ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes);
  5714. //[1/4] Response Code
  5715. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes_isUsed = 1u;
  5716. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_OK;
  5717. //[HEADER] Check Req SessionID
  5718. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  5719. {
  5720. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  5721. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  5722. errn = -1;
  5723. }
  5724. //Check for SequenceError
  5725. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5726. {
  5727. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  5728. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  5729. errn = -1;
  5730. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5731. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5732. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5733. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5734. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5735. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5736. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5737. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5738. }
  5739. //Detect for CP State should be 9V (State B)
  5740. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5741. {
  5742. #if CP_PROTECTION_MECHANISM == ENABLE
  5743. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  5744. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5745. Update_V2G_Flow_Status(Other_Fault);
  5746. errn = -1;
  5747. sprintf(buf_log_evcomm,
  5748. "[Proc_iso1_ServiceDiscoveryRes]Emergency Stop by CP Error (%d)",
  5749. sys->CpState);
  5750. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5751. #else
  5752. sprintf(buf_log_evcomm,
  5753. "[Proc_iso1_ServiceDiscoveryRes]Emergency Stop by CP Error (%d): ignored",
  5754. sys->CpState);
  5755. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5756. #endif
  5757. }
  5758. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5759. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5760. {
  5761. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  5762. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_ServiceDiscoveryRes]shutdown by EVSE");
  5763. errn = -1;
  5764. }
  5765. //Check for Permission Changing from TRUE to FALSE
  5766. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  5767. if (ShmInternalComm->ChargingPermission_pre >=1 &&
  5768. ShmInternalComm->ChargingPermission == FALSE)
  5769. {
  5770. SAVE_SYS_LOG_MSG_EVCOMM("[Error][ISO1][ServiceDiscoveryRes]Permission OFF");
  5771. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  5772. errn = -1;
  5773. }
  5774. //[2/4] PaymentOptionList
  5775. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptionList.PaymentOption.arrayLen = 1u;
  5776. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptionList.PaymentOption.array[0] = iso1paymentOptionType_ExternalPayment; //1
  5777. //[3/4] Charge Service
  5778. res->ChargeService.Services.ServiceID = 1;
  5779. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceID = (unsigned short) res->ChargeService.Services.ServiceID;
  5780. //ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName_isUsed = 1; //new in ISO1, not be used, yet.
  5781. //ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName.charactersLen = xxx;
  5782. //ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName.characters[i] = xxx;
  5783. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceCategory = iso1serviceCategoryType_EVCharging;
  5784. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.FreeService = 1; //[Joseph] for test
  5785. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.arrayLen = 1u; //[ISO1] max = 6
  5786. switch (ShmCcsData->EnergyTransferMode)
  5787. {
  5788. case DC_extended:
  5789. {
  5790. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_DC_extended;
  5791. break;
  5792. }
  5793. case AC_single_phase_core:
  5794. {
  5795. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_AC_single_phase_core;
  5796. break;
  5797. }
  5798. case AC_three_phase_core:
  5799. {
  5800. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_AC_three_phase_core;
  5801. break;
  5802. }
  5803. default:
  5804. {
  5805. sprintf(buf_log_evcomm,
  5806. "[ISO1][ServiceDiscoveryRes][Warning]unexpected EnergyTransferMode(%d)",
  5807. ShmCcsData->EnergyTransferMode);
  5808. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5809. break;
  5810. }
  5811. }
  5812. //[4/4] Service List (null, not be uesed for now.)
  5813. //#if PARAMETER_NORMAL_MODE == ENABLE
  5814. ///////////////SHM_Read_iso1_ServiceDiscoveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  5815. //#endif
  5816. // ============ Encode and Send Response Message ===========
  5817. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  5818. {
  5819. errn = -1;
  5820. }
  5821. return errn;
  5822. }
  5823. /*===========================================================================
  5824. FUNCTION: Proc_din_ServiceDiscoveryReq
  5825. DESCRIPTION:
  5826. PRE-CONDITION:
  5827. INPUT:
  5828. OUTPUT:
  5829. GLOBAL VARIABLES:
  5830. =============================================================================*/
  5831. int Proc_din_ServiceDiscoveryReq(int AcceptFd)
  5832. {
  5833. int errn = 0;
  5834. DEBUG_PRINTF_EVCOMM_DETAIL("\n[SeccComm][Proc_din_ServiceDiscoveryReq]Got ServiceDiscoveryReq\n");
  5835. //Print the decoded XML Document
  5836. PRINT_XML_DOC_DIN_ServiceDiscoveryReq(&ccs_exi_doc_DIN);
  5837. //Save into Share Memory
  5838. SHM_Save_din_ServiceDiscoveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  5839. errn = Proc_din_ServiceDiscoveryRes(AcceptFd);
  5840. if (errn == 0)
  5841. {
  5842. //send response successfully.
  5843. SAVE_SYS_LOG_MSG_EVCOMM("[DIN][V2G][Tx]ServiceDiscoveryRes");
  5844. }
  5845. else
  5846. {
  5847. sprintf(buf_log_evcomm,
  5848. "[Error][SeccComm][Proc_din_ServiceDiscoveryReq]Proc_din_ServiceDiscoveryRes(): %d (DEC)",
  5849. errn
  5850. );
  5851. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5852. }
  5853. return errn;
  5854. }
  5855. /*===========================================================================
  5856. FUNCTION: Proc_iso1_ServiceDiscoveryReq
  5857. DESCRIPTION:
  5858. PRE-CONDITION:
  5859. INPUT:
  5860. OUTPUT:
  5861. GLOBAL VARIABLES:
  5862. =============================================================================*/
  5863. int Proc_iso1_ServiceDiscoveryReq(int AcceptFd)
  5864. {
  5865. int errn = 0;
  5866. DEBUG_PRINTF_EVCOMM_DETAIL("\n[SeccComm][Proc_iso1_ServiceDiscoveryReq]Got ServiceDiscoveryReq\n");
  5867. //Print the decoded XML Document
  5868. PRINT_XML_DOC_ISO1_ServiceDiscoveryReq(&ccs_exi_doc_ISO1);
  5869. //Save into Share Memory
  5870. SHM_Save_iso1_ServiceDiscoveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  5871. errn = Proc_iso1_ServiceDiscoveryRes(AcceptFd);
  5872. if (errn == 0)
  5873. {
  5874. //send response successfully.
  5875. SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]ServiceDiscoveryRes");
  5876. }
  5877. else
  5878. {
  5879. sprintf(buf_log_evcomm,
  5880. "[Error][SeccComm][Proc_iso1_ServiceDiscoveryReq]Proc_iso1_ServiceDiscoveryRes(): %d (DEC)",
  5881. errn
  5882. );
  5883. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5884. }
  5885. return errn;
  5886. }
  5887. /*===========================================================================
  5888. FUNCTION: Proc_din_ServiceAndPaymentSelectionRes
  5889. DESCRIPTION:
  5890. PRE-CONDITION:
  5891. INPUT:
  5892. OUTPUT:
  5893. GLOBAL VARIABLES:
  5894. =============================================================================*/
  5895. int Proc_din_ServiceAndPaymentSelectionRes(int AcceptFd)
  5896. {
  5897. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  5898. //if it is not the same, the packet should be ignored.
  5899. int i = 0;
  5900. int errn = 0;
  5901. bitstream_t v2g_tx_stream;
  5902. struct ServiceAndPaymentSelectionRequest_DIN70121 *req;
  5903. struct ServiceAndPaymentSelectionResponse_DIN70121 *res;
  5904. struct ServiceDiscoveryResponse_DIN70121 *sd_res;
  5905. struct ChargingInfoData *sys;
  5906. size_t pos = 0;
  5907. v2g_tx_stream.pos = &pos;
  5908. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5909. v2g_tx_stream.data = V2GTP_Tx_buf;
  5910. req = &ShmCcsData->V2GMessage_DIN70121.ServiceAndPaymentSelectionRequest;
  5911. res = &ShmCcsData->V2GMessage_DIN70121.ServiceAndPaymentSelectionResponse;
  5912. sd_res = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryResponse;
  5913. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5914. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  5915. init_dinServicePaymentSelectionResType(&ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes);
  5916. //[1/1] Response Code
  5917. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes_isUsed = 1u;
  5918. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_OK;
  5919. //[HEADER] Check Req SessionID
  5920. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  5921. {
  5922. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  5923. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  5924. errn = -1;
  5925. }
  5926. //Check for SequenceError
  5927. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5928. {
  5929. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  5930. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  5931. errn = -1;
  5932. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5933. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5934. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5935. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5936. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5937. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5938. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5939. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5940. }
  5941. //Check for SelectedPaymentOption (TC_SECC_VTB_ServicePaymentSelection_007)
  5942. if (req->SelectedPaymentOption != dinpaymentOptionType_ExternalPayment) //1
  5943. {
  5944. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_PaymentSelectionInvalid;
  5945. sprintf(buf_log_evcomm,
  5946. "[ERROR]unexpected SelectedPaymentOption(%d) => End_Process (EIM only, no PnC, yet.)",
  5947. req->SelectedPaymentOption);
  5948. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5949. errn = -1;
  5950. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5951. //CCS_SECC_ResponseCode_FAILED_Payment SelectionInvalid (023762)
  5952. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5953. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5954. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5955. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5956. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  5957. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  5958. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5959. }
  5960. //Check for ServiceID (TC_SECC_VTB_ServicePaymentSelection_004)
  5961. if (req->SelectedServiceList.SelectedService[0].ServiceID != sd_res->ChargeService_DIN70121.Services.ServiceTag.ServiceID)
  5962. {
  5963. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_ServiceSelectionInvalid; //8
  5964. sprintf(buf_log_evcomm,
  5965. "[ERROR]Wrong selected ServiceID(%d) => End_Process",
  5966. req->SelectedServiceList.SelectedService[0].ServiceID);
  5967. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5968. errn = -1;
  5969. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5970. //CCS_SECC_ResponseCode_FAILED_ServiceSelectionInvalid (023764)
  5971. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5972. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5973. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5974. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5975. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  5976. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  5977. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5978. }
  5979. //Detect for CP State should be 9V (State B)
  5980. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5981. {
  5982. #if CP_PROTECTION_MECHANISM == ENABLE
  5983. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  5984. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5985. Update_V2G_Flow_Status(Other_Fault);
  5986. errn = -1;
  5987. sprintf(buf_log_evcomm,
  5988. "[Proc_din_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d)",
  5989. sys->CpState);
  5990. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5991. #else
  5992. sprintf(buf_log_evcomm,
  5993. "[Proc_din_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d): ignored due to function is disabled",
  5994. sys->CpState);
  5995. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  5996. #endif
  5997. }
  5998. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5999. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6000. {
  6001. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_ServiceAndPaymentSelectionRes]shutdown by EVSE");
  6002. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  6003. errn = -1;
  6004. }
  6005. //Check for Permission Changing from TRUE to FALSE
  6006. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  6007. if (ShmInternalComm->ChargingPermission_pre >=1 &&
  6008. ShmInternalComm->ChargingPermission == FALSE)
  6009. {
  6010. SAVE_SYS_LOG_MSG_EVCOMM("[Error][DIN][ServicePaymentSelectionRes]Permission OFF");
  6011. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  6012. errn = -1;
  6013. }
  6014. // ============ Encode and Send Response Message ===========
  6015. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  6016. {
  6017. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_ServiceAndPaymentSelectionRes]Tx encoded msg error");
  6018. errn = -1;
  6019. }
  6020. return errn;
  6021. }
  6022. /*===========================================================================
  6023. FUNCTION: Proc_iso1_ServiceAndPaymentSelectionRes
  6024. DESCRIPTION:
  6025. PRE-CONDITION:
  6026. INPUT:
  6027. OUTPUT:
  6028. GLOBAL VARIABLES:
  6029. =============================================================================*/
  6030. int Proc_iso1_ServiceAndPaymentSelectionRes(int AcceptFd)
  6031. {
  6032. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6033. //if it is not the same, the packet should be ignored.
  6034. int i = 0;
  6035. int errn = 0;
  6036. bitstream_t v2g_tx_stream;
  6037. struct ServiceAndPaymentSelectionRequest_ISO15118_2014 *req;
  6038. struct ServiceAndPaymentSelectionResponse_ISO15118_2014 *res;
  6039. struct ServiceDiscoveryResponse_ISO15118_2014 *sd_res;
  6040. static struct ChargingInfoData *sys;
  6041. size_t pos = 0;
  6042. v2g_tx_stream.pos = &pos;
  6043. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6044. v2g_tx_stream.data = V2GTP_Tx_buf;
  6045. req = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceAndPaymentSelectionRequest;
  6046. res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceAndPaymentSelectionResponse;
  6047. sd_res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryResponse;
  6048. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6049. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  6050. init_iso1PaymentServiceSelectionResType(&ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes);
  6051. //[1/1] Response Code
  6052. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes_isUsed = 1u;
  6053. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_OK;
  6054. //[HEADER] Check Req SessionID
  6055. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  6056. {
  6057. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  6058. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  6059. errn = -1;
  6060. }
  6061. //Check for SequenceError
  6062. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6063. {
  6064. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  6065. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  6066. errn = -1;
  6067. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6068. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6069. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6070. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6071. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6072. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6073. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6074. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6075. }
  6076. //Check for SelectedPaymentOption (TC_SECC_VTB_ServicePaymentSelection_007)
  6077. if (req->SelectedPaymentOption != dinpaymentOptionType_ExternalPayment) //1
  6078. {
  6079. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_PaymentSelectionInvalid;
  6080. sprintf(buf_log_evcomm,
  6081. "[ERROR]unexpected SelectedPaymentOption(%d) => End_Process (EIM only, no PnC, yet.)",
  6082. req->SelectedPaymentOption);
  6083. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6084. errn = -1;
  6085. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6086. //CCS_SECC_ResponseCode_FAILED_Payment SelectionInvalid (023762)
  6087. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6088. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6089. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6090. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6091. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  6092. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  6093. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6094. }
  6095. //Check for ServiceID (TC_SECC_VTB_ServicePaymentSelection_004)
  6096. if (req->SelectedServiceList.SelectedService[0].ServiceID != sd_res->ChargeService.Services.ServiceID)
  6097. {
  6098. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_ServiceSelectionInvalid; //8
  6099. sprintf(buf_log_evcomm,
  6100. "[ERROR]Wrong selected ServiceID(%d) => End_Process",
  6101. req->SelectedServiceList.SelectedService[0].ServiceID);
  6102. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6103. errn = -1;
  6104. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6105. //CCS_SECC_ResponseCode_FAILED_ServiceSelectionInvalid (023764)
  6106. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6107. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6108. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6109. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6110. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  6111. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  6112. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6113. }
  6114. //Detect for CP State should be 9V (State B)
  6115. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  6116. {
  6117. #if CP_PROTECTION_MECHANISM == ENABLE
  6118. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  6119. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6120. Update_V2G_Flow_Status(Other_Fault);
  6121. errn = -1;
  6122. sprintf(buf_log_evcomm,
  6123. "[Proc_iso1_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d)",
  6124. sys->CpState);
  6125. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6126. #else
  6127. sprintf(buf_log_evcomm,
  6128. "[Proc_iso1_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d): ignored due to function is disabled",
  6129. sys->CpState);
  6130. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6131. #endif
  6132. }
  6133. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  6134. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6135. {
  6136. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_ServiceAndPaymentSelectionRes]shutdown by EVSE");
  6137. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  6138. errn = -1;
  6139. }
  6140. //Check for Permission Changing from TRUE to FALSE
  6141. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  6142. if (ShmInternalComm->ChargingPermission_pre >=1 &&
  6143. ShmInternalComm->ChargingPermission == FALSE)
  6144. {
  6145. SAVE_SYS_LOG_MSG_EVCOMM("[Error][ISO1][PaymentServiceSelectionRes]Permission OFF");
  6146. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  6147. errn = -1;
  6148. }
  6149. // ============ Encode and Send Response Message ===========
  6150. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  6151. {
  6152. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_ServiceAndPaymentSelectionRes]Tx encoded msg error");
  6153. errn = -1;
  6154. }
  6155. return errn;
  6156. }
  6157. /*===========================================================================
  6158. FUNCTION: Proc_din_ServiceAndPaymentSelectionReq
  6159. DESCRIPTION:
  6160. PRE-CONDITION:
  6161. INPUT:
  6162. OUTPUT:
  6163. GLOBAL VARIABLES:
  6164. =============================================================================*/
  6165. int Proc_din_ServiceAndPaymentSelectionReq(int AcceptFd)
  6166. {
  6167. int errn = 0;
  6168. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][Proc_din_ServiceAndPaymentSelectionReq]Got ServiceAndPaymentSelectionReq");
  6169. //Print the decoded XML Document
  6170. PRINT_XML_DOC_DIN_ServiceAndPaymentSelectionReq(&ccs_exi_doc_DIN);
  6171. //Save into Share Memory
  6172. SHM_Save_din_ServiceAndPaymentSelectionReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  6173. errn = Proc_din_ServiceAndPaymentSelectionRes(AcceptFd);
  6174. if (errn == 0)
  6175. {
  6176. //send response successfully.
  6177. SAVE_SYS_LOG_MSG_EVCOMM("[DIN][V2G][Tx]ServiceAndPaymentSelectionRes");
  6178. }
  6179. else
  6180. {
  6181. sprintf(buf_log_evcomm,
  6182. "[Error][Proc_iso1_ServiceAndPaymentSelectionReq]Proc_iso1_ServiceAndPaymentSelectionRes(): %d (DEC)",
  6183. errn);
  6184. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6185. }
  6186. return errn;
  6187. }
  6188. /*===========================================================================
  6189. FUNCTION: Proc_iso1_ServiceAndPaymentSelectionReq
  6190. DESCRIPTION:
  6191. PRE-CONDITION:
  6192. INPUT:
  6193. OUTPUT:
  6194. GLOBAL VARIABLES:
  6195. =============================================================================*/
  6196. int Proc_iso1_ServiceAndPaymentSelectionReq(int AcceptFd)
  6197. {
  6198. int errn = 0;
  6199. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][Proc_iso1_ServiceAndPaymentSelectionReq]Got ServiceAndPaymentSelectionReq");
  6200. //Print the decoded XML Document
  6201. PRINT_XML_DOC_ISO1_ServiceAndPaymentSelectionReq(&ccs_exi_doc_ISO1);
  6202. //Save into Share Memory
  6203. SHM_Save_iso1_ServiceAndPaymentSelectionReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  6204. errn = Proc_iso1_ServiceAndPaymentSelectionRes(AcceptFd);
  6205. if (errn == 0)
  6206. {
  6207. //send response successfully.
  6208. SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]ServiceAndPaymentSelectionRes");
  6209. }
  6210. else
  6211. {
  6212. sprintf(buf_log_evcomm,
  6213. "[Error][Proc_iso1_ServiceAndPaymentSelectionReq]Proc_iso1_ServiceAndPaymentSelectionRes(): %d (DEC)",
  6214. errn);
  6215. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6216. }
  6217. return errn;
  6218. }
  6219. /*===========================================================================
  6220. FUNCTION: Proc_din_ContractAuthenticationRes
  6221. DESCRIPTION:
  6222. PRE-CONDITION:
  6223. INPUT:
  6224. OUTPUT:
  6225. GLOBAL VARIABLES:
  6226. =============================================================================*/
  6227. int Proc_din_ContractAuthenticationRes(int AcceptFd)
  6228. {
  6229. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6230. //if it is not the same, the packet should be ignored.
  6231. int i = 0;
  6232. int errn = 0;
  6233. bitstream_t v2g_tx_stream;
  6234. static struct ChargingInfoData *sys;
  6235. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6236. size_t pos = 0;
  6237. v2g_tx_stream.pos = &pos;
  6238. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6239. v2g_tx_stream.data = V2GTP_Tx_buf;
  6240. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  6241. init_dinContractAuthenticationResType(&ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes);
  6242. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes_isUsed = 1u;
  6243. //[BODY (1/2)] ResponseCode
  6244. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_OK;
  6245. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Ongoing; //0
  6246. //[HEADER] Check Req SessionID
  6247. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  6248. {
  6249. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  6250. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  6251. errn = -1;
  6252. }
  6253. //Check for SequenceError
  6254. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6255. {
  6256. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  6257. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  6258. errn = -1;
  6259. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6260. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6261. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6262. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6263. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6264. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6265. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6266. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6267. }
  6268. //Detect for CP State should be 9V (State B)
  6269. #if CP_PROTECTION_MECHANISM == ENABLE
  6270. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  6271. {
  6272. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6273. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6274. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6275. Update_V2G_Flow_Status(Other_Fault);
  6276. errn = -1;
  6277. sprintf(buf_log_evcomm,
  6278. "[Proc_din_ContractAuthenticationRes]Emergency Stop by CP Error (%d)",
  6279. sys->CpState);
  6280. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6281. }
  6282. #endif
  6283. //Check for CSU command of "Stop by EVSE"
  6284. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6285. {
  6286. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6287. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6288. errn = -1;
  6289. }
  6290. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6291. {
  6292. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6293. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6294. errn = -1;
  6295. }
  6296. //[BODY (2/2)] EVSEProcessing
  6297. if(ShmInternalComm->ChargingPermission == TRUE)
  6298. {
  6299. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished; //0
  6300. SAVE_SYS_LOG_MSG_EVCOMM("CSU Permission: OK");
  6301. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  6302. {
  6303. //Set PWM as 5% (for SLAC first case)
  6304. SAVE_SYS_LOG_MSG_EVCOMM("Set PWM as 5%%");
  6305. SwitchCpStateE(DISABLE);
  6306. OutputCpPwmDuty(5);
  6307. }
  6308. #endif
  6309. }
  6310. //Check for Permission Changing from TRUE to FALSE
  6311. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  6312. if (ShmInternalComm->ChargingPermission_pre >=1 &&
  6313. ShmInternalComm->ChargingPermission == FALSE)
  6314. {
  6315. SAVE_SYS_LOG_MSG_EVCOMM("[Error][DIN][ContractAuthenticationRes]Permission OFF");
  6316. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6317. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6318. errn = -1;
  6319. }
  6320. #if PARAMETER_NORMAL_MODE == ENABLE
  6321. ////////////SHM_Read_din_ContractAuthenticationRes(&ccs_exi_doc_DIN, ShmCcsData);
  6322. #endif
  6323. // ============ Encode and Send Response Message ===========
  6324. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  6325. {
  6326. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  6327. errn = -1;
  6328. }
  6329. return errn;
  6330. }
  6331. /*===========================================================================
  6332. FUNCTION: Proc_iso1_AuthorizationRes
  6333. DESCRIPTION:
  6334. PRE-CONDITION:
  6335. INPUT:
  6336. OUTPUT:
  6337. GLOBAL VARIABLES:
  6338. =============================================================================*/
  6339. int Proc_iso1_AuthorizationRes(int AcceptFd)
  6340. {
  6341. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6342. //if it is not the same, the packet should be ignored.
  6343. int i = 0;
  6344. int errn = 0;
  6345. bitstream_t v2g_tx_stream;
  6346. static struct ChargingInfoData *sys;
  6347. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6348. size_t pos = 0;
  6349. v2g_tx_stream.pos = &pos;
  6350. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6351. v2g_tx_stream.data = V2GTP_Tx_buf;
  6352. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  6353. init_iso1AuthorizationResType(&ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes);
  6354. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes_isUsed = 1u;
  6355. //[BODY (1/2)] ResponseCode
  6356. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_OK;
  6357. /*+++ 20200808, vern, EVSEProcessing should be waiting for Customer during authrization +++*/
  6358. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Ongoing_WaitingForCustomerInteraction; //0
  6359. /*--- 20200808, vern, should be waiting for Customer during authrization ---*/
  6360. //[HEADER] Check Req SessionID
  6361. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  6362. {
  6363. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  6364. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  6365. errn = -1;
  6366. }
  6367. //Check for SequenceError
  6368. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6369. {
  6370. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  6371. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  6372. errn = -1;
  6373. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6374. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6375. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6376. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6377. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6378. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6379. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6380. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6381. }
  6382. //Detect for CP State should be 9V (State B)
  6383. #if CP_PROTECTION_MECHANISM == ENABLE
  6384. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  6385. {
  6386. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED;
  6387. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6388. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6389. Update_V2G_Flow_Status(Other_Fault);
  6390. errn = -1;
  6391. sprintf(buf_log_evcomm,
  6392. "[Proc_iso1_AuthorizationRes]Emergency Stop by CP Error (%d)",
  6393. sys->CpState);
  6394. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6395. }
  6396. #endif
  6397. //Check for CSU command of "Stop by EVSE"
  6398. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6399. {
  6400. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  6401. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6402. errn = -1;
  6403. }
  6404. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6405. {
  6406. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  6407. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6408. errn = -1;
  6409. }
  6410. //[BODY (2/2)] EVSEProcessing
  6411. //Check for Permission from CSU
  6412. if(ShmInternalComm->ChargingPermission == TRUE)
  6413. {
  6414. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  6415. SAVE_SYS_LOG_MSG_EVCOMM("CSU Permission: OK");
  6416. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  6417. {
  6418. //Set PWM as 5% (for SLAC first case)
  6419. SAVE_SYS_LOG_MSG_EVCOMM("Set PWM as 5%%");
  6420. SwitchCpStateE(DISABLE);
  6421. OutputCpPwmDuty(5);
  6422. }
  6423. #endif
  6424. }
  6425. //Check for Permission Changing from TRUE to FALSE
  6426. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  6427. if (ShmInternalComm->ChargingPermission_pre >=1 &&
  6428. ShmInternalComm->ChargingPermission == FALSE)
  6429. {
  6430. SAVE_SYS_LOG_MSG_EVCOMM("[Error][ISO1][AuthorizationRes]Permission OFF");
  6431. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  6432. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6433. errn = -1;
  6434. }
  6435. #if PARAMETER_NORMAL_MODE == ENABLE
  6436. ////////////SHM_Read_iso1_AuthorizationRes(&ccs_exi_doc_ISO1, ShmCcsData);
  6437. #endif
  6438. // ============ Encode and Send Response Message ===========
  6439. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  6440. {
  6441. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  6442. errn = -1;
  6443. }
  6444. return errn;
  6445. }
  6446. /*===========================================================================
  6447. FUNCTION: Proc_din_ContractAuthenticationReq
  6448. DESCRIPTION:
  6449. PRE-CONDITION:
  6450. INPUT:
  6451. OUTPUT:
  6452. GLOBAL VARIABLES:
  6453. =============================================================================*/
  6454. int Proc_din_ContractAuthenticationReq(int AcceptFd)
  6455. {
  6456. int errn = 0;
  6457. DEBUG_PRINTF_EVCOMM_DETAIL("\n[SeccComm][Proc_din_ContractAuthenticationReq]Got Proc_din_ContractAuthenticationReq\n");
  6458. //Print the decoded XML Document
  6459. PRINT_XML_DOC_DIN_ContractAuthenticationReq(&ccs_exi_doc_DIN);
  6460. //Save into Share Memory
  6461. SHM_Save_din_ContractAuthenticationReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  6462. errn = Proc_din_ContractAuthenticationRes(AcceptFd);
  6463. if (errn == 0)
  6464. {
  6465. //send response successfully.
  6466. //SAVE_SYS_LOG_MSG_EVCOMM("[DIN][V2G][Tx]ContractAuthenticationRes");
  6467. }
  6468. else
  6469. {
  6470. sprintf(buf_log_evcomm,
  6471. "[Error][SeccComm][Proc_din_ContractAuthenticationReq]Proc_iso1_AuthenticationRes(): %d (DEC)",
  6472. errn);
  6473. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6474. }
  6475. return errn;
  6476. }
  6477. /*===========================================================================
  6478. FUNCTION: Proc_iso1_AuthenticationReq
  6479. DESCRIPTION:
  6480. PRE-CONDITION:
  6481. INPUT:
  6482. OUTPUT:
  6483. GLOBAL VARIABLES:
  6484. =============================================================================*/
  6485. int Proc_iso1_AuthenticationReq(int AcceptFd)
  6486. {
  6487. int errn = 0;
  6488. DEBUG_PRINTF_EVCOMM_DETAIL("\n[SeccComm][Proc_iso1_AuthenticationReq]Got AuthorizationReq\n");
  6489. //Print the decoded XML Document
  6490. PRINT_XML_DOC_ISO1_AuthorizationReq(&ccs_exi_doc_ISO1);
  6491. //Save into Share Memory
  6492. SHM_Save_iso1_AuthorizationReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  6493. errn = Proc_iso1_AuthorizationRes(AcceptFd);
  6494. if (errn == 0)
  6495. {
  6496. //send response successfully.
  6497. //SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]AuthorizationRes");
  6498. }
  6499. else
  6500. {
  6501. sprintf(buf_log_evcomm,
  6502. "[Error][SeccComm][Proc_iso1_AuthenticationReq]Proc_iso1_AuthenticationRes(): %d (DEC)",
  6503. errn);
  6504. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6505. }
  6506. return errn;
  6507. }
  6508. /*===========================================================================
  6509. FUNCTION: SHM_Init_dinDC_EVSEStatusType
  6510. DESCRIPTION:
  6511. PRE-CONDITION:
  6512. INPUT:
  6513. 1. in
  6514. OUTPUT:
  6515. 2. out
  6516. GLOBAL VARIABLES:
  6517. =============================================================================*/
  6518. void SHM_Init_dinDC_EVSEStatusType(struct DC_EVSEStatusType_DIN70121 *in)
  6519. {
  6520. in->EVSEIsolationStatus = dinisolationLevelType_Valid;
  6521. // dinisolationLevelType_Invalid = 0,
  6522. // dinisolationLevelType_Valid = 1, (default)
  6523. // dinisolationLevelType_Warning = 2,
  6524. // dinisolationLevelType_Fault = 3
  6525. in->EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  6526. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6527. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1, (default)
  6528. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6529. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6530. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6531. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6532. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6533. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  6534. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  6535. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  6536. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  6537. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  6538. in->EVSENotification = dinEVSENotificationType_None;
  6539. // dinEVSENotificationType_None = 0, (default)
  6540. // dinEVSENotificationType_StopCharging = 1,
  6541. // dinEVSENotificationType_ReNegotiation = 2
  6542. }
  6543. /*===========================================================================
  6544. FUNCTION: SHM_Save_dinPhysicalValueType
  6545. DESCRIPTION:
  6546. PRE-CONDITION:
  6547. INPUT:
  6548. OUTPUT:
  6549. GLOBAL VARIABLES:
  6550. =============================================================================*/
  6551. void SHM_Save_dinPhysicalValueType(struct PhysicalValueType_DIN70121 *obj, short value, int multiplier, unsigned char unit)
  6552. {
  6553. obj->Value = value;
  6554. obj->Multiplier = multiplier;
  6555. obj->Unit =unit;
  6556. }
  6557. /*===========================================================================
  6558. FUNCTION: SHM_Init_din_ChargeParameterDiscoveryRes
  6559. DESCRIPTION:
  6560. PRE-CONDITION:
  6561. INPUT:
  6562. 1. shm_ccs
  6563. OUTPUT:
  6564. 2. shm_ccs
  6565. GLOBAL VARIABLES:
  6566. =============================================================================*/
  6567. void SHM_Init_din_ChargeParameterDiscoveryRes(struct CcsData *shm_ccs)
  6568. {
  6569. struct ChargeParameterDiscoveryResponse_DIN70121 *in;
  6570. in = &shm_ccs->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse;
  6571. //----- [BODY (1/5)] ResponseCode -----
  6572. in->ResponseCode = dinresponseCodeType_OK;
  6573. //----- [BODY (2/5)] EVSEProcessing -----
  6574. in->EVSEProcessing = dinEVSEProcessingType_Ongoing; //0
  6575. // dinEVSEProcessingType_Finished = 0,
  6576. // dinEVSEProcessingType_Ongoing = 1
  6577. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  6578. //be fixed in another function.
  6579. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  6580. //ignore, since DIN doesn't support AC
  6581. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  6582. struct DC_EVSEChargeParameterType_DIN70121 *in_para;
  6583. in_para = &in->DC_EVSEChargeParameter;
  6584. //DC_EVSEStatus
  6585. SHM_Init_dinDC_EVSEStatusType(&in_para->DC_EVSEStatus);
  6586. in_para->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  6587. short value = 0; int multiplier = 0; unsigned char unit = 0;
  6588. //EVSEMaximumCurrentLimit
  6589. value = 600; multiplier = -1; unit = A_DIN70121; //60A
  6590. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumCurrentLimit, value, multiplier, unit);
  6591. //EVSEMaximumPowerLimit
  6592. value = 3000; multiplier = 1; unit = W_DIN70121; //30KW
  6593. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumPowerLimit, value, multiplier, unit);
  6594. //EVSEMaximumVoltageLimit
  6595. value = 7500; multiplier = -1; unit = V_DIN70121; //750V
  6596. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumVoltageLimit, value, multiplier, unit);
  6597. //EVSEMinimumVoltageLimit
  6598. value = 1500; multiplier = -1; unit = V_DIN70121; //150V
  6599. SHM_Save_dinPhysicalValueType(&in_para->EVSEMinimumVoltageLimit, value, multiplier, unit);
  6600. //EVSEMinimumCurrentLimit
  6601. value = 10; multiplier = -1; unit = A_DIN70121; //1A
  6602. SHM_Save_dinPhysicalValueType(&in_para->EVSEMinimumCurrentLimit, value, multiplier, unit);
  6603. //EVSECurrentRegulationTolerance
  6604. value = 10; multiplier = -1; unit = A_DIN70121; //1A
  6605. SHM_Save_dinPhysicalValueType(&in_para->EVSECurrentRegulationTolerance, value, multiplier, unit);
  6606. //EVSEPeakCurrentRipple
  6607. value = 2; multiplier = -1; unit = A_DIN70121; //0.2A
  6608. SHM_Save_dinPhysicalValueType(&in_para->EVSEPeakCurrentRipple, value, multiplier, unit);
  6609. //EVSEEnergyToBeDelivered (optional)
  6610. //SHM_Save_dinPhysicalValueType(&out_para->EVSEEnergyToBeDelivered, &in_para->EVSEEnergyToBeDelivered);
  6611. }
  6612. /*===========================================================================
  6613. FUNCTION: Sudo_Parameter_din_ChargeParameterDiscoveryRes
  6614. DESCRIPTION:
  6615. PRE-CONDITION:
  6616. INPUT:
  6617. OUTPUT:
  6618. GLOBAL VARIABLES:
  6619. =============================================================================*/
  6620. void Sudo_Parameter_din_ChargeParameterDiscoveryRes()
  6621. {
  6622. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  6623. init_dinChargeParameterDiscoveryResType(&ccs_exi_doc_DIN.V2G_Message.Body.ChargeParameterDiscoveryRes);
  6624. ccs_exi_doc_DIN.V2G_Message.Body.ChargeParameterDiscoveryRes_isUsed = 1u;
  6625. //----- [BODY (1/5)] ResponseCode -----
  6626. struct dinChargeParameterDiscoveryResType *res;
  6627. res = &ccs_exi_doc_DIN.V2G_Message.Body.ChargeParameterDiscoveryRes;
  6628. res->ResponseCode = dinresponseCodeType_OK;
  6629. //----- [BODY (2/5)] EVSEProcessing -----
  6630. res->EVSEProcessing = dinEVSEProcessingType_Finished; //0
  6631. // dinEVSEProcessingType_Finished = 0,
  6632. // dinEVSEProcessingType_Ongoing = 1
  6633. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  6634. res->SAScheduleList_isUsed = 1u;
  6635. struct dinSAScheduleListType *list;
  6636. list = &res->SAScheduleList;
  6637. //
  6638. list->SAScheduleTuple.arrayLen = 1;
  6639. list->SAScheduleTuple.array[0].SAScheduleTupleID = 0;
  6640. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleID = 0;
  6641. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.arrayLen = 1;
  6642. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval_isUsed = 1u;
  6643. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.start = 0;
  6644. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration = xxx
  6645. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration_isUsed = 1u;
  6646. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval_isUsed = 0u;
  6647. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval.xxx
  6648. //list->SAScheduleTuple.array[0].SalesTariff.xxx
  6649. //list->SAScheduleTuple.array[0].SalesTariff_isUsed = 0u;
  6650. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  6651. //ignore, since DIN doesn't support AC
  6652. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  6653. res->DC_EVSEChargeParameter_isUsed = 1u;
  6654. struct dinDC_EVSEChargeParameterType *para;
  6655. para = &res->DC_EVSEChargeParameter;
  6656. //DC_EVSEStatus
  6657. para->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  6658. para->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  6659. // dinisolationLevelType_Invalid = 0,
  6660. // dinisolationLevelType_Valid = 1,
  6661. // dinisolationLevelType_Warning = 2,
  6662. // dinisolationLevelType_Fault = 3
  6663. para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  6664. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6665. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  6666. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6667. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6668. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6669. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6670. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6671. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  6672. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  6673. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  6674. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  6675. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  6676. para->DC_EVSEStatus.NotificationMaxDelay = 0u;
  6677. para->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  6678. // dinEVSENotificationType_None = 0,
  6679. // dinEVSENotificationType_StopCharging = 1,
  6680. // dinEVSENotificationType_ReNegotiation = 2
  6681. //EVSEMaximumCurrentLimit
  6682. para->EVSEMaximumCurrentLimit.Value = 2400;
  6683. para->EVSEMaximumCurrentLimit.Multiplier = -1;
  6684. para->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  6685. para->EVSEMaximumCurrentLimit.Unit = dinunitSymbolType_A;
  6686. // dinunitSymbolType_h = 0,
  6687. // dinunitSymbolType_m = 1,
  6688. // dinunitSymbolType_s = 2,
  6689. // dinunitSymbolType_A = 3,
  6690. // dinunitSymbolType_Ah = 4,
  6691. // dinunitSymbolType_V = 5,
  6692. // dinunitSymbolType_VA = 6,
  6693. // dinunitSymbolType_W = 7,
  6694. // dinunitSymbolType_W_s = 8,
  6695. // dinunitSymbolType_Wh = 9
  6696. //EVSEMaximumPowerLimit
  6697. para->EVSEMaximumPowerLimit.Value = 6000;
  6698. para->EVSEMaximumPowerLimit.Multiplier = 1;
  6699. para->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  6700. para->EVSEMaximumPowerLimit.Unit = dinunitSymbolType_W;
  6701. //EVSEMaximumVoltageLimit
  6702. para->EVSEMaximumVoltageLimit.Value = 7500;
  6703. para->EVSEMaximumVoltageLimit.Multiplier = -1;
  6704. para->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  6705. para->EVSEMaximumVoltageLimit.Unit = dinunitSymbolType_V;
  6706. //EVSEMinimumVoltageLimit
  6707. para->EVSEMinimumVoltageLimit.Value = 1500;
  6708. para->EVSEMinimumVoltageLimit.Multiplier = -1;
  6709. para->EVSEMinimumVoltageLimit.Unit_isUsed = 1u;
  6710. para->EVSEMinimumVoltageLimit.Unit = dinunitSymbolType_V;
  6711. //EVSEMinimumCurrentLimit
  6712. para->EVSEMinimumCurrentLimit.Value = 20;
  6713. para->EVSEMinimumCurrentLimit.Multiplier = -1;
  6714. para->EVSEMinimumCurrentLimit.Unit_isUsed = 1u;
  6715. para->EVSEMinimumCurrentLimit.Unit = dinunitSymbolType_A;
  6716. //EVSECurrentRegulationTolerance_isUsed
  6717. para->EVSECurrentRegulationTolerance_isUsed = 1u;
  6718. //EVSECurrentRegulationTolerance
  6719. para->EVSECurrentRegulationTolerance.Value = 10;
  6720. para->EVSECurrentRegulationTolerance.Multiplier = -1;
  6721. para->EVSECurrentRegulationTolerance.Unit_isUsed = 1u;
  6722. para->EVSECurrentRegulationTolerance.Unit = dinunitSymbolType_A;
  6723. //EVSEEnergyToBeDelivered_isUsed
  6724. para->EVSEEnergyToBeDelivered_isUsed = 1u;
  6725. //EVSEPeakCurrentRipple
  6726. para->EVSEPeakCurrentRipple.Value = 2;
  6727. para->EVSEPeakCurrentRipple.Multiplier = -1;
  6728. para->EVSEPeakCurrentRipple.Unit_isUsed = 1u;
  6729. para->EVSEPeakCurrentRipple.Unit = dinunitSymbolType_A;
  6730. //EVSEEnergyToBeDelivered (optional)
  6731. /*
  6732. para->EVSEEnergyToBeDelivered.Value = 360;
  6733. para->EVSEEnergyToBeDelivered.Multiplier = 3;
  6734. para->EVSEEnergyToBeDelivered.Unit_isUsed = 1u;
  6735. para->EVSEEnergyToBeDelivered.Unit = dinunitSymbolType_Wh;
  6736. */
  6737. }
  6738. /*===========================================================================
  6739. FUNCTION: Sudo_Parameter_iso1_ChargeParameterDiscoveryRes
  6740. DESCRIPTION:
  6741. PRE-CONDITION:
  6742. INPUT:
  6743. OUTPUT:
  6744. GLOBAL VARIABLES:
  6745. =============================================================================*/
  6746. void Sudo_Parameter_iso1_ChargeParameterDiscoveryRes()
  6747. {
  6748. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  6749. init_iso1ChargeParameterDiscoveryResType(&ccs_exi_doc_ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes);
  6750. ccs_exi_doc_ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes_isUsed = 1u;
  6751. //----- [BODY (1/5)] ResponseCode -----
  6752. struct iso1ChargeParameterDiscoveryResType *res;
  6753. res = &ccs_exi_doc_ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes;
  6754. res->ResponseCode = iso1responseCodeType_OK;
  6755. //----- [BODY (2/5)] EVSEProcessing -----
  6756. res->EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  6757. // iso1EVSEProcessingType_Finished = 0,
  6758. // iso1EVSEProcessingType_Ongoing = 1
  6759. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  6760. res->SAScheduleList_isUsed = 1u;
  6761. struct iso1SAScheduleListType *list;
  6762. list = &res->SAScheduleList;
  6763. //
  6764. list->SAScheduleTuple.arrayLen = 1;
  6765. list->SAScheduleTuple.array[0].SAScheduleTupleID = 0;
  6766. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.arrayLen = 1;
  6767. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval_isUsed = 1u;
  6768. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.start = 0;
  6769. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration = xxx
  6770. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration_isUsed = 1u;
  6771. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval_isUsed = 0u;
  6772. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval.xxx
  6773. //list->SAScheduleTuple.array[0].SalesTariff.xxx
  6774. //list->SAScheduleTuple.array[0].SalesTariff_isUsed = 0u;
  6775. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  6776. //ignore, since our ISO1 doesn't support AC, yet
  6777. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  6778. res->DC_EVSEChargeParameter_isUsed = 1u;
  6779. struct iso1DC_EVSEChargeParameterType *para;
  6780. para = &res->DC_EVSEChargeParameter;
  6781. //DC_EVSEStatus
  6782. para->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  6783. para->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  6784. // iso1isolationLevelType_Invalid = 0,
  6785. // iso1isolationLevelType_Valid = 1,
  6786. // iso1isolationLevelType_Warning = 2,
  6787. // iso1isolationLevelType_Fault = 3
  6788. para->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  6789. // iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6790. // iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  6791. // iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6792. // iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6793. // iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6794. // iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6795. // iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6796. // iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  6797. // iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  6798. // iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  6799. // iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  6800. // iso1DC_EVSEStatusCodeType_Reserved_C = 11
  6801. para->DC_EVSEStatus.NotificationMaxDelay = 0u;
  6802. para->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  6803. // iso1EVSENotificationType_None = 0,
  6804. // iso1EVSENotificationType_StopCharging = 1,
  6805. // iso1EVSENotificationType_ReNegotiation = 2
  6806. //EVSEMaximumCurrentLimit
  6807. para->EVSEMaximumCurrentLimit.Value = 2400;
  6808. para->EVSEMaximumCurrentLimit.Multiplier = -1;
  6809. //para->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  6810. para->EVSEMaximumCurrentLimit.Unit = iso1unitSymbolType_A;
  6811. //iso1unitSymbolType_h = 0,
  6812. //iso1unitSymbolType_m = 1,
  6813. //iso1unitSymbolType_s = 2,
  6814. //iso1unitSymbolType_A = 3,
  6815. //iso1unitSymbolType_V = 4,
  6816. //iso1unitSymbolType_W = 5,
  6817. //iso1unitSymbolType_Wh = 6
  6818. //EVSEMaximumPowerLimit
  6819. para->EVSEMaximumPowerLimit.Value = 6000;
  6820. para->EVSEMaximumPowerLimit.Multiplier = 1;
  6821. //para->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  6822. para->EVSEMaximumPowerLimit.Unit = iso1unitSymbolType_W;
  6823. //EVSEMaximumVoltageLimit
  6824. para->EVSEMaximumVoltageLimit.Value = 7500;
  6825. para->EVSEMaximumVoltageLimit.Multiplier = -1;
  6826. //para->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  6827. para->EVSEMaximumVoltageLimit.Unit = iso1unitSymbolType_V;
  6828. //EVSEMinimumVoltageLimit
  6829. para->EVSEMinimumVoltageLimit.Value = 1500;
  6830. para->EVSEMinimumVoltageLimit.Multiplier = -1;
  6831. //para->EVSEMinimumVoltageLimit.Unit_isUsed = 1u;
  6832. para->EVSEMinimumVoltageLimit.Unit = iso1unitSymbolType_V;
  6833. //EVSEMinimumCurrentLimit
  6834. para->EVSEMinimumCurrentLimit.Value = 20;
  6835. para->EVSEMinimumCurrentLimit.Multiplier = -1;
  6836. //para->EVSEMinimumCurrentLimit.Unit_isUsed = 1u;
  6837. para->EVSEMinimumCurrentLimit.Unit = iso1unitSymbolType_A;
  6838. //EVSECurrentRegulationTolerance_isUsed
  6839. para->EVSECurrentRegulationTolerance_isUsed = 1u;
  6840. //EVSECurrentRegulationTolerance
  6841. para->EVSECurrentRegulationTolerance.Value = 10;
  6842. para->EVSECurrentRegulationTolerance.Multiplier = -1;
  6843. //para->EVSECurrentRegulationTolerance.Unit_isUsed = 1u;
  6844. para->EVSECurrentRegulationTolerance.Unit = iso1unitSymbolType_A;
  6845. //EVSEEnergyToBeDelivered_isUsed
  6846. para->EVSEEnergyToBeDelivered_isUsed = 1u;
  6847. //EVSEPeakCurrentRipple
  6848. para->EVSEPeakCurrentRipple.Value = 2;
  6849. para->EVSEPeakCurrentRipple.Multiplier = -1;
  6850. //para->EVSEPeakCurrentRipple.Unit_isUsed = 1u;
  6851. para->EVSEPeakCurrentRipple.Unit = iso1unitSymbolType_A;
  6852. //EVSEEnergyToBeDelivered (optional)
  6853. /*
  6854. para->EVSEEnergyToBeDelivered.Value = 360;
  6855. para->EVSEEnergyToBeDelivered.Multiplier = 3;
  6856. para->EVSEEnergyToBeDelivered.Unit_isUsed = 1u;
  6857. para->EVSEEnergyToBeDelivered.Unit = iso1unitSymbolType_Wh;
  6858. */
  6859. }
  6860. /*===========================================================================
  6861. FUNCTION: Proc_din_ChargeParameterDiscoveryRes
  6862. DESCRIPTION:
  6863. PRE-CONDITION:
  6864. INPUT:
  6865. OUTPUT:
  6866. GLOBAL VARIABLES:
  6867. =============================================================================*/
  6868. int Proc_din_ChargeParameterDiscoveryRes(int AcceptFd)
  6869. {
  6870. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6871. //if it is not the same, the packet should be ignored.
  6872. //STEP 1: ============ Initialize ============
  6873. int errn = 0;
  6874. bitstream_t v2g_tx_stream;
  6875. struct ChargeParameterDiscoveryResponse_DIN70121 *res;
  6876. struct ChargeParameterDiscoveryRequest_DIN70121 *req;
  6877. struct DC_EVSEChargeParameterType_DIN70121 *dc_para;
  6878. struct ChargingInfoData *sys;
  6879. size_t pos = 0;
  6880. v2g_tx_stream.pos = &pos;
  6881. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6882. v2g_tx_stream.data = V2GTP_Tx_buf;
  6883. res = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse;
  6884. req = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest;
  6885. dc_para = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse.DC_EVSEChargeParameter;
  6886. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6887. res->ResponseCode = OK_DIN70121;
  6888. res->EVSEProcessing = Ongoing_DIN70121;
  6889. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready; //1
  6890. //[HEADER] Check Req SessionID
  6891. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  6892. {
  6893. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  6894. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  6895. errn = -1;
  6896. }
  6897. //Check for SequenceError
  6898. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6899. {
  6900. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  6901. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  6902. errn = -1;
  6903. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6904. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6905. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6906. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6907. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6908. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6909. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6910. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6911. }
  6912. //[TC_SECC_VTB_ChargeParameterDiscovery_005]
  6913. if ( (sys->EvBatteryMaxCurrent < 0) ||
  6914. (sys->EvBatteryMaxVoltage<150)
  6915. )
  6916. {
  6917. sprintf(buf_log_evcomm,
  6918. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  6919. sys->EvBatteryMaxCurrent);
  6920. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6921. res->ResponseCode = dinresponseCodeType_FAILED_WrongChargeParameter; //16
  6922. res->EVSEProcessing = Finished_DIN70121;
  6923. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  6924. errn = -1;
  6925. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6926. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  6927. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6928. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6929. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6930. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6931. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  6932. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  6933. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6934. }
  6935. //STEP 2: ============ Modify Parameters of ShmCcsData ============
  6936. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_DIN70121);
  6937. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_DIN70121);
  6938. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_DIN70121);
  6939. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMinimumVoltageLimit, 1500, V_DIN70121); //150V
  6940. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMinimumCurrentLimit, 10, A_DIN70121); //1A
  6941. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSECurrentRegulationTolerance, 10, A_DIN70121); //1A
  6942. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEPeakCurrentRipple, 2, A_DIN70121); //0.2A
  6943. //SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEEnergyToBeDelivered, 0, WH_DIN70121); //optional
  6944. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  6945. if (sys->EvBatteryMaxVoltage <= 500)
  6946. {
  6947. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumVoltageLimit, (int) (500 * 10), V_DIN70121); //500V
  6948. }
  6949. #endif
  6950. //for test with Tesla Model 3, 10A
  6951. //SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumCurrentLimit, (int) (10 * 10), A_DIN70121); //10A, for testing with Tesla Model 3
  6952. //Check for EnergyTransferMode [TC_SECC_VTB_ChargeParameterDiscovery_004]
  6953. if (req->EVRequestedEnergyTransferType != dinEVSESupportedEnergyTransferType_DC_extended)
  6954. {
  6955. sprintf(buf_log_evcomm,
  6956. "[ERROR]Wrong EVRequestedEnergyTransferType(%d,%d)",
  6957. req->EVRequestedEnergyTransferType,
  6958. dinEVSESupportedEnergyTransferType_DC_extended);
  6959. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  6960. res->ResponseCode = FAILED_WrongEnergyTransferType_DIN70121;
  6961. res->EVSEProcessing = Finished_DIN70121;
  6962. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  6963. errn = -1;
  6964. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6965. Update_V2G_Flow_Status(Other_Fault);
  6966. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6967. //CCS_SECC_ResponseCode_FAILED_WrongEnergyTransferMode (023774)
  6968. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6969. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6970. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6971. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6972. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  6973. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  6974. }
  6975. //Check for CSU command of "Stop by EVSE"
  6976. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6977. {
  6978. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_ChargeParameterDiscoveryRes]EVSE_Shutdown");
  6979. res->ResponseCode = FAILED_DIN70121;
  6980. res->EVSEProcessing = Finished_DIN70121;
  6981. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  6982. errn = -1;
  6983. }
  6984. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6985. {
  6986. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_ChargeParameterDiscoveryRes]EVSE_EmergencyShutdown");
  6987. res->ResponseCode = FAILED_DIN70121;
  6988. res->EVSEProcessing = Finished_DIN70121;
  6989. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6990. errn = -1;
  6991. }
  6992. //Detect for CP State should be 9V (State B)
  6993. #if CP_PROTECTION_MECHANISM == ENABLE
  6994. if (sys->CpState != 3) //State B1, B2
  6995. {
  6996. res->ResponseCode = FAILED_DIN70121;
  6997. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6998. res->EVSEProcessing = Finished_DIN70121;
  6999. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7000. Update_V2G_Flow_Status(Other_Fault);
  7001. errn = -1;
  7002. sprintf(buf_log_evcomm,
  7003. "[Proc_din_ChargeParameterDiscoveryRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  7004. sys->CpState,
  7005. sys->CpVoltage);
  7006. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7007. }
  7008. #endif
  7009. //Check for Permission from CSU
  7010. if (ShmInternalComm->ChargingPermission == TRUE)
  7011. {
  7012. res->EVSEProcessing = Finished_DIN70121;
  7013. }
  7014. //Check for Permission Off
  7015. if (ShmInternalComm->ChargingPermission == FALSE)
  7016. {
  7017. SAVE_SYS_LOG_MSG_EVCOMM("[Error][DIN][ChargeParameterDiscoveryRes]Permission OFF");
  7018. res->ResponseCode = FAILED_DIN70121;
  7019. res->EVSEProcessing = Finished_DIN70121;
  7020. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7021. errn = -1;
  7022. }
  7023. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7024. #if PARAMETER_NORMAL_MODE == ENABLE
  7025. SHM_Read_din_ChargeParameterDiscoveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  7026. #else
  7027. Sudo_Parameter_din_ChargeParameterDiscoveryRes();
  7028. #endif
  7029. //STEP 4: ============ Encode and Send Response Message ===========
  7030. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  7031. {
  7032. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  7033. errn = -1;
  7034. }
  7035. //STPE 5: ============ Update Flags ============
  7036. return errn;
  7037. }
  7038. /*===========================================================================
  7039. FUNCTION: Proc_iso1_ChargeParameterDiscoveryRes
  7040. DESCRIPTION:
  7041. PRE-CONDITION:
  7042. INPUT:
  7043. OUTPUT:
  7044. GLOBAL VARIABLES:
  7045. =============================================================================*/
  7046. int Proc_iso1_ChargeParameterDiscoveryRes(int AcceptFd)
  7047. {
  7048. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  7049. //if it is not the same, the packet should be ignored.
  7050. //STEP 1: ============ Initialize ============
  7051. int errn = 0;
  7052. bitstream_t v2g_tx_stream;
  7053. struct ChargeParameterDiscoveryResponse_ISO15118_2014 *res;
  7054. struct ChargeParameterDiscoveryRequest_ISO15118_2014 *req;
  7055. struct DC_EVSEChargeParameterType_ISO15118_2014 *dc_para;
  7056. struct AC_EVSEChargeParameterType_ISO15118_2014 *ac_para;
  7057. struct ChargingInfoData *sys;
  7058. size_t pos = 0;
  7059. v2g_tx_stream.pos = &pos;
  7060. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7061. v2g_tx_stream.data = V2GTP_Tx_buf;
  7062. res = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse;
  7063. req = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest;
  7064. dc_para = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse.DC_EVSEChargeParameter;
  7065. ac_para = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse.AC_EVSEChargeParameter;
  7066. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7067. res->ResponseCode = OK_ISO15118_2014;
  7068. /*+++ 20200808, vern, EVSEProcessing should be on-going during ChargeParameterDiscovery +++*/
  7069. res->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  7070. /*--- 20200808, vern, EVSEProcessing should be on-going during ChargeParameterDiscovery ---*/
  7071. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready; //1
  7072. ac_para->AC_EVSEStatus.RCD = ShmInternalComm->AC_RcdStatus; //0:no error, 1:error
  7073. ac_para->AC_EVSEStatus.NotificationMaxDelay = 0; //unit: 1s
  7074. ac_para->AC_EVSEStatus.EVSENotification = ShmInternalComm->AC_EVSENotification; //0:none, 1:StopCharging, 2:RenNgotiation
  7075. //[HEADER] Check Req SessionID
  7076. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  7077. {
  7078. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  7079. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  7080. errn = -1;
  7081. }
  7082. //Check for SequenceError
  7083. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7084. {
  7085. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  7086. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  7087. errn = -1;
  7088. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7089. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7090. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7091. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7092. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7093. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7094. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7095. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7096. }
  7097. //[TC_SECC_VTB_ChargeParameterDiscovery_005]
  7098. /*+++ 20200808, vern, should check the voltage and current to see if the range of battery parameter is accepted by charger ---*/
  7099. if ((sys->EvBatteryMaxCurrent < 0) ||(sys->EvBatteryMaxVoltage<150))
  7100. {
  7101. sprintf(buf_log_evcomm,
  7102. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  7103. sys->EvBatteryMaxCurrent);
  7104. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7105. res->ResponseCode = iso1responseCodeType_FAILED_WrongChargeParameter; //16
  7106. res->EVSEProcessing = Finished_ISO15118_2014;
  7107. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7108. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7109. errn = -1;
  7110. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7111. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  7112. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7113. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7114. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7115. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7116. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  7117. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  7118. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7119. }
  7120. //STEP 2: ============ Modify Parameters of ShmCcsData ============
  7121. //DC
  7122. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_ISO15118_2014);
  7123. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  7124. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_ISO15118_2014);
  7125. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMinimumVoltageLimit, 1500, V_ISO15118_2014); //150V
  7126. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMinimumCurrentLimit, 10, A_ISO15118_2014); //1A
  7127. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSECurrentRegulationTolerance, 10, A_ISO15118_2014); //1A
  7128. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEPeakCurrentRipple, 2, A_ISO15118_2014); //0.2A
  7129. //SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEEnergyToBeDelivered, 0, Wh_ISO15118_2014); //optional
  7130. //AC
  7131. SAVE_PhysicalValueType_ISO15118_2014(&ac_para->EVSENominalVoltage, (int) (sys->AC_EVSENominalVoltage * 10), V_ISO15118_2014);
  7132. SAVE_PhysicalValueType_ISO15118_2014(&ac_para->EVSEMaxCurrent, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  7133. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  7134. if (sys->EvBatteryMaxVoltage <= 500)
  7135. {
  7136. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumVoltageLimit, (int) (500 * 10), V_ISO15118_2014); //500V
  7137. }
  7138. #endif
  7139. //for test with Tesla Model 3, 10A
  7140. //SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumCurrentLimit, (int) (10 * 10), A_ISO15118_2014); //10A, for testing with Tesla Model 3
  7141. //Check for EnergyTransferMode
  7142. if (req->RequestedEnergyTransferMode != ShmCcsData->EnergyTransferMode) //[CAUTION] Their table should be kept as the same.
  7143. {
  7144. sprintf(buf_log_evcomm,
  7145. "[ERROR]Unmatched RequestedEnergyTransferMode(%d,%d)",
  7146. req->RequestedEnergyTransferMode,
  7147. ShmCcsData->EnergyTransferMode);
  7148. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7149. res->ResponseCode = FAILED_WrongEnergyTransferMode_ISO15118_2014;
  7150. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7151. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7152. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7153. errn = -1;
  7154. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7155. Update_V2G_Flow_Status(Other_Fault);
  7156. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7157. //CCS_SECC_ResponseCode_FAILED_WrongEnergyTransferMode (023774)
  7158. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7159. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7160. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7161. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7162. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  7163. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  7164. }
  7165. //Check for CSU command of "Stop by EVSE"
  7166. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7167. {
  7168. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_ChargeParameterDiscoveryRes]EVSE_Shutdown");
  7169. res->ResponseCode = FAILED_ISO15118_2014;
  7170. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7171. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7172. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7173. errn = -1;
  7174. }
  7175. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7176. {
  7177. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_ChargeParameterDiscoveryRes]EVSE_EmergencyShutdown");
  7178. res->ResponseCode = FAILED_ISO15118_2014;
  7179. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7180. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7181. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7182. errn = -1;
  7183. }
  7184. //Detect for CP State should be 9V (State B)
  7185. #if CP_PROTECTION_MECHANISM == ENABLE
  7186. if (sys->CpState != 3) //State B1, B2
  7187. {
  7188. res->ResponseCode = FAILED_ISO15118_2014;
  7189. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7190. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7191. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7192. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7193. Update_V2G_Flow_Status(Other_Fault);
  7194. errn = -1;
  7195. sprintf(buf_log_evcomm,
  7196. "[Proc_iso1_ChargeParameterDiscoveryRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  7197. sys->CpState,
  7198. sys->CpVoltage);
  7199. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7200. }
  7201. #endif
  7202. //Check for Permission from CSU
  7203. if(ShmInternalComm->ChargingPermission == TRUE)
  7204. {
  7205. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7206. }
  7207. //Check for Permission Off
  7208. if (ShmInternalComm->ChargingPermission == FALSE)
  7209. {
  7210. SAVE_SYS_LOG_MSG_EVCOMM("[Error][ISO1][ChargeParameterDiscoveryRes]Permission OFF");
  7211. res->ResponseCode = FAILED_ISO15118_2014;
  7212. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7213. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7214. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7215. errn = -1;
  7216. }
  7217. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7218. #if PARAMETER_NORMAL_MODE == ENABLE
  7219. SHM_Read_iso1_ChargeParameterDiscoveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  7220. #else
  7221. Sudo_Parameter_iso1_ChargeParameterDiscoveryRes();
  7222. #endif
  7223. //STEP 4: ============ Encode and Send Response Message ===========
  7224. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  7225. {
  7226. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  7227. errn = -1;
  7228. }
  7229. //STPE 5: ============ Update Flags ============
  7230. return errn;
  7231. }
  7232. /*===========================================================================
  7233. FUNCTION: Proc_din_ChargeParameterDiscoveryReq
  7234. DESCRIPTION:
  7235. PRE-CONDITION:
  7236. INPUT:
  7237. OUTPUT:
  7238. GLOBAL VARIABLES:
  7239. =============================================================================*/
  7240. int Proc_din_ChargeParameterDiscoveryReq(int AcceptFd)
  7241. {
  7242. int errn = 0;
  7243. //SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_ChargeParameterDiscoveryReq] Got ChargeParameterDiscoveryReq");
  7244. //ftime(&SeqStartTime);
  7245. //Print the decoded XML Document
  7246. PRINT_XML_DOC_DIN_ChargeParameterDiscoveryReq(&ccs_exi_doc_DIN);
  7247. //Save into Share Memory
  7248. SHM_Save_din_ChargeParameterDiscoveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  7249. //Check for EV Error Code
  7250. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.DC_EVStatus.EVErrorCode);
  7251. errn = Proc_din_ChargeParameterDiscoveryRes(AcceptFd);
  7252. //ftime(&SeqEndTime);
  7253. //DEBUG_PRINTF_EVCOMM_DETAIL("\ndelta = %.02lf ms\n", DiffTimeb(SeqStartTime, SeqEndTime));
  7254. if ( errn == 0)
  7255. {
  7256. //send response successfully.
  7257. //SAVE_SYS_LOG_MSG_EVCOMM("[DIN][V2G][Tx]ChargeParameterDiscoveryRes");
  7258. }
  7259. else
  7260. {
  7261. sprintf(buf_log_evcomm,
  7262. "[Error][DIN][ChargeParameterDiscoveryRes]%d (DEC)",
  7263. errn);
  7264. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7265. }
  7266. return errn;
  7267. }
  7268. /*===========================================================================
  7269. FUNCTION: Proc_iso1_ChargeParameterDiscoveryReq
  7270. DESCRIPTION:
  7271. PRE-CONDITION:
  7272. INPUT:
  7273. OUTPUT:
  7274. GLOBAL VARIABLES:
  7275. =============================================================================*/
  7276. int Proc_iso1_ChargeParameterDiscoveryReq(int AcceptFd)
  7277. {
  7278. int errn = 0;
  7279. //SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_ChargeParameterDiscoveryReq] Got ChargeParameterDiscoveryReq");
  7280. //ftime(&SeqStartTime);
  7281. //Print the decoded XML Document
  7282. PRINT_XML_DOC_ISO1_ChargeParameterDiscoveryReq(&ccs_exi_doc_ISO1);
  7283. //Save into Share Memory
  7284. SHM_Save_iso1_ChargeParameterDiscoveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  7285. //Check for EV Error Code
  7286. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.DC_EVStatus.EVErrorCode);
  7287. errn = Proc_iso1_ChargeParameterDiscoveryRes(AcceptFd);
  7288. //ftime(&SeqEndTime);
  7289. //DEBUG_PRINTF_EVCOMM_DETAIL("\ndelta = %.02lf ms\n", DiffTimeb(SeqStartTime, SeqEndTime));
  7290. if (errn == 0)
  7291. {
  7292. //send response successfully.
  7293. //SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]ChargeParameterDiscoveryRes");
  7294. }
  7295. else
  7296. {
  7297. sprintf(buf_log_evcomm,
  7298. "[Error][ISO1][ChargeParameterDiscoveryRes]%d (DEC)",
  7299. errn);
  7300. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7301. }
  7302. return errn;
  7303. }
  7304. /*===========================================================================
  7305. FUNCTION: SHM_Init_din_CableCheckRes
  7306. DESCRIPTION:
  7307. PRE-CONDITION:
  7308. INPUT:
  7309. 1. shm_ccs
  7310. OUTPUT:
  7311. 1. shm_ccs
  7312. GLOBAL VARIABLES:
  7313. =============================================================================*/
  7314. void SHM_Init_din_CableCheckRes(struct CcsData *shm_ccs)
  7315. {
  7316. struct CableCheckResponse_DIN70121 *in;
  7317. in = &shm_ccs->V2GMessage_DIN70121.CableCheckResponse;
  7318. //----- [BODY (1/3)] ResponseCode -----
  7319. in->ResponseCode = dinresponseCodeType_OK;
  7320. //----- [BODY (2/3)] EVSEProcessing -----
  7321. //in->EVSEProcessing = dinEVSEProcessingType_Finished; //for test
  7322. in->EVSEProcessing = dinEVSEProcessingType_Ongoing; //default
  7323. // dinEVSEProcessingType_Finished = 0,
  7324. // dinEVSEProcessingType_Ongoing = 1
  7325. //----- [BODY (3/3)] DC_EVSEStatus -----
  7326. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  7327. in->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0 (default)
  7328. // dinisolationLevelType_Invalid = 0,
  7329. // dinisolationLevelType_Valid = 1, (default)
  7330. // dinisolationLevelType_Warning = 2,
  7331. // dinisolationLevelType_Fault = 3
  7332. }
  7333. /*===========================================================================
  7334. FUNCTION: Sudo_Parameter_din_CableCheckRes
  7335. DESCRIPTION:
  7336. PRE-CONDITION:
  7337. INPUT:
  7338. OUTPUT:
  7339. GLOBAL VARIABLES:
  7340. =============================================================================*/
  7341. void Sudo_Parameter_din_CableCheckRes()
  7342. {
  7343. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  7344. init_dinCableCheckResType(&ccs_exi_doc_DIN.V2G_Message.Body.CableCheckRes);
  7345. ccs_exi_doc_DIN.V2G_Message.Body.CableCheckRes_isUsed = 1u;
  7346. //----- [BODY (1/3)] ResponseCode -----
  7347. struct dinCableCheckResType *res;
  7348. res = &ccs_exi_doc_DIN.V2G_Message.Body.CableCheckRes;
  7349. res->ResponseCode = dinresponseCodeType_OK;
  7350. //----- [BODY (2/3)] EVSEProcessing -----
  7351. res->EVSEProcessing = dinEVSEProcessingType_Finished; //0
  7352. // dinEVSEProcessingType_Finished = 0,
  7353. // dinEVSEProcessingType_Ongoing = 1
  7354. //----- [BODY (3/3)] DC_EVSEStatus -----
  7355. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7356. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  7357. // dinisolationLevelType_Invalid = 0,
  7358. // dinisolationLevelType_Valid = 1,
  7359. // dinisolationLevelType_Warning = 2,
  7360. // dinisolationLevelType_Fault = 3
  7361. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7362. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7363. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  7364. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7365. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7366. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7367. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7368. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7369. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  7370. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  7371. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  7372. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  7373. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  7374. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7375. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  7376. // dinEVSENotificationType_None = 0,
  7377. // dinEVSENotificationType_StopCharging = 1,
  7378. // dinEVSENotificationType_ReNegotiation = 2
  7379. }
  7380. /*===========================================================================
  7381. FUNCTION: Sudo_Parameter_iso1_CableCheckRes
  7382. DESCRIPTION:
  7383. PRE-CONDITION:
  7384. INPUT:
  7385. OUTPUT:
  7386. GLOBAL VARIABLES:
  7387. =============================================================================*/
  7388. void Sudo_Parameter_iso1_CableCheckRes()
  7389. {
  7390. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  7391. init_iso1CableCheckResType(&ccs_exi_doc_ISO1.V2G_Message.Body.CableCheckRes);
  7392. ccs_exi_doc_ISO1.V2G_Message.Body.CableCheckRes_isUsed = 1u;
  7393. //----- [BODY (1/3)] ResponseCode -----
  7394. struct iso1CableCheckResType *res;
  7395. res = &ccs_exi_doc_ISO1.V2G_Message.Body.CableCheckRes;
  7396. res->ResponseCode = iso1responseCodeType_OK;
  7397. //----- [BODY (2/3)] EVSEProcessing -----
  7398. res->EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  7399. //iso1EVSEProcessingType_Finished = 0,
  7400. //iso1EVSEProcessingType_Ongoing = 1,
  7401. //iso1EVSEProcessingType_Ongoing_WaitingForCustomerInteraction = 2
  7402. //----- [BODY (3/3)] DC_EVSEStatus -----
  7403. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7404. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  7405. //iso1isolationLevelType_Invalid = 0,
  7406. //iso1isolationLevelType_Valid = 1,
  7407. //iso1isolationLevelType_Warning = 2,
  7408. //iso1isolationLevelType_Fault = 3,
  7409. //iso1isolationLevelType_No_IMD = 4
  7410. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  7411. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7412. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  7413. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7414. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7415. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7416. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7417. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7418. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  7419. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  7420. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  7421. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  7422. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  7423. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7424. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  7425. //iso1EVSENotificationType_None = 0,
  7426. //iso1EVSENotificationType_StopCharging = 1,
  7427. //iso1EVSENotificationType_ReNegotiation = 2
  7428. }
  7429. /*===========================================================================
  7430. FUNCTION: Proc_din_CableCheckRes
  7431. DESCRIPTION:
  7432. PRE-CONDITION:
  7433. INPUT:
  7434. OUTPUT:
  7435. GLOBAL VARIABLES:
  7436. =============================================================================*/
  7437. int Proc_din_CableCheckRes(int AcceptFd)
  7438. {
  7439. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  7440. //if it is not the same, the packet should be ignored.
  7441. //STEP 1: ============ Initialize ============
  7442. int i = 0;
  7443. int errn = 0;
  7444. bitstream_t v2g_tx_stream;
  7445. static struct CableCheckResponse_DIN70121 *cab;
  7446. static struct ChargingInfoData *sys;
  7447. size_t pos = 0;
  7448. v2g_tx_stream.pos = &pos;
  7449. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7450. v2g_tx_stream.data = V2GTP_Tx_buf;
  7451. cab = &ShmCcsData->V2GMessage_DIN70121.CableCheckResponse;
  7452. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7453. cab->ResponseCode = OK_DIN70121;
  7454. cab->cnt++;
  7455. DEBUG_PRINTF_EVCOMM_DETAIL("CP_State = %d (%.02f V), V_now = %.02f, Isolation Status = %d, EVSEProcessing = %d\n",
  7456. sys->CpState,
  7457. sys->CpVoltage,
  7458. sys->PresentChargingVoltage,
  7459. sys->IsolationStatus,
  7460. cab->EVSEProcessing);
  7461. //[HEADER] Check Req SessionID
  7462. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  7463. {
  7464. cab->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  7465. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  7466. errn = -1;
  7467. }
  7468. //Check for SequenceError
  7469. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7470. {
  7471. cab->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  7472. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  7473. errn = -1;
  7474. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7475. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7476. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7477. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7478. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7479. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7480. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7481. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7482. }
  7483. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  7484. if (sys->IsolationStatus == GFD_Invalid) //0: on going
  7485. {
  7486. //For PSU
  7487. sys->EvBatterytargetVoltage = CABLECHECK_TARGET_VOLTAGE; //500V //[To-Do] this should be modified to auto decision.
  7488. sys->EvBatterytargetCurrent = CABLECHECK_TARGET_CURRENT; //2A(default), unit: 1A
  7489. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  7490. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive; //4
  7491. cab->EVSEProcessing = dinEVSEProcessingType_Ongoing;
  7492. }
  7493. else if ((sys->IsolationStatus == GFD_Valid) || (sys->IsolationStatus == GFD_Warning)) //1: valid, 2: warning
  7494. {
  7495. //For PSU
  7496. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7497. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7498. if (sys->PresentChargingVoltage < 60) // < 60V
  7499. {
  7500. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  7501. sprintf(buf_log_evcomm, "[V2G][CableCheck]Pass (V_now = %.02f, Isolated = %d)",
  7502. sys->PresentChargingVoltage,
  7503. sys->IsolationStatus);
  7504. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7505. //IsolationStatus
  7506. if (sys->IsolationStatus == GFD_Valid)
  7507. {
  7508. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  7509. }
  7510. else if (sys->IsolationStatus == GFD_Warning)
  7511. {
  7512. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning;
  7513. }
  7514. else
  7515. {
  7516. sprintf(buf_log_evcomm,
  7517. "[WARNING]unexpected IsolationStatus(%d)",
  7518. sys->IsolationStatus);
  7519. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7520. }
  7521. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7522. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7523. }
  7524. else
  7525. {
  7526. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  7527. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  7528. cab->EVSEProcessing = dinEVSEProcessingType_Ongoing;
  7529. }
  7530. }
  7531. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  7532. {
  7533. //For PSU
  7534. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7535. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7536. cab->ResponseCode = FAILED_DIN70121;
  7537. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7538. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_NotReady;
  7539. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7540. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_CableCheckRes]IsolationStatus = 3 (fault)");
  7541. errn = -1;
  7542. }
  7543. else
  7544. {
  7545. //For PSU
  7546. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7547. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7548. cab->ResponseCode = FAILED_DIN70121;
  7549. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  7550. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_NotReady;
  7551. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7552. sprintf(buf_log_evcomm,
  7553. "[Proc_din_CableCheckRes]Undefined Isolation Status(%d)",
  7554. sys->IsolationStatus);
  7555. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7556. }
  7557. //Check for CSU command of "Stop by EVSE"
  7558. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7559. {
  7560. cab->ResponseCode = FAILED_DIN70121;
  7561. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7562. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7563. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7564. errn = -1;
  7565. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_CableCheckRes]EVSE_Shutdown");
  7566. }
  7567. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7568. {
  7569. cab->ResponseCode = FAILED_DIN70121;
  7570. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7571. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7572. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7573. errn = -1;
  7574. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_CableCheckRes]EVSE_EmergencyShutdown");
  7575. }
  7576. else if (ShmInternalComm->ChargingPermission == FALSE)
  7577. {
  7578. cab->ResponseCode = FAILED_DIN70121;
  7579. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7580. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7581. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7582. errn = -1;
  7583. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_din_CableCheckRes]ChargingPermission = FALSE");
  7584. }
  7585. else
  7586. {
  7587. //null
  7588. }
  7589. //Response to CP Error
  7590. #if CP_PROTECTION_MECHANISM == ENABLE
  7591. //#if 1
  7592. if (sys->CableCheckPreCountDownDone == TRUE) //[To-Do] Here should be modified to 2 seconds
  7593. {
  7594. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  7595. {
  7596. cab->ResponseCode = FAILED_DIN70121;
  7597. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7598. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7599. Update_V2G_Flow_Status(Other_Fault);
  7600. errn = -1;
  7601. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7602. //CCS_SECC_CP_State_Error (023889)
  7603. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7604. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7605. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7606. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  7607. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  7608. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  7609. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  7610. sprintf(buf_log_evcomm,
  7611. "[CableCheck]Emergency Stop by CP Error (%d, %.02f V, %d)\n",
  7612. sys->CpState,
  7613. sys->CpVoltage,
  7614. cab->cnt
  7615. );
  7616. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7617. //cab->cnt = 0;
  7618. }
  7619. }
  7620. #endif
  7621. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7622. #if PARAMETER_NORMAL_MODE == ENABLE
  7623. SHM_Read_din_CableCheckRes(&ccs_exi_doc_DIN, ShmCcsData);
  7624. #else
  7625. Sudo_Parameter_din_CableCheckRes();
  7626. #endif
  7627. //STEP 4: ============ Encode and Send Response Message ===========
  7628. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  7629. {
  7630. errn = -1;
  7631. }
  7632. //STEP 5: ============ Update Flags ===========
  7633. sys->ConnectorLocked = TRUE; //Inicating EVSE that the CCS Connector is Locked.
  7634. return errn;
  7635. }
  7636. /*===========================================================================
  7637. FUNCTION: Proc_iso1_CableCheckRes
  7638. DESCRIPTION:
  7639. PRE-CONDITION:
  7640. INPUT:
  7641. OUTPUT:
  7642. GLOBAL VARIABLES:
  7643. =============================================================================*/
  7644. int Proc_iso1_CableCheckRes(int AcceptFd)
  7645. {
  7646. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  7647. //if it is not the same, the packet should be ignored.
  7648. //STEP 1: ============ Initialize ============
  7649. int i = 0;
  7650. int errn = 0;
  7651. bitstream_t v2g_tx_stream;
  7652. static struct CableCheckResponse_ISO15118_2014 *cab;
  7653. static struct ChargingInfoData *sys;
  7654. size_t pos = 0;
  7655. v2g_tx_stream.pos = &pos;
  7656. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7657. v2g_tx_stream.data = V2GTP_Tx_buf;
  7658. cab = &ShmCcsData->V2GMessage_ISO15118_2014.CableCheckResponse;
  7659. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7660. cab->ResponseCode = OK_ISO15118_2014;
  7661. cab->cnt++;
  7662. DEBUG_PRINTF_EVCOMM_DETAIL("CP_State = %d (%.02f V), V_now = %.02f, Isolation Status = %d, EVSEProcessing = %d\n",
  7663. sys->CpState,
  7664. sys->CpVoltage,
  7665. sys->PresentChargingVoltage,
  7666. sys->IsolationStatus,
  7667. cab->EVSEProcessing);
  7668. //[HEADER] Check Req SessionID
  7669. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  7670. {
  7671. cab->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  7672. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  7673. errn = -1;
  7674. }
  7675. //Check for SequenceError
  7676. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7677. {
  7678. cab->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  7679. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  7680. errn = -1;
  7681. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7682. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7683. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7684. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7685. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7686. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7687. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7688. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7689. }
  7690. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  7691. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  7692. {
  7693. //For PSU
  7694. sys->EvBatterytargetVoltage = CABLECHECK_TARGET_VOLTAGE; //500V //[To-Do] this should be modified to auto decision.
  7695. sys->EvBatterytargetCurrent = CABLECHECK_TARGET_CURRENT; //2A(default), unit: 1A
  7696. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  7697. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  7698. cab->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  7699. }
  7700. else if ((sys->IsolationStatus == GFD_Valid) || (sys->IsolationStatus == GFD_Warning)) //1: valid, 2:waring
  7701. {
  7702. //For PSU
  7703. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7704. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7705. if (sys->PresentChargingVoltage < 60) // < 60V
  7706. {
  7707. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  7708. sprintf(buf_log_evcomm, "[V2G][CableCheck]Pass (V_now = %.02f, Isolated = %d)",
  7709. sys->PresentChargingVoltage,
  7710. sys->IsolationStatus);
  7711. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7712. //IsolationStatus
  7713. if (sys->IsolationStatus == GFD_Valid)
  7714. {
  7715. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  7716. }
  7717. else if (sys->IsolationStatus == GFD_Warning)
  7718. {
  7719. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Warning;
  7720. }
  7721. else
  7722. {
  7723. sprintf(buf_log_evcomm,
  7724. "[WARNING]unexpected IsolationStatus(%d)",
  7725. sys->IsolationStatus);
  7726. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7727. }
  7728. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  7729. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7730. }
  7731. else
  7732. {
  7733. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  7734. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  7735. cab->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  7736. }
  7737. }
  7738. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  7739. {
  7740. //For PSU
  7741. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7742. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7743. cab->ResponseCode = FAILED_ISO15118_2014;
  7744. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7745. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_NotReady;
  7746. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7747. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_CableCheckRes]IsolationStatus = 3 (fault)");
  7748. errn = -1;
  7749. }
  7750. //else if (sys->IsolationStatus == GFD_No_IMD){} //only for ISO15118
  7751. else
  7752. {
  7753. //For PSU
  7754. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7755. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7756. cab->ResponseCode = FAILED_ISO15118_2014;
  7757. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7758. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_NotReady;
  7759. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7760. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_CableCheckRes]Undefined Isolation Status.");
  7761. }
  7762. //Check for CSU command of "Stop by EVSE"
  7763. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7764. {
  7765. cab->ResponseCode = FAILED_ISO15118_2014;
  7766. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7767. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7768. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7769. errn = -1;
  7770. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_CableCheckRes]EVSE_Shutdown");
  7771. }
  7772. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7773. {
  7774. cab->ResponseCode = FAILED_ISO15118_2014;
  7775. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7776. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7777. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7778. errn = -1;
  7779. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_CableCheckRes]EVSE_EmergencyShutdown");
  7780. }
  7781. else if (ShmInternalComm->ChargingPermission == FALSE)
  7782. {
  7783. cab->ResponseCode = FAILED_ISO15118_2014;
  7784. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7785. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7786. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7787. errn = -1;
  7788. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_iso1_CableCheckRes]ChargingPermission = FALSE");
  7789. }
  7790. else
  7791. {
  7792. //null
  7793. }
  7794. //Response to CP Error
  7795. #if CP_PROTECTION_MECHANISM == ENABLE
  7796. if (sys->CableCheckPreCountDownDone == TRUE) //[To-Do] Here should be modified to 2 seconds
  7797. {
  7798. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  7799. {
  7800. cab->ResponseCode = FAILED_ISO15118_2014;
  7801. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7802. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7803. Update_V2G_Flow_Status(Other_Fault);
  7804. errn = -1;
  7805. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7806. //CCS_SECC_CP_State_Error (023889)
  7807. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7808. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7809. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7810. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  7811. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  7812. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  7813. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  7814. sprintf(buf_log_evcomm,
  7815. "[CableCheck]Emergency Stop by CP Error (%d, %.02f V, %d)\n",
  7816. sys->CpState,
  7817. sys->CpVoltage,
  7818. cab->cnt
  7819. );
  7820. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7821. //cab->cnt = 0;
  7822. }
  7823. }
  7824. #endif
  7825. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7826. #if PARAMETER_NORMAL_MODE == ENABLE
  7827. SHM_Read_iso1_CableCheckRes(&ccs_exi_doc_ISO1, ShmCcsData);
  7828. #else
  7829. Sudo_Parameter_iso1_CableCheckRes();
  7830. #endif
  7831. //STEP 4: ============ Encode and Send Response Message ===========
  7832. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  7833. {
  7834. errn = -1;
  7835. }
  7836. //STEP 5: ============ Update Flags ===========
  7837. sys->ConnectorLocked = TRUE; //Inicating EVSE that the CCS Connector is Locked.
  7838. return errn;
  7839. }
  7840. /*===========================================================================
  7841. FUNCTION: Proc_din_CableCheckReq
  7842. DESCRIPTION:
  7843. PRE-CONDITION:
  7844. INPUT:
  7845. OUTPUT:
  7846. GLOBAL VARIABLES:
  7847. =============================================================================*/
  7848. int Proc_din_CableCheckReq(int AcceptFd)
  7849. {
  7850. int errn = 0;
  7851. //SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_CableCheckReq] Got CableCheckReq\n");
  7852. //Print the decoded XML Document
  7853. PRINT_XML_DOC_DIN_CableCheckReq(&ccs_exi_doc_DIN);
  7854. //Save into Share Memory
  7855. SHM_Save_din_CableCheckReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  7856. //Check for EV Error Code
  7857. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.CableCheckRequest.DC_EVStatus.EVErrorCode);
  7858. errn = Proc_din_CableCheckRes(AcceptFd);
  7859. if (errn == 0)
  7860. {
  7861. //send response successfully.
  7862. //SAVE_SYS_LOG_MSG_EVCOMM("[DIN][V2G][Tx]CableCheckRes");
  7863. }
  7864. else
  7865. {
  7866. sprintf(buf_log_evcomm,
  7867. "[Error][SeccComm][Proc_din_CableCheckReq]Proc_iso1_CableCheckRes(): %d (DEC)",
  7868. errn);
  7869. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7870. }
  7871. return errn;
  7872. }
  7873. /*===========================================================================
  7874. FUNCTION: Proc_iso1_CableCheckReq
  7875. DESCRIPTION:
  7876. PRE-CONDITION:
  7877. INPUT:
  7878. OUTPUT:
  7879. GLOBAL VARIABLES:
  7880. =============================================================================*/
  7881. int Proc_iso1_CableCheckReq(int AcceptFd)
  7882. {
  7883. int errn = 0;
  7884. //SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_CableCheckReq] Got CableCheckReq");
  7885. //Print the decoded XML Document
  7886. PRINT_XML_DOC_ISO1_CableCheckReq(&ccs_exi_doc_ISO1);
  7887. //Save into Share Memory
  7888. SHM_Save_iso1_CableCheckReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  7889. //Check for EV Error Code
  7890. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.CableCheckRequest.DC_EVStatus.EVErrorCode);
  7891. errn = Proc_iso1_CableCheckRes(AcceptFd);
  7892. if (errn == 0)
  7893. {
  7894. //send response successfully.
  7895. //SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]CableCheckRes");
  7896. }
  7897. else
  7898. {
  7899. sprintf(buf_log_evcomm,
  7900. "[Error][SeccComm][Proc_iso1_CableCheckReq]Proc_iso1_CableCheckRes(): %d (DEC)",
  7901. errn);
  7902. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  7903. }
  7904. return errn;
  7905. }
  7906. /*===========================================================================
  7907. FUNCTION: SHM_Init_din_PreChargeRes
  7908. DESCRIPTION:
  7909. PRE-CONDITION:
  7910. INPUT:
  7911. 1. shm_ccs
  7912. OUTPUT:
  7913. 1. shm_ccs
  7914. GLOBAL VARIABLES:
  7915. =============================================================================*/
  7916. void SHM_Init_din_PreChargeRes(struct CcsData *shm_ccs)
  7917. {
  7918. struct PreChargeResponse_DIN70121 *in;
  7919. in = &shm_ccs->V2GMessage_DIN70121.PreChargeResponse;
  7920. //----- [BODY (1/3)] ResponseCode -----
  7921. in->ResponseCode = dinresponseCodeType_OK;
  7922. //----- [BODY (2/3)] EVSEPresentVoltage -----
  7923. short value = 0; int multiplier = 0; unsigned char unit = 0;
  7924. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for Test
  7925. value = 0; multiplier = 0; unit = V_DIN70121; //waiting for CsuComm to update V to fit EV Target
  7926. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  7927. //----- [BODY (3/3)] DC_EVSEStatus -----
  7928. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  7929. }
  7930. /*===========================================================================
  7931. FUNCTION: Sudo_Parameter_din_PreChargeRes
  7932. DESCRIPTION:
  7933. PRE-CONDITION:
  7934. INPUT:
  7935. OUTPUT:
  7936. GLOBAL VARIABLES:
  7937. =============================================================================*/
  7938. void Sudo_Parameter_din_PreChargeRes()
  7939. {
  7940. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  7941. init_dinPreChargeResType(&ccs_exi_doc_DIN.V2G_Message.Body.PreChargeRes);
  7942. ccs_exi_doc_DIN.V2G_Message.Body.PreChargeRes_isUsed = 1u;
  7943. //----- [BODY (1/3)] ResponseCode -----
  7944. struct dinPreChargeResType *res;
  7945. res = &ccs_exi_doc_DIN.V2G_Message.Body.PreChargeRes;
  7946. res->ResponseCode = dinresponseCodeType_OK;
  7947. //----- [BODY (2/3)] EVSEPresentVoltage -----
  7948. res->EVSEPresentVoltage.Value = 3820;
  7949. res->EVSEPresentVoltage.Multiplier = -1;
  7950. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  7951. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  7952. // dinunitSymbolType_h = 0,
  7953. // dinunitSymbolType_m = 1,
  7954. // dinunitSymbolType_s = 2,
  7955. // dinunitSymbolType_A = 3,
  7956. // dinunitSymbolType_Ah = 4,
  7957. // dinunitSymbolType_V = 5,
  7958. // dinunitSymbolType_VA = 6,
  7959. // dinunitSymbolType_W = 7,
  7960. // dinunitSymbolType_W_s = 8,
  7961. // dinunitSymbolType_Wh = 9
  7962. //----- [BODY (3/3)] DC_EVSEStatus -----
  7963. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7964. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  7965. // dinisolationLevelType_Invalid = 0,
  7966. // dinisolationLevelType_Valid = 1,
  7967. // dinisolationLevelType_Warning = 2,
  7968. // dinisolationLevelType_Fault = 3
  7969. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7970. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7971. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  7972. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7973. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7974. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7975. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7976. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7977. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  7978. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  7979. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  7980. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  7981. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  7982. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7983. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  7984. // dinEVSENotificationType_None = 0,
  7985. // dinEVSENotificationType_StopCharging = 1,
  7986. // dinEVSENotificationType_ReNegotiation = 2
  7987. }
  7988. /*===========================================================================
  7989. FUNCTION: Sudo_Parameter_iso1_PreChargeRes
  7990. DESCRIPTION:
  7991. PRE-CONDITION:
  7992. INPUT:
  7993. OUTPUT:
  7994. GLOBAL VARIABLES:
  7995. =============================================================================*/
  7996. void Sudo_Parameter_iso1_PreChargeRes()
  7997. {
  7998. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  7999. init_iso1PreChargeResType(&ccs_exi_doc_ISO1.V2G_Message.Body.PreChargeRes);
  8000. ccs_exi_doc_ISO1.V2G_Message.Body.PreChargeRes_isUsed = 1u;
  8001. //----- [BODY (1/3)] ResponseCode -----
  8002. struct iso1PreChargeResType *res;
  8003. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PreChargeRes;
  8004. res->ResponseCode = iso1responseCodeType_OK;
  8005. //----- [BODY (2/3)] EVSEPresentVoltage -----
  8006. res->EVSEPresentVoltage.Value = 3820;
  8007. res->EVSEPresentVoltage.Multiplier = -1;
  8008. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  8009. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  8010. //iso1unitSymbolType_h = 0,
  8011. //iso1unitSymbolType_m = 1,
  8012. //iso1unitSymbolType_s = 2,
  8013. //iso1unitSymbolType_A = 3,
  8014. //iso1unitSymbolType_V = 4,
  8015. //iso1unitSymbolType_W = 5,
  8016. //iso1unitSymbolType_Wh = 6
  8017. //----- [BODY (3/3)] DC_EVSEStatus -----
  8018. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8019. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  8020. //iso1isolationLevelType_Invalid = 0,
  8021. //iso1isolationLevelType_Valid = 1,
  8022. //iso1isolationLevelType_Warning = 2,
  8023. //iso1isolationLevelType_Fault = 3,
  8024. //iso1isolationLevelType_No_IMD = 4
  8025. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8026. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8027. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  8028. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8029. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8030. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8031. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8032. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8033. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  8034. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  8035. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  8036. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  8037. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  8038. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8039. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8040. //iso1EVSENotificationType_None = 0,
  8041. //iso1EVSENotificationType_StopCharging = 1,
  8042. //iso1EVSENotificationType_ReNegotiation = 2
  8043. }
  8044. /*===========================================================================
  8045. FUNCTION: Proc_din_PreChargeRes
  8046. DESCRIPTION:
  8047. PRE-CONDITION:
  8048. INPUT:
  8049. OUTPUT:
  8050. GLOBAL VARIABLES:
  8051. =============================================================================*/
  8052. int Proc_din_PreChargeRes(int AcceptFd)
  8053. {
  8054. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8055. //if it is not the same, the packet should be ignored.
  8056. //STEP 1: ============ Initialize ============
  8057. int i = 0;
  8058. int errn = 0;
  8059. bitstream_t v2g_tx_stream;
  8060. static struct PreChargeResponse_DIN70121 *pre;
  8061. static struct ChargingInfoData *sys;
  8062. size_t pos = 0;
  8063. v2g_tx_stream.pos = &pos;
  8064. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8065. v2g_tx_stream.data = V2GTP_Tx_buf;
  8066. pre = &ShmCcsData->V2GMessage_DIN70121.PreChargeResponse;
  8067. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8068. pre->ResponseCode = OK_DIN70121;
  8069. //EVSE Status Code
  8070. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8071. //[HEADER] Check Req SessionID
  8072. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  8073. {
  8074. pre->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  8075. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  8076. errn = -1;
  8077. }
  8078. //Check for SequenceError
  8079. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8080. {
  8081. pre->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  8082. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  8083. errn = -1;
  8084. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8085. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8086. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8087. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8088. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8089. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8090. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8091. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8092. }
  8093. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8094. SAVE_PhysicalValueType_DIN70121(&pre->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  8095. //Isolation Status
  8096. if (sys->IsolationStatus == GFD_Invalid) //0: invalid
  8097. {
  8098. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  8099. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8100. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8101. Update_V2G_Flow_Status(Other_Fault);
  8102. errn = -1;
  8103. sprintf(buf_log_evcomm, "[Error][Proc_din_PreChargeRes]IsolationStatus = %d", sys->IsolationStatus);
  8104. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  8105. }
  8106. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  8107. {
  8108. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  8109. }
  8110. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  8111. {
  8112. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  8113. }
  8114. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  8115. {
  8116. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_din_PreChargeRes]GFD_Fault => Emergency Shutdown");
  8117. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  8118. pre->ResponseCode = FAILED_DIN70121;
  8119. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8120. errn = -1;
  8121. }
  8122. else //GFD_No_IMD or other unexpected status
  8123. {
  8124. pre->ResponseCode = FAILED_DIN70121;
  8125. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  8126. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8127. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8128. Update_V2G_Flow_Status(Other_Fault);
  8129. errn = -1;
  8130. sprintf(buf_log_evcomm,
  8131. "[Error][Proc_din_PreChargeRes]IsolationStatus = %d (undefined)",
  8132. sys->IsolationStatus);
  8133. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  8134. }
  8135. //Check for CSU command of "Stop by EVSE"
  8136. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8137. {
  8138. //Check for Alarm Code: CCS GFD trip (012235)
  8139. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8140. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8141. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8142. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8143. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8144. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8145. {
  8146. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_din_PreChargeRes]CCS GFD trip => EVSE_Shutdown");
  8147. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  8148. }
  8149. else
  8150. {
  8151. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_din_PreChargeRes]EVSE_Shutdown");
  8152. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8153. }
  8154. pre->ResponseCode = FAILED_DIN70121;
  8155. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8156. errn = -1;
  8157. }
  8158. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8159. {
  8160. //Check for Alarm Code: CCS GFD trip (012235)
  8161. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8162. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8163. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8164. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8165. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8166. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8167. {
  8168. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_din_PreChargeRes]CCS GFD trip => EVSE_EmergencyShutdown");
  8169. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  8170. }
  8171. else
  8172. {
  8173. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_din_PreChargeRes]EVSE_EmergencyShutdown");
  8174. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8175. }
  8176. pre->ResponseCode = FAILED_DIN70121;
  8177. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8178. errn = -1;
  8179. }
  8180. else if (ShmInternalComm->ChargingPermission == FALSE)
  8181. {
  8182. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_din_PreChargeRes]ChargingPermission = FALSE");
  8183. pre->ResponseCode = FAILED_DIN70121;
  8184. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8185. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8186. errn = -1;
  8187. }
  8188. else
  8189. {
  8190. //null
  8191. }
  8192. //Response to CP Error
  8193. #if CP_PROTECTION_MECHANISM == ENABLE
  8194. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  8195. {
  8196. pre->ResponseCode = FAILED_DIN70121;
  8197. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8198. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8199. Update_V2G_Flow_Status(Other_Fault);
  8200. errn = -1;
  8201. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8202. //CCS_SECC_CP_State_Error (023889)
  8203. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8204. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8205. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8206. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  8207. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  8208. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  8209. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  8210. sprintf(buf_log_evcomm,
  8211. "[Error][Proc_din_PreChargeRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  8212. sys->CpState,
  8213. sys->CpVoltage
  8214. );
  8215. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  8216. }
  8217. #endif
  8218. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8219. #if PARAMETER_NORMAL_MODE == ENABLE
  8220. SHM_Read_din_PreChargeRes(&ccs_exi_doc_DIN, ShmCcsData);
  8221. #else
  8222. Sudo_Parameter_din_PreChargeRes();
  8223. #endif
  8224. //STEP 4: ============ Encode and Send Response Message ===========
  8225. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  8226. {
  8227. SAVE_SYS_LOG_MSG_EVCOMM("[Error][send_encoded_din_V2GTP_Stream]:Tx encoded msg error");
  8228. errn = -1;
  8229. }
  8230. //STEP 5: ============ Update Flags ===========
  8231. return errn;
  8232. }
  8233. /*===========================================================================
  8234. FUNCTION: Proc_iso1_PreChargeRes
  8235. DESCRIPTION:
  8236. PRE-CONDITION:
  8237. INPUT:
  8238. OUTPUT:
  8239. GLOBAL VARIABLES:
  8240. =============================================================================*/
  8241. int Proc_iso1_PreChargeRes(int AcceptFd)
  8242. {
  8243. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8244. //if it is not the same, the packet should be ignored.
  8245. //STEP 1: ============ Initialize ============
  8246. int i = 0;
  8247. int errn = 0;
  8248. bitstream_t v2g_tx_stream;
  8249. static struct PreChargeResponse_ISO15118_2014 *pre;
  8250. static struct ChargingInfoData *sys;
  8251. size_t pos = 0;
  8252. v2g_tx_stream.pos = &pos;
  8253. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8254. v2g_tx_stream.data = V2GTP_Tx_buf;
  8255. pre = &ShmCcsData->V2GMessage_ISO15118_2014.PreChargeResponse;
  8256. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8257. pre->ResponseCode = OK_ISO15118_2014;
  8258. //EVSE Status Code
  8259. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8260. //[HEADER] Check Req SessionID
  8261. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  8262. {
  8263. pre->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  8264. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  8265. errn = -1;
  8266. }
  8267. //Check for SequenceError
  8268. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8269. {
  8270. pre->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  8271. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  8272. errn = -1;
  8273. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8274. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8275. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8276. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8277. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8278. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8279. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8280. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8281. }
  8282. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8283. SAVE_PhysicalValueType_ISO15118_2014(&pre->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  8284. //Isolation Status
  8285. if (sys->IsolationStatus == GFD_Invalid) //0: invalid(on going)
  8286. {
  8287. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  8288. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8289. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8290. Update_V2G_Flow_Status(Other_Fault);
  8291. errn = -1;
  8292. sprintf(buf_log_evcomm, "[Error][Proc_iso1_PreChargeRes]IsolationStatus = %d", sys->IsolationStatus);
  8293. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  8294. }
  8295. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  8296. {
  8297. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  8298. }
  8299. else if (sys->IsolationStatus == GFD_Warning) //2: waring
  8300. {
  8301. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Warning; //1
  8302. }
  8303. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  8304. {
  8305. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_iso1_PreChargeRes]GFD_Fault => Emergency Shutdown");
  8306. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  8307. pre->ResponseCode = FAILED_ISO15118_2014;
  8308. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8309. errn = -1;
  8310. }
  8311. else
  8312. {
  8313. pre->ResponseCode = FAILED_ISO15118_2014;
  8314. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  8315. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8316. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8317. Update_V2G_Flow_Status(Other_Fault);
  8318. errn = -1;
  8319. sprintf(buf_log_evcomm,
  8320. "[Error][Proc_iso1_PreChargeRes]IsolationStatus = %d (undefined)",
  8321. sys->IsolationStatus);
  8322. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  8323. }
  8324. //Check for CSU command of "Stop by EVSE"
  8325. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8326. {
  8327. //Check for Alarm Code: CCS GFD trip (012235)
  8328. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8329. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8330. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8331. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8332. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8333. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8334. {
  8335. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_iso1_PreChargeRes]CCS GFD trip => EVSE_Shutdown");
  8336. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  8337. }
  8338. else
  8339. {
  8340. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_iso1_PreChargeRes]EVSE_Shutdown");
  8341. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8342. }
  8343. pre->ResponseCode = FAILED_ISO15118_2014;
  8344. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8345. errn = -1;
  8346. }
  8347. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8348. {
  8349. //Check for Alarm Code: CCS GFD trip (012235)
  8350. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8351. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8352. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8353. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8354. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8355. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8356. {
  8357. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_iso1_PreChargeRes]CCS GFD trip => EVSE_EmergencyShutdown");
  8358. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  8359. }
  8360. else
  8361. {
  8362. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_iso1_PreChargeRes]EVSE_EmergencyShutdown");
  8363. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8364. }
  8365. pre->ResponseCode = FAILED_ISO15118_2014;
  8366. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8367. errn = -1;
  8368. }
  8369. else if (ShmInternalComm->ChargingPermission == FALSE)
  8370. {
  8371. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_iso1_PreChargeRes]ChargingPermission = FALSE");
  8372. pre->ResponseCode = FAILED_ISO15118_2014;
  8373. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8374. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8375. errn = -1;
  8376. }
  8377. else
  8378. {
  8379. //null
  8380. }
  8381. //Response to CP Error
  8382. #if CP_PROTECTION_MECHANISM == ENABLE
  8383. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  8384. {
  8385. pre->ResponseCode = FAILED_ISO15118_2014;
  8386. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8387. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8388. Update_V2G_Flow_Status(Other_Fault);
  8389. errn = -1;
  8390. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8391. //CCS_SECC_CP_State_Error (023889)
  8392. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8393. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8394. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8395. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  8396. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  8397. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  8398. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  8399. sprintf(buf_log_evcomm,
  8400. "[Error][Proc_iso1_PreChargeRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  8401. sys->CpState,
  8402. sys->CpVoltage
  8403. );
  8404. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  8405. }
  8406. #endif
  8407. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8408. #if PARAMETER_NORMAL_MODE == ENABLE
  8409. SHM_Read_iso1_PreChargeRes(&ccs_exi_doc_ISO1, ShmCcsData);
  8410. #else
  8411. Sudo_Parameter_iso1_PreChargeRes();
  8412. #endif
  8413. //STEP 4: ============ Encode and Send Response Message ===========
  8414. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  8415. {
  8416. SAVE_SYS_LOG_MSG_EVCOMM("[Error][send_encoded_iso1_V2GTP_Stream]:Tx encoded msg error");
  8417. errn = -1;
  8418. }
  8419. //STEP 5: ============ Update Flags ===========
  8420. return errn;
  8421. }
  8422. /*===========================================================================
  8423. FUNCTION: Proc_din_PreChargeReq
  8424. DESCRIPTION:
  8425. PRE-CONDITION:
  8426. INPUT:
  8427. OUTPUT:
  8428. GLOBAL VARIABLES:
  8429. =============================================================================*/
  8430. int Proc_din_PreChargeReq(int AcceptFd)
  8431. {
  8432. int errn = 0;
  8433. //SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_PreChargeReq] Got PreChargeReq");
  8434. //Print the decoded XML Document
  8435. PRINT_XML_DOC_DIN_PreChargeReq(&ccs_exi_doc_DIN);
  8436. //Save into Share Memory
  8437. SHM_Save_din_PreChargeReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  8438. //Check for EV Error Code
  8439. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PreChargeRequest.DC_EVStatus.EVErrorCode);
  8440. errn = Proc_din_PreChargeRes(AcceptFd);
  8441. if (errn == 0)
  8442. {
  8443. //send response successfully.
  8444. //SAVE_SYS_LOG_MSG_EVCOMM("[DIN][V2G][Tx]PreChargeRes");
  8445. }
  8446. else
  8447. {
  8448. sprintf(buf_log_evcomm,
  8449. "[Error][SeccComm][Proc_din_PreChargeReq]Proc_iso1_PreChargeRes(): %d (DEC)",
  8450. errn);
  8451. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  8452. }
  8453. return errn;
  8454. }
  8455. /*===========================================================================
  8456. FUNCTION: Proc_iso1_PreChargeReq
  8457. DESCRIPTION:
  8458. PRE-CONDITION:
  8459. INPUT:
  8460. OUTPUT:
  8461. GLOBAL VARIABLES:
  8462. =============================================================================*/
  8463. int Proc_iso1_PreChargeReq(int AcceptFd)
  8464. {
  8465. int errn = 0;
  8466. //SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_PreChargeReq] Got PreChargeReq");
  8467. //Print the decoded XML Document
  8468. PRINT_XML_DOC_ISO1_PreChargeReq(&ccs_exi_doc_ISO1);
  8469. //Save into Share Memory
  8470. SHM_Save_iso1_PreChargeReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  8471. //Check for EV Error Code
  8472. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PreChargeRequest.DC_EVStatus.EVErrorCode);
  8473. errn = Proc_iso1_PreChargeRes(AcceptFd);
  8474. if (errn == 0)
  8475. {
  8476. //send response successfully.
  8477. //SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]PreChargeRes");
  8478. }
  8479. else
  8480. {
  8481. sprintf(buf_log_evcomm,
  8482. "[Error][SeccComm][Proc_iso1_PreChargeReq]Proc_iso1_PreChargeRes(): %d (DEC)",
  8483. errn);
  8484. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  8485. }
  8486. return errn;
  8487. }
  8488. /*===========================================================================
  8489. FUNCTION: SHM_Init_din_PowerDeliveryRes
  8490. DESCRIPTION:
  8491. PRE-CONDITION:
  8492. INPUT:
  8493. 1. shm_ccs
  8494. OUTPUT:
  8495. 1. shm_ccs
  8496. GLOBAL VARIABLES:
  8497. =============================================================================*/
  8498. void SHM_Init_din_PowerDeliveryRes(struct CcsData *shm_ccs)
  8499. {
  8500. struct PowerDeliveryResponse_DIN70121 *in;
  8501. in = &shm_ccs->V2GMessage_DIN70121.PowerDeliveryResponse;
  8502. //----- [BODY (1/3)] ResponseCode -----
  8503. in->ResponseCode = dinresponseCodeType_OK;
  8504. //----- [BODY (2/3)] AC_EVSEStatus -----
  8505. //ignore, since DIN 70121 doesn't support AC, yet.
  8506. //----- [BODY (2/3)] DC_EVSEStatus -----
  8507. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  8508. }
  8509. /*===========================================================================
  8510. FUNCTION: Sudo_Parameter_din_PowerDeliveryRes
  8511. DESCRIPTION:
  8512. PRE-CONDITION:
  8513. INPUT:
  8514. OUTPUT:
  8515. GLOBAL VARIABLES:
  8516. =============================================================================*/
  8517. void Sudo_Parameter_din_PowerDeliveryRes()
  8518. {
  8519. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  8520. init_dinPowerDeliveryResType(&ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes);
  8521. ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes_isUsed = 1u;
  8522. //----- [BODY (1/3)] ResponseCode -----
  8523. struct dinPowerDeliveryResType *res;
  8524. res = &ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes;
  8525. res->ResponseCode = dinresponseCodeType_OK;
  8526. //----- [BODY (2/3)] AC_EVSEStatus -----
  8527. //ignore, since DIN 70121 doesn't support AC, yet.
  8528. //----- [BODY (2/3)] DC_EVSEStatus -----
  8529. res->DC_EVSEStatus_isUsed = 1u;
  8530. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8531. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  8532. // dinisolationLevelType_Invalid = 0,
  8533. // dinisolationLevelType_Valid = 1,
  8534. // dinisolationLevelType_Warning = 2,
  8535. // dinisolationLevelType_Fault = 3
  8536. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8537. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8538. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  8539. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8540. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8541. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8542. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8543. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8544. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  8545. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  8546. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  8547. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  8548. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  8549. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8550. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  8551. // dinEVSENotificationType_None = 0,
  8552. // dinEVSENotificationType_StopCharging = 1,
  8553. // dinEVSENotificationType_ReNegotiation = 2
  8554. }
  8555. /*===========================================================================
  8556. FUNCTION: Sudo_Parameter_iso1_PowerDeliveryRes
  8557. DESCRIPTION:
  8558. PRE-CONDITION:
  8559. INPUT:
  8560. OUTPUT:
  8561. GLOBAL VARIABLES:
  8562. =============================================================================*/
  8563. void Sudo_Parameter_iso1_PowerDeliveryRes()
  8564. {
  8565. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  8566. init_iso1PowerDeliveryResType(&ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes);
  8567. ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes_isUsed = 1u;
  8568. //----- [BODY (1/3)] ResponseCode -----
  8569. struct iso1PowerDeliveryResType *res;
  8570. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes;
  8571. res->ResponseCode = iso1responseCodeType_OK;
  8572. //----- [BODY (2/3)] AC_EVSEStatus -----
  8573. //ignore, since our ISO1 70121 doesn't support AC, yet.
  8574. //----- [BODY (2/3)] DC_EVSEStatus -----
  8575. res->DC_EVSEStatus_isUsed = 1u;
  8576. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8577. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  8578. //iso1isolationLevelType_Invalid = 0,
  8579. //iso1isolationLevelType_Valid = 1,
  8580. //iso1isolationLevelType_Warning = 2,
  8581. //iso1isolationLevelType_Fault = 3,
  8582. //iso1isolationLevelType_No_IMD = 4
  8583. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8584. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8585. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  8586. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8587. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8588. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8589. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8590. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8591. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  8592. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  8593. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  8594. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  8595. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  8596. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8597. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8598. //iso1EVSENotificationType_None = 0,
  8599. //iso1EVSENotificationType_StopCharging = 1,
  8600. //iso1EVSENotificationType_ReNegotiation = 2
  8601. }
  8602. /*===========================================================================
  8603. FUNCTION: Proc_din_PowerDeliveryStartRes
  8604. DESCRIPTION:
  8605. PRE-CONDITION:
  8606. INPUT:
  8607. OUTPUT:
  8608. GLOBAL VARIABLES:
  8609. =============================================================================*/
  8610. int Proc_din_PowerDeliveryStartRes(int AcceptFd)
  8611. {
  8612. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8613. //if it is not the same, the packet should be ignored.
  8614. //STEP 1: ============ Initialize ============
  8615. int i = 0;
  8616. int errn = 0;
  8617. bitstream_t v2g_tx_stream;
  8618. struct dinPowerDeliveryResType *res;
  8619. struct ChargingInfoData *sys;
  8620. size_t pos = 0;
  8621. v2g_tx_stream.pos = &pos;
  8622. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8623. v2g_tx_stream.data = V2GTP_Tx_buf;
  8624. res = &ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes;
  8625. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8626. res->ResponseCode = OK_DIN70121;
  8627. //[HEADER] Check Req SessionID
  8628. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  8629. {
  8630. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  8631. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  8632. errn = -1;
  8633. }
  8634. //Check for SequenceError
  8635. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8636. {
  8637. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  8638. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  8639. errn = -1;
  8640. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8641. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8642. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8643. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8644. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8645. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8646. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8647. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8648. }
  8649. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8650. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8651. #if PARAMETER_NORMAL_MODE == ENABLE
  8652. SHM_Read_din_PowerDeliveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  8653. #else
  8654. Sudo_Parameter_din_PowerDeliveryRes();
  8655. #endif
  8656. //EVSE Status Code
  8657. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  8658. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8659. //Check for CSU command of "Stop by EVSE"
  8660. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8661. {
  8662. //res->ResponseCode = FAILED_DIN70121;
  8663. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8664. }
  8665. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8666. {
  8667. //res->ResponseCode = FAILED_DIN70121;
  8668. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8669. }
  8670. else if (ShmInternalComm->ChargingPermission == FALSE)
  8671. {
  8672. //res->ResponseCode = FAILED_DIN70121;
  8673. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8674. }
  8675. //STEP 4: ============ Encode and Send Response Message ===========
  8676. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  8677. {
  8678. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_PowerDeliveryStartRes][Error]Tx encoded msg error");
  8679. errn = -1;
  8680. }
  8681. //STEP 5: ============ Update Flags ===========
  8682. return errn;
  8683. }
  8684. /*===========================================================================
  8685. FUNCTION: Proc_iso1_PowerDeliveryStartRes
  8686. DESCRIPTION:
  8687. PRE-CONDITION:
  8688. INPUT:
  8689. OUTPUT:
  8690. GLOBAL VARIABLES:
  8691. =============================================================================*/
  8692. int Proc_iso1_PowerDeliveryStartRes(int AcceptFd)
  8693. {
  8694. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8695. //if it is not the same, the packet should be ignored.
  8696. //STEP 1: ============ Initialize ============
  8697. int i = 0;
  8698. int errn = 0;
  8699. bitstream_t v2g_tx_stream;
  8700. struct iso1PowerDeliveryResType *res;
  8701. struct ChargingInfoData *sys;
  8702. size_t pos = 0;
  8703. v2g_tx_stream.pos = &pos;
  8704. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8705. v2g_tx_stream.data = V2GTP_Tx_buf;
  8706. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes;
  8707. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8708. res->ResponseCode = OK_ISO15118_2014;
  8709. //[HEADER] Check Req SessionID
  8710. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  8711. {
  8712. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  8713. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  8714. errn = -1;
  8715. }
  8716. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8717. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8718. #if PARAMETER_NORMAL_MODE == ENABLE
  8719. SHM_Read_iso1_PowerDeliveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  8720. #else
  8721. Sudo_Parameter_iso1_PowerDeliveryRes();
  8722. #endif
  8723. //EVSE Status Code
  8724. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  8725. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8726. //Check for CSU command of "Stop by EVSE"
  8727. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8728. {
  8729. //res->ResponseCode = FAILED_ISO15118_2014;
  8730. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8731. }
  8732. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8733. {
  8734. //res->ResponseCode = FAILED_ISO15118_2014;
  8735. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8736. }
  8737. else if (ShmInternalComm->ChargingPermission == FALSE)
  8738. {
  8739. //res->ResponseCode = FAILED_ISO15118_2014;
  8740. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8741. }
  8742. //STEP 4: ============ Encode and Send Response Message ===========
  8743. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  8744. {
  8745. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_PowerDeliveryStartRes][Error]Tx encoded msg error");
  8746. errn = -1;
  8747. }
  8748. //STEP 5: ============ Update Flags ===========
  8749. return errn;
  8750. }
  8751. /*===========================================================================
  8752. FUNCTION: Proc_din_PowerDeliveryStartReq
  8753. DESCRIPTION:
  8754. PRE-CONDITION:
  8755. INPUT:
  8756. OUTPUT:
  8757. GLOBAL VARIABLES:
  8758. =============================================================================*/
  8759. int Proc_din_PowerDeliveryStartReq(int AcceptFd)
  8760. {
  8761. int errn = 0;
  8762. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]PowerDeliveryStartReq");
  8763. //Print the decoded XML Document
  8764. PRINT_XML_DOC_DIN_PowerDeliveryReq(&ccs_exi_doc_DIN);
  8765. //Save into Share Memory
  8766. SHM_Save_din_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  8767. //Check for EV Error Code
  8768. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  8769. errn = Proc_din_PowerDeliveryStartRes(AcceptFd);
  8770. if (errn == 0)
  8771. {
  8772. //send response successfully.
  8773. SAVE_SYS_LOG_MSG_EVCOMM("[DIN][V2G][Tx]PowerDeliveryStartRes");
  8774. }
  8775. else
  8776. {
  8777. sprintf(buf_log_evcomm,
  8778. "[Error][Proc_din_PowerDeliveryStartReq]Proc_iso1_PowerDeliveryStartRes(): %d (DEC)",
  8779. errn);
  8780. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  8781. }
  8782. return errn;
  8783. }
  8784. /*===========================================================================
  8785. FUNCTION: Proc_iso1_PowerDeliveryStartReq
  8786. DESCRIPTION:
  8787. PRE-CONDITION:
  8788. INPUT:
  8789. OUTPUT:
  8790. GLOBAL VARIABLES:
  8791. =============================================================================*/
  8792. int Proc_iso1_PowerDeliveryStartReq(int AcceptFd)
  8793. {
  8794. int errn = 0;
  8795. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]PowerDeliveryStartReq");
  8796. //Print the decoded XML Document
  8797. PRINT_XML_DOC_ISO1_PowerDeliveryReq(&ccs_exi_doc_ISO1);
  8798. //Save into Share Memory
  8799. SHM_Save_iso1_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  8800. //Check for EV Error Code
  8801. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  8802. errn = Proc_iso1_PowerDeliveryStartRes(AcceptFd);
  8803. if (errn == 0)
  8804. {
  8805. //send response successfully.
  8806. SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]PowerDeliveryStartRes");
  8807. }
  8808. else
  8809. {
  8810. sprintf(buf_log_evcomm,
  8811. "[Error][Proc_iso1_PowerDeliveryStartReq]Proc_iso1_PowerDeliveryStartRes(): %d (DEC)",
  8812. errn);
  8813. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  8814. }
  8815. return errn;
  8816. }
  8817. /*===========================================================================
  8818. FUNCTION: SHM_Init_din_CurrentDemandRes
  8819. DESCRIPTION:
  8820. PRE-CONDITION:
  8821. INPUT:
  8822. 1. shm_ccs
  8823. OUTPUT:
  8824. 1. shm_ccs
  8825. GLOBAL VARIABLES:
  8826. =============================================================================*/
  8827. void SHM_Init_din_CurrentDemandRes(struct CcsData *shm_ccs)
  8828. {
  8829. struct CurrentDemandResponse_DIN70121 *in;
  8830. in = &shm_ccs->V2GMessage_DIN70121.CurrentDemandResponse;
  8831. //----- [BODY (1/10)] ResponseCode -----
  8832. in->ResponseCode = dinresponseCodeType_OK;
  8833. //----- [BODY (2/10)] DC_EVSEStatus -----
  8834. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  8835. //----- [BODY (3/10)] EVSEPresentVoltage -----
  8836. short value = 0; int multiplier = 0; unsigned char unit = 0;
  8837. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for test only.
  8838. value = 0; multiplier = 0; unit = V_DIN70121;
  8839. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  8840. //----- [BODY (4/10)] EVSEPresentCurrent -----
  8841. //value = 600; multiplier = -1; unit = A_DIN70121; //60A, for test only.
  8842. value = 0; multiplier = 0; unit = A_DIN70121;
  8843. SHM_Save_dinPhysicalValueType(&in->EVSEPresentCurrent, value, multiplier, unit);
  8844. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  8845. in->EVSECurrentLimitAchieved = FALSE;
  8846. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  8847. in->EVSEVoltageLimitAchieved = FALSE;
  8848. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  8849. in->EVSEPowerLimitAchieved = FALSE;
  8850. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  8851. //value = 7500; multiplier = -1; unit = V_DIN70121; //750V
  8852. value = 5000; multiplier = -1; unit = V_DIN70121; //500V
  8853. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumVoltageLimit, value, multiplier, unit);
  8854. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  8855. value = 600; multiplier = -1; unit = A_DIN70121; //60A
  8856. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumCurrentLimit, value, multiplier, unit);
  8857. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  8858. value = 3000; multiplier = 1; unit = W_DIN70121; //30KW
  8859. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumPowerLimit, value, multiplier, unit);
  8860. }
  8861. /*===========================================================================
  8862. FUNCTION: Sudo_Parameter_din_CurrentDemandRes
  8863. DESCRIPTION:
  8864. PRE-CONDITION:
  8865. INPUT:
  8866. OUTPUT:
  8867. GLOBAL VARIABLES:
  8868. =============================================================================*/
  8869. void Sudo_Parameter_din_CurrentDemandRes()
  8870. {
  8871. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  8872. init_dinCurrentDemandResType(&ccs_exi_doc_DIN.V2G_Message.Body.CurrentDemandRes);
  8873. ccs_exi_doc_DIN.V2G_Message.Body.CurrentDemandRes_isUsed = 1u;
  8874. //----- [BODY (1/10)] ResponseCode -----
  8875. struct dinCurrentDemandResType *res;
  8876. res = &ccs_exi_doc_DIN.V2G_Message.Body.CurrentDemandRes;
  8877. res->ResponseCode = dinresponseCodeType_OK;
  8878. //----- [BODY (2/10)] DC_EVSEStatus -----
  8879. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8880. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  8881. // dinisolationLevelType_Invalid = 0,
  8882. // dinisolationLevelType_Valid = 1,
  8883. // dinisolationLevelType_Warning = 2,
  8884. // dinisolationLevelType_Fault = 3
  8885. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8886. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8887. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  8888. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8889. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8890. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8891. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8892. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8893. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  8894. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  8895. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  8896. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  8897. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  8898. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8899. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  8900. // dinEVSENotificationType_None = 0,
  8901. // dinEVSENotificationType_StopCharging = 1,
  8902. // dinEVSENotificationType_ReNegotiation = 2
  8903. //----- [BODY (3/10)] EVSEPresentVoltage -----
  8904. res->EVSEPresentVoltage.Value = 3820;
  8905. res->EVSEPresentVoltage.Multiplier = -1;
  8906. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  8907. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  8908. //----- [BODY (4/10)] EVSEPresentCurrent -----
  8909. res->EVSEPresentCurrent.Value = 1200;
  8910. res->EVSEPresentCurrent.Multiplier = -1;
  8911. res->EVSEPresentCurrent.Unit_isUsed = 1u;
  8912. res->EVSEPresentCurrent.Unit = dinunitSymbolType_A;
  8913. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  8914. res->EVSECurrentLimitAchieved = 0;
  8915. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  8916. res->EVSEVoltageLimitAchieved = 0;
  8917. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  8918. res->EVSEPowerLimitAchieved = 0;
  8919. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  8920. res->EVSEMaximumVoltageLimit_isUsed = 1u;
  8921. res->EVSEMaximumVoltageLimit.Value = 7500;
  8922. res->EVSEMaximumVoltageLimit.Multiplier = -1;
  8923. res->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  8924. res->EVSEMaximumVoltageLimit.Unit = dinunitSymbolType_V;
  8925. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  8926. res->EVSEMaximumCurrentLimit_isUsed = 1u;
  8927. res->EVSEMaximumCurrentLimit.Value = 1200;
  8928. res->EVSEMaximumCurrentLimit.Multiplier = -1;
  8929. res->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  8930. res->EVSEMaximumCurrentLimit.Unit = dinunitSymbolType_A;
  8931. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  8932. res->EVSEMaximumPowerLimit_isUsed = 1u;
  8933. res->EVSEMaximumPowerLimit.Value = 6000;
  8934. res->EVSEMaximumPowerLimit.Multiplier = 1;
  8935. res->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  8936. res->EVSEMaximumPowerLimit.Unit = dinunitSymbolType_W;
  8937. }
  8938. /*===========================================================================
  8939. FUNCTION: Sudo_Parameter_iso1_CurrentDemandRes
  8940. DESCRIPTION:
  8941. PRE-CONDITION:
  8942. INPUT:
  8943. OUTPUT:
  8944. GLOBAL VARIABLES:
  8945. =============================================================================*/
  8946. void Sudo_Parameter_iso1_CurrentDemandRes()
  8947. {
  8948. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  8949. init_iso1CurrentDemandResType(&ccs_exi_doc_ISO1.V2G_Message.Body.CurrentDemandRes);
  8950. ccs_exi_doc_ISO1.V2G_Message.Body.CurrentDemandRes_isUsed = 1u;
  8951. //----- [BODY (1/10)] ResponseCode -----
  8952. struct iso1CurrentDemandResType *res;
  8953. res = &ccs_exi_doc_ISO1.V2G_Message.Body.CurrentDemandRes;
  8954. res->ResponseCode = iso1responseCodeType_OK;
  8955. //----- [BODY (2/10)] DC_EVSEStatus -----
  8956. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8957. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  8958. //iso1isolationLevelType_Invalid = 0,
  8959. //iso1isolationLevelType_Valid = 1,
  8960. //iso1isolationLevelType_Warning = 2,
  8961. //iso1isolationLevelType_Fault = 3,
  8962. //iso1isolationLevelType_No_IMD = 4
  8963. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8964. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8965. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  8966. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8967. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8968. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8969. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8970. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8971. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  8972. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  8973. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  8974. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  8975. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  8976. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8977. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8978. //iso1EVSENotificationType_None = 0,
  8979. //iso1EVSENotificationType_StopCharging = 1,
  8980. //iso1EVSENotificationType_ReNegotiation = 2
  8981. //----- [BODY (3/10)] EVSEPresentVoltage -----
  8982. res->EVSEPresentVoltage.Value = 3820;
  8983. res->EVSEPresentVoltage.Multiplier = -1;
  8984. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  8985. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  8986. //----- [BODY (4/10)] EVSEPresentCurrent -----
  8987. res->EVSEPresentCurrent.Value = 1200;
  8988. res->EVSEPresentCurrent.Multiplier = -1;
  8989. //res->EVSEPresentCurrent.Unit_isUsed = 1u;
  8990. res->EVSEPresentCurrent.Unit = iso1unitSymbolType_A;
  8991. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  8992. res->EVSECurrentLimitAchieved = 0;
  8993. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  8994. res->EVSEVoltageLimitAchieved = 0;
  8995. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  8996. res->EVSEPowerLimitAchieved = 0;
  8997. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  8998. res->EVSEMaximumVoltageLimit_isUsed = 1u;
  8999. res->EVSEMaximumVoltageLimit.Value = 7500;
  9000. res->EVSEMaximumVoltageLimit.Multiplier = -1;
  9001. //res->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  9002. res->EVSEMaximumVoltageLimit.Unit = iso1unitSymbolType_V;
  9003. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  9004. res->EVSEMaximumCurrentLimit_isUsed = 1u;
  9005. res->EVSEMaximumCurrentLimit.Value = 1200;
  9006. res->EVSEMaximumCurrentLimit.Multiplier = -1;
  9007. //res->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  9008. res->EVSEMaximumCurrentLimit.Unit = iso1unitSymbolType_A;
  9009. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  9010. res->EVSEMaximumPowerLimit_isUsed = 1u;
  9011. res->EVSEMaximumPowerLimit.Value = 6000;
  9012. res->EVSEMaximumPowerLimit.Multiplier = 1;
  9013. //res->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  9014. res->EVSEMaximumPowerLimit.Unit = iso1unitSymbolType_W;
  9015. }
  9016. /*===========================================================================
  9017. FUNCTION: Sudo_Parameter_iso1_ChargingStatusRes
  9018. DESCRIPTION:
  9019. PRE-CONDITION:
  9020. INPUT:
  9021. OUTPUT:
  9022. GLOBAL VARIABLES:
  9023. =============================================================================*/
  9024. void Sudo_Parameter_iso1_ChargingStatusRes()
  9025. {
  9026. int i = 0;
  9027. struct iso1ChargingStatusResType *res;
  9028. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  9029. init_iso1ChargingStatusResType(&ccs_exi_doc_ISO1.V2G_Message.Body.ChargingStatusRes);
  9030. res = &ccs_exi_doc_ISO1.V2G_Message.Body.ChargingStatusRes;
  9031. ccs_exi_doc_ISO1.V2G_Message.Body.ChargingStatusRes_isUsed = 1u;
  9032. //----- [BODY (1/10)] ResponseCode -----
  9033. res->ResponseCode = OK_ISO15118_2014;
  9034. //----- [BODY (2/10)] AC_EVSEStatus -----
  9035. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  9036. res->AC_EVSEStatus.NotificationMaxDelay = 0;
  9037. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  9038. // iso1EVSENotificationType_None = 0,
  9039. // iso1EVSENotificationType_StopCharging = 1,
  9040. // iso1EVSENotificationType_ReNegotiation = 2
  9041. //----- [BODY (3/10)] EVSEMaxCurrent -----
  9042. res->EVSEMaxCurrent_isUsed = 1u;
  9043. res->EVSEMaxCurrent.Value = 32;
  9044. res->EVSEMaxCurrent.Multiplier = 0;
  9045. res->EVSEMaxCurrent.Unit = iso1unitSymbolType_A;
  9046. //----- [BODY (4/10)] SAScheduleTupleID -----
  9047. res->SAScheduleTupleID = 0;
  9048. //----- [BODY (5/10)] EVSEID -----
  9049. res->EVSEID.charactersLen = 37;
  9050. memset(res->EVSEID.characters, 0, sizeof(res->EVSEID.characters));
  9051. //sprintf(res->EVSEID.characters, CCS_AC_EVSEID);
  9052. //----- [BODY (6/10)] MeterInfo -----
  9053. res->MeterInfo_isUsed = 1u;
  9054. memset(res->MeterInfo.MeterID.characters, 0, sizeof(res->MeterInfo.MeterID.characters));
  9055. memset(res->MeterInfo.SigMeterReading.bytes, 0, sizeof(res->MeterInfo.SigMeterReading.bytes));
  9056. //[MeterInfo][1/5] MeterID
  9057. //sprintf(res->MeterInfo.MeterID , CCS_AC_METER_ID);
  9058. //[MeterInfo][2/5] SigMeterReading (optional)
  9059. //sprintf(res->MeterInfo.SigMeterReading , CCS_AC_SIG_METER_READING);
  9060. //[MeterInfo][3/5] MeterStatus (optional)
  9061. res->MeterInfo.MeterStatus = 0;
  9062. //[MeterInfo][4/5] MeterReading (optional)
  9063. res->MeterInfo.MeterReading = 12345;
  9064. //[MeterInfo][5/5] TMeter (optional)
  9065. res->MeterInfo.TMeter = 1586243587; //Unix Time Stamp format
  9066. //----- [BODY (7/10)] ReceiptRequired -----
  9067. res->ReceiptRequired_isUsed = 1u;
  9068. res->ReceiptRequired = FALSE; //optional
  9069. }
  9070. /*===========================================================================
  9071. FUNCTION: Check_EVErrorCode
  9072. DESCRIPTION:
  9073. PRE-CONDITION:
  9074. INPUT:
  9075. OUTPUT:
  9076. GLOBAL VARIABLES:
  9077. =============================================================================*/
  9078. void Check_EVErrorCode(int code)
  9079. {
  9080. if (code != NO_ERROR) //NO_ERROR = 0
  9081. {
  9082. //Asking CSU to Stop
  9083. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9084. //Update_V2G_Flow_Status(Other_Fault);
  9085. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  9086. sprintf(buf_log_evcomm,
  9087. "Stop by EV (EVErrorCode = %d (DEC))",
  9088. code);
  9089. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9090. }
  9091. }
  9092. /*===========================================================================
  9093. FUNCTION: Proc_din_CurrentDemandRes
  9094. DESCRIPTION:
  9095. PRE-CONDITION:
  9096. INPUT:
  9097. OUTPUT:
  9098. GLOBAL VARIABLES:
  9099. =============================================================================*/
  9100. int Proc_din_CurrentDemandRes(int AcceptFd)
  9101. {
  9102. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9103. //if it is not the same, the packet should be ignored.
  9104. //STEP 1: ============ Initialize ============
  9105. int i = 0;
  9106. int errn = 0;
  9107. bitstream_t v2g_tx_stream;
  9108. static struct CurrentDemandResponse_DIN70121 *cur;
  9109. static struct ChargingInfoData *sys;
  9110. static int EVSE_max_current;
  9111. int tmp = 0;
  9112. size_t pos = 0;
  9113. v2g_tx_stream.pos = &pos;
  9114. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9115. v2g_tx_stream.data = V2GTP_Tx_buf;
  9116. cur = &ShmCcsData->V2GMessage_DIN70121.CurrentDemandResponse;
  9117. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9118. cur->ResponseCode = OK_DIN70121;
  9119. //EVSE Status Code
  9120. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  9121. //[HEADER] Check Req SessionID
  9122. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  9123. {
  9124. cur->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  9125. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  9126. errn = -1;
  9127. }
  9128. //Check for SequenceError
  9129. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9130. {
  9131. cur->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  9132. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  9133. errn = -1;
  9134. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9135. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9136. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9137. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9138. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9139. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9140. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9141. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9142. }
  9143. //Check for negative EvBatteryMaxCurrent
  9144. if (sys->EvBatteryMaxCurrent < 0)
  9145. {
  9146. sprintf(buf_log_evcomm,
  9147. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  9148. sys->EvBatteryMaxCurrent);
  9149. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9150. cur->DC_EVSEStatus.EVSEStatusCode = dinresponseCodeType_FAILED_WrongChargeParameter; //16
  9151. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9152. errn = -1;
  9153. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9154. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  9155. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9156. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9157. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9158. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9159. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  9160. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  9161. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9162. }
  9163. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9164. SAVE_PhysicalValueType_DIN70121(&cur->EVSEPresentCurrent, (int) (sys->PresentChargingCurrent * 10), A_DIN70121);
  9165. SAVE_PhysicalValueType_DIN70121(&cur->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  9166. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == DISABLE
  9167. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_DIN70121);
  9168. #endif
  9169. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_DIN70121);
  9170. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_DIN70121);
  9171. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  9172. if (sys->EvBatteryMaxVoltage <= 500)
  9173. {
  9174. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumVoltageLimit, (int) (500 * 10), V_DIN70121); //500V
  9175. }
  9176. #endif
  9177. //Limit the EVTargetCurrent should be under EVSEMaximumCurrentLimit
  9178. if (sys->EvBatterytargetCurrent > sys->AvailableChargingCurrent)
  9179. {
  9180. sys->EvBatterytargetCurrent = sys->AvailableChargingCurrent;
  9181. //[To-Do] Limit is achieved flag
  9182. }
  9183. //Isolation Status
  9184. if (sys->IsolationStatus == GFD_Invalid) //0:invalid (on going)
  9185. {
  9186. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  9187. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9188. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9189. sprintf(buf_log_evcomm, "[Error][Proc_din_CurrentDemandRes]IsolationStatus = %d", sys->IsolationStatus);
  9190. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9191. Update_V2G_Flow_Status(Other_Fault);
  9192. errn = -1;
  9193. }
  9194. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  9195. {
  9196. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  9197. }
  9198. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  9199. {
  9200. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  9201. }
  9202. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  9203. {
  9204. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  9205. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9206. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9207. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_din_CurrentDemandRes]GFD_Fault => Emergency Shutdown");
  9208. Update_V2G_Flow_Status(Other_Fault);
  9209. errn = -1;
  9210. }
  9211. else //GFD_No_IMD or other unexpected status
  9212. {
  9213. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  9214. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9215. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9216. sprintf(buf_log_evcomm, "[Error][Proc_din_CurrentDemandRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  9217. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9218. Update_V2G_Flow_Status(Other_Fault);
  9219. errn = -1;
  9220. }
  9221. //For testing with Tesla Model 3
  9222. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == ENABLE
  9223. //[CAUTION][To-Do] Decresement of Current is not implemented.
  9224. //SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumCurrentLimit, 10, A_DIN70121);
  9225. if (sys->EvBatterytargetCurrent <= 0)
  9226. {
  9227. EVSE_max_current = 50; //10A
  9228. cur->EVSEMaximumCurrentLimit.Value = 50; //10A
  9229. /*
  9230. DEBUG_PRINTF_EVCOMM_DETAIL("1PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9231. sys->PresentChargingCurrent,
  9232. sys->EvBatterytargetCurrent,
  9233. EVSE_max_current,
  9234. cur->EVSEMaximumCurrentLimit.Value
  9235. );
  9236. */
  9237. }
  9238. else //1A
  9239. {
  9240. /*
  9241. DEBUG_PRINTF_EVCOMM_DETAIL("2PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9242. sys->PresentChargingCurrent,
  9243. sys->EvBatterytargetCurrent,
  9244. EVSE_max_current,
  9245. cur->EVSEMaximumCurrentLimit.Value
  9246. );
  9247. */
  9248. if ((abs((int)sys->PresentChargingCurrent - (int)sys->EvBatterytargetCurrent) < 3)&&(abs((int)(EVSE_max_current/10) - (int)sys->EvBatterytargetCurrent) < 3))
  9249. {
  9250. tmp = EVSE_max_current + 50; //10A
  9251. if (tmp <= (int)(sys->AvailableChargingCurrent * 10))
  9252. {
  9253. cur->EVSEMaximumCurrentLimit.Value = (int)tmp;
  9254. EVSE_max_current = tmp;
  9255. }
  9256. else
  9257. {
  9258. cur->EVSEMaximumCurrentLimit.Value = (int) (sys->AvailableChargingCurrent * 10); //max is set to 40A
  9259. EVSE_max_current = (int) (sys->AvailableChargingCurrent * 10);
  9260. }
  9261. }
  9262. }
  9263. #endif
  9264. //Check for CSU command of "Stop by EVSE"
  9265. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9266. {
  9267. //Check for Alarm Code: CCS GFD trip (012235)
  9268. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9269. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9270. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9271. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9272. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9273. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9274. {
  9275. SAVE_SYS_LOG_MSG_EVCOMM("[DIN][Error][CurrentDemandRes]CCS GFD trip => EVSE_Shutdown");
  9276. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  9277. }
  9278. else
  9279. {
  9280. SAVE_SYS_LOG_MSG_EVCOMM("[DIN][Error][CurrentDemandRes]EVSE_Shutdown");
  9281. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9282. }
  9283. //cur->ResponseCode = FAILED_DIN70121;
  9284. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9285. //errn = -1;
  9286. }
  9287. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9288. {
  9289. //Check for Alarm Code: CCS GFD trip (012235)
  9290. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9291. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9292. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9293. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9294. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9295. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9296. {
  9297. SAVE_SYS_LOG_MSG_EVCOMM("[DIN][Error][CurrentDemandRes]CCS GFD trip => EVSE_EmergencyShutdown");
  9298. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  9299. }
  9300. else
  9301. {
  9302. SAVE_SYS_LOG_MSG_EVCOMM("[DIN][Error][CurrentDemandRes]EVSE_EmergencyShutdown");
  9303. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9304. }
  9305. //cur->ResponseCode = FAILED_DIN70121;
  9306. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9307. //errn = -1;
  9308. }
  9309. else if (ShmInternalComm->ChargingPermission == FALSE)
  9310. {
  9311. SAVE_SYS_LOG_MSG_EVCOMM("[DIN][Error][CurrentDemandRes]Permission OFF");
  9312. //cur->ResponseCode = FAILED_DIN70121;
  9313. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9314. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9315. //errn = -1;
  9316. }
  9317. else
  9318. {
  9319. //null
  9320. }
  9321. //Response to CP Error
  9322. #if CP_PROTECTION_MECHANISM == ENABLE
  9323. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  9324. {
  9325. cur->ResponseCode = FAILED_DIN70121;
  9326. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9327. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9328. Update_V2G_Flow_Status(Other_Fault);
  9329. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9330. //CCS_SECC_CP_State_Error (023889)
  9331. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9332. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9333. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9334. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  9335. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  9336. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  9337. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  9338. sprintf(buf_log_evcomm,
  9339. "[CurrentDemand]Emergency Stop by CP Error (%d, %.02f V)",
  9340. sys->CpState,
  9341. sys->CpVoltage);
  9342. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9343. }
  9344. #endif
  9345. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9346. #if PARAMETER_NORMAL_MODE == ENABLE
  9347. SHM_Read_din_CurrentDemandRes(&ccs_exi_doc_DIN, ShmCcsData);
  9348. #else
  9349. Sudo_Parameter_din_CurrentDemandRes();
  9350. #endif
  9351. //STEP 4: ============ Encode and Send Response Message ===========
  9352. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  9353. {
  9354. errn = -1;
  9355. }
  9356. //STEP 5: ============ Update Flags ===========
  9357. return errn;
  9358. }
  9359. /*===========================================================================
  9360. FUNCTION: Proc_iso1_CurrentDemandRes
  9361. DESCRIPTION:
  9362. PRE-CONDITION:
  9363. INPUT:
  9364. OUTPUT:
  9365. GLOBAL VARIABLES:
  9366. =============================================================================*/
  9367. int Proc_iso1_CurrentDemandRes(int AcceptFd)
  9368. {
  9369. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9370. //if it is not the same, the packet should be ignored.
  9371. //STEP 1: ============ Initialize ============
  9372. int i = 0;
  9373. int errn = 0;
  9374. bitstream_t v2g_tx_stream;
  9375. static struct CurrentDemandResponse_ISO15118_2014 *cur;
  9376. static struct ChargingInfoData *sys;
  9377. static int EVSE_max_current;
  9378. int tmp = 0;
  9379. size_t pos = 0;
  9380. v2g_tx_stream.pos = &pos;
  9381. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9382. v2g_tx_stream.data = V2GTP_Tx_buf;
  9383. cur = &ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandResponse;
  9384. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9385. cur->ResponseCode = OK_ISO15118_2014;
  9386. //EVSE Status Code
  9387. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  9388. //[HEADER] Check Req SessionID
  9389. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  9390. {
  9391. cur->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  9392. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  9393. errn = -1;
  9394. }
  9395. //Check for SequenceError
  9396. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9397. {
  9398. cur->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  9399. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  9400. errn = -1;
  9401. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9402. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9403. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9404. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9405. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9406. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9407. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9408. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9409. }
  9410. //Check for negative EvBatteryMaxCurrent
  9411. if (sys->EvBatteryMaxCurrent < 0)
  9412. {
  9413. sprintf(buf_log_evcomm,
  9414. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  9415. sys->EvBatteryMaxCurrent);
  9416. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9417. cur->ResponseCode = iso1responseCodeType_FAILED_WrongChargeParameter; //16
  9418. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  9419. errn = -1;
  9420. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9421. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  9422. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9423. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9424. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9425. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9426. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  9427. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  9428. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9429. }
  9430. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9431. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEPresentCurrent, (int) (sys->PresentChargingCurrent * 10), A_ISO15118_2014);
  9432. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  9433. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == DISABLE
  9434. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  9435. #endif
  9436. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_ISO15118_2014);
  9437. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_ISO15118_2014);
  9438. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  9439. if (sys->EvBatteryMaxVoltage <= 500)
  9440. {
  9441. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumVoltageLimit, (int) (500 * 10), V_ISO15118_2014); //500V
  9442. }
  9443. #endif
  9444. //Limit the EVTargetCurrent should be under EVSEMaximumCurrentLimit
  9445. if (sys->EvBatterytargetCurrent > sys->AvailableChargingCurrent)
  9446. {
  9447. sys->EvBatterytargetCurrent = sys->AvailableChargingCurrent;
  9448. //[To-Do] Limit is achieved flag
  9449. }
  9450. //Isolation Status
  9451. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  9452. {
  9453. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  9454. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9455. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9456. sprintf(buf_log_evcomm, "[Error][Proc_iso1_CurrentDemandRes]IsolationStatus = %d", sys->IsolationStatus);
  9457. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9458. Update_V2G_Flow_Status(Other_Fault);
  9459. errn = -1;
  9460. }
  9461. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  9462. {
  9463. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  9464. }
  9465. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  9466. {
  9467. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Warning; //2
  9468. }
  9469. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  9470. {
  9471. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  9472. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9473. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9474. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_iso1_CurrentDemandRes]GFD_Fault => EmergencyShutdown");
  9475. Update_V2G_Flow_Status(Other_Fault);
  9476. errn = -1;
  9477. }
  9478. else
  9479. {
  9480. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  9481. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9482. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9483. sprintf(buf_log_evcomm, "[Error][Proc_iso1_CurrentDemandRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  9484. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9485. Update_V2G_Flow_Status(Other_Fault);
  9486. errn = -1;
  9487. }
  9488. //For testing with Tesla Model 3
  9489. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == ENABLE
  9490. //[CAUTION][To-Do] Decresement of Current is not implemented.
  9491. //SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumCurrentLimit, 10, A_ISO15118_2014);
  9492. if (sys->EvBatterytargetCurrent <= 0)
  9493. {
  9494. EVSE_max_current = 50; //10A
  9495. cur->EVSEMaximumCurrentLimit.Value = 50; //10A
  9496. /*
  9497. DEBUG_PRINTF_EVCOMM_DETAIL("1PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9498. sys->PresentChargingCurrent,
  9499. sys->EvBatterytargetCurrent,
  9500. EVSE_max_current,
  9501. cur->EVSEMaximumCurrentLimit.Value
  9502. );
  9503. */
  9504. }
  9505. else //1A
  9506. {
  9507. /*
  9508. DEBUG_PRINTF_EVCOMM_DETAIL("2PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9509. sys->PresentChargingCurrent,
  9510. sys->EvBatterytargetCurrent,
  9511. EVSE_max_current,
  9512. cur->EVSEMaximumCurrentLimit.Value
  9513. );
  9514. */
  9515. if ((abs((int)sys->PresentChargingCurrent - (int)sys->EvBatterytargetCurrent) < 3)&&(abs((int)(EVSE_max_current/10) - (int)sys->EvBatterytargetCurrent) < 3))
  9516. {
  9517. tmp = EVSE_max_current + 50; //10A
  9518. if (tmp <= (int)(sys->AvailableChargingCurrent * 10))
  9519. {
  9520. cur->EVSEMaximumCurrentLimit.Value = (int)tmp;
  9521. EVSE_max_current = tmp;
  9522. }
  9523. else
  9524. {
  9525. cur->EVSEMaximumCurrentLimit.Value = (int) (sys->AvailableChargingCurrent * 10); //max is set to 40A
  9526. EVSE_max_current = (int) (sys->AvailableChargingCurrent * 10);
  9527. }
  9528. }
  9529. }
  9530. #endif
  9531. //Check for CSU command of "Stop by EVSE"
  9532. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9533. {
  9534. //Check for Alarm Code: CCS GFD trip (012235)
  9535. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9536. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9537. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9538. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9539. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9540. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9541. {
  9542. SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][Error][CurrentDemandRes]CCS GFD trip => EVSE_Shutdown");
  9543. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  9544. }
  9545. else
  9546. {
  9547. SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][Error][CurrentDemandRes]EVSE_Shutdown");
  9548. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9549. }
  9550. //cur->ResponseCode = FAILED_ISO15118_2014;
  9551. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  9552. //errn = -1;
  9553. }
  9554. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9555. {
  9556. //Check for Alarm Code: CCS GFD trip (012235)
  9557. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9558. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9559. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9560. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9561. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9562. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9563. {
  9564. SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][Error][CurrentDemandRes]CCS GFD trip => EVSE_EmergencyShutdown");
  9565. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  9566. }
  9567. else
  9568. {
  9569. SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][Error][CurrentDemandRes]EVSE_EmergencyShutdown");
  9570. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9571. }
  9572. //cur->ResponseCode = FAILED_ISO15118_2014;
  9573. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9574. //errn = -1;
  9575. }
  9576. else if (ShmInternalComm->ChargingPermission == FALSE)
  9577. {
  9578. SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][Error][CurrentDemandRes]Permission OFF");
  9579. //cur->ResponseCode = FAILED_ISO15118_2014;
  9580. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9581. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  9582. //errn = -1;
  9583. }
  9584. else
  9585. {
  9586. //null
  9587. }
  9588. //Response to CP Error
  9589. #if CP_PROTECTION_MECHANISM == ENABLE
  9590. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  9591. {
  9592. cur->ResponseCode = FAILED_ISO15118_2014;
  9593. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9594. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9595. Update_V2G_Flow_Status(Other_Fault);
  9596. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9597. //CCS_SECC_CP_State_Error (023889)
  9598. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9599. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9600. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9601. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  9602. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  9603. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  9604. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  9605. sprintf(buf_log_evcomm,
  9606. "[CurrentDemand]Emergency Stop by CP Error (%d, %.02f V)",
  9607. sys->CpState,
  9608. sys->CpVoltage);
  9609. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9610. }
  9611. #endif
  9612. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9613. #if PARAMETER_NORMAL_MODE == ENABLE
  9614. SHM_Read_iso1_CurrentDemandRes(&ccs_exi_doc_ISO1, ShmCcsData);
  9615. #else
  9616. Sudo_Parameter_iso1_CurrentDemandRes();
  9617. #endif
  9618. //STEP 4: ============ Encode and Send Response Message ===========
  9619. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  9620. {
  9621. errn = -1;
  9622. }
  9623. //STEP 5: ============ Update Flags ===========
  9624. return errn;
  9625. }
  9626. /*===========================================================================
  9627. FUNCTION: Proc_iso1_ChargingStatusRes
  9628. DESCRIPTION:
  9629. PRE-CONDITION:
  9630. INPUT:
  9631. OUTPUT:
  9632. GLOBAL VARIABLES:
  9633. =============================================================================*/
  9634. int Proc_iso1_ChargingStatusRes(int AcceptFd)
  9635. {
  9636. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9637. //if it is not the same, the packet should be ignored.
  9638. //STEP 1: ============ Initialize ============
  9639. int i = 0;
  9640. int errn = 0;
  9641. bitstream_t v2g_tx_stream;
  9642. static struct ChargingStatusResponse_ISO15118_2014 *res;
  9643. static struct ChargingInfoData *sys;
  9644. size_t pos = 0;
  9645. v2g_tx_stream.pos = &pos;
  9646. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9647. v2g_tx_stream.data = V2GTP_Tx_buf;
  9648. res = &ShmCcsData->V2GMessage_ISO15118_2014.ChargingStatusResponse;
  9649. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9650. //Init
  9651. res->ResponseCode = OK_ISO15118_2014;
  9652. res->ReceiptRequired = FALSE; //optional
  9653. res->SAScheduleTupleID = 0;
  9654. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  9655. res->AC_EVSEStatus.NotificationMaxDelay = 0;
  9656. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  9657. // iso1EVSENotificationType_None = 0,
  9658. // iso1EVSENotificationType_StopCharging = 1,
  9659. // iso1EVSENotificationType_ReNegotiation = 2
  9660. //[HEADER] Check Req SessionID
  9661. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  9662. {
  9663. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  9664. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  9665. errn = -1;
  9666. }
  9667. //Check for SequenceError
  9668. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9669. {
  9670. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  9671. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  9672. errn = -1;
  9673. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9674. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9675. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9676. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9677. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9678. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9679. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9680. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9681. }
  9682. //EVSE ID
  9683. memset(res->EVSEID, 0, sizeof(res->EVSEID));
  9684. //sprintf(res->EVSEID, CCS_AC_EVSEID);
  9685. //[MeterInfo][0/5] init
  9686. //memset(res->MeterInfo.MeterID.characters, 0, sizeof(res->MeterInfo.MeterID.characters));
  9687. //memset(res->MeterInfo.SigMeterReading.bytes, 0, sizeof(res->MeterInfo.SigMeterReading.bytes));
  9688. //[MeterInfo][1/5] MeterID
  9689. //sprintf(res->MeterInfo.MeterID , CCS_AC_METER_ID);
  9690. //[MeterInfo][2/5] SigMeterReading (optional)
  9691. //sprintf(res->MeterInfo.SigMeterReading , CCS_AC_SIG_METER_READING);
  9692. //[MeterInfo][3/5] MeterStatus (optional)
  9693. res->MeterInfo.MeterStatus = 0;
  9694. //[MeterInfo][4/5] MeterReading (optional)
  9695. res->MeterInfo.MeterReading = 12345;
  9696. //[MeterInfo][5/5] TMeter (optional)
  9697. res->MeterInfo.TMeter = 1586243587; //Unix Time Stamp format
  9698. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9699. SAVE_PhysicalValueType_ISO15118_2014(&res->EVSEMaxCurrent, (int)(sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  9700. //Isolation Status (RCD)
  9701. if (sys->IsolationStatus == 0) //Isolation is invalid
  9702. {
  9703. res->AC_EVSEStatus.RCD = TRUE; //FALSE(no error), TRUE(error is detected)
  9704. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9705. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9706. sprintf(buf_log_evcomm, "[ISO1][Error][ChargingStatusRes]IsolationStatus = %d", sys->IsolationStatus);
  9707. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9708. Update_V2G_Flow_Status(Other_Fault);
  9709. errn = -1;
  9710. }
  9711. else if (sys->IsolationStatus == 1) //Isolation is valid
  9712. {
  9713. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  9714. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  9715. }
  9716. else
  9717. {
  9718. res->AC_EVSEStatus.RCD = TRUE; //FALSE(no error), TRUE(error is detected)
  9719. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9720. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9721. sprintf(buf_log_evcomm, "[ISO1][Error][ChargingStatusRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  9722. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9723. Update_V2G_Flow_Status(Other_Fault);
  9724. errn = -1;
  9725. }
  9726. //Check for CSU command of "Stop by EVSE"
  9727. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9728. {
  9729. //res->ResponseCode = FAILED_ISO15118_2014;
  9730. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9731. //errn = -1;
  9732. }
  9733. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9734. {
  9735. //res->ResponseCode = FAILED_ISO15118_2014;
  9736. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9737. //errn = -1;
  9738. }
  9739. else if (ShmInternalComm->ChargingPermission == FALSE)
  9740. {
  9741. //res->ResponseCode = FAILED_ISO15118_2014;
  9742. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9743. //errn = -1;
  9744. }
  9745. //Response to CP Error
  9746. #if CP_PROTECTION_MECHANISM == ENABLE
  9747. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  9748. {
  9749. res->ResponseCode = FAILED_ISO15118_2014;
  9750. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9751. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9752. Update_V2G_Flow_Status(Other_Fault);
  9753. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9754. //CCS_SECC_CP_State_Error (023889)
  9755. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9756. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9757. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9758. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  9759. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  9760. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  9761. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  9762. sprintf(buf_log_evcomm,
  9763. "[ChargingStatus]Emergency Stop by CP Error (%d, %.02f V)",
  9764. sys->CpState,
  9765. sys->CpVoltage);
  9766. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9767. }
  9768. #endif
  9769. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9770. #if PARAMETER_NORMAL_MODE == ENABLE
  9771. SHM_Read_iso1_ChargingStatusRes(&ccs_exi_doc_ISO1, ShmCcsData);
  9772. #else
  9773. Sudo_Parameter_iso1_ChargingStatusRes();
  9774. #endif
  9775. //STEP 4: ============ Encode and Send Response Message ===========
  9776. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  9777. {
  9778. errn = -1;
  9779. }
  9780. //STEP 5: ============ Update Flags ===========
  9781. return errn;
  9782. }
  9783. /*===========================================================================
  9784. FUNCTION: Proc_din_CurrentDemandReq
  9785. DESCRIPTION:
  9786. PRE-CONDITION:
  9787. INPUT:
  9788. OUTPUT:
  9789. GLOBAL VARIABLES:
  9790. =============================================================================*/
  9791. int Proc_din_CurrentDemandReq(int AcceptFd)
  9792. {
  9793. int errn = 0;
  9794. //SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_CurrentDemandReq] Got CurrentDemandReq\n");
  9795. //Print the decoded XML Document
  9796. PRINT_XML_DOC_DIN_CurrentDemandReq(&ccs_exi_doc_DIN);
  9797. //Save into Share Memory
  9798. SHM_Save_din_CurrentDemandReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  9799. //Check for EV Error Code
  9800. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.CurrentDemandRequest.DC_EVStatus.EVErrorCode);
  9801. errn = Proc_din_CurrentDemandRes(AcceptFd);
  9802. if (errn == 0)
  9803. {
  9804. //Response is sent successfully.
  9805. //SAVE_SYS_LOG_MSG_EVCOMM("[DIN][V2G][Tx]CurrentDemandRes");
  9806. }
  9807. else
  9808. {
  9809. sprintf(buf_log_evcomm,
  9810. "[DIN][Error][CurrentDemandReq]CurrentDemandRes: fail(%d,DEC)",
  9811. errn);
  9812. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9813. }
  9814. return errn;
  9815. }
  9816. /*===========================================================================
  9817. FUNCTION: Proc_iso1_CurrentDemandReq
  9818. DESCRIPTION:
  9819. PRE-CONDITION:
  9820. INPUT:
  9821. OUTPUT:
  9822. GLOBAL VARIABLES:
  9823. =============================================================================*/
  9824. int Proc_iso1_CurrentDemandReq(int AcceptFd)
  9825. {
  9826. int errn = 0;
  9827. //SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_CurrentDemandReq] Got CurrentDemandReq\n");
  9828. //Print the decoded XML Document
  9829. PRINT_XML_DOC_ISO1_CurrentDemandReq(&ccs_exi_doc_ISO1);
  9830. //Save into Share Memory
  9831. SHM_Save_iso1_CurrentDemandReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  9832. //Check for EV Error Code
  9833. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandRequest.DC_EVStatus.EVErrorCode);
  9834. errn = Proc_iso1_CurrentDemandRes(AcceptFd);
  9835. if (errn == 0)
  9836. {
  9837. //Response is sent successfully.
  9838. //SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]CurrentDemandRes");
  9839. }
  9840. else
  9841. {
  9842. sprintf(buf_log_evcomm,
  9843. "[ISO1][Error][CurrentDemandReq]CurrentDemandRes: fail(%d,DEC)",
  9844. errn);
  9845. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9846. }
  9847. return errn;
  9848. }
  9849. /*===========================================================================
  9850. FUNCTION: Proc_iso1_ChargingStatusReq
  9851. DESCRIPTION:
  9852. PRE-CONDITION:
  9853. INPUT:
  9854. OUTPUT:
  9855. GLOBAL VARIABLES:
  9856. =============================================================================*/
  9857. int Proc_iso1_ChargingStatusReq(int AcceptFd)
  9858. {
  9859. int errn = 0;
  9860. //SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_ChargingStatusReq] Got ChargingStatusReq\n");
  9861. //Print the decoded XML Document
  9862. PRINT_XML_DOC_ISO1_ChargingStatusReq(&ccs_exi_doc_ISO1);
  9863. //Save into Share Memory
  9864. SHM_Save_iso1_ChargingStatusReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  9865. //Check for EV Error Code
  9866. //no content in ISO1
  9867. errn = Proc_iso1_ChargingStatusRes(AcceptFd);
  9868. if (errn == 0)
  9869. {
  9870. //Response is sent successfully.
  9871. //SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]ChargingStatusRes");
  9872. }
  9873. else
  9874. {
  9875. sprintf(buf_log_evcomm,
  9876. "[ISO1][Error][ChargingStatusReq]ChargingStatusRes: fail(%d,DEC)",
  9877. errn);
  9878. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  9879. }
  9880. return errn;
  9881. }
  9882. /*===========================================================================
  9883. FUNCTION: Proc_din_PowerDeliveryStopRes
  9884. DESCRIPTION:
  9885. PRE-CONDITION:
  9886. INPUT:
  9887. OUTPUT:
  9888. GLOBAL VARIABLES:
  9889. =============================================================================*/
  9890. int Proc_din_PowerDeliveryStopRes(int AcceptFd)
  9891. {
  9892. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9893. //if it is not the same, the packet should be ignored.
  9894. //STEP 1: ============ Initialize ============
  9895. int i = 0;
  9896. int errn = 0;
  9897. bitstream_t v2g_tx_stream;
  9898. struct dinPowerDeliveryResType *res;
  9899. struct ChargingInfoData *sys;
  9900. size_t pos = 0;
  9901. v2g_tx_stream.pos = &pos;
  9902. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9903. v2g_tx_stream.data = V2GTP_Tx_buf;
  9904. res = &ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes;
  9905. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9906. res->ResponseCode = OK_DIN70121;
  9907. //[HEADER] Check Req SessionID
  9908. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  9909. {
  9910. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  9911. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  9912. errn = -1;
  9913. }
  9914. //Check for SequenceError
  9915. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9916. {
  9917. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  9918. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  9919. errn = -1;
  9920. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9921. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9922. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9923. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9924. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9925. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9926. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9927. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9928. }
  9929. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9930. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9931. #if PARAMETER_NORMAL_MODE == ENABLE
  9932. ShmCcsData->V2GMessage_DIN70121.PowerDeliveryResponse.DC_EVSEStatus.EVSEStatusCode = EVSE_NotReady;
  9933. SHM_Read_din_PowerDeliveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  9934. #else
  9935. Sudo_Parameter_din_PreChargeRes();
  9936. #endif
  9937. //EVSE Status Code
  9938. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  9939. //Check for CSU command of "Stop by EVSE"
  9940. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9941. {
  9942. //res->ResponseCode = FAILED_DIN70121;
  9943. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9944. }
  9945. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9946. {
  9947. //res->ResponseCode = FAILED_DIN70121;
  9948. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9949. }
  9950. else if (ShmInternalComm->ChargingPermission == FALSE)
  9951. {
  9952. //res->ResponseCode = FAILED_DIN70121;
  9953. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9954. }
  9955. //STEP 4: ============ Encode and Send Response Message ===========
  9956. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  9957. {
  9958. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_PowerDeliveryStopRes][Error]Tx encoded msg error");
  9959. errn = -1;
  9960. }
  9961. //STEP 5: ============ Update Flags ===========
  9962. return errn;
  9963. }
  9964. /*===========================================================================
  9965. FUNCTION: Proc_iso1_PowerDeliveryStopRes
  9966. DESCRIPTION:
  9967. PRE-CONDITION:
  9968. INPUT:
  9969. OUTPUT:
  9970. GLOBAL VARIABLES:
  9971. =============================================================================*/
  9972. int Proc_iso1_PowerDeliveryStopRes(int AcceptFd)
  9973. {
  9974. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9975. //if it is not the same, the packet should be ignored.
  9976. //STEP 1: ============ Initialize ============
  9977. int i = 0;
  9978. int errn = 0;
  9979. bitstream_t v2g_tx_stream;
  9980. struct iso1PowerDeliveryResType *res;
  9981. struct ChargingInfoData *sys;
  9982. size_t pos = 0;
  9983. v2g_tx_stream.pos = &pos;
  9984. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9985. v2g_tx_stream.data = V2GTP_Tx_buf;
  9986. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes;
  9987. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9988. res->ResponseCode = OK_ISO15118_2014;
  9989. //[HEADER] Check Req SessionID
  9990. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  9991. {
  9992. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  9993. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  9994. errn = -1;
  9995. }
  9996. //Check for SequenceError
  9997. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9998. {
  9999. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  10000. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  10001. errn = -1;
  10002. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10003. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10004. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10005. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10006. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10007. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10008. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10009. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10010. }
  10011. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10012. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10013. #if PARAMETER_NORMAL_MODE == ENABLE
  10014. ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryResponse.DC_EVSEStatus.DC_EVSEStatusCode = EVSE_NotReady;
  10015. SHM_Read_iso1_PowerDeliveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  10016. #else
  10017. Sudo_Parameter_iso1_PreChargeRes();
  10018. #endif
  10019. //EVSE Status Code
  10020. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  10021. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1 /*+++ 20200808, vern, Isolation Status should be valid during 2nd PowerDelivert ---*/
  10022. //Check for CSU command of "Stop by EVSE"
  10023. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  10024. {
  10025. //res->ResponseCode = FAILED_ISO15118_2014;
  10026. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready; /*+++ 20200808, vern, Isolation Status should be valid during 2nd PowerDelivert ---*/
  10027. }
  10028. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  10029. {
  10030. //res->ResponseCode = FAILED_ISO15118_2014;
  10031. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10032. }
  10033. else if (ShmInternalComm->ChargingPermission == FALSE)
  10034. {
  10035. //res->ResponseCode = FAILED_ISO15118_2014;
  10036. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready; /*+++ 20200808, vern, Isolation Status should be valid during 2nd PowerDelivert ---*/
  10037. }
  10038. //STEP 4: ============ Encode and Send Response Message ===========
  10039. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  10040. {
  10041. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_PowerDeliveryStopRes][Error]Tx encoded msg error");
  10042. errn = -1;
  10043. }
  10044. //STEP 5: ============ Update Flags ===========
  10045. return errn;
  10046. }
  10047. /*===========================================================================
  10048. FUNCTION: Proc_din_PowerDeliveryStopReq
  10049. DESCRIPTION:
  10050. PRE-CONDITION:
  10051. INPUT:
  10052. OUTPUT:
  10053. GLOBAL VARIABLES:
  10054. =============================================================================*/
  10055. int Proc_din_PowerDeliveryStopReq(int AcceptFd)
  10056. {
  10057. int errn = 0;
  10058. //Request CSU to STOP
  10059. //This should be reponsed as soon as possible once this message is received.
  10060. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10061. EVCOMM_SYS_INFO.EvBatterytargetVoltage = 0;
  10062. EVCOMM_SYS_INFO.EvBatterytargetCurrent = 0;
  10063. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]PowerDeliveryReq (2nd)\n");
  10064. //Print the decoded XML Document
  10065. PRINT_XML_DOC_DIN_PowerDeliveryReq(&ccs_exi_doc_DIN);
  10066. //Save into Share Memory
  10067. SHM_Save_din_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  10068. //Check for EV Error Code
  10069. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  10070. //[To-Do] Sending response after the EVSE output voltage decreases to under 60V
  10071. usleep(1500000); //1.5 seconds
  10072. //sleep(1); //1 second
  10073. errn = Proc_din_PowerDeliveryStopRes(AcceptFd);
  10074. if (errn == 0)
  10075. {
  10076. //send response successfully.
  10077. SAVE_SYS_LOG_MSG_EVCOMM("[DIN][V2G][Tx]PowerDeliveryRes (2nd)\n");
  10078. }
  10079. else
  10080. {
  10081. sprintf(buf_log_evcomm,
  10082. "[Error][SeccComm][Proc_din_PowerDeliveryStopReq]Proc_iso1_PowerDeliveryStopRes(): %d (DEC)",
  10083. errn);
  10084. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  10085. }
  10086. return errn;
  10087. }
  10088. /*===========================================================================
  10089. FUNCTION: Proc_iso1_PowerDeliveryStopReq
  10090. DESCRIPTION:
  10091. PRE-CONDITION:
  10092. INPUT:
  10093. OUTPUT:
  10094. GLOBAL VARIABLES:
  10095. =============================================================================*/
  10096. int Proc_iso1_PowerDeliveryStopReq(int AcceptFd)
  10097. {
  10098. int errn = 0;
  10099. //Request CSU to STOP
  10100. //This should be reponsed as soon as possible once this message is received.
  10101. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10102. EVCOMM_SYS_INFO.EvBatterytargetVoltage = 0;
  10103. EVCOMM_SYS_INFO.EvBatterytargetCurrent = 0;
  10104. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]PowerDeliveryReq (2nd)\n");
  10105. //Print the decoded XML Document
  10106. PRINT_XML_DOC_ISO1_PowerDeliveryReq(&ccs_exi_doc_ISO1);
  10107. //Save into Share Memory
  10108. SHM_Save_iso1_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  10109. //Check for EV Error Code
  10110. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  10111. //[To-Do] Sending response after the EVSE output voltage decreases to under 60V
  10112. usleep(1500000); //1.5 seconds
  10113. //sleep(1); //1 second
  10114. errn = Proc_iso1_PowerDeliveryStopRes(AcceptFd);
  10115. if (errn == 0)
  10116. {
  10117. //send response successfully.
  10118. SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]PowerDeliveryRes (2nd)");
  10119. }
  10120. else
  10121. {
  10122. sprintf(buf_log_evcomm,
  10123. "[Error][SeccComm][Proc_iso1_PowerDeliveryStopReq]Proc_iso1_PowerDeliveryStopRes(): %d (DEC)",
  10124. errn);
  10125. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  10126. }
  10127. return errn;
  10128. }
  10129. /*===========================================================================
  10130. FUNCTION: SHM_Init_din_WeldingDetectionRes
  10131. DESCRIPTION:
  10132. PRE-CONDITION:
  10133. INPUT:
  10134. 1. shm_ccs
  10135. OUTPUT:
  10136. 1. shm_ccs
  10137. GLOBAL VARIABLES:
  10138. =============================================================================*/
  10139. void SHM_Init_din_WeldingDetectionRes(struct CcsData *shm_ccs)
  10140. {
  10141. struct WeldingDetectionResponse_DIN70121 *in;
  10142. in = &shm_ccs->V2GMessage_DIN70121.WeldingDetectionResponse;
  10143. //----- [BODY (1/3)] ResponseCode -----
  10144. in->ResponseCode = dinresponseCodeType_OK;
  10145. //----- [BODY (2/3)] EVSEPresentVoltage -----
  10146. short value = 0; int multiplier = 0; unsigned char unit = 0;
  10147. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for test only.
  10148. value = 0; multiplier = 0; unit = V_DIN70121;
  10149. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  10150. //----- [BODY (3/3)] DC_EVSEStatus -----
  10151. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  10152. }
  10153. /*===========================================================================
  10154. FUNCTION: Sudo_Parameter_din_WeldingDetectionRes
  10155. DESCRIPTION:
  10156. PRE-CONDITION:
  10157. INPUT:
  10158. OUTPUT:
  10159. GLOBAL VARIABLES:
  10160. =============================================================================*/
  10161. void Sudo_Parameter_din_WeldingDetectionRes()
  10162. {
  10163. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  10164. init_dinWeldingDetectionResType(&ccs_exi_doc_DIN.V2G_Message.Body.WeldingDetectionRes);
  10165. ccs_exi_doc_DIN.V2G_Message.Body.WeldingDetectionRes_isUsed = 1u;
  10166. //----- [BODY (1/3)] ResponseCode -----
  10167. struct dinWeldingDetectionResType *res;
  10168. res = &ccs_exi_doc_DIN.V2G_Message.Body.WeldingDetectionRes;
  10169. res->ResponseCode = dinresponseCodeType_OK;
  10170. //----- [BODY (2/3)] EVSEPresentVoltage -----
  10171. res->EVSEPresentVoltage.Value = 3820;
  10172. res->EVSEPresentVoltage.Multiplier = -1;
  10173. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  10174. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  10175. // dinunitSymbolType_h = 0,
  10176. // dinunitSymbolType_m = 1,
  10177. // dinunitSymbolType_s = 2,
  10178. // dinunitSymbolType_A = 3,
  10179. // dinunitSymbolType_Ah = 4,
  10180. // dinunitSymbolType_V = 5,
  10181. // dinunitSymbolType_VA = 6,
  10182. // dinunitSymbolType_W = 7,
  10183. // dinunitSymbolType_W_s = 8,
  10184. // dinunitSymbolType_Wh = 9
  10185. //----- [BODY (3/3)] DC_EVSEStatus -----
  10186. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  10187. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  10188. // dinisolationLevelType_Invalid = 0,
  10189. // dinisolationLevelType_Valid = 1,
  10190. // dinisolationLevelType_Warning = 2,
  10191. // dinisolationLevelType_Fault = 3
  10192. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  10193. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  10194. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  10195. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  10196. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  10197. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  10198. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  10199. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  10200. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  10201. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  10202. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  10203. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  10204. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  10205. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  10206. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  10207. // dinEVSENotificationType_None = 0,
  10208. // dinEVSENotificationType_StopCharging = 1,
  10209. // dinEVSENotificationType_ReNegotiation = 2
  10210. }
  10211. /*===========================================================================
  10212. FUNCTION: Sudo_Parameter_iso1_WeldingDetectionRes
  10213. DESCRIPTION:
  10214. PRE-CONDITION:
  10215. INPUT:
  10216. OUTPUT:
  10217. GLOBAL VARIABLES:
  10218. =============================================================================*/
  10219. void Sudo_Parameter_iso1_WeldingDetectionRes()
  10220. {
  10221. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  10222. init_iso1WeldingDetectionResType(&ccs_exi_doc_ISO1.V2G_Message.Body.WeldingDetectionRes);
  10223. ccs_exi_doc_ISO1.V2G_Message.Body.WeldingDetectionRes_isUsed = 1u;
  10224. //----- [BODY (1/3)] ResponseCode -----
  10225. struct iso1WeldingDetectionResType *res;
  10226. res = &ccs_exi_doc_ISO1.V2G_Message.Body.WeldingDetectionRes;
  10227. res->ResponseCode = iso1responseCodeType_OK;
  10228. //----- [BODY (2/3)] EVSEPresentVoltage -----
  10229. res->EVSEPresentVoltage.Value = 3820;
  10230. res->EVSEPresentVoltage.Multiplier = -1;
  10231. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  10232. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  10233. //iso1unitSymbolType_h = 0,
  10234. //iso1unitSymbolType_m = 1,
  10235. //iso1unitSymbolType_s = 2,
  10236. //iso1unitSymbolType_A = 3,
  10237. //iso1unitSymbolType_V = 4,
  10238. //iso1unitSymbolType_W = 5,
  10239. //iso1unitSymbolType_Wh = 6
  10240. //----- [BODY (3/3)] DC_EVSEStatus -----
  10241. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  10242. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  10243. //iso1isolationLevelType_Invalid = 0,
  10244. //iso1isolationLevelType_Valid = 1,
  10245. //iso1isolationLevelType_Warning = 2,
  10246. //iso1isolationLevelType_Fault = 3,
  10247. //iso1isolationLevelType_No_IMD = 4
  10248. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  10249. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  10250. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  10251. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  10252. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  10253. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  10254. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  10255. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  10256. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  10257. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  10258. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  10259. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  10260. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  10261. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  10262. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  10263. //iso1EVSENotificationType_None = 0,
  10264. //iso1EVSENotificationType_StopCharging = 1,
  10265. //iso1EVSENotificationType_ReNegotiation = 2
  10266. }
  10267. /*===========================================================================
  10268. FUNCTION: Proc_din_WeldingDetectionRes
  10269. DESCRIPTION:
  10270. PRE-CONDITION:
  10271. INPUT:
  10272. OUTPUT:
  10273. GLOBAL VARIABLES:
  10274. =============================================================================*/
  10275. int Proc_din_WeldingDetectionRes(int AcceptFd)
  10276. {
  10277. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10278. //if it is not the same, the packet should be ignored.
  10279. //STEP 1: ============ Initialize ============
  10280. int i = 0;
  10281. int errn = 0;
  10282. bitstream_t v2g_tx_stream;
  10283. static struct WeldingDetectionResponse_DIN70121 *wel;
  10284. static struct ChargingInfoData *sys;
  10285. size_t pos = 0;
  10286. v2g_tx_stream.pos = &pos;
  10287. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10288. v2g_tx_stream.data = V2GTP_Tx_buf;
  10289. wel = &ShmCcsData->V2GMessage_DIN70121.WeldingDetectionResponse;
  10290. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  10291. wel->ResponseCode = OK_DIN70121;
  10292. //[HEADER] Check Req SessionID
  10293. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  10294. {
  10295. wel->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  10296. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  10297. errn = -1;
  10298. }
  10299. //Check for SequenceError
  10300. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10301. {
  10302. wel->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  10303. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  10304. errn = -1;
  10305. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10306. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10307. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10308. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10309. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10310. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10311. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10312. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10313. }
  10314. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10315. SAVE_PhysicalValueType_DIN70121(&wel->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  10316. //EVSE Status Code
  10317. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  10318. //Check for CSU command of "Stop by EVSE"
  10319. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  10320. {
  10321. //wel->ResponseCode = FAILED_DIN70121;
  10322. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  10323. }
  10324. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  10325. {
  10326. //wel->ResponseCode = FAILED_DIN70121;
  10327. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10328. }
  10329. else if (ShmInternalComm->ChargingPermission == FALSE)
  10330. {
  10331. //wel->ResponseCode = FAILED_DIN70121;
  10332. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  10333. }
  10334. //Isolation Status
  10335. if (sys->IsolationStatus == GFD_Invalid) //0:invalid (on going)
  10336. {
  10337. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  10338. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10339. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10340. sprintf(buf_log_evcomm, "[Error][Proc_din_WeldingDetectionRes]IsolationStatus = %d", sys->IsolationStatus);
  10341. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  10342. Update_V2G_Flow_Status(Other_Fault);
  10343. errn = -1;
  10344. }
  10345. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  10346. {
  10347. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  10348. }
  10349. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  10350. {
  10351. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  10352. }
  10353. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  10354. {
  10355. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  10356. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10357. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10358. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_din_WeldingDetectionRes]GFD_Fault => Emergency Shutdown");
  10359. Update_V2G_Flow_Status(Other_Fault);
  10360. errn = -1;
  10361. }
  10362. else //GFD_No_IMD or other unexpected status
  10363. {
  10364. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  10365. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10366. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10367. sprintf(buf_log_evcomm, "[Error][Proc_din_WeldingDetectionRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  10368. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  10369. Update_V2G_Flow_Status(Other_Fault);
  10370. errn = -1;
  10371. }
  10372. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10373. #if PARAMETER_NORMAL_MODE == ENABLE
  10374. SHM_Read_din_WeldingDetectionRes(&ccs_exi_doc_DIN, ShmCcsData);
  10375. #else
  10376. Sudo_Parameter_din_WeldingDetectionRes();
  10377. #endif
  10378. //STEP 4: ============ Encode and Send Response Message ===========
  10379. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  10380. {
  10381. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_WeldingDetectionRes][Error]Tx encoded msg error");
  10382. errn = -1;
  10383. }
  10384. //STEP 5: ============ Update Flags ===========
  10385. return errn;
  10386. }
  10387. /*===========================================================================
  10388. FUNCTION: Proc_iso1_WeldingDetectionRes
  10389. DESCRIPTION:
  10390. PRE-CONDITION:
  10391. INPUT:
  10392. OUTPUT:
  10393. GLOBAL VARIABLES:
  10394. =============================================================================*/
  10395. int Proc_iso1_WeldingDetectionRes(int AcceptFd)
  10396. {
  10397. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10398. //if it is not the same, the packet should be ignored.
  10399. //STEP 1: ============ Initialize ============
  10400. int i = 0;
  10401. int errn = 0;
  10402. bitstream_t v2g_tx_stream;
  10403. static struct WeldingDetectionResponse_ISO15118_2014 *wel;
  10404. static struct ChargingInfoData *sys;
  10405. size_t pos = 0;
  10406. v2g_tx_stream.pos = &pos;
  10407. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10408. v2g_tx_stream.data = V2GTP_Tx_buf;
  10409. wel = &ShmCcsData->V2GMessage_ISO15118_2014.WeldingDetectionResponse;
  10410. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  10411. wel->ResponseCode = OK_ISO15118_2014;
  10412. //[HEADER] Check Req SessionID
  10413. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  10414. {
  10415. wel->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  10416. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  10417. errn = -1;
  10418. }
  10419. //Check for SequenceError
  10420. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10421. {
  10422. wel->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  10423. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  10424. errn = -1;
  10425. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10426. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10427. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10428. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10429. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10430. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10431. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10432. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10433. }
  10434. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10435. SAVE_PhysicalValueType_ISO15118_2014(&wel->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  10436. //EVSE Status Code
  10437. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  10438. //Check for CSU command of "Stop by EVSE"
  10439. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  10440. {
  10441. //wel->ResponseCode = FAILED_ISO15118_2014;
  10442. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  10443. }
  10444. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  10445. {
  10446. //wel->ResponseCode = FAILED_ISO15118_2014;
  10447. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10448. }
  10449. else if (ShmInternalComm->ChargingPermission == FALSE)
  10450. {
  10451. //wel->ResponseCode = FAILED_ISO15118_2014;
  10452. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  10453. }
  10454. //Isolation Status
  10455. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  10456. {
  10457. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  10458. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10459. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10460. sprintf(buf_log_evcomm, "[Error][Proc_iso1_WeldingDetectionRes]IsolationStatus = %d", sys->IsolationStatus);
  10461. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  10462. Update_V2G_Flow_Status(Other_Fault);
  10463. errn = -1;
  10464. }
  10465. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  10466. {
  10467. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  10468. }
  10469. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  10470. {
  10471. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  10472. }
  10473. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  10474. {
  10475. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  10476. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10477. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10478. SAVE_SYS_LOG_MSG_EVCOMM("[Error][Proc_iso1_WeldingDetectionRes]GFD_Fault => EmergencyShutdown");
  10479. Update_V2G_Flow_Status(Other_Fault);
  10480. errn = -1;
  10481. }
  10482. else
  10483. {
  10484. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  10485. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10486. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10487. sprintf(buf_log_evcomm, "[Error][Proc_iso1_WeldingDetectionRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  10488. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  10489. Update_V2G_Flow_Status(Other_Fault);
  10490. errn = -1;
  10491. }
  10492. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10493. #if PARAMETER_NORMAL_MODE == ENABLE
  10494. SHM_Read_iso1_WeldingDetectionRes(&ccs_exi_doc_ISO1, ShmCcsData);
  10495. #else
  10496. Sudo_Parameter_iso1_WeldingDetectionRes();
  10497. #endif
  10498. //STEP 4: ============ Encode and Send Response Message ===========
  10499. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  10500. {
  10501. SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_WeldingDetectionRes][Error]Tx encoded msg error");
  10502. errn = -1;
  10503. }
  10504. //STEP 5: ============ Update Flags ===========
  10505. return errn;
  10506. }
  10507. /*===========================================================================
  10508. FUNCTION: Proc_din_WeldingDetectionReq
  10509. DESCRIPTION:
  10510. PRE-CONDITION:
  10511. INPUT:
  10512. OUTPUT:
  10513. GLOBAL VARIABLES:
  10514. =============================================================================*/
  10515. int Proc_din_WeldingDetectionReq(int AcceptFd)
  10516. {
  10517. int errn = 0;
  10518. //SAVE_SYS_LOG_MSG_EVCOMM("[Proc_din_WeldingDetectionReq] Got WeldingDetectionReq\n");
  10519. //Print the decoded XML Document
  10520. PRINT_XML_DOC_DIN_WeldingDetectionReq(&ccs_exi_doc_DIN);
  10521. //Save into Share Memory
  10522. SHM_Save_din_WeldingDetectionReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  10523. //Check for EV Error Code
  10524. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.WeldingDetectionRequest.DC_EVStatus.EVErrorCode);
  10525. errn = Proc_din_WeldingDetectionRes(AcceptFd);
  10526. if (errn == 0)
  10527. {
  10528. //send response successfully.
  10529. //SAVE_SYS_LOG_MSG_EVCOMM("[DIN][V2G][Tx]WeldingDetectionRes");
  10530. }
  10531. else
  10532. {
  10533. sprintf(buf_log_evcomm,
  10534. "[Error][SeccComm][Proc_din_WeldingDetectionReq]Proc_din_WeldingDetectionRes(): %d (DEC)",
  10535. errn);
  10536. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  10537. }
  10538. return errn;
  10539. }
  10540. /*===========================================================================
  10541. FUNCTION: Proc_iso1_WeldingDetectionReq
  10542. DESCRIPTION:
  10543. PRE-CONDITION:
  10544. INPUT:
  10545. OUTPUT:
  10546. GLOBAL VARIABLES:
  10547. =============================================================================*/
  10548. int Proc_iso1_WeldingDetectionReq(int AcceptFd)
  10549. {
  10550. int errn = 0;
  10551. //SAVE_SYS_LOG_MSG_EVCOMM("[Proc_iso1_WeldingDetectionReq] Got WeldingDetectionReq\n");
  10552. //Print the decoded XML Document
  10553. PRINT_XML_DOC_ISO1_WeldingDetectionReq(&ccs_exi_doc_ISO1);
  10554. //Save into Share Memory
  10555. SHM_Save_iso1_WeldingDetectionReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  10556. //Check for EV Error Code
  10557. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.WeldingDetectionRequest.DC_EVStatus.EVErrorCode);
  10558. errn = Proc_iso1_WeldingDetectionRes(AcceptFd);
  10559. if (errn == 0)
  10560. {
  10561. //send response successfully.
  10562. //SAVE_SYS_LOG_MSG_EVCOMM("[ISO1][V2G][Tx]WeldingDetectionRes");
  10563. }
  10564. else
  10565. {
  10566. sprintf(buf_log_evcomm,
  10567. "[Error][SeccComm][Proc_iso1_WeldingDetectionReq]Proc_iso1_WeldingDetectionRes(): %d (DEC)",
  10568. errn);
  10569. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  10570. }
  10571. return errn;
  10572. }
  10573. /*===========================================================================
  10574. FUNCTION: SHM_Init_din_SessionStopRes
  10575. DESCRIPTION:
  10576. PRE-CONDITION:
  10577. INPUT:
  10578. 1. shm_ccs
  10579. OUTPUT:
  10580. 1. shm_ccs
  10581. GLOBAL VARIABLES:
  10582. =============================================================================*/
  10583. void SHM_Init_din_SessionStopRes(struct CcsData *shm_ccs)
  10584. {
  10585. struct SessionStopResponse_DIN70121 *in;
  10586. in = &shm_ccs->V2GMessage_DIN70121.SessionStopResponse;
  10587. //----- [BODY (1/3)] ResponseCode -----
  10588. in->ResponseCode = dinresponseCodeType_OK;
  10589. }
  10590. /*===========================================================================
  10591. FUNCTION: Sudo_Parameter_din_SessionStopRes
  10592. DESCRIPTION:
  10593. PRE-CONDITION:
  10594. INPUT:
  10595. OUTPUT:
  10596. GLOBAL VARIABLES:
  10597. =============================================================================*/
  10598. void Sudo_Parameter_din_SessionStopRes()
  10599. {
  10600. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  10601. init_dinSessionStopResType(&ccs_exi_doc_DIN.V2G_Message.Body.SessionStopRes);
  10602. ccs_exi_doc_DIN.V2G_Message.Body.SessionStopRes_isUsed = 1u;
  10603. //----- [BODY (1/3)] ResponseCode -----
  10604. struct dinSessionStopResType *res;
  10605. res = &ccs_exi_doc_DIN.V2G_Message.Body.SessionStopRes;
  10606. res->ResponseCode = dinresponseCodeType_OK;
  10607. }
  10608. /*===========================================================================
  10609. FUNCTION: Sudo_Parameter_iso1_SessionStopRes
  10610. DESCRIPTION:
  10611. PRE-CONDITION:
  10612. INPUT:
  10613. OUTPUT:
  10614. GLOBAL VARIABLES:
  10615. =============================================================================*/
  10616. void Sudo_Parameter_iso1_SessionStopRes()
  10617. {
  10618. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  10619. init_iso1SessionStopResType(&ccs_exi_doc_ISO1.V2G_Message.Body.SessionStopRes);
  10620. ccs_exi_doc_ISO1.V2G_Message.Body.SessionStopRes_isUsed = 1u;
  10621. //----- [BODY (1/3)] ResponseCode -----
  10622. struct iso1SessionStopResType *res;
  10623. res = &ccs_exi_doc_ISO1.V2G_Message.Body.SessionStopRes;
  10624. res->ResponseCode = iso1responseCodeType_OK;
  10625. }
  10626. /*===========================================================================
  10627. FUNCTION: Proc_din_SessionStopRes
  10628. DESCRIPTION:
  10629. PRE-CONDITION:
  10630. INPUT:
  10631. OUTPUT:
  10632. GLOBAL VARIABLES:
  10633. =============================================================================*/
  10634. int Proc_din_SessionStopRes(int AcceptFd)
  10635. {
  10636. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10637. //if it is not the same, the packet should be ignored.
  10638. //STEP 1: ============ Initialize ============
  10639. int i = 0;
  10640. int errn = 0;
  10641. bitstream_t v2g_tx_stream;
  10642. struct SessionStopResponse_DIN70121 *stp;
  10643. struct ChargingInfoData *sys;
  10644. size_t pos = 0;
  10645. v2g_tx_stream.pos = &pos;
  10646. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10647. v2g_tx_stream.data = V2GTP_Tx_buf;
  10648. stp = &ShmCcsData->V2GMessage_DIN70121.SessionStopResponse;
  10649. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  10650. stp->ResponseCode = OK_DIN70121;
  10651. //[HEADER] Check Req SessionID
  10652. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  10653. {
  10654. stp->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  10655. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  10656. errn = -1;
  10657. }
  10658. //Check for SequenceError
  10659. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10660. {
  10661. stp->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  10662. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  10663. errn = -1;
  10664. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10665. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10666. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10667. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10668. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10669. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10670. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10671. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10672. }
  10673. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10674. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10675. #if PARAMETER_NORMAL_MODE == ENABLE
  10676. SHM_Read_din_SessionStopRes(&ccs_exi_doc_DIN, ShmCcsData);
  10677. #else
  10678. Sudo_Parameter_din_SessionStopRes();
  10679. #endif
  10680. //STEP 4: ============ Encode and Send Response Message ===========
  10681. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  10682. {
  10683. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Tx]SessionStopRes: fail");
  10684. errn = -1;
  10685. }
  10686. else
  10687. {
  10688. //send response successfully.
  10689. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Tx]SessionStopRes");
  10690. }
  10691. //STEP 5: ============ Update Flags ===========
  10692. EVCOMM_SYS_INFO.ConnectorLocked = FALSE; //Indicating CSU that the Connector is unlocked.
  10693. //Once this is set, the CSU should
  10694. // =========== Annouce to CSU [To-Be Implemented]=============
  10695. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE; //This should be also added to EV Error events.
  10696. // =========== Re-initialized [To-Be Implemented]=============
  10697. //Keep 5% PWM for 2 seconds
  10698. SAVE_SYS_LOG_MSG_EVCOMM("PWM 5\% countdown: 2");
  10699. sleep(1);
  10700. SAVE_SYS_LOG_MSG_EVCOMM("PWM 5\% countdown: 1");
  10701. sleep(1);
  10702. SAVE_SYS_LOG_MSG_EVCOMM("PWM 5\% countdown: 0");
  10703. OutputCpPwmDuty(100);
  10704. //[To-Do] Reset All Share memory
  10705. //[To-Do] CCS Module enter "idle" mode and CSU should check for this state.
  10706. //system("reboot -f");
  10707. //sleep(5);
  10708. //system("reboot -f");
  10709. return errn;
  10710. }
  10711. /*===========================================================================
  10712. FUNCTION: Proc_iso1_SessionStopRes
  10713. DESCRIPTION:
  10714. PRE-CONDITION:
  10715. INPUT:
  10716. OUTPUT:
  10717. GLOBAL VARIABLES:
  10718. =============================================================================*/
  10719. int Proc_iso1_SessionStopRes(int AcceptFd)
  10720. {
  10721. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10722. //if it is not the same, the packet should be ignored.
  10723. //STEP 1: ============ Initialize ============
  10724. int i = 0;
  10725. int errn = 0;
  10726. bitstream_t v2g_tx_stream;
  10727. struct SessionStopResponse_ISO15118_2014 *stp;
  10728. struct ChargingInfoData *sys;
  10729. size_t pos = 0;
  10730. v2g_tx_stream.pos = &pos;
  10731. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10732. v2g_tx_stream.data = V2GTP_Tx_buf;
  10733. stp = &ShmCcsData->V2GMessage_ISO15118_2014.SessionStopResponse;
  10734. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  10735. stp->ResponseCode = OK_ISO15118_2014;
  10736. //[HEADER] Check Req SessionID
  10737. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  10738. {
  10739. stp->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  10740. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]unmatched SessionID => End_Process");
  10741. errn = -1;
  10742. }
  10743. //Check for SequenceError
  10744. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10745. {
  10746. stp->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  10747. SAVE_SYS_LOG_MSG_EVCOMM("[ERROR]SequenceError => End_Process");
  10748. errn = -1;
  10749. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10750. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10751. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10752. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10753. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10754. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10755. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10756. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10757. }
  10758. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10759. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10760. #if PARAMETER_NORMAL_MODE == ENABLE
  10761. SHM_Read_iso1_SessionStopRes(&ccs_exi_doc_ISO1, ShmCcsData);
  10762. #else
  10763. Sudo_Parameter_iso1_SessionStopRes();
  10764. #endif
  10765. //STEP 4: ============ Encode and Send Response Message ===========
  10766. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  10767. {
  10768. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Tx]SessionStopRes: fail");
  10769. errn = -1;
  10770. }
  10771. else
  10772. {
  10773. //send response successfully.
  10774. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Tx]SessionStopRes");
  10775. }
  10776. //STEP 5: ============ Update Flags ===========
  10777. EVCOMM_SYS_INFO.ConnectorLocked = FALSE; //Indicating CSU that the Connector is unlocked.
  10778. //Once this is set, the CSU should
  10779. // =========== Annouce to CSU [To-Be Implemented]=============
  10780. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE; //This should be also added to EV Error events.
  10781. // =========== Re-initialized [To-Be Implemented]=============
  10782. //Keep 5% PWM for 2 seconds
  10783. SAVE_SYS_LOG_MSG_EVCOMM("PWM 5\% countdown: 2");
  10784. sleep(1);
  10785. SAVE_SYS_LOG_MSG_EVCOMM("PWM 5\% countdown: 1");
  10786. sleep(1);
  10787. SAVE_SYS_LOG_MSG_EVCOMM("PWM 5\% countdown: 0");
  10788. OutputCpPwmDuty(100);
  10789. //[To-Do] Reset All Share memory
  10790. //[To-Do] CCS Module enter "idle" mode and CSU should check for this state.
  10791. //system("reboot -f");
  10792. //sleep(5);
  10793. //system("reboot -f");
  10794. return errn;
  10795. }
  10796. /*===========================================================================
  10797. FUNCTION: Proc_din_SessionStopReq
  10798. DESCRIPTION:
  10799. PRE-CONDITION:
  10800. INPUT:
  10801. OUTPUT:
  10802. GLOBAL VARIABLES:
  10803. =============================================================================*/
  10804. int Proc_din_SessionStopReq(int AcceptFd)
  10805. {
  10806. int errn = 0;
  10807. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]SessionStopReq");
  10808. //Print the decoded XML Document
  10809. PRINT_XML_DOC_DIN_SessionStopReq(&ccs_exi_doc_DIN);
  10810. //Save into Share Memory
  10811. SHM_Save_din_SessionStopReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  10812. errn = Proc_din_SessionStopRes(AcceptFd);
  10813. if (errn == 0)
  10814. {
  10815. //send response successfully.
  10816. }
  10817. else
  10818. {
  10819. sprintf(buf_log_evcomm,
  10820. "[Error][SeccComm][Proc_din_SessionStopReq]Proc_iso1_SessionStopRes(): %d (DEC)",
  10821. errn);
  10822. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  10823. }
  10824. return errn;
  10825. }
  10826. /*===========================================================================
  10827. FUNCTION: Proc_iso1_SessionStopReq
  10828. DESCRIPTION:
  10829. PRE-CONDITION:
  10830. INPUT:
  10831. OUTPUT:
  10832. GLOBAL VARIABLES:
  10833. =============================================================================*/
  10834. int Proc_iso1_SessionStopReq(int AcceptFd)
  10835. {
  10836. int errn = 0;
  10837. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]SessionStopReq");
  10838. //Print the decoded XML Document
  10839. PRINT_XML_DOC_ISO1_SessionStopReq(&ccs_exi_doc_ISO1);
  10840. //Save into Share Memory
  10841. SHM_Save_iso1_SessionStopReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  10842. errn = Proc_iso1_SessionStopRes(AcceptFd);
  10843. if (errn == 0)
  10844. {
  10845. //send response successfully.
  10846. }
  10847. else
  10848. {
  10849. sprintf(buf_log_evcomm,
  10850. "[Error][SeccComm][Proc_iso1_SessionStopReq]Proc_iso1_SessionStopRes(): %d (DEC)",
  10851. errn);
  10852. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  10853. }
  10854. return errn;
  10855. }
  10856. /*===========================================================================
  10857. FUNCTION: V2gMsg_Process_din
  10858. DESCRIPTION:
  10859. PRE-CONDITION:
  10860. INPUT:
  10861. 1. V2gFlowStatus
  10862. OUTPUT:
  10863. GLOBAL VARIABLES:
  10864. 1. V2gFlowStatus
  10865. =============================================================================*/
  10866. int V2gMsg_Process_din(int AcceptFd)
  10867. {
  10868. unsigned char req_is_responsed = FALSE;
  10869. while (req_is_responsed == FALSE)
  10870. {
  10871. //Check if it is in End_Process
  10872. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  10873. {
  10874. break;
  10875. }
  10876. switch(V2gFlowStatus)
  10877. {
  10878. //-------------------------------------------
  10879. case SupportedAppProtocolRequest:
  10880. {
  10881. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  10882. {
  10883. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  10884. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  10885. {
  10886. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10887. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  10888. }
  10889. else
  10890. {
  10891. SAVE_SYS_LOG_MSG_EVCOMM("[Error]supportedAppProtocolResReq: fail");
  10892. Update_V2G_Flow_Status(Other_Fault);
  10893. }
  10894. }
  10895. req_is_responsed = TRUE;
  10896. break;
  10897. }
  10898. case SupportedAppProtocolResponse:
  10899. {
  10900. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  10901. {
  10902. Update_V2G_Flow_Status(SessionSetupRequest);
  10903. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10904. }
  10905. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10906. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10907. {
  10908. sprintf(buf_log_evcomm,
  10909. "[ERROR]SequenceError(%d) => Tx Res MSG",
  10910. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10911. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10912. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10913. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10914. }
  10915. else
  10916. {
  10917. break;
  10918. }
  10919. }
  10920. //-------------------------------------------
  10921. case SessionSetupRequest: //19
  10922. {
  10923. if (Proc_din_SessionSetupReq(AcceptFd) == 0)
  10924. {
  10925. Update_V2G_Flow_Status(SessionSetupResponse);
  10926. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10927. }
  10928. else
  10929. {
  10930. SAVE_SYS_LOG_MSG_EVCOMM("[Error]SessionSetupReqRes: fail");
  10931. Update_V2G_Flow_Status(Other_Fault);
  10932. }
  10933. req_is_responsed = TRUE;
  10934. break;
  10935. }
  10936. case SessionSetupResponse: //20
  10937. {
  10938. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  10939. {
  10940. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  10941. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10942. }
  10943. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10944. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10945. {
  10946. sprintf(buf_log_evcomm,
  10947. "[ERROR]SequenceError(%d) => Tx Res MSG",
  10948. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10949. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10950. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10951. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10952. }
  10953. else
  10954. {
  10955. break;
  10956. }
  10957. }
  10958. //-------------------------------------------
  10959. case ServiceDiscoveryRequest: //21
  10960. {
  10961. if (Proc_din_ServiceDiscoveryReq(AcceptFd) == 0)
  10962. {
  10963. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  10964. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10965. }
  10966. else
  10967. {
  10968. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ServiceDiscoveryReqRes: fail");
  10969. Update_V2G_Flow_Status(Other_Fault);
  10970. }
  10971. req_is_responsed = TRUE;
  10972. break;
  10973. }
  10974. case ServiceDiscoveryResponse: //22
  10975. {
  10976. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  10977. {
  10978. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  10979. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10980. }
  10981. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10982. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10983. {
  10984. sprintf(buf_log_evcomm,
  10985. "[ERROR]SequenceError(%d) => Tx Res MSG",
  10986. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10987. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10988. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10989. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10990. }
  10991. else
  10992. {
  10993. break;
  10994. }
  10995. }
  10996. //-------------------------------------------
  10997. case ServiceAndPaymentSelectionRequest: //25
  10998. {
  10999. if (Proc_din_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  11000. {
  11001. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  11002. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11003. }
  11004. else
  11005. {
  11006. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ServiceAndPaymentSelectionReqRes: fail");
  11007. Update_V2G_Flow_Status(Other_Fault);
  11008. }
  11009. req_is_responsed = TRUE;
  11010. break;
  11011. }
  11012. case ServiceAndPaymentSelectionResponse: //26
  11013. {
  11014. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11015. {
  11016. Update_V2G_Flow_Status(AuthorizationRequest);
  11017. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11018. }
  11019. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11020. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11021. {
  11022. sprintf(buf_log_evcomm,
  11023. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11024. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11025. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11026. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11027. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11028. }
  11029. else
  11030. {
  11031. break;
  11032. }
  11033. }
  11034. //-------------------------------------------
  11035. //case ContractAuthenticationReq:
  11036. case AuthorizationRequest: //29
  11037. {
  11038. if (Proc_din_ContractAuthenticationReq(AcceptFd) == 0)
  11039. {
  11040. Update_V2G_Flow_Status(AuthorizationResponse);
  11041. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11042. }
  11043. else
  11044. {
  11045. SAVE_SYS_LOG_MSG_EVCOMM("[Error]AuthorizationReqRes: fail");
  11046. Update_V2G_Flow_Status(Other_Fault);
  11047. }
  11048. req_is_responsed = TRUE;
  11049. break;
  11050. }
  11051. case AuthorizationResponse: //30
  11052. {
  11053. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11054. {
  11055. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11056. if (Proc_din_ContractAuthenticationReq(AcceptFd) == 0)
  11057. {
  11058. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11059. }
  11060. else
  11061. {
  11062. SAVE_SYS_LOG_MSG_EVCOMM("[Error]AuthorizationReqRes: fail");
  11063. Update_V2G_Flow_Status(Other_Fault);
  11064. }
  11065. req_is_responsed = TRUE;
  11066. break;
  11067. }
  11068. //Check for ChargeParameterDiscoveryReq
  11069. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11070. {
  11071. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11072. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  11073. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  11074. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11075. ftime(&SeqStartTime);
  11076. #endif
  11077. }
  11078. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11079. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11080. {
  11081. sprintf(buf_log_evcomm,
  11082. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11083. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11084. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11085. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11086. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11087. }
  11088. else
  11089. {
  11090. break;
  11091. }
  11092. }
  11093. //-------------------------------------------
  11094. case ChargeParameterDiscoveryRequest: //35
  11095. {
  11096. if (Proc_din_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11097. {
  11098. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  11099. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11100. }
  11101. else
  11102. {
  11103. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ChargeParameterDiscoveryReqRes: fail");
  11104. Update_V2G_Flow_Status(Other_Fault);
  11105. }
  11106. req_is_responsed = TRUE;
  11107. break;
  11108. }
  11109. case ChargeParameterDiscoveryResponse:
  11110. {
  11111. //STEP 1: Check for Process Timeout
  11112. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11113. ftime(&SeqEndTime);
  11114. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  11115. {
  11116. sprintf(buf_log_evcomm,
  11117. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  11118. DiffTimeb(SeqStartTime, SeqEndTime),
  11119. V2G_SECC_ChargingParameter_Performance_Time);
  11120. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  11121. Update_V2G_Flow_Status(Sequence_Timeout);
  11122. break;
  11123. }
  11124. #endif
  11125. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11126. {
  11127. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11128. if (Proc_din_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11129. {
  11130. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11131. }
  11132. else
  11133. {
  11134. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ChargeParameterDiscoveryReqRes: fail");
  11135. Update_V2G_Flow_Status(Other_Fault);
  11136. }
  11137. req_is_responsed = TRUE;
  11138. break;
  11139. }
  11140. //STEP 2: Check for CableCheckReq message
  11141. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  11142. {
  11143. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11144. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]CableCheckReqReq: isolated?");
  11145. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  11146. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  11147. Update_V2G_Flow_Status(CableCheckRequest);
  11148. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11149. ftime(&SeqStartTime);
  11150. #endif
  11151. }
  11152. //STEP 3: Wait for PowerDeliveryReq Message
  11153. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11154. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11155. {
  11156. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  11157. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11158. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  11159. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11160. ftime(&SeqStartTime);
  11161. #endif
  11162. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  11163. {
  11164. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  11165. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11166. }
  11167. else
  11168. {
  11169. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStopReqRes: fail");
  11170. Update_V2G_Flow_Status(Other_Fault);
  11171. }
  11172. break;
  11173. }
  11174. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11175. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11176. {
  11177. sprintf(buf_log_evcomm,
  11178. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11179. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11180. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11181. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11182. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11183. }
  11184. else
  11185. {
  11186. break;
  11187. }
  11188. }
  11189. //-------------------------------------------
  11190. case CableCheckRequest: //37
  11191. {
  11192. //STEP 3: Execute Cable Check Process
  11193. if (Proc_din_CableCheckReq(AcceptFd) == 0)
  11194. {
  11195. Update_V2G_Flow_Status(CableCheckResponse);
  11196. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11197. }
  11198. else
  11199. {
  11200. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CableCheckReqRes: fail");
  11201. Update_V2G_Flow_Status(Other_Fault);
  11202. }
  11203. req_is_responsed = TRUE;
  11204. break;
  11205. }
  11206. case CableCheckResponse: //38
  11207. {
  11208. //STEP 1: Check for Process Timeout
  11209. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11210. ftime(&SeqEndTime);
  11211. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  11212. {
  11213. //SAVE_SYS_LOG_MSG_EVCOMM("[CableCheck] end counting...");
  11214. sprintf(buf_log_evcomm,
  11215. "CableCheck Timeout - (%.02lf of %d ms)\n",
  11216. DiffTimeb(SeqStartTime, SeqEndTime),
  11217. V2G_SECC_CableCheck_Performance_Time);
  11218. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  11219. Update_V2G_Flow_Status(Sequence_Timeout);
  11220. //Update_ShmStatusCode(); //[To-Do] to be implemented
  11221. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  11222. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  11223. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  11224. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  11225. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  11226. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  11227. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  11228. break;
  11229. }
  11230. #endif
  11231. //STEP 2: Check for CableCheckReq message
  11232. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  11233. {
  11234. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11235. //STEP 3: Execute Cable Check Process
  11236. if (Proc_din_CableCheckReq(AcceptFd) == 0)
  11237. {
  11238. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11239. }
  11240. else
  11241. {
  11242. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CableCheckReqRes: fail");
  11243. Update_V2G_Flow_Status(Other_Fault);
  11244. }
  11245. req_is_responsed = TRUE;
  11246. break;
  11247. }
  11248. //STEP 3: Check for PreChargeReq message
  11249. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  11250. {
  11251. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11252. Update_V2G_Flow_Status(PreChargeRequest);
  11253. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]PreChargeReqReq: waiting for precharge voltage...");
  11254. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  11255. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  11256. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11257. ftime(&SeqStartTime);
  11258. #endif
  11259. }
  11260. //STEP 3: Wait for PowerDeliveryReq Message
  11261. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11262. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11263. {
  11264. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  11265. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11266. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  11267. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11268. ftime(&SeqStartTime);
  11269. #endif
  11270. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  11271. {
  11272. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  11273. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11274. }
  11275. else
  11276. {
  11277. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStopReqRes: fail");
  11278. Update_V2G_Flow_Status(Other_Fault);
  11279. }
  11280. break;
  11281. }
  11282. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11283. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11284. {
  11285. sprintf(buf_log_evcomm,
  11286. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11287. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11288. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11289. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11290. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11291. }
  11292. else
  11293. {
  11294. break;
  11295. }
  11296. }
  11297. //-------------------------------------------
  11298. case PreChargeRequest: //39
  11299. {
  11300. if (Proc_din_PreChargeReq(AcceptFd) == 0)
  11301. {
  11302. Update_V2G_Flow_Status(PreChargeResponse);
  11303. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11304. }
  11305. else
  11306. {
  11307. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PreChargeReqRes: fail");
  11308. Update_V2G_Flow_Status(Other_Fault);
  11309. }
  11310. req_is_responsed = TRUE;
  11311. break;
  11312. }
  11313. case PreChargeResponse: //40
  11314. {
  11315. //STEP 1: Check for Process Timeout
  11316. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11317. ftime(&SeqEndTime);
  11318. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  11319. {
  11320. sprintf(buf_log_evcomm,
  11321. "Precharge Timeout - (%.02lf of %d ms)\n",
  11322. DiffTimeb(SeqStartTime, SeqEndTime),
  11323. V2G_SECC_PreCharge_Performance_Time);
  11324. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  11325. Update_V2G_Flow_Status(Sequence_Timeout);
  11326. //Update_ShmStatusCode(); //[To-Do] to be implemented
  11327. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  11328. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  11329. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  11330. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  11331. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  11332. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  11333. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  11334. break;
  11335. }
  11336. #endif
  11337. //STEP 2: Check for PreChargeReq message
  11338. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  11339. {
  11340. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11341. if (Proc_din_PreChargeReq(AcceptFd) == 0)
  11342. {
  11343. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11344. }
  11345. else
  11346. {
  11347. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PreChargeReqRes: fail");
  11348. Update_V2G_Flow_Status(Other_Fault);
  11349. }
  11350. req_is_responsed = TRUE;
  11351. break;
  11352. }
  11353. //STEP 3: Check for PowerDeliveryReq message
  11354. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11355. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11356. {
  11357. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11358. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  11359. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  11360. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11361. ftime(&SeqStartTime);
  11362. #endif
  11363. }
  11364. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11365. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11366. {
  11367. sprintf(buf_log_evcomm,
  11368. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11369. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11370. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11371. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11372. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11373. }
  11374. else
  11375. {
  11376. break;
  11377. }
  11378. }
  11379. //-------------------------------------------
  11380. case PowerDeliveryRequestStart: //41
  11381. {
  11382. if (Proc_din_PowerDeliveryStartReq(AcceptFd) == 0)
  11383. {
  11384. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  11385. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11386. }
  11387. else
  11388. {
  11389. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStartReqRes: fail");
  11390. Update_V2G_Flow_Status(Other_Fault);
  11391. }
  11392. req_is_responsed = TRUE;
  11393. break;
  11394. }
  11395. case PowerDeliveryResponsetStart: //42
  11396. {
  11397. //STEP 1: Check for Process Timeout
  11398. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11399. if (ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  11400. {
  11401. ftime(&SeqEndTime);
  11402. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  11403. {
  11404. sprintf(buf_log_evcomm,
  11405. "Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n",
  11406. DiffTimeb(SeqStartTime, SeqEndTime),
  11407. 2000);
  11408. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  11409. Update_V2G_Flow_Status(Sequence_Timeout);
  11410. }
  11411. break;
  11412. }
  11413. #endif
  11414. //STEP 2: Wait for CurrentDemandReq Message
  11415. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  11416. {
  11417. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11418. Update_V2G_Flow_Status(CurrentDemandRequest);
  11419. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]CurrentDemandReqReq: energy transfering...");
  11420. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  11421. }
  11422. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11423. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11424. {
  11425. sprintf(buf_log_evcomm,
  11426. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11427. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11428. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11429. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11430. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11431. }
  11432. else
  11433. {
  11434. break;
  11435. }
  11436. }
  11437. //-------------------------------------------
  11438. case CurrentDemandRequest: //45,
  11439. {
  11440. if (Proc_din_CurrentDemandReq(AcceptFd) == 0)
  11441. {
  11442. Update_V2G_Flow_Status(CurrentDemandResponse);
  11443. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11444. }
  11445. else
  11446. {
  11447. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CurrentDemandReqRes: fail");
  11448. Update_V2G_Flow_Status(Other_Fault);
  11449. }
  11450. req_is_responsed = TRUE;
  11451. break;
  11452. }
  11453. case CurrentDemandResponse: //46,
  11454. {
  11455. //STEP 1: Wait for CurrentDemandReq Message
  11456. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  11457. {
  11458. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11459. if (Proc_din_CurrentDemandReq(AcceptFd) == 0)
  11460. {
  11461. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11462. }
  11463. else
  11464. {
  11465. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CurrentDemandReqRes: fail");
  11466. Update_V2G_Flow_Status(Other_Fault);
  11467. }
  11468. req_is_responsed = TRUE;
  11469. break;
  11470. }
  11471. //STEP 2: Wait for PowerDeliveryReq Message
  11472. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11473. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11474. {
  11475. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  11476. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11477. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  11478. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11479. ftime(&SeqStartTime);
  11480. #endif
  11481. }
  11482. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11483. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11484. {
  11485. sprintf(buf_log_evcomm,
  11486. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11487. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11488. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11489. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11490. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11491. }
  11492. else
  11493. {
  11494. break;
  11495. }
  11496. }
  11497. //-------------------------------------------
  11498. case PowerDeliveryRequestStop: //49,
  11499. {
  11500. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  11501. {
  11502. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  11503. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11504. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11505. ftime(&SeqStartTime);
  11506. #endif
  11507. }
  11508. else
  11509. {
  11510. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStopReqRes: fail");
  11511. Update_V2G_Flow_Status(Other_Fault);
  11512. }
  11513. req_is_responsed = TRUE;
  11514. break;
  11515. }
  11516. case PowerDeliveryResponseStop: //50,
  11517. {
  11518. //STEP 1: Check for Process Timeout
  11519. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11520. ftime(&SeqEndTime);
  11521. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  11522. {
  11523. sprintf(buf_log_evcomm,
  11524. "Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n",
  11525. DiffTimeb(SeqStartTime, SeqEndTime),
  11526. 2000);
  11527. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  11528. Update_V2G_Flow_Status(Sequence_Timeout);
  11529. break;
  11530. }
  11531. #endif
  11532. //STEP 2: Check for WeldingDetectionReq Message
  11533. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  11534. {
  11535. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11536. Update_V2G_Flow_Status(WeldingDetectionRequest);
  11537. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]WeldingDetectionReq: ongoing...");
  11538. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  11539. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11540. ftime(&SeqStartTime);
  11541. #endif
  11542. }
  11543. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  11544. {
  11545. Update_V2G_Flow_Status(SessionStopRequest);
  11546. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11547. if (Proc_din_SessionStopReq(AcceptFd) == 0)
  11548. {
  11549. Update_V2G_Flow_Status(SessionStopResponse);
  11550. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11551. }
  11552. else
  11553. {
  11554. SAVE_SYS_LOG_MSG_EVCOMM("[Error]SessionStopReqRes: fail");
  11555. Update_V2G_Flow_Status(Other_Fault);
  11556. }
  11557. break;
  11558. }
  11559. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11560. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11561. {
  11562. sprintf(buf_log_evcomm,
  11563. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11564. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11565. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11566. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11567. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11568. }
  11569. else
  11570. {
  11571. break;
  11572. }
  11573. }
  11574. //-------------------------------------------
  11575. case WeldingDetectionRequest: //51,
  11576. {
  11577. if (Proc_din_WeldingDetectionReq(AcceptFd) == 0)
  11578. {
  11579. Update_V2G_Flow_Status(WeldingDetectionResponse);
  11580. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11581. }
  11582. else
  11583. {
  11584. SAVE_SYS_LOG_MSG_EVCOMM("[Error]WeldingDetectionReqRes: fail");
  11585. Update_V2G_Flow_Status(Other_Fault);
  11586. }
  11587. req_is_responsed = TRUE;
  11588. break;
  11589. }
  11590. case WeldingDetectionResponse: //52,
  11591. {
  11592. //STEP 1: Check for Process Timeout
  11593. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11594. ftime(&SeqEndTime);
  11595. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  11596. {
  11597. sprintf(buf_log_evcomm,
  11598. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  11599. DiffTimeb(SeqStartTime, SeqEndTime),
  11600. V2G_SECC_WeldingDetection_Performance_Time);
  11601. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  11602. Update_V2G_Flow_Status(Sequence_Timeout);
  11603. break;
  11604. }
  11605. #endif
  11606. //STEP 2: Check for WeldingDetectionReq Message
  11607. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  11608. {
  11609. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11610. if (Proc_din_WeldingDetectionReq(AcceptFd) == 0)
  11611. {
  11612. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11613. }
  11614. else
  11615. {
  11616. SAVE_SYS_LOG_MSG_EVCOMM("[Error]WeldingDetectionReqRes: fail");
  11617. Update_V2G_Flow_Status(Other_Fault);
  11618. }
  11619. req_is_responsed = TRUE;
  11620. break;
  11621. }
  11622. //STEP 3: Check for SessionStopReq Message
  11623. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  11624. {
  11625. Update_V2G_Flow_Status(SessionStopRequest);
  11626. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11627. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  11628. }
  11629. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11630. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11631. {
  11632. sprintf(buf_log_evcomm,
  11633. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11634. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11635. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11636. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11637. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11638. }
  11639. else
  11640. {
  11641. break;
  11642. }
  11643. }
  11644. //-------------------------------------------
  11645. case SessionStopRequest: //53,
  11646. {
  11647. if (Proc_din_SessionStopReq(AcceptFd) == 0)
  11648. {
  11649. Update_V2G_Flow_Status(SessionStopResponse);
  11650. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11651. }
  11652. else
  11653. {
  11654. SAVE_SYS_LOG_MSG_EVCOMM("[Error]SessionStopReqRes: fail");
  11655. Update_V2G_Flow_Status(Other_Fault);
  11656. }
  11657. req_is_responsed = TRUE;
  11658. //break;
  11659. }
  11660. case SessionStopResponse: //54,
  11661. {
  11662. break;
  11663. }
  11664. //-------------------------------------------
  11665. default:
  11666. {
  11667. break;
  11668. }
  11669. }
  11670. }
  11671. }
  11672. /*===========================================================================
  11673. FUNCTION: V2gMsg_Process_iso1_DC
  11674. DESCRIPTION:
  11675. PRE-CONDITION:
  11676. INPUT:
  11677. 1. V2gFlowStatus
  11678. OUTPUT:
  11679. GLOBAL VARIABLES:
  11680. 1. V2gFlowStatus
  11681. =============================================================================*/
  11682. int V2gMsg_Process_iso1_DC(int AcceptFd)
  11683. {
  11684. unsigned char req_is_responsed = FALSE;
  11685. while (req_is_responsed == FALSE)
  11686. {
  11687. //Check if it is in End_Process
  11688. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  11689. {
  11690. break;
  11691. }
  11692. switch(V2gFlowStatus)
  11693. {
  11694. //-------------------------------------------
  11695. case SupportedAppProtocolRequest:
  11696. {
  11697. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  11698. {
  11699. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  11700. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  11701. {
  11702. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11703. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  11704. }
  11705. else
  11706. {
  11707. SAVE_SYS_LOG_MSG_EVCOMM("[Error]supportedAppProtocolResReq: fail");
  11708. Update_V2G_Flow_Status(Other_Fault);
  11709. }
  11710. }
  11711. req_is_responsed = TRUE;
  11712. break;
  11713. }
  11714. case SupportedAppProtocolResponse:
  11715. {
  11716. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  11717. {
  11718. Update_V2G_Flow_Status(SessionSetupRequest);
  11719. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11720. }
  11721. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11722. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11723. {
  11724. sprintf(buf_log_evcomm,
  11725. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11726. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11727. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11728. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11729. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11730. }
  11731. else
  11732. {
  11733. break;
  11734. }
  11735. }
  11736. //-------------------------------------------
  11737. case SessionSetupRequest: //19
  11738. {
  11739. if (Proc_iso1_SessionSetupReq(AcceptFd) == 0)
  11740. {
  11741. Update_V2G_Flow_Status(SessionSetupResponse);
  11742. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11743. }
  11744. else
  11745. {
  11746. SAVE_SYS_LOG_MSG_EVCOMM("[Error]SessionSetupReqRes: fail");
  11747. Update_V2G_Flow_Status(Other_Fault);
  11748. }
  11749. req_is_responsed = TRUE;
  11750. break;
  11751. }
  11752. case SessionSetupResponse: //20
  11753. {
  11754. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  11755. {
  11756. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  11757. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11758. }
  11759. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11760. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11761. {
  11762. sprintf(buf_log_evcomm,
  11763. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11764. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11765. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11766. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11767. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11768. }
  11769. else
  11770. {
  11771. break;
  11772. }
  11773. }
  11774. //-------------------------------------------
  11775. case ServiceDiscoveryRequest: //21
  11776. {
  11777. if (Proc_iso1_ServiceDiscoveryReq(AcceptFd) == 0)
  11778. {
  11779. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  11780. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11781. }
  11782. else
  11783. {
  11784. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ServiceDiscoveryReqRes: fail");
  11785. Update_V2G_Flow_Status(Other_Fault);
  11786. }
  11787. req_is_responsed = TRUE;
  11788. break;
  11789. }
  11790. case ServiceDiscoveryResponse: //22
  11791. {
  11792. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  11793. {
  11794. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  11795. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11796. }
  11797. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11798. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11799. {
  11800. sprintf(buf_log_evcomm,
  11801. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11802. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11803. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11804. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11805. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11806. }
  11807. else
  11808. {
  11809. break;
  11810. }
  11811. }
  11812. //-------------------------------------------
  11813. case ServiceAndPaymentSelectionRequest: //25
  11814. {
  11815. if (Proc_iso1_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  11816. {
  11817. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  11818. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11819. }
  11820. else
  11821. {
  11822. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ServiceAndPaymentSelectionReqRes: fail");
  11823. Update_V2G_Flow_Status(Other_Fault);
  11824. }
  11825. req_is_responsed = TRUE;
  11826. break;
  11827. }
  11828. case ServiceAndPaymentSelectionResponse: //26
  11829. {
  11830. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11831. {
  11832. Update_V2G_Flow_Status(AuthorizationRequest);
  11833. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11834. }
  11835. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11836. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11837. {
  11838. sprintf(buf_log_evcomm,
  11839. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11840. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11841. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11842. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11843. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11844. }
  11845. else
  11846. {
  11847. break;
  11848. }
  11849. }
  11850. //-------------------------------------------
  11851. //case ContractAuthenticationReq:
  11852. case AuthorizationRequest: //29
  11853. {
  11854. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  11855. {
  11856. Update_V2G_Flow_Status(AuthorizationResponse);
  11857. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11858. }
  11859. else
  11860. {
  11861. SAVE_SYS_LOG_MSG_EVCOMM("[Error]AuthorizationReqRes: fail");
  11862. Update_V2G_Flow_Status(Other_Fault);
  11863. }
  11864. req_is_responsed = TRUE;
  11865. break;
  11866. }
  11867. case AuthorizationResponse: //30
  11868. {
  11869. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11870. {
  11871. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11872. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  11873. {
  11874. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11875. }
  11876. else
  11877. {
  11878. SAVE_SYS_LOG_MSG_EVCOMM("[Error]AuthorizationReqRes: fail");
  11879. Update_V2G_Flow_Status(Other_Fault);
  11880. }
  11881. req_is_responsed = TRUE;
  11882. break;
  11883. }
  11884. //Check for ChargeParameterDiscoveryReq
  11885. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11886. {
  11887. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11888. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  11889. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  11890. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11891. ftime(&SeqStartTime);
  11892. #endif
  11893. }
  11894. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11895. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11896. {
  11897. sprintf(buf_log_evcomm,
  11898. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11899. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11900. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11901. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11902. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11903. }
  11904. else
  11905. {
  11906. break;
  11907. }
  11908. }
  11909. //-------------------------------------------
  11910. case ChargeParameterDiscoveryRequest: //35
  11911. {
  11912. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11913. {
  11914. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  11915. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11916. }
  11917. else
  11918. {
  11919. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ChargeParameterDiscoveryReqRes: fail");
  11920. Update_V2G_Flow_Status(Other_Fault);
  11921. }
  11922. req_is_responsed = TRUE;
  11923. break;
  11924. }
  11925. case ChargeParameterDiscoveryResponse:
  11926. {
  11927. //STEP 1: Check for Process Timeout
  11928. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11929. ftime(&SeqEndTime);
  11930. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  11931. {
  11932. sprintf(buf_log_evcomm,
  11933. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  11934. DiffTimeb(SeqStartTime, SeqEndTime),
  11935. V2G_SECC_ChargingParameter_Performance_Time);
  11936. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  11937. Update_V2G_Flow_Status(Sequence_Timeout);
  11938. break;
  11939. }
  11940. #endif
  11941. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11942. {
  11943. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11944. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11945. {
  11946. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11947. }
  11948. else
  11949. {
  11950. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ChargeParameterDiscoveryReqRes: fail");
  11951. Update_V2G_Flow_Status(Other_Fault);
  11952. }
  11953. req_is_responsed = TRUE;
  11954. break;
  11955. }
  11956. //STEP 2: Check for CableCheckReq message
  11957. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  11958. {
  11959. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11960. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]CableCheckReqReq: isolated?");
  11961. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  11962. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  11963. Update_V2G_Flow_Status(CableCheckRequest);
  11964. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11965. ftime(&SeqStartTime);
  11966. #endif
  11967. }
  11968. //STEP 3: Wait for PowerDeliveryReq Message
  11969. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11970. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11971. {
  11972. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  11973. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11974. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  11975. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11976. ftime(&SeqStartTime);
  11977. #endif
  11978. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  11979. {
  11980. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  11981. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11982. }
  11983. else
  11984. {
  11985. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStopReqRes: fail");
  11986. Update_V2G_Flow_Status(Other_Fault);
  11987. }
  11988. break;
  11989. }
  11990. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11991. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11992. {
  11993. sprintf(buf_log_evcomm,
  11994. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11995. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11996. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11997. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11998. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11999. }
  12000. else
  12001. {
  12002. break;
  12003. }
  12004. }
  12005. //-------------------------------------------
  12006. case CableCheckRequest: //37
  12007. {
  12008. //STEP 3: Execute Cable Check Process
  12009. if (Proc_iso1_CableCheckReq(AcceptFd) == 0)
  12010. {
  12011. Update_V2G_Flow_Status(CableCheckResponse);
  12012. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12013. }
  12014. else
  12015. {
  12016. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CableCheckReqRes: fail");
  12017. Update_V2G_Flow_Status(Other_Fault);
  12018. }
  12019. req_is_responsed = TRUE;
  12020. break;
  12021. }
  12022. case CableCheckResponse: //38
  12023. {
  12024. //STEP 1: Check for Process Timeout
  12025. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12026. ftime(&SeqEndTime);
  12027. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  12028. {
  12029. //SAVE_SYS_LOG_MSG_EVCOMM("[CableCheck] end counting...");
  12030. sprintf(buf_log_evcomm,
  12031. "CableCheck Timeout - (%.02lf of %d ms)\n",
  12032. DiffTimeb(SeqStartTime, SeqEndTime),
  12033. V2G_SECC_CableCheck_Performance_Time);
  12034. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  12035. Update_V2G_Flow_Status(Sequence_Timeout);
  12036. //Update_ShmStatusCode(); //[To-Do] to be implemented
  12037. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  12038. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  12039. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  12040. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  12041. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  12042. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  12043. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  12044. break;
  12045. }
  12046. #endif
  12047. //STEP 2: Check for CableCheckReq message
  12048. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  12049. {
  12050. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12051. //STEP 3: Execute Cable Check Process
  12052. if (Proc_iso1_CableCheckReq(AcceptFd) == 0)
  12053. {
  12054. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12055. }
  12056. else
  12057. {
  12058. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CableCheckReqRes: fail");
  12059. Update_V2G_Flow_Status(Other_Fault);
  12060. }
  12061. req_is_responsed = TRUE;
  12062. break;
  12063. }
  12064. //STEP 3: Check for PreChargeReq message
  12065. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  12066. {
  12067. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12068. Update_V2G_Flow_Status(PreChargeRequest);
  12069. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]PreChargeReqReq: waiting for precharge voltage...");
  12070. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  12071. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  12072. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12073. ftime(&SeqStartTime);
  12074. #endif
  12075. }
  12076. //STEP 3: Wait for PowerDeliveryReq Message
  12077. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12078. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12079. {
  12080. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  12081. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12082. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  12083. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12084. ftime(&SeqStartTime);
  12085. #endif
  12086. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12087. {
  12088. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12089. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12090. }
  12091. else
  12092. {
  12093. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStopReqRes: fail");
  12094. Update_V2G_Flow_Status(Other_Fault);
  12095. }
  12096. break;
  12097. }
  12098. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12099. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12100. {
  12101. sprintf(buf_log_evcomm,
  12102. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12103. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12104. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12105. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12106. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12107. }
  12108. else
  12109. {
  12110. break;
  12111. }
  12112. }
  12113. //-------------------------------------------
  12114. case PreChargeRequest: //39
  12115. {
  12116. if (Proc_iso1_PreChargeReq(AcceptFd) == 0)
  12117. {
  12118. Update_V2G_Flow_Status(PreChargeResponse);
  12119. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12120. }
  12121. else
  12122. {
  12123. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PreChargeReqRes: fail");
  12124. Update_V2G_Flow_Status(Other_Fault);
  12125. }
  12126. req_is_responsed = TRUE;
  12127. break;
  12128. }
  12129. case PreChargeResponse: //40
  12130. {
  12131. //STEP 1: Check for Process Timeout
  12132. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12133. ftime(&SeqEndTime);
  12134. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  12135. {
  12136. sprintf(buf_log_evcomm,
  12137. "Precharge Timeout - (%.02lf of %d ms)\n",
  12138. DiffTimeb(SeqStartTime, SeqEndTime),
  12139. V2G_SECC_PreCharge_Performance_Time);
  12140. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  12141. Update_V2G_Flow_Status(Sequence_Timeout);
  12142. //Update_ShmStatusCode(); //[To-Do] to be implemented
  12143. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  12144. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  12145. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  12146. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  12147. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  12148. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  12149. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  12150. break;
  12151. }
  12152. #endif
  12153. //STEP 2: Check for PreChargeReq message
  12154. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  12155. {
  12156. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12157. if (Proc_iso1_PreChargeReq(AcceptFd) == 0)
  12158. {
  12159. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12160. }
  12161. else
  12162. {
  12163. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PreChargeReqRes: fail");
  12164. Update_V2G_Flow_Status(Other_Fault);
  12165. }
  12166. req_is_responsed = TRUE;
  12167. break;
  12168. }
  12169. //STEP 3: Check for PowerDeliveryReq message
  12170. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12171. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12172. {
  12173. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12174. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  12175. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  12176. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12177. ftime(&SeqStartTime);
  12178. #endif
  12179. }
  12180. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12181. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12182. {
  12183. sprintf(buf_log_evcomm,
  12184. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12185. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12186. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12187. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12188. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12189. }
  12190. else
  12191. {
  12192. break;
  12193. }
  12194. }
  12195. //-------------------------------------------
  12196. case PowerDeliveryRequestStart: //41
  12197. {
  12198. if (Proc_iso1_PowerDeliveryStartReq(AcceptFd) == 0)
  12199. {
  12200. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  12201. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12202. }
  12203. else
  12204. {
  12205. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStartReqRes: fail");
  12206. Update_V2G_Flow_Status(Other_Fault);
  12207. }
  12208. req_is_responsed = TRUE;
  12209. break;
  12210. }
  12211. case PowerDeliveryResponsetStart: //42
  12212. {
  12213. //STEP 1: Check for Process Timeout
  12214. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12215. if (ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  12216. {
  12217. ftime(&SeqEndTime);
  12218. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  12219. {
  12220. sprintf(buf_log_evcomm,
  12221. "Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n",
  12222. DiffTimeb(SeqStartTime, SeqEndTime),
  12223. 2000);
  12224. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  12225. Update_V2G_Flow_Status(Sequence_Timeout);
  12226. }
  12227. break;
  12228. }
  12229. #endif
  12230. //STEP 2: Wait for CurrentDemandReq Message
  12231. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  12232. {
  12233. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12234. Update_V2G_Flow_Status(CurrentDemandRequest);
  12235. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]CurrentDemandReqReq: energy transfering...");
  12236. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  12237. }
  12238. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12239. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12240. {
  12241. sprintf(buf_log_evcomm,
  12242. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12243. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12244. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12245. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12246. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12247. }
  12248. else
  12249. {
  12250. break;
  12251. }
  12252. }
  12253. //-------------------------------------------
  12254. case CurrentDemandRequest: //45,
  12255. {
  12256. if (Proc_iso1_CurrentDemandReq(AcceptFd) == 0)
  12257. {
  12258. Update_V2G_Flow_Status(CurrentDemandResponse);
  12259. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12260. }
  12261. else
  12262. {
  12263. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CurrentDemandReqRes: fail");
  12264. Update_V2G_Flow_Status(Other_Fault);
  12265. }
  12266. req_is_responsed = TRUE;
  12267. break;
  12268. }
  12269. case CurrentDemandResponse: //46,
  12270. {
  12271. //STEP 1: Wait for CurrentDemandReq Message
  12272. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  12273. {
  12274. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12275. if (Proc_iso1_CurrentDemandReq(AcceptFd) == 0)
  12276. {
  12277. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12278. }
  12279. else
  12280. {
  12281. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CurrentDemandReqRes: fail");
  12282. Update_V2G_Flow_Status(Other_Fault);
  12283. }
  12284. req_is_responsed = TRUE;
  12285. break;
  12286. }
  12287. //STEP 2: Wait for PowerDeliveryReq Message
  12288. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12289. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12290. {
  12291. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  12292. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12293. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  12294. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12295. ftime(&SeqStartTime);
  12296. #endif
  12297. }
  12298. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12299. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12300. {
  12301. sprintf(buf_log_evcomm,
  12302. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12303. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12304. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12305. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12306. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12307. }
  12308. else
  12309. {
  12310. break;
  12311. }
  12312. }
  12313. //-------------------------------------------
  12314. case PowerDeliveryRequestStop: //49,
  12315. {
  12316. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12317. {
  12318. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12319. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12320. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12321. ftime(&SeqStartTime);
  12322. #endif
  12323. }
  12324. else
  12325. {
  12326. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStopReqRes: fail");
  12327. Update_V2G_Flow_Status(Other_Fault);
  12328. }
  12329. req_is_responsed = TRUE;
  12330. break;
  12331. }
  12332. case PowerDeliveryResponseStop: //50,
  12333. {
  12334. //STEP 1: Check for Process Timeout
  12335. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12336. ftime(&SeqEndTime);
  12337. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  12338. {
  12339. sprintf(buf_log_evcomm,
  12340. "Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n",
  12341. DiffTimeb(SeqStartTime, SeqEndTime),
  12342. 2000);
  12343. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  12344. Update_V2G_Flow_Status(Sequence_Timeout);
  12345. break;
  12346. }
  12347. #endif
  12348. //STEP 2: Check for WeldingDetectionReq Message
  12349. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  12350. {
  12351. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12352. Update_V2G_Flow_Status(WeldingDetectionRequest);
  12353. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]WeldingDetectionReq: ongoing...");
  12354. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  12355. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12356. ftime(&SeqStartTime);
  12357. #endif
  12358. }
  12359. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  12360. {
  12361. Update_V2G_Flow_Status(SessionStopRequest);
  12362. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12363. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  12364. {
  12365. Update_V2G_Flow_Status(SessionStopResponse);
  12366. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12367. }
  12368. else
  12369. {
  12370. SAVE_SYS_LOG_MSG_EVCOMM("[Error]SessionStopReqRes: fail");
  12371. Update_V2G_Flow_Status(Other_Fault);
  12372. }
  12373. break;
  12374. }
  12375. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12376. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12377. {
  12378. sprintf(buf_log_evcomm,
  12379. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12380. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12381. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12382. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12383. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12384. }
  12385. else
  12386. {
  12387. break;
  12388. }
  12389. }
  12390. //-------------------------------------------
  12391. case WeldingDetectionRequest: //51,
  12392. {
  12393. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  12394. {
  12395. Update_V2G_Flow_Status(WeldingDetectionResponse);
  12396. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12397. }
  12398. else
  12399. {
  12400. SAVE_SYS_LOG_MSG_EVCOMM("[Error]WeldingDetectionReqRes: fail");
  12401. Update_V2G_Flow_Status(Other_Fault);
  12402. }
  12403. req_is_responsed = TRUE;
  12404. break;
  12405. }
  12406. case WeldingDetectionResponse: //52,
  12407. {
  12408. //STEP 1: Check for Process Timeout
  12409. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12410. ftime(&SeqEndTime);
  12411. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  12412. {
  12413. sprintf(buf_log_evcomm,
  12414. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  12415. DiffTimeb(SeqStartTime, SeqEndTime),
  12416. V2G_SECC_WeldingDetection_Performance_Time);
  12417. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  12418. Update_V2G_Flow_Status(Sequence_Timeout);
  12419. break;
  12420. }
  12421. #endif
  12422. //STEP 2: Check for WeldingDetectionReq Message
  12423. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  12424. {
  12425. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12426. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  12427. {
  12428. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12429. }
  12430. else
  12431. {
  12432. SAVE_SYS_LOG_MSG_EVCOMM("[Error]WeldingDetectionReqRes: fail");
  12433. Update_V2G_Flow_Status(Other_Fault);
  12434. }
  12435. req_is_responsed = TRUE;
  12436. break;
  12437. }
  12438. //STEP 3: Check for SessionStopReq Message
  12439. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  12440. {
  12441. Update_V2G_Flow_Status(SessionStopRequest);
  12442. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12443. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  12444. }
  12445. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12446. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12447. {
  12448. sprintf(buf_log_evcomm,
  12449. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12450. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12451. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12452. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12453. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12454. }
  12455. else
  12456. {
  12457. break;
  12458. }
  12459. }
  12460. //-------------------------------------------
  12461. case SessionStopRequest: //53,
  12462. {
  12463. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  12464. {
  12465. Update_V2G_Flow_Status(SessionStopResponse);
  12466. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12467. }
  12468. else
  12469. {
  12470. SAVE_SYS_LOG_MSG_EVCOMM("[Error]SessionStopReqRes: fail");
  12471. Update_V2G_Flow_Status(Other_Fault);
  12472. }
  12473. req_is_responsed = TRUE;
  12474. //break;
  12475. }
  12476. case SessionStopResponse: //54,
  12477. {
  12478. break;
  12479. }
  12480. //-------------------------------------------
  12481. default:
  12482. {
  12483. break;
  12484. }
  12485. }
  12486. }
  12487. }
  12488. /*===========================================================================
  12489. FUNCTION: V2gMsg_Process_iso1_AC
  12490. DESCRIPTION:
  12491. PRE-CONDITION:
  12492. INPUT:
  12493. 1. V2gFlowStatus
  12494. OUTPUT:
  12495. GLOBAL VARIABLES:
  12496. 1. V2gFlowStatus
  12497. =============================================================================*/
  12498. int V2gMsg_Process_iso1_AC(int AcceptFd)
  12499. {
  12500. unsigned char req_is_responsed = FALSE;
  12501. while (req_is_responsed == FALSE)
  12502. {
  12503. //Check if it is in End_Process
  12504. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  12505. {
  12506. break;
  12507. }
  12508. switch(V2gFlowStatus)
  12509. {
  12510. //-------------------------------------------
  12511. case SupportedAppProtocolRequest: //17
  12512. {
  12513. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  12514. {
  12515. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  12516. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  12517. {
  12518. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12519. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  12520. }
  12521. else
  12522. {
  12523. SAVE_SYS_LOG_MSG_EVCOMM("[Error]supportedAppProtocolResReq: fail");
  12524. Update_V2G_Flow_Status(Other_Fault);
  12525. }
  12526. }
  12527. req_is_responsed = TRUE;
  12528. break;
  12529. }
  12530. case SupportedAppProtocolResponse: //18
  12531. {
  12532. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  12533. {
  12534. Update_V2G_Flow_Status(SessionSetupRequest);
  12535. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12536. }
  12537. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12538. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12539. {
  12540. sprintf(buf_log_evcomm,
  12541. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12542. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12543. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12544. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12545. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12546. }
  12547. else
  12548. {
  12549. break;
  12550. }
  12551. }
  12552. //-------------------------------------------
  12553. case SessionSetupRequest: //19
  12554. {
  12555. if (Proc_iso1_SessionSetupReq(AcceptFd) == 0)
  12556. {
  12557. Update_V2G_Flow_Status(SessionSetupResponse);
  12558. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12559. }
  12560. else
  12561. {
  12562. SAVE_SYS_LOG_MSG_EVCOMM("[Error]SessionSetupReqRes: fail");
  12563. Update_V2G_Flow_Status(Other_Fault);
  12564. }
  12565. req_is_responsed = TRUE;
  12566. break;
  12567. }
  12568. case SessionSetupResponse: //20
  12569. {
  12570. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  12571. {
  12572. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  12573. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12574. }
  12575. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12576. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12577. {
  12578. sprintf(buf_log_evcomm,
  12579. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12580. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12581. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12582. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12583. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12584. }
  12585. else
  12586. {
  12587. break;
  12588. }
  12589. }
  12590. //-------------------------------------------
  12591. case ServiceDiscoveryRequest: //21
  12592. {
  12593. if (Proc_iso1_ServiceDiscoveryReq(AcceptFd) == 0)
  12594. {
  12595. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  12596. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12597. }
  12598. else
  12599. {
  12600. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ServiceDiscoveryReqRes: fail");
  12601. Update_V2G_Flow_Status(Other_Fault);
  12602. }
  12603. req_is_responsed = TRUE;
  12604. break;
  12605. }
  12606. case ServiceDiscoveryResponse: //22
  12607. {
  12608. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  12609. {
  12610. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  12611. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12612. }
  12613. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12614. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12615. {
  12616. sprintf(buf_log_evcomm,
  12617. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12618. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12619. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12620. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12621. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12622. }
  12623. else
  12624. {
  12625. break;
  12626. }
  12627. }
  12628. //-------------------------------------------
  12629. case ServiceAndPaymentSelectionRequest: //25
  12630. {
  12631. if (Proc_iso1_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  12632. {
  12633. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  12634. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12635. }
  12636. else
  12637. {
  12638. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ServiceAndPaymentSelectionReqRes: fail");
  12639. Update_V2G_Flow_Status(Other_Fault);
  12640. }
  12641. req_is_responsed = TRUE;
  12642. break;
  12643. }
  12644. case ServiceAndPaymentSelectionResponse://26
  12645. {
  12646. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  12647. {
  12648. Update_V2G_Flow_Status(AuthorizationRequest);
  12649. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12650. }
  12651. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12652. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12653. {
  12654. sprintf(buf_log_evcomm,
  12655. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12656. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12657. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12658. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12659. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12660. }
  12661. else
  12662. {
  12663. break;
  12664. }
  12665. }
  12666. //-------------------------------------------
  12667. //case ContractAuthenticationReq:
  12668. case AuthorizationRequest: //29
  12669. {
  12670. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  12671. {
  12672. Update_V2G_Flow_Status(AuthorizationResponse);
  12673. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12674. }
  12675. else
  12676. {
  12677. SAVE_SYS_LOG_MSG_EVCOMM("[Error]AuthorizationReqRes: fail");
  12678. Update_V2G_Flow_Status(Other_Fault);
  12679. }
  12680. break;
  12681. }
  12682. case AuthorizationResponse: //30
  12683. {
  12684. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  12685. {
  12686. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12687. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  12688. {
  12689. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12690. }
  12691. else
  12692. {
  12693. SAVE_SYS_LOG_MSG_EVCOMM("[Error]AuthorizationReqRes: fail");
  12694. Update_V2G_Flow_Status(Other_Fault);
  12695. }
  12696. req_is_responsed = TRUE;
  12697. break;
  12698. }
  12699. //Check for ChargeParameterDiscoveryReq
  12700. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  12701. {
  12702. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12703. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  12704. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  12705. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12706. ftime(&SeqStartTime);
  12707. #endif
  12708. }
  12709. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12710. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12711. {
  12712. sprintf(buf_log_evcomm,
  12713. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12714. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12715. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12716. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12717. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12718. }
  12719. else
  12720. {
  12721. break;
  12722. }
  12723. }
  12724. //-------------------------------------------
  12725. case ChargeParameterDiscoveryRequest: //35
  12726. {
  12727. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  12728. {
  12729. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  12730. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12731. }
  12732. else
  12733. {
  12734. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ChargeParameterDiscoveryReqRes: fail");
  12735. Update_V2G_Flow_Status(Other_Fault);
  12736. }
  12737. req_is_responsed = TRUE;
  12738. break;
  12739. }
  12740. case ChargeParameterDiscoveryResponse: //36
  12741. {
  12742. //STEP 1: Check for Process Timeout
  12743. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12744. ftime(&SeqEndTime);
  12745. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  12746. {
  12747. sprintf(buf_log_evcomm,
  12748. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  12749. DiffTimeb(SeqStartTime, SeqEndTime),
  12750. V2G_SECC_ChargingParameter_Performance_Time);
  12751. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  12752. Update_V2G_Flow_Status(Sequence_Timeout);
  12753. break;
  12754. }
  12755. #endif
  12756. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  12757. {
  12758. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12759. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  12760. {
  12761. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12762. }
  12763. else
  12764. {
  12765. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ChargeParameterDiscoveryReqRes: fail");
  12766. Update_V2G_Flow_Status(Other_Fault);
  12767. }
  12768. req_is_responsed = TRUE;
  12769. break;
  12770. }
  12771. //STEP 3: Wait for PowerDeliveryReq Message
  12772. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12773. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12774. {
  12775. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  12776. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12777. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  12778. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12779. ftime(&SeqStartTime);
  12780. #endif
  12781. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12782. {
  12783. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12784. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12785. }
  12786. else
  12787. {
  12788. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStopReqRes: fail");
  12789. Update_V2G_Flow_Status(Other_Fault);
  12790. }
  12791. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12792. ftime(&SeqStartTime);
  12793. #endif
  12794. break;
  12795. }
  12796. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12797. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12798. {
  12799. sprintf(buf_log_evcomm,
  12800. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12801. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12802. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12803. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12804. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12805. }
  12806. else
  12807. {
  12808. break;
  12809. }
  12810. }
  12811. //-------------------------------------------
  12812. case PowerDeliveryRequestStart: //41
  12813. {
  12814. if (Proc_iso1_PowerDeliveryStartReq(AcceptFd) == 0)
  12815. {
  12816. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  12817. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12818. }
  12819. else
  12820. {
  12821. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStartReqRes: fail");
  12822. Update_V2G_Flow_Status(Other_Fault);
  12823. }
  12824. req_is_responsed = TRUE;
  12825. break;
  12826. }
  12827. case PowerDeliveryResponsetStart: //42
  12828. {
  12829. //STEP 1: Check for Process Timeout
  12830. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12831. if (ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  12832. {
  12833. ftime(&SeqEndTime);
  12834. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  12835. {
  12836. sprintf(buf_log_evcomm,
  12837. "Wait for ChargingStatusReq Timeout - (%.02lf of %d ms)\n",
  12838. DiffTimeb(SeqStartTime, SeqEndTime),
  12839. 2000);
  12840. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  12841. Update_V2G_Flow_Status(Sequence_Timeout);
  12842. }
  12843. break;
  12844. }
  12845. #endif
  12846. //STEP 2: Wait for ChargingStatusReq Message
  12847. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargingStatusRequest)
  12848. {
  12849. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12850. Update_V2G_Flow_Status(ChargingStatusRequest);
  12851. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]ChargingStatusReq: energy transfering...");
  12852. ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargingStatus_Timer_Start);
  12853. }
  12854. else
  12855. {
  12856. break;
  12857. }
  12858. }
  12859. //-------------------------------------------
  12860. case ChargingStatusRequest: //43
  12861. {
  12862. if (Proc_iso1_ChargingStatusReq(AcceptFd) == 0)
  12863. {
  12864. Update_V2G_Flow_Status(ChargingStatusResponse);
  12865. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12866. }
  12867. else
  12868. {
  12869. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ChargingStatusRes: fail");
  12870. Update_V2G_Flow_Status(Other_Fault);
  12871. }
  12872. req_is_responsed = TRUE;
  12873. break;
  12874. }
  12875. case ChargingStatusResponse: //44
  12876. {
  12877. //STEP 1: Wait for ChargingStatusReq Message
  12878. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargingStatusRequest)
  12879. {
  12880. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12881. if (Proc_iso1_ChargingStatusReq(AcceptFd) == 0)
  12882. {
  12883. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12884. }
  12885. else
  12886. {
  12887. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ChargingStatusRes: fail");
  12888. Update_V2G_Flow_Status(Other_Fault);
  12889. }
  12890. req_is_responsed = TRUE;
  12891. break;
  12892. }
  12893. //STEP 2: Wait for PowerDeliveryReq Message
  12894. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12895. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12896. {
  12897. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  12898. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12899. ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargingStatus_Timer_End);
  12900. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12901. ftime(&SeqStartTime);
  12902. #endif
  12903. }
  12904. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12905. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12906. {
  12907. sprintf(buf_log_evcomm,
  12908. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12909. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12910. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12911. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12912. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12913. }
  12914. else
  12915. {
  12916. break;
  12917. }
  12918. }
  12919. //-------------------------------------------
  12920. case PowerDeliveryRequestStop: //49
  12921. {
  12922. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12923. {
  12924. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12925. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12926. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12927. ftime(&SeqStartTime);
  12928. #endif
  12929. }
  12930. else
  12931. {
  12932. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStopReqRes: fail");
  12933. Update_V2G_Flow_Status(Other_Fault);
  12934. }
  12935. req_is_responsed = TRUE;
  12936. break;
  12937. }
  12938. case PowerDeliveryResponseStop: //50
  12939. {
  12940. //STEP 1: Check for Process Timeout
  12941. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12942. ftime(&SeqEndTime);
  12943. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  12944. {
  12945. sprintf(buf_log_evcomm,
  12946. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  12947. DiffTimeb(SeqStartTime, SeqEndTime),
  12948. 2000);
  12949. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  12950. Update_V2G_Flow_Status(Sequence_Timeout);
  12951. break;
  12952. }
  12953. #endif
  12954. //STEP 3: Check for SessionStopReq Message
  12955. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  12956. {
  12957. Update_V2G_Flow_Status(SessionStopRequest);
  12958. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12959. }
  12960. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12961. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12962. {
  12963. sprintf(buf_log_evcomm,
  12964. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12965. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12966. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12967. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12968. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12969. }
  12970. else
  12971. {
  12972. break;
  12973. }
  12974. }
  12975. //-------------------------------------------
  12976. case SessionStopRequest: //53
  12977. {
  12978. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  12979. {
  12980. Update_V2G_Flow_Status(SessionStopResponse);
  12981. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12982. }
  12983. else
  12984. {
  12985. SAVE_SYS_LOG_MSG_EVCOMM("[Error]SessionStopReqRes: fail");
  12986. Update_V2G_Flow_Status(Other_Fault);
  12987. }
  12988. req_is_responsed = TRUE;
  12989. //break;
  12990. }
  12991. case SessionStopResponse: //54
  12992. {
  12993. break;
  12994. }
  12995. //-------------------------------------------
  12996. default:
  12997. {
  12998. break;
  12999. }
  13000. }
  13001. }
  13002. }
  13003. /*===========================================================================
  13004. FUNCTION: V2gMsg_Process_iso1
  13005. DESCRIPTION:
  13006. 1. EnergyTransferMode:
  13007. AC_single_phase_core = 0,
  13008. AC_three_phase_core = 1,
  13009. DC_core = 2,
  13010. DC_extended = 3,
  13011. DC_combo_core = 4,
  13012. DC_unique = 5
  13013. iso1EnergyTransferModeType_AC_single_phase_core = 0,
  13014. iso1EnergyTransferModeType_AC_three_phase_core = 1,
  13015. iso1EnergyTransferModeType_DC_core = 2,
  13016. iso1EnergyTransferModeType_DC_extended = 3,
  13017. iso1EnergyTransferModeType_DC_combo_core = 4,
  13018. iso1EnergyTransferModeType_DC_unique = 5
  13019. PRE-CONDITION:
  13020. INPUT:
  13021. 1. V2gFlowStatus
  13022. OUTPUT:
  13023. GLOBAL VARIABLES:
  13024. 1. V2gFlowStatus
  13025. =============================================================================*/
  13026. int V2gMsg_Process_iso1(int AcceptFd, unsigned char EnergyTransferMode)
  13027. {
  13028. switch (EnergyTransferMode)
  13029. {
  13030. case DC_extended:
  13031. {
  13032. V2gMsg_Process_iso1_DC(AcceptFd);
  13033. break;
  13034. }
  13035. case AC_single_phase_core:
  13036. case AC_three_phase_core:
  13037. {
  13038. V2gMsg_Process_iso1_AC(AcceptFd);
  13039. break;
  13040. }
  13041. default:
  13042. {
  13043. sprintf(buf_log_evcomm,
  13044. "[ISO1][Warning]Unexpected EnergyTransferMode(%d)",
  13045. EnergyTransferMode);
  13046. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  13047. break;
  13048. }
  13049. }
  13050. }
  13051. /*===========================================================================
  13052. FUNCTION: V2gMsg_Process_iso2_DC
  13053. DESCRIPTION:
  13054. PRE-CONDITION:
  13055. INPUT:
  13056. 1. V2gFlowStatus
  13057. OUTPUT:
  13058. GLOBAL VARIABLES:
  13059. 1. V2gFlowStatus
  13060. =============================================================================*/
  13061. int V2gMsg_Process_iso2_DC(int AcceptFd)
  13062. {
  13063. unsigned char req_is_responsed = FALSE;
  13064. while (req_is_responsed == FALSE)
  13065. {
  13066. //Check if it is in End_Process
  13067. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  13068. {
  13069. break;
  13070. }
  13071. switch(V2gFlowStatus)
  13072. {
  13073. //-------------------------------------------
  13074. case SupportedAppProtocolRequest:
  13075. {
  13076. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  13077. {
  13078. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  13079. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  13080. {
  13081. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13082. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  13083. }
  13084. else
  13085. {
  13086. SAVE_SYS_LOG_MSG_EVCOMM("[Error]supportedAppProtocolResReq: fail");
  13087. Update_V2G_Flow_Status(Other_Fault);
  13088. }
  13089. }
  13090. req_is_responsed = TRUE;
  13091. break;
  13092. }
  13093. case SupportedAppProtocolResponse:
  13094. {
  13095. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  13096. {
  13097. Update_V2G_Flow_Status(SessionSetupRequest);
  13098. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13099. }
  13100. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13101. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13102. {
  13103. sprintf(buf_log_evcomm,
  13104. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13105. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13106. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13107. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13108. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13109. }
  13110. else
  13111. {
  13112. break;
  13113. }
  13114. }
  13115. //-------------------------------------------
  13116. case SessionSetupRequest: //19
  13117. {
  13118. if (Proc_iso2_SessionSetupReq(AcceptFd) == 0)
  13119. {
  13120. Update_V2G_Flow_Status(SessionSetupResponse);
  13121. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13122. }
  13123. else
  13124. {
  13125. SAVE_SYS_LOG_MSG_EVCOMM("[Error]SessionSetupReqRes: fail");
  13126. Update_V2G_Flow_Status(Other_Fault);
  13127. }
  13128. req_is_responsed = TRUE;
  13129. break;
  13130. }
  13131. case SessionSetupResponse: //20
  13132. {
  13133. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  13134. {
  13135. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  13136. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13137. }
  13138. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13139. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13140. {
  13141. sprintf(buf_log_evcomm,
  13142. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13143. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13144. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13145. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13146. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13147. }
  13148. else
  13149. {
  13150. break;
  13151. }
  13152. }
  13153. #if 0
  13154. //-------------------------------------------
  13155. case ServiceDiscoveryRequest: //21
  13156. {
  13157. if (Proc_iso2_ServiceDiscoveryReq(AcceptFd) == 0)
  13158. {
  13159. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  13160. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13161. }
  13162. else
  13163. {
  13164. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ServiceDiscoveryReqRes: fail");
  13165. Update_V2G_Flow_Status(Other_Fault);
  13166. }
  13167. req_is_responsed = TRUE;
  13168. break;
  13169. }
  13170. case ServiceDiscoveryResponse: //22
  13171. {
  13172. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  13173. {
  13174. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  13175. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13176. }
  13177. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13178. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13179. {
  13180. sprintf(buf_log_evcomm,
  13181. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13182. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13183. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13184. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13185. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13186. }
  13187. else
  13188. {
  13189. break;
  13190. }
  13191. }
  13192. //-------------------------------------------
  13193. case ServiceAndPaymentSelectionRequest: //25
  13194. {
  13195. if (Proc_iso2_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  13196. {
  13197. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  13198. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13199. }
  13200. else
  13201. {
  13202. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ServiceAndPaymentSelectionReqRes: fail");
  13203. Update_V2G_Flow_Status(Other_Fault);
  13204. }
  13205. req_is_responsed = TRUE;
  13206. break;
  13207. }
  13208. case ServiceAndPaymentSelectionResponse: //26
  13209. {
  13210. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  13211. {
  13212. Update_V2G_Flow_Status(AuthorizationRequest);
  13213. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13214. }
  13215. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13216. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13217. {
  13218. sprintf(buf_log_evcomm,
  13219. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13220. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13221. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13222. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13223. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13224. }
  13225. else
  13226. {
  13227. break;
  13228. }
  13229. }
  13230. //-------------------------------------------
  13231. //case ContractAuthenticationReq:
  13232. case AuthorizationRequest: //29
  13233. {
  13234. if (Proc_iso2_AuthenticationReq(AcceptFd) == 0)
  13235. {
  13236. Update_V2G_Flow_Status(AuthorizationResponse);
  13237. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13238. }
  13239. else
  13240. {
  13241. SAVE_SYS_LOG_MSG_EVCOMM("[Error]AuthorizationReqRes: fail");
  13242. Update_V2G_Flow_Status(Other_Fault);
  13243. }
  13244. req_is_responsed = TRUE;
  13245. break;
  13246. }
  13247. case AuthorizationResponse: //30
  13248. {
  13249. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  13250. {
  13251. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13252. if (Proc_iso2_AuthenticationReq(AcceptFd) == 0)
  13253. {
  13254. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13255. }
  13256. else
  13257. {
  13258. SAVE_SYS_LOG_MSG_EVCOMM("[Error]AuthorizationReqRes: fail");
  13259. Update_V2G_Flow_Status(Other_Fault);
  13260. }
  13261. req_is_responsed = TRUE;
  13262. break;
  13263. }
  13264. //Check for ChargeParameterDiscoveryReq
  13265. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  13266. {
  13267. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13268. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  13269. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  13270. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13271. ftime(&SeqStartTime);
  13272. #endif
  13273. }
  13274. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13275. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13276. {
  13277. sprintf(buf_log_evcomm,
  13278. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13279. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13280. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13281. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13282. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13283. }
  13284. else
  13285. {
  13286. break;
  13287. }
  13288. }
  13289. //-------------------------------------------
  13290. case ChargeParameterDiscoveryRequest: //35
  13291. {
  13292. if (Proc_iso2_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  13293. {
  13294. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  13295. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13296. }
  13297. else
  13298. {
  13299. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ChargeParameterDiscoveryReqRes: fail");
  13300. Update_V2G_Flow_Status(Other_Fault);
  13301. }
  13302. req_is_responsed = TRUE;
  13303. break;
  13304. }
  13305. case ChargeParameterDiscoveryResponse:
  13306. {
  13307. //STEP 1: Check for Process Timeout
  13308. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13309. ftime(&SeqEndTime);
  13310. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  13311. {
  13312. sprintf(buf_log_evcomm,
  13313. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  13314. DiffTimeb(SeqStartTime, SeqEndTime),
  13315. V2G_SECC_ChargingParameter_Performance_Time);
  13316. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  13317. Update_V2G_Flow_Status(Sequence_Timeout);
  13318. break;
  13319. }
  13320. #endif
  13321. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  13322. {
  13323. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13324. if (Proc_iso2_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  13325. {
  13326. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13327. }
  13328. else
  13329. {
  13330. SAVE_SYS_LOG_MSG_EVCOMM("[Error]ChargeParameterDiscoveryReqRes: fail");
  13331. Update_V2G_Flow_Status(Other_Fault);
  13332. }
  13333. req_is_responsed = TRUE;
  13334. break;
  13335. }
  13336. //STEP 2: Check for CableCheckReq message
  13337. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  13338. {
  13339. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13340. Update_V2G_Flow_Status(CableCheckRequest);
  13341. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]CableCheckReqReq: isolated?");
  13342. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  13343. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  13344. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13345. ftime(&SeqStartTime);
  13346. #endif
  13347. }
  13348. //STEP 3: Wait for PowerDeliveryReq Message
  13349. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13350. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13351. {
  13352. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  13353. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13354. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  13355. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13356. ftime(&SeqStartTime);
  13357. #endif
  13358. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  13359. {
  13360. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  13361. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13362. }
  13363. else
  13364. {
  13365. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStopReqRes: fail");
  13366. Update_V2G_Flow_Status(Other_Fault);
  13367. }
  13368. break;
  13369. }
  13370. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13371. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13372. {
  13373. sprintf(buf_log_evcomm,
  13374. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13375. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13376. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13377. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13378. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13379. }
  13380. else
  13381. {
  13382. break;
  13383. }
  13384. }
  13385. //-------------------------------------------
  13386. case CableCheckRequest: //37
  13387. {
  13388. //STEP 3: Execute Cable Check Process
  13389. if (Proc_iso2_CableCheckReq(AcceptFd) == 0)
  13390. {
  13391. Update_V2G_Flow_Status(CableCheckResponse);
  13392. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13393. }
  13394. else
  13395. {
  13396. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CableCheckReqRes: fail");
  13397. Update_V2G_Flow_Status(Other_Fault);
  13398. }
  13399. req_is_responsed = TRUE;
  13400. break;
  13401. }
  13402. case CableCheckResponse: //38
  13403. {
  13404. //STEP 1: Check for Process Timeout
  13405. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13406. ftime(&SeqEndTime);
  13407. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  13408. {
  13409. //SAVE_SYS_LOG_MSG_EVCOMM("[CableCheck] end counting...");
  13410. sprintf(buf_log_evcomm,
  13411. "CableCheck Timeout - (%.02lf of %d ms)\n",
  13412. DiffTimeb(SeqStartTime, SeqEndTime),
  13413. V2G_SECC_CableCheck_Performance_Time);
  13414. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  13415. Update_V2G_Flow_Status(Sequence_Timeout);
  13416. //Update_ShmStatusCode(); //[To-Do] to be implemented
  13417. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  13418. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  13419. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  13420. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  13421. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  13422. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  13423. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  13424. break;
  13425. }
  13426. #endif
  13427. //STEP 2: Check for CableCheckReq message
  13428. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  13429. {
  13430. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13431. //STEP 3: Execute Cable Check Process
  13432. if (Proc_iso2_CableCheckReq(AcceptFd) == 0)
  13433. {
  13434. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13435. }
  13436. else
  13437. {
  13438. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CableCheckReqRes: fail");
  13439. Update_V2G_Flow_Status(Other_Fault);
  13440. }
  13441. req_is_responsed = TRUE;
  13442. break;
  13443. }
  13444. //STEP 3: Check for PreChargeReq message
  13445. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  13446. {
  13447. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13448. Update_V2G_Flow_Status(PreChargeRequest);
  13449. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]PreChargeReqReq: waiting for precharge voltage...");
  13450. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  13451. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  13452. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13453. ftime(&SeqStartTime);
  13454. #endif
  13455. }
  13456. //STEP 3: Wait for PowerDeliveryReq Message
  13457. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13458. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13459. {
  13460. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  13461. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13462. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  13463. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13464. ftime(&SeqStartTime);
  13465. #endif
  13466. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  13467. {
  13468. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  13469. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13470. }
  13471. else
  13472. {
  13473. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStopReqRes: fail");
  13474. Update_V2G_Flow_Status(Other_Fault);
  13475. }
  13476. break;
  13477. }
  13478. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13479. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13480. {
  13481. sprintf(buf_log_evcomm,
  13482. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13483. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13484. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13485. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13486. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13487. }
  13488. else
  13489. {
  13490. break;
  13491. }
  13492. }
  13493. //-------------------------------------------
  13494. case PreChargeRequest: //39
  13495. {
  13496. if (Proc_iso2_PreChargeReq(AcceptFd) == 0)
  13497. {
  13498. Update_V2G_Flow_Status(PreChargeResponse);
  13499. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13500. }
  13501. else
  13502. {
  13503. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PreChargeReqRes: fail");
  13504. Update_V2G_Flow_Status(Other_Fault);
  13505. }
  13506. req_is_responsed = TRUE;
  13507. break;
  13508. }
  13509. case PreChargeResponse: //40
  13510. {
  13511. //STEP 1: Check for Process Timeout
  13512. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13513. ftime(&SeqEndTime);
  13514. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  13515. {
  13516. sprintf(buf_log_evcomm,
  13517. "Precharge Timeout - (%.02lf of %d ms)\n",
  13518. DiffTimeb(SeqStartTime, SeqEndTime),
  13519. V2G_SECC_PreCharge_Performance_Time);
  13520. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  13521. Update_V2G_Flow_Status(Sequence_Timeout);
  13522. //Update_ShmStatusCode(); //[To-Do] to be implemented
  13523. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  13524. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  13525. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  13526. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  13527. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  13528. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  13529. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  13530. break;
  13531. }
  13532. #endif
  13533. //STEP 2: Check for PreChargeReq message
  13534. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  13535. {
  13536. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13537. if (Proc_iso2_PreChargeReq(AcceptFd) == 0)
  13538. {
  13539. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13540. }
  13541. else
  13542. {
  13543. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PreChargeReqRes: fail");
  13544. Update_V2G_Flow_Status(Other_Fault);
  13545. }
  13546. req_is_responsed = TRUE;
  13547. break;
  13548. }
  13549. //STEP 3: Check for PowerDeliveryReq message
  13550. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13551. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13552. {
  13553. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13554. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  13555. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  13556. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13557. ftime(&SeqStartTime);
  13558. #endif
  13559. }
  13560. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13561. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13562. {
  13563. sprintf(buf_log_evcomm,
  13564. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13565. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13566. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13567. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13568. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13569. }
  13570. else
  13571. {
  13572. break;
  13573. }
  13574. }
  13575. //-------------------------------------------
  13576. case PowerDeliveryRequestStart: //41
  13577. {
  13578. if (Proc_iso2_PowerDeliveryStartReq(AcceptFd) == 0)
  13579. {
  13580. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  13581. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13582. }
  13583. else
  13584. {
  13585. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStartReqRes: fail");
  13586. Update_V2G_Flow_Status(Other_Fault);
  13587. }
  13588. req_is_responsed = TRUE;
  13589. break;
  13590. }
  13591. case PowerDeliveryResponsetStart: //42
  13592. {
  13593. //STEP 1: Check for Process Timeout
  13594. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13595. if (ccs_exi_doc_ISO2.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  13596. {
  13597. ftime(&SeqEndTime);
  13598. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  13599. {
  13600. sprintf(buf_log_evcomm,
  13601. "Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n",
  13602. DiffTimeb(SeqStartTime, SeqEndTime),
  13603. 2000);
  13604. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  13605. Update_V2G_Flow_Status(Sequence_Timeout);
  13606. }
  13607. break;
  13608. }
  13609. #endif
  13610. //STEP 2: Wait for CurrentDemandReq Message
  13611. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  13612. {
  13613. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13614. Update_V2G_Flow_Status(CurrentDemandRequest);
  13615. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]CurrentDemandReqReq: energy transfering...");
  13616. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  13617. }
  13618. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13619. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13620. {
  13621. sprintf(buf_log_evcomm,
  13622. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13623. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13624. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13625. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13626. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13627. }
  13628. else
  13629. {
  13630. break;
  13631. }
  13632. }
  13633. //-------------------------------------------
  13634. case CurrentDemandRequest: //45,
  13635. {
  13636. if (Proc_iso2_CurrentDemandReq(AcceptFd) == 0)
  13637. {
  13638. Update_V2G_Flow_Status(CurrentDemandResponse);
  13639. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13640. }
  13641. else
  13642. {
  13643. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CurrentDemandReqRes: fail");
  13644. Update_V2G_Flow_Status(Other_Fault);
  13645. }
  13646. req_is_responsed = TRUE;
  13647. break;
  13648. }
  13649. case CurrentDemandResponse: //46,
  13650. {
  13651. //STEP 1: Wait for CurrentDemandReq Message
  13652. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  13653. {
  13654. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13655. if (Proc_iso2_CurrentDemandReq(AcceptFd) == 0)
  13656. {
  13657. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13658. }
  13659. else
  13660. {
  13661. SAVE_SYS_LOG_MSG_EVCOMM("[Error]CurrentDemandReqRes: fail");
  13662. Update_V2G_Flow_Status(Other_Fault);
  13663. }
  13664. req_is_responsed = TRUE;
  13665. break;
  13666. }
  13667. //STEP 2: Wait for PowerDeliveryReq Message
  13668. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13669. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13670. {
  13671. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  13672. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13673. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  13674. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13675. ftime(&SeqStartTime);
  13676. #endif
  13677. }
  13678. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13679. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13680. {
  13681. sprintf(buf_log_evcomm,
  13682. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13683. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13684. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13685. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13686. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13687. }
  13688. else
  13689. {
  13690. break;
  13691. }
  13692. }
  13693. //-------------------------------------------
  13694. case PowerDeliveryRequestStop: //49,
  13695. {
  13696. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  13697. {
  13698. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  13699. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13700. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13701. ftime(&SeqStartTime);
  13702. #endif
  13703. }
  13704. else
  13705. {
  13706. SAVE_SYS_LOG_MSG_EVCOMM("[Error]PowerDeliveryStopReqRes: fail");
  13707. Update_V2G_Flow_Status(Other_Fault);
  13708. }
  13709. req_is_responsed = TRUE;
  13710. break;
  13711. }
  13712. case PowerDeliveryResponseStop: //50,
  13713. {
  13714. //STEP 1: Check for Process Timeout
  13715. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13716. ftime(&SeqEndTime);
  13717. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  13718. {
  13719. sprintf(buf_log_evcomm,
  13720. "Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n",
  13721. DiffTimeb(SeqStartTime, SeqEndTime),
  13722. 2000);
  13723. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  13724. Update_V2G_Flow_Status(Sequence_Timeout);
  13725. break;
  13726. }
  13727. #endif
  13728. //STEP 2: Check for WeldingDetectionReq Message
  13729. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  13730. {
  13731. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13732. Update_V2G_Flow_Status(WeldingDetectionRequest);
  13733. SAVE_SYS_LOG_MSG_EVCOMM("[V2G][Rx]WeldingDetectionReq: ongoing...");
  13734. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  13735. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13736. ftime(&SeqStartTime);
  13737. #endif
  13738. }
  13739. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  13740. {
  13741. Update_V2G_Flow_Status(SessionStopRequest);
  13742. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13743. if (Proc_iso2_SessionStopReq(AcceptFd) == 0)
  13744. {
  13745. Update_V2G_Flow_Status(SessionStopResponse);
  13746. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13747. }
  13748. else
  13749. {
  13750. SAVE_SYS_LOG_MSG_EVCOMM("[Error]SessionStopReqRes: fail");
  13751. Update_V2G_Flow_Status(Other_Fault);
  13752. }
  13753. break;
  13754. }
  13755. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13756. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13757. {
  13758. sprintf(buf_log_evcomm,
  13759. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13760. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13761. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13762. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13763. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13764. }
  13765. else
  13766. {
  13767. break;
  13768. }
  13769. }
  13770. //-------------------------------------------
  13771. case WeldingDetectionRequest: //51,
  13772. {
  13773. if (Proc_iso2_WeldingDetectionReq(AcceptFd) == 0)
  13774. {
  13775. Update_V2G_Flow_Status(WeldingDetectionResponse);
  13776. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13777. }
  13778. else
  13779. {
  13780. SAVE_SYS_LOG_MSG_EVCOMM("[Error]WeldingDetectionReqRes: fail");
  13781. Update_V2G_Flow_Status(Other_Fault);
  13782. }
  13783. req_is_responsed = TRUE;
  13784. break;
  13785. }
  13786. case WeldingDetectionResponse: //52,
  13787. {
  13788. //STEP 1: Check for Process Timeout
  13789. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13790. ftime(&SeqEndTime);
  13791. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  13792. {
  13793. sprintf(buf_log_evcomm,
  13794. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  13795. DiffTimeb(SeqStartTime, SeqEndTime),
  13796. V2G_SECC_WeldingDetection_Performance_Time);
  13797. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  13798. Update_V2G_Flow_Status(Sequence_Timeout);
  13799. break;
  13800. }
  13801. #endif
  13802. //STEP 2: Check for WeldingDetectionReq Message
  13803. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  13804. {
  13805. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13806. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  13807. {
  13808. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13809. }
  13810. else
  13811. {
  13812. SAVE_SYS_LOG_MSG_EVCOMM("[Error]WeldingDetectionReqRes: fail");
  13813. Update_V2G_Flow_Status(Other_Fault);
  13814. }
  13815. req_is_responsed = TRUE;
  13816. break;
  13817. }
  13818. //STEP 3: Check for SessionStopReq Message
  13819. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  13820. {
  13821. Update_V2G_Flow_Status(SessionStopRequest);
  13822. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13823. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  13824. }
  13825. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13826. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13827. {
  13828. sprintf(buf_log_evcomm,
  13829. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13830. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13831. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13832. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13833. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13834. }
  13835. else
  13836. {
  13837. break;
  13838. }
  13839. }
  13840. //-------------------------------------------
  13841. case SessionStopRequest: //53,
  13842. {
  13843. if (Proc_iso2_SessionStopReq(AcceptFd) == 0)
  13844. {
  13845. Update_V2G_Flow_Status(SessionStopResponse);
  13846. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13847. }
  13848. else
  13849. {
  13850. SAVE_SYS_LOG_MSG_EVCOMM("[Error]SessionStopReqRes: fail");
  13851. Update_V2G_Flow_Status(Other_Fault);
  13852. }
  13853. req_is_responsed = TRUE;
  13854. //break;
  13855. }
  13856. case SessionStopResponse: //54,
  13857. {
  13858. break;
  13859. }
  13860. #endif
  13861. //-------------------------------------------
  13862. default:
  13863. {
  13864. break;
  13865. }
  13866. }
  13867. }
  13868. }
  13869. /*===========================================================================
  13870. FUNCTION: V2gMsg_Process_iso2_AC
  13871. DESCRIPTION:
  13872. PRE-CONDITION:
  13873. INPUT:
  13874. 1. V2gFlowStatus
  13875. OUTPUT:
  13876. GLOBAL VARIABLES:
  13877. 1. V2gFlowStatus
  13878. =============================================================================*/
  13879. int V2gMsg_Process_iso2_AC(int AcceptFd)
  13880. {
  13881. }
  13882. /*===========================================================================
  13883. FUNCTION: V2gMsg_Process_iso2
  13884. DESCRIPTION:
  13885. PRE-CONDITION:
  13886. INPUT:
  13887. 1. V2gFlowStatus
  13888. OUTPUT:
  13889. GLOBAL VARIABLES:
  13890. 1. V2gFlowStatus
  13891. =============================================================================*/
  13892. int V2gMsg_Process_iso2(int AcceptFd, unsigned char EnergyTransferMode)
  13893. {
  13894. switch (EnergyTransferMode)
  13895. {
  13896. case DC_extended:
  13897. {
  13898. V2gMsg_Process_iso2_DC(AcceptFd);
  13899. break;
  13900. }
  13901. case AC_single_phase_core:
  13902. case AC_three_phase_core:
  13903. {
  13904. V2gMsg_Process_iso2_AC(AcceptFd);
  13905. break;
  13906. }
  13907. default:
  13908. {
  13909. sprintf(buf_log_evcomm,
  13910. "[ISO2][Warning]Unexpected EnergyTransferMode(%d)",
  13911. EnergyTransferMode);
  13912. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  13913. break;
  13914. }
  13915. }
  13916. }
  13917. /*===========================================================================
  13918. FUNCTION: V2gMsg_Process
  13919. DESCRIPTION:
  13920. PRE-CONDITION:
  13921. INPUT:
  13922. OUTPUT:
  13923. GLOBAL VARIABLES:
  13924. =============================================================================*/
  13925. int V2gMsg_Process(int AcceptFd)
  13926. {
  13927. int errn = 0;
  13928. switch (ShmCcsData->CommProtocol)
  13929. {
  13930. case V2GT_MSG_PROTOCOL_DIN70121: //0
  13931. {
  13932. V2gMsg_Process_din(AcceptFd);
  13933. break;
  13934. }
  13935. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  13936. {
  13937. V2gMsg_Process_iso1(AcceptFd, ShmCcsData->EnergyTransferMode);
  13938. break;
  13939. }
  13940. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  13941. {
  13942. V2gMsg_Process_iso2(AcceptFd, ShmCcsData->EnergyTransferMode);
  13943. break;
  13944. }
  13945. default:
  13946. {
  13947. sprintf(buf_log_evcomm,
  13948. "[Warning]Unexpected CommProtocol(%d)",
  13949. ShmCcsData->CommProtocol);
  13950. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  13951. break;
  13952. }
  13953. }
  13954. return errn;
  13955. }
  13956. /*===========================================================================
  13957. FUNCTION: V2gMsg_Rx
  13958. DESCRIPTION:
  13959. PRE-CONDITION:
  13960. INPUT:
  13961. OUTPUT:
  13962. GLOBAL VARIABLES:
  13963. =============================================================================*/
  13964. int V2gMsg_Rx(int AcceptFd)
  13965. {
  13966. int errn = 0;
  13967. unsigned int packet_size = 0;
  13968. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  13969. packet_size = recv(AcceptFd, V2gtpMsgRxBuf, V2GTP_MSG_RX_BUFFER_SIZE, 0);
  13970. //[CAUTION] Everytime recv() is called, it will take a default time to wait for TCP packets.
  13971. //The more time you call recv(), the more time you will waste here.
  13972. //Here it is suggested that response immediatedly once you receive any packets.
  13973. //For configuring the Rx waiting time, please use setsockopt().
  13974. if(packet_size > 0) //[Joseph] If there is no data, ignore. (TBD)
  13975. {
  13976. //DEBUG_PRINTF_EVCOMM_DETAIL("\n\n Got TCP V2GTP Message: size = %d Bytes\n", packet_size);
  13977. errn = V2gMsgDecoder(V2gtpMsgRxBuf, packet_size, V2gFlowStatus);
  13978. if (errn < 0)
  13979. {
  13980. sprintf(buf_log_evcomm, "[ERROR]V2gMsgDecoder", errn);
  13981. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  13982. }
  13983. }
  13984. return errn;
  13985. }
  13986. /*===========================================================================
  13987. FUNCTION: V2gComm
  13988. DESCRIPTION:
  13989. PRE-CONDITION:
  13990. 1. TCP socket is connected.
  13991. INPUT:
  13992. 1. AcceptFd //TCP Connection ID
  13993. 2. V2gtpMsgRxBuf[] //V2GTP Messages (Header + Payload)
  13994. 3. V2GTP_MSG_RX_BUFFER_SIZE //64*1024 = 65,536 = 65.5K (Bytes)
  13995. OUTPUT:
  13996. GLOBAL VARIABLES:
  13997. 1. V2gtpMsgRxBuf[]
  13998. =============================================================================*/
  13999. int V2gComm(int AcceptFd)
  14000. {
  14001. int errn = 0;
  14002. if (V2gMsg_Rx(AcceptFd) < 0)
  14003. {
  14004. Update_V2G_Flow_Status(Other_Fault);
  14005. errn = -1;
  14006. }
  14007. //following are the response message handling according to status flag
  14008. if (V2gMsg_Process(AcceptFd) < 0)
  14009. {
  14010. errn = -1;
  14011. }
  14012. //Error Check
  14013. //V2G_Error_Monitor();
  14014. return errn;
  14015. }
  14016. /*===========================================================================
  14017. FUNCTION: SdpUdpConnected
  14018. DESCRIPTION:
  14019. PRE-CONDITION:
  14020. INPUT:
  14021. OUTPUT:
  14022. GLOBAL VARIABLES:
  14023. =============================================================================*/
  14024. int SdpUdpConnected()
  14025. {
  14026. int packet_size,Rtn;
  14027. struct sockaddr_in6 ServerAddr,ClientAddr;
  14028. struct V2gtpHeader *header;
  14029. unsigned char *payload;
  14030. if(UdpSock <= 0)
  14031. {
  14032. if ((UdpSock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
  14033. {
  14034. SAVE_SYS_LOG_MSG_EVCOMM("SdpUdpConnected: Fail to open UdpSock");
  14035. return 0;
  14036. }
  14037. memset(&ServerAddr,0, sizeof(struct sockaddr_in));
  14038. ServerAddr.sin6_family = AF_INET6;
  14039. ServerAddr.sin6_addr = in6addr_any;
  14040. ServerAddr.sin6_port = htons(SdpUdpServerPort);
  14041. if(bind(UdpSock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) <0)
  14042. {
  14043. sprintf(buf_log_evcomm,
  14044. "SdpUdpConnected: Fail to bind UdpSock(%d)",
  14045. UdpSock);
  14046. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14047. close(UdpSock);
  14048. UdpSock = -1;
  14049. return 0;
  14050. }
  14051. sprintf(buf_log_evcomm, "[UdpSock]opend:%d", UdpSock);
  14052. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14053. sprintf(buf_log_evcomm, "[SDP]Req from EVCC: waiting...(%d)", UdpSock);
  14054. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14055. }
  14056. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  14057. memset(&ClientAddr, 0, sizeof(struct sockaddr_in));
  14058. Rtn = sizeof(struct sockaddr_in6);
  14059. packet_size = recvfrom(UdpSock, V2gtpMsgRxBuf, V2GTP_MSG_RX_BUFFER_SIZE, MSG_DONTWAIT, (struct sockaddr *)&ClientAddr, &Rtn);
  14060. if(packet_size > 0)
  14061. {
  14062. header = (struct V2gtpHeader *) V2gtpMsgRxBuf;
  14063. payload = V2gtpMsgRxBuf+sizeof(struct V2gtpHeader);
  14064. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  14065. {
  14066. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n Got UDP packet_size = %d (Bytes)\n", packet_size);
  14067. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***********************************\n");
  14068. DEBUG_PRINTF_EVCOMM_DETAIL("***** Received SDP Packet *****\n");
  14069. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  14070. DEBUG_PRINTF_EVCOMM_DETAIL("ClientAddress=");
  14071. for(Rtn = 0; Rtn < 16; Rtn += 2)
  14072. {
  14073. DEBUG_PRINTF_EVCOMM_DETAIL("%02x%02x:",ClientAddr.sin6_addr.s6_addr[Rtn],ClientAddr.sin6_addr.s6_addr[Rtn+1]);
  14074. }
  14075. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14076. DEBUG_PRINTF_EVCOMM_DETAIL("ClientPort=%d\n",ClientAddr.sin6_port);
  14077. DEBUG_PRINTF_EVCOMM_DETAIL("ProtocolVersion=%d\n",header->ProtocolVersion);
  14078. DEBUG_PRINTF_EVCOMM_DETAIL("InverseProtocolVersion=0x%x\n",header->InverseProtocolVersion);
  14079. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadType=0x%x\n",htons(header->PayloadType));
  14080. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadLength=0x%x\n",htonl(header->PayloadLength));
  14081. DEBUG_PRINTF_EVCOMM_DETAIL("htons(header->PayloadType)=0x%x\n",htons(header->PayloadType));
  14082. }
  14083. #endif
  14084. if( (header->ProtocolVersion == 0x01) &&
  14085. (header->InverseProtocolVersion == 0xFE) &&
  14086. (htons(header->PayloadType) == V2GTP_PAYLOAD_TYPE_SDP_REQUEST))
  14087. {
  14088. sprintf(buf_log_evcomm, "[SDP][Rx]Req from EVCC: GOT(%d)", UdpSock);
  14089. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14090. DEBUG_PRINTF_EVCOMM_DETAIL("Security=0x%x\n", *(payload + 0));
  14091. DEBUG_PRINTF_EVCOMM_DETAIL("TransportProtocol=0x%x\n", *(payload + 1));
  14092. header->PayloadType = htons(V2GTP_PAYLOAD_TYPE_SDP_RESPONSE);
  14093. header->PayloadLength = htonl(20); //Fixed Length=20
  14094. memset(payload, 0, 20);
  14095. // MAC address[0:2] + FFFE + MAC address[3:5]
  14096. payload[0] = (IPV6_LINK_LOCAL_PREFIX>>8) & 0xFF;
  14097. payload[1] = IPV6_LINK_LOCAL_PREFIX & 0xFF;
  14098. payload[8 ]= CsuMac[0];
  14099. payload[8] ^= 0x02;// bit 1 should complemented.
  14100. payload[9] = CsuMac[1];
  14101. payload[10] = CsuMac[2];
  14102. payload[11] = 0xFF;
  14103. payload[12] = 0xFE;
  14104. payload[13] = CsuMac[3];
  14105. payload[14] = CsuMac[4];
  14106. payload[15] = CsuMac[5];
  14107. //TCP port
  14108. payload[16] = (EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active>>8) & 0xFF;
  14109. payload[17] = EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active & 0xFF;
  14110. payload[18] = SDP_PAYLOAD_SECURITY_NONE; //Security
  14111. payload[19] = SDP_PAYLOAD_TRANS_PROTOCOL_TCP; //Transport protocol
  14112. //[To-Do] Setup the TCP in advance to receive the coming TCP handshke messages after SDP response.
  14113. Rtn = sendto(UdpSock, V2gtpMsgRxBuf, sizeof(struct V2gtpHeader) + htonl(header->PayloadLength), 0, (struct sockaddr *)&ClientAddr, sizeof(struct sockaddr_in6));
  14114. sprintf(buf_log_evcomm, "[SDP][Tx]Res from SECC");
  14115. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14116. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  14117. {
  14118. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response SDP Packet *****\n");
  14119. DEBUG_PRINTF_EVCOMM_DETAIL("Send size=%d\n",Rtn);
  14120. DEBUG_PRINTF_EVCOMM_DETAIL("Destination Address=");
  14121. for(Rtn = 0; Rtn < 16; Rtn++)
  14122. {
  14123. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ", ClientAddr.sin6_addr.s6_addr[Rtn]);
  14124. }
  14125. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14126. DEBUG_PRINTF_EVCOMM_DETAIL("Destination Port=%d\n", ClientAddr.sin6_port);
  14127. DEBUG_PRINTF_EVCOMM_DETAIL("ProtocolVersion=%d\n", header->ProtocolVersion);
  14128. DEBUG_PRINTF_EVCOMM_DETAIL("InverseProtocolVersion=0x%x\n", header->InverseProtocolVersion);
  14129. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadType=0x%x\n", htons(header->PayloadType));
  14130. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadLength=0x%x\n", htonl(header->PayloadLength));
  14131. DEBUG_PRINTF_EVCOMM_DETAIL("SECC Ipv6 Address=");
  14132. for(Rtn = 0; Rtn < 16; Rtn++)
  14133. {
  14134. DEBUG_PRINTF_EVCOMM_DETAIL("%02x:",payload[Rtn]);
  14135. }
  14136. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14137. DEBUG_PRINTF_EVCOMM_DETAIL("SECC Port=%d\n", (payload[16]<<8 | payload[17]));
  14138. DEBUG_PRINTF_EVCOMM_DETAIL("Security=0x%x\n", payload[19]);
  14139. DEBUG_PRINTF_EVCOMM_DETAIL("TransportProtocol=0x%x\n", payload[20]);
  14140. }
  14141. #endif
  14142. if(Rtn > 0)
  14143. {
  14144. return 1;
  14145. }
  14146. }
  14147. }
  14148. return 0;
  14149. }
  14150. /*===========================================================================
  14151. FUNCTION: V2gTcpConnected
  14152. DESCRIPTION:
  14153. PRE-CONDITION:
  14154. INPUT:
  14155. OUTPUT:
  14156. GLOBAL VARIABLES:
  14157. =============================================================================*/
  14158. int V2gTcpConnected()
  14159. {
  14160. int packet_size,Rtn,AcceptFd;
  14161. struct sockaddr_in6 ServerAddr,ClientAddr;
  14162. if(TcpSock <= 0)
  14163. {
  14164. if ((TcpSock = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
  14165. {
  14166. sprintf(buf_log_evcomm,
  14167. "V2gTcpConnected: Fail to open TcpSock (%s)",
  14168. strerror(errno));
  14169. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14170. usleep(100000); //100ms
  14171. return 0;
  14172. }
  14173. fcntl(TcpSock, F_SETFL, O_NONBLOCK); //set to O_NONBLOCK
  14174. SAVE_SYS_LOG_MSG_EVCOMM("[TCP]socket set: DONE");
  14175. memset(&ServerAddr,0, sizeof(struct sockaddr_in));
  14176. ServerAddr.sin6_family = PF_INET6;
  14177. ServerAddr.sin6_addr = in6addr_any;
  14178. ServerAddr.sin6_port = htons(EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  14179. //ServerAddr.sin6_port = htons(SdpTcpServerPort + (rand() % 10) + 1);
  14180. //printf("SdpTcpServerPort = %d(DEC)\n", htons(ServerAddr.sin6_port));
  14181. if(bind(TcpSock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) < 0)
  14182. {
  14183. sprintf(buf_log_evcomm,
  14184. "V2gTcpConnected: Fail to bind TcpSock (%s), SdpTcpServerPort = %d",
  14185. strerror(errno),
  14186. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active
  14187. );
  14188. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14189. //Change to another TCP port
  14190. /*
  14191. memset(&ServerAddr, 0, sizeof(struct sockaddr_in));
  14192. ServerAddr.sin6_family = PF_INET6;
  14193. ServerAddr.sin6_addr = in6addr_any;
  14194. //ServerAddr.sin6_port = htons(SdpTcpServerPort + (rand() % 1000) + 1);
  14195. ServerAddr.sin6_port = htons(EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  14196. */
  14197. if(bind(TcpSock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) < 0)
  14198. {
  14199. sprintf(buf_log_evcomm,
  14200. "V2gTcpConnected: Fail to bind TcpSock (%s), SdpTcpServerPort = %d",
  14201. strerror(errno),
  14202. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active
  14203. );
  14204. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14205. usleep(100000); //100ms
  14206. close(TcpSock);
  14207. TcpSock = -1;
  14208. return 0;
  14209. }
  14210. }
  14211. SAVE_SYS_LOG_MSG_EVCOMM("[TCP]blid: DONE");
  14212. SAVE_SYS_LOG_MSG_EVCOMM("[TCP]listen: START");
  14213. if(listen(TcpSock, 1) == -1) //only accept one connection
  14214. {
  14215. sprintf(buf_log_evcomm,
  14216. "V2gTcpConnected: Fail to listen TcpSock (%s)",
  14217. strerror(errno));
  14218. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14219. usleep(100000); //100ms
  14220. close(TcpSock);
  14221. TcpSock = -1;
  14222. return 0;
  14223. }
  14224. SAVE_SYS_LOG_MSG_EVCOMM("[TCP]listen: DONE");
  14225. sprintf(buf_log_evcomm, "[TCP]TcpSock: opened(%d), port=%d", TcpSock, htons(ServerAddr.sin6_port));
  14226. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14227. SAVE_SYS_LOG_MSG_EVCOMM("[TCP]accept: START");
  14228. }
  14229. Rtn = sizeof(struct sockaddr_in6);
  14230. if((AcceptFd = accept(TcpSock, (struct sockaddr *)&ClientAddr, &Rtn)) == -1)
  14231. {
  14232. static BOOL tmp = 0;
  14233. if (tmp = 0)
  14234. {
  14235. SAVE_SYS_LOG_MSG_EVCOMM("[TCP]Wait TCP connection...");
  14236. tmp = 1;
  14237. }
  14238. else
  14239. {
  14240. //DEBUG_PRINTF_EVCOMM_DETAIL(".");
  14241. }
  14242. return 0;
  14243. }
  14244. SAVE_SYS_LOG_MSG_EVCOMM("[TCP]accept: DONE\n");
  14245. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  14246. {
  14247. DEBUG_PRINTF_EVCOMM_DETAIL("\n\nAccept one TCP connection:\n");
  14248. DEBUG_PRINTF_EVCOMM_DETAIL("AcceptFd=%d\n",AcceptFd);
  14249. DEBUG_PRINTF_EVCOMM_DETAIL("ClientAddress=");
  14250. for(Rtn = 0; Rtn < 16; Rtn += 2)
  14251. {
  14252. DEBUG_PRINTF_EVCOMM_DETAIL("%02x%02x:", ClientAddr.sin6_addr.s6_addr[Rtn], ClientAddr.sin6_addr.s6_addr[Rtn+1]);
  14253. }
  14254. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14255. DEBUG_PRINTF_EVCOMM_DETAIL("ClientPort=%d\n", ClientAddr.sin6_port);
  14256. }
  14257. #endif
  14258. return AcceptFd;
  14259. }
  14260. /*===========================================================================
  14261. FUNCTION: End_Process
  14262. DESCRIPTION:
  14263. PRE-CONDITION:
  14264. 1. <CAUTION> This function could only be used in SeccComm fork2.
  14265. INPUT:
  14266. OUTPUT:
  14267. GLOBAL VARIABLES:
  14268. =============================================================================*/
  14269. int End_Process()
  14270. {
  14271. if(EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  14272. {
  14273. SAVE_SYS_LOG_MSG_EVCOMM("End_Process has been triggered by another event.");
  14274. return -1;
  14275. }
  14276. SAVE_SYS_LOG_MSG_EVCOMM("[End_Process]entering...");
  14277. //STEP 1: Ask CSU to Stop
  14278. EVCOMM_SYS_INFO.End_Process_inused = TRUE;
  14279. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  14280. EVCOMM_SYS_INFO.ConnectorLocked = FALSE;
  14281. ShmInternalComm->ChargingPermission = FALSE;
  14282. //Step 2: Close sockets
  14283. SAVE_SYS_LOG_MSG_EVCOMM("[End_Process]close sockets...");
  14284. if(RawSock > 0)
  14285. {
  14286. close(RawSock);
  14287. }
  14288. if(UdpSock > 0)
  14289. {
  14290. close(UdpSock);
  14291. }
  14292. if(TcpSock > 0)
  14293. {
  14294. close(TcpSock);
  14295. close(TcpAcceptFd);
  14296. }
  14297. RawSock = UdpSock = TcpSock = TcpAcceptFd = -1;
  14298. //STEP 3: Switch to State E
  14299. //SwitchCpStateE(ENABLE);
  14300. //STEP 4: Close tcpdump
  14301. Sniffer_Tcpdump(DISABLE);
  14302. //STEP 5: Keep 100% PWM for 5 seconds
  14303. OutputCpPwmDuty(100);
  14304. SAVE_SYS_LOG_MSG_EVCOMM("PWM 100\% countdown: 5");
  14305. sleep(1);
  14306. SAVE_SYS_LOG_MSG_EVCOMM("PWM 100\% countdown: 4");
  14307. sleep(1);
  14308. SAVE_SYS_LOG_MSG_EVCOMM("PWM 100\% countdown: 3");
  14309. sleep(1);
  14310. SAVE_SYS_LOG_MSG_EVCOMM("PWM 100\% countdown: 2");
  14311. sleep(1);
  14312. SAVE_SYS_LOG_MSG_EVCOMM("PWM 100\% countdown: 1");
  14313. sleep(1);
  14314. SAVE_SYS_LOG_MSG_EVCOMM("PWM 100\% countdown: 0");
  14315. AttenProfileCnt = 0;
  14316. init_appHandEXIDocument(&ccs_handshake);
  14317. // Qca7kPowerReset(); //reset QCA7000 /* +++ 20200808, vern, should disconnected PLC connection after session stop ---*/
  14318. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
  14319. {
  14320. SAVE_SYS_LOG_MSG_EVCOMM("[End_Process]1st sync...");
  14321. system("sync");
  14322. }
  14323. #endif
  14324. //STEP 4: Switch to State E
  14325. //Keep State E for 5 seconds
  14326. #if 0
  14327. SwitchCpStateE(ENABLE); //Set PWM Duty as 0 and set State as E (0V)
  14328. SAVE_SYS_LOG_MSG_EVCOMM("State E countdown: 5");
  14329. sleep(1);
  14330. SAVE_SYS_LOG_MSG_EVCOMM("State E countdown: 4");
  14331. sleep(1);
  14332. SAVE_SYS_LOG_MSG_EVCOMM("State E countdown: 3");
  14333. sleep(1);
  14334. SAVE_SYS_LOG_MSG_EVCOMM("State E countdown: 2");
  14335. sleep(1);
  14336. SAVE_SYS_LOG_MSG_EVCOMM("State E countdown: 1");
  14337. sleep(1);
  14338. SAVE_SYS_LOG_MSG_EVCOMM("State E countdown: 0");
  14339. #endif
  14340. //Reset Memory
  14341. unsigned char SlaveAddress_backup;
  14342. unsigned int matched_backup;
  14343. //unsigned char state_backup;
  14344. SAVE_SYS_LOG_MSG_EVCOMM("[CCS Board]soft reset (closing SeccComm)...");
  14345. SAVE_SYS_LOG_MSG_EVCOMM("---------------------------------------------");
  14346. SAVE_SYS_LOG_MSG_EVCOMM("-- EVCOMM: END --");
  14347. SAVE_SYS_LOG_MSG_EVCOMM("---------------------------------------------\n");
  14348. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
  14349. {
  14350. SAVE_SYS_LOG_MSG_EVCOMM("[End_Process]2nd sync...");
  14351. system("sync");
  14352. }
  14353. #endif
  14354. system("sleep 1");
  14355. //Backup CsuComm flags
  14356. SlaveAddress_backup = ShmInternalComm->SlaveAddress;
  14357. matched_backup = CSUCOMMDC_TASK_FLAG.matched;
  14358. //state_backup = Check_V2G_Flow_Status();
  14359. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  14360. memset(V2gtpMsgTxBuf, 0, V2GTP_MSG_TX_BUFFER_SIZE);
  14361. memset(&ccs_handshake, 0, sizeof(struct appHandEXIDocument));
  14362. memset(&ccs_exi_doc_DIN, 0, sizeof(struct dinEXIDocument));
  14363. memset(V2GTP_Tx_buf, 0, V2GTP_MSG_TX_BUFFER_SIZE);
  14364. memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  14365. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  14366. memset(ShmInternalComm, 0, sizeof(struct InternalComm));
  14367. memset(ShmCcsData, 0, sizeof(struct CcsData));
  14368. //Resume CsuComm flags
  14369. ShmInternalComm->SlaveAddress = SlaveAddress_backup;
  14370. CSUCOMMDC_TASK_FLAG.matched = matched_backup;
  14371. Sniffer_Candump(DISABLE);
  14372. Sniffer_Candump(ENABLE);
  14373. //memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo)); //EVCOMM_SYS_INFO.End_Process_inused = FALSE;
  14374. //EVCOMM_SYS_INFO.End_Process_inused = TRUE;
  14375. //Update_V2G_Flow_Status(state_backup);
  14376. //CP_Detection_Pid = 0;
  14377. //PP_Detection_Pid = 0;
  14378. //Error_Monitor_Pid = 0;
  14379. /* while(CheckConnectorPlugIn() != TRUE)
  14380. {
  14381. sleep(1);
  14382. }*/
  14383. free(V2gtpMsgRxBuf);
  14384. free(V2gtpMsgTxBuf);
  14385. //DetachShareMemory();
  14386. system("cd /root;./reset_soft.sh");
  14387. while(1)
  14388. {
  14389. //wait for CSU configrm
  14390. }
  14391. }
  14392. /*===========================================================================
  14393. FUNCTION: Parameters_Init
  14394. DESCRIPTION:
  14395. PRE-CONDITION:
  14396. INPUT:
  14397. OUTPUT:
  14398. GLOBAL VARIABLES:
  14399. =============================================================================*/
  14400. int Parameters_Init()
  14401. {
  14402. //Step 0: Generate random number
  14403. unsigned int value_random;
  14404. struct timeb time_seed;
  14405. ftime(&time_seed);
  14406. srand(time_seed.millitm);
  14407. //Step 1: Init SDP TCP Port
  14408. value_random = rand();
  14409. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active = (unsigned short)(SdpTcpServerPort + (value_random % 10000) + 1);
  14410. sprintf(buf_log_evcomm,
  14411. "[Init]TCP Port:OK(%d)",
  14412. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  14413. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14414. //Step 2: Init SessionID
  14415. value_random = rand();
  14416. memcpy(&EVCOMM_SYS_INFO.SessionID[0], &value_random, 4);
  14417. value_random = rand();
  14418. memcpy(&EVCOMM_SYS_INFO.SessionID[4], &value_random, 4);
  14419. sprintf(buf_log_evcomm,
  14420. "[Init]SessionID:OK(%02X%02X%02X%02X%02X%02X%02X%02X)",
  14421. EVCOMM_SYS_INFO.SessionID[0],
  14422. EVCOMM_SYS_INFO.SessionID[1],
  14423. EVCOMM_SYS_INFO.SessionID[2],
  14424. EVCOMM_SYS_INFO.SessionID[3],
  14425. EVCOMM_SYS_INFO.SessionID[4],
  14426. EVCOMM_SYS_INFO.SessionID[5],
  14427. EVCOMM_SYS_INFO.SessionID[6],
  14428. EVCOMM_SYS_INFO.SessionID[7]);
  14429. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14430. }
  14431. /*===========================================================================
  14432. FUNCTION: main
  14433. DESCRIPTION:
  14434. PRE-CONDITION:
  14435. INPUT:
  14436. OUTPUT:
  14437. GLOBAL VARIABLES:
  14438. =============================================================================*/
  14439. int main(int argc, char *argv[])
  14440. {
  14441. unsigned char Rtn;
  14442. //Initialization
  14443. if(ShareMemory_Init()==0)
  14444. {
  14445. system("reboot -f");
  14446. sleep(5);
  14447. system("reboot -f");
  14448. }
  14449. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  14450. if(RawSock > 0)
  14451. {
  14452. close(RawSock);
  14453. }
  14454. if(UdpSock > 0)
  14455. {
  14456. close(UdpSock);
  14457. }
  14458. if(TcpSock > 0)
  14459. {
  14460. close(TcpSock);
  14461. }
  14462. RawSock = UdpSock = TcpSock = -1;
  14463. Update_V2G_Flow_Status(IDLE);
  14464. SAVE_SYS_LOG_MSG_EVCOMM("---------------------------------------------");
  14465. SAVE_SYS_LOG_MSG_EVCOMM("-- EVCOMM: START --");
  14466. SAVE_SYS_LOG_MSG_EVCOMM("---------------------------------------------\n");
  14467. //Print Linux Kernel Version
  14468. sprintf(buf_log_evcomm, "\nLinux Image SHA1: %s\n", LINUX_IMAGE_VERSION);
  14469. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14470. //Print Hardware Version
  14471. sprintf(buf_log_evcomm, "HW: %s\n", HARDWARE_VERSION);
  14472. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14473. //Print Firmware Version
  14474. sprintf(buf_log_evcomm, "FW: %s\n", FIRMWARE_VERSION);
  14475. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14476. SAVE_SYS_LOG_MSG_EVCOMM("init...");
  14477. //Init V2G TCP/IPv6 packets buffer
  14478. V2gtpMsgRxBuf = (unsigned char *)malloc(V2GTP_MSG_RX_BUFFER_SIZE);
  14479. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  14480. V2gtpMsgTxBuf = (unsigned char *)malloc(V2GTP_MSG_TX_BUFFER_SIZE);
  14481. memset(V2gtpMsgTxBuf, 0, V2GTP_MSG_TX_BUFFER_SIZE);
  14482. //Release State E Control
  14483. SwitchCpStateE(DISABLE);
  14484. OutputCpPwmDuty(100);
  14485. //start to detect CP pilot state
  14486. CP_Detection_Pid = 0;
  14487. CP_Detection(); //fork1
  14488. SAVE_SYS_LOG_MSG_EVCOMM("[fork1]CP Detection: ON");
  14489. //start to detect errors
  14490. Error_Monitor(); //fork2
  14491. SAVE_SYS_LOG_MSG_EVCOMM("[fork2]Error Monitor: ON");
  14492. //start to detect PP
  14493. PP_Detection_Pid = 0;
  14494. #if (PP_PROTECTION_MECHANISM == ENABLE)
  14495. PP_Detection();
  14496. SAVE_SYS_LOG_MSG_EVCOMM("[fork3]PP Detection: ON");
  14497. #else
  14498. SAVE_SYS_LOG_MSG_EVCOMM("[fork3]PP Detection: OFF");
  14499. #endif
  14500. //Init communication parameters
  14501. GetEthMac(QcaInterface, CsuMac);
  14502. AttenProfileCnt = 0;
  14503. init_appHandEXIDocument(&ccs_handshake);
  14504. //Init Energy transfer mode
  14505. //[To-Do] Parsing Model Name
  14506. ShmCcsData->EnergyTransferMode = CCS_ENERGY_TRANSFER_MODE;
  14507. struct ChargingInfoData *ccs;
  14508. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  14509. Parameters_Init();
  14510. Sniffer_Tcpdump(ENABLE);
  14511. #if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  14512. sleep(1); //wait for tcpdump getting ready
  14513. #endif
  14514. SAVE_SYS_LOG_MSG_EVCOMM("[Init]DONE");
  14515. while(1)
  14516. {
  14517. //DEBUG_PRINTF_EVCOMM_DETAIL("\nV2gFlowStatus = %d\n", V2gFlowStatus);
  14518. //if((ShmInternalComm->ChargingPermission == 0x01) && (CheckConnectorPlugIn() == 1))
  14519. //if((CheckConnectorPlugIn() == TRUE) && (ShmInternalComm->ChargingPermission == TRUE))
  14520. //if(ShmInternalComm->ChargingPermission == 0x01)
  14521. //if(CheckConnectorPlugIn() == TRUE)
  14522. if (EVCOMM_SYS_INFO.End_Process_inused == FALSE)
  14523. {
  14524. if(V2gFlowStatus < SLACC_SDP_UDP_Connection)
  14525. {
  14526. SlacComm();
  14527. }
  14528. else if(V2gFlowStatus == SLACC_SDP_UDP_Connection)
  14529. {
  14530. if(SdpUdpConnected() == 1)
  14531. {
  14532. Update_V2G_Flow_Status(SLACC_SDP_TCP_Connection);
  14533. continue;
  14534. }
  14535. SlacComm(); //TC_SECC_VTB_CmSlacMatch_004
  14536. ftime(&SeqEndTime);
  14537. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_join)
  14538. {
  14539. sprintf(buf_log_evcomm,
  14540. "Wait SLACC_SDP_UDP_Connection Timeout - TT_match_join (%.02lf of %d ms)\n",
  14541. DiffTimeb(SeqStartTime, SeqEndTime),
  14542. TT_match_join);
  14543. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14544. Update_V2G_Flow_Status(Sequence_Timeout);
  14545. //Update_ShmStatusCode(); //[To-Do] to be implemented
  14546. //CCS_SECC_TIMEOUT_SLACC_SDP_UDP_TT_match_join (023823)
  14547. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  14548. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  14549. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  14550. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  14551. ShmStatusCodeData->PresentStatusCode[0][4] = 2;
  14552. ShmStatusCodeData->PresentStatusCode[0][5] = 3;
  14553. //CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  14554. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  14555. }
  14556. }
  14557. else if(V2gFlowStatus == SLACC_SDP_TCP_Connection)
  14558. {
  14559. if((TcpAcceptFd = V2gTcpConnected()) > 0)
  14560. {
  14561. Update_V2G_Flow_Status(SupportedAppProtocolRequest);
  14562. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  14563. continue;
  14564. }
  14565. SlacComm();
  14566. ftime(&SeqEndTime);
  14567. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_join)
  14568. {
  14569. sprintf(buf_log_evcomm,
  14570. "Wait SLACC_SDP_TCP_Connection Timeout - TT_match_join (%.02lf / %d ms)",
  14571. DiffTimeb(SeqStartTime, SeqEndTime),
  14572. TT_match_join);
  14573. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  14574. Update_V2G_Flow_Status(Sequence_Timeout);
  14575. //Update_ShmStatusCode(); //[To-Do] to be implemented
  14576. //CCS_SECC_TIMEOUT_SLACC_SDP_TCP_TT_match_join (023824)
  14577. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  14578. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  14579. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  14580. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  14581. ShmStatusCodeData->PresentStatusCode[0][4] = 2;
  14582. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  14583. //CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  14584. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  14585. }
  14586. }
  14587. else if(V2gFlowStatus <= SessionStopResponse)
  14588. {
  14589. if (V2gComm(TcpAcceptFd) < 0)
  14590. {
  14591. //error occours
  14592. }
  14593. }
  14594. else if (V2gFlowStatus >= Performance_Timeout)
  14595. {
  14596. //End_Process
  14597. }
  14598. else
  14599. {
  14600. //null
  14601. }
  14602. }
  14603. } //while
  14604. }//main while