Module_CCS.c 665 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530
  1. /*
  2. * Module_CCS.c
  3. *
  4. * Created on: 2020/-3/16
  5. * Author: foluswen
  6. */
  7. #include "Module_CCS.h"
  8. #include "define_ccs.h"
  9. #include "main.h"
  10. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  11. struct StatusCodeData *ShmStatusCodeData;
  12. struct CcsData *ShmCcsData;
  13. struct InternalComm *ShmInternalComm;
  14. struct Charger *ShmCharger;
  15. struct appHandEXIDocument ccs_handshake;
  16. struct dinEXIDocument ccs_exi_doc_DIN;
  17. struct iso1EXIDocument ccs_exi_doc_ISO1;
  18. struct iso2EXIDocument ccs_exi_doc_ISO2;
  19. unsigned char V2GTP_Tx_buf[V2GTP_MSG_TX_BUFFER_SIZE]; //64*1024 = 65,536 = 65.5KB
  20. unsigned char buf_log_evcomm[SIZE_OF_LOG_BUFFER];
  21. unsigned char buf_log_evcomm_fork1[SIZE_OF_LOG_BUFFER];
  22. unsigned char buf_log_evcomm_fork2[SIZE_OF_LOG_BUFFER];
  23. unsigned char buf_log_evcomm_fork3[SIZE_OF_LOG_BUFFER];
  24. pid_t CP_Detection_Pid = 0;
  25. pid_t PP_Detection_Pid = 0;
  26. pid_t Error_Monitor_Pid = 0;
  27. int TcpAcceptFd;
  28. enum MsgFlowStatus V2gFlowStatus;
  29. int RawSock,UdpSock,TcpSock;
  30. unsigned char V2gtpMsgRxBuf[V2GTP_MSG_RX_BUFFER_SIZE] = {0};
  31. unsigned char V2gtpMsgTxBuf[V2GTP_MSG_TX_BUFFER_SIZE] = {0};
  32. unsigned short Aag[64];
  33. struct MmeHeader SendMmePacket;
  34. int SendMmePacketSize;
  35. unsigned char CsuMac[6],QcaMac[6],EvMac[6],SlacRunId[8];
  36. struct sockaddr_ll DestSocketAddress;
  37. struct ifreq Req;
  38. unsigned int PwmStartTime;
  39. struct timeb SeqStartTime, SeqEndTime;
  40. unsigned char AagGroupsNum, MnbcSoundNum, AttenProfileCnt;
  41. unsigned char NewNmkKey[16], Nid[7];
  42. uint8_t get_V2G_Status_Name(uint8_t statusCode, char *statusName)
  43. {
  44. int result = PASS;
  45. switch(statusCode)
  46. {
  47. case IDLE:
  48. sprintf(statusName, "IDLE");
  49. break;
  50. case CM_SLAC_PARM_REQ:
  51. sprintf(statusName, "CM_SLAC_PARM_REQ");
  52. break;
  53. case CM_SLAC_PARM_CONF:
  54. sprintf(statusName, "CM_SLAC_PARM_CONF");
  55. break;
  56. case CM_START_ATTEN_CHAR_IND:
  57. sprintf(statusName, "CM_START_ATTEN_CHAR_IND");
  58. break;
  59. case CM_MNBC_SOUND_IND:
  60. sprintf(statusName, "CM_MNBC_SOUND_IND");
  61. break;
  62. case CM_ATTEN_CHAR_IND:
  63. sprintf(statusName, "CM_ATTEN_CHAR_IND");
  64. break;
  65. case CM_ATTEN_CHAR_RSP:
  66. sprintf(statusName, "CM_ATTEN_CHAR_RSP");
  67. break;
  68. case CM_VALIDATE_REQ:
  69. sprintf(statusName, "CM_VALIDATE_REQ");
  70. break;
  71. case CM_VALIDATE_CNF:
  72. sprintf(statusName, "CM_VALIDATE_CNF");
  73. break;
  74. case CM_SLAC_MATCH_REQ:
  75. sprintf(statusName, "CM_SLAC_MATCH_REQ");
  76. break;
  77. case CM_SLAC_MATCH_CNF:
  78. sprintf(statusName, "CM_SLAC_MATCH_CNF");
  79. break;
  80. case CM_AMP_MAP_REQ:
  81. sprintf(statusName, "CM_AMP_MAP_REQ");
  82. break;
  83. case CM_AMP_MAP_CNF:
  84. sprintf(statusName, "CM_AMP_MAP_CNF");
  85. break;
  86. case CM_SET_KEY_REQ:
  87. sprintf(statusName, "CM_SET_KEY_REQ");
  88. break;
  89. case CM_SET_KEY_CNF:
  90. sprintf(statusName, "CM_SET_KEY_CNF");
  91. break;
  92. case SLACC_SDP_UDP_Connection:
  93. sprintf(statusName, "SLACC_SDP_UDP_Connection");
  94. break;
  95. case SLACC_SDP_TCP_Connection:
  96. sprintf(statusName, "SLACC_SDP_TCP_Connection");
  97. break;
  98. case SupportedAppProtocolRequest:
  99. sprintf(statusName, "SupportedAppProtocolRequest");
  100. break;
  101. case SupportedAppProtocolResponse:
  102. sprintf(statusName, "SupportedAppProtocolResponse");
  103. break;
  104. case SessionSetupRequest:
  105. sprintf(statusName, "SessionSetupRequest");
  106. break;
  107. case SessionSetupResponse:
  108. sprintf(statusName, "UnSessionSetupResponseknown");
  109. break;
  110. case ServiceDiscoveryRequest:
  111. sprintf(statusName, "ServiceDiscoveryRequest");
  112. break;
  113. case ServiceDiscoveryResponse:
  114. sprintf(statusName, "ServiceDiscoveryResponse");
  115. break;
  116. case ServiceDetailRequest:
  117. sprintf(statusName, "ServiceDetailRequest");
  118. break;
  119. case ServiceDetailResponse:
  120. sprintf(statusName, "ServiceDetailResponse");
  121. break;
  122. case ServiceAndPaymentSelectionRequest:
  123. sprintf(statusName, "ServiceAndPaymentSelectionRequest");
  124. break;
  125. case ServiceAndPaymentSelectionResponse:
  126. sprintf(statusName, "ServiceAndPaymentSelectionResponse");
  127. break;
  128. case PaymentDetailsRequest:
  129. sprintf(statusName, "PaymentDetailsRequest");
  130. break;
  131. case PaymentDetailsResponse:
  132. sprintf(statusName, "PaymentDetailsResponse");
  133. break;
  134. case AuthorizationRequest:
  135. sprintf(statusName, "AuthorizationRequest");
  136. break;
  137. case AuthorizationResponse:
  138. sprintf(statusName, "AuthorizationResponse");
  139. break;
  140. case CertificateUpdateRequest:
  141. sprintf(statusName, "CertificateUpdateRequest");
  142. break;
  143. case CertificateUpdateResponse:
  144. sprintf(statusName, "CertificateUpdateResponse");
  145. break;
  146. case CertificateInstallationRequest:
  147. sprintf(statusName, "CertificateInstallationRequest");
  148. break;
  149. case CertificateInstallationResponse:
  150. sprintf(statusName, "CertificateInstallationResponse");
  151. break;
  152. case ChargeParameterDiscoveryRequest:
  153. sprintf(statusName, "ChargeParameterDiscoveryRequest");
  154. break;
  155. case ChargeParameterDiscoveryResponse:
  156. sprintf(statusName, "ChargeParameterDiscoveryResponse");
  157. break;
  158. case CableCheckRequest:
  159. sprintf(statusName, "CableCheckRequest");
  160. break;
  161. case CableCheckResponse:
  162. sprintf(statusName, "CableCheckResponse");
  163. break;
  164. case PreChargeRequest:
  165. sprintf(statusName, "PreChargeRequest");
  166. break;
  167. case PreChargeResponse:
  168. sprintf(statusName, "PreChargeResponse");
  169. break;
  170. case PowerDeliveryRequestStart:
  171. sprintf(statusName, "PowerDeliveryRequestStart");
  172. break;
  173. case PowerDeliveryResponsetStart:
  174. sprintf(statusName, "PowerDeliveryResponsetStart");
  175. break;
  176. case ChargingStatusRequest:
  177. sprintf(statusName, "ChargingStatusRequest");
  178. break;
  179. case ChargingStatusResponse:
  180. sprintf(statusName, "ChargingStatusResponse");
  181. break;
  182. case CurrentDemandRequest:
  183. sprintf(statusName, "CurrentDemandRequest");
  184. break;
  185. case CurrentDemandResponse:
  186. sprintf(statusName, "CurrentDemandResponse");
  187. break;
  188. case MeteringReceiptRequest:
  189. sprintf(statusName, "MeteringReceiptRequest");
  190. break;
  191. case MeteringReceiptResponse:
  192. sprintf(statusName, "MeteringReceiptResponse");
  193. break;
  194. case PowerDeliveryRequestStop:
  195. sprintf(statusName, "PowerDeliveryRequestStop");
  196. break;
  197. case PowerDeliveryResponseStop:
  198. sprintf(statusName, "PowerDeliveryResponseStop");
  199. break;
  200. case WeldingDetectionRequest:
  201. sprintf(statusName, "WeldingDetectionRequest");
  202. break;
  203. case WeldingDetectionResponse:
  204. sprintf(statusName, "WeldingDetectionResponse");
  205. break;
  206. case SessionStopRequest:
  207. sprintf(statusName, "SessionStopRequest");
  208. break;
  209. case SessionStopResponse:
  210. sprintf(statusName, "SessionStopResponse");
  211. break;
  212. case Performance_Timeout:
  213. sprintf(statusName, "Performance_Timeout");
  214. break;
  215. case Sequence_Timeout:
  216. sprintf(statusName, "Sequence_Timeout");
  217. break;
  218. case Other_Fault:
  219. sprintf(statusName, "Other_Fault");
  220. break;
  221. default:
  222. sprintf(statusName, "Unknown");
  223. result = FAIL;
  224. break;
  225. }
  226. return result;
  227. }
  228. /*===========================================================================
  229. FUNCTION: Check_V2G_Flow_Status
  230. DESCRIPTION:
  231. PRE-CONDITION:
  232. INPUT:
  233. OUTPUT:
  234. GLOBAL VARIABLES:
  235. =============================================================================*/
  236. unsigned char Check_V2G_Flow_Status()
  237. {
  238. unsigned char result = 0;
  239. switch (ShmCcsData->CommProtocol)
  240. {
  241. case V2GT_MSG_PROTOCOL_DIN70121: //0
  242. {
  243. result = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus;
  244. break;
  245. }
  246. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  247. {
  248. result = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus;
  249. break;
  250. }
  251. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  252. {
  253. result = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus;
  254. break;
  255. }
  256. default:
  257. break;
  258. }
  259. return result;
  260. }
  261. /*===========================================================================
  262. FUNCTION: CAN_Tx_MSG
  263. DESCRIPTION:
  264. PRE-CONDITION:
  265. INPUT:
  266. OUTPUT:
  267. GLOBAL VARIABLES:
  268. =============================================================================*/
  269. int CAN_Tx_MSG(int Fd, unsigned int MsgId, unsigned char SlaveAddress, unsigned char DataLength, unsigned char *SendData)
  270. {
  271. #if 0
  272. struct can_frame frame;
  273. struct timeb StartTime, EndTime;
  274. unsigned int tmp = 0;
  275. int nbytes = 0;
  276. int i = 0;
  277. //Protection: To avoid unexpected length for CAN bus payload.
  278. if (DataLength > 8)
  279. {
  280. DataLength = 8;
  281. }
  282. memset(&frame, 0, sizeof(struct can_frame));
  283. frame.can_id = 0x80000000 | CAN_SEND_DIRECTION | MsgId | SlaveAddress; //0x80000000: extension ID format
  284. frame.can_dlc = DataLength;
  285. memcpy(frame.data, SendData, DataLength);
  286. nbytes = write(Fd, &frame, sizeof(struct can_frame));
  287. #if 0
  288. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][CAN_Tx_MSG] <%X> ", frame.can_id);
  289. for (i = 0; i < frame.can_dlc; i++)
  290. {
  291. DEBUG_PRINTF_EVCOMM_DETAIL("%02X ", frame.data[i]);
  292. }
  293. DEBUG_PRINTF_EVCOMM_DETAIL("(%d Bytes)\n", frame.can_dlc);
  294. #endif
  295. return nbytes;
  296. #else
  297. return -1;
  298. #endif
  299. }
  300. /*===========================================================================
  301. FUNCTION: Proc_EVStopRes
  302. DESCRIPTION:
  303. PRE-CONDITION:
  304. INPUT:
  305. OUTPUT:
  306. GLOBAL VARIABLES:
  307. =============================================================================*/
  308. int Proc_EVStopRes(int Fd)
  309. {
  310. int nbytes;
  311. unsigned char Buffer[8];
  312. memset(Buffer, 0, sizeof(Buffer));
  313. if (CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency == TRUE)
  314. {
  315. Buffer[0] = EV_EMERGENCY_STOP; //2
  316. }
  317. else
  318. {
  319. Buffer[0] = EV_NORMAL_STOP; //1
  320. }
  321. /*
  322. if(strlen(ShmStatusCodeData->PresentStatusCode[0]) > 0)
  323. {
  324. memcpy(Buffer + 2, ShmStatusCodeData->PresentStatusCode[0], 6);
  325. }
  326. */
  327. Buffer[1] = ShmStatusCodeData->PresentStatusCode[0][0];
  328. Buffer[2] = ShmStatusCodeData->PresentStatusCode[0][1];
  329. Buffer[3] = ShmStatusCodeData->PresentStatusCode[0][2];
  330. Buffer[4] = ShmStatusCodeData->PresentStatusCode[0][3];
  331. Buffer[5] = ShmStatusCodeData->PresentStatusCode[0][4];
  332. Buffer[6] = ShmStatusCodeData->PresentStatusCode[0][5];
  333. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_EV_STOP_EVENT, ShmInternalComm->SlaveAddress, 7, Buffer);
  334. //system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"); //for test only
  335. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  336. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_EVStopRes] Sending STOP Command to CSU");
  337. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = FALSE;
  338. return nbytes;
  339. }
  340. /*===========================================================================
  341. FUNCTION: Sniffer_Candump
  342. DESCRIPTION:
  343. 1. Only used in End_Process(), which is in fork2 of SeccComm task.
  344. PRE-CONDITION:
  345. INPUT:
  346. OUTPUT:
  347. 0: accept
  348. -1: invalid
  349. GLOBAL VARIABLES:
  350. =============================================================================*/
  351. int Sniffer_Candump(char cmd)
  352. {
  353. #if (CANDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  354. if (cmd == ENABLE)
  355. {
  356. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[candump]init");
  357. system("cd /mnt/;rm -rf candump/");
  358. system("cd /mnt/;mkdir candump");
  359. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[candump]on");
  360. system("cd /mnt/candump;candump -l can0 &");
  361. return 0;
  362. }
  363. else if (cmd == DISABLE)
  364. {
  365. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[candump]off");
  366. system("killall candump");
  367. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[candump]save");
  368. system("cd /;cp -rfv /mnt/candump /Storage/SystemLog/");
  369. return 0;
  370. }
  371. else
  372. {
  373. sprintf((char*)buf_log_evcomm_fork2, "[candump]unexpected cmd(%d)", cmd);
  374. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  375. return -1;
  376. }
  377. #endif
  378. return 0;
  379. }
  380. /*===========================================================================
  381. FUNCTION: Sniffer_Tcpdump
  382. DESCRIPTION:
  383. PRE-CONDITION:
  384. INPUT:
  385. OUTPUT:
  386. GLOBAL VARIABLES:
  387. =============================================================================*/
  388. int Sniffer_Tcpdump(char cmd)
  389. {
  390. #if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  391. if (cmd == ENABLE)
  392. {
  393. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]init...");
  394. system("cd /mnt/;rm -rf tcpdump/");
  395. system("cd /mnt/;mkdir tcpdump");
  396. unsigned char buf[256];
  397. time_t CurrentTime;
  398. struct tm *tm;
  399. struct timeval tv;
  400. memset(buf, 0, sizeof(buf));
  401. CurrentTime = time(NULL);
  402. tm = localtime(&CurrentTime);
  403. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  404. sprintf((char*)buf,
  405. "tcpdump -i eth1 -w /mnt/tcpdump/[%s][%04d%02d%02d-%02d:%02d:%02d.%06d]packets.pcap&",
  406. FIRMWARE_VERSION,
  407. tm->tm_year + 1900,
  408. tm->tm_mon + 1,
  409. tm->tm_mday,
  410. tm->tm_hour,
  411. tm->tm_min,
  412. tm->tm_sec,
  413. tv.tv_usec
  414. );
  415. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]on");
  416. system(buf);
  417. return 0;
  418. }
  419. else if (cmd == DISABLE)
  420. {
  421. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]off");
  422. system("killall tcpdump");
  423. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]wait: 2s");
  424. sleep(1);
  425. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]wait: 1s");
  426. sleep(1);
  427. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]save");
  428. system("cd /;cp -rfv /mnt/tcpdump /Storage/SystemLog/");
  429. return 0;
  430. }
  431. else
  432. {
  433. sprintf((char*)buf_log_evcomm, "[tcpdump]unexpected cmd(%d)", cmd);
  434. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  435. return -1;
  436. }
  437. #endif
  438. return 0;
  439. }
  440. /*===========================================================================
  441. FUNCTION: Check_V2G_Flow_Status_pre
  442. DESCRIPTION:
  443. PRE-CONDITION:
  444. INPUT:
  445. OUTPUT:
  446. GLOBAL VARIABLES:
  447. =============================================================================*/
  448. unsigned char Check_V2G_Flow_Status_pre()
  449. {
  450. unsigned char result = 0;
  451. switch (ShmCcsData->CommProtocol)
  452. {
  453. case V2GT_MSG_PROTOCOL_DIN70121: //0
  454. {
  455. result = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus_pre;
  456. break;
  457. }
  458. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  459. {
  460. result = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus_pre;
  461. break;
  462. }
  463. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  464. {
  465. result = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus_pre;
  466. break;
  467. }
  468. default:
  469. break;
  470. }
  471. return result;
  472. }
  473. /*===========================================================================
  474. FUNCTION: Update_V2G_Flow_Status
  475. DESCRIPTION:
  476. PRE-CONDITION:
  477. INPUT:
  478. OUTPUT:
  479. 1. V2gFlowStatus
  480. GLOBAL VARIABLES:
  481. 1. V2gFlowStatus
  482. =============================================================================*/
  483. int Update_V2G_Flow_Status(unsigned int state_new)
  484. {
  485. unsigned char state_now;
  486. char nameNow[64], nameNew[64];
  487. state_now = Check_V2G_Flow_Status();
  488. get_V2G_Status_Name(state_now, nameNow);
  489. get_V2G_Status_Name(state_new, nameNew);
  490. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE ||
  491. CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused == TRUE)
  492. {
  493. if (state_now != Performance_Timeout && //253
  494. state_now != Sequence_Timeout && //254
  495. state_now != Other_Fault) //255
  496. {
  497. sprintf((char*)buf_log_evcomm,
  498. "[Warning]state-%s(%d) change: ignored (End_Process_inused:%d, FW_Update_Task_inused:%d)",
  499. nameNew, state_new,
  500. EVCOMM_SYS_INFO.End_Process_inused,
  501. CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused
  502. );
  503. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  504. return -1;
  505. }
  506. }
  507. if (state_now != state_new)
  508. {
  509. //Step 1: Check if Error Occours
  510. if (state_now == Performance_Timeout || //253
  511. state_now == Sequence_Timeout || //254
  512. state_now == Other_Fault) //255
  513. {
  514. if (state_new != IDLE)
  515. {
  516. if (EVCOMM_SYS_INFO.State_Change_Ignored_Notice == FALSE)
  517. {
  518. EVCOMM_SYS_INFO.State_Change_Ignored_Notice = TRUE;
  519. sprintf((char*)buf_log_evcomm,
  520. "[Warning]state-%s(%d) change: ignored(now in error state-%s(%d))",
  521. nameNow, state_new,
  522. nameNew, state_now);
  523. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  524. }
  525. return -1;
  526. }
  527. }
  528. //Updating the state formally.
  529. sprintf((char*)buf_log_evcomm,
  530. "%s(%02d) >> %s(%02d)",
  531. nameNow, state_now,
  532. nameNew, state_new
  533. );
  534. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  535. V2gFlowStatus = state_new;
  536. state_now = state_new;
  537. switch (ShmCcsData->CommProtocol)
  538. {
  539. case V2GT_MSG_PROTOCOL_DIN70121: //0
  540. {
  541. ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus;
  542. ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus = V2gFlowStatus;
  543. break;
  544. }
  545. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  546. {
  547. ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus;
  548. ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus = V2gFlowStatus;
  549. break;
  550. }
  551. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  552. {
  553. ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus;
  554. ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus = V2gFlowStatus;
  555. break;
  556. }
  557. default:
  558. break;
  559. }
  560. }
  561. return 0;
  562. }
  563. double DiffTimeb(struct timeb ST, struct timeb ET)
  564. {
  565. //return milli-second
  566. double StartTime, EndTime;
  567. double t_diff;
  568. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  569. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  570. t_diff = EndTime - StartTime;
  571. if (t_diff < 0)
  572. {
  573. #if 0
  574. if (t_diff < -1000) //1000ms
  575. {
  576. sprintf((char*)buf_log_evcomm,
  577. "[Warning]StartTime(%.02lf) > EndTime(%.02lf), d(%.02lf)",
  578. StartTime,
  579. EndTime,
  580. t_diff);
  581. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  582. }
  583. #endif
  584. return -1;
  585. }
  586. return t_diff;
  587. }
  588. double DiffTimeb_fork1_Error_Monitor(struct timeb ST, struct timeb ET)
  589. {
  590. //return milli-second
  591. static double StartTime, EndTime;
  592. static double t_diff;
  593. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  594. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  595. t_diff = EndTime - StartTime;
  596. if (t_diff < 0)
  597. {
  598. #if 0
  599. if (t_diff < -1000) //1000ms
  600. {
  601. sprintf((char*)buf_log_evcomm_fork2,
  602. "[fork1][Warning]S(%.02lf) > E(%.02lf), d(%.02lf)",
  603. StartTime,
  604. EndTime,
  605. t_diff);
  606. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  607. }
  608. #endif
  609. return -1;
  610. }
  611. return t_diff;
  612. }
  613. double DiffTimeb_fork2_Error_Monitor(struct timeb ST, struct timeb ET)
  614. {
  615. //return milli-second
  616. static double StartTime, EndTime;
  617. static double t_diff;
  618. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  619. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  620. t_diff = EndTime - StartTime;
  621. if (t_diff < 0)
  622. {
  623. #if 0
  624. if (t_diff < -1000) //1000ms
  625. {
  626. sprintf((char*)buf_log_evcomm_fork2,
  627. "[fork2][Warning]S(%.02lf) > E(%.02lf), d(%.02lf)",
  628. StartTime,
  629. EndTime,
  630. t_diff);
  631. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  632. }
  633. #endif
  634. return -1;
  635. }
  636. return t_diff;
  637. }
  638. int CreateShareMemory()
  639. {
  640. int MeterSMId;
  641. //create ShmSysConfigAndInfo
  642. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  643. {
  644. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmget ShmSysConfigAndInfo NG");
  645. return 0;
  646. }
  647. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  648. {
  649. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmat ShmSysConfigAndInfo NG");
  650. return 0;
  651. }
  652. memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
  653. //create ShmStatusCodeData
  654. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  655. {
  656. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmget ShmStatusCodeData NG");
  657. return 0;
  658. }
  659. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  660. {
  661. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmat ShmStatusCodeData NG");
  662. return 0;
  663. }
  664. memset(ShmStatusCodeData,0,sizeof(struct StatusCodeData));
  665. //create ShmCcsData
  666. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  667. {
  668. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmget ShmCcsData NG");
  669. return 0;
  670. }
  671. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  672. {
  673. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmat ShmCcsData NG");
  674. return 0;
  675. }
  676. memset(ShmCcsData,0,sizeof(struct CcsData));
  677. return 1;
  678. }
  679. /*===========================================================================
  680. FUNCTION: ShareMemory_Init
  681. DESCRIPTION:
  682. PRE-CONDITION:
  683. INPUT:
  684. OUTPUT:
  685. GLOBAL VARIABLES:
  686. =============================================================================*/
  687. int ShareMemory_Init()
  688. {
  689. int MeterSMId;
  690. //create ShmSysConfigAndInfo
  691. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  692. {
  693. DEBUG_ERROR("ShareMemory_Init:shmget ShmSysConfigAndInfo NG");
  694. return 0;
  695. }
  696. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  697. {
  698. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "ShareMemory_Init:shmat ShmSysConfigAndInfo NG");
  699. return 0;
  700. }
  701. //create ShmStatusCodeData
  702. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  703. {
  704. DEBUG_ERROR("ShareMemory_Init:shmget ShmStatusCodeData NG");
  705. return 0;
  706. }
  707. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  708. {
  709. DEBUG_ERROR("ShareMemory_Init:shmat ShmStatusCodeData NG");
  710. return 0;
  711. }
  712. //create ShmCcsData
  713. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  714. {
  715. DEBUG_ERROR("ShareMemory_Init:shmget ShmCcsData NG");
  716. return 0;
  717. }
  718. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  719. {
  720. DEBUG_ERROR("ShareMemory_Init:shmat ShmCcsData NG");
  721. return 0;
  722. }
  723. //create ShmInternalComm
  724. if ((MeterSMId = shmget(ShmInternalCommKey, sizeof(struct InternalComm), IPC_CREAT | 0777)) < 0)
  725. {
  726. DEBUG_ERROR("ShareMemory_Init:shmget ShmInternalComm NG");
  727. return 0;
  728. }
  729. else if ((ShmInternalComm = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  730. {
  731. DEBUG_ERROR("ShareMemory_Init:shmat ShmInternalComm NG");
  732. return 0;
  733. }
  734. //Initial ShmCharger
  735. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), 0777)) < 0)
  736. {
  737. DEBUG_ERROR("shmget ShmChargerKey NG\r\n");
  738. return 0;
  739. }
  740. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  741. {
  742. DEBUG_ERROR("shmat ShmChargerKey NG\r\n");
  743. return 0;
  744. }
  745. //[To-Do] The initialization here is reduntant and should be removed partially.
  746. //SHM_Init_supportedAppProtocolRes(ShmCcsData);
  747. //SHM_Init_din_SessionSetupRes(ShmCcsData);
  748. //SHM_Init_din_ServiceDiscoveryRes(ShmCcsData);
  749. //SHM_Init_din_ServiceAndPaymentSelectionRes(ShmCcsData);
  750. //SHM_Init_din_ContractAuthenticationRes(ShmCcsData);
  751. SHM_Init_din_ChargeParameterDiscoveryRes(ShmCcsData);
  752. SHM_Init_din_CableCheckRes(ShmCcsData);
  753. SHM_Init_din_PreChargeRes(ShmCcsData);
  754. SHM_Init_din_PowerDeliveryRes(ShmCcsData);
  755. SHM_Init_din_CurrentDemandRes(ShmCcsData);
  756. SHM_Init_din_WeldingDetectionRes(ShmCcsData);
  757. SHM_Init_din_SessionStopRes(ShmCcsData);
  758. return 1;
  759. }
  760. /*===========================================================================
  761. FUNCTION: GetEthMac
  762. DESCRIPTION:
  763. 1. Accessing current CsuMac address, if Eth = eth1
  764. 2. The address of eth1(for QCA7000) is a random number, which will be modified
  765. after each time of booting up system.
  766. PRE-CONDITION:
  767. INPUT:
  768. OUTPUT:
  769. GLOBAL VARIABLES:
  770. =============================================================================*/
  771. int GetEthMac(unsigned char *Eth, unsigned char *mac)
  772. {
  773. //Parameters:MAC,IP,Mask,Gateway
  774. int fd;
  775. unsigned char addr[18], Buffer[128];
  776. memset(Buffer, 0, sizeof(Buffer));
  777. sprintf((char*)Buffer, "cat /sys/class/net/%s/address > /mnt/GetEthInfo", Eth); //CsuMac (Eth = eth1)
  778. system((char*)Buffer);
  779. fd = open("/mnt/GetEthInfo", O_RDONLY);
  780. if(fd < 0)
  781. {
  782. system("rm -f /mnt/GetEthInfo");
  783. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "GetEthMac: MAC Address open error");
  784. return 0;
  785. }
  786. memset(mac, 0, 6);
  787. memset(addr, 0, sizeof(addr));
  788. read(fd, addr, 17);
  789. close(fd);
  790. system("rm -f /mnt/GetEthInfo");
  791. sscanf((char*)addr, "%02x:%02x:%02x:%02x:%02x:%02x", (uint32_t *)&mac[0], (uint32_t *)&mac[1], (uint32_t *)&mac[2], (uint32_t *)&mac[3], (uint32_t *)&mac[4], (uint32_t *)&mac[5]); //CsuMac (Eth = eth1)
  792. sprintf((char*)buf_log_evcomm,
  793. "[GetEthMac]EVSE MACAddr(CsuMac): %02x:%02x:%02x:%02x:%02x:%02x",
  794. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); //CsuMac (Eth = eth1)
  795. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  796. return 1;
  797. }
  798. /*===========================================================================
  799. FUNCTION: ReadAdcVolt
  800. DESCRIPTION:
  801. 1. fork1
  802. PRE-CONDITION:
  803. INPUT:
  804. OUTPUT:
  805. GLOBAL VARIABLES:
  806. =============================================================================*/
  807. float ReadAdcVolt(unsigned char AdcChannel)
  808. {
  809. //AIN0=CCS GUN Temp 1
  810. //AIN1=CCS GUN Temp 2
  811. //AIN2=CCS_Proximity/2
  812. //AIN3=pilot voltage
  813. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  814. return 0;
  815. #else
  816. if(AdcChannel == 3)
  817. {
  818. int fd,count,AvgTimes;
  819. unsigned char SampleBuf[4];
  820. float TmpVolt, MinSample, AvgSample = 0;
  821. fd = open("/sys/bus/iio/devices/iio\\:device0/in_voltage3_raw", O_RDONLY);
  822. if(fd > 0)
  823. {
  824. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  825. for(AvgTimes = 0; AvgTimes < 3; AvgTimes++) //period = 60~91 ms(renice -10, , CurrentDemand()) /*+++ 20200909, vern, extend detection time for interference ---*/
  826. {
  827. count = 0;
  828. MinSample = 2306;
  829. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  830. while(count < 40) //period = 21~42ms (renice -10, CurrentDemand()) /*+++ 20200909, vern, extend detection time for interference ---*/
  831. {
  832. //re-sampling period = 3~13ms (renice -10, SLAC())
  833. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  834. read(fd, SampleBuf, 4); //period = 3.2~10ms (renice -10, SLAC())
  835. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  836. TmpVolt = atoi((char*)SampleBuf);
  837. if((TmpVolt < 2306) && (TmpVolt > 0))//positive voltage
  838. {
  839. if(TmpVolt < MinSample)
  840. {
  841. MinSample = TmpVolt;
  842. }
  843. count++;
  844. }
  845. lseek(fd, 0, SEEK_SET);
  846. }
  847. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  848. AvgSample += MinSample;
  849. }
  850. AvgSample /= AvgTimes;
  851. close(fd);
  852. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  853. return ((0.954-(1.8*AvgSample/4095))/0.06);
  854. }
  855. else
  856. {
  857. return -1;
  858. }
  859. }
  860. else
  861. {
  862. FILE *fp;
  863. unsigned char str[64];
  864. unsigned char AdcValue[8];
  865. if(AdcChannel > 7)
  866. {
  867. return -1;
  868. }
  869. memset(str,0,sizeof(str));
  870. memset(AdcValue,0,sizeof(AdcValue));
  871. sprintf((char*)str, "cat /sys/bus/iio/devices/iio\\:device0/in_voltage%d_raw", AdcChannel);
  872. fp=popen((char*)str, "r");
  873. if(fgets((char*)AdcValue,sizeof(AdcValue),fp) == NULL)
  874. {
  875. pclose(fp);
  876. return -1;
  877. }
  878. pclose(fp);
  879. //Vin = Vref *D / (2^n - 1)
  880. return ((float)1.8*(float)atoi((char*)AdcValue))/4095;
  881. }
  882. #endif
  883. }
  884. /*===========================================================================
  885. FUNCTION: ReadAdcVolt
  886. DESCRIPTION:
  887. //AIN0=CCS GUN Temp 1
  888. //AIN1=CCS GUN Temp 2
  889. //AIN2=CCS_Proximity/2
  890. //AIN3=pilot voltage
  891. PRE-CONDITION:
  892. INPUT:
  893. OUTPUT:
  894. GLOBAL VARIABLES:
  895. =============================================================================*/
  896. float ReadAdcVolt_PP_fork3()
  897. {
  898. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  899. return 0;
  900. #else
  901. int fd, AvgTimes;
  902. unsigned char SampleBuf[4];
  903. float TmpVolt = 0;
  904. float AvgSample = 0;
  905. float V_pp = 0;
  906. fd = open("/sys/bus/iio/devices/iio\\:device0/in_voltage2_raw", O_RDONLY); //PP
  907. if(fd > 0)
  908. {
  909. for(AvgTimes = 0; AvgTimes < 5; AvgTimes++) //get 5 samples
  910. {
  911. read(fd, SampleBuf, 4);
  912. TmpVolt = atoi((char*)SampleBuf);
  913. lseek(fd, 0, SEEK_SET);
  914. AvgSample += TmpVolt;
  915. }
  916. close(fd);
  917. AvgSample /= AvgTimes;
  918. V_pp = (3.6*AvgSample)/4095; //PP
  919. #if 0
  920. printf("PP_adc = %.02f, PP_v = %.02f\n", AvgSample, V_pp);
  921. usleep(3000000); //300ms
  922. #endif
  923. usleep(20000); //20ms
  924. return V_pp;
  925. }
  926. else
  927. {
  928. return -1;
  929. }
  930. #endif
  931. }
  932. /*===========================================================================
  933. FUNCTION: Qca7kPowerReset
  934. DESCRIPTION:
  935. PRE-CONDITION:
  936. INPUT:
  937. OUTPUT:
  938. GLOBAL VARIABLES:
  939. =============================================================================*/
  940. void Qca7kPowerReset()
  941. {
  942. printf("\n[CCS Board]Reset QCA7000...\n");
  943. system("echo 1 > /sys/class/gpio/gpio88/value");
  944. usleep(500000);
  945. system("echo 0 > /sys/class/gpio/gpio88/value");
  946. }
  947. /*===========================================================================
  948. FUNCTION: SwitchCpStateE
  949. DESCRIPTION:
  950. PRE-CONDITION:
  951. INPUT:
  952. OUTPUT:
  953. GLOBAL VARIABLES:
  954. =============================================================================*/
  955. int SwitchCpStateE(unsigned char OnOff)
  956. {
  957. /*
  958. * TODO: Request CP change to state E
  959. */
  960. if((OnOff != ENABLE) && (OnOff != DISABLE))
  961. {
  962. return -1;
  963. }
  964. struct ChargingInfoData *ccs;
  965. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  966. ccs->SwitchCpStateE_status = OnOff;
  967. //OnOff = 1 => switch State to E
  968. //OnOff = 0 => return noraml
  969. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  970. {
  971. if(OnOff == DISABLE)
  972. {
  973. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SwitchCpStateE: released\n");
  974. CSUCOMMAC_SHM.CpSetStateE = DISABLE;
  975. }
  976. else
  977. {
  978. OutputCpPwmDuty(100); //set CP duty as 100, firstly.
  979. CSUCOMMAC_SHM.CpSetStateE = ENABLE;
  980. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SwitchCpStateE: enabled!\n");
  981. }
  982. }
  983. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  984. {
  985. if(OnOff == DISABLE)
  986. {
  987. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SwitchCpStateE: released\n");
  988. system("echo 0 > /sys/class/gpio/gpio86/value");
  989. }
  990. else
  991. {
  992. OutputCpPwmDuty(100); //set CP duty as 100, firstly.
  993. system("echo 1 > /sys/class/gpio/gpio86/value");
  994. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SwitchCpStateE: enabled!\n");
  995. }
  996. }
  997. #endif
  998. if (ccs->SwitchCpStateE_status_pre != ccs->SwitchCpStateE_status)
  999. {
  1000. sprintf((char*)buf_log_evcomm,
  1001. "SwitchCpStateE: %d >> %d",
  1002. ccs->SwitchCpStateE_status_pre,
  1003. ccs->SwitchCpStateE_status
  1004. );
  1005. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  1006. ccs->SwitchCpStateE_status_pre = ccs->SwitchCpStateE_status;
  1007. }
  1008. return 0;
  1009. }
  1010. /*===========================================================================
  1011. FUNCTION: OutputCpPwmDuty
  1012. DESCRIPTION:
  1013. PRE-CONDITION:
  1014. INPUT:
  1015. OUTPUT:
  1016. GLOBAL VARIABLES:
  1017. =============================================================================*/
  1018. int OutputCpPwmDuty(unsigned char Duty)
  1019. {
  1020. #if ((CCS_ENERGY_TRANSFER_MODE != MODE_AC_SINGLE_PHASE_CORE) && (CCS_ENERGY_TRANSFER_MODE != MODE_AC_THREE_PHASE_CORE))
  1021. int DutyInNanoSec;
  1022. #endif
  1023. struct ChargingInfoData *ccs;
  1024. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1025. if((Duty < 0)||(Duty > 100))
  1026. {
  1027. return -1;
  1028. }
  1029. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  1030. {
  1031. CSUCOMMAC_SHM.CpSetPWMDuty = Duty;
  1032. }
  1033. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  1034. {
  1035. DutyInNanoSec = 10000 * Duty;
  1036. sprintf((char*)buf_log_evcomm, "echo %d > /sys/class/pwm/pwmchip0/pwm0/duty_cycle", DutyInNanoSec);//nanoseconds
  1037. system((char*)buf_log_evcomm);
  1038. }
  1039. #endif
  1040. ccs->CpDuty = Duty;
  1041. //CP Duty
  1042. if (ccs->CpDuty != ccs->CpDuty_pre)
  1043. {
  1044. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  1045. sprintf((char*)buf_log_evcomm, "CP Duty: %d %% >> %d %%", ccs->CpDuty_pre, ccs->CpDuty);
  1046. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  1047. ccs->CpDuty_pre = ccs->CpDuty;
  1048. }
  1049. return 0;
  1050. }
  1051. /*===========================================================================
  1052. FUNCTION: Check_Plugin_Status_Update_fork1
  1053. DESCRIPTION:
  1054. PRE-CONDITION:
  1055. INPUT:
  1056. OUTPUT:
  1057. GLOBAL VARIABLES:
  1058. =============================================================================*/
  1059. //#if SAVE_SYS_LOG_MSG_EVCOMM_SWITCH == ENABLE
  1060. void Check_Plugin_Status_Update_fork1(struct ChargingInfoData *ccs)
  1061. {
  1062. if (ccs->ConnectorPlugIn != ccs->ConnectorPlugIn_new)
  1063. {
  1064. sprintf((char*)buf_log_evcomm_fork1, "[fork1]Plugin: %d >> %d (CP=%.2fV, PP=%.2fV)",
  1065. ccs->ConnectorPlugIn,
  1066. ccs->ConnectorPlugIn_new,
  1067. ccs->CpVoltage,
  1068. ccs->PpVoltage
  1069. );
  1070. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork1);
  1071. ccs->ConnectorPlugIn_pre = ccs->ConnectorPlugIn;
  1072. ccs->ConnectorPlugIn = ccs->ConnectorPlugIn_new;
  1073. //ccs->CpVoltage_pre = ccs->CpVoltage;
  1074. }
  1075. }
  1076. //#endif
  1077. /*===========================================================================
  1078. FUNCTION: ConnectorPlugIn
  1079. DESCRIPTION:
  1080. PRE-CONDITION:
  1081. INPUT:
  1082. OUTPUT:
  1083. 0: Unplug
  1084. 1: Plug-in
  1085. GLOBAL VARIABLES:
  1086. =============================================================================*/
  1087. int CheckConnectorPlugIn()
  1088. {
  1089. /*
  1090. * TODO: Return connector status depedon CP
  1091. */
  1092. static struct ChargingInfoData *ccs;
  1093. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1094. #if (CP_PROTECTION_MECHANISM == ENABLE)
  1095. return (int)ccs->ConnectorPlugIn;
  1096. #else
  1097. return TRUE;
  1098. #endif
  1099. }
  1100. /*===========================================================================
  1101. FUNCTION: Check_CP_State_Error_fork1
  1102. DESCRIPTION:
  1103. PRE-CONDITION:
  1104. INPUT:
  1105. OUTPUT:
  1106. GLOBAL VARIABLES:
  1107. =============================================================================*/
  1108. int Check_CP_State_Error_fork1(struct ChargingInfoData *ccs)
  1109. {
  1110. #if (CP_PROTECTION_MECHANISM == DISABLE)
  1111. {
  1112. return -1;
  1113. }
  1114. #endif
  1115. unsigned char state = 0;
  1116. #if ((CCS_ENERGY_TRANSFER_MODE != MODE_AC_SINGLE_PHASE_CORE) && (CCS_ENERGY_TRANSFER_MODE != MODE_AC_THREE_PHASE_CORE))
  1117. double time_diff = 0;
  1118. #endif
  1119. state = Check_V2G_Flow_Status();
  1120. //SLAC, SLAAC, SDP, ..., ChargeParameterDiscovery
  1121. if (state >= CM_SLAC_PARM_CONF && //by considering 15118(SLAC first)
  1122. state < ChargeParameterDiscoveryRequest &&
  1123. state != IDLE &&
  1124. state != CM_SET_KEY_REQ &&
  1125. state != CM_SET_KEY_CNF &&
  1126. state != CM_VALIDATE_REQ &&
  1127. state != CM_VALIDATE_CNF)
  1128. {
  1129. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  1130. {
  1131. if((ccs->CpState != 2) && (ccs->CpState != 3))
  1132. {
  1133. if (ccs->CpState_err == FALSE)
  1134. {
  1135. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork1][CP][Error]before CPD"); //CPD: ChargeParameterDiscoveryRequest
  1136. //CPD: ChargeParameterDiscovery
  1137. }
  1138. ccs->CpState_err = TRUE;
  1139. }
  1140. }
  1141. #else
  1142. {
  1143. if(ccs->CpState != 3)
  1144. {
  1145. if (ccs->CpState_err == FALSE)
  1146. {
  1147. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork1][CP][Error]before CPD"); //CPD: ChargeParameterDiscoveryRequest
  1148. //CPD: ChargeParameterDiscovery
  1149. }
  1150. ccs->CpState_err = TRUE;
  1151. }
  1152. }
  1153. #endif
  1154. }
  1155. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  1156. {
  1157. //ChargeParameterDiscovery
  1158. if(state >= ChargeParameterDiscoveryRequest && //35
  1159. state <= ChargeParameterDiscoveryResponse) //36
  1160. {
  1161. if(ccs->CpState != 3)
  1162. {
  1163. if (ccs->CpState_err == FALSE)
  1164. {
  1165. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CP][Error]CPD");
  1166. //PRC: Precharge
  1167. //CUD: CurrentDemand
  1168. }
  1169. ccs->CpState_err = TRUE;
  1170. }
  1171. }
  1172. //ChargingStatus
  1173. if(state >= ChargingStatusRequest && //43
  1174. state <= ChargingStatusResponse) //44
  1175. {
  1176. if(ccs->CpState != 4 && ccs->CpState != 5)
  1177. {
  1178. if (ccs->CpState_err == FALSE)
  1179. {
  1180. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CP][Error]CGS");
  1181. //PRC: Precharge
  1182. //CUD: CurrentDemand
  1183. }
  1184. ccs->CpState_err = TRUE;
  1185. }
  1186. }
  1187. }
  1188. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  1189. {
  1190. //ChargeParameterDiscovery, CableCheck
  1191. if (state >= ChargeParameterDiscoveryRequest && //35
  1192. state <= CableCheckResponse) //38
  1193. {
  1194. if(ccs->CableCheckPreCountDownDone == FALSE &&
  1195. state >= CableCheckRequest)
  1196. {
  1197. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  1198. time_diff = DiffTimeb_fork1_Error_Monitor(EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start, EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  1199. if(time_diff >= V2G_SECC_CP_Timeout_CableCheck) //2 sec
  1200. {
  1201. sprintf((char*)buf_log_evcomm_fork1,
  1202. "[fork1][CP]check C(4),D(5): ON (%.02lf of %dms)",
  1203. time_diff,
  1204. V2G_SECC_CP_Timeout_CableCheck);
  1205. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork1);
  1206. ccs->CableCheckPreCountDownDone = TRUE;
  1207. }
  1208. }
  1209. if(ccs->CableCheckPreCountDownDone == TRUE)
  1210. {
  1211. if(ccs->CpState != 4 && ccs->CpState != 5)
  1212. {
  1213. if (ccs->CpState_err == FALSE)
  1214. {
  1215. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CP][Error]CPD and CCK (after CNT)");
  1216. //CPD: ChargeParameterDiscovery
  1217. //CCK: CableCheck
  1218. //CNT: count down
  1219. }
  1220. ccs->CpState_err = TRUE;
  1221. }
  1222. }
  1223. else
  1224. {
  1225. if(ccs->CpState != 3 && ccs->CpState != 4 && ccs->CpState != 5)
  1226. {
  1227. if (ccs->CpState_err == FALSE)
  1228. {
  1229. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CP][Error]CPD and CCK (before CNT)");
  1230. //CPD: ChargeParameterDiscovery
  1231. //CCK: CableCheck
  1232. //CNT: count down
  1233. }
  1234. ccs->CpState_err = TRUE;
  1235. }
  1236. }
  1237. }
  1238. //Precharge, CurrentDemand
  1239. if(state >= PreChargeRequest && //39
  1240. state <= CurrentDemandResponse) //46
  1241. {
  1242. if(ccs->CpState != 4 && ccs->CpState != 5)
  1243. {
  1244. if (ccs->CpState_err == FALSE)
  1245. {
  1246. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CP][Error]PRC and CUD");
  1247. //PRC: Precharge
  1248. //CUD: CurrentDemand
  1249. }
  1250. ccs->CpState_err = TRUE;
  1251. }
  1252. }
  1253. }
  1254. #endif
  1255. if(ccs->CpState_err == TRUE && ccs->CpState_err_logged == FALSE)
  1256. {
  1257. OutputCpPwmDuty(100);
  1258. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  1259. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1260. //CCS_SECC_CP_State_Error (023889)
  1261. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1262. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1263. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1264. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1265. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1266. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1267. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1268. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1269. sprintf((char*)buf_log_evcomm_fork1, "[fork1][Erorr]CP(%d) Error in state %d(%d)",
  1270. ccs->CpState,
  1271. state,
  1272. ccs->CableCheckPreCountDownDone
  1273. );
  1274. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork1);
  1275. ccs->CpState_err_logged = TRUE;
  1276. }
  1277. return 0;
  1278. }
  1279. /*===========================================================================
  1280. FUNCTION: Check_CP_State_Update_fork1
  1281. DESCRIPTION:
  1282. PRE-CONDITION:
  1283. INPUT:
  1284. OUTPUT:
  1285. GLOBAL VARIABLES:
  1286. =============================================================================*/
  1287. //#if SAVE_SYS_LOG_MSG_EVCOMM_SWITCH == ENABLE
  1288. void Check_CP_State_Update_fork1(struct ChargingInfoData *ccs)
  1289. {
  1290. //CP State
  1291. if (ccs->CpState != ccs->CpState_pre)
  1292. {
  1293. sprintf((char*)buf_log_evcomm_fork1, "[fork1]CP State: %d >> %d (%.2fV)",
  1294. ccs->CpState_pre,
  1295. ccs->CpState,
  1296. ccs->CpVoltage
  1297. );
  1298. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork1);
  1299. ccs->CpState_pre = ccs->CpState;
  1300. }
  1301. }
  1302. //#endif
  1303. /*===========================================================================
  1304. FUNCTION: CP_Detection
  1305. DESCRIPTION:
  1306. 1. In order to detect CP in efficient response time, we creat an independent
  1307. thead for this procedure.
  1308. 2. The priority of this thread is set as the same as other tasks.
  1309. 3. fork1
  1310. PRE-CONDITION:
  1311. INPUT:
  1312. OUTPUT:
  1313. GLOBAL VARIABLES:
  1314. =============================================================================*/
  1315. void CP_Detection()
  1316. {
  1317. pid_t tmp = 0;
  1318. // struct timeb StartTime, EndTime;
  1319. #if ((CCS_ENERGY_TRANSFER_MODE != MODE_AC_SINGLE_PHASE_CORE) && (CCS_ENERGY_TRANSFER_MODE != MODE_AC_THREE_PHASE_CORE))
  1320. unsigned char Statetmp;
  1321. float TotalTmpVolt;
  1322. #endif
  1323. struct ChargingInfoData *ccs;
  1324. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1325. if(CP_Detection_Pid == 0)
  1326. {
  1327. tmp = fork();
  1328. if(tmp > 0)
  1329. {
  1330. CP_Detection_Pid = tmp;
  1331. #if 1
  1332. unsigned char buf[64];
  1333. memset(buf, 0, sizeof(buf));
  1334. //sprintf((char*)buf, "renice -20 -p %d", tmp);
  1335. sprintf((char*)buf, "renice -10 -p %d", tmp);
  1336. system((char*)buf);
  1337. #endif
  1338. return;
  1339. }
  1340. }
  1341. while(1)
  1342. {
  1343. //ADC_Voltage = (1+RE62/RE63)*0.9 - (RE62/RE63)*Pilot_Voltage, RE62/RE63=0.06
  1344. //=>Pilot_Voltage=(0.954-ADC_Voltage)/0.06
  1345. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  1346. {
  1347. ccs->CpVoltage = ShmInternalComm->AC_CpPositiveVoltage;
  1348. ccs->CpState = ShmInternalComm->AC_CpPresentState;
  1349. }
  1350. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  1351. {
  1352. TotalTmpVolt = ReadAdcVolt(3);
  1353. ccs->CpVoltage = TotalTmpVolt;
  1354. if (ccs->CpVoltage_pre != ccs->CpVoltage)
  1355. {
  1356. ccs->CpVoltage_pre = ccs->CpVoltage;
  1357. }
  1358. //printf("TotalTmpVolt = %.02f\n", TotalTmpVolt);
  1359. //If CP voltage is higer than 13.5V
  1360. if(TotalTmpVolt >= 13.5)
  1361. {
  1362. //Pilot Error
  1363. if((ccs->CpState == 1) &&
  1364. (TotalTmpVolt < 13.75))
  1365. {
  1366. //V_cp = 13.5 ~ 13.75
  1367. Statetmp = 1;
  1368. }
  1369. else
  1370. {
  1371. //V_cp = 13.5 ~
  1372. Statetmp = 8;
  1373. }
  1374. }
  1375. else if((TotalTmpVolt >= 10.5) && (TotalTmpVolt < 13.5)) //V_cp = 10.5 ~ 13.5
  1376. {
  1377. //State A (12V)
  1378. if((ccs->CpState >= 2) &&
  1379. (ccs->CpState <= 3) &&
  1380. (TotalTmpVolt < 10.75))
  1381. {
  1382. if((ccs->CpDuty >= 5) &&
  1383. (ccs->CpDuty < 100))
  1384. {
  1385. Statetmp = 3;
  1386. }
  1387. else
  1388. {
  1389. Statetmp = 2;
  1390. }
  1391. }
  1392. else if((ccs->CpState == 8) &&
  1393. (TotalTmpVolt >= 13.25))
  1394. {
  1395. Statetmp = 8;
  1396. }
  1397. else
  1398. {
  1399. Statetmp = 1;
  1400. }
  1401. }
  1402. else if((TotalTmpVolt >= 7.5) && (TotalTmpVolt < 10.5))
  1403. {
  1404. //State B (9V)
  1405. if((ccs->CpState == 4) && (TotalTmpVolt < 7.75))
  1406. {
  1407. Statetmp = 4;
  1408. }
  1409. else if((ccs->CpState == 1) && (TotalTmpVolt >= 10.25))
  1410. {
  1411. Statetmp = 1;
  1412. }
  1413. else
  1414. {
  1415. if((ccs->CpDuty >= 5) && (ccs->CpDuty < 100))
  1416. {
  1417. Statetmp = 3;
  1418. }
  1419. else
  1420. {
  1421. Statetmp = 2;
  1422. }
  1423. }
  1424. }
  1425. else if((TotalTmpVolt >= 4.5) && (TotalTmpVolt < 7.5))
  1426. {
  1427. //State C (6V)
  1428. if((ccs->CpState == 5) && (TotalTmpVolt < 4.75))
  1429. {
  1430. Statetmp = 5;
  1431. }
  1432. else if((ccs->CpState >= 2) && (ccs->CpState <= 3) && (TotalTmpVolt >= 7.25))
  1433. {
  1434. if((ccs->CpDuty >= 5)&&(ccs->CpDuty < 100))
  1435. {
  1436. Statetmp = 3;
  1437. }
  1438. else
  1439. {
  1440. Statetmp = 2;
  1441. }
  1442. }
  1443. else
  1444. {
  1445. Statetmp = 4;
  1446. }
  1447. }
  1448. else if((TotalTmpVolt >= 1.5) && (TotalTmpVolt < 4.5))
  1449. {
  1450. //State D (3V)
  1451. if((ccs->CpState == 6)&&(TotalTmpVolt < 1.75))
  1452. {
  1453. Statetmp = 6;
  1454. }
  1455. else if((ccs->CpState == 4)&&(TotalTmpVolt >= 4.25))
  1456. {
  1457. Statetmp = 4;
  1458. }
  1459. else
  1460. {
  1461. Statetmp = 5;
  1462. }
  1463. }
  1464. else if((TotalTmpVolt >= -1.5) && (TotalTmpVolt < 1.5)) //V_cp = -1.5V ~ 1.5V
  1465. {
  1466. //State E (0V)
  1467. if((ccs->CpState == 8) &&
  1468. (TotalTmpVolt < -1.25))
  1469. {
  1470. Statetmp = 8;
  1471. }
  1472. else if((ccs->CpState == 5) &&
  1473. (TotalTmpVolt >= 1.25))
  1474. {
  1475. Statetmp = 5;
  1476. }
  1477. else
  1478. {
  1479. Statetmp = 6;
  1480. }
  1481. }
  1482. else if((TotalTmpVolt >= -13.5) && (TotalTmpVolt < -10.5)) //V_cp = -10.5V ~ -13.5V
  1483. {
  1484. //State F (-12V)
  1485. if((ccs->CpState == 8) &&
  1486. (TotalTmpVolt >= -10.75))
  1487. {
  1488. Statetmp = 8;
  1489. }
  1490. else
  1491. {
  1492. Statetmp = 7;
  1493. }
  1494. }
  1495. else
  1496. {
  1497. //null
  1498. }
  1499. ccs->CpState = Statetmp;
  1500. }
  1501. #endif
  1502. Check_CP_State_Update_fork1(ccs);
  1503. Check_CP_State_Error_fork1(ccs);
  1504. //Updating Plugin status
  1505. #if (PP_PROTECTION_MECHANISM == ENABLE)
  1506. if((ccs->CpState >= 2 ) && (ccs->CpState <= 5 ) && (ccs->PpVoltage >= 1.0)) //PP >= 1.0V
  1507. #else
  1508. if((ccs->CpState >= 2 ) && (ccs->CpState <= 5 ))
  1509. #endif
  1510. {
  1511. ccs->ConnectorPlugIn_new = TRUE;
  1512. }
  1513. else
  1514. {
  1515. ccs->ConnectorPlugIn_new = FALSE;
  1516. OutputCpPwmDuty(100);
  1517. }
  1518. Check_Plugin_Status_Update_fork1(ccs);
  1519. //ftime(&EndTime);
  1520. //DEBUG_PRINTF_EVCOMM_DETAIL("duration:%.02lf\n", DiffTimeb(StartTime, EndTime));
  1521. usleep(1000);
  1522. }//while
  1523. }
  1524. /*===========================================================================
  1525. FUNCTION: PP_Detection
  1526. DESCRIPTION:
  1527. 0. SeccComm fork3
  1528. 1. In order to detect CP in efficient response time, we creat an independent
  1529. thead for this procedure.
  1530. 2. The priority of this thread is set as the same as other tasks.
  1531. PRE-CONDITION:
  1532. INPUT:
  1533. OUTPUT:
  1534. GLOBAL VARIABLES:
  1535. =============================================================================*/
  1536. void PP_Detection()
  1537. {
  1538. pid_t tmp = 0;
  1539. // struct timeb StartTime, EndTime;
  1540. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED))
  1541. //unsigned char Statetmp;
  1542. #endif
  1543. float TotalTmpVolt;
  1544. if(PP_Detection_Pid == 0)
  1545. {
  1546. tmp = fork();
  1547. if(tmp > 0)
  1548. {
  1549. PP_Detection_Pid = tmp;
  1550. #if 0
  1551. unsigned char buf[64];
  1552. memset(buf, 0, sizeof(buf));
  1553. sprintf((char*)buf, "renice -20 -p %d", tmp);
  1554. system(buf);
  1555. #endif
  1556. return;
  1557. }
  1558. }
  1559. while(1)
  1560. {
  1561. TotalTmpVolt = ReadAdcVolt_PP_fork3();
  1562. EVCOMM_SYS_INFO.PpVoltage = TotalTmpVolt;
  1563. if (EVCOMM_SYS_INFO.PpVoltage_pre != EVCOMM_SYS_INFO.PpVoltage)
  1564. {
  1565. if ((EVCOMM_SYS_INFO.PpVoltage_pre < 0.5 && EVCOMM_SYS_INFO.PpVoltage >= 1.0) ||
  1566. (EVCOMM_SYS_INFO.PpVoltage_pre >= 1.0 && EVCOMM_SYS_INFO.PpVoltage < 0.5))
  1567. {
  1568. sprintf((char*)buf_log_evcomm_fork3,
  1569. "[fork3]PP(%.2f >> %.2fV)",
  1570. EVCOMM_SYS_INFO.PpVoltage_pre,
  1571. EVCOMM_SYS_INFO.PpVoltage
  1572. );
  1573. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork3);
  1574. EVCOMM_SYS_INFO.PpVoltage_pre = EVCOMM_SYS_INFO.PpVoltage;
  1575. }
  1576. }
  1577. #if 0
  1578. sleep(5);
  1579. printf("V_pp = %.02f\n", TotalTmpVolt);
  1580. #endif
  1581. usleep(1000);
  1582. }//while
  1583. }
  1584. /*===========================================================================
  1585. FUNCTION: Error_Monitor
  1586. DESCRIPTION:
  1587. 1. This function only works in SeccComm fork2.
  1588. PRE-CONDITION:
  1589. INPUT:
  1590. OUTPUT:
  1591. GLOBAL VARIABLES:
  1592. =============================================================================*/
  1593. void Error_Monitor()
  1594. {
  1595. pid_t tmp = 0;
  1596. double time_diff = 0;
  1597. unsigned char status = 0;
  1598. struct ChargingInfoData *ccs;
  1599. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1600. if(Error_Monitor_Pid == 0)
  1601. {
  1602. tmp = fork(); //SeccComm fork2
  1603. if(tmp > 0)
  1604. {
  1605. Error_Monitor_Pid = tmp;
  1606. #if 0
  1607. unsigned char buf[64];
  1608. memset(buf, 0, sizeof(buf));
  1609. sprintf((char*)buf, "renice -20 -p %d", tmp);
  1610. system(buf);
  1611. #endif
  1612. return;
  1613. }
  1614. }
  1615. while(1)
  1616. {
  1617. //Step 0
  1618. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  1619. {
  1620. //If the End_Process is in processing, disable Error_Monitor.
  1621. continue;
  1622. }
  1623. //Step1 1: Check and Response to Plugin Status
  1624. if(CheckConnectorPlugIn() == FALSE)
  1625. {
  1626. status = Check_V2G_Flow_Status();
  1627. if (status > IDLE &&
  1628. status < Performance_Timeout &&
  1629. status != CM_SET_KEY_REQ &&
  1630. status != CM_SET_KEY_CNF &&
  1631. EVCOMM_SYS_INFO.End_Process_inused == FALSE)
  1632. {
  1633. #if (CP_PROTECTION_MECHANISM == ENABLE)
  1634. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2] Emergency Stop (due to Connector is plugged out during communication.)");
  1635. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]Plug out Error => End_Process");
  1636. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1637. //CCS_SECC_CP_State_Error (023889)
  1638. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1639. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1640. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1641. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1642. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1643. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1644. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1645. End_Process();
  1646. #else
  1647. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "CP_PROTECTION_MECHANISM is disabled. Emergency Stop: skipped" );
  1648. #endif
  1649. }
  1650. }
  1651. //Step 2: Check for V2G_SECC_Sequence_Timeout
  1652. //#if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  1653. #if 1
  1654. status = Check_V2G_Flow_Status();
  1655. if (status >= SupportedAppProtocolRequest &&
  1656. status < SessionStopRequest)
  1657. {
  1658. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_End);
  1659. time_diff = DiffTimeb_fork2_Error_Monitor(EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start, EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_End);
  1660. if(time_diff > V2G_SECC_Sequence_Timeout) //60s
  1661. {
  1662. sprintf((char*)buf_log_evcomm_fork2,
  1663. "[fork2]V2G_SECC_Sequence_Timeout in state %d - (%.02lf of %d ms)\n",
  1664. status,
  1665. time_diff,
  1666. V2G_SECC_Sequence_Timeout);
  1667. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1668. Update_V2G_Flow_Status(Sequence_Timeout);
  1669. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1670. //CCS_SECC_TIMEOUT_V2G_Sequence_Time (023844)
  1671. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1672. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1673. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1674. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1675. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  1676. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  1677. End_Process();
  1678. break;
  1679. }
  1680. else if (time_diff > 4000) //4s
  1681. {
  1682. //Check for CSU command of "Stop by EVSE"
  1683. if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_Shutdown)
  1684. {
  1685. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error_Monitor]EVSE_Shutdown");
  1686. Update_V2G_Flow_Status(Other_Fault);
  1687. }
  1688. else if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_EmergencyShutdown)
  1689. {
  1690. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error_Monitor]EVSE_EmergencyShutdown");
  1691. Update_V2G_Flow_Status(Other_Fault);
  1692. }
  1693. else if (ShmInternalComm->ChargingPermission == FALSE)
  1694. {
  1695. if (status >= ChargeParameterDiscoveryRequest) //&& status < SessionStopRequest
  1696. {
  1697. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error_Monitor]ChargingPermission = FALSE");
  1698. Update_V2G_Flow_Status(Other_Fault);
  1699. }
  1700. }
  1701. else
  1702. {
  1703. //null
  1704. }
  1705. }
  1706. else
  1707. {
  1708. //null
  1709. }
  1710. }
  1711. #endif
  1712. //Step 3: Check and Response to Error V2gFlowStatus
  1713. status = Check_V2G_Flow_Status();
  1714. if (status == Performance_Timeout ||
  1715. status == Sequence_Timeout ||
  1716. status == Other_Fault)
  1717. {
  1718. sprintf((char*)buf_log_evcomm_fork2,
  1719. "[fork2]Timeout or Fault State(%d) => End_Process",
  1720. status);
  1721. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1722. //Normal Stop
  1723. //alarm and duty to 100%
  1724. //OutputCpPwmDuty(100);
  1725. //SwitchCpStateE(ENABLE); //Set CP duty as 100% and set CP state as E (0V).
  1726. //[Joseph/ To-Do] Set State E (0V) via GPIO setting
  1727. End_Process();
  1728. }
  1729. //Step 4: Check and Response to CP State Error
  1730. if(ccs->CpState_err == TRUE)
  1731. {
  1732. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]CP Error => End_Process");
  1733. Update_V2G_Flow_Status(Other_Fault);
  1734. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1735. //CCS_SECC_CP_State_Error (023889)
  1736. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1737. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1738. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1739. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1740. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1741. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1742. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1743. End_Process();
  1744. }
  1745. //Step 5: Check and Response to Shutdown Commnad from CSU
  1746. if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_Shutdown ||
  1747. EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_EmergencyShutdown)
  1748. {
  1749. if (Check_V2G_Flow_Status() <= SLACC_SDP_TCP_Connection)
  1750. {
  1751. sprintf((char*)buf_log_evcomm_fork2,
  1752. "[fork2]EVSE Shutdown(%d) => End_Process",
  1753. EVCOMM_SYS_INFO.DC_EVSEStatus);
  1754. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1755. Update_V2G_Flow_Status(Other_Fault);
  1756. End_Process();
  1757. }
  1758. }
  1759. //Step 6: Check and Response to SessionStop
  1760. status = Check_V2G_Flow_Status();
  1761. if (status == SessionStopResponse)
  1762. {
  1763. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]SessionStopResponse => End_Process");
  1764. End_Process();
  1765. }
  1766. //Step 7: Check for ChargingPermission from TRUE to FALSE before V2G Messages
  1767. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  1768. if ((ShmInternalComm->ChargingPermission == FALSE) &&
  1769. (ShmInternalComm->ChargingPermission_pre >= 1) &&
  1770. (ccs->CpState >= 3) && (ccs->CpState <=5))
  1771. {
  1772. if (status >= CM_SLAC_PARM_REQ &&
  1773. status != CM_SET_KEY_REQ &&
  1774. status != CM_SET_KEY_CNF &&
  1775. status <= SLACC_SDP_TCP_Connection)
  1776. {
  1777. sprintf((char*)buf_log_evcomm_fork2,
  1778. "[fork2]Permission OFF before V2G msg(%d) => End_Process",
  1779. ShmInternalComm->ChargingPermission);
  1780. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1781. Update_V2G_Flow_Status(Other_Fault);
  1782. End_Process();
  1783. }
  1784. }
  1785. //Step 8: DC OVP Protection
  1786. if (CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED &&
  1787. status > CableCheckResponse &&
  1788. status <= SessionStopRequest &&
  1789. status != ChargingStatusRequest &&
  1790. status != ChargingStatusResponse &&
  1791. EVCOMM_SYS_INFO.EvBatteryMaxVoltage != 0)
  1792. {
  1793. //Part A: OVP Protection
  1794. if (EVCOMM_SYS_INFO.PresentChargingVoltage >= (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02)) // 2%
  1795. {
  1796. sprintf((char*)buf_log_evcomm_fork2,
  1797. "[fork2][Error]OVP => End_Process (%.02f > %.02f)",
  1798. EVCOMM_SYS_INFO.PresentChargingVoltage,
  1799. (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02));
  1800. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1801. Update_V2G_Flow_Status(Other_Fault);
  1802. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1803. //System CCS output OVP (012219)
  1804. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1805. ShmStatusCodeData->PresentStatusCode[0][1] = 1;
  1806. ShmStatusCodeData->PresentStatusCode[0][2] = 2;
  1807. ShmStatusCodeData->PresentStatusCode[0][3] = 2;
  1808. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  1809. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1810. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1811. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1812. End_Process();
  1813. }
  1814. //Part B: Over Voltage Request Protection
  1815. if (EVCOMM_SYS_INFO.EvBatterytargetVoltage >= (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02))
  1816. {
  1817. sprintf((char*)buf_log_evcomm_fork2,
  1818. "[fork2]Over V Req => End_Process (%.02f > %.02f)",
  1819. EVCOMM_SYS_INFO.EvBatterytargetVoltage,
  1820. (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02)
  1821. );
  1822. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1823. Update_V2G_Flow_Status(Other_Fault);
  1824. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1825. //System CCS output OVP (012219)
  1826. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1827. ShmStatusCodeData->PresentStatusCode[0][1] = 1;
  1828. ShmStatusCodeData->PresentStatusCode[0][2] = 2;
  1829. ShmStatusCodeData->PresentStatusCode[0][3] = 2;
  1830. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  1831. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1832. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1833. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1834. End_Process();
  1835. }
  1836. }
  1837. //Step 9: Check 60V
  1838. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  1839. if (CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED &&
  1840. ShmInternalComm->ChargingPermission >= 1 &&
  1841. status < CableCheckRequest)
  1842. {
  1843. if (EVCOMM_SYS_INFO.PresentChargingVoltage >= 60) //60V
  1844. {
  1845. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]DC Output Voltage is over 60V => End_Process");
  1846. Update_V2G_Flow_Status(Other_Fault);
  1847. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1848. //CCS_SECC_Unexpected_60V_Before_Charing_Error (023890)
  1849. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1850. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1851. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1852. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1853. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  1854. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  1855. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1856. //Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1857. End_Process();
  1858. }
  1859. }
  1860. //Step 10: Check if the connector is unplug from plugin
  1861. if (EVCOMM_SYS_INFO.ConnectorPlugIn_pre == TRUE && EVCOMM_SYS_INFO.ConnectorPlugIn == FALSE)
  1862. {
  1863. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]Unplug Reset => End_Process");
  1864. Update_V2G_Flow_Status(Other_Fault);
  1865. End_Process();
  1866. }
  1867. usleep(1000);
  1868. }//while
  1869. }
  1870. /*===========================================================================
  1871. FUNCTION: SendSetKey
  1872. DESCRIPTION:
  1873. PRE-CONDITION:
  1874. INPUT:
  1875. OUTPUT:
  1876. GLOBAL VARIABLES:
  1877. =============================================================================*/
  1878. int SendSetKey()
  1879. {
  1880. int i = 0;
  1881. unsigned char nRandValue = 0x0;
  1882. unsigned char ConstString[16] = "PhihongKey000000";
  1883. memset(&SendMmePacket,0,sizeof(struct MmeHeader));
  1884. memcpy(SendMmePacket.ODA,QcaMac,6);
  1885. memcpy(SendMmePacket.OSA,CsuMac,6);
  1886. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  1887. SendMmePacket.MMV = 0x01;
  1888. SendMmePacket.MMTYPE = MMTYPE_CM_SET_KEY_REQ;
  1889. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  1890. SendMmePacketSize = 0;
  1891. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x01;//Fixed value (0x01) to indicate ��NMK��
  1892. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,4);//My Nonce, Fixed value(0x00000000), encrypted payload not used
  1893. SendMmePacketSize+=4;
  1894. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,4);//Your Nonce, Fixed value(0x00000000), encrypted payload not used
  1895. SendMmePacketSize+=4;
  1896. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x04;//PID, Fixed value (0x04) to indicate ��HLE protocol��
  1897. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,2);//PRN, Fixed value(0x00), encrypted payload not used
  1898. SendMmePacketSize+=2;
  1899. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00;//PMN, Fixed value(0x00) encrypted payload not used
  1900. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x01;//CCo Capablility
  1901. srand(time(NULL));
  1902. for (i = 10; i < 16; i++)
  1903. {
  1904. nRandValue = (rand()%62) + 1;
  1905. if((nRandValue>=0)&&(nRandValue<=9)) // 0 ~ 9
  1906. {
  1907. ConstString[i]= nRandValue + 0x30;
  1908. }
  1909. else if((nRandValue>=10)&&(nRandValue<=35)) // A ~ Z
  1910. {
  1911. ConstString[i]= nRandValue -10 + 0x41;
  1912. }
  1913. else if((nRandValue>=36)&&(nRandValue<=61)) // a ~ z
  1914. {
  1915. ConstString[i]= nRandValue -37 + 0x61;
  1916. }
  1917. else
  1918. {
  1919. ConstString[i]= 0x30;
  1920. }
  1921. }
  1922. memset(NewNmkKey,0,sizeof(NewNmkKey));
  1923. memset(Nid,0,sizeof(Nid));
  1924. HPAVKeyNMK(NewNmkKey, (char*)ConstString);
  1925. HPAVKeyNID(Nid,NewNmkKey,DEFAULT_LEVEL);
  1926. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,Nid,sizeof(Nid)); //NID, 54 LSBs contain the NID 2 MSBs = 0b00
  1927. SendMmePacketSize+=sizeof(Nid);
  1928. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x01;//NewEKS,Fixed value (0x01)to indicate ��NMK��
  1929. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,NewNmkKey,sizeof(NewNmkKey));//NewKey
  1930. SendMmePacketSize += sizeof(NewNmkKey);
  1931. SendMmePacketSize += 19; //the size before MMENTRY
  1932. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[QCA7K][Tx]CM_SET_KEY_REQ");
  1933. i = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  1934. Update_V2G_Flow_Status(CM_SET_KEY_REQ);
  1935. DEBUG_PRINTF_EVCOMM_DETAIL("SendSetKey: send size =%d\n",i);
  1936. return 0;
  1937. }
  1938. /*===========================================================================
  1939. FUNCTION: GetQca7kMac
  1940. DESCRIPTION:
  1941. PRE-CONDITION:
  1942. INPUT:
  1943. OUTPUT:
  1944. GLOBAL VARIABLES:
  1945. =============================================================================*/
  1946. int GetQca7kMac()
  1947. {
  1948. struct QcaVendorMmeHeader SendPacket;
  1949. memset(&SendPacket, 0, sizeof(struct QcaVendorMmeHeader));
  1950. memset(SendPacket.ODA, 0xFF, 6); //broadcast
  1951. memcpy(SendPacket.OSA, CsuMac, 6);
  1952. SendPacket.MTYPE = htons(EtherType_HomePlug);
  1953. SendPacket.MMV = 0x00;
  1954. SendPacket.MMTYPE = MMTYPE_VENDOR_VS_NW_INFO;
  1955. SendPacket.OUI[0] = 0x00;
  1956. SendPacket.OUI[1] = 0xB0;
  1957. SendPacket.OUI[2] = 0x52;
  1958. DEBUG_PRINTF_EVCOMM_DETAIL("GetQca7kMac: send size =%d\n", sendto(RawSock, &SendPacket, 20, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll)));
  1959. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Req for QCA7K MacAddr");
  1960. return 0;
  1961. }
  1962. /*===========================================================================
  1963. FUNCTION: Array_Check_All_Zero
  1964. DESCRIPTION:
  1965. PRE-CONDITION:
  1966. INPUT:
  1967. OUTPUT:
  1968. result:
  1969. (1) TRUE: all zero
  1970. (2) FALSE: not all zero
  1971. GLOBAL VARIABLES:
  1972. =============================================================================*/
  1973. int Array_Check_All_Zero(unsigned char *ptr, int size)
  1974. {
  1975. int result = TRUE;
  1976. int i = 0;
  1977. for (i = 0; i < size; i++)
  1978. {
  1979. if (ptr[i] != 0)
  1980. {
  1981. result = FALSE;
  1982. break;
  1983. }
  1984. }
  1985. return result;
  1986. }
  1987. /*===========================================================================
  1988. FUNCTION: Array_Compare_Identity
  1989. DESCRIPTION:
  1990. PRE-CONDITION:
  1991. INPUT:
  1992. OUTPUT:
  1993. result = FALSE (not identical)
  1994. result = TRUE (identical)
  1995. GLOBAL VARIABLES:
  1996. =============================================================================*/
  1997. int Array_Compare_Identity(unsigned char *ptrA, unsigned char *ptrB, int size)
  1998. {
  1999. int result = TRUE;
  2000. int i = 0;
  2001. for (i = 0; i < size; i++)
  2002. {
  2003. if (ptrA[i] != ptrB[i])
  2004. {
  2005. result = FALSE;
  2006. #if 0
  2007. sprintf((char*)buf_log_evcomm,
  2008. "[Array_Compare_Identity]%02X%02X%02X%02X%02X%02X,%02X%02X%02X%02X%02X%02X(%d)\n",
  2009. ptrA[0], ptrA[1], ptrA[2], ptrA[3], ptrA[4], ptrA[5],
  2010. ptrB[0], ptrB[1], ptrB[2], ptrB[3], ptrB[4], ptrB[5],
  2011. result);
  2012. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2013. #endif
  2014. break;
  2015. }
  2016. }
  2017. return result;
  2018. }
  2019. /*===========================================================================
  2020. FUNCTION: SLAC_DB_Search_EvMac_idx
  2021. DESCRIPTION:
  2022. PRE-CONDITION:
  2023. INPUT:
  2024. OUTPUT:
  2025. idx = -1 (not found)
  2026. GLOBAL VARIABLES:
  2027. =============================================================================*/
  2028. int SLAC_DB_Search_EvMac_idx(struct EVCC_SLAC_DATA_TYPE *evcc, unsigned char *EvMac_in)
  2029. {
  2030. int idx = -1;
  2031. int i = 0;
  2032. if (evcc->arrayLen == 0)
  2033. {
  2034. //printf("[SLAC_DB_Search_EvMac_idx]arrayLen is empty (%d)\n", evcc->arrayLen);
  2035. //no need to search
  2036. }
  2037. else if (evcc->arrayLen > EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE)
  2038. {
  2039. //error
  2040. sprintf((char*)buf_log_evcomm,
  2041. "[ERROR][SLAC_DB_Search_EvMac_idx]DB length(%d) > %d",
  2042. evcc->arrayLen,
  2043. EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE);
  2044. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2045. evcc->arrayLen = 0; //reset
  2046. }
  2047. else
  2048. {
  2049. //start searching
  2050. for (i = 0; i < evcc->arrayLen; i++)
  2051. {
  2052. //printf("[SLAC_DB_Search_EvMac_idx]checking DB[%d]...\n", i);
  2053. if (Array_Compare_Identity(evcc->array[i].EvMac, EvMac_in, SLAC_EVMAC_LENGTH) == TRUE)
  2054. {
  2055. //printf("[SLAC_DB_Search_EvMac_idx]identical at DB[%d]...\n", i);
  2056. idx = i;
  2057. break;
  2058. }
  2059. }
  2060. }
  2061. //printf("[SLAC_DB_Search_EvMac_idx]return = %d\n", idx);
  2062. return idx;
  2063. }
  2064. /*===========================================================================
  2065. FUNCTION: SLAC_DB_Check_EvMac_RunID_Matching
  2066. DESCRIPTION:
  2067. PRE-CONDITION:
  2068. INPUT:
  2069. OUTPUT:
  2070. res = FALSE (unmatched)
  2071. res = TRUE (matched)
  2072. GLOBAL VARIABLES:
  2073. =============================================================================*/
  2074. int SLAC_DB_Check_EvMac_RunID_Matching(struct EVCC_SLAC_DATA_TYPE *evcc, unsigned char *EvMac_in, unsigned char *RunID_in)
  2075. {
  2076. int res = FALSE;
  2077. int idx = -1;
  2078. idx = SLAC_DB_Search_EvMac_idx(evcc, EvMac_in);
  2079. if (idx >= 0)
  2080. {
  2081. res = Array_Compare_Identity(evcc->array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH);
  2082. }
  2083. else
  2084. {
  2085. //not found the EvMac data in DB
  2086. res = FALSE;
  2087. }
  2088. return res;
  2089. }
  2090. /*===========================================================================
  2091. FUNCTION: SLAC_DB_Add
  2092. DESCRIPTION:
  2093. 1. Saving each CM_SLAC_PARM_REQ data into EVCC database if its
  2094. EvMac and RunID data are different, respectively.
  2095. PRE-CONDITION:
  2096. INPUT:
  2097. OUTPUT:
  2098. idx = saved index (must be a positive value)
  2099. GLOBAL VARIABLES:
  2100. =============================================================================*/
  2101. int SLAC_DB_Add(unsigned char *EvMac_in, unsigned char *RunID_in)
  2102. {
  2103. int idx = -1;
  2104. //Search if this EvMac and RunID already exists
  2105. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2106. if (idx < 0) //not exist, yet.
  2107. {
  2108. if (SLAC_INFO.arrayLen < EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE) //20
  2109. {
  2110. sprintf((char*)buf_log_evcomm,
  2111. "[SLAC_DB_Add]data does not exist => added to %d-th",
  2112. SLAC_INFO.arrayLen);
  2113. DEBUG_PRINTF_EVCOMM_DETAIL("%s\n", buf_log_evcomm);
  2114. if (SLAC_INFO.arrayLen >= 0)
  2115. {
  2116. memset(&SLAC_INFO.array[SLAC_INFO.arrayLen], 0, sizeof(struct EVCC_SLAC_DATA_ARRAY_TYPE));
  2117. memcpy(SLAC_INFO.array[SLAC_INFO.arrayLen].EvMac, EvMac_in, SLAC_EVMAC_LENGTH);
  2118. memcpy(SLAC_INFO.array[SLAC_INFO.arrayLen].RunID, RunID_in, SLAC_RUNID_LENGTH);
  2119. idx = SLAC_INFO.arrayLen;
  2120. SLAC_INFO.arrayLen++;
  2121. }
  2122. else
  2123. {
  2124. sprintf((char*)buf_log_evcomm,
  2125. "[ERROR][SLAC_DB_Add]arrayLen: unexpected(%d)",
  2126. SLAC_INFO.arrayLen);
  2127. DEBUG_PRINTF_EVCOMM_DETAIL("%s\n", buf_log_evcomm);
  2128. SLAC_INFO.arrayLen = 0;
  2129. }
  2130. }
  2131. else
  2132. {
  2133. //DB is full
  2134. sprintf((char*)buf_log_evcomm,
  2135. "[SLAC_DB_Add]DB is full(%d) => bypass",
  2136. SLAC_INFO.arrayLen);
  2137. DEBUG_PRINTF_EVCOMM_DETAIL("%s\n", buf_log_evcomm);
  2138. }
  2139. }
  2140. else
  2141. {
  2142. #if 0
  2143. sprintf((char*)buf_log_evcomm,
  2144. "[SLAC_DB_Add]EvMac: existed (%d)",
  2145. idx);
  2146. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2147. #endif
  2148. //Check RunID
  2149. if (Array_Compare_Identity(SLAC_INFO.array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH) == TRUE)
  2150. {
  2151. //RunID is the same
  2152. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC_DB_Add]RunID: same");
  2153. }
  2154. else
  2155. {
  2156. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC_DB_Add]RunID: diff");
  2157. }
  2158. //Reset all corresponding parameters
  2159. #if 0
  2160. sprintf((char*)buf_log_evcomm,
  2161. "[SLAC_DB_Add]EvMac: reset para(%d)",
  2162. idx);
  2163. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2164. #endif
  2165. memset(&SLAC_INFO.array[idx], 0, sizeof(struct EVCC_SLAC_DATA_ARRAY_TYPE));
  2166. memcpy(SLAC_INFO.array[idx].EvMac, EvMac_in, SLAC_EVMAC_LENGTH);
  2167. memcpy(SLAC_INFO.array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH);
  2168. }
  2169. return idx;
  2170. }
  2171. /*===========================================================================
  2172. FUNCTION: SLAC_DB_Reset
  2173. DESCRIPTION:
  2174. PRE-CONDITION:
  2175. INPUT:
  2176. OUTPUT:
  2177. GLOBAL VARIABLES:
  2178. =============================================================================*/
  2179. int SLAC_DB_Reset()
  2180. {
  2181. memset(&SLAC_INFO, 0, sizeof(struct EVCC_SLAC_DATA_TYPE));
  2182. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC_DB_Reset]DONE");
  2183. return 0;
  2184. }
  2185. /*===========================================================================
  2186. FUNCTION: MmeProcess
  2187. DESCRIPTION:
  2188. PRE-CONDITION:
  2189. INPUT:
  2190. OUTPUT:
  2191. GLOBAL VARIABLES:
  2192. =============================================================================*/
  2193. int MmeProcess(unsigned char *Buffer, int DataLength)
  2194. {
  2195. //struct ethhdr *EthPacket;
  2196. struct MmeHeader *MmePacket;
  2197. static unsigned char counter;
  2198. unsigned char state = 0;
  2199. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2200. unsigned char tmpBuf[2048]={0};
  2201. #endif
  2202. int Rtn = 0;
  2203. #if(NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2204. int idx = 0;
  2205. unsigned char *EvMac_in;
  2206. unsigned char *RunID_in;
  2207. #endif
  2208. MmePacket = (struct MmeHeader *)Buffer;
  2209. state = Check_V2G_Flow_Status();
  2210. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2211. {
  2212. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  2213. DEBUG_PRINTF_EVCOMM_DETAIL("******* Received MME Packet *******\n");
  2214. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  2215. DEBUG_PRINTF_EVCOMM_DETAIL("DataLength=%d\n",DataLength);
  2216. DEBUG_PRINTF_EVCOMM_DETAIL("ODA: %02x:%02x:%02x:%02x:%02x:%02x\n", //Destination MAC Address
  2217. MmePacket->ODA[0], MmePacket->ODA[1],
  2218. MmePacket->ODA[2], MmePacket->ODA[3],
  2219. MmePacket->ODA[4], MmePacket->ODA[5]);
  2220. DEBUG_PRINTF_EVCOMM_DETAIL("OSA: %02x:%02x:%02x:%02x:%02x:%02x\n", //Source MAC Address (EV MAC)
  2221. MmePacket->OSA[0], MmePacket->OSA[1],
  2222. MmePacket->OSA[2], MmePacket->OSA[3],
  2223. MmePacket->OSA[4], MmePacket->OSA[5]);
  2224. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(MmePacket->MTYPE));
  2225. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", MmePacket->MMV);
  2226. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", MmePacket->MMTYPE);
  2227. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n",
  2228. MmePacket->FMI[0],MmePacket->FMI[1]);
  2229. }
  2230. #endif
  2231. #if (SLAC_FIRST_RESPONSE_METHOD == SET_NO_PWM_IF_NOT_GET_PERMISSION)
  2232. {
  2233. //Check CP as 5%
  2234. if (EVCOMM_SYS_INFO.CpState != 3 &&
  2235. EVCOMM_SYS_INFO.CpState != 4 &&
  2236. MmePacket->MMTYPE != MMTYPE_CM_SET_KEY_CNF &&
  2237. MmePacket->MMTYPE != MMTYPE_VENDOR_VS_HOST_ACTION &&
  2238. MmePacket->MMTYPE != MMTYPE_VENDOR_ATTEN_CHAR &&
  2239. MmePacket->MMTYPE != MMTYPE_VENDOR_VS_NW_INFO_CNF
  2240. )
  2241. {
  2242. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC]ignored(wrong CP state)");
  2243. return 0;
  2244. }
  2245. }
  2246. #endif
  2247. //[To-Do] Adding a pre-check filter mechanism for Source and Destination MAC Address
  2248. //check if the destination address is broadcast or EVSE itself, it can be ignore if not belong to itself.
  2249. switch(MmePacket->MMTYPE)
  2250. {
  2251. case MMTYPE_CM_SET_KEY_CNF:
  2252. {
  2253. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SET_KEY_CNF ---\n");
  2254. DEBUG_PRINTF_EVCOMM_DETAIL("Result: 0x%x\n", MmePacket->MMENTRY[0]);
  2255. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[QCA7K][Rx]CM_SET_KEY_CNF (SetKey: DONE)");
  2256. Update_V2G_Flow_Status(CM_SET_KEY_CNF);
  2257. break;
  2258. }
  2259. case MMTYPE_CM_SLAC_PARM_REQ:
  2260. {
  2261. //Check QCA7000 status
  2262. if (EVCOMM_SYS_INFO.QCA7K_SetKeyDone == FALSE)
  2263. {
  2264. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx]CM_SLAC_PARM_REQ: ignored(QCA7K init...)");
  2265. break;
  2266. }
  2267. //Check error state
  2268. state = Check_V2G_Flow_Status();
  2269. if (state == Performance_Timeout || //253
  2270. state == Sequence_Timeout || //254
  2271. state == Other_Fault) //255
  2272. {
  2273. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx]CM_SLAC_PARM_REQ: ignored(in error state)");
  2274. break;
  2275. }
  2276. //Printing EV MAC Address
  2277. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx]CM_SLAC_PARM_REQ");
  2278. //Avoid Coupled SLAC_PARM_REQ
  2279. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2280. {
  2281. #if 0
  2282. sprintf((char*)buf_log_evcomm,
  2283. "[WARNING][SLAC][Rx]CM_SLAC_PARM_REQ: ignored(coupled SLAC,%d)",
  2284. CheckConnectorPlugIn());
  2285. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2286. #endif
  2287. break;
  2288. }
  2289. //[TC_SECC_VTB_CmSlacParm_003] SECURITY_TYPE needs to be 0x00 (no security)
  2290. if (MmePacket->MMENTRY[1] != 0)
  2291. {
  2292. sprintf((char*)buf_log_evcomm,
  2293. "[WARNING][SLAC][Rx]CM_SLAC_PARM_REQ: ignored(invalid SECURITY_TYPE,%d)", //Source MAC Address (EV MAC)
  2294. MmePacket->MMENTRY[1]);
  2295. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2296. break;
  2297. }
  2298. //=================== Legal CM_SLAC_PARM_REQ Zone =================
  2299. Update_V2G_Flow_Status(CM_SLAC_PARM_REQ);
  2300. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_RX_CM_SLAC_PARM_REQ)
  2301. {
  2302. //Once receiving CM_SLAC_PARM_REQ, set PWM as 5%
  2303. SwitchCpStateE(DISABLE);
  2304. OutputCpPwmDuty(5);
  2305. }
  2306. #endif
  2307. //Checking if this SLAC Req comes before 5% PWM (for 15118)
  2308. if(ShmInternalComm->ChargingPermission == FALSE)
  2309. {
  2310. //Sniffer_Tcpdump(ENABLE);
  2311. sprintf((char*)buf_log_evcomm, "Check Permission: %d (SLAC first => START)", ShmInternalComm->ChargingPermission);
  2312. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2313. }
  2314. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2315. {
  2316. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SLAC_PARM_REQ ---\n");
  2317. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2318. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2319. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2320. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2321. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2322. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7],
  2323. MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2324. DEBUG_PRINTF_EVCOMM_DETAIL("CipherSuiteSetSize: 0x%x\n", MmePacket->MMENTRY[10]);
  2325. DEBUG_PRINTF_EVCOMM_DETAIL("CipherSuite [1]: 0x%x,0x%x\n",
  2326. MmePacket->MMENTRY[11], MmePacket->MMENTRY[12]);
  2327. }
  2328. #endif
  2329. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2330. {
  2331. //Saving each CM_SLAC_PARM_REQ data into EVCC database
  2332. EvMac_in = &MmePacket->OSA[0];
  2333. RunID_in = &MmePacket->MMENTRY[2];
  2334. idx = SLAC_DB_Add(EvMac_in, RunID_in); //(EvMac, RunID)
  2335. if (idx < 0)
  2336. {
  2337. sprintf((char*)buf_log_evcomm,
  2338. "[WARNING][CM_SLAC_PARM_REQ]DB is full or errors occour(%d) => ignore",
  2339. idx);
  2340. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2341. break;
  2342. }
  2343. //Select the 1st EV MAC address
  2344. if (SLAC_INFO.arrayLen == 1) //1st Req
  2345. {
  2346. #if 1
  2347. sprintf((char*)buf_log_evcomm,
  2348. "[SLAC][Rx]CM_SLAC_PARM_REQ[%d](%02X:%02X:%02X:%02X:%02X:%02X,%02X%02X%02X%02X%02X%02X%02X%02X):selected",
  2349. (idx + 1),
  2350. MmePacket->OSA[0], MmePacket->OSA[1], MmePacket->OSA[2], MmePacket->OSA[3], MmePacket->OSA[4], MmePacket->OSA[5],
  2351. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3], MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2352. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7], MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2353. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2354. #endif
  2355. /*
  2356. sprintf((char*)buf_log_evcomm,
  2357. "[SLAC][EVMAC][%d-th]%02X:%02X:%02X:%02X:%02X:%02X (selected)", //Source MAC Address (EV MAC)
  2358. SLAC_INFO.arrayLen,
  2359. MmePacket->OSA[0], MmePacket->OSA[1],
  2360. MmePacket->OSA[2], MmePacket->OSA[3],
  2361. MmePacket->OSA[4], MmePacket->OSA[5]);
  2362. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2363. sprintf((char*)buf_log_evcomm,
  2364. "[SLAC][RunID][%d-th]%02X%02X%02X%02X%02X%02X%02X%02X (selected)", //RunID (from EVCC)
  2365. SLAC_INFO.arrayLen,
  2366. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2367. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2368. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7],
  2369. MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2370. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2371. */
  2372. }
  2373. else //2nd Req
  2374. {
  2375. #if 1
  2376. sprintf((char*)buf_log_evcomm,
  2377. "[SLAC][Rx]CM_SLAC_PARM_REQ[%d](%02X:%02X:%02X:%02X:%02X:%02X,%02X%02X%02X%02X%02X%02X%02X%02X):not selected",
  2378. (idx + 1),
  2379. MmePacket->OSA[0], MmePacket->OSA[1], MmePacket->OSA[2], MmePacket->OSA[3], MmePacket->OSA[4], MmePacket->OSA[5],
  2380. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3], MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2381. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7], MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2382. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2383. #endif
  2384. /*
  2385. sprintf((char*)buf_log_evcomm,
  2386. "[SLAC][EVMAC][%d-th]%02X:%02X:%02X:%02X:%02X:%02X (not selected)", //Source MAC Address (EV MAC)
  2387. SLAC_INFO.arrayLen,
  2388. MmePacket->OSA[0], MmePacket->OSA[1],
  2389. MmePacket->OSA[2], MmePacket->OSA[3],
  2390. MmePacket->OSA[4], MmePacket->OSA[5]);
  2391. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2392. sprintf((char*)buf_log_evcomm,
  2393. "[SLAC][RunID][%d-th]%02X%02X%02X%02X%02X%02X%02X%02X (not selected)", //RunID (from EVCC)
  2394. SLAC_INFO.arrayLen,
  2395. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2396. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2397. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7],
  2398. MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2399. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2400. */
  2401. }
  2402. memcpy(EvMac, MmePacket->OSA, sizeof(EvMac));
  2403. memcpy(DestSocketAddress.sll_addr, MmePacket->OSA, SLAC_EVMAC_LENGTH);
  2404. memcpy(SlacRunId, MmePacket->MMENTRY + 2, SLAC_RUNID_LENGTH);
  2405. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  2406. memcpy(SendMmePacket.ODA, MmePacket->OSA, 6);
  2407. memcpy(SendMmePacket.OSA, CsuMac, 6);
  2408. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  2409. SendMmePacket.MMV = MmePacket->MMV;
  2410. SendMmePacket.MMTYPE = MMTYPE_CM_SLAC_PARM_CNF;
  2411. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  2412. SendMmePacketSize = 0;
  2413. memset(SendMmePacket.MMENTRY, 0xFF, 6); //M-SOUND_TARGET(6-byte:0xFFFFFFFFFFFF): Fixed value indicating that M-Sounds to be sent as Ethernet broadcast
  2414. SendMmePacketSize += 6;
  2415. 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
  2416. SendMmePacket.MMENTRY[SendMmePacketSize++] = 6; //Time_Out(0x06): unit = 100ms
  2417. 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
  2418. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, SLAC_INFO.array[idx].EvMac, 6); //FORWARDING_STA(MAC Address of the EV HLE)
  2419. SendMmePacketSize += 6;
  2420. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00): 0x00(PEV-EVSE Association), 0x01-0xFF(Reserved)
  2421. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00): 0x00(No Security), 0x01(Public Key Signature), 0x02-0xFF(Reserved)
  2422. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, SlacRunId, SLAC_RUNID_LENGTH); //RunID (8-byte)
  2423. SendMmePacketSize += SLAC_RUNID_LENGTH;
  2424. SendMmePacketSize += 19; //the size before MMENTRY
  2425. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2426. {
  2427. DEBUG_PRINTF_EVCOMM_DETAIL("***** Response MME Packet *****\n");
  2428. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2429. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  2430. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2431. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  2432. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  2433. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  2434. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  2435. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  2436. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_PARM_CNF ---\n");
  2437. DEBUG_PRINTF_EVCOMM_DETAIL("M-SOUND_TARGET: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2438. SendMmePacket.MMENTRY[0],SendMmePacket.MMENTRY[1],SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],
  2439. SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5]);
  2440. DEBUG_PRINTF_EVCOMM_DETAIL("NUM_SOUNDS: 0x%x\n", SendMmePacket.MMENTRY[6]);
  2441. DEBUG_PRINTF_EVCOMM_DETAIL("Time_Out: 0x%x\n", SendMmePacket.MMENTRY[7]);
  2442. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_TYPE: 0x%x\n", SendMmePacket.MMENTRY[8]);
  2443. DEBUG_PRINTF_EVCOMM_DETAIL("M-FORWARDING_STA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2444. SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],SendMmePacket.MMENTRY[12],
  2445. SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14]);
  2446. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[15]);
  2447. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[16]);
  2448. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2449. SendMmePacket.MMENTRY[17],SendMmePacket.MMENTRY[18],SendMmePacket.MMENTRY[19],SendMmePacket.MMENTRY[20],
  2450. SendMmePacket.MMENTRY[21],SendMmePacket.MMENTRY[22],SendMmePacket.MMENTRY[23],SendMmePacket.MMENTRY[24]);
  2451. }
  2452. #endif
  2453. Update_V2G_Flow_Status(CM_SLAC_PARM_CONF);
  2454. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  2455. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  2456. ftime(&SeqStartTime);
  2457. counter = 0;
  2458. break;
  2459. }
  2460. #else
  2461. {
  2462. memcpy(EvMac, MmePacket->OSA, sizeof(EvMac));
  2463. memcpy(DestSocketAddress.sll_addr, MmePacket->OSA, sizeof(EvMac));
  2464. memcpy(SlacRunId,MmePacket->MMENTRY + 2, sizeof(SlacRunId));
  2465. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  2466. memcpy(SendMmePacket.ODA, MmePacket->OSA, 6);
  2467. memcpy(SendMmePacket.OSA, CsuMac, 6);
  2468. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  2469. SendMmePacket.MMV = MmePacket->MMV;
  2470. SendMmePacket.MMTYPE = MMTYPE_CM_SLAC_PARM_CNF;
  2471. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  2472. SendMmePacketSize = 0;
  2473. memset(SendMmePacket.MMENTRY, 0xFF, 6); //M-SOUND_TARGET(6-byte:0xFFFFFFFFFFFF): Fixed value indicating that M-Sounds to be sent as Ethernet broadcast
  2474. SendMmePacketSize += 6;
  2475. 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
  2476. SendMmePacket.MMENTRY[SendMmePacketSize++] = 6; //Time_Out(0x06): unit = 100ms
  2477. 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
  2478. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac, 6); //FORWARDING_STA(MAC Address of the EV HLE)
  2479. SendMmePacketSize += 6;
  2480. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00): 0x00(PEV-EVSE Association), 0x01-0xFF(Reserved)
  2481. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00): 0x00(No Security), 0x01(Public Key Signature), 0x02-0xFF(Reserved)
  2482. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, SlacRunId, sizeof(SlacRunId)); //RunID (8-byte)
  2483. SendMmePacketSize += sizeof(SlacRunId);
  2484. SendMmePacketSize += 19; //the size before MMENTRY
  2485. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2486. {
  2487. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response MME Packet *****\n");
  2488. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2489. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  2490. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2491. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  2492. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  2493. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  2494. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  2495. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  2496. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_PARM_CNF ---\n");
  2497. DEBUG_PRINTF_EVCOMM_DETAIL("M-SOUND_TARGET: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2498. SendMmePacket.MMENTRY[0],SendMmePacket.MMENTRY[1],SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],
  2499. SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5]);
  2500. DEBUG_PRINTF_EVCOMM_DETAIL("NUM_SOUNDS: 0x%x\n", SendMmePacket.MMENTRY[6]);
  2501. DEBUG_PRINTF_EVCOMM_DETAIL("Time_Out: 0x%x\n", SendMmePacket.MMENTRY[7]);
  2502. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_TYPE: 0x%x\n", SendMmePacket.MMENTRY[8]);
  2503. DEBUG_PRINTF_EVCOMM_DETAIL("M-FORWARDING_STA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2504. SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],SendMmePacket.MMENTRY[12],
  2505. SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14]);
  2506. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[15]);
  2507. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[16]);
  2508. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2509. SendMmePacket.MMENTRY[17],SendMmePacket.MMENTRY[18],SendMmePacket.MMENTRY[19],SendMmePacket.MMENTRY[20],
  2510. SendMmePacket.MMENTRY[21],SendMmePacket.MMENTRY[22],SendMmePacket.MMENTRY[23],SendMmePacket.MMENTRY[24]);
  2511. }
  2512. #endif
  2513. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  2514. Update_V2G_Flow_Status(CM_SLAC_PARM_CONF);
  2515. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  2516. ftime(&SeqStartTime);
  2517. counter = 0;
  2518. break;
  2519. }
  2520. #endif
  2521. }
  2522. case MMTYPE_CM_START_ATTEN_CHAR_IND:
  2523. {
  2524. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2525. {
  2526. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[WARNING][SLAC][Rx]CM_START_ATTEN_CHAR_IND: ignored(timeup)");
  2527. break;
  2528. }
  2529. //Avoid Coupled CM_START_ATTEN_CHAR_IND
  2530. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2531. {
  2532. sprintf((char*)buf_log_evcomm,
  2533. "[WARNING][SLAC][Rx]CM_START_ATTEN_CHAR_IND: ignored(coupled SLAC,%d)",
  2534. CheckConnectorPlugIn());
  2535. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2536. break;
  2537. }
  2538. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2539. {
  2540. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_START_ATTEN_CHAR_IND (counter : %d/3 ) ---\n", counter);
  2541. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2542. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2543. DEBUG_PRINTF_EVCOMM_DETAIL("NUM_SOUNDS: 0x%x\n", MmePacket->MMENTRY[2]);
  2544. DEBUG_PRINTF_EVCOMM_DETAIL("Time_Out 0x%x\n", MmePacket->MMENTRY[3]);
  2545. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_TYPE 0x%x\n", MmePacket->MMENTRY[4]); //Fixed value (0x01) indicating ��other Green PHY station��
  2546. DEBUG_PRINTF_EVCOMM_DETAIL("FORWARDING_STA: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2547. MmePacket->MMENTRY[5],MmePacket->MMENTRY[6],MmePacket->MMENTRY[7],MmePacket->MMENTRY[8],
  2548. MmePacket->MMENTRY[9],MmePacket->MMENTRY[10]);
  2549. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2550. MmePacket->MMENTRY[11],MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],MmePacket->MMENTRY[14],
  2551. MmePacket->MMENTRY[15],MmePacket->MMENTRY[16],MmePacket->MMENTRY[17],MmePacket->MMENTRY[18]);
  2552. }
  2553. #endif
  2554. //New SLAC architecture designed by Joseph
  2555. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2556. {
  2557. EvMac_in = &MmePacket->OSA[0];
  2558. RunID_in = &MmePacket->MMENTRY[11];
  2559. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2560. if (idx >= 0)
  2561. {
  2562. Update_V2G_Flow_Status(CM_START_ATTEN_CHAR_IND);
  2563. counter++;
  2564. //[TC_SECC_VTB_AttenuationCharacterization_013]
  2565. if (MmePacket->MMENTRY[0] != 0) //APPLICATION_TYPE must be 0x00(EV-EVSE Matching)
  2566. {
  2567. sprintf((char*)buf_log_evcomm,
  2568. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]APPLICATION_TYPE(%d): invalid => ignore Req",
  2569. MmePacket->MMENTRY[0]);
  2570. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2571. break;
  2572. }
  2573. //[TC_SECC_VTB_AttenuationCharacterization_014]
  2574. if (MmePacket->MMENTRY[1] != 0) //SECURITY_TYPE must be 0x00(No Security)
  2575. {
  2576. sprintf((char*)buf_log_evcomm,
  2577. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]SECURITY_TYPE(%d): invalid => ignore Req",
  2578. MmePacket->MMENTRY[1]);
  2579. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2580. break;
  2581. }
  2582. //[TC_SECC_VTB_AttenuationCharacterization_017]
  2583. if (MmePacket->MMENTRY[4] != 0x01) //RESP_TYPE must be 0x01(Send to another GP STA(EV))
  2584. {
  2585. sprintf((char*)buf_log_evcomm,
  2586. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]RESP_TYPE(%d): invalid => ignore Req",
  2587. MmePacket->MMENTRY[4]);
  2588. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2589. break;
  2590. }
  2591. //[TC_SECC_VTB_AttenuationCharacterization_021]
  2592. //FORWARDING_STA: MAC Address of the EV HLE (station which the measurement results shall be sent to)
  2593. if (Array_Compare_Identity(EvMac_in, &MmePacket->MMENTRY[5], SLAC_EVMAC_LENGTH) == FALSE)
  2594. {
  2595. sprintf((char*)buf_log_evcomm,
  2596. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]FORWARDING_STA(%02X:%02X:%02X:%02X:%02X:%02X): invalid => ignore Req",
  2597. MmePacket->MMENTRY[5], MmePacket->MMENTRY[6],
  2598. MmePacket->MMENTRY[7], MmePacket->MMENTRY[8],
  2599. MmePacket->MMENTRY[9], MmePacket->MMENTRY[10]);
  2600. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2601. SLAC_INFO.array[idx].StartAttenCharErr = TRUE;
  2602. break;
  2603. }
  2604. //Check RunID
  2605. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE)
  2606. {
  2607. SLAC_INFO.array[idx].MnbcSoundNum = MmePacket->MMENTRY[2];
  2608. SLAC_INFO.array[idx].StartAttenCharCnt++;
  2609. #if 0
  2610. sprintf((char*)buf_log_evcomm,
  2611. "[SLAC][Rx]CM_START_ATTEN_CHAR_IND[%d]:%d-th",
  2612. (idx + 1),
  2613. SLAC_INFO.array[idx].StartAttenCharCnt);
  2614. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2615. #endif
  2616. if (SLAC_INFO.array[idx].StartAttenCharCnt == 1)
  2617. {
  2618. memset(SLAC_INFO.array[idx].AAG, 0, sizeof(SLAC_INFO.array[idx].AAG));
  2619. SLAC_INFO.array[idx].AttenProfileCnt = 0;
  2620. ftime(&SeqStartTime); //start TT_EVSE_match_MNBC
  2621. }
  2622. else if (SLAC_INFO.array[idx].StartAttenCharCnt >= 3)
  2623. {
  2624. sprintf((char*)buf_log_evcomm,
  2625. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]counter(%d): unexpected",
  2626. SLAC_INFO.array[idx].StartAttenCharCnt);
  2627. }
  2628. else
  2629. {
  2630. //null
  2631. }
  2632. }
  2633. else
  2634. {
  2635. //This RunID is not matched with this EvMac,
  2636. //or this RunID is not found in DB.
  2637. sprintf((char*)buf_log_evcomm,
  2638. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]EvMac-RunID: unmatched => ignore Req");
  2639. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2640. //Response: ignore
  2641. }
  2642. }
  2643. else
  2644. {
  2645. //this msg source is not in database
  2646. //ignore
  2647. }
  2648. break;
  2649. }
  2650. #else //Old SLAC architecture designed by Vern
  2651. {
  2652. MnbcSoundNum = MmePacket->MMENTRY[2];
  2653. Update_V2G_Flow_Status(CM_START_ATTEN_CHAR_IND);
  2654. counter++;
  2655. if(counter == 1)
  2656. {
  2657. memset(Aag, 0, sizeof(Aag));
  2658. AttenProfileCnt = 0;
  2659. ftime(&SeqStartTime); //start TT_EVSE_match_MNBC
  2660. }
  2661. else if(counter >= 3)
  2662. {
  2663. counter = 0;
  2664. }
  2665. break;
  2666. }
  2667. #endif
  2668. }
  2669. case MMTYPE_CM_MNBC_SOUND_IND:
  2670. {
  2671. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2672. {
  2673. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND: ignored(timeup)");
  2674. break;
  2675. }
  2676. //Avoid Coupled CM_MNBC_SOUND_IND
  2677. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2678. {
  2679. sprintf((char*)buf_log_evcomm,
  2680. "[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND: ignored(coupled SLAC,%d)",
  2681. CheckConnectorPlugIn());
  2682. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2683. break;
  2684. }
  2685. if(V2gFlowStatus == CM_START_ATTEN_CHAR_IND)
  2686. {
  2687. counter = 0;
  2688. }
  2689. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2690. {
  2691. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_MNBC_SOUND_IND (counter : %d/%d) ---\n", counter, SLAC_INFO.array[idx].MnbcSoundNum);
  2692. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2693. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2694. DEBUG_PRINTF_EVCOMM_DETAIL("SenderID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2695. MmePacket->MMENTRY[2],MmePacket->MMENTRY[3],MmePacket->MMENTRY[4],MmePacket->MMENTRY[5],
  2696. MmePacket->MMENTRY[6],MmePacket->MMENTRY[7],MmePacket->MMENTRY[8],MmePacket->MMENTRY[9],
  2697. MmePacket->MMENTRY[10],MmePacket->MMENTRY[11],MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],
  2698. MmePacket->MMENTRY[14],MmePacket->MMENTRY[15],MmePacket->MMENTRY[16],MmePacket->MMENTRY[17],
  2699. MmePacket->MMENTRY[18]);
  2700. DEBUG_PRINTF_EVCOMM_DETAIL("Cnt: 0x%x\n", MmePacket->MMENTRY[19]);
  2701. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2702. MmePacket->MMENTRY[20],MmePacket->MMENTRY[21],MmePacket->MMENTRY[22],MmePacket->MMENTRY[23],
  2703. MmePacket->MMENTRY[24],MmePacket->MMENTRY[25],MmePacket->MMENTRY[26],MmePacket->MMENTRY[27]);
  2704. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2705. MmePacket->MMENTRY[28],MmePacket->MMENTRY[29],MmePacket->MMENTRY[30],MmePacket->MMENTRY[31],
  2706. MmePacket->MMENTRY[32],MmePacket->MMENTRY[33],MmePacket->MMENTRY[34],MmePacket->MMENTRY[35]);
  2707. DEBUG_PRINTF_EVCOMM_DETAIL("Rnd: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2708. MmePacket->MMENTRY[36],MmePacket->MMENTRY[37],MmePacket->MMENTRY[38],MmePacket->MMENTRY[39],
  2709. MmePacket->MMENTRY[40],MmePacket->MMENTRY[41],MmePacket->MMENTRY[42],MmePacket->MMENTRY[43],
  2710. MmePacket->MMENTRY[44],MmePacket->MMENTRY[45],MmePacket->MMENTRY[46],MmePacket->MMENTRY[47],
  2711. MmePacket->MMENTRY[48],MmePacket->MMENTRY[49],MmePacket->MMENTRY[50],MmePacket->MMENTRY[51]);
  2712. }
  2713. #endif
  2714. //New SLAC architecture designed by Joseph
  2715. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2716. {
  2717. EvMac_in = &MmePacket->OSA[0];
  2718. RunID_in = &MmePacket->MMENTRY[20];
  2719. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2720. if (idx >= 0)
  2721. {
  2722. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2723. counter++;
  2724. //Check for RunID
  2725. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE)
  2726. {
  2727. SLAC_INFO.array[idx].MnbcSoundCnt++;
  2728. /*
  2729. sprintf((char*)buf_log_evcomm,
  2730. "[SLAC][Rx]CM_MNBC_SOUND_IND[%d]:%d-th",
  2731. (idx + 1),
  2732. SLAC_INFO.array[idx].MnbcSoundCnt);
  2733. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2734. */
  2735. }
  2736. else
  2737. {
  2738. //RunID is not matched or does not exist.
  2739. sprintf((char*)buf_log_evcomm,
  2740. "[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND]EvMac-RunID: unmatched");
  2741. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2742. }
  2743. }
  2744. else
  2745. {
  2746. //ignore
  2747. sprintf((char*)buf_log_evcomm,
  2748. "[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND]EvMac does not exist");
  2749. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2750. }
  2751. break;
  2752. }
  2753. #else //Old SLAC architecture designed by Vern
  2754. {
  2755. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2756. counter++;
  2757. break;
  2758. }
  2759. #endif
  2760. }
  2761. case MMTYPE_CM_ATTEN_PROFILE_IND:
  2762. {
  2763. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2764. {
  2765. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[WARNING][SLAC][Rx]CM_ATTEN_PROFILE_IND: ignore(timeup)");
  2766. break;
  2767. }
  2768. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2769. {
  2770. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_ATTEN_PROFILE_IND (counter : %d/%d) ---\n", counter, SLAC_INFO.array[idx].MnbcSoundNum);
  2771. DEBUG_PRINTF_EVCOMM_DETAIL("EV MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2772. MmePacket->MMENTRY[0], MmePacket->MMENTRY[1], MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2773. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5]);
  2774. DEBUG_PRINTF_EVCOMM_DETAIL("NumGroups: 0x%x\n", MmePacket->MMENTRY[6]); //NumGroups, Number of AAG Groups (ex: 0x3A = 58)
  2775. //NumGroups: Number of OFDM carrier groups used for the SLAC signal characterization.
  2776. //AAG[i]: Average Attenuation of Group i (i = 1 ~ 58)
  2777. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: 0x%x\n", MmePacket->MMENTRY[7]);
  2778. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  2779. for(Rtn = 0; Rtn < MmePacket->MMENTRY[6]; Rtn++)
  2780. {
  2781. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, MmePacket->MMENTRY[8 + Rtn]);
  2782. }
  2783. DEBUG_PRINTF_EVCOMM_DETAIL("AAG: %s\n", tmpBuf);
  2784. }
  2785. #endif
  2786. //New SLAC architecture designed by Joseph
  2787. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2788. {
  2789. EvMac_in = &MmePacket->MMENTRY[0]; //[CAUTION] The EvMac address is not &MmePacket->OSA[0]
  2790. //[CAUTION] There is no RunID information of CM_ATTEN_PROFILE_IND
  2791. // packet, which means SECC cannot use the RunID to
  2792. // distinguish those SLAC request with identical EvMac
  2793. // but with different RunID.
  2794. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2795. /*
  2796. printf("%d, %d, %d, %d\n",
  2797. idx,
  2798. SLAC_INFO.array[idx].AagGroupsNum,
  2799. MmePacket->MMENTRY[6],
  2800. SLAC_INFO.array[idx].AttenProfileCnt);
  2801. */
  2802. if (idx >= 0)
  2803. {
  2804. SLAC_INFO.array[idx].AttenProfileCnt++;
  2805. /*
  2806. sprintf((char*)buf_log_evcomm,
  2807. "[SLAC][Rx]CM_ATTEN_PROFILE_IND[%d]:%d-th",
  2808. (idx + 1),
  2809. SLAC_INFO.array[idx].AttenProfileCnt);
  2810. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2811. */
  2812. //printf("%d, %d\n", SLAC_INFO.array[idx].AagGroupsNum, MmePacket->MMENTRY[6]);
  2813. SLAC_INFO.array[idx].AagGroupsNum = MmePacket->MMENTRY[6];
  2814. ////NumGroups, Number of AAG Groups (ex: 0x3A = 58)
  2815. for(Rtn = 0; Rtn < SLAC_INFO.array[idx].AagGroupsNum; Rtn++) //ex:58
  2816. {
  2817. SLAC_INFO.array[idx].AAG[Rtn] += MmePacket->MMENTRY[8 + Rtn];
  2818. }
  2819. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2820. break;
  2821. }
  2822. else
  2823. {
  2824. //The EvMac is not in the database
  2825. //ignore
  2826. sprintf((char*)buf_log_evcomm,
  2827. "[SLAC][Rx][CM_ATTEN_PROFILE_IND]EvMac(%02X%02X%02X%02X%02X%02X): not exist => ignore",
  2828. EvMac_in[0], EvMac_in[1], EvMac_in[2], EvMac_in[3], EvMac_in[4], EvMac_in[5]);
  2829. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2830. break;
  2831. }
  2832. }
  2833. #else //Old SLAC architecture designed by Vern
  2834. {
  2835. AagGroupsNum = MmePacket->MMENTRY[6];
  2836. for(Rtn = 0; Rtn < MmePacket->MMENTRY[6]; Rtn++)
  2837. {
  2838. Aag[Rtn] += MmePacket->MMENTRY[8 + Rtn];
  2839. }
  2840. AttenProfileCnt++;
  2841. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2842. break;
  2843. }
  2844. #endif
  2845. }
  2846. case MMTYPE_CM_ATTEN_CHAR_RSP:
  2847. {
  2848. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2849. {
  2850. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_ATTEN_CHAR_RSP ---\n");
  2851. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2852. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2853. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2854. MmePacket->MMENTRY[2],MmePacket->MMENTRY[3],MmePacket->MMENTRY[4],MmePacket->MMENTRY[5],
  2855. MmePacket->MMENTRY[6],MmePacket->MMENTRY[7]);
  2856. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2857. MmePacket->MMENTRY[8],MmePacket->MMENTRY[9],MmePacket->MMENTRY[10],MmePacket->MMENTRY[11],
  2858. MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],MmePacket->MMENTRY[14],MmePacket->MMENTRY[15]);
  2859. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  2860. for(Rtn = 0; Rtn < 17; Rtn++)
  2861. {
  2862. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, MmePacket->MMENTRY[16 + Rtn]);
  2863. }
  2864. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ID: %s\n", tmpBuf);
  2865. ;
  2866. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  2867. for(Rtn = 0; Rtn < 17; Rtn++)
  2868. {
  2869. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, MmePacket->MMENTRY[33 + Rtn]);
  2870. }
  2871. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_ID: %s\n", tmpBuf);
  2872. DEBUG_PRINTF_EVCOMM_DETAIL("Result: 0x%x\n", MmePacket->MMENTRY[50]); //Fixed value of 0x00 indicates a successful SLAC process
  2873. }
  2874. #endif
  2875. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2876. {
  2877. //Check ODA (Destination Address)
  2878. if (Array_Compare_Identity(CsuMac, MmePacket->ODA, SLAC_EVSE_MAC_LENGTH) == FALSE)
  2879. {
  2880. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx][CM_ATTEN_CHAR_RSP]wrong ODA: ignore");
  2881. break;
  2882. }
  2883. EvMac_in = &MmePacket->OSA[0];
  2884. RunID_in = &MmePacket->MMENTRY[8];
  2885. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2886. //Check Parameters
  2887. if ((MmePacket->MMENTRY[0] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_005] applicationType must be 0x00
  2888. (MmePacket->MMENTRY[1] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_006] securityType must be 0x00
  2889. (SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in) < 0) || //[TC_SECC_VTB_AttenuationCharacterization_007] sourceAddress cannot not be all zero
  2890. (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_008] check for invalid runID
  2891. (Array_Check_All_Zero(&MmePacket->MMENTRY[16], 17) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_009] SourceID(17 bytes) must be all zero.
  2892. (Array_Check_All_Zero(&MmePacket->MMENTRY[33], 17) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_009] RESP_ID (17 bytes) must be all zero.
  2893. (MmePacket->MMENTRY[50] != 0) //Result: must be 0x00(Success)
  2894. )
  2895. {
  2896. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry++;
  2897. sprintf((char*)buf_log_evcomm,
  2898. "[WARNING][SLAC][Rx]CM_ATTEN_CHAR_RSP:invalid para(%d,%d,%d,%d,%d) => %d-th retry",
  2899. MmePacket->MMENTRY[0], //applicationType must be 0x00
  2900. MmePacket->MMENTRY[1], //securityType must be 0x00
  2901. SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in),
  2902. SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in),
  2903. MmePacket->MMENTRY[50],
  2904. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry
  2905. );
  2906. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2907. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry <= 2) //limited to 2 retries
  2908. {
  2909. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2910. ftime(&SeqStartTime);
  2911. break;
  2912. }
  2913. else
  2914. {
  2915. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR][SLAC][Rx]CM_ATTEN_CHAR_RSP:invalid para => 2-retry fail => End_Process");
  2916. Update_V2G_Flow_Status(Other_Fault);
  2917. break;
  2918. }
  2919. }
  2920. else
  2921. {
  2922. //The CM_ATTEN_CHAR_IND is legal
  2923. SLAC_INFO.array[idx].AttenCharRspCnt++;
  2924. sprintf((char*)buf_log_evcomm,
  2925. "[SLAC][Rx]CM_ATTEN_CHAR_RSP[%d]:%d-th",
  2926. (idx + 1),
  2927. SLAC_INFO.array[idx].AttenCharRspCnt);
  2928. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2929. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry = 0;
  2930. Update_V2G_Flow_Status(CM_ATTEN_CHAR_RSP);
  2931. ftime(&SeqStartTime);
  2932. break;
  2933. }
  2934. }
  2935. #else
  2936. {
  2937. //Check Parameters
  2938. if ((MmePacket->MMENTRY[0] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_005] applicationType must be 0x00
  2939. (MmePacket->MMENTRY[1] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_006] securityType must be 0x00
  2940. (Array_Compare_Identity(SLAC_INFO.array[0].EvMac, &MmePacket->MMENTRY[2], 6) == TRUE) || //[TC_SECC_VTB_AttenuationCharacterization_007] sourceAddress cannot not be all zero
  2941. (Array_Compare_Identity(SLAC_INFO.array[0].RunID, &MmePacket->MMENTRY[8], 8) == TRUE) //[TC_SECC_VTB_AttenuationCharacterization_008] check for invalid runID
  2942. )
  2943. {
  2944. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry++;
  2945. sprintf((char*)buf_log_evcomm,
  2946. "[WARNING][SLAC][Rx]CM_ATTEN_CHAR_RSP: invalid parameters(%d-th)",
  2947. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry);
  2948. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2949. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry <= 2) //limited to 2 retries
  2950. {
  2951. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2952. ftime(&SeqStartTime);
  2953. break;
  2954. }
  2955. else
  2956. {
  2957. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]CM_ATTEN_CHAR_RSP: invalid parameters => End_Process");
  2958. Update_V2G_Flow_Status(Other_Fault);
  2959. ftime(&SeqStartTime);
  2960. break;
  2961. }
  2962. }
  2963. else
  2964. {
  2965. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry = 0;
  2966. Update_V2G_Flow_Status(CM_ATTEN_CHAR_RSP);
  2967. ftime(&SeqStartTime);
  2968. break;
  2969. }
  2970. }
  2971. #endif
  2972. }
  2973. case MMTYPE_CM_VALIDATE_REQ: //BCB Toggle
  2974. {
  2975. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2976. {
  2977. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_VALIDATE_REQ ---\n");
  2978. DEBUG_PRINTF_EVCOMM_DETAIL("Signal Type: 0x%x\n", MmePacket->MMENTRY[0]); //Fixed value (0x00) to indicate ��PEV S2 toggles on control pilot line��
  2979. 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.
  2980. 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��.
  2981. }
  2982. #endif
  2983. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2984. {
  2985. counter = 0;
  2986. EvMac_in = &MmePacket->OSA[0];
  2987. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2988. if (idx >= 0)
  2989. {
  2990. Update_V2G_Flow_Status(CM_VALIDATE_CNF);
  2991. SLAC_INFO.array[idx].ValidateReqCnt++;
  2992. sprintf((char*)buf_log_evcomm,
  2993. "[SLAC][Rx]CM_VALIDATE_REQ[%d]:%d-th",
  2994. (idx + 1),
  2995. SLAC_INFO.array[idx].ValidateReqCnt);
  2996. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2997. //[To-Do] Protection
  2998. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  2999. memcpy(SendMmePacket.ODA, EvMac_in, SLAC_EVMAC_LENGTH);
  3000. memcpy(SendMmePacket.OSA, CsuMac, 6);
  3001. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  3002. SendMmePacket.MMV = 0x01;
  3003. SendMmePacket.MMTYPE = MMTYPE_CM_VALIDATE_CNF;
  3004. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  3005. SendMmePacketSize = 0;
  3006. if(counter == 0)
  3007. {
  3008. //The First MMTYPE_CM_VALIDATE_REQ because Unicast
  3009. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SignalType(0x00): Fixed value to indicate "EV S2 toggles on control pilot line"
  3010. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //ToggleNum(0x00): Fixed value. In the first VALIDATE Request-Response exchange, the ToggleNum field shall be set to zero.
  3011. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  3012. SendMmePacket.MMENTRY[SendMmePacketSize++] = 1; //0x01 = Ready
  3013. #else
  3014. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  3015. #endif
  3016. }
  3017. else
  3018. {
  3019. //The Second MMTYPE_CM_VALIDATE_REQ because Broadcast
  3020. unsigned char PreStatus = 3;
  3021. unsigned char ToggleNum = 0;
  3022. ftime(&SeqStartTime);
  3023. while(1)
  3024. {
  3025. if((EVCOMM_SYS_INFO.CpState == 4) && (PreStatus == 3))
  3026. {
  3027. ToggleNum++;
  3028. PreStatus = 4;
  3029. }
  3030. else
  3031. {
  3032. PreStatus = 3;
  3033. }
  3034. ftime(&SeqEndTime);
  3035. if(DiffTimeb(SeqStartTime, SeqEndTime) >= (SendMmePacket.MMENTRY[1]*100 + 100))
  3036. {
  3037. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value to indicate "PEV S2 toggles on control pilot line"
  3038. SendMmePacket.MMENTRY[SendMmePacketSize++] = ToggleNum;
  3039. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  3040. SendMmePacket.MMENTRY[SendMmePacketSize++] = 2; //0x02 = Success
  3041. #else
  3042. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  3043. #endif
  3044. break;
  3045. }
  3046. }
  3047. }
  3048. SendMmePacketSize += 19; //the size before MMENTRY
  3049. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3050. ftime(&SeqStartTime);
  3051. }
  3052. else
  3053. {
  3054. //EvMac does not exist.
  3055. //ignore
  3056. }
  3057. break;
  3058. }
  3059. #else
  3060. {
  3061. counter = 0;
  3062. for(Rtn = 0; Rtn < 6; Rtn++)
  3063. {
  3064. if(MmePacket->ODA[Rtn] != CsuMac[Rtn])
  3065. {
  3066. counter = 1;
  3067. break;
  3068. }
  3069. }
  3070. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  3071. memcpy(SendMmePacket.ODA, EvMac,6);
  3072. memcpy(SendMmePacket.OSA, CsuMac,6);
  3073. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  3074. SendMmePacket.MMV = 0x01;
  3075. SendMmePacket.MMTYPE = MMTYPE_CM_VALIDATE_CNF;
  3076. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  3077. SendMmePacketSize = 0;
  3078. if(counter == 0)
  3079. {
  3080. //The First MMTYPE_CM_VALIDATE_REQ because Unicast
  3081. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value to indicate ��PEV S2 toggles on control pilot line��
  3082. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value In the first VALIDATE Request-Response exchange, the ToggleNum field shall be set to zero.
  3083. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  3084. SendMmePacket.MMENTRY[SendMmePacketSize++] = 1; //0x01 = Ready
  3085. #else
  3086. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  3087. #endif
  3088. }
  3089. else
  3090. {
  3091. //The Second MMTYPE_CM_VALIDATE_REQ because Broadcast
  3092. unsigned char PreStatus = 3, ToggleNum = 0;
  3093. ftime(&SeqStartTime);
  3094. while(1)
  3095. {
  3096. ftime(&SeqEndTime);
  3097. if((EVCOMM_SYS_INFO.CpState == 4) && (PreStatus == 3))
  3098. {
  3099. ToggleNum++;
  3100. PreStatus = 4;
  3101. }
  3102. else
  3103. {
  3104. PreStatus = 3;
  3105. }
  3106. if(DiffTimeb(SeqStartTime, SeqEndTime) >= (SendMmePacket.MMENTRY[1]*100 + 100))
  3107. {
  3108. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value to indicate ��PEV S2 toggles on control pilot line��
  3109. SendMmePacket.MMENTRY[SendMmePacketSize++] = ToggleNum;
  3110. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  3111. SendMmePacket.MMENTRY[SendMmePacketSize++] = 2; //0x02 = Success
  3112. #else
  3113. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  3114. #endif
  3115. break;
  3116. }
  3117. }
  3118. }
  3119. SendMmePacketSize += 19; //the size before MMENTRY
  3120. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3121. Update_V2G_Flow_Status(CM_VALIDATE_CNF);
  3122. ftime(&SeqStartTime);
  3123. break;
  3124. }
  3125. #endif
  3126. }
  3127. case MMTYPE_CM_SLAC_MATCH_REQ:
  3128. {
  3129. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  3130. {
  3131. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SLAC_MATCH_REQ ---\n");
  3132. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  3133. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  3134. DEBUG_PRINTF_EVCOMM_DETAIL("MVFLength: 0x%x, 0x%x\n", MmePacket->MMENTRY[2],MmePacket->MMENTRY[3]); //Fixed value (0x3E) for matching
  3135. DEBUG_PRINTF_EVCOMM_DETAIL("PEV ID: \n");
  3136. for(Rtn=0; Rtn<17; Rtn++)
  3137. {
  3138. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[4+Rtn]);
  3139. }
  3140. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3141. DEBUG_PRINTF_EVCOMM_DETAIL("PEV MAC: \n");
  3142. for(Rtn=0; Rtn<6; Rtn++)
  3143. {
  3144. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[21+Rtn]);
  3145. }
  3146. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3147. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE ID: \n");
  3148. for(Rtn=0; Rtn<17; Rtn++)
  3149. {
  3150. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[27+Rtn]);
  3151. }
  3152. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3153. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE MAC: \n");
  3154. for(Rtn=0; Rtn<6; Rtn++)
  3155. {
  3156. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[44+Rtn]);
  3157. }
  3158. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3159. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: \n");
  3160. for(Rtn=0; Rtn<8; Rtn++)
  3161. {
  3162. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[50+Rtn]);
  3163. }
  3164. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3165. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: \n");
  3166. for(Rtn=0; Rtn<8; Rtn++)
  3167. {
  3168. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[58+Rtn]);
  3169. }
  3170. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3171. }
  3172. #endif
  3173. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  3174. {
  3175. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx]CM_SLAC_MATCH_REQ");
  3176. //Check ODA (Destination Address)
  3177. if (Array_Compare_Identity(CsuMac, MmePacket->ODA, SLAC_EVSE_MAC_LENGTH) == FALSE)
  3178. {
  3179. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx][CM_SLAC_MATCH_REQ]wrong ODA: ignore");
  3180. //break; //patch for DEKRA I2SE EVCC (another EVSE MAC Address protection comes as below.)
  3181. }
  3182. EvMac_in = &MmePacket->OSA[0];
  3183. RunID_in = &MmePacket->MMENTRY[50];
  3184. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  3185. if (idx >= 0)
  3186. {
  3187. Update_V2G_Flow_Status(CM_SLAC_MATCH_REQ);
  3188. SLAC_INFO.array[idx].MatchReqNum++;
  3189. sprintf((char*)buf_log_evcomm,
  3190. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:%d-th",
  3191. (idx + 1),
  3192. SLAC_INFO.array[idx].MatchReqNum);
  3193. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3194. //[TC_SECC_VTB_CmSlacMatch_007] APPLICATION_TYPE must be 0x00(EV-EVSE Matching)
  3195. //[TC_SECC_VTB_CmSlacMatch_008]
  3196. if (MmePacket->MMENTRY[0] != 0)
  3197. {
  3198. sprintf((char*)buf_log_evcomm,
  3199. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:wrong APPLICATION_TYPE(%d)",
  3200. (idx + 1),
  3201. MmePacket->MMENTRY[0]);
  3202. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3203. break;
  3204. }
  3205. //[TC_SECC_VTB_CmSlacMatch_009] SECURITY_TYPE must be 0x00(No Security)
  3206. //[TC_SECC_VTB_CmSlacMatch_010]
  3207. if (MmePacket->MMENTRY[1] != 0)
  3208. {
  3209. sprintf((char*)buf_log_evcomm,
  3210. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:wrong SECURITY_TYPE(%d)",
  3211. (idx + 1),
  3212. MmePacket->MMENTRY[1]);
  3213. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3214. break;
  3215. }
  3216. //[TC_SECC_VTB_CmSlacMatch_011] MVFLength must be 0x3E(MatchVarField Length)
  3217. //[TC_SECC_VTB_CmSlacMatch_012]
  3218. if (MmePacket->MMENTRY[2] != 0x3E || MmePacket->MMENTRY[3] != 0x00)
  3219. {
  3220. sprintf((char*)buf_log_evcomm,
  3221. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:wrong MVFLength(%d,%d)",
  3222. (idx + 1),
  3223. MmePacket->MMENTRY[2],
  3224. MmePacket->MMENTRY[3]);
  3225. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3226. break;
  3227. }
  3228. //[TC_SECC_VTB_CmSlacMatch_013] EV ID (muxt be all zero)
  3229. //[TC_SECC_VTB_CmSlacMatch_014]
  3230. if (Array_Check_All_Zero(&MmePacket->MMENTRY[4], 17) == FALSE)
  3231. {
  3232. sprintf((char*)buf_log_evcomm,
  3233. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EV ID",
  3234. (idx + 1));
  3235. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3236. break;
  3237. }
  3238. //[TC_SECC_VTB_CmSlacMatch_015] EV MAC
  3239. //[TC_SECC_VTB_CmSlacMatch_016]
  3240. if (Array_Compare_Identity(EvMac_in, &MmePacket->MMENTRY[21], SLAC_EVMAC_LENGTH) == FALSE)
  3241. {
  3242. sprintf((char*)buf_log_evcomm,
  3243. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EV MAC(%02X:%02X:%02X:%02X:%02X:%02X)",
  3244. (idx + 1),
  3245. MmePacket->MMENTRY[21], MmePacket->MMENTRY[22], MmePacket->MMENTRY[23],
  3246. MmePacket->MMENTRY[24], MmePacket->MMENTRY[25], MmePacket->MMENTRY[26]);
  3247. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3248. break;
  3249. }
  3250. //[TC_SECC_VTB_CmSlacMatch_017] EVSE ID should be all zero
  3251. //[TC_SECC_VTB_CmSlacMatch_018]
  3252. if (Array_Check_All_Zero(&MmePacket->MMENTRY[27], 17) == FALSE)
  3253. {
  3254. sprintf((char*)buf_log_evcomm,
  3255. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EVSE ID",
  3256. (idx + 1));
  3257. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3258. break;
  3259. }
  3260. //[TC_SECC_VTB_CmSlacMatch_019] EVSE MAC
  3261. //[TC_SECC_VTB_CmSlacMatch_020]
  3262. if (Array_Compare_Identity(CsuMac, &MmePacket->MMENTRY[44], SLAC_EVSE_MAC_LENGTH) == FALSE)
  3263. {
  3264. sprintf((char*)buf_log_evcomm,
  3265. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EVSE MAC(%02X:%02X:%02X:%02X:%02X:%02X)",
  3266. (idx + 1),
  3267. MmePacket->MMENTRY[44], MmePacket->MMENTRY[45], MmePacket->MMENTRY[46],
  3268. MmePacket->MMENTRY[47], MmePacket->MMENTRY[48], MmePacket->MMENTRY[49]);
  3269. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3270. break;
  3271. }
  3272. //[TC_SECC_VTB_CmSlacMatch_021]runID: Check RunID
  3273. //[TC_SECC_VTB_CmSlacMatch_022]
  3274. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE &&
  3275. (idx == 0)) //only setup a successful link establishment with the first SLAC Req instance
  3276. {
  3277. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  3278. memcpy(SendMmePacket.ODA, MmePacket->OSA, 6);
  3279. memcpy(SendMmePacket.OSA, CsuMac, 6);
  3280. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  3281. SendMmePacket.MMV = MmePacket->MMV;
  3282. SendMmePacket.MMTYPE = MMTYPE_CM_SLAC_MATCH_CNF;
  3283. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  3284. SendMmePacketSize = 0;
  3285. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //APPLICATION_TYPE: Fixed value (0x00: EV-EVSE matching)
  3286. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //SECURITY_TYPE: Fixed value (0x00: No Security)
  3287. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x56; //MVFLength: MatchVarField Length (Fixed value: 0x0056)
  3288. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //MVFLength: MatchVarField Length (Fixed value: 0x0056)
  3289. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //EV ID
  3290. SendMmePacketSize += 17;
  3291. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac_in, SLAC_EVMAC_LENGTH); //EV MAC
  3292. SendMmePacketSize += 6;
  3293. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //EVSE ID
  3294. SendMmePacketSize += 17;
  3295. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, CsuMac, 6); //EVSE MAC
  3296. SendMmePacketSize += 6;
  3297. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, RunID_in, SLAC_RUNID_LENGTH);
  3298. SendMmePacketSize += SLAC_RUNID_LENGTH;
  3299. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 8); //RSVD
  3300. SendMmePacketSize += 8;
  3301. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, Nid, sizeof(Nid)); //NID: Network ID given by the CCo(EVSE)
  3302. SendMmePacketSize += sizeof(Nid); //NID caculated from the random NMK that will be set.
  3303. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //RSVD
  3304. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, NewNmkKey, sizeof(NewNmkKey)); //NMK: Random value
  3305. SendMmePacketSize += sizeof(NewNmkKey); //NMK: Private NMK of the EVSE (random value)
  3306. SendMmePacketSize += 19; //the size before MMENTRY
  3307. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  3308. {
  3309. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response MME Packet *****\n");
  3310. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3311. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  3312. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3313. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  3314. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  3315. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  3316. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  3317. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  3318. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_MATCH_CNF ---\n");
  3319. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  3320. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  3321. DEBUG_PRINTF_EVCOMM_DETAIL("MVFLength: 0x%x, 0x%x\n", SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3]);
  3322. DEBUG_PRINTF_EVCOMM_DETAIL("PEV ID: \n");
  3323. for(Rtn=0; Rtn<17; Rtn++)
  3324. {
  3325. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[4+Rtn]);
  3326. }
  3327. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3328. DEBUG_PRINTF_EVCOMM_DETAIL("PEV MAC: \n");
  3329. for(Rtn=0; Rtn<6; Rtn++)
  3330. {
  3331. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[21+Rtn]);
  3332. }
  3333. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3334. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE ID: \n");
  3335. for(Rtn=0; Rtn<17; Rtn++)
  3336. {
  3337. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[27+Rtn]);
  3338. }
  3339. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3340. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE MAC: \n");
  3341. for(Rtn=0; Rtn<6; Rtn++)
  3342. {
  3343. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[44+Rtn]);
  3344. }
  3345. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3346. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: \n");
  3347. for(Rtn=0; Rtn<8; Rtn++)
  3348. {
  3349. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[50+Rtn]);
  3350. }
  3351. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3352. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: \n");
  3353. for(Rtn=0; Rtn<8; Rtn++)
  3354. {
  3355. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[58+Rtn]);
  3356. }
  3357. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3358. DEBUG_PRINTF_EVCOMM_DETAIL("NID: \n");
  3359. for(Rtn=0; Rtn<7; Rtn++)
  3360. {
  3361. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[66+Rtn]);
  3362. }
  3363. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3364. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: 0x%x\n", SendMmePacket.MMENTRY[73]);
  3365. DEBUG_PRINTF_EVCOMM_DETAIL("NMK: \n");
  3366. for(Rtn=0; Rtn<16; Rtn++)
  3367. {
  3368. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[74+Rtn]);
  3369. }
  3370. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3371. }
  3372. #endif
  3373. Update_V2G_Flow_Status(CM_SLAC_MATCH_CNF);
  3374. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3375. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  3376. sprintf((char*)buf_log_evcomm,
  3377. "[SLAC][Tx]CM_SLAC_MATCH_CNF[%d]",
  3378. (idx + 1));
  3379. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3380. ftime(&SeqStartTime);
  3381. }
  3382. else
  3383. {
  3384. //RunID does not match and it's not the first SLAC request
  3385. //Reset the SLAC database to embrace SLAC retry
  3386. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CM_SLAC_MATCH_REQ]No Real MATCH_REQ");
  3387. SLAC_DB_Reset();
  3388. }
  3389. }
  3390. else
  3391. {
  3392. //OSA(EvMac) does not exist
  3393. }
  3394. break;
  3395. }
  3396. #else
  3397. {
  3398. Update_V2G_Flow_Status(CM_SLAC_MATCH_REQ);
  3399. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx]CM_SLAC_MATCH_REQ");
  3400. memset(&SendMmePacket,0,sizeof(struct MmeHeader));
  3401. memcpy(SendMmePacket.ODA,MmePacket->OSA,6);
  3402. memcpy(SendMmePacket.OSA,CsuMac,6);
  3403. SendMmePacket.MTYPE=htons(EtherType_HomePlug);
  3404. SendMmePacket.MMV=MmePacket->MMV;
  3405. SendMmePacket.MMTYPE=MMTYPE_CM_SLAC_MATCH_CNF;
  3406. SendMmePacket.FMI[0]=SendMmePacket.FMI[1]=0;
  3407. SendMmePacketSize=0;
  3408. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //Fixed value (0x00) indicating ��PEV-EVSE matching��
  3409. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //Fixed value (0x00) indicating ��No Security��
  3410. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x56; //Fixed value (0x0056) for matching
  3411. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //Fixed value (0x0056) for matching
  3412. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,17); //PEV ID
  3413. SendMmePacketSize+=17;
  3414. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,EvMac,6);
  3415. SendMmePacketSize+=6;
  3416. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,17); //EVSE ID
  3417. SendMmePacketSize+=17;
  3418. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,CsuMac,6);
  3419. SendMmePacketSize+=6;
  3420. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,SlacRunId,sizeof(SlacRunId));
  3421. SendMmePacketSize+=sizeof(SlacRunId);
  3422. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,8); //RSVD
  3423. SendMmePacketSize+=8;
  3424. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,Nid,sizeof(Nid));
  3425. SendMmePacketSize+=sizeof(Nid);
  3426. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //RSVD
  3427. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,NewNmkKey,sizeof(NewNmkKey));
  3428. SendMmePacketSize+=sizeof(NewNmkKey);
  3429. SendMmePacketSize+=19; //the size before MMENTRY
  3430. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  3431. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response MME Packet *****\n");
  3432. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3433. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  3434. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3435. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  3436. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  3437. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  3438. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  3439. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  3440. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_MATCH_CNF ---\n");
  3441. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  3442. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  3443. DEBUG_PRINTF_EVCOMM_DETAIL("MVFLength: 0x%x, 0x%x\n", SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3]);
  3444. DEBUG_PRINTF_EVCOMM_DETAIL("PEV ID: \n");
  3445. for(Rtn=0; Rtn<17; Rtn++)
  3446. {
  3447. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[4+Rtn]);
  3448. }
  3449. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3450. DEBUG_PRINTF_EVCOMM_DETAIL("PEV MAC: \n");
  3451. for(Rtn=0; Rtn<6; Rtn++)
  3452. {
  3453. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[21+Rtn]);
  3454. }
  3455. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3456. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE ID: \n");
  3457. for(Rtn=0; Rtn<17; Rtn++)
  3458. {
  3459. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[27+Rtn]);
  3460. }
  3461. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3462. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE MAC: \n");
  3463. for(Rtn=0; Rtn<6; Rtn++)
  3464. {
  3465. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[44+Rtn]);
  3466. }
  3467. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3468. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: \n");
  3469. for(Rtn=0; Rtn<8; Rtn++)
  3470. {
  3471. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[50+Rtn]);
  3472. }
  3473. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3474. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: \n");
  3475. for(Rtn=0; Rtn<8; Rtn++)
  3476. {
  3477. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[58+Rtn]);
  3478. }
  3479. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3480. DEBUG_PRINTF_EVCOMM_DETAIL("NID: \n");
  3481. for(Rtn=0; Rtn<7; Rtn++)
  3482. {
  3483. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[66+Rtn]);
  3484. }
  3485. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3486. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: 0x%x\n", SendMmePacket.MMENTRY[73]);
  3487. DEBUG_PRINTF_EVCOMM_DETAIL("NMK: \n");
  3488. for(Rtn=0; Rtn<16; Rtn++)
  3489. {
  3490. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[74+Rtn]);
  3491. }
  3492. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3493. #endif
  3494. Update_V2G_Flow_Status(CM_SLAC_MATCH_CNF);
  3495. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3496. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  3497. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Tx]CM_SLAC_MATCH_CNF");
  3498. ftime(&SeqStartTime);
  3499. break;
  3500. }
  3501. #endif
  3502. }
  3503. case MMTYPE_VENDOR_VS_HOST_ACTION:
  3504. {
  3505. struct QcaVendorMmeHeader *RecvPacket;
  3506. RecvPacket = (struct QcaVendorMmeHeader *)Buffer;
  3507. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_VS_HOST_ACTION ---\n");
  3508. switch (RecvPacket->MBODY[0])
  3509. {
  3510. case 0x00:
  3511. //Loader (Device Softloader or Bootloader) ready
  3512. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Loader Ready\n");
  3513. break;
  3514. case 0x01:
  3515. //Firmware Upgrade Ready
  3516. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Firmware Upgrade Ready\n");
  3517. break;
  3518. case 0x02:
  3519. //PIB Update Ready
  3520. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: PIB Update Ready\n");
  3521. break;
  3522. case 0x03:
  3523. //Firmware Upgrade and PIB Update ready
  3524. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Firmware Upgrade and PIB Update ready\n");
  3525. break;
  3526. case 0x04:
  3527. //Loader (Bootloader) ready to receive SDRAM configuration.
  3528. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Loader ready to receive SDRAM configuration\n");
  3529. break;
  3530. case 0x05:
  3531. //Reset to Factory Defaults.
  3532. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Reset to Factory Defaults\n");
  3533. break;
  3534. default:
  3535. //Reserved
  3536. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Reserved\n");
  3537. break;
  3538. }
  3539. break;
  3540. }
  3541. case MMTYPE_VENDOR_ATTEN_CHAR:
  3542. {
  3543. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_ATTEN_CHAR ---\n");
  3544. break;
  3545. }
  3546. case MMTYPE_VENDOR_VS_NW_INFO_CNF:
  3547. {
  3548. memcpy(QcaMac, MmePacket->OSA, 6);
  3549. sprintf((char*)buf_log_evcomm,
  3550. "[QCA7K][Rx][VENDOR_VS_NW_INFO_CNF]Got QCA7K MacAddr:%02X:%02X:%02X:%02X:%02X:%02X (comm:OK)",
  3551. QcaMac[0], QcaMac[1], QcaMac[2], QcaMac[3], QcaMac[4], QcaMac[5]);
  3552. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3553. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_VS_NW_INFO_CNF ---\n");
  3554. DEBUG_PRINTF_EVCOMM_DETAIL("QcaMac: %02x:%02x:%02x:%02x:%02x:%02x\n",
  3555. QcaMac[0],
  3556. QcaMac[1],
  3557. QcaMac[2],
  3558. QcaMac[3],
  3559. QcaMac[4],
  3560. QcaMac[5]);
  3561. Update_V2G_Flow_Status(CM_SET_KEY_REQ);
  3562. ftime(&SeqStartTime);
  3563. break;
  3564. }
  3565. default:
  3566. {
  3567. break;
  3568. }
  3569. }
  3570. return 0;
  3571. }
  3572. /*===========================================================================
  3573. FUNCTION: SlacComm
  3574. DESCRIPTION:
  3575. PRE-CONDITION:
  3576. INPUT:
  3577. OUTPUT:
  3578. GLOBAL VARIABLES:
  3579. =============================================================================*/
  3580. int SlacComm()
  3581. {
  3582. static unsigned char qca7k_comm_retry = 0;
  3583. double t_diff = 0;
  3584. int packet_size = 0;
  3585. int count = 0;
  3586. //int idx = 0;
  3587. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  3588. unsigned char tmpBuf[2048]={0};
  3589. #endif
  3590. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  3591. unsigned char *EvMac_in;
  3592. unsigned char *RunID_in;
  3593. int i = 0;
  3594. #endif
  3595. if(RawSock >= 0)
  3596. {
  3597. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  3598. packet_size = recvfrom(RawSock, V2gtpMsgRxBuf, V2GTP_MSG_RX_BUFFER_SIZE, 0, NULL, NULL);
  3599. if(packet_size > 0)
  3600. {
  3601. /*#ifdef Debug
  3602. DEBUG_PRINTF_EVCOMM_DETAIL("Raw Data: ");
  3603. for(count=0;count<packet_size;count++)
  3604. DEBUG_PRINTF_EVCOMM_DETAIL("0x%x, ",V2gtpMsgRxBuf[count]);
  3605. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3606. #endif*/
  3607. MmeProcess(V2gtpMsgRxBuf, packet_size);
  3608. }
  3609. }
  3610. switch(Check_V2G_Flow_Status())
  3611. {
  3612. case IDLE:
  3613. {
  3614. if(RawSock < 0)
  3615. {
  3616. RawSock = socket(PF_PACKET, SOCK_RAW, htons(EtherType_HomePlug));
  3617. sprintf((char*)buf_log_evcomm, "[RawSock]opened(%d)", RawSock);
  3618. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3619. if(RawSock == -1)
  3620. {
  3621. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SlacComm:Failed to create socket");
  3622. Update_V2G_Flow_Status(Other_Fault);
  3623. return -1;
  3624. }
  3625. if (setsockopt(RawSock, SOL_SOCKET, SO_BINDTODEVICE, QcaInterface, 4) < 0)
  3626. {
  3627. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SlacComm:Set SO_BINDTODEVICE NG");
  3628. Update_V2G_Flow_Status(Other_Fault);
  3629. return -1;
  3630. }
  3631. struct timeval tv;
  3632. tv.tv_sec = 0;
  3633. tv.tv_usec = 100000; //100ms (Rx timeout)
  3634. if (setsockopt(RawSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  3635. {
  3636. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SlacComm:Set SO_RCVTIMEO NG");
  3637. Update_V2G_Flow_Status(Other_Fault);
  3638. return -1;
  3639. }
  3640. tv.tv_usec = 100000; //100ms (Tx timeout)
  3641. if (setsockopt(RawSock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  3642. {
  3643. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SlacComm:Set SO_SNDTIMEO NG");
  3644. Update_V2G_Flow_Status(Other_Fault);
  3645. return -1;
  3646. }
  3647. memset(&Req, 0, sizeof(struct ifreq));
  3648. strcpy( (char*)Req.ifr_name, QcaInterface);
  3649. if (ioctl(RawSock, SIOCGIFINDEX, &Req) < 0)
  3650. {
  3651. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SlacComm: ioctl NG");
  3652. Update_V2G_Flow_Status(Other_Fault);
  3653. return -1;
  3654. }
  3655. memset( &DestSocketAddress, 0, sizeof(struct sockaddr_ll));
  3656. DestSocketAddress.sll_ifindex = Req.ifr_ifindex;
  3657. DestSocketAddress.sll_halen = ETH_ALEN;
  3658. PwmStartTime = 0;
  3659. EVCOMM_SYS_INFO.QCA7K_SetKeyDone = FALSE;
  3660. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[QCA7K]connecting...");
  3661. //Get QCA7K MAC address
  3662. GetQca7kMac();
  3663. ftime(&SeqStartTime);
  3664. break;
  3665. }
  3666. else //RawSock: opened
  3667. {
  3668. if(EVCOMM_SYS_INFO.QCA7K_SetKeyDone == FALSE)
  3669. {
  3670. ftime(&SeqEndTime);
  3671. t_diff = DiffTimeb(SeqStartTime, SeqEndTime);
  3672. if (t_diff > V2G_SECC_QCA7000_GET_MAC_ADDR_REQ_RETRY_PERIOD) //3 secs
  3673. {
  3674. qca7k_comm_retry++;
  3675. sprintf((char*)buf_log_evcomm,
  3676. "Re-try connecting...(%.02lf/%dms)",
  3677. t_diff,
  3678. V2G_SECC_QCA7000_GET_MAC_ADDR_REQ_RETRY_PERIOD);
  3679. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3680. GetQca7kMac(); //re-send req
  3681. ftime(&SeqStartTime);
  3682. break;
  3683. }
  3684. else
  3685. {
  3686. //null
  3687. }
  3688. //Retry by 3 times
  3689. if (qca7k_comm_retry >= 3)
  3690. {
  3691. sprintf((char*)buf_log_evcomm,
  3692. "[Error]Comm: fail (retry by %d times)",
  3693. qca7k_comm_retry);
  3694. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3695. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3696. //CCS_SECCC_TIMEOUT_QCA7000_COMM (023892): The firmware code of QCA7000 may not be installed, yet
  3697. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3698. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3699. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3700. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3701. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  3702. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  3703. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3704. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3705. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  3706. Update_V2G_Flow_Status(Sequence_Timeout);
  3707. qca7k_comm_retry = 0;
  3708. break;
  3709. }
  3710. }
  3711. else //RawSock: opened; Set Key: DONE
  3712. {
  3713. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  3714. if((CheckConnectorPlugIn() == TRUE) && (ShmInternalComm->ChargingPermission >= 1))
  3715. {
  3716. if(PwmStartTime <= 0)
  3717. {
  3718. //Sniffer_Tcpdump(ENABLE);
  3719. //#if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  3720. //sleep(1); //wait for tcpdump to be ready.
  3721. //#endif
  3722. SwitchCpStateE(DISABLE);
  3723. OutputCpPwmDuty(5);
  3724. PwmStartTime = time(NULL);
  3725. }
  3726. else
  3727. {
  3728. if((time(NULL) - PwmStartTime) > TT_EVSE_SLAC_init)
  3729. {
  3730. sprintf((char*)buf_log_evcomm,
  3731. "SlacComm: Wait CM_SLAC_PARM_REQ Timeout - TT_EVSE_SLAC_init, NowTime(%ld)-PwmStartTime(%d)>%d (sec)",
  3732. time(NULL),
  3733. PwmStartTime,
  3734. TT_EVSE_SLAC_init);
  3735. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3736. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3737. //CCS_SECC_TIMEOUT_SLAC_TT_EVSE_SLAC_init (023809)
  3738. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3739. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3740. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3741. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3742. ShmStatusCodeData->PresentStatusCode[0][4] = 0;
  3743. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  3744. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  3745. Update_V2G_Flow_Status(Sequence_Timeout);
  3746. PwmStartTime = 0;
  3747. return -1;
  3748. }
  3749. else
  3750. {
  3751. //waiting for CM_SLAC_PARM_REQ
  3752. }
  3753. }
  3754. }
  3755. else
  3756. {
  3757. PwmStartTime = 0;
  3758. }
  3759. }
  3760. }
  3761. break;
  3762. }
  3763. case CM_SET_KEY_REQ: //13
  3764. {
  3765. //CM_SET_KEY_REQ
  3766. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[QCA7K][Rx]CM_SET_KEY_REQ");
  3767. ftime(&SeqEndTime);
  3768. t_diff = DiffTimeb(SeqStartTime, SeqEndTime);
  3769. if (t_diff > V2G_SECC_QCA7000_COMM_TIMEOUT) //10 seconds
  3770. {
  3771. sprintf((char*)buf_log_evcomm,
  3772. "[QCA7K]Failed on SetKey => End_Process (%.02lf/%dms)",
  3773. t_diff,
  3774. V2G_SECC_QCA7000_COMM_TIMEOUT);
  3775. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3776. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3777. //CCS_SECCC_TIMEOUT_QCA7000_COMM (023892): The firmware code of QCA7000 may not be installed, yet
  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] = 9;
  3783. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  3784. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3785. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3786. Update_V2G_Flow_Status(Sequence_Timeout);
  3787. }
  3788. else if (t_diff > V2G_SECC_QCA7000_SEND_SET_KEY_PERIOD) //2 seconds
  3789. {
  3790. sprintf((char*)buf_log_evcomm,
  3791. "[QCA7K]SetKey: proceed (%.02lf/%dms)",
  3792. t_diff,
  3793. V2G_SECC_QCA7000_SEND_SET_KEY_PERIOD);
  3794. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3795. SendSetKey();
  3796. ftime(&SeqStartTime);
  3797. }
  3798. else
  3799. {
  3800. //null
  3801. }
  3802. break;
  3803. }
  3804. case CM_SET_KEY_CNF: //14
  3805. {
  3806. sprintf((char*)buf_log_evcomm,
  3807. "[SECCReady]Wait: plugin(%d), matached(%d), permission(%d)...",
  3808. CheckConnectorPlugIn(),
  3809. CSUCOMMDC_TASK_FLAG.matched,
  3810. ShmInternalComm->ChargingPermission
  3811. );
  3812. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3813. EVCOMM_SYS_INFO.QCA7K_SetKeyDone = TRUE;
  3814. PwmStartTime = 0;
  3815. Update_V2G_Flow_Status(IDLE);
  3816. break;
  3817. }
  3818. case CM_SLAC_PARM_CONF:
  3819. {
  3820. ftime(&SeqEndTime);
  3821. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_sequence)
  3822. {
  3823. sprintf((char*)buf_log_evcomm,
  3824. "SlacComm: Wait CM_START_ATTEN_CHAR_IND Timeout - TT_match_sequence (%.02lf of %dms)",
  3825. DiffTimeb(SeqStartTime, SeqEndTime),
  3826. TT_match_sequence);
  3827. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3828. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3829. //CCS_SECC_TIMEOUT_CM_START_ATTEN_CHAR_IND (023811):
  3830. //[Possible Reason] CCS Board image might not be updated properly. The 2nd reboot process is not finished.
  3831. //[Verification Method] By tying reboot command to the terminal and see if it could be executed immediately.
  3832. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3833. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3834. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3835. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3836. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  3837. ShmStatusCodeData->PresentStatusCode[0][5] = 1;
  3838. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3839. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3840. Update_V2G_Flow_Status(Sequence_Timeout);
  3841. return -1;
  3842. }
  3843. break;
  3844. }
  3845. case CM_START_ATTEN_CHAR_IND:
  3846. {
  3847. ftime(&SeqEndTime);
  3848. if(DiffTimeb(SeqStartTime, SeqEndTime) > (TP_EV_batch_msg_interval)) //one more time interval for tolerance
  3849. {
  3850. sprintf((char*)buf_log_evcomm,
  3851. "SlacComm: Wait CM_MNBC_SOUND_IND Timeout - TP_EV_batch_msg_interval (%.02lf of %dms)",
  3852. DiffTimeb(SeqStartTime, SeqEndTime),
  3853. TP_EV_batch_msg_interval);
  3854. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3855. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3856. //CCS_SECC_TIMEOUT_SLAC_CM_MNBC_SOUND_IND (023818):
  3857. //[Possible Reason] CCS Board image might not be updated properly. The 2nd reboot process is not finished.
  3858. //[Verification Method] By tying reboot command to the terminal and see if it could be executed immediately.
  3859. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3860. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3861. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3862. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3863. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  3864. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  3865. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3866. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3867. Update_V2G_Flow_Status(Sequence_Timeout);
  3868. return -1;
  3869. }
  3870. break;
  3871. }
  3872. case CM_MNBC_SOUND_IND:
  3873. {
  3874. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  3875. {
  3876. ftime(&SeqEndTime);
  3877. t_diff = DiffTimeb(SeqStartTime, SeqEndTime);
  3878. //printf("time:%.02lf, profilNum:%d\n",DiffTimeb(SeqStartTime, SeqEndTime),AttenProfileCnt); //added by Vern
  3879. if(t_diff > TT_EVSE_match_MNBC ||
  3880. (SLAC_INFO.array[0].AttenProfileCnt >= SLAC_INFO.array[0].MnbcSoundNum) ||
  3881. (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0)) //TC_SECC_VTB_AttenuationCharacterization_003
  3882. {
  3883. //Wait for other SLAC Req sets
  3884. if ((SLAC_INFO.arrayLen >= 2) &&
  3885. (t_diff < TT_EVSE_match_MNBC) &&
  3886. (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry == 0)) //not a retry
  3887. {
  3888. break;
  3889. }
  3890. //Check if it is a timeup response
  3891. if (t_diff > TT_EVSE_match_MNBC)
  3892. {
  3893. sprintf((char*)buf_log_evcomm,
  3894. "[WARNING][SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: timeup(%.2f/%dms) => send",
  3895. (i + 1),
  3896. t_diff,
  3897. TT_EVSE_match_MNBC);
  3898. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3899. }
  3900. //Sending all CM_ATTEN_CHAR_IND according to all corresponding SLAC Req sets
  3901. for (i = 0; i < SLAC_INFO.arrayLen; i++)
  3902. {
  3903. if ((SLAC_INFO.array[i].AttenProfileCnt == 0) ||
  3904. (SLAC_INFO.array[i].AagGroupsNum == 0) ||
  3905. (SLAC_INFO.array[i].StartAttenCharCnt == 0) ||
  3906. (SLAC_INFO.array[i].MnbcSoundNum != 10) || //received in CM_START_ATTEN_CHAR_IND
  3907. (SLAC_INFO.array[i].StartAttenCharErr == TRUE)
  3908. )
  3909. {
  3910. //Ignoring those SLAC request sets without sending CM_MNBC_SOUND_IND(CM_ATTEN_PROFILE_IND)
  3911. sprintf((char*)buf_log_evcomm,
  3912. "[WARNING][SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: para err(%d,%d,%d,%d,%d) => canceled",
  3913. (i + 1),
  3914. SLAC_INFO.array[i].AttenProfileCnt,
  3915. SLAC_INFO.array[i].AagGroupsNum,
  3916. SLAC_INFO.array[i].StartAttenCharCnt,
  3917. SLAC_INFO.array[i].MnbcSoundNum,
  3918. SLAC_INFO.array[i].StartAttenCharErr);
  3919. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3920. continue;
  3921. }
  3922. //In CM_ATTEN_CHAR_IND retry process, here only re-send this message according to the 1st coming SLAC request
  3923. if ((EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0) && (i != 0))
  3924. {
  3925. sprintf((char*)buf_log_evcomm,
  3926. "[NOTE][SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: canceled",
  3927. (i + 1));
  3928. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3929. break;
  3930. }
  3931. EvMac_in = SLAC_INFO.array[i].EvMac;
  3932. RunID_in = SLAC_INFO.array[i].RunID;
  3933. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  3934. memcpy(SendMmePacket.ODA, EvMac_in, SLAC_EVMAC_LENGTH);
  3935. memcpy(SendMmePacket.OSA, CsuMac, 6);
  3936. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  3937. SendMmePacket.MMV = 0x01;
  3938. SendMmePacket.MMTYPE = MMTYPE_CM_ATTEN_CHAR_IND;
  3939. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  3940. SendMmePacketSize = 0;
  3941. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00: EV-EVSE Matching)
  3942. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00: No Security)
  3943. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac_in, SLAC_EVMAC_LENGTH); //SOURCE_ADDRESS, MAC address of the EV Host
  3944. SendMmePacketSize += SLAC_EVMAC_LENGTH;
  3945. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, RunID_in, SLAC_RUNID_LENGTH);
  3946. SendMmePacketSize += SLAC_RUNID_LENGTH;
  3947. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //SOURCE_ID(0x00)
  3948. SendMmePacketSize += 17;
  3949. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //RESP_ID(0x00)
  3950. SendMmePacketSize += 17;
  3951. SendMmePacket.MMENTRY[SendMmePacketSize++] = SLAC_INFO.array[i].AttenProfileCnt; //NumSounds: Number of M-Sounds used for generation of the ATTEN_PROFILE
  3952. SendMmePacket.MMENTRY[SendMmePacketSize++] = SLAC_INFO.array[i].AagGroupsNum; //NumGroups
  3953. for(count = 0; count < SLAC_INFO.array[i].AagGroupsNum; count++)
  3954. {
  3955. unsigned char TmpAag;
  3956. TmpAag = ((SLAC_INFO.array[i].AAG[count] / SLAC_INFO.array[i].AttenProfileCnt) & 0xFF);
  3957. SLAC_INFO.array[i].AAG_quality_ori += (float) TmpAag; //original signal quality
  3958. #if (SUDO_PSD_PARAMETER_MECHANISM == ENABLE) //default: ENABLE
  3959. #if 1
  3960. //TC_SECC_VTB_AttenuationCharacterization_019
  3961. TmpAag = TmpAag >> 1; //new method proposed by Joseph (divided by 2)
  3962. #else
  3963. if(TmpAag >= 39) //original method proposed by Vern
  3964. {
  3965. TmpAag = 37;
  3966. }
  3967. #endif
  3968. #endif
  3969. SendMmePacket.MMENTRY[SendMmePacketSize++] = TmpAag;
  3970. SLAC_INFO.array[i].AAG_quality_refined += (float) TmpAag; //refined signal quality
  3971. }
  3972. SendMmePacketSize += 19; //the size before MMENTRY
  3973. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  3974. {
  3975. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Send MME Packet *****\n");
  3976. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3977. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  3978. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3979. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  3980. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  3981. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  3982. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  3983. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  3984. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_ATTEN_CHAR_IND ---\n");
  3985. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  3986. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  3987. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
  3988. SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5],
  3989. SendMmePacket.MMENTRY[6],SendMmePacket.MMENTRY[7]);
  3990. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  3991. SendMmePacket.MMENTRY[8],SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],
  3992. SendMmePacket.MMENTRY[12],SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14],SendMmePacket.MMENTRY[15]);
  3993. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  3994. for(count=0; count<17; count++)
  3995. {
  3996. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, SendMmePacket.MMENTRY[16+count]);
  3997. }
  3998. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ID: %s\n", tmpBuf);
  3999. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  4000. for(count=0; count<17; count++)
  4001. {
  4002. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, SendMmePacket.MMENTRY[33+count]);
  4003. }
  4004. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_ID: %s\n", tmpBuf);
  4005. DEBUG_PRINTF_EVCOMM_DETAIL("NumSounds: 0x%x\n", SendMmePacket.MMENTRY[50]);
  4006. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  4007. for(count=0; count<AagGroupsNum; count++)
  4008. {
  4009. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, SendMmePacket.MMENTRY[51+count]);
  4010. }
  4011. DEBUG_PRINTF_EVCOMM_DETAIL("ATTEN_PROFILE: %s\n", tmpBuf);
  4012. }
  4013. #endif
  4014. count = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  4015. SLAC_INFO.array[i].AAG_quality_ori /= SLAC_INFO.array[i].AagGroupsNum;
  4016. SLAC_INFO.array[i].AAG_quality_refined /= SLAC_INFO.array[i].AagGroupsNum;
  4017. //Print log if this CM_ATTEN_CHAR_IND belongs to RETRY Tx message.
  4018. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0)
  4019. {
  4020. sprintf((char*)buf_log_evcomm,
  4021. "[SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: %d-th resend (Q=%.2f/%.2f)(%d/%d)",
  4022. (i + 1),
  4023. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry,
  4024. SLAC_INFO.array[i].AAG_quality_refined,
  4025. SLAC_INFO.array[i].AAG_quality_ori,
  4026. SLAC_INFO.array[i].AttenProfileCnt,
  4027. SLAC_INFO.array[i].MnbcSoundNum);
  4028. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4029. }
  4030. else if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry == 0)
  4031. {
  4032. sprintf((char*)buf_log_evcomm,
  4033. "[SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: Q=%.2f/%.2f(%d/%d)",
  4034. (i + 1),
  4035. SLAC_INFO.array[i].AAG_quality_refined,
  4036. SLAC_INFO.array[i].AAG_quality_ori,
  4037. SLAC_INFO.array[i].AttenProfileCnt,
  4038. SLAC_INFO.array[i].MnbcSoundNum);
  4039. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4040. }
  4041. else
  4042. {
  4043. sprintf((char*)buf_log_evcomm,
  4044. "[SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: unexpected CM_ATTEN_CHAR_IND_retry(%d))",
  4045. i,
  4046. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry);
  4047. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4048. }
  4049. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n", SendMmePacketSize,count);
  4050. } //end of for loop
  4051. Update_V2G_Flow_Status(CM_ATTEN_CHAR_IND);
  4052. ftime(&SeqStartTime);
  4053. }
  4054. break;
  4055. }
  4056. #else
  4057. {
  4058. ftime(&SeqEndTime);
  4059. //printf("time:%.02lf, profilNum:%d\n",DiffTimeb(SeqStartTime, SeqEndTime),AttenProfileCnt); //added by Vern
  4060. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_EVSE_match_MNBC || (AttenProfileCnt >= MnbcSoundNum) || (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0))
  4061. {
  4062. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  4063. memcpy(SendMmePacket.ODA, EvMac, 6);
  4064. memcpy(SendMmePacket.OSA, CsuMac, 6);
  4065. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  4066. SendMmePacket.MMV = 0x01;
  4067. SendMmePacket.MMTYPE = MMTYPE_CM_ATTEN_CHAR_IND;
  4068. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  4069. SendMmePacketSize = 0;
  4070. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00: EV-EVSE Matching)
  4071. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00: No Security)
  4072. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac, 6); //SOURCE_ADDRESS, MAC address of the EV Host
  4073. SendMmePacketSize += 6;
  4074. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize, SlacRunId, sizeof(SlacRunId));
  4075. SendMmePacketSize += sizeof(SlacRunId);
  4076. memset(SendMmePacket.MMENTRY+SendMmePacketSize, 0, 17); //SOURCE_ID
  4077. SendMmePacketSize += 17;
  4078. memset(SendMmePacket.MMENTRY+SendMmePacketSize, 0, 17); //RESP_ID
  4079. SendMmePacketSize += 17;
  4080. SendMmePacket.MMENTRY[SendMmePacketSize++] = AttenProfileCnt; //NumSounds
  4081. SendMmePacket.MMENTRY[SendMmePacketSize++] = AagGroupsNum; //NumGroups
  4082. for(count=0; count < AagGroupsNum; count++)
  4083. {
  4084. unsigned char TmpAag;
  4085. TmpAag = ((Aag[count] / AttenProfileCnt) & 0xFF);
  4086. #if (SUDO_PSD_PARAMETER_MECHANISM == ENABLE) //default: ENABLE
  4087. #if 1
  4088. //TC_SECC_VTB_AttenuationCharacterization_019
  4089. TmpAag = TmpAag >> 1; //new method proposed by Joseph (divided by 2)
  4090. #else
  4091. if(TmpAag >= 39) //original method proposed by Vern
  4092. {
  4093. /*
  4094. // [To-do] If this statement is enabled, SLAC will fail due to timeout.
  4095. {
  4096. unsigned char TmpBuf[64];
  4097. memset(TmpBuf,0,sizeof(TmpBuf));
  4098. sprintf((char*)TmpBuf,"SlacComm: bad Aag[%d]=%d",count,TmpAag);
  4099. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", TmpBuf);
  4100. }
  4101. */
  4102. printf("%d,", TmpAag);
  4103. TmpAag = 37;
  4104. }
  4105. #endif
  4106. #endif
  4107. SendMmePacket.MMENTRY[SendMmePacketSize++] = TmpAag;
  4108. }
  4109. SendMmePacketSize += 19; //the size before MMENTRY
  4110. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  4111. {
  4112. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Send MME Packet *****\n");
  4113. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  4114. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  4115. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  4116. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  4117. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  4118. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  4119. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  4120. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  4121. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_ATTEN_CHAR_IND ---\n");
  4122. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  4123. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  4124. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
  4125. SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5],
  4126. SendMmePacket.MMENTRY[6],SendMmePacket.MMENTRY[7]);
  4127. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  4128. SendMmePacket.MMENTRY[8],SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],
  4129. SendMmePacket.MMENTRY[12],SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14],SendMmePacket.MMENTRY[15]);
  4130. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  4131. for(count=0; count<17; count++)
  4132. {
  4133. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, SendMmePacket.MMENTRY[16+count]);
  4134. }
  4135. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ID: %s\n", tmpBuf);
  4136. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));;
  4137. for(count=0; count<17; count++)
  4138. {
  4139. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, SendMmePacket.MMENTRY[33+count]);
  4140. }
  4141. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_ID: %s\n", tmpBuf);
  4142. DEBUG_PRINTF_EVCOMM_DETAIL("NumSounds: 0x%x\n", SendMmePacket.MMENTRY[50]);
  4143. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  4144. for(count=0; count<AagGroupsNum; count++)
  4145. {
  4146. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, SendMmePacket.MMENTRY[51+count]);
  4147. }
  4148. DEBUG_PRINTF_EVCOMM_DETAIL("ATTEN_PROFILE: %s\n", tmpBuf);
  4149. }
  4150. #endif
  4151. Update_V2G_Flow_Status(CM_ATTEN_CHAR_IND);
  4152. count = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  4153. //Print log if this CM_ATTEN_CHAR_IND belongs to RETRY Tx message.
  4154. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0)
  4155. {
  4156. sprintf((char*)buf_log_evcomm,
  4157. "[SLAC][Tx]CM_ATTEN_CHAR_IND: resend(%d-th)",
  4158. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry);
  4159. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4160. }
  4161. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n", SendMmePacketSize,count);
  4162. ftime(&SeqStartTime);
  4163. }
  4164. break;
  4165. }
  4166. #endif
  4167. }
  4168. case CM_ATTEN_CHAR_IND:
  4169. {
  4170. ftime(&SeqEndTime);
  4171. //if(DiffTimeb(SeqStartTime, SeqEndTime)>TT_match_response)
  4172. 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.
  4173. {
  4174. sprintf((char*)buf_log_evcomm,
  4175. "SlacComm: Wait CM_ATTEN_CHAR_RSP Timeout - TT_match_response (%.02lf of %dms)",
  4176. DiffTimeb(SeqStartTime, SeqEndTime),
  4177. TT_match_response);
  4178. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4179. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4180. //CCS_SECC_TIMEOUT_SLAC_CM_ATTEN_CHAR_RSP (023814):
  4181. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  4182. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4183. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4184. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4185. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  4186. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  4187. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  4188. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  4189. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4190. Update_V2G_Flow_Status(Sequence_Timeout);
  4191. return -1;
  4192. }
  4193. break;
  4194. }
  4195. case CM_ATTEN_CHAR_RSP:
  4196. {
  4197. ftime(&SeqEndTime);
  4198. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_EVSE_match_session)
  4199. {
  4200. sprintf((char*)buf_log_evcomm,
  4201. "SlacComm: Wait CM_VALIDATE_REQ_1ST or CM_SLAC_MATCH_REQ Timeout - TT_EVSE_match_session (%.02lf of %dms)",
  4202. DiffTimeb(SeqStartTime, SeqEndTime),
  4203. TT_EVSE_match_session);
  4204. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4205. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4206. //CCS_SECC_TIMEOUT_SLAC_CM_VALIDATE_REQ_1ST__CM_SLAC_MATCH_REQ (023815):
  4207. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  4208. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4209. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4210. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4211. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  4212. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  4213. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  4214. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  4215. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4216. Update_V2G_Flow_Status(Sequence_Timeout);
  4217. return -1;
  4218. }
  4219. break;
  4220. }
  4221. case CM_VALIDATE_CNF:
  4222. {
  4223. ftime(&SeqEndTime);
  4224. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_sequence)
  4225. {
  4226. sprintf((char*)buf_log_evcomm,
  4227. "SlacComm: Wait CM_VALIDATE_REQ_2ND or CM_SLAC_MATCH_REQ Timeout - TT_match_sequence (%.02lf of %dms)",
  4228. DiffTimeb(SeqStartTime, SeqEndTime),
  4229. TT_match_sequence);
  4230. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4231. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4232. //CCS_SECC_TIMEOUT_SLAC_CM_VALIDATE_REQ_2ND__CM_SLAC_MATCH_REQ (023819):
  4233. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  4234. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4235. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4236. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4237. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  4238. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  4239. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  4240. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  4241. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4242. Update_V2G_Flow_Status(Sequence_Timeout);
  4243. return -1;
  4244. }
  4245. break;
  4246. }
  4247. case CM_SLAC_MATCH_CNF:
  4248. {
  4249. if(UdpSock > 0)
  4250. {
  4251. close(UdpSock);
  4252. UdpSock = -1;
  4253. }
  4254. if(TcpSock > 0)
  4255. {
  4256. close(TcpSock);
  4257. TcpSock = -1;
  4258. }
  4259. ftime(&SeqStartTime);
  4260. V2gTcpConnected();
  4261. Update_V2G_Flow_Status(SLACC_SDP_UDP_Connection);
  4262. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SLAAC,SDP,UDP: connecting...");
  4263. break;
  4264. }
  4265. default:
  4266. {
  4267. break;
  4268. }
  4269. }
  4270. return 0;
  4271. }
  4272. /*===========================================================================
  4273. FUNCTION: V2gMsgDecoder
  4274. DESCRIPTION:
  4275. 1. Decode the V2GTP messages inside "msg" and save the decoded
  4276. messages in ccs_exi_doc_DIN, ccs_exi_doc_ISO1, and ccs_exi_doc_ISO2,
  4277. respectively.
  4278. 2. After decoding, V2gMsg_Process() could then use
  4279. ccs_exi_doc_DIN, ccs_exi_doc_ISO1, or ccs_exi_doc_ISO2
  4280. to deal with the corresponding Response messages, respectively.
  4281. PRE-CONDITION:
  4282. 1. msg_length > 0
  4283. INPUT:
  4284. 1. msg
  4285. 2. msg_length
  4286. OUTPUT:
  4287. 1. ccs_exi_doc_DIN //global variable
  4288. ccs_exi_doc_ISO1
  4289. ccs_exi_doc_ISO2
  4290. 2. v2g_state //Status Flag
  4291. //indicating the V2gMsg_Process_din to proceed
  4292. the next process.
  4293. 3. return value // < 0: ERROR
  4294. // > 0: Message Type
  4295. GLOBAL VARIABLES:
  4296. =============================================================================*/
  4297. int V2gMsgDecoder(unsigned char *msg, unsigned int msg_length, unsigned int v2g_state)
  4298. {
  4299. int errn = 0;
  4300. //Checking the minimum Header size requirement
  4301. if(msg_length < V2GTP_MSG_HEADER_LENGTH) //The minimum requirement should be 8-byte header.
  4302. {
  4303. errn = -1;
  4304. return errn;
  4305. }
  4306. //Decode the 1st V2GMSG: AppProtocol
  4307. if(v2g_state == SupportedAppProtocolRequest) //17
  4308. {
  4309. if ((errn = API_V2GMSG_EXI_Decoder_AppProtocol(msg, msg_length, &ccs_handshake)) < 0)
  4310. {
  4311. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR = %d (DEC)]V2gMsgDecoder: SupportedAppProtocolRequest()\n", errn);
  4312. }
  4313. else //decoded successfully.
  4314. {
  4315. //PRINT_XML_DOC_supportedAppProtocolReq(&ccs_handshake);
  4316. }
  4317. }
  4318. //Decode the subsequent V2GMSG (DIN 70121, ISO 15118-2, ISO 15118-20)
  4319. else if(v2g_state > SupportedAppProtocolRequest && v2g_state <= SessionStopResponse)
  4320. {
  4321. //Decoding according to its own protocol
  4322. switch (ShmCcsData->CommProtocol)
  4323. {
  4324. case V2GT_MSG_PROTOCOL_DIN70121: //0
  4325. {
  4326. //DIN
  4327. if((errn = API_V2GMSG_EXI_Decoder_DIN(msg, msg_length, &ccs_exi_doc_DIN)) < 0)
  4328. {
  4329. sprintf((char*)buf_log_evcomm,
  4330. "[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_DIN()",
  4331. errn);
  4332. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4333. }
  4334. break;
  4335. }
  4336. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  4337. {
  4338. //ISO1
  4339. if((errn = API_V2GMSG_EXI_Decoder_ISO1(msg, msg_length, &ccs_exi_doc_ISO1)) < 0)
  4340. {
  4341. sprintf((char*)buf_log_evcomm,
  4342. "[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_ISO1()",
  4343. errn);
  4344. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4345. }
  4346. break;
  4347. }
  4348. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  4349. {
  4350. //ISO2
  4351. if((errn = API_V2GMSG_EXI_Decoder_ISO2(msg, msg_length, &ccs_exi_doc_ISO2)) < 0)
  4352. {
  4353. sprintf((char*)buf_log_evcomm,
  4354. "[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_ISO2()",
  4355. errn);
  4356. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4357. }
  4358. break;
  4359. }
  4360. default:
  4361. break;
  4362. }
  4363. }
  4364. else
  4365. {
  4366. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR = %d (DEC)]V2gMsgDecoder: Unexpected v2g_state\n", errn);
  4367. errn = -1;
  4368. }
  4369. return errn;
  4370. }
  4371. /*===========================================================================
  4372. FUNCTION: encode_din_V2GTP_stream
  4373. DESCRIPTION:
  4374. PRE-CONDITION:
  4375. INPUT:
  4376. OUTPUT:
  4377. GLOBAL VARIABLES:
  4378. =============================================================================*/
  4379. int encode_din_V2GTP_stream(bitstream_t *v2g_tx_stream, struct dinEXIDocument *ccs_exi_doc_DIN)
  4380. {
  4381. int errn = 0;
  4382. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  4383. errn = encode_dinExiDocument(v2g_tx_stream, ccs_exi_doc_DIN);
  4384. if (errn == 0)
  4385. {
  4386. //successfully encoded
  4387. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  4388. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  4389. if (errn != 0)
  4390. {
  4391. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_din_V2GTP_stream]write_v2gtpHeader(): %d (DEC)\n", errn);
  4392. }
  4393. }
  4394. else
  4395. {
  4396. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_din_V2GTP_stream]encode_dinExiDocument(): %d (DEC)\n", errn);
  4397. }
  4398. return errn;
  4399. }
  4400. /*===========================================================================
  4401. FUNCTION: encode_iso1_V2GTP_stream
  4402. DESCRIPTION:
  4403. PRE-CONDITION:
  4404. INPUT:
  4405. OUTPUT:
  4406. GLOBAL VARIABLES:
  4407. =============================================================================*/
  4408. int encode_iso1_V2GTP_stream(bitstream_t *v2g_tx_stream, struct iso1EXIDocument *ccs_exi_doc_ISO1)
  4409. {
  4410. int errn = 0;
  4411. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  4412. errn = encode_iso1ExiDocument(v2g_tx_stream, ccs_exi_doc_ISO1);
  4413. if (errn == 0)
  4414. {
  4415. //successfully encoded
  4416. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  4417. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  4418. if (errn != 0)
  4419. {
  4420. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso1_V2GTP_stream]write_v2gtpHeader(): %d (DEC)\n", errn);
  4421. }
  4422. }
  4423. else
  4424. {
  4425. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso1_V2GTP_stream]encode_iso1ExiDocument(): %d (DEC)\n", errn);
  4426. }
  4427. return errn;
  4428. }
  4429. /*===========================================================================
  4430. FUNCTION: encode_iso2_V2GTP_stream
  4431. DESCRIPTION:
  4432. PRE-CONDITION:
  4433. INPUT:
  4434. OUTPUT:
  4435. GLOBAL VARIABLES:
  4436. =============================================================================*/
  4437. int encode_iso2_V2GTP_stream(bitstream_t *v2g_tx_stream, struct iso2EXIDocument *ccs_exi_doc_ISO2)
  4438. {
  4439. int errn = 0;
  4440. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  4441. errn = encode_iso2ExiDocument(v2g_tx_stream, ccs_exi_doc_ISO2);
  4442. if (errn == 0)
  4443. {
  4444. //successfully encoded
  4445. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  4446. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  4447. if (errn != 0)
  4448. {
  4449. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso2_V2GTP_stream]write_v2gtpHeader(): %d (DEC)\n", errn);
  4450. }
  4451. }
  4452. else
  4453. {
  4454. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso2_V2GTP_stream]encode_iso2ExiDocument(): %d (DEC)\n", errn);
  4455. }
  4456. return errn;
  4457. }
  4458. /*===========================================================================
  4459. FUNCTION: send_encoded_din_V2GTP_Stream
  4460. DESCRIPTION:
  4461. PRE-CONDITION:
  4462. INPUT:
  4463. OUTPUT:
  4464. GLOBAL VARIABLES:
  4465. =============================================================================*/
  4466. int send_encoded_din_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct dinEXIDocument *ccs_exi_doc_DIN)
  4467. {
  4468. int errn = 0;
  4469. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  4470. errn = encode_din_V2GTP_stream(v2g_tx_stream, ccs_exi_doc_DIN);
  4471. // STEP 2: =========== Send Response Packet ===========
  4472. int rtn = 0;
  4473. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  4474. if (rtn == v2g_tx_stream->size)
  4475. {
  4476. /*
  4477. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][send_encoded_din_V2GTP_Stream] Response Message is sent ( %d / %d ). (Bytes, DEC): OK\n",
  4478. rtn, v2g_tx_stream->size);
  4479. */
  4480. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  4481. }
  4482. else if (rtn >= 0)
  4483. {
  4484. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][send_encoded_din_V2GTP_Stream]Imcomplete Tx ( %d / %d ). (Bytes, DEC): FAIL\n", rtn, v2g_tx_stream->size);
  4485. }
  4486. else
  4487. {
  4488. errn = rtn;
  4489. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][send_encoded_din_V2GTP_Stream]send(): %d (DEC)\n", errn);
  4490. }
  4491. //STEP 3: ========= Reset V2G MSG Flags ==========
  4492. init_dinBodyType(&ccs_exi_doc_DIN->V2G_Message.Body);
  4493. return errn;
  4494. }
  4495. /*===========================================================================
  4496. FUNCTION: send_encoded_iso1_V2GTP_Stream
  4497. DESCRIPTION:
  4498. PRE-CONDITION:
  4499. INPUT:
  4500. OUTPUT:
  4501. GLOBAL VARIABLES:
  4502. =============================================================================*/
  4503. int send_encoded_iso1_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct iso1EXIDocument *ccs_exi_doc_ISO1)
  4504. {
  4505. int errn = 0;
  4506. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  4507. errn = encode_iso1_V2GTP_stream(v2g_tx_stream, ccs_exi_doc_ISO1);
  4508. // STEP 2: =========== Send Response Packet ===========
  4509. int rtn = 0;
  4510. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  4511. if (rtn == v2g_tx_stream->size)
  4512. {
  4513. /*
  4514. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][send_encoded_iso1_V2GTP_Stream] Response Message is sent ( %d / %d ). (Bytes, DEC): OK\n",
  4515. rtn, v2g_tx_stream->size);
  4516. */
  4517. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  4518. }
  4519. else if (rtn >= 0)
  4520. {
  4521. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][send_encoded_iso1_V2GTP_Stream]Imcomplete Tx ( %d / %d ). (Bytes, DEC): FAIL\n", rtn, v2g_tx_stream->size);
  4522. }
  4523. else
  4524. {
  4525. errn = rtn;
  4526. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][send_encoded_iso1_V2GTP_Stream]send(): %d (DEC)\n", errn);
  4527. }
  4528. //STEP 3: ========= Reset V2G MSG Flags ==========
  4529. init_iso1BodyType(&ccs_exi_doc_ISO1->V2G_Message.Body);
  4530. return errn;
  4531. }
  4532. /*===========================================================================
  4533. FUNCTION: send_encoded_iso2_V2GTP_Stream
  4534. DESCRIPTION:
  4535. PRE-CONDITION:
  4536. INPUT:
  4537. OUTPUT:
  4538. GLOBAL VARIABLES:
  4539. =============================================================================*/
  4540. int send_encoded_iso2_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct iso2EXIDocument *ccs_exi_doc_ISO2)
  4541. {
  4542. int errn = 0;
  4543. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  4544. errn = encode_iso2_V2GTP_stream(v2g_tx_stream, ccs_exi_doc_ISO2);
  4545. // STEP 2: =========== Send Response Packet ===========
  4546. int rtn = 0;
  4547. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  4548. if (rtn == v2g_tx_stream->size)
  4549. {
  4550. /*
  4551. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][send_encoded_iso2_V2GTP_Stream] Response Message is sent ( %d / %d ). (Bytes, DEC): OK\n",
  4552. rtn, v2g_tx_stream->size);
  4553. */
  4554. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  4555. }
  4556. else if (rtn >= 0)
  4557. {
  4558. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][send_encoded_iso2_V2GTP_Stream]Imcomplete Tx ( %d / %d ). (Bytes, DEC): FAIL\n",
  4559. rtn, v2g_tx_stream->size);
  4560. }
  4561. else
  4562. {
  4563. errn = rtn;
  4564. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][send_encoded_iso2_V2GTP_Stream]send(): %d (DEC)\n", errn);
  4565. }
  4566. //STEP 3: ========= Reset V2G MSG Flags ==========
  4567. init_iso2BodyType(&ccs_exi_doc_ISO2->V2G_Message.Body);
  4568. return errn;
  4569. }
  4570. /*===========================================================================
  4571. FUNCTION: Check_din_V2G_Rx_MSG_SessionID
  4572. DESCRIPTION:
  4573. PRE-CONDITION:
  4574. INPUT:
  4575. OUTPUT:
  4576. GLOBAL VARIABLES:
  4577. =============================================================================*/
  4578. int Check_din_V2G_Rx_MSG_SessionID(struct dinEXIDocument *exi_doc_DIN)
  4579. {
  4580. int i = 0;
  4581. int leng = 0;
  4582. int errn = 0;
  4583. leng = exi_doc_DIN->V2G_Message.Header.SessionID.bytesLen;
  4584. //Step 1: Check SessionID Length
  4585. if (leng != 8) //8-byte
  4586. {
  4587. sprintf((char*)buf_log_evcomm, "[ERROR]SessionID: incorrect length(%d)", leng);
  4588. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4589. errn = -1;
  4590. }
  4591. else
  4592. {
  4593. //Step 2-1: Check SessionID content
  4594. for (i = 0; i < leng; i++)
  4595. {
  4596. if (exi_doc_DIN->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  4597. {
  4598. errn = -2;
  4599. break;
  4600. }
  4601. }
  4602. }
  4603. //Step 2-2: Print Incorrect ID
  4604. if (errn == -2) //incorrect ID
  4605. {
  4606. sprintf((char*)buf_log_evcomm,
  4607. "[ERROR]SessionID: incorrect ID(RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)",
  4608. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[0],
  4609. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[1],
  4610. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[2],
  4611. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[3],
  4612. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[4],
  4613. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[5],
  4614. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[6],
  4615. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[7],
  4616. EVCOMM_SYS_INFO.SessionID[0],
  4617. EVCOMM_SYS_INFO.SessionID[1],
  4618. EVCOMM_SYS_INFO.SessionID[2],
  4619. EVCOMM_SYS_INFO.SessionID[3],
  4620. EVCOMM_SYS_INFO.SessionID[4],
  4621. EVCOMM_SYS_INFO.SessionID[5],
  4622. EVCOMM_SYS_INFO.SessionID[6],
  4623. EVCOMM_SYS_INFO.SessionID[7]
  4624. );
  4625. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4626. }
  4627. //Step 3: Correct SessionID for Res Message
  4628. if (errn != 0)
  4629. {
  4630. exi_doc_DIN->V2G_Message.Header.SessionID.bytesLen = 8;
  4631. memset(exi_doc_DIN->V2G_Message.Header.SessionID.bytes, 0, 8);
  4632. memcpy(exi_doc_DIN->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4633. }
  4634. return errn;
  4635. }
  4636. /*===========================================================================
  4637. FUNCTION: Check_iso1_V2G_Rx_MSG_SessionID
  4638. DESCRIPTION:
  4639. PRE-CONDITION:
  4640. INPUT:
  4641. OUTPUT:
  4642. GLOBAL VARIABLES:
  4643. =============================================================================*/
  4644. int Check_iso1_V2G_Rx_MSG_SessionID(struct iso1EXIDocument *exi_doc_ISO1)
  4645. {
  4646. int i = 0;
  4647. int leng = 0;
  4648. int errn = 0;
  4649. leng = exi_doc_ISO1->V2G_Message.Header.SessionID.bytesLen;
  4650. //Step 1: Check SessionID Length
  4651. if (leng != 8) //8-byte
  4652. {
  4653. sprintf((char*)buf_log_evcomm, "[ERROR]SessionID: incorrect length(%d)", leng);
  4654. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4655. errn = -1;
  4656. }
  4657. else
  4658. {
  4659. //Step 2-1: Check SessionID content
  4660. for (i = 0; i < leng; i++)
  4661. {
  4662. if (exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  4663. {
  4664. errn = -2;
  4665. break;
  4666. }
  4667. }
  4668. }
  4669. //Step 2-2: Print Incorrect ID
  4670. if (errn == -2) //incorrect ID
  4671. {
  4672. sprintf((char*)buf_log_evcomm,
  4673. "[ERROR]SessionID: incorrect ID(RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)",
  4674. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[0],
  4675. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[1],
  4676. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[2],
  4677. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[3],
  4678. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[4],
  4679. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[5],
  4680. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[6],
  4681. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[7],
  4682. EVCOMM_SYS_INFO.SessionID[0],
  4683. EVCOMM_SYS_INFO.SessionID[1],
  4684. EVCOMM_SYS_INFO.SessionID[2],
  4685. EVCOMM_SYS_INFO.SessionID[3],
  4686. EVCOMM_SYS_INFO.SessionID[4],
  4687. EVCOMM_SYS_INFO.SessionID[5],
  4688. EVCOMM_SYS_INFO.SessionID[6],
  4689. EVCOMM_SYS_INFO.SessionID[7]
  4690. );
  4691. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4692. }
  4693. //Step 3: Correct SessionID for Res Message
  4694. if (errn != 0)
  4695. {
  4696. exi_doc_ISO1->V2G_Message.Header.SessionID.bytesLen = 8;
  4697. memset(exi_doc_ISO1->V2G_Message.Header.SessionID.bytes, 0, 8);
  4698. memcpy(exi_doc_ISO1->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4699. }
  4700. return errn;
  4701. }
  4702. /*===========================================================================
  4703. FUNCTION: Check_iso2_V2G_Rx_MSG_SessionID
  4704. DESCRIPTION:
  4705. PRE-CONDITION:
  4706. INPUT:
  4707. OUTPUT:
  4708. GLOBAL VARIABLES:
  4709. =============================================================================*/
  4710. int Check_iso2_V2G_Rx_MSG_SessionID(struct iso2EXIDocument *exi_doc_ISO2)
  4711. {
  4712. int i = 0;
  4713. int leng = 0;
  4714. int errn = 0;
  4715. leng = exi_doc_ISO2->V2G_Message.Header.SessionID.bytesLen;
  4716. //Step 1: Check SessionID Length
  4717. if (leng != 8) //8-byte
  4718. {
  4719. sprintf((char*)buf_log_evcomm, "[ERROR]SessionID: incorrect length(%d)", leng);
  4720. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4721. errn = -1;
  4722. }
  4723. else
  4724. {
  4725. //Step 2-1: Check SessionID content
  4726. for (i = 0; i < leng; i++)
  4727. {
  4728. if (exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  4729. {
  4730. errn = -2;
  4731. break;
  4732. }
  4733. }
  4734. }
  4735. //Step 2-2: Print Incorrect ID
  4736. if (errn == -2) //incorrect ID
  4737. {
  4738. sprintf((char*)buf_log_evcomm,
  4739. "[ERROR]SessionID: incorrect ID(RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)",
  4740. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[0],
  4741. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[1],
  4742. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[2],
  4743. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[3],
  4744. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[4],
  4745. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[5],
  4746. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[6],
  4747. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[7],
  4748. EVCOMM_SYS_INFO.SessionID[0],
  4749. EVCOMM_SYS_INFO.SessionID[1],
  4750. EVCOMM_SYS_INFO.SessionID[2],
  4751. EVCOMM_SYS_INFO.SessionID[3],
  4752. EVCOMM_SYS_INFO.SessionID[4],
  4753. EVCOMM_SYS_INFO.SessionID[5],
  4754. EVCOMM_SYS_INFO.SessionID[6],
  4755. EVCOMM_SYS_INFO.SessionID[7]
  4756. );
  4757. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4758. }
  4759. //Step 3: Correct SessionID for Res Message
  4760. if (errn != 0)
  4761. {
  4762. exi_doc_ISO2->V2G_Message.Header.SessionID.bytesLen = 8;
  4763. memset(exi_doc_ISO2->V2G_Message.Header.SessionID.bytes, 0, 8);
  4764. memcpy(exi_doc_ISO2->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4765. }
  4766. return errn;
  4767. }
  4768. /*===========================================================================
  4769. FUNCTION: GetSchemaID_of_Protocol
  4770. DESCRIPTION:
  4771. 1. Get the SchemaID accroding to the input target
  4772. *V2GT_MSG_PROTOCOL_DIN70121: choose DIN 70121
  4773. *V2GT_MSG_PROTOCOL_ISO15118_2014: choose ISO 15118-2 (ed1)
  4774. *V2GT_MSG_PROTOCOL_ISO15118_2018: choose ISO 15118-20 (ed2)
  4775. *V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY: choose the one with the highest priority
  4776. 2. [To-do] Checking Major and Minor version
  4777. 3. The parsing method will not support those private protocols,
  4778. such as "usr:tesla...," since "tesla" is 5 bytes but "din" and "iso" are 3 bytes.
  4779. 4. [To-do] If the target is selected as DIN," but there is no DIN.
  4780. However, if EV and EVSE all support ISO, how to use ISO instead?
  4781. PRE-CONDITION:
  4782. INPUT:
  4783. 1. target: target protocol
  4784. OUTPUT:
  4785. 1. id: SchemaID of selected protocol by EVSE
  4786. 2. ShmCcsData->CommProtocol (selected protocol)
  4787. 3. SupportedAppProtocol_result
  4788. (1) appHandresponseCodeType_OK_SuccessfulNegotiation = 0,
  4789. (2) appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation = 1,
  4790. (3) appHandresponseCodeType_Failed_NoNegotiation = 2
  4791. GLOBAL VARIABLES:
  4792. 1. ccs_handshake
  4793. 2. ShmCcsData
  4794. =============================================================================*/
  4795. int GetSchemaID_of_Protocol(unsigned char target)
  4796. {
  4797. int i = 0;
  4798. int ii = 0;
  4799. int id = 0;
  4800. //unsigned char tmp = 0;
  4801. unsigned char pri = 20; //priority = 1(highest)~20(lowerest)
  4802. char num[10];
  4803. //struct CCS_ProtocolNamespacestructCharacters pro;
  4804. //Choose the 1st protocol as default.
  4805. //id = CCS_HANDSHAKE_PROTOCOLS.array[0].SchemaID;
  4806. id = -1;
  4807. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  4808. for(i = 0; i < CCS_HANDSHAKE_PROTOCOLS.arrayLen; i++)
  4809. {
  4810. //Checking for <ProtocolNamespace>urn:din:70121:2012:MsgDef</ProtocolNamespace>
  4811. //[To-Do] Ignoring the priority from EV and force DIN 70121 as our only option. (for temp)
  4812. //[CAUTION] The parsing method will not support those private protocols, such as "usr:tesla..."
  4813. num[0] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[8];
  4814. num[1] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[9];
  4815. num[2] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[10];
  4816. num[3] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[11];
  4817. num[4] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[12];
  4818. num[5] = '\0';
  4819. if (atoi(num) == 70121)
  4820. {
  4821. sprintf((char*)buf_log_evcomm,
  4822. "[GetSchemaID_of_Protocol]supported(%d-th/%d): DIN 70121(%d:v%d.%d;id=%d,pri=%d)",
  4823. (i+1),
  4824. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4825. atoi(num),
  4826. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4827. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4828. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4829. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4830. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4831. if (target == V2GT_MSG_PROTOCOL_DIN70121)
  4832. {
  4833. sprintf((char*)buf_log_evcomm,
  4834. "[GetSchemaID_of_Protocol]selected(%d-th/%d): DIN 70121(%d:v%d.%d;id=%d,pri=%d)",
  4835. (i+1),
  4836. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4837. atoi(num),
  4838. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4839. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4840. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4841. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4842. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4843. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == DIN_SPEC_70121_2012_VersionNumberMajor)
  4844. {
  4845. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == DIN_SPEC_70121_2012_VersionNumberMinor)
  4846. {
  4847. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  4848. }
  4849. else
  4850. { //[TC_SECC_VTB_SupportedAppProtocol_005]
  4851. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  4852. }
  4853. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4854. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  4855. return id;
  4856. }
  4857. else
  4858. {
  4859. //keep looking for the suitable protocol
  4860. }
  4861. }
  4862. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  4863. {
  4864. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  4865. {
  4866. ii = i;
  4867. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4868. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  4869. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  4870. }
  4871. else
  4872. {
  4873. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  4874. }
  4875. }
  4876. else
  4877. {
  4878. //null
  4879. }
  4880. }
  4881. else if (atoi(num) == 15118)
  4882. {
  4883. //urn:din:70121:2012:MsgDef
  4884. //urn:iso:15118:2:2013:MsgDef
  4885. memset(num, 0, sizeof(num));
  4886. num[0] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[16];
  4887. num[1] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[17];
  4888. num[2] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[18];
  4889. num[3] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[19];
  4890. num[4] = '\0';
  4891. if (atoi(num) < 2018 && atoi(num) >= 2010)
  4892. {
  4893. sprintf((char*)buf_log_evcomm,
  4894. "[GetSchemaID_of_Protocol]supported(%d-th/%d): ISO 15118-2(ed1,%d:v%d.%d;id=%d,pri=%d)",
  4895. (i+1),
  4896. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4897. atoi(num),
  4898. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4899. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4900. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4901. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4902. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4903. if (target == V2GT_MSG_PROTOCOL_ISO15118_2014)
  4904. {
  4905. sprintf((char*)buf_log_evcomm,
  4906. "[GetSchemaID_of_Protocol]selected(%d-th/%d): ISO 15118-2,ed1(%d:v%d.%d;id=%d,pri=%d)",
  4907. (i+1),
  4908. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4909. atoi(num),
  4910. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4911. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4912. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4913. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4914. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4915. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == ISO1_15118_2013_VersionNumberMajor)
  4916. {
  4917. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == ISO1_15118_2013_VersionNumberMinor)
  4918. {
  4919. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  4920. }
  4921. else
  4922. {
  4923. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  4924. }
  4925. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4926. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2014;
  4927. return id;
  4928. }
  4929. else
  4930. {
  4931. //keep looking for the suitable protocol
  4932. }
  4933. }
  4934. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  4935. {
  4936. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  4937. {
  4938. ii = i;
  4939. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4940. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  4941. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2014;
  4942. }
  4943. else
  4944. {
  4945. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  4946. }
  4947. }
  4948. else
  4949. {
  4950. //null
  4951. }
  4952. }
  4953. else if (atoi(num) >= 2018 && atoi(num) <= 2100) // >= 2018
  4954. {
  4955. sprintf((char*)buf_log_evcomm,
  4956. "[GetSchemaID_of_Protocol]supported(%d-th/%d): ISO 15118-20(ed2,%d:v%d.%d;id=%d,pri=%d)",
  4957. (i+1),
  4958. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4959. atoi(num),
  4960. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4961. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4962. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4963. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4964. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4965. if (target == V2GT_MSG_PROTOCOL_ISO15118_2018)
  4966. {
  4967. sprintf((char*)buf_log_evcomm,
  4968. "[GetSchemaID_of_Protocol]selected(%d-th/%d): ISO 15118-20,ed2(%d:v%d.%d;id=%d,pri=%d)",
  4969. (i+1),
  4970. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4971. atoi(num),
  4972. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4973. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4974. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4975. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4976. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4977. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == ISO2_15118_2018_VersionNumberMajor)
  4978. {
  4979. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == ISO2_15118_2018_VersionNumberMinor)
  4980. {
  4981. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  4982. }
  4983. else
  4984. {
  4985. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  4986. }
  4987. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4988. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2018;
  4989. return id;
  4990. }
  4991. else
  4992. {
  4993. //keep looking for the suitable protocol
  4994. }
  4995. }
  4996. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  4997. {
  4998. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  4999. {
  5000. ii = i;
  5001. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  5002. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  5003. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2018;
  5004. }
  5005. else
  5006. {
  5007. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  5008. }
  5009. }
  5010. else
  5011. {
  5012. //null
  5013. }
  5014. }
  5015. else
  5016. {
  5017. //Unexpected Year
  5018. sprintf((char*)buf_log_evcomm,
  5019. "[GetSchemaID_of_Protocol]unsupported(%d-th/%d): ISO 15118-X(unexpected year:%d,v%d.%d),id=%d,pri=%d",
  5020. (i+1),
  5021. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  5022. atoi(num),
  5023. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  5024. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  5025. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  5026. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  5027. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5028. //return -1;
  5029. }
  5030. }
  5031. else
  5032. {
  5033. sprintf((char*)buf_log_evcomm,
  5034. "[GetSchemaID_of_Protocol]unsupported protocol(%d-th/%d)(%d:v%d.%d;id=%d,pri=%d)",
  5035. (i+1),
  5036. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  5037. atoi(num),
  5038. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  5039. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  5040. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  5041. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  5042. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5043. //return -1;
  5044. }
  5045. }
  5046. //The final result of highest priority protocol
  5047. sprintf((char*)buf_log_evcomm,
  5048. "[GetSchemaID_of_Protocol]selected(%d-th/%d): pro=%d(0:DIN,1:ISO1,2:ISO2);id=%d,pri=%d",
  5049. (ii+1),
  5050. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  5051. ShmCcsData->CommProtocol,
  5052. id,
  5053. pri);
  5054. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5055. if (id < 0)
  5056. {
  5057. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_Failed_NoNegotiation;
  5058. }
  5059. return id;
  5060. }
  5061. /*===========================================================================
  5062. FUNCTION: Proc_supportedAppProtocolRes
  5063. DESCRIPTION:
  5064. PRE-CONDITION:
  5065. INPUT:
  5066. OUTPUT:
  5067. GLOBAL VARIABLES:
  5068. =============================================================================*/
  5069. int Proc_supportedAppProtocolRes(int AcceptFd)
  5070. {
  5071. int errn = 0;
  5072. bitstream_t v2g_tx_stream;
  5073. static struct ChargingInfoData *sys;
  5074. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5075. //STEP 1: =========== Setting the Response Message ===========
  5076. init_appHandEXIDocument(&ccs_handshake);
  5077. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_OK_SuccessfulNegotiation;
  5078. ccs_handshake.supportedAppProtocolRes_isUsed = 1u;
  5079. //select the 1st one as the default
  5080. ccs_handshake.supportedAppProtocolRes.SchemaID = CCS_HANDSHAKE_PROTOCOLS.array[0].SchemaID;
  5081. ccs_handshake.supportedAppProtocolRes.SchemaID_isUsed = 1u;
  5082. int id = 0;
  5083. /*+++ 20200808, vern, support both DIN and ISO +++*/
  5084. //id = GetSchemaID_of_Protocol(V2GT_MSG_PROTOCOL_PREFERENCE); //output: EVCOMM_SYS_INFO.SupportedAppProtocol_result
  5085. id = GetSchemaID_of_Protocol(V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY); //output: EVCOMM_SYS_INFO.SupportedAppProtocol_result
  5086. /*--- 20200808, vern, support both DIN and ISO ---*/
  5087. ccs_handshake.supportedAppProtocolRes.ResponseCode = EVCOMM_SYS_INFO.SupportedAppProtocol_result; //updating the response code
  5088. if (id < 0)
  5089. {
  5090. sprintf((char*)buf_log_evcomm,
  5091. "[Error]No avalible CCS protocol (id = %d, preference = %d)",
  5092. id,
  5093. V2GT_MSG_PROTOCOL_PREFERENCE);
  5094. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5095. }
  5096. else
  5097. {
  5098. //selected SchemaID
  5099. ccs_handshake.supportedAppProtocolRes.SchemaID = (unsigned char) id;
  5100. }
  5101. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  5102. {
  5103. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_supportedAppProtocolRes]EVSE_Shutdown => End_Process");
  5104. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5105. errn = -1;
  5106. }
  5107. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5108. {
  5109. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_supportedAppProtocolRes]EVSE_EmergencyShutdown => End_Process");
  5110. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5111. errn = -1;
  5112. }
  5113. else
  5114. {
  5115. //null
  5116. }
  5117. //Check for Permission Changing from TRUE to FALSE
  5118. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  5119. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  5120. ShmInternalComm->ChargingPermission == FALSE)
  5121. {
  5122. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][supportedAppProtocolRes]Permission OFF");
  5123. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5124. //errn = -1;
  5125. }
  5126. #if (CP_PROTECTION_MECHANISM == ENABLE)
  5127. {
  5128. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  5129. {
  5130. //Detect for CP State should be 9V with 5% PWM or 100%PWM (State B1, B2)
  5131. if (sys->CpState != 2 && sys->CpState != 3) //State B1, B2
  5132. {
  5133. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5134. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5135. Update_V2G_Flow_Status(Other_Fault);
  5136. sprintf((char*)buf_log_evcomm,
  5137. "[Proc_supportedAppProtocolRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  5138. sys->CpState,
  5139. sys->CpVoltage);
  5140. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5141. }
  5142. }
  5143. #else
  5144. {
  5145. //Detect for CP State should be 9V (State B)
  5146. if (sys->CpState != 3) //B2
  5147. {
  5148. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5149. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5150. Update_V2G_Flow_Status(Other_Fault);
  5151. sprintf((char*)buf_log_evcomm,
  5152. "[Proc_supportedAppProtocolRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  5153. sys->CpState,
  5154. sys->CpVoltage);
  5155. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5156. }
  5157. }
  5158. #endif
  5159. }
  5160. #endif
  5161. //STEP 2: =========== Encode into EXI ===========
  5162. if ((errn = API_V2GMSG_EXI_Encoder_AppProtocol(&ccs_handshake, &v2g_tx_stream)) !=0)
  5163. {
  5164. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Error]API_V2GMSG_EXI_Encoder_AppProtocol\n");
  5165. return errn;
  5166. }
  5167. //STEP 3: =========== Send Response Packet ===========
  5168. int Rtn = 0;
  5169. Rtn = send(AcceptFd, v2g_tx_stream.data, v2g_tx_stream.size, 0);
  5170. //Rtn = send(6, v2g_tx_stream.data, v2g_tx_stream.size, 0);
  5171. DEBUG_PRINTF_EVCOMM_DETAIL("Send V2GTP Packet Size = %d, Rtn = %d (Bytes, DEC)\n", v2g_tx_stream.size, Rtn);
  5172. if (Rtn < 0)
  5173. {
  5174. return Rtn;
  5175. }
  5176. //STEP 4: =========== Save into Share Memory =========
  5177. //[Joseph, To-Do] Adding a mechanism to choose DIN 70121 as our 1st priority. (for multiple protocols)
  5178. //STEP 5: =========== Updating the Flow State Flag =========
  5179. if (id < 0)
  5180. {
  5181. errn = -1;
  5182. }
  5183. //STEP 6: =========== Reset Flags ============
  5184. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  5185. init_appHandEXIDocument(&ccs_handshake);
  5186. return errn;
  5187. }
  5188. /*===========================================================================
  5189. FUNCTION: Proc_supportedAppProtocolReq
  5190. DESCRIPTION:
  5191. PRE-CONDITION:
  5192. INPUT:
  5193. OUTPUT:
  5194. GLOBAL VARIABLES:
  5195. =============================================================================*/
  5196. int Proc_supportedAppProtocolReq(int AcceptFd)
  5197. {
  5198. //[Joseph, To-Do] analysis on Req message and choose the prefered protocol
  5199. //Default: DIN 70121 (find SchemaID)
  5200. int errn = 0;
  5201. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]supportedAppProtocolReq");
  5202. SHM_Save_din_supportedAppProtocolReq(ShmCcsData, &ccs_handshake, ShmSysConfigAndInfo);
  5203. errn = Proc_supportedAppProtocolRes(AcceptFd);
  5204. if (errn == 0)
  5205. {
  5206. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Tx]supportedAppProtocolRes");
  5207. }
  5208. else
  5209. {
  5210. DEBUG_PRINTF_EVCOMM_DETAIL("[Error]Proc_supportedAppProtocolRes(): %d (DEC)\n", errn);
  5211. }
  5212. return errn;
  5213. }
  5214. /*===========================================================================
  5215. FUNCTION: Proc_din_SessionSetupRes
  5216. DESCRIPTION:
  5217. PRE-CONDITION:
  5218. INPUT:
  5219. OUTPUT:
  5220. GLOBAL VARIABLES:
  5221. =============================================================================*/
  5222. int Proc_din_SessionSetupRes(int AcceptFd)
  5223. {
  5224. //int i = 0;
  5225. int errn = 0;
  5226. bitstream_t v2g_tx_stream;
  5227. static struct ChargingInfoData *sys;
  5228. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5229. size_t pos = 0;
  5230. v2g_tx_stream.pos = &pos;
  5231. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5232. v2g_tx_stream.data = V2GTP_Tx_buf;
  5233. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  5234. init_dinSessionSetupResType(&ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes);
  5235. // ====== [BODY (1/2) ResponseCode ======
  5236. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  5237. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_OK_NewSessionEstablished;
  5238. //[HEADER] Assign Res SessionID
  5239. ccs_exi_doc_DIN.V2G_Message.Header.SessionID.bytesLen = 8;
  5240. memset(ccs_exi_doc_DIN.V2G_Message.Header.SessionID.bytes, 0, 8);
  5241. memcpy(ccs_exi_doc_DIN.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  5242. //Check for SequenceError
  5243. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5244. {
  5245. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError;
  5246. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5247. errn = -1;
  5248. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5249. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5250. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5251. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5252. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5253. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5254. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5255. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5256. }
  5257. //#if PARAMETER_NORMAL_MODE == ENABLE
  5258. //SHM_Read_din_V2GMSG_Header(&ccs_exi_doc_DIN, ShmCcsData);
  5259. //#endif
  5260. //Detect for CP State should be 9V (State B)
  5261. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5262. {
  5263. #if CP_PROTECTION_MECHANISM == ENABLE
  5264. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  5265. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5266. Update_V2G_Flow_Status(Other_Fault);
  5267. errn = -1;
  5268. sprintf((char*)buf_log_evcomm,
  5269. "[Proc_din_SessionSetupRes]Emergency Stop by CP Error (%d)\n",
  5270. sys->CpState);
  5271. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5272. #else
  5273. sprintf((char*)buf_log_evcomm,
  5274. "[Proc_din_SessionSetupRes]Emergency Stop by CP Error (%d): ignored\n",
  5275. sys->CpState);
  5276. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5277. #endif
  5278. }
  5279. //Check for shutdown commands from EVSE(DC Main Board)
  5280. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5281. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5282. {
  5283. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  5284. sprintf((char*)buf_log_evcomm, "[Proc_din_SessionSetupRes]Stop by EVSE(%d:normal, %d:emergency): (%d)\n",
  5285. EVSE_Shutdown,
  5286. EVSE_EmergencyShutdown,
  5287. sys->DC_EVSEStatus);
  5288. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5289. errn = -1;
  5290. }
  5291. //Check for Permission Changing from TRUE to FALSE
  5292. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  5293. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  5294. ShmInternalComm->ChargingPermission == FALSE)
  5295. {
  5296. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][SessionSetupRes]Permission OFF");
  5297. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  5298. errn = -1;
  5299. }
  5300. // ====== [BODY (2/3) EVSEID ======
  5301. //EVSEID = all zero
  5302. memset(ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytes, 0, sizeof(ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytes));
  5303. //vern, should be encode by SN
  5304. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytesLen = 1; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  5305. // ====== [BODY (3/3) DateTimeNow ======
  5306. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.DateTimeNow_isUsed = 1u;
  5307. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.DateTimeNow = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  5308. #if PARAMETER_NORMAL_MODE == ENABLE
  5309. ///////////SHM_Read_din_SessionSetupRes(&ccs_exi_doc_DIN, ShmCcsData);
  5310. #endif
  5311. // ============ Encode and Send Response Message ===========
  5312. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  5313. {
  5314. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_SessionSetupRes][Error]Tx encoded msg error");
  5315. errn = -1;
  5316. }
  5317. return errn;
  5318. }
  5319. /*===========================================================================
  5320. FUNCTION: Proc_iso1_SessionSetupRes
  5321. DESCRIPTION:
  5322. PRE-CONDITION:
  5323. INPUT:
  5324. OUTPUT:
  5325. GLOBAL VARIABLES:
  5326. =============================================================================*/
  5327. int Proc_iso1_SessionSetupRes(int AcceptFd)
  5328. {
  5329. //int i = 0;
  5330. int errn = 0;
  5331. bitstream_t v2g_tx_stream;
  5332. static struct ChargingInfoData *sys;
  5333. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5334. size_t pos = 0;
  5335. v2g_tx_stream.pos = &pos;
  5336. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5337. v2g_tx_stream.data = V2GTP_Tx_buf;
  5338. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  5339. init_iso1SessionSetupResType(&ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes);
  5340. // ====== [BODY (1/2) ResponseCode ======
  5341. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  5342. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_OK_NewSessionEstablished;
  5343. //[HEADER] Assign Res SessionID
  5344. ccs_exi_doc_ISO1.V2G_Message.Header.SessionID.bytesLen = 8;
  5345. memset(ccs_exi_doc_ISO1.V2G_Message.Header.SessionID.bytes, 0, 8);
  5346. memcpy(ccs_exi_doc_ISO1.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  5347. //Check for SequenceError
  5348. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5349. {
  5350. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  5351. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5352. errn = -1;
  5353. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5354. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5355. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5356. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5357. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5358. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5359. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5360. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5361. }
  5362. //#if PARAMETER_NORMAL_MODE == ENABLE
  5363. //SHM_Read_iso1_V2GMSG_Header(&ccs_exi_doc_ISO1, ShmCcsData);
  5364. //#endif
  5365. //Detect for CP State should be 9V (State B)
  5366. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5367. {
  5368. #if CP_PROTECTION_MECHANISM == ENABLE
  5369. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  5370. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5371. Update_V2G_Flow_Status(Other_Fault);
  5372. errn = -1;
  5373. sprintf((char*)buf_log_evcomm,
  5374. "[Proc_iso1_SessionSetupRes]Emergency Stop by CP Error (%d)\n",
  5375. sys->CpState);
  5376. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5377. #else
  5378. sprintf((char*)buf_log_evcomm,
  5379. "[Proc_iso1_SessionSetupRes]Emergency Stop by CP Error (%d): ignored\n",
  5380. sys->CpState);
  5381. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5382. #endif
  5383. }
  5384. //Check for shutdown commands from EVSE(DC Main Board)
  5385. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5386. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5387. {
  5388. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  5389. sprintf((char*)buf_log_evcomm, "[Proc_iso1_SessionSetupRes]Stop by EVSE(%d:normal, %d:emergency): (%d)\n",
  5390. EVSE_Shutdown,
  5391. EVSE_EmergencyShutdown,
  5392. sys->DC_EVSEStatus);
  5393. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5394. errn = -1;
  5395. }
  5396. //Check for Permission Changing from TRUE to FALSE
  5397. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  5398. ShmInternalComm->ChargingPermission == FALSE)
  5399. {
  5400. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO1][SessionSetupRes]Permission OFF");
  5401. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  5402. errn = -1;
  5403. }
  5404. // ====== [BODY (2/3) EVSEID ======
  5405. //EVSEID = all zero
  5406. memset(ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters, 0, sizeof(ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters));
  5407. /*+++ 20200808, vern, set default EVSEID +++*/
  5408. //vern, should be encoded by SN
  5409. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[0]='Z';
  5410. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[1]='Z';
  5411. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[2]='0';
  5412. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[3]='0';
  5413. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[4]='0';
  5414. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[5]='0';
  5415. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[6]='0';
  5416. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 7; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  5417. /*--- 20200808, vern, set default EVSEID ---*/
  5418. // ====== [BODY (3/3) DateTimeNow ======
  5419. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSETimeStamp_isUsed = 1u;
  5420. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSETimeStamp = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  5421. #if PARAMETER_NORMAL_MODE == ENABLE
  5422. ///////////SHM_Read_iso1_SessionSetupRes(&ccs_exi_doc_ISO1, ShmCcsData);
  5423. #endif
  5424. // ============ Encode and Send Response Message ===========
  5425. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  5426. {
  5427. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_SessionSetupRes][Error]Tx encoded msg error");
  5428. errn = -1;
  5429. }
  5430. return errn;
  5431. }
  5432. /*===========================================================================
  5433. FUNCTION: Proc_iso2_SessionSetupRes
  5434. DESCRIPTION:
  5435. PRE-CONDITION:
  5436. INPUT:
  5437. OUTPUT:
  5438. GLOBAL VARIABLES:
  5439. =============================================================================*/
  5440. int Proc_iso2_SessionSetupRes(int AcceptFd)
  5441. {
  5442. //int i = 0;
  5443. int errn = 0;
  5444. bitstream_t v2g_tx_stream;
  5445. static struct ChargingInfoData *sys;
  5446. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5447. size_t pos = 0;
  5448. v2g_tx_stream.pos = &pos;
  5449. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5450. v2g_tx_stream.data = V2GTP_Tx_buf;
  5451. init_iso2BodyType(&ccs_exi_doc_ISO2.V2G_Message.Body);
  5452. init_iso2SessionSetupResType(&ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes);
  5453. // ====== [BODY (1/2) ResponseCode ======
  5454. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  5455. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_OK_NewSessionEstablished;
  5456. //[HEADER] Assign Res SessionID
  5457. ccs_exi_doc_ISO2.V2G_Message.Header.SessionID.bytesLen = 8;
  5458. memset(ccs_exi_doc_ISO2.V2G_Message.Header.SessionID.bytes, 0, 8);
  5459. memcpy(ccs_exi_doc_ISO2.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  5460. //Check for SequenceError
  5461. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5462. {
  5463. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  5464. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5465. errn = -1;
  5466. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5467. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5468. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5469. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5470. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5471. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5472. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5473. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5474. }
  5475. //#if PARAMETER_NORMAL_MODE == ENABLE
  5476. //SHM_Read_iso2_V2GMSG_Header(&ccs_exi_doc_ISO2, ShmCcsData);
  5477. //#endif
  5478. //Detect for CP State should be 9V (State B)
  5479. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5480. {
  5481. #if CP_PROTECTION_MECHANISM == ENABLE
  5482. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  5483. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5484. Update_V2G_Flow_Status(Other_Fault);
  5485. errn = -1;
  5486. sprintf((char*)buf_log_evcomm,
  5487. "[Proc_iso2_SessionSetupRes]Emergency Stop by CP Error (%d)",
  5488. sys->CpState);
  5489. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5490. #else
  5491. sprintf((char*)buf_log_evcomm,
  5492. "[Proc_iso2_SessionSetupRes]Emergency Stop by CP Error (%d): ignored",
  5493. sys->CpState);
  5494. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5495. #endif
  5496. }
  5497. //Check for shutdown commands from EVSE(DC Main Board)
  5498. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5499. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5500. {
  5501. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  5502. sprintf((char*)buf_log_evcomm, "[Proc_iso2_SessionSetupRes]Stop by EVSE(%d:normal, %d:emergency): (%d)\n",
  5503. EVSE_Shutdown,
  5504. EVSE_EmergencyShutdown,
  5505. sys->DC_EVSEStatus);
  5506. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5507. errn = -1;
  5508. }
  5509. //Check for Permission Changing from TRUE to FALSE
  5510. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  5511. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  5512. ShmInternalComm->ChargingPermission == FALSE)
  5513. {
  5514. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO2][SessionSetupRes]Permission OFF");
  5515. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  5516. errn = -1;
  5517. }
  5518. // ====== [BODY (2/3) EVSEID ======
  5519. //EVSEID = all zero
  5520. memset(ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.characters, 0, sizeof(ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.characters));
  5521. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 15; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  5522. // ====== [BODY (3/3) DateTimeNow ======
  5523. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSETimeStamp_isUsed = 1u;
  5524. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSETimeStamp = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  5525. #if PARAMETER_NORMAL_MODE == ENABLE
  5526. ///////////SHM_Read_iso2_SessionSetupRes(&ccs_exi_doc_ISO2, ShmCcsData);
  5527. #endif
  5528. // ============ Encode and Send Response Message ===========
  5529. if (send_encoded_iso2_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO2) != 0)
  5530. {
  5531. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso2_SessionSetupRes][Error]Tx encoded msg error");
  5532. errn = -1;
  5533. }
  5534. return errn;
  5535. }
  5536. /*===========================================================================
  5537. FUNCTION: Proc_din_SessionSetupReq
  5538. DESCRIPTION:
  5539. PRE-CONDITION:
  5540. INPUT:
  5541. OUTPUT:
  5542. GLOBAL VARIABLES:
  5543. 2. ccs_exi_doc_DIN
  5544. =============================================================================*/
  5545. int Proc_din_SessionSetupReq(int AcceptFd)
  5546. {
  5547. int errn = 0;
  5548. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Proc_din_SessionSetupReq]Got SessionSetupReq\n");
  5549. //Print the decoded XML Document
  5550. PRINT_XML_DOC_DIN_SessionSetupReq(&ccs_exi_doc_DIN);
  5551. //Save into Share Memory
  5552. SHM_Save_din_SessionSetupReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  5553. errn = Proc_din_SessionSetupRes(AcceptFd);
  5554. if (errn == 0)
  5555. {
  5556. //successfully send response.
  5557. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]SessionSetupRes");
  5558. }
  5559. else
  5560. {
  5561. sprintf((char*)buf_log_evcomm,
  5562. "[Error]Proc_din_SessionSetupRes(): %d (DEC)",
  5563. errn);
  5564. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5565. }
  5566. return errn;
  5567. }
  5568. /*===========================================================================
  5569. FUNCTION: Proc_iso1_SessionSetupReq
  5570. DESCRIPTION:
  5571. PRE-CONDITION:
  5572. INPUT:
  5573. OUTPUT:
  5574. GLOBAL VARIABLES:
  5575. 2. ccs_exi_doc_ISO1
  5576. =============================================================================*/
  5577. int Proc_iso1_SessionSetupReq(int AcceptFd)
  5578. {
  5579. int errn = 0;
  5580. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Proc_iso1_SessionSetupReq]Got SessionSetupReq\n");
  5581. //Print the decoded XML Document
  5582. PRINT_XML_DOC_ISO1_SessionSetupReq(&ccs_exi_doc_ISO1);
  5583. //Save into Share Memory
  5584. SHM_Save_iso1_SessionSetupReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  5585. errn = Proc_iso1_SessionSetupRes(AcceptFd);
  5586. if (errn == 0)
  5587. {
  5588. //successfully send response.
  5589. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]SessionSetupRes");
  5590. }
  5591. else
  5592. {
  5593. sprintf((char*)buf_log_evcomm,
  5594. "[Error]Proc_iso1_SessionSetupRes(): %d (DEC)",
  5595. errn);
  5596. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5597. }
  5598. return errn;
  5599. }
  5600. /*===========================================================================
  5601. FUNCTION: Proc_iso2_SessionSetupReq
  5602. DESCRIPTION:
  5603. PRE-CONDITION:
  5604. INPUT:
  5605. OUTPUT:
  5606. GLOBAL VARIABLES:
  5607. 2. ccs_exi_doc_ISO2
  5608. =============================================================================*/
  5609. int Proc_iso2_SessionSetupReq(int AcceptFd)
  5610. {
  5611. int errn = 0;
  5612. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Proc_iso2_SessionSetupReq]Got SessionSetupReq\n");
  5613. //Print the decoded XML Document
  5614. PRINT_XML_DOC_ISO2_SessionSetupReq(&ccs_exi_doc_ISO2);
  5615. //Save into Share Memory
  5616. SHM_Save_iso2_SessionSetupReq(ShmCcsData, &ccs_exi_doc_ISO2, ShmSysConfigAndInfo);
  5617. errn = Proc_iso2_SessionSetupRes(AcceptFd);
  5618. if (errn == 0)
  5619. {
  5620. //successfully send response.
  5621. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO2][V2G][Tx]SessionSetupRes");
  5622. }
  5623. else
  5624. {
  5625. sprintf((char*)buf_log_evcomm,
  5626. "[Error]Proc_iso2_SessionSetupRes(): %d (DEC)",
  5627. errn);
  5628. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5629. }
  5630. return errn;
  5631. }
  5632. /*===========================================================================
  5633. FUNCTION: Proc_din_ServiceDiscoveryRes
  5634. DESCRIPTION:
  5635. PRE-CONDITION:
  5636. INPUT:
  5637. OUTPUT:
  5638. GLOBAL VARIABLES:
  5639. =============================================================================*/
  5640. int Proc_din_ServiceDiscoveryRes(int AcceptFd)
  5641. {
  5642. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  5643. //if it is not the same, the packet should be ignored.
  5644. //int i = 0;
  5645. int errn = 0;
  5646. bitstream_t v2g_tx_stream;
  5647. struct ChargingInfoData *sys;
  5648. //struct ServiceDiscoveryRequest_DIN70121 *req;
  5649. struct ServiceDiscoveryResponse_DIN70121 *res;
  5650. size_t pos = 0;
  5651. v2g_tx_stream.pos = &pos;
  5652. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5653. v2g_tx_stream.data = V2GTP_Tx_buf;
  5654. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5655. //req = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryRequest;
  5656. res = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryResponse;
  5657. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  5658. init_dinServiceDiscoveryResType(&ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes);
  5659. //[1/4] Response Code
  5660. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes_isUsed = 1u;
  5661. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_OK;
  5662. //[HEADER] Check Req SessionID
  5663. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  5664. {
  5665. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  5666. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  5667. errn = -1;
  5668. }
  5669. //Check for SequenceError
  5670. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5671. {
  5672. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  5673. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5674. errn = -1;
  5675. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5676. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5677. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5678. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5679. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5680. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5681. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5682. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5683. }
  5684. //Detect for CP State should be 9V (State B)
  5685. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5686. {
  5687. #if CP_PROTECTION_MECHANISM == ENABLE
  5688. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  5689. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5690. Update_V2G_Flow_Status(Other_Fault);
  5691. errn = -1;
  5692. sprintf((char*)buf_log_evcomm, "[Proc_din_ServiceDiscoveryRes]Emergency Stop by CP Error (%d)", sys->CpState);
  5693. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5694. #else
  5695. sprintf((char*)buf_log_evcomm,
  5696. "[Proc_din_ServiceDiscoveryRes]Emergency Stop by CP Error (%d): ignored",
  5697. sys->CpState);
  5698. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5699. #endif
  5700. }
  5701. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5702. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5703. {
  5704. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  5705. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ServiceDiscoveryRes]shutdown by EVSE");
  5706. errn = -1;
  5707. }
  5708. //Check for Permission Changing from TRUE to FALSE
  5709. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  5710. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  5711. ShmInternalComm->ChargingPermission == FALSE)
  5712. {
  5713. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][ServiceDiscoveryRes]Permission OFF");
  5714. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  5715. errn = -1;
  5716. }
  5717. //[2/4] PaymentOptions
  5718. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptions.PaymentOption.arrayLen = 1u;
  5719. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptions.PaymentOption.array[0] = dinpaymentOptionType_ExternalPayment; //1
  5720. //[3/4] Charge Service
  5721. res->ChargeService_DIN70121.Services.ServiceTag.ServiceID = 1;
  5722. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceTag.ServiceID = (unsigned short) res->ChargeService_DIN70121.Services.ServiceTag.ServiceID;
  5723. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceTag.ServiceCategory = dinserviceCategoryType_EVCharging;
  5724. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.FreeService = 1; //[Joseph] for test
  5725. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.EnergyTransferType = dinEVSESupportedEnergyTransferType_DC_extended;
  5726. //[4/4] Service List (null, not be uesed for now.)
  5727. //#if PARAMETER_NORMAL_MODE == ENABLE
  5728. ///////////////SHM_Read_din_ServiceDiscoveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  5729. //#endif
  5730. // ============ Encode and Send Response Message ===========
  5731. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  5732. {
  5733. errn = -1;
  5734. }
  5735. return errn;
  5736. }
  5737. /*===========================================================================
  5738. FUNCTION: Proc_iso1_ServiceDiscoveryRes
  5739. DESCRIPTION:
  5740. PRE-CONDITION:
  5741. INPUT:
  5742. OUTPUT:
  5743. GLOBAL VARIABLES:
  5744. =============================================================================*/
  5745. int Proc_iso1_ServiceDiscoveryRes(int AcceptFd)
  5746. {
  5747. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  5748. //if it is not the same, the packet should be ignored.
  5749. //int i = 0;
  5750. int errn = 0;
  5751. bitstream_t v2g_tx_stream;
  5752. struct ChargingInfoData *sys;
  5753. //struct ServiceDiscoveryRequest_ISO15118_2014 *req;
  5754. struct ServiceDiscoveryResponse_ISO15118_2014 *res;
  5755. size_t pos = 0;
  5756. v2g_tx_stream.pos = &pos;
  5757. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5758. v2g_tx_stream.data = V2GTP_Tx_buf;
  5759. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5760. //req = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryRequest;
  5761. res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryResponse;
  5762. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  5763. init_iso1ServiceDiscoveryResType(&ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes);
  5764. //[1/4] Response Code
  5765. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes_isUsed = 1u;
  5766. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_OK;
  5767. //[HEADER] Check Req SessionID
  5768. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  5769. {
  5770. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  5771. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  5772. errn = -1;
  5773. }
  5774. //Check for SequenceError
  5775. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5776. {
  5777. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  5778. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5779. errn = -1;
  5780. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5781. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5782. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5783. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5784. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5785. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5786. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5787. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5788. }
  5789. //Detect for CP State should be 9V (State B)
  5790. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5791. {
  5792. #if CP_PROTECTION_MECHANISM == ENABLE
  5793. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  5794. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5795. Update_V2G_Flow_Status(Other_Fault);
  5796. errn = -1;
  5797. sprintf((char*)buf_log_evcomm,
  5798. "[Proc_iso1_ServiceDiscoveryRes]Emergency Stop by CP Error (%d)",
  5799. sys->CpState);
  5800. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5801. #else
  5802. sprintf((char*)buf_log_evcomm,
  5803. "[Proc_iso1_ServiceDiscoveryRes]Emergency Stop by CP Error (%d): ignored",
  5804. sys->CpState);
  5805. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5806. #endif
  5807. }
  5808. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5809. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5810. {
  5811. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  5812. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ServiceDiscoveryRes]shutdown by EVSE");
  5813. errn = -1;
  5814. }
  5815. //Check for Permission Changing from TRUE to FALSE
  5816. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  5817. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  5818. ShmInternalComm->ChargingPermission == FALSE)
  5819. {
  5820. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO1][ServiceDiscoveryRes]Permission OFF");
  5821. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  5822. errn = -1;
  5823. }
  5824. //[2/4] PaymentOptionList
  5825. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptionList.PaymentOption.arrayLen = 1u;
  5826. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptionList.PaymentOption.array[0] = iso1paymentOptionType_ExternalPayment; //1
  5827. //[3/4] Charge Service
  5828. res->ChargeService.Services.ServiceID = 1;
  5829. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceID = (unsigned short) res->ChargeService.Services.ServiceID;
  5830. //ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName_isUsed = 1; //new in ISO1, not be used, yet.
  5831. //ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName.charactersLen = xxx;
  5832. //ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName.characters[i] = xxx;
  5833. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceCategory = iso1serviceCategoryType_EVCharging;
  5834. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.FreeService = 1; //[Joseph] for test
  5835. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.arrayLen = 1u; //[ISO1] max = 6
  5836. switch (ShmCcsData->EnergyTransferMode)
  5837. {
  5838. case DC_extended:
  5839. {
  5840. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_DC_extended;
  5841. break;
  5842. }
  5843. case AC_single_phase_core:
  5844. {
  5845. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_AC_single_phase_core;
  5846. break;
  5847. }
  5848. case AC_three_phase_core:
  5849. {
  5850. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_AC_three_phase_core;
  5851. break;
  5852. }
  5853. default:
  5854. {
  5855. sprintf((char*)buf_log_evcomm,
  5856. "[ISO1][ServiceDiscoveryRes][Warning]unexpected EnergyTransferMode(%d)",
  5857. ShmCcsData->EnergyTransferMode);
  5858. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5859. break;
  5860. }
  5861. }
  5862. //[4/4] Service List (null, not be uesed for now.)
  5863. //#if PARAMETER_NORMAL_MODE == ENABLE
  5864. ///////////////SHM_Read_iso1_ServiceDiscoveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  5865. //#endif
  5866. // ============ Encode and Send Response Message ===========
  5867. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  5868. {
  5869. errn = -1;
  5870. }
  5871. return errn;
  5872. }
  5873. /*===========================================================================
  5874. FUNCTION: Proc_din_ServiceDiscoveryReq
  5875. DESCRIPTION:
  5876. PRE-CONDITION:
  5877. INPUT:
  5878. OUTPUT:
  5879. GLOBAL VARIABLES:
  5880. =============================================================================*/
  5881. int Proc_din_ServiceDiscoveryReq(int AcceptFd)
  5882. {
  5883. int errn = 0;
  5884. DEBUG_PRINTF_EVCOMM_DETAIL("\n[SeccComm][Proc_din_ServiceDiscoveryReq]Got ServiceDiscoveryReq\n");
  5885. //Print the decoded XML Document
  5886. PRINT_XML_DOC_DIN_ServiceDiscoveryReq(&ccs_exi_doc_DIN);
  5887. //Save into Share Memory
  5888. SHM_Save_din_ServiceDiscoveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  5889. errn = Proc_din_ServiceDiscoveryRes(AcceptFd);
  5890. if (errn == 0)
  5891. {
  5892. //send response successfully.
  5893. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]ServiceDiscoveryRes");
  5894. }
  5895. else
  5896. {
  5897. sprintf((char*)buf_log_evcomm,
  5898. "[Error][SeccComm][Proc_din_ServiceDiscoveryReq]Proc_din_ServiceDiscoveryRes(): %d (DEC)",
  5899. errn
  5900. );
  5901. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5902. }
  5903. return errn;
  5904. }
  5905. /*===========================================================================
  5906. FUNCTION: Proc_iso1_ServiceDiscoveryReq
  5907. DESCRIPTION:
  5908. PRE-CONDITION:
  5909. INPUT:
  5910. OUTPUT:
  5911. GLOBAL VARIABLES:
  5912. =============================================================================*/
  5913. int Proc_iso1_ServiceDiscoveryReq(int AcceptFd)
  5914. {
  5915. int errn = 0;
  5916. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][Proc_iso1_ServiceDiscoveryReq]Got ServiceDiscoveryReq\n");
  5917. //Print the decoded XML Document
  5918. PRINT_XML_DOC_ISO1_ServiceDiscoveryReq(&ccs_exi_doc_ISO1);
  5919. //Save into Share Memory
  5920. SHM_Save_iso1_ServiceDiscoveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  5921. errn = Proc_iso1_ServiceDiscoveryRes(AcceptFd);
  5922. if (errn == 0)
  5923. {
  5924. //send response successfully.
  5925. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]ServiceDiscoveryRes");
  5926. }
  5927. else
  5928. {
  5929. sprintf((char*)buf_log_evcomm,
  5930. "[Error][SeccComm][Proc_iso1_ServiceDiscoveryReq]Proc_iso1_ServiceDiscoveryRes(): %d (DEC)",
  5931. errn
  5932. );
  5933. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5934. }
  5935. return errn;
  5936. }
  5937. /*===========================================================================
  5938. FUNCTION: Proc_din_ServiceAndPaymentSelectionRes
  5939. DESCRIPTION:
  5940. PRE-CONDITION:
  5941. INPUT:
  5942. OUTPUT:
  5943. GLOBAL VARIABLES:
  5944. =============================================================================*/
  5945. int Proc_din_ServiceAndPaymentSelectionRes(int AcceptFd)
  5946. {
  5947. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  5948. //if it is not the same, the packet should be ignored.
  5949. //int i = 0;
  5950. int errn = 0;
  5951. bitstream_t v2g_tx_stream;
  5952. struct ServiceAndPaymentSelectionRequest_DIN70121 *req;
  5953. //struct ServiceAndPaymentSelectionResponse_DIN70121 *res;
  5954. struct ServiceDiscoveryResponse_DIN70121 *sd_res;
  5955. struct ChargingInfoData *sys;
  5956. size_t pos = 0;
  5957. v2g_tx_stream.pos = &pos;
  5958. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5959. v2g_tx_stream.data = V2GTP_Tx_buf;
  5960. req = &ShmCcsData->V2GMessage_DIN70121.ServiceAndPaymentSelectionRequest;
  5961. //res = &ShmCcsData->V2GMessage_DIN70121.ServiceAndPaymentSelectionResponse;
  5962. sd_res = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryResponse;
  5963. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5964. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  5965. init_dinServicePaymentSelectionResType(&ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes);
  5966. //[1/1] Response Code
  5967. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes_isUsed = 1u;
  5968. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_OK;
  5969. //[HEADER] Check Req SessionID
  5970. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  5971. {
  5972. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  5973. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  5974. errn = -1;
  5975. }
  5976. //Check for SequenceError
  5977. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5978. {
  5979. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  5980. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5981. errn = -1;
  5982. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5983. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5984. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5985. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5986. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5987. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5988. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5989. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5990. }
  5991. //Check for SelectedPaymentOption (TC_SECC_VTB_ServicePaymentSelection_007)
  5992. if (req->SelectedPaymentOption != ExternalPayment) //1
  5993. {
  5994. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_PaymentSelectionInvalid;
  5995. sprintf((char*)buf_log_evcomm,
  5996. "[ERROR]unexpected SelectedPaymentOption(%d) => End_Process (EIM only, no PnC, yet.)",
  5997. req->SelectedPaymentOption);
  5998. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5999. errn = -1;
  6000. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6001. //CCS_SECC_ResponseCode_FAILED_Payment SelectionInvalid (023762)
  6002. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6003. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6004. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6005. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6006. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  6007. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  6008. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6009. }
  6010. //Check for ServiceID (TC_SECC_VTB_ServicePaymentSelection_004)
  6011. if (req->SelectedServiceList.SelectedService[0].ServiceID != sd_res->ChargeService_DIN70121.Services.ServiceTag.ServiceID)
  6012. {
  6013. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_ServiceSelectionInvalid; //8
  6014. sprintf((char*)buf_log_evcomm,
  6015. "[ERROR]Wrong selected ServiceID(%d) => End_Process",
  6016. req->SelectedServiceList.SelectedService[0].ServiceID);
  6017. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6018. errn = -1;
  6019. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6020. //CCS_SECC_ResponseCode_FAILED_ServiceSelectionInvalid (023764)
  6021. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6022. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6023. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6024. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6025. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  6026. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  6027. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6028. }
  6029. //Detect for CP State should be 9V (State B)
  6030. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  6031. {
  6032. #if CP_PROTECTION_MECHANISM == ENABLE
  6033. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  6034. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6035. Update_V2G_Flow_Status(Other_Fault);
  6036. errn = -1;
  6037. sprintf((char*)buf_log_evcomm,
  6038. "[Proc_din_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d)",
  6039. sys->CpState);
  6040. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6041. #else
  6042. sprintf((char*)buf_log_evcomm,
  6043. "[Proc_din_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d): ignored due to function is disabled",
  6044. sys->CpState);
  6045. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6046. #endif
  6047. }
  6048. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  6049. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6050. {
  6051. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ServiceAndPaymentSelectionRes]shutdown by EVSE");
  6052. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  6053. errn = -1;
  6054. }
  6055. //Check for Permission Changing from TRUE to FALSE
  6056. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  6057. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  6058. ShmInternalComm->ChargingPermission == FALSE)
  6059. {
  6060. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][ServicePaymentSelectionRes]Permission OFF");
  6061. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  6062. errn = -1;
  6063. }
  6064. // ============ Encode and Send Response Message ===========
  6065. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  6066. {
  6067. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ServiceAndPaymentSelectionRes]Tx encoded msg error");
  6068. errn = -1;
  6069. }
  6070. return errn;
  6071. }
  6072. /*===========================================================================
  6073. FUNCTION: Proc_iso1_ServiceAndPaymentSelectionRes
  6074. DESCRIPTION:
  6075. PRE-CONDITION:
  6076. INPUT:
  6077. OUTPUT:
  6078. GLOBAL VARIABLES:
  6079. =============================================================================*/
  6080. int Proc_iso1_ServiceAndPaymentSelectionRes(int AcceptFd)
  6081. {
  6082. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6083. //if it is not the same, the packet should be ignored.
  6084. //int i = 0;
  6085. int errn = 0;
  6086. bitstream_t v2g_tx_stream;
  6087. struct ServiceAndPaymentSelectionRequest_ISO15118_2014 *req;
  6088. //struct ServiceAndPaymentSelectionResponse_ISO15118_2014 *res;
  6089. struct ServiceDiscoveryResponse_ISO15118_2014 *sd_res;
  6090. static struct ChargingInfoData *sys;
  6091. size_t pos = 0;
  6092. v2g_tx_stream.pos = &pos;
  6093. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6094. v2g_tx_stream.data = V2GTP_Tx_buf;
  6095. req = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceAndPaymentSelectionRequest;
  6096. //res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceAndPaymentSelectionResponse;
  6097. sd_res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryResponse;
  6098. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6099. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  6100. init_iso1PaymentServiceSelectionResType(&ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes);
  6101. //[1/1] Response Code
  6102. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes_isUsed = 1u;
  6103. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_OK;
  6104. //[HEADER] Check Req SessionID
  6105. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  6106. {
  6107. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  6108. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  6109. errn = -1;
  6110. }
  6111. //Check for SequenceError
  6112. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6113. {
  6114. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  6115. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  6116. errn = -1;
  6117. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6118. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6119. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6120. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6121. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6122. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6123. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6124. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6125. }
  6126. //Check for SelectedPaymentOption (TC_SECC_VTB_ServicePaymentSelection_007)
  6127. if (req->SelectedPaymentOption != ExternalPayment) //1
  6128. {
  6129. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_PaymentSelectionInvalid;
  6130. sprintf((char*)buf_log_evcomm,
  6131. "[ERROR]unexpected SelectedPaymentOption(%d) => End_Process (EIM only, no PnC, yet.)",
  6132. req->SelectedPaymentOption);
  6133. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6134. errn = -1;
  6135. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6136. //CCS_SECC_ResponseCode_FAILED_Payment SelectionInvalid (023762)
  6137. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6138. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6139. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6140. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6141. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  6142. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  6143. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6144. }
  6145. //Check for ServiceID (TC_SECC_VTB_ServicePaymentSelection_004)
  6146. if (req->SelectedServiceList.SelectedService[0].ServiceID != sd_res->ChargeService.Services.ServiceID)
  6147. {
  6148. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_ServiceSelectionInvalid; //8
  6149. sprintf((char*)buf_log_evcomm,
  6150. "[ERROR]Wrong selected ServiceID(%d) => End_Process",
  6151. req->SelectedServiceList.SelectedService[0].ServiceID);
  6152. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6153. errn = -1;
  6154. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6155. //CCS_SECC_ResponseCode_FAILED_ServiceSelectionInvalid (023764)
  6156. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6157. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6158. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6159. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6160. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  6161. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  6162. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6163. }
  6164. //Detect for CP State should be 9V (State B)
  6165. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  6166. {
  6167. #if CP_PROTECTION_MECHANISM == ENABLE
  6168. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  6169. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6170. Update_V2G_Flow_Status(Other_Fault);
  6171. errn = -1;
  6172. sprintf((char*)buf_log_evcomm,
  6173. "[Proc_iso1_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d)",
  6174. sys->CpState);
  6175. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6176. #else
  6177. sprintf((char*)buf_log_evcomm,
  6178. "[Proc_iso1_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d): ignored due to function is disabled",
  6179. sys->CpState);
  6180. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6181. #endif
  6182. }
  6183. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  6184. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6185. {
  6186. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ServiceAndPaymentSelectionRes]shutdown by EVSE");
  6187. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  6188. errn = -1;
  6189. }
  6190. //Check for Permission Changing from TRUE to FALSE
  6191. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  6192. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  6193. ShmInternalComm->ChargingPermission == FALSE)
  6194. {
  6195. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO1][PaymentServiceSelectionRes]Permission OFF");
  6196. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  6197. errn = -1;
  6198. }
  6199. // ============ Encode and Send Response Message ===========
  6200. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  6201. {
  6202. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ServiceAndPaymentSelectionRes]Tx encoded msg error");
  6203. errn = -1;
  6204. }
  6205. return errn;
  6206. }
  6207. /*===========================================================================
  6208. FUNCTION: Proc_din_ServiceAndPaymentSelectionReq
  6209. DESCRIPTION:
  6210. PRE-CONDITION:
  6211. INPUT:
  6212. OUTPUT:
  6213. GLOBAL VARIABLES:
  6214. =============================================================================*/
  6215. int Proc_din_ServiceAndPaymentSelectionReq(int AcceptFd)
  6216. {
  6217. int errn = 0;
  6218. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][Proc_din_ServiceAndPaymentSelectionReq]Got ServiceAndPaymentSelectionReq");
  6219. //Print the decoded XML Document
  6220. PRINT_XML_DOC_DIN_ServiceAndPaymentSelectionReq(&ccs_exi_doc_DIN);
  6221. //Save into Share Memory
  6222. SHM_Save_din_ServiceAndPaymentSelectionReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  6223. errn = Proc_din_ServiceAndPaymentSelectionRes(AcceptFd);
  6224. if (errn == 0)
  6225. {
  6226. //send response successfully.
  6227. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]ServiceAndPaymentSelectionRes");
  6228. }
  6229. else
  6230. {
  6231. sprintf((char*)buf_log_evcomm,
  6232. "[Error][Proc_iso1_ServiceAndPaymentSelectionReq]Proc_iso1_ServiceAndPaymentSelectionRes(): %d (DEC)",
  6233. errn);
  6234. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6235. }
  6236. return errn;
  6237. }
  6238. /*===========================================================================
  6239. FUNCTION: Proc_iso1_ServiceAndPaymentSelectionReq
  6240. DESCRIPTION:
  6241. PRE-CONDITION:
  6242. INPUT:
  6243. OUTPUT:
  6244. GLOBAL VARIABLES:
  6245. =============================================================================*/
  6246. int Proc_iso1_ServiceAndPaymentSelectionReq(int AcceptFd)
  6247. {
  6248. int errn = 0;
  6249. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][Proc_iso1_ServiceAndPaymentSelectionReq]Got ServiceAndPaymentSelectionReq");
  6250. //Print the decoded XML Document
  6251. PRINT_XML_DOC_ISO1_ServiceAndPaymentSelectionReq(&ccs_exi_doc_ISO1);
  6252. //Save into Share Memory
  6253. SHM_Save_iso1_ServiceAndPaymentSelectionReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  6254. errn = Proc_iso1_ServiceAndPaymentSelectionRes(AcceptFd);
  6255. if (errn == 0)
  6256. {
  6257. //send response successfully.
  6258. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]ServiceAndPaymentSelectionRes");
  6259. }
  6260. else
  6261. {
  6262. sprintf((char*)buf_log_evcomm,
  6263. "[Error][Proc_iso1_ServiceAndPaymentSelectionReq]Proc_iso1_ServiceAndPaymentSelectionRes(): %d (DEC)",
  6264. errn);
  6265. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6266. }
  6267. return errn;
  6268. }
  6269. /*===========================================================================
  6270. FUNCTION: Proc_din_ContractAuthenticationRes
  6271. DESCRIPTION:
  6272. PRE-CONDITION:
  6273. INPUT:
  6274. OUTPUT:
  6275. GLOBAL VARIABLES:
  6276. =============================================================================*/
  6277. int Proc_din_ContractAuthenticationRes(int AcceptFd)
  6278. {
  6279. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6280. //if it is not the same, the packet should be ignored.
  6281. //int i = 0;
  6282. int errn = 0;
  6283. bitstream_t v2g_tx_stream;
  6284. static struct ChargingInfoData *sys;
  6285. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6286. size_t pos = 0;
  6287. v2g_tx_stream.pos = &pos;
  6288. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6289. v2g_tx_stream.data = V2GTP_Tx_buf;
  6290. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  6291. init_dinContractAuthenticationResType(&ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes);
  6292. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes_isUsed = 1u;
  6293. //[BODY (1/2)] ResponseCode
  6294. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_OK;
  6295. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Ongoing; //0
  6296. //[HEADER] Check Req SessionID
  6297. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  6298. {
  6299. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  6300. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  6301. errn = -1;
  6302. }
  6303. //Check for SequenceError
  6304. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6305. {
  6306. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  6307. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  6308. errn = -1;
  6309. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6310. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6311. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6312. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6313. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6314. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6315. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6316. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6317. }
  6318. //Detect for CP State should be 9V (State B)
  6319. #if CP_PROTECTION_MECHANISM == ENABLE
  6320. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  6321. {
  6322. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6323. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6324. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6325. Update_V2G_Flow_Status(Other_Fault);
  6326. errn = -1;
  6327. sprintf((char*)buf_log_evcomm,
  6328. "[Proc_din_ContractAuthenticationRes]Emergency Stop by CP Error (%d)",
  6329. sys->CpState);
  6330. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6331. }
  6332. #endif
  6333. //Check for CSU command of "Stop by EVSE"
  6334. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6335. {
  6336. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6337. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6338. errn = -1;
  6339. }
  6340. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6341. {
  6342. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6343. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6344. errn = -1;
  6345. }
  6346. //[BODY (2/2)] EVSEProcessing
  6347. if(ShmInternalComm->ChargingPermission == TRUE)
  6348. {
  6349. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished; //0
  6350. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "CSU Permission: OK");
  6351. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  6352. {
  6353. //Set PWM as 5% (for SLAC first case)
  6354. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Set PWM as 5%%");
  6355. SwitchCpStateE(DISABLE);
  6356. OutputCpPwmDuty(5);
  6357. }
  6358. #endif
  6359. }
  6360. //Check for Permission Changing from TRUE to FALSE
  6361. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  6362. ShmInternalComm->ChargingPermission == FALSE)
  6363. {
  6364. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][ContractAuthenticationRes]Permission OFF");
  6365. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6366. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6367. errn = -1;
  6368. }
  6369. #if PARAMETER_NORMAL_MODE == ENABLE
  6370. ////////////SHM_Read_din_ContractAuthenticationRes(&ccs_exi_doc_DIN, ShmCcsData);
  6371. #endif
  6372. // ============ Encode and Send Response Message ===========
  6373. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  6374. {
  6375. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  6376. errn = -1;
  6377. }
  6378. return errn;
  6379. }
  6380. /*===========================================================================
  6381. FUNCTION: Proc_iso1_AuthorizationRes
  6382. DESCRIPTION:
  6383. PRE-CONDITION:
  6384. INPUT:
  6385. OUTPUT:
  6386. GLOBAL VARIABLES:
  6387. =============================================================================*/
  6388. int Proc_iso1_AuthorizationRes(int AcceptFd)
  6389. {
  6390. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6391. //if it is not the same, the packet should be ignored.
  6392. //int i = 0;
  6393. int errn = 0;
  6394. bitstream_t v2g_tx_stream;
  6395. static struct ChargingInfoData *sys;
  6396. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6397. size_t pos = 0;
  6398. v2g_tx_stream.pos = &pos;
  6399. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6400. v2g_tx_stream.data = V2GTP_Tx_buf;
  6401. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  6402. init_iso1AuthorizationResType(&ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes);
  6403. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes_isUsed = 1u;
  6404. //[BODY (1/2)] ResponseCode
  6405. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_OK;
  6406. /*+++ 20200808, vern, EVSEProcessing should be waiting for Customer during authrization +++*/
  6407. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Ongoing_WaitingForCustomerInteraction; //0
  6408. /*--- 20200808, vern, should be waiting for Customer during authrization ---*/
  6409. //[HEADER] Check Req SessionID
  6410. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  6411. {
  6412. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  6413. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  6414. errn = -1;
  6415. }
  6416. //Check for SequenceError
  6417. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6418. {
  6419. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  6420. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  6421. errn = -1;
  6422. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6423. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6424. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6425. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6426. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6427. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6428. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6429. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6430. }
  6431. //Detect for CP State should be 9V (State B)
  6432. #if CP_PROTECTION_MECHANISM == ENABLE
  6433. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  6434. {
  6435. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED;
  6436. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6437. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6438. Update_V2G_Flow_Status(Other_Fault);
  6439. errn = -1;
  6440. sprintf((char*)buf_log_evcomm,
  6441. "[Proc_iso1_AuthorizationRes]Emergency Stop by CP Error (%d)",
  6442. sys->CpState);
  6443. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6444. }
  6445. #endif
  6446. //Check for CSU command of "Stop by EVSE"
  6447. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6448. {
  6449. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  6450. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6451. errn = -1;
  6452. }
  6453. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6454. {
  6455. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  6456. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6457. errn = -1;
  6458. }
  6459. //[BODY (2/2)] EVSEProcessing
  6460. //Check for Permission from CSU
  6461. if(ShmInternalComm->ChargingPermission == TRUE)
  6462. {
  6463. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  6464. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "CSU Permission: OK");
  6465. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  6466. {
  6467. //Set PWM as 5% (for SLAC first case)
  6468. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Set PWM as 5%");
  6469. SwitchCpStateE(DISABLE);
  6470. OutputCpPwmDuty(5);
  6471. }
  6472. #endif
  6473. }
  6474. //Check for Permission Changing from TRUE to FALSE
  6475. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  6476. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  6477. ShmInternalComm->ChargingPermission == FALSE)
  6478. {
  6479. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO1][AuthorizationRes]Permission OFF");
  6480. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  6481. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6482. errn = -1;
  6483. }
  6484. #if PARAMETER_NORMAL_MODE == ENABLE
  6485. ////////////SHM_Read_iso1_AuthorizationRes(&ccs_exi_doc_ISO1, ShmCcsData);
  6486. #endif
  6487. // ============ Encode and Send Response Message ===========
  6488. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  6489. {
  6490. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  6491. errn = -1;
  6492. }
  6493. return errn;
  6494. }
  6495. /*===========================================================================
  6496. FUNCTION: Proc_din_ContractAuthenticationReq
  6497. DESCRIPTION:
  6498. PRE-CONDITION:
  6499. INPUT:
  6500. OUTPUT:
  6501. GLOBAL VARIABLES:
  6502. =============================================================================*/
  6503. int Proc_din_ContractAuthenticationReq(int AcceptFd)
  6504. {
  6505. int errn = 0;
  6506. DEBUG_PRINTF_EVCOMM_DETAIL("\n[SeccComm][Proc_din_ContractAuthenticationReq]Got Proc_din_ContractAuthenticationReq\n");
  6507. //Print the decoded XML Document
  6508. PRINT_XML_DOC_DIN_ContractAuthenticationReq(&ccs_exi_doc_DIN);
  6509. //Save into Share Memory
  6510. SHM_Save_din_ContractAuthenticationReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  6511. errn = Proc_din_ContractAuthenticationRes(AcceptFd);
  6512. if (errn == 0)
  6513. {
  6514. //send response successfully.
  6515. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]ContractAuthenticationRes");
  6516. }
  6517. else
  6518. {
  6519. sprintf((char*)buf_log_evcomm,
  6520. "[Error][SeccComm][Proc_din_ContractAuthenticationReq]Proc_iso1_AuthenticationRes(): %d (DEC)",
  6521. errn);
  6522. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6523. }
  6524. return errn;
  6525. }
  6526. /*===========================================================================
  6527. FUNCTION: Proc_iso1_AuthenticationReq
  6528. DESCRIPTION:
  6529. PRE-CONDITION:
  6530. INPUT:
  6531. OUTPUT:
  6532. GLOBAL VARIABLES:
  6533. =============================================================================*/
  6534. int Proc_iso1_AuthenticationReq(int AcceptFd)
  6535. {
  6536. int errn = 0;
  6537. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][Proc_iso1_AuthenticationReq]Got AuthorizationReq\n");
  6538. //Print the decoded XML Document
  6539. PRINT_XML_DOC_ISO1_AuthorizationReq(&ccs_exi_doc_ISO1);
  6540. //Save into Share Memory
  6541. SHM_Save_iso1_AuthorizationReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  6542. errn = Proc_iso1_AuthorizationRes(AcceptFd);
  6543. if (errn == 0)
  6544. {
  6545. //send response successfully.
  6546. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]AuthorizationRes");
  6547. }
  6548. else
  6549. {
  6550. sprintf((char*)buf_log_evcomm,
  6551. "[Error][SeccComm][Proc_iso1_AuthenticationReq]Proc_iso1_AuthenticationRes(): %d (DEC)",
  6552. errn);
  6553. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6554. }
  6555. return errn;
  6556. }
  6557. /*===========================================================================
  6558. FUNCTION: SHM_Init_dinDC_EVSEStatusType
  6559. DESCRIPTION:
  6560. PRE-CONDITION:
  6561. INPUT:
  6562. 1. in
  6563. OUTPUT:
  6564. 2. out
  6565. GLOBAL VARIABLES:
  6566. =============================================================================*/
  6567. void SHM_Init_dinDC_EVSEStatusType(struct DC_EVSEStatusType_DIN70121 *in)
  6568. {
  6569. in->EVSEIsolationStatus = dinisolationLevelType_Valid;
  6570. // dinisolationLevelType_Invalid = 0,
  6571. // dinisolationLevelType_Valid = 1, (default)
  6572. // dinisolationLevelType_Warning = 2,
  6573. // dinisolationLevelType_Fault = 3
  6574. in->EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  6575. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6576. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1, (default)
  6577. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6578. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6579. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6580. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6581. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6582. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  6583. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  6584. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  6585. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  6586. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  6587. in->EVSENotification = dinEVSENotificationType_None;
  6588. // dinEVSENotificationType_None = 0, (default)
  6589. // dinEVSENotificationType_StopCharging = 1,
  6590. // dinEVSENotificationType_ReNegotiation = 2
  6591. }
  6592. /*===========================================================================
  6593. FUNCTION: SHM_Save_dinPhysicalValueType
  6594. DESCRIPTION:
  6595. PRE-CONDITION:
  6596. INPUT:
  6597. OUTPUT:
  6598. GLOBAL VARIABLES:
  6599. =============================================================================*/
  6600. void SHM_Save_dinPhysicalValueType(struct PhysicalValueType_DIN70121 *obj, short value, int multiplier, unsigned char unit)
  6601. {
  6602. obj->Value = value;
  6603. obj->Multiplier = multiplier;
  6604. obj->Unit =unit;
  6605. }
  6606. /*===========================================================================
  6607. FUNCTION: SHM_Init_din_ChargeParameterDiscoveryRes
  6608. DESCRIPTION:
  6609. PRE-CONDITION:
  6610. INPUT:
  6611. 1. shm_ccs
  6612. OUTPUT:
  6613. 2. shm_ccs
  6614. GLOBAL VARIABLES:
  6615. =============================================================================*/
  6616. void SHM_Init_din_ChargeParameterDiscoveryRes(struct CcsData *shm_ccs)
  6617. {
  6618. struct ChargeParameterDiscoveryResponse_DIN70121 *in;
  6619. in = &shm_ccs->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse;
  6620. //----- [BODY (1/5)] ResponseCode -----
  6621. in->ResponseCode = dinresponseCodeType_OK;
  6622. //----- [BODY (2/5)] EVSEProcessing -----
  6623. in->EVSEProcessing = dinEVSEProcessingType_Ongoing; //0
  6624. // dinEVSEProcessingType_Finished = 0,
  6625. // dinEVSEProcessingType_Ongoing = 1
  6626. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  6627. //be fixed in another function.
  6628. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  6629. //ignore, since DIN doesn't support AC
  6630. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  6631. struct DC_EVSEChargeParameterType_DIN70121 *in_para;
  6632. in_para = &in->DC_EVSEChargeParameter;
  6633. //DC_EVSEStatus
  6634. SHM_Init_dinDC_EVSEStatusType(&in_para->DC_EVSEStatus);
  6635. in_para->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  6636. short value = 0; int multiplier = 0; unsigned char unit = 0;
  6637. //EVSEMaximumCurrentLimit
  6638. value = 600; multiplier = -1; unit = A_DIN70121; //60A
  6639. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumCurrentLimit, value, multiplier, unit);
  6640. //EVSEMaximumPowerLimit
  6641. value = 3000; multiplier = 1; unit = W_DIN70121; //30KW
  6642. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumPowerLimit, value, multiplier, unit);
  6643. //EVSEMaximumVoltageLimit
  6644. value = 7500; multiplier = -1; unit = V_DIN70121; //750V
  6645. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumVoltageLimit, value, multiplier, unit);
  6646. //EVSEMinimumVoltageLimit
  6647. value = 1500; multiplier = -1; unit = V_DIN70121; //150V
  6648. SHM_Save_dinPhysicalValueType(&in_para->EVSEMinimumVoltageLimit, value, multiplier, unit);
  6649. //EVSEMinimumCurrentLimit
  6650. value = 10; multiplier = -1; unit = A_DIN70121; //1A
  6651. SHM_Save_dinPhysicalValueType(&in_para->EVSEMinimumCurrentLimit, value, multiplier, unit);
  6652. //EVSECurrentRegulationTolerance
  6653. value = 10; multiplier = -1; unit = A_DIN70121; //1A
  6654. SHM_Save_dinPhysicalValueType(&in_para->EVSECurrentRegulationTolerance, value, multiplier, unit);
  6655. //EVSEPeakCurrentRipple
  6656. value = 2; multiplier = -1; unit = A_DIN70121; //0.2A
  6657. SHM_Save_dinPhysicalValueType(&in_para->EVSEPeakCurrentRipple, value, multiplier, unit);
  6658. //EVSEEnergyToBeDelivered (optional)
  6659. //SHM_Save_dinPhysicalValueType(&out_para->EVSEEnergyToBeDelivered, &in_para->EVSEEnergyToBeDelivered);
  6660. }
  6661. /*===========================================================================
  6662. FUNCTION: Sudo_Parameter_din_ChargeParameterDiscoveryRes
  6663. DESCRIPTION:
  6664. PRE-CONDITION:
  6665. INPUT:
  6666. OUTPUT:
  6667. GLOBAL VARIABLES:
  6668. =============================================================================*/
  6669. void Sudo_Parameter_din_ChargeParameterDiscoveryRes()
  6670. {
  6671. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  6672. init_dinChargeParameterDiscoveryResType(&ccs_exi_doc_DIN.V2G_Message.Body.ChargeParameterDiscoveryRes);
  6673. ccs_exi_doc_DIN.V2G_Message.Body.ChargeParameterDiscoveryRes_isUsed = 1u;
  6674. //----- [BODY (1/5)] ResponseCode -----
  6675. struct dinChargeParameterDiscoveryResType *res;
  6676. res = &ccs_exi_doc_DIN.V2G_Message.Body.ChargeParameterDiscoveryRes;
  6677. res->ResponseCode = dinresponseCodeType_OK;
  6678. //----- [BODY (2/5)] EVSEProcessing -----
  6679. res->EVSEProcessing = dinEVSEProcessingType_Finished; //0
  6680. // dinEVSEProcessingType_Finished = 0,
  6681. // dinEVSEProcessingType_Ongoing = 1
  6682. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  6683. res->SAScheduleList_isUsed = 1u;
  6684. struct dinSAScheduleListType *list;
  6685. list = &res->SAScheduleList;
  6686. //
  6687. list->SAScheduleTuple.arrayLen = 1;
  6688. list->SAScheduleTuple.array[0].SAScheduleTupleID = 0;
  6689. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleID = 0;
  6690. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.arrayLen = 1;
  6691. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval_isUsed = 1u;
  6692. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.start = 0;
  6693. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration = xxx
  6694. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration_isUsed = 1u;
  6695. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval_isUsed = 0u;
  6696. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval.xxx
  6697. //list->SAScheduleTuple.array[0].SalesTariff.xxx
  6698. //list->SAScheduleTuple.array[0].SalesTariff_isUsed = 0u;
  6699. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  6700. //ignore, since DIN doesn't support AC
  6701. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  6702. res->DC_EVSEChargeParameter_isUsed = 1u;
  6703. struct dinDC_EVSEChargeParameterType *para;
  6704. para = &res->DC_EVSEChargeParameter;
  6705. //DC_EVSEStatus
  6706. para->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  6707. para->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  6708. // dinisolationLevelType_Invalid = 0,
  6709. // dinisolationLevelType_Valid = 1,
  6710. // dinisolationLevelType_Warning = 2,
  6711. // dinisolationLevelType_Fault = 3
  6712. para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  6713. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6714. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  6715. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6716. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6717. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6718. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6719. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6720. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  6721. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  6722. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  6723. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  6724. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  6725. para->DC_EVSEStatus.NotificationMaxDelay = 0u;
  6726. para->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  6727. // dinEVSENotificationType_None = 0,
  6728. // dinEVSENotificationType_StopCharging = 1,
  6729. // dinEVSENotificationType_ReNegotiation = 2
  6730. //EVSEMaximumCurrentLimit
  6731. para->EVSEMaximumCurrentLimit.Value = 2400;
  6732. para->EVSEMaximumCurrentLimit.Multiplier = -1;
  6733. para->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  6734. para->EVSEMaximumCurrentLimit.Unit = dinunitSymbolType_A;
  6735. // dinunitSymbolType_h = 0,
  6736. // dinunitSymbolType_m = 1,
  6737. // dinunitSymbolType_s = 2,
  6738. // dinunitSymbolType_A = 3,
  6739. // dinunitSymbolType_Ah = 4,
  6740. // dinunitSymbolType_V = 5,
  6741. // dinunitSymbolType_VA = 6,
  6742. // dinunitSymbolType_W = 7,
  6743. // dinunitSymbolType_W_s = 8,
  6744. // dinunitSymbolType_Wh = 9
  6745. //EVSEMaximumPowerLimit
  6746. para->EVSEMaximumPowerLimit.Value = 6000;
  6747. para->EVSEMaximumPowerLimit.Multiplier = 1;
  6748. para->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  6749. para->EVSEMaximumPowerLimit.Unit = dinunitSymbolType_W;
  6750. //EVSEMaximumVoltageLimit
  6751. para->EVSEMaximumVoltageLimit.Value = 7500;
  6752. para->EVSEMaximumVoltageLimit.Multiplier = -1;
  6753. para->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  6754. para->EVSEMaximumVoltageLimit.Unit = dinunitSymbolType_V;
  6755. //EVSEMinimumVoltageLimit
  6756. para->EVSEMinimumVoltageLimit.Value = 1500;
  6757. para->EVSEMinimumVoltageLimit.Multiplier = -1;
  6758. para->EVSEMinimumVoltageLimit.Unit_isUsed = 1u;
  6759. para->EVSEMinimumVoltageLimit.Unit = dinunitSymbolType_V;
  6760. //EVSEMinimumCurrentLimit
  6761. para->EVSEMinimumCurrentLimit.Value = 20;
  6762. para->EVSEMinimumCurrentLimit.Multiplier = -1;
  6763. para->EVSEMinimumCurrentLimit.Unit_isUsed = 1u;
  6764. para->EVSEMinimumCurrentLimit.Unit = dinunitSymbolType_A;
  6765. //EVSECurrentRegulationTolerance_isUsed
  6766. para->EVSECurrentRegulationTolerance_isUsed = 1u;
  6767. //EVSECurrentRegulationTolerance
  6768. para->EVSECurrentRegulationTolerance.Value = 10;
  6769. para->EVSECurrentRegulationTolerance.Multiplier = -1;
  6770. para->EVSECurrentRegulationTolerance.Unit_isUsed = 1u;
  6771. para->EVSECurrentRegulationTolerance.Unit = dinunitSymbolType_A;
  6772. //EVSEEnergyToBeDelivered_isUsed
  6773. para->EVSEEnergyToBeDelivered_isUsed = 1u;
  6774. //EVSEPeakCurrentRipple
  6775. para->EVSEPeakCurrentRipple.Value = 2;
  6776. para->EVSEPeakCurrentRipple.Multiplier = -1;
  6777. para->EVSEPeakCurrentRipple.Unit_isUsed = 1u;
  6778. para->EVSEPeakCurrentRipple.Unit = dinunitSymbolType_A;
  6779. //EVSEEnergyToBeDelivered (optional)
  6780. /*
  6781. para->EVSEEnergyToBeDelivered.Value = 360;
  6782. para->EVSEEnergyToBeDelivered.Multiplier = 3;
  6783. para->EVSEEnergyToBeDelivered.Unit_isUsed = 1u;
  6784. para->EVSEEnergyToBeDelivered.Unit = dinunitSymbolType_Wh;
  6785. */
  6786. }
  6787. /*===========================================================================
  6788. FUNCTION: Sudo_Parameter_iso1_ChargeParameterDiscoveryRes
  6789. DESCRIPTION:
  6790. PRE-CONDITION:
  6791. INPUT:
  6792. OUTPUT:
  6793. GLOBAL VARIABLES:
  6794. =============================================================================*/
  6795. void Sudo_Parameter_iso1_ChargeParameterDiscoveryRes()
  6796. {
  6797. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  6798. init_iso1ChargeParameterDiscoveryResType(&ccs_exi_doc_ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes);
  6799. ccs_exi_doc_ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes_isUsed = 1u;
  6800. //----- [BODY (1/5)] ResponseCode -----
  6801. struct iso1ChargeParameterDiscoveryResType *res;
  6802. res = &ccs_exi_doc_ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes;
  6803. res->ResponseCode = iso1responseCodeType_OK;
  6804. //----- [BODY (2/5)] EVSEProcessing -----
  6805. res->EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  6806. // iso1EVSEProcessingType_Finished = 0,
  6807. // iso1EVSEProcessingType_Ongoing = 1
  6808. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  6809. res->SAScheduleList_isUsed = 1u;
  6810. struct iso1SAScheduleListType *list;
  6811. list = &res->SAScheduleList;
  6812. //
  6813. list->SAScheduleTuple.arrayLen = 1;
  6814. list->SAScheduleTuple.array[0].SAScheduleTupleID = 0;
  6815. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.arrayLen = 1;
  6816. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval_isUsed = 1u;
  6817. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.start = 0;
  6818. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration = xxx
  6819. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration_isUsed = 1u;
  6820. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval_isUsed = 0u;
  6821. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval.xxx
  6822. //list->SAScheduleTuple.array[0].SalesTariff.xxx
  6823. //list->SAScheduleTuple.array[0].SalesTariff_isUsed = 0u;
  6824. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  6825. //ignore, since our ISO1 doesn't support AC, yet
  6826. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  6827. res->DC_EVSEChargeParameter_isUsed = 1u;
  6828. struct iso1DC_EVSEChargeParameterType *para;
  6829. para = &res->DC_EVSEChargeParameter;
  6830. //DC_EVSEStatus
  6831. para->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  6832. para->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  6833. // iso1isolationLevelType_Invalid = 0,
  6834. // iso1isolationLevelType_Valid = 1,
  6835. // iso1isolationLevelType_Warning = 2,
  6836. // iso1isolationLevelType_Fault = 3
  6837. para->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  6838. // iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6839. // iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  6840. // iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6841. // iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6842. // iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6843. // iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6844. // iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6845. // iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  6846. // iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  6847. // iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  6848. // iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  6849. // iso1DC_EVSEStatusCodeType_Reserved_C = 11
  6850. para->DC_EVSEStatus.NotificationMaxDelay = 0u;
  6851. para->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  6852. // iso1EVSENotificationType_None = 0,
  6853. // iso1EVSENotificationType_StopCharging = 1,
  6854. // iso1EVSENotificationType_ReNegotiation = 2
  6855. //EVSEMaximumCurrentLimit
  6856. para->EVSEMaximumCurrentLimit.Value = 2400;
  6857. para->EVSEMaximumCurrentLimit.Multiplier = -1;
  6858. //para->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  6859. para->EVSEMaximumCurrentLimit.Unit = iso1unitSymbolType_A;
  6860. //iso1unitSymbolType_h = 0,
  6861. //iso1unitSymbolType_m = 1,
  6862. //iso1unitSymbolType_s = 2,
  6863. //iso1unitSymbolType_A = 3,
  6864. //iso1unitSymbolType_V = 4,
  6865. //iso1unitSymbolType_W = 5,
  6866. //iso1unitSymbolType_Wh = 6
  6867. //EVSEMaximumPowerLimit
  6868. para->EVSEMaximumPowerLimit.Value = 6000;
  6869. para->EVSEMaximumPowerLimit.Multiplier = 1;
  6870. //para->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  6871. para->EVSEMaximumPowerLimit.Unit = iso1unitSymbolType_W;
  6872. //EVSEMaximumVoltageLimit
  6873. para->EVSEMaximumVoltageLimit.Value = 7500;
  6874. para->EVSEMaximumVoltageLimit.Multiplier = -1;
  6875. //para->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  6876. para->EVSEMaximumVoltageLimit.Unit = iso1unitSymbolType_V;
  6877. //EVSEMinimumVoltageLimit
  6878. para->EVSEMinimumVoltageLimit.Value = 1500;
  6879. para->EVSEMinimumVoltageLimit.Multiplier = -1;
  6880. //para->EVSEMinimumVoltageLimit.Unit_isUsed = 1u;
  6881. para->EVSEMinimumVoltageLimit.Unit = iso1unitSymbolType_V;
  6882. //EVSEMinimumCurrentLimit
  6883. para->EVSEMinimumCurrentLimit.Value = 20;
  6884. para->EVSEMinimumCurrentLimit.Multiplier = -1;
  6885. //para->EVSEMinimumCurrentLimit.Unit_isUsed = 1u;
  6886. para->EVSEMinimumCurrentLimit.Unit = iso1unitSymbolType_A;
  6887. //EVSECurrentRegulationTolerance_isUsed
  6888. para->EVSECurrentRegulationTolerance_isUsed = 1u;
  6889. //EVSECurrentRegulationTolerance
  6890. para->EVSECurrentRegulationTolerance.Value = 10;
  6891. para->EVSECurrentRegulationTolerance.Multiplier = -1;
  6892. //para->EVSECurrentRegulationTolerance.Unit_isUsed = 1u;
  6893. para->EVSECurrentRegulationTolerance.Unit = iso1unitSymbolType_A;
  6894. //EVSEEnergyToBeDelivered_isUsed
  6895. para->EVSEEnergyToBeDelivered_isUsed = 1u;
  6896. //EVSEPeakCurrentRipple
  6897. para->EVSEPeakCurrentRipple.Value = 2;
  6898. para->EVSEPeakCurrentRipple.Multiplier = -1;
  6899. //para->EVSEPeakCurrentRipple.Unit_isUsed = 1u;
  6900. para->EVSEPeakCurrentRipple.Unit = iso1unitSymbolType_A;
  6901. //EVSEEnergyToBeDelivered (optional)
  6902. /*
  6903. para->EVSEEnergyToBeDelivered.Value = 360;
  6904. para->EVSEEnergyToBeDelivered.Multiplier = 3;
  6905. para->EVSEEnergyToBeDelivered.Unit_isUsed = 1u;
  6906. para->EVSEEnergyToBeDelivered.Unit = iso1unitSymbolType_Wh;
  6907. */
  6908. }
  6909. /*===========================================================================
  6910. FUNCTION: Proc_din_ChargeParameterDiscoveryRes
  6911. DESCRIPTION:
  6912. PRE-CONDITION:
  6913. INPUT:
  6914. OUTPUT:
  6915. GLOBAL VARIABLES:
  6916. =============================================================================*/
  6917. int Proc_din_ChargeParameterDiscoveryRes(int AcceptFd)
  6918. {
  6919. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6920. //if it is not the same, the packet should be ignored.
  6921. //STEP 1: ============ Initialize ============
  6922. int errn = 0;
  6923. bitstream_t v2g_tx_stream;
  6924. struct ChargeParameterDiscoveryResponse_DIN70121 *res;
  6925. struct ChargeParameterDiscoveryRequest_DIN70121 *req;
  6926. struct DC_EVSEChargeParameterType_DIN70121 *dc_para;
  6927. struct ChargingInfoData *sys;
  6928. size_t pos = 0;
  6929. v2g_tx_stream.pos = &pos;
  6930. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6931. v2g_tx_stream.data = V2GTP_Tx_buf;
  6932. res = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse;
  6933. req = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest;
  6934. dc_para = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse.DC_EVSEChargeParameter;
  6935. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6936. res->ResponseCode = OK_DIN70121;
  6937. res->EVSEProcessing = Ongoing_DIN70121;
  6938. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready; //1
  6939. //[HEADER] Check Req SessionID
  6940. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  6941. {
  6942. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  6943. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  6944. errn = -1;
  6945. }
  6946. //Check for SequenceError
  6947. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6948. {
  6949. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  6950. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  6951. errn = -1;
  6952. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6953. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6954. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6955. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6956. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6957. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6958. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6959. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6960. }
  6961. //[TC_SECC_VTB_ChargeParameterDiscovery_005]
  6962. if (sys->EvBatteryMaxCurrent < 0)
  6963. {
  6964. sprintf((char*)buf_log_evcomm,
  6965. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  6966. sys->EvBatteryMaxCurrent);
  6967. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6968. res->ResponseCode = dinresponseCodeType_FAILED_WrongChargeParameter; //16
  6969. res->EVSEProcessing = Finished_DIN70121;
  6970. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  6971. errn = -1;
  6972. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6973. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  6974. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6975. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6976. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6977. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6978. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  6979. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  6980. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6981. }
  6982. //STEP 2: ============ Modify Parameters of ShmCcsData ============
  6983. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_DIN70121);
  6984. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_DIN70121);
  6985. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_DIN70121);
  6986. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMinimumVoltageLimit, 1500, V_DIN70121); //150V
  6987. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMinimumCurrentLimit, 10, A_DIN70121); //1A
  6988. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSECurrentRegulationTolerance, 10, A_DIN70121); //1A
  6989. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEPeakCurrentRipple, 2, A_DIN70121); //0.2A
  6990. //SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEEnergyToBeDelivered, 0, WH_DIN70121); //optional
  6991. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  6992. if (sys->EvBatteryMaxVoltage <= 500)
  6993. {
  6994. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumVoltageLimit, (int) (500 * 10), V_DIN70121); //500V
  6995. }
  6996. #endif
  6997. //for test with Tesla Model 3, 10A
  6998. //SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumCurrentLimit, (int) (10 * 10), A_DIN70121); //10A, for testing with Tesla Model 3
  6999. //Check for EnergyTransferMode [TC_SECC_VTB_ChargeParameterDiscovery_004]
  7000. if (req->EVRequestedEnergyTransferType != DC_extended)
  7001. {
  7002. sprintf((char*)buf_log_evcomm,
  7003. "[ERROR]Wrong EVRequestedEnergyTransferType(%d,%d)",
  7004. req->EVRequestedEnergyTransferType,
  7005. dinEVSESupportedEnergyTransferType_DC_extended);
  7006. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7007. res->ResponseCode = FAILED_WrongEnergyTransferType_DIN70121;
  7008. res->EVSEProcessing = Finished_DIN70121;
  7009. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7010. errn = -1;
  7011. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7012. Update_V2G_Flow_Status(Other_Fault);
  7013. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7014. //CCS_SECC_ResponseCode_FAILED_WrongEnergyTransferMode (023774)
  7015. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7016. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7017. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7018. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7019. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  7020. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  7021. }
  7022. //Check for CSU command of "Stop by EVSE"
  7023. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7024. {
  7025. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ChargeParameterDiscoveryRes]EVSE_Shutdown");
  7026. res->ResponseCode = FAILED_DIN70121;
  7027. res->EVSEProcessing = Finished_DIN70121;
  7028. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7029. errn = -1;
  7030. }
  7031. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7032. {
  7033. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ChargeParameterDiscoveryRes]EVSE_EmergencyShutdown");
  7034. res->ResponseCode = FAILED_DIN70121;
  7035. res->EVSEProcessing = Finished_DIN70121;
  7036. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7037. errn = -1;
  7038. }
  7039. //Detect for CP State should be 9V (State B)
  7040. #if CP_PROTECTION_MECHANISM == ENABLE
  7041. if (sys->CpState != 3) //State B1, B2
  7042. {
  7043. res->ResponseCode = FAILED_DIN70121;
  7044. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7045. res->EVSEProcessing = Finished_DIN70121;
  7046. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7047. Update_V2G_Flow_Status(Other_Fault);
  7048. errn = -1;
  7049. sprintf((char*)buf_log_evcomm,
  7050. "[Proc_din_ChargeParameterDiscoveryRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  7051. sys->CpState,
  7052. sys->CpVoltage);
  7053. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7054. }
  7055. #endif
  7056. //Check for Permission from CSU
  7057. if (ShmInternalComm->ChargingPermission == TRUE)
  7058. {
  7059. res->EVSEProcessing = Finished_DIN70121;
  7060. }
  7061. //Check for Permission Off
  7062. if (ShmInternalComm->ChargingPermission == FALSE)
  7063. {
  7064. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][ChargeParameterDiscoveryRes]Permission OFF");
  7065. res->ResponseCode = FAILED_DIN70121;
  7066. res->EVSEProcessing = Finished_DIN70121;
  7067. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7068. errn = -1;
  7069. }
  7070. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7071. #if PARAMETER_NORMAL_MODE == ENABLE
  7072. SHM_Read_din_ChargeParameterDiscoveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  7073. #else
  7074. Sudo_Parameter_din_ChargeParameterDiscoveryRes();
  7075. #endif
  7076. //STEP 4: ============ Encode and Send Response Message ===========
  7077. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  7078. {
  7079. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  7080. errn = -1;
  7081. }
  7082. //STPE 5: ============ Update Flags ============
  7083. return errn;
  7084. }
  7085. /*===========================================================================
  7086. FUNCTION: Proc_iso1_ChargeParameterDiscoveryRes
  7087. DESCRIPTION:
  7088. PRE-CONDITION:
  7089. INPUT:
  7090. OUTPUT:
  7091. GLOBAL VARIABLES:
  7092. =============================================================================*/
  7093. int Proc_iso1_ChargeParameterDiscoveryRes(int AcceptFd)
  7094. {
  7095. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  7096. //if it is not the same, the packet should be ignored.
  7097. //STEP 1: ============ Initialize ============
  7098. int errn = 0;
  7099. bitstream_t v2g_tx_stream;
  7100. struct ChargeParameterDiscoveryResponse_ISO15118_2014 *res;
  7101. struct ChargeParameterDiscoveryRequest_ISO15118_2014 *req;
  7102. struct DC_EVSEChargeParameterType_ISO15118_2014 *dc_para;
  7103. struct AC_EVSEChargeParameterType_ISO15118_2014 *ac_para;
  7104. struct ChargingInfoData *sys;
  7105. size_t pos = 0;
  7106. v2g_tx_stream.pos = &pos;
  7107. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7108. v2g_tx_stream.data = V2GTP_Tx_buf;
  7109. res = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse;
  7110. req = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest;
  7111. dc_para = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse.DC_EVSEChargeParameter;
  7112. ac_para = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse.AC_EVSEChargeParameter;
  7113. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7114. res->ResponseCode = OK_ISO15118_2014;
  7115. /*+++ 20200808, vern, EVSEProcessing should be on-going during ChargeParameterDiscovery +++*/
  7116. res->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  7117. /*--- 20200808, vern, EVSEProcessing should be on-going during ChargeParameterDiscovery ---*/
  7118. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready; //1
  7119. ac_para->AC_EVSEStatus.RCD = ShmInternalComm->AC_RcdStatus; //0:no error, 1:error
  7120. ac_para->AC_EVSEStatus.NotificationMaxDelay = 0; //unit: 1s
  7121. ac_para->AC_EVSEStatus.EVSENotification = ShmInternalComm->AC_EVSENotification; //0:none, 1:StopCharging, 2:RenNgotiation
  7122. //[HEADER] Check Req SessionID
  7123. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  7124. {
  7125. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  7126. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  7127. errn = -1;
  7128. }
  7129. //Check for SequenceError
  7130. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7131. {
  7132. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  7133. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  7134. errn = -1;
  7135. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7136. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7137. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7138. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7139. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7140. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7141. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7142. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7143. }
  7144. //[TC_SECC_VTB_ChargeParameterDiscovery_005]
  7145. /*+++ 20200808, vern, should check the voltage and current to see if the range of battery parameter is accepted by charger ---*/
  7146. if ((sys->EvBatteryMaxCurrent < 0) ||(sys->EvBatteryMaxVoltage<150))
  7147. {
  7148. sprintf((char*)buf_log_evcomm,
  7149. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  7150. sys->EvBatteryMaxCurrent);
  7151. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7152. res->ResponseCode = iso1responseCodeType_FAILED_WrongChargeParameter; //16
  7153. res->EVSEProcessing = Finished_ISO15118_2014;
  7154. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7155. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7156. errn = -1;
  7157. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7158. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  7159. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7160. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7161. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7162. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7163. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  7164. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  7165. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7166. }
  7167. //STEP 2: ============ Modify Parameters of ShmCcsData ============
  7168. //DC
  7169. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_ISO15118_2014);
  7170. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  7171. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_ISO15118_2014);
  7172. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMinimumVoltageLimit, 1500, V_ISO15118_2014); //150V
  7173. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMinimumCurrentLimit, 10, A_ISO15118_2014); //1A
  7174. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSECurrentRegulationTolerance, 10, A_ISO15118_2014); //1A
  7175. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEPeakCurrentRipple, 2, A_ISO15118_2014); //0.2A
  7176. //SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEEnergyToBeDelivered, 0, Wh_ISO15118_2014); //optional
  7177. //AC
  7178. SAVE_PhysicalValueType_ISO15118_2014(&ac_para->EVSENominalVoltage, (int) (sys->AC_EVSENominalVoltage * 10), V_ISO15118_2014);
  7179. SAVE_PhysicalValueType_ISO15118_2014(&ac_para->EVSEMaxCurrent, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  7180. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  7181. if (sys->EvBatteryMaxVoltage <= 500)
  7182. {
  7183. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumVoltageLimit, (int) (500 * 10), V_ISO15118_2014); //500V
  7184. }
  7185. #endif
  7186. //for test with Tesla Model 3, 10A
  7187. //SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumCurrentLimit, (int) (10 * 10), A_ISO15118_2014); //10A, for testing with Tesla Model 3
  7188. //Check for EnergyTransferMode
  7189. if (req->RequestedEnergyTransferMode != ShmCcsData->EnergyTransferMode) //[CAUTION] Their table should be kept as the same.
  7190. {
  7191. sprintf((char*)buf_log_evcomm,
  7192. "[ERROR]Unmatched RequestedEnergyTransferMode(%d,%d)",
  7193. req->RequestedEnergyTransferMode,
  7194. ShmCcsData->EnergyTransferMode);
  7195. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7196. res->ResponseCode = FAILED_WrongEnergyTransferMode_ISO15118_2014;
  7197. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7198. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7199. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7200. errn = -1;
  7201. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7202. Update_V2G_Flow_Status(Other_Fault);
  7203. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7204. //CCS_SECC_ResponseCode_FAILED_WrongEnergyTransferMode (023774)
  7205. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7206. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7207. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7208. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7209. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  7210. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  7211. }
  7212. //Check for CSU command of "Stop by EVSE"
  7213. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7214. {
  7215. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargeParameterDiscoveryRes]EVSE_Shutdown");
  7216. res->ResponseCode = FAILED_ISO15118_2014;
  7217. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7218. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7219. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7220. errn = -1;
  7221. }
  7222. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7223. {
  7224. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargeParameterDiscoveryRes]EVSE_EmergencyShutdown");
  7225. res->ResponseCode = FAILED_ISO15118_2014;
  7226. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7227. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7228. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7229. errn = -1;
  7230. }
  7231. //Detect for CP State should be 9V (State B)
  7232. #if CP_PROTECTION_MECHANISM == ENABLE
  7233. if (sys->CpState != 3) //State B1, B2
  7234. {
  7235. res->ResponseCode = FAILED_ISO15118_2014;
  7236. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7237. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7238. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7239. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7240. Update_V2G_Flow_Status(Other_Fault);
  7241. errn = -1;
  7242. sprintf((char*)buf_log_evcomm,
  7243. "[Proc_iso1_ChargeParameterDiscoveryRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  7244. sys->CpState,
  7245. sys->CpVoltage);
  7246. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7247. }
  7248. #endif
  7249. //Check for Permission from CSU
  7250. if(ShmInternalComm->ChargingPermission == TRUE)
  7251. {
  7252. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7253. }
  7254. //Check for Permission Off
  7255. if (ShmInternalComm->ChargingPermission == FALSE)
  7256. {
  7257. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO1][ChargeParameterDiscoveryRes]Permission OFF");
  7258. res->ResponseCode = FAILED_ISO15118_2014;
  7259. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7260. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7261. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7262. errn = -1;
  7263. }
  7264. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7265. #if PARAMETER_NORMAL_MODE == ENABLE
  7266. SHM_Read_iso1_ChargeParameterDiscoveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  7267. #else
  7268. Sudo_Parameter_iso1_ChargeParameterDiscoveryRes();
  7269. #endif
  7270. //STEP 4: ============ Encode and Send Response Message ===========
  7271. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  7272. {
  7273. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  7274. errn = -1;
  7275. }
  7276. //STPE 5: ============ Update Flags ============
  7277. return errn;
  7278. }
  7279. /*===========================================================================
  7280. FUNCTION: Proc_din_ChargeParameterDiscoveryReq
  7281. DESCRIPTION:
  7282. PRE-CONDITION:
  7283. INPUT:
  7284. OUTPUT:
  7285. GLOBAL VARIABLES:
  7286. =============================================================================*/
  7287. int Proc_din_ChargeParameterDiscoveryReq(int AcceptFd)
  7288. {
  7289. int errn = 0;
  7290. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ChargeParameterDiscoveryReq] Got ChargeParameterDiscoveryReq");
  7291. //ftime(&SeqStartTime);
  7292. //Print the decoded XML Document
  7293. PRINT_XML_DOC_DIN_ChargeParameterDiscoveryReq(&ccs_exi_doc_DIN);
  7294. //Save into Share Memory
  7295. SHM_Save_din_ChargeParameterDiscoveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  7296. //Check for EV Error Code
  7297. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.DC_EVStatus.EVErrorCode);
  7298. errn = Proc_din_ChargeParameterDiscoveryRes(AcceptFd);
  7299. //ftime(&SeqEndTime);
  7300. //DEBUG_PRINTF_EVCOMM_DETAIL("\ndelta = %.02lf ms\n", DiffTimeb(SeqStartTime, SeqEndTime));
  7301. if ( errn == 0)
  7302. {
  7303. //send response successfully.
  7304. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]ChargeParameterDiscoveryRes");
  7305. }
  7306. else
  7307. {
  7308. sprintf((char*)buf_log_evcomm,
  7309. "[Error][DIN][ChargeParameterDiscoveryRes]%d (DEC)",
  7310. errn);
  7311. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7312. }
  7313. return errn;
  7314. }
  7315. /*===========================================================================
  7316. FUNCTION: Proc_iso1_ChargeParameterDiscoveryReq
  7317. DESCRIPTION:
  7318. PRE-CONDITION:
  7319. INPUT:
  7320. OUTPUT:
  7321. GLOBAL VARIABLES:
  7322. =============================================================================*/
  7323. int Proc_iso1_ChargeParameterDiscoveryReq(int AcceptFd)
  7324. {
  7325. int errn = 0;
  7326. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargeParameterDiscoveryReq] Got ChargeParameterDiscoveryReq");
  7327. //ftime(&SeqStartTime);
  7328. //Print the decoded XML Document
  7329. PRINT_XML_DOC_ISO1_ChargeParameterDiscoveryReq(&ccs_exi_doc_ISO1);
  7330. //Save into Share Memory
  7331. SHM_Save_iso1_ChargeParameterDiscoveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  7332. //Check for EV Error Code
  7333. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.DC_EVStatus.EVErrorCode);
  7334. errn = Proc_iso1_ChargeParameterDiscoveryRes(AcceptFd);
  7335. //ftime(&SeqEndTime);
  7336. //DEBUG_PRINTF_EVCOMM_DETAIL("\ndelta = %.02lf ms\n", DiffTimeb(SeqStartTime, SeqEndTime));
  7337. if (errn == 0)
  7338. {
  7339. //send response successfully.
  7340. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]ChargeParameterDiscoveryRes");
  7341. }
  7342. else
  7343. {
  7344. sprintf((char*)buf_log_evcomm,
  7345. "[Error][ISO1][ChargeParameterDiscoveryRes]%d (DEC)",
  7346. errn);
  7347. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7348. }
  7349. return errn;
  7350. }
  7351. /*===========================================================================
  7352. FUNCTION: SHM_Init_din_CableCheckRes
  7353. DESCRIPTION:
  7354. PRE-CONDITION:
  7355. INPUT:
  7356. 1. shm_ccs
  7357. OUTPUT:
  7358. 1. shm_ccs
  7359. GLOBAL VARIABLES:
  7360. =============================================================================*/
  7361. void SHM_Init_din_CableCheckRes(struct CcsData *shm_ccs)
  7362. {
  7363. struct CableCheckResponse_DIN70121 *in;
  7364. in = &shm_ccs->V2GMessage_DIN70121.CableCheckResponse;
  7365. //----- [BODY (1/3)] ResponseCode -----
  7366. in->ResponseCode = dinresponseCodeType_OK;
  7367. //----- [BODY (2/3)] EVSEProcessing -----
  7368. //in->EVSEProcessing = dinEVSEProcessingType_Finished; //for test
  7369. in->EVSEProcessing = dinEVSEProcessingType_Ongoing; //default
  7370. // dinEVSEProcessingType_Finished = 0,
  7371. // dinEVSEProcessingType_Ongoing = 1
  7372. //----- [BODY (3/3)] DC_EVSEStatus -----
  7373. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  7374. in->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0 (default)
  7375. // dinisolationLevelType_Invalid = 0,
  7376. // dinisolationLevelType_Valid = 1, (default)
  7377. // dinisolationLevelType_Warning = 2,
  7378. // dinisolationLevelType_Fault = 3
  7379. }
  7380. /*===========================================================================
  7381. FUNCTION: Sudo_Parameter_din_CableCheckRes
  7382. DESCRIPTION:
  7383. PRE-CONDITION:
  7384. INPUT:
  7385. OUTPUT:
  7386. GLOBAL VARIABLES:
  7387. =============================================================================*/
  7388. void Sudo_Parameter_din_CableCheckRes()
  7389. {
  7390. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  7391. init_dinCableCheckResType(&ccs_exi_doc_DIN.V2G_Message.Body.CableCheckRes);
  7392. ccs_exi_doc_DIN.V2G_Message.Body.CableCheckRes_isUsed = 1u;
  7393. //----- [BODY (1/3)] ResponseCode -----
  7394. struct dinCableCheckResType *res;
  7395. res = &ccs_exi_doc_DIN.V2G_Message.Body.CableCheckRes;
  7396. res->ResponseCode = dinresponseCodeType_OK;
  7397. //----- [BODY (2/3)] EVSEProcessing -----
  7398. res->EVSEProcessing = dinEVSEProcessingType_Finished; //0
  7399. // dinEVSEProcessingType_Finished = 0,
  7400. // dinEVSEProcessingType_Ongoing = 1
  7401. //----- [BODY (3/3)] DC_EVSEStatus -----
  7402. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7403. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  7404. // dinisolationLevelType_Invalid = 0,
  7405. // dinisolationLevelType_Valid = 1,
  7406. // dinisolationLevelType_Warning = 2,
  7407. // dinisolationLevelType_Fault = 3
  7408. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7409. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7410. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  7411. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7412. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7413. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7414. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7415. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7416. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  7417. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  7418. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  7419. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  7420. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  7421. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7422. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  7423. // dinEVSENotificationType_None = 0,
  7424. // dinEVSENotificationType_StopCharging = 1,
  7425. // dinEVSENotificationType_ReNegotiation = 2
  7426. }
  7427. /*===========================================================================
  7428. FUNCTION: Sudo_Parameter_iso1_CableCheckRes
  7429. DESCRIPTION:
  7430. PRE-CONDITION:
  7431. INPUT:
  7432. OUTPUT:
  7433. GLOBAL VARIABLES:
  7434. =============================================================================*/
  7435. void Sudo_Parameter_iso1_CableCheckRes()
  7436. {
  7437. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  7438. init_iso1CableCheckResType(&ccs_exi_doc_ISO1.V2G_Message.Body.CableCheckRes);
  7439. ccs_exi_doc_ISO1.V2G_Message.Body.CableCheckRes_isUsed = 1u;
  7440. //----- [BODY (1/3)] ResponseCode -----
  7441. struct iso1CableCheckResType *res;
  7442. res = &ccs_exi_doc_ISO1.V2G_Message.Body.CableCheckRes;
  7443. res->ResponseCode = iso1responseCodeType_OK;
  7444. //----- [BODY (2/3)] EVSEProcessing -----
  7445. res->EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  7446. //iso1EVSEProcessingType_Finished = 0,
  7447. //iso1EVSEProcessingType_Ongoing = 1,
  7448. //iso1EVSEProcessingType_Ongoing_WaitingForCustomerInteraction = 2
  7449. //----- [BODY (3/3)] DC_EVSEStatus -----
  7450. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7451. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  7452. //iso1isolationLevelType_Invalid = 0,
  7453. //iso1isolationLevelType_Valid = 1,
  7454. //iso1isolationLevelType_Warning = 2,
  7455. //iso1isolationLevelType_Fault = 3,
  7456. //iso1isolationLevelType_No_IMD = 4
  7457. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  7458. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7459. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  7460. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7461. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7462. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7463. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7464. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7465. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  7466. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  7467. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  7468. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  7469. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  7470. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7471. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  7472. //iso1EVSENotificationType_None = 0,
  7473. //iso1EVSENotificationType_StopCharging = 1,
  7474. //iso1EVSENotificationType_ReNegotiation = 2
  7475. }
  7476. /*===========================================================================
  7477. FUNCTION: Proc_din_CableCheckRes
  7478. DESCRIPTION:
  7479. PRE-CONDITION:
  7480. INPUT:
  7481. OUTPUT:
  7482. GLOBAL VARIABLES:
  7483. =============================================================================*/
  7484. int Proc_din_CableCheckRes(int AcceptFd)
  7485. {
  7486. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  7487. //if it is not the same, the packet should be ignored.
  7488. //STEP 1: ============ Initialize ============
  7489. //int i = 0;
  7490. int errn = 0;
  7491. bitstream_t v2g_tx_stream;
  7492. static struct CableCheckResponse_DIN70121 *cab;
  7493. static struct ChargingInfoData *sys;
  7494. size_t pos = 0;
  7495. v2g_tx_stream.pos = &pos;
  7496. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7497. v2g_tx_stream.data = V2GTP_Tx_buf;
  7498. cab = &ShmCcsData->V2GMessage_DIN70121.CableCheckResponse;
  7499. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7500. cab->ResponseCode = OK_DIN70121;
  7501. cab->cnt++;
  7502. DEBUG_PRINTF_EVCOMM_DETAIL("CP_State = %d (%.02f V), V_now = %.02f, Isolation Status = %d, EVSEProcessing = %d\n",
  7503. sys->CpState,
  7504. sys->CpVoltage,
  7505. sys->PresentChargingVoltage,
  7506. sys->IsolationStatus,
  7507. cab->EVSEProcessing);
  7508. //[HEADER] Check Req SessionID
  7509. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  7510. {
  7511. cab->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  7512. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  7513. errn = -1;
  7514. }
  7515. //Check for SequenceError
  7516. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7517. {
  7518. cab->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  7519. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  7520. errn = -1;
  7521. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7522. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7523. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7524. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7525. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7526. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7527. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7528. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7529. }
  7530. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  7531. if (sys->IsolationStatus == GFD_Invalid) //0: on going
  7532. {
  7533. //For PSU
  7534. sys->EvBatterytargetVoltage = CABLECHECK_TARGET_VOLTAGE; //500V //[To-Do] this should be modified to auto decision.
  7535. sys->EvBatterytargetCurrent = CABLECHECK_TARGET_CURRENT; //2A(default), unit: 1A
  7536. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  7537. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive; //4
  7538. cab->EVSEProcessing = dinEVSEProcessingType_Ongoing;
  7539. }
  7540. else if ((sys->IsolationStatus == GFD_Valid) || (sys->IsolationStatus == GFD_Warning)) //1: valid, 2: warning
  7541. {
  7542. //For PSU
  7543. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7544. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7545. if (sys->PresentChargingVoltage < 60) // < 60V
  7546. {
  7547. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  7548. sprintf((char*)buf_log_evcomm, "[V2G][CableCheck]Pass (V_now = %.02f, Isolated = %d)",
  7549. sys->PresentChargingVoltage,
  7550. sys->IsolationStatus);
  7551. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7552. //IsolationStatus
  7553. if (sys->IsolationStatus == GFD_Valid)
  7554. {
  7555. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  7556. }
  7557. else if (sys->IsolationStatus == GFD_Warning)
  7558. {
  7559. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning;
  7560. }
  7561. else
  7562. {
  7563. sprintf((char*)buf_log_evcomm,
  7564. "[WARNING]unexpected IsolationStatus(%d)",
  7565. sys->IsolationStatus);
  7566. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7567. }
  7568. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7569. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7570. }
  7571. else
  7572. {
  7573. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  7574. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  7575. cab->EVSEProcessing = dinEVSEProcessingType_Ongoing;
  7576. }
  7577. }
  7578. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  7579. {
  7580. //For PSU
  7581. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7582. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7583. cab->ResponseCode = FAILED_DIN70121;
  7584. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7585. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_NotReady;
  7586. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7587. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_CableCheckRes]IsolationStatus = 3 (fault)");
  7588. errn = -1;
  7589. }
  7590. else
  7591. {
  7592. //For PSU
  7593. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7594. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7595. cab->ResponseCode = FAILED_DIN70121;
  7596. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  7597. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_NotReady;
  7598. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7599. sprintf((char*)buf_log_evcomm,
  7600. "[Proc_din_CableCheckRes]Undefined Isolation Status(%d)",
  7601. sys->IsolationStatus);
  7602. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7603. }
  7604. //Check for CSU command of "Stop by EVSE"
  7605. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7606. {
  7607. cab->ResponseCode = FAILED_DIN70121;
  7608. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7609. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7610. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7611. errn = -1;
  7612. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_CableCheckRes]EVSE_Shutdown");
  7613. }
  7614. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7615. {
  7616. cab->ResponseCode = FAILED_DIN70121;
  7617. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7618. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7619. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7620. errn = -1;
  7621. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_CableCheckRes]EVSE_EmergencyShutdown");
  7622. }
  7623. else if (ShmInternalComm->ChargingPermission == FALSE)
  7624. {
  7625. cab->ResponseCode = FAILED_DIN70121;
  7626. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7627. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7628. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7629. errn = -1;
  7630. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_CableCheckRes]ChargingPermission = FALSE");
  7631. }
  7632. else
  7633. {
  7634. //null
  7635. }
  7636. //Response to CP Error
  7637. #if CP_PROTECTION_MECHANISM == ENABLE
  7638. //#if 1
  7639. if (sys->CableCheckPreCountDownDone == TRUE) //[To-Do] Here should be modified to 2 seconds
  7640. {
  7641. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  7642. {
  7643. cab->ResponseCode = FAILED_DIN70121;
  7644. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7645. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7646. Update_V2G_Flow_Status(Other_Fault);
  7647. errn = -1;
  7648. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7649. //CCS_SECC_CP_State_Error (023889)
  7650. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7651. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7652. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7653. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  7654. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  7655. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  7656. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  7657. sprintf((char*)buf_log_evcomm,
  7658. "[CableCheck]Emergency Stop by CP Error (%d, %.02f V, %d)\n",
  7659. sys->CpState,
  7660. sys->CpVoltage,
  7661. cab->cnt
  7662. );
  7663. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7664. //cab->cnt = 0;
  7665. }
  7666. }
  7667. #endif
  7668. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7669. #if PARAMETER_NORMAL_MODE == ENABLE
  7670. SHM_Read_din_CableCheckRes(&ccs_exi_doc_DIN, ShmCcsData);
  7671. #else
  7672. Sudo_Parameter_din_CableCheckRes();
  7673. #endif
  7674. //STEP 4: ============ Encode and Send Response Message ===========
  7675. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  7676. {
  7677. errn = -1;
  7678. }
  7679. //STEP 5: ============ Update Flags ===========
  7680. sys->ConnectorLocked = TRUE; //Inicating EVSE that the CCS Connector is Locked.
  7681. return errn;
  7682. }
  7683. /*===========================================================================
  7684. FUNCTION: Proc_iso1_CableCheckRes
  7685. DESCRIPTION:
  7686. PRE-CONDITION:
  7687. INPUT:
  7688. OUTPUT:
  7689. GLOBAL VARIABLES:
  7690. =============================================================================*/
  7691. int Proc_iso1_CableCheckRes(int AcceptFd)
  7692. {
  7693. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  7694. //if it is not the same, the packet should be ignored.
  7695. //STEP 1: ============ Initialize ============
  7696. //int i = 0;
  7697. int errn = 0;
  7698. bitstream_t v2g_tx_stream;
  7699. static struct CableCheckResponse_ISO15118_2014 *cab;
  7700. static struct ChargingInfoData *sys;
  7701. size_t pos = 0;
  7702. v2g_tx_stream.pos = &pos;
  7703. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7704. v2g_tx_stream.data = V2GTP_Tx_buf;
  7705. cab = &ShmCcsData->V2GMessage_ISO15118_2014.CableCheckResponse;
  7706. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7707. cab->ResponseCode = OK_ISO15118_2014;
  7708. cab->cnt++;
  7709. DEBUG_PRINTF_EVCOMM_DETAIL("CP_State = %d (%.02f V), V_now = %.02f, Isolation Status = %d, EVSEProcessing = %d\n",
  7710. sys->CpState,
  7711. sys->CpVoltage,
  7712. sys->PresentChargingVoltage,
  7713. sys->IsolationStatus,
  7714. cab->EVSEProcessing);
  7715. //[HEADER] Check Req SessionID
  7716. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  7717. {
  7718. cab->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  7719. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  7720. errn = -1;
  7721. }
  7722. //Check for SequenceError
  7723. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7724. {
  7725. cab->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  7726. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  7727. errn = -1;
  7728. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7729. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7730. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7731. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7732. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7733. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7734. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7735. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7736. }
  7737. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  7738. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  7739. {
  7740. //For PSU
  7741. sys->EvBatterytargetVoltage = CABLECHECK_TARGET_VOLTAGE; //500V //[To-Do] this should be modified to auto decision.
  7742. sys->EvBatterytargetCurrent = CABLECHECK_TARGET_CURRENT; //2A(default), unit: 1A
  7743. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  7744. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  7745. cab->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  7746. }
  7747. else if ((sys->IsolationStatus == GFD_Valid) || (sys->IsolationStatus == GFD_Warning)) //1: valid, 2:waring
  7748. {
  7749. //For PSU
  7750. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7751. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7752. if (sys->PresentChargingVoltage < 60) // < 60V
  7753. {
  7754. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  7755. sprintf((char*)buf_log_evcomm, "[V2G][CableCheck]Pass (V_now = %.02f, Isolated = %d)",
  7756. sys->PresentChargingVoltage,
  7757. sys->IsolationStatus);
  7758. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7759. //IsolationStatus
  7760. if (sys->IsolationStatus == GFD_Valid)
  7761. {
  7762. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  7763. }
  7764. else if (sys->IsolationStatus == GFD_Warning)
  7765. {
  7766. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Warning;
  7767. }
  7768. else
  7769. {
  7770. sprintf((char*)buf_log_evcomm,
  7771. "[WARNING]unexpected IsolationStatus(%d)",
  7772. sys->IsolationStatus);
  7773. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7774. }
  7775. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  7776. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7777. }
  7778. else
  7779. {
  7780. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  7781. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  7782. cab->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  7783. }
  7784. }
  7785. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  7786. {
  7787. //For PSU
  7788. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7789. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7790. cab->ResponseCode = FAILED_ISO15118_2014;
  7791. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7792. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_NotReady;
  7793. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7794. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CableCheckRes]IsolationStatus = 3 (fault)");
  7795. errn = -1;
  7796. }
  7797. //else if (sys->IsolationStatus == GFD_No_IMD){} //only for ISO15118
  7798. else
  7799. {
  7800. //For PSU
  7801. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7802. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7803. cab->ResponseCode = FAILED_ISO15118_2014;
  7804. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7805. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_NotReady;
  7806. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7807. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CableCheckRes]Undefined Isolation Status.");
  7808. }
  7809. //Check for CSU command of "Stop by EVSE"
  7810. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7811. {
  7812. cab->ResponseCode = FAILED_ISO15118_2014;
  7813. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7814. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7815. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7816. errn = -1;
  7817. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CableCheckRes]EVSE_Shutdown");
  7818. }
  7819. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7820. {
  7821. cab->ResponseCode = FAILED_ISO15118_2014;
  7822. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7823. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7824. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7825. errn = -1;
  7826. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CableCheckRes]EVSE_EmergencyShutdown");
  7827. }
  7828. else if (ShmInternalComm->ChargingPermission == FALSE)
  7829. {
  7830. cab->ResponseCode = FAILED_ISO15118_2014;
  7831. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7832. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7833. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7834. errn = -1;
  7835. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_CableCheckRes]ChargingPermission = FALSE");
  7836. }
  7837. else
  7838. {
  7839. //null
  7840. }
  7841. //Response to CP Error
  7842. #if CP_PROTECTION_MECHANISM == ENABLE
  7843. if (sys->CableCheckPreCountDownDone == TRUE) //[To-Do] Here should be modified to 2 seconds
  7844. {
  7845. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  7846. {
  7847. cab->ResponseCode = FAILED_ISO15118_2014;
  7848. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7849. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7850. Update_V2G_Flow_Status(Other_Fault);
  7851. errn = -1;
  7852. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7853. //CCS_SECC_CP_State_Error (023889)
  7854. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7855. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7856. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7857. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  7858. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  7859. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  7860. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  7861. sprintf((char*)buf_log_evcomm,
  7862. "[CableCheck]Emergency Stop by CP Error (%d, %.02f V, %d)\n",
  7863. sys->CpState,
  7864. sys->CpVoltage,
  7865. cab->cnt
  7866. );
  7867. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7868. //cab->cnt = 0;
  7869. }
  7870. }
  7871. #endif
  7872. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7873. #if PARAMETER_NORMAL_MODE == ENABLE
  7874. SHM_Read_iso1_CableCheckRes(&ccs_exi_doc_ISO1, ShmCcsData);
  7875. #else
  7876. Sudo_Parameter_iso1_CableCheckRes();
  7877. #endif
  7878. //STEP 4: ============ Encode and Send Response Message ===========
  7879. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  7880. {
  7881. errn = -1;
  7882. }
  7883. //STEP 5: ============ Update Flags ===========
  7884. sys->ConnectorLocked = TRUE; //Inicating EVSE that the CCS Connector is Locked.
  7885. return errn;
  7886. }
  7887. /*===========================================================================
  7888. FUNCTION: Proc_din_CableCheckReq
  7889. DESCRIPTION:
  7890. PRE-CONDITION:
  7891. INPUT:
  7892. OUTPUT:
  7893. GLOBAL VARIABLES:
  7894. =============================================================================*/
  7895. int Proc_din_CableCheckReq(int AcceptFd)
  7896. {
  7897. int errn = 0;
  7898. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_CableCheckReq] Got CableCheckReq\n");
  7899. //Print the decoded XML Document
  7900. PRINT_XML_DOC_DIN_CableCheckReq(&ccs_exi_doc_DIN);
  7901. //Save into Share Memory
  7902. SHM_Save_din_CableCheckReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  7903. //Check for EV Error Code
  7904. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.CableCheckRequest.DC_EVStatus.EVErrorCode);
  7905. errn = Proc_din_CableCheckRes(AcceptFd);
  7906. if (errn == 0)
  7907. {
  7908. //send response successfully.
  7909. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]CableCheckRes");
  7910. }
  7911. else
  7912. {
  7913. sprintf((char*)buf_log_evcomm,
  7914. "[Error][SeccComm][Proc_din_CableCheckReq]Proc_iso1_CableCheckRes(): %d (DEC)",
  7915. errn);
  7916. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7917. }
  7918. return errn;
  7919. }
  7920. /*===========================================================================
  7921. FUNCTION: Proc_iso1_CableCheckReq
  7922. DESCRIPTION:
  7923. PRE-CONDITION:
  7924. INPUT:
  7925. OUTPUT:
  7926. GLOBAL VARIABLES:
  7927. =============================================================================*/
  7928. int Proc_iso1_CableCheckReq(int AcceptFd)
  7929. {
  7930. int errn = 0;
  7931. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CableCheckReq] Got CableCheckReq");
  7932. //Print the decoded XML Document
  7933. PRINT_XML_DOC_ISO1_CableCheckReq(&ccs_exi_doc_ISO1);
  7934. //Save into Share Memory
  7935. SHM_Save_iso1_CableCheckReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  7936. //Check for EV Error Code
  7937. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.CableCheckRequest.DC_EVStatus.EVErrorCode);
  7938. errn = Proc_iso1_CableCheckRes(AcceptFd);
  7939. if (errn == 0)
  7940. {
  7941. //send response successfully.
  7942. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]CableCheckRes");
  7943. }
  7944. else
  7945. {
  7946. sprintf((char*)buf_log_evcomm,
  7947. "[Error][SeccComm][Proc_iso1_CableCheckReq]Proc_iso1_CableCheckRes(): %d (DEC)",
  7948. errn);
  7949. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7950. }
  7951. return errn;
  7952. }
  7953. /*===========================================================================
  7954. FUNCTION: SHM_Init_din_PreChargeRes
  7955. DESCRIPTION:
  7956. PRE-CONDITION:
  7957. INPUT:
  7958. 1. shm_ccs
  7959. OUTPUT:
  7960. 1. shm_ccs
  7961. GLOBAL VARIABLES:
  7962. =============================================================================*/
  7963. void SHM_Init_din_PreChargeRes(struct CcsData *shm_ccs)
  7964. {
  7965. struct PreChargeResponse_DIN70121 *in;
  7966. in = &shm_ccs->V2GMessage_DIN70121.PreChargeResponse;
  7967. //----- [BODY (1/3)] ResponseCode -----
  7968. in->ResponseCode = dinresponseCodeType_OK;
  7969. //----- [BODY (2/3)] EVSEPresentVoltage -----
  7970. short value = 0; int multiplier = 0; unsigned char unit = 0;
  7971. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for Test
  7972. value = 0; multiplier = 0; unit = V_DIN70121; //waiting for CsuComm to update V to fit EV Target
  7973. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  7974. //----- [BODY (3/3)] DC_EVSEStatus -----
  7975. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  7976. }
  7977. /*===========================================================================
  7978. FUNCTION: Sudo_Parameter_din_PreChargeRes
  7979. DESCRIPTION:
  7980. PRE-CONDITION:
  7981. INPUT:
  7982. OUTPUT:
  7983. GLOBAL VARIABLES:
  7984. =============================================================================*/
  7985. void Sudo_Parameter_din_PreChargeRes()
  7986. {
  7987. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  7988. init_dinPreChargeResType(&ccs_exi_doc_DIN.V2G_Message.Body.PreChargeRes);
  7989. ccs_exi_doc_DIN.V2G_Message.Body.PreChargeRes_isUsed = 1u;
  7990. //----- [BODY (1/3)] ResponseCode -----
  7991. struct dinPreChargeResType *res;
  7992. res = &ccs_exi_doc_DIN.V2G_Message.Body.PreChargeRes;
  7993. res->ResponseCode = dinresponseCodeType_OK;
  7994. //----- [BODY (2/3)] EVSEPresentVoltage -----
  7995. res->EVSEPresentVoltage.Value = 3820;
  7996. res->EVSEPresentVoltage.Multiplier = -1;
  7997. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  7998. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  7999. // dinunitSymbolType_h = 0,
  8000. // dinunitSymbolType_m = 1,
  8001. // dinunitSymbolType_s = 2,
  8002. // dinunitSymbolType_A = 3,
  8003. // dinunitSymbolType_Ah = 4,
  8004. // dinunitSymbolType_V = 5,
  8005. // dinunitSymbolType_VA = 6,
  8006. // dinunitSymbolType_W = 7,
  8007. // dinunitSymbolType_W_s = 8,
  8008. // dinunitSymbolType_Wh = 9
  8009. //----- [BODY (3/3)] DC_EVSEStatus -----
  8010. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8011. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  8012. // dinisolationLevelType_Invalid = 0,
  8013. // dinisolationLevelType_Valid = 1,
  8014. // dinisolationLevelType_Warning = 2,
  8015. // dinisolationLevelType_Fault = 3
  8016. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8017. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8018. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  8019. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8020. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8021. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8022. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8023. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8024. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  8025. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  8026. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  8027. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  8028. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  8029. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8030. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  8031. // dinEVSENotificationType_None = 0,
  8032. // dinEVSENotificationType_StopCharging = 1,
  8033. // dinEVSENotificationType_ReNegotiation = 2
  8034. }
  8035. /*===========================================================================
  8036. FUNCTION: Sudo_Parameter_iso1_PreChargeRes
  8037. DESCRIPTION:
  8038. PRE-CONDITION:
  8039. INPUT:
  8040. OUTPUT:
  8041. GLOBAL VARIABLES:
  8042. =============================================================================*/
  8043. void Sudo_Parameter_iso1_PreChargeRes()
  8044. {
  8045. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  8046. init_iso1PreChargeResType(&ccs_exi_doc_ISO1.V2G_Message.Body.PreChargeRes);
  8047. ccs_exi_doc_ISO1.V2G_Message.Body.PreChargeRes_isUsed = 1u;
  8048. //----- [BODY (1/3)] ResponseCode -----
  8049. struct iso1PreChargeResType *res;
  8050. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PreChargeRes;
  8051. res->ResponseCode = iso1responseCodeType_OK;
  8052. //----- [BODY (2/3)] EVSEPresentVoltage -----
  8053. res->EVSEPresentVoltage.Value = 3820;
  8054. res->EVSEPresentVoltage.Multiplier = -1;
  8055. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  8056. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  8057. //iso1unitSymbolType_h = 0,
  8058. //iso1unitSymbolType_m = 1,
  8059. //iso1unitSymbolType_s = 2,
  8060. //iso1unitSymbolType_A = 3,
  8061. //iso1unitSymbolType_V = 4,
  8062. //iso1unitSymbolType_W = 5,
  8063. //iso1unitSymbolType_Wh = 6
  8064. //----- [BODY (3/3)] DC_EVSEStatus -----
  8065. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8066. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  8067. //iso1isolationLevelType_Invalid = 0,
  8068. //iso1isolationLevelType_Valid = 1,
  8069. //iso1isolationLevelType_Warning = 2,
  8070. //iso1isolationLevelType_Fault = 3,
  8071. //iso1isolationLevelType_No_IMD = 4
  8072. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8073. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8074. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  8075. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8076. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8077. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8078. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8079. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8080. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  8081. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  8082. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  8083. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  8084. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  8085. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8086. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8087. //iso1EVSENotificationType_None = 0,
  8088. //iso1EVSENotificationType_StopCharging = 1,
  8089. //iso1EVSENotificationType_ReNegotiation = 2
  8090. }
  8091. /*===========================================================================
  8092. FUNCTION: Proc_din_PreChargeRes
  8093. DESCRIPTION:
  8094. PRE-CONDITION:
  8095. INPUT:
  8096. OUTPUT:
  8097. GLOBAL VARIABLES:
  8098. =============================================================================*/
  8099. int Proc_din_PreChargeRes(int AcceptFd)
  8100. {
  8101. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8102. //if it is not the same, the packet should be ignored.
  8103. //STEP 1: ============ Initialize ============
  8104. //int i = 0;
  8105. int errn = 0;
  8106. bitstream_t v2g_tx_stream;
  8107. static struct PreChargeResponse_DIN70121 *pre;
  8108. static struct ChargingInfoData *sys;
  8109. size_t pos = 0;
  8110. v2g_tx_stream.pos = &pos;
  8111. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8112. v2g_tx_stream.data = V2GTP_Tx_buf;
  8113. pre = &ShmCcsData->V2GMessage_DIN70121.PreChargeResponse;
  8114. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8115. pre->ResponseCode = OK_DIN70121;
  8116. //EVSE Status Code
  8117. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8118. //[HEADER] Check Req SessionID
  8119. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  8120. {
  8121. pre->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  8122. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  8123. errn = -1;
  8124. }
  8125. //Check for SequenceError
  8126. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8127. {
  8128. pre->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  8129. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  8130. errn = -1;
  8131. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8132. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8133. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8134. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8135. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8136. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8137. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8138. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8139. }
  8140. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8141. SAVE_PhysicalValueType_DIN70121(&pre->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  8142. //Isolation Status
  8143. if (sys->IsolationStatus == GFD_Invalid) //0: invalid
  8144. {
  8145. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  8146. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8147. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8148. Update_V2G_Flow_Status(Other_Fault);
  8149. errn = -1;
  8150. sprintf((char*)buf_log_evcomm, "[Error][Proc_din_PreChargeRes]IsolationStatus = %d", sys->IsolationStatus);
  8151. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8152. }
  8153. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  8154. {
  8155. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  8156. }
  8157. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  8158. {
  8159. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  8160. }
  8161. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  8162. {
  8163. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]GFD_Fault => Emergency Shutdown");
  8164. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  8165. pre->ResponseCode = FAILED_DIN70121;
  8166. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8167. errn = -1;
  8168. }
  8169. else //GFD_No_IMD or other unexpected status
  8170. {
  8171. pre->ResponseCode = FAILED_DIN70121;
  8172. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  8173. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8174. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8175. Update_V2G_Flow_Status(Other_Fault);
  8176. errn = -1;
  8177. sprintf((char*)buf_log_evcomm,
  8178. "[Error][Proc_din_PreChargeRes]IsolationStatus = %d (undefined)",
  8179. sys->IsolationStatus);
  8180. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8181. }
  8182. //Check for CSU command of "Stop by EVSE"
  8183. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8184. {
  8185. //Check for Alarm Code: CCS GFD trip (012235)
  8186. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8187. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8188. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8189. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8190. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8191. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8192. {
  8193. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]CCS GFD trip => EVSE_Shutdown");
  8194. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  8195. }
  8196. else
  8197. {
  8198. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]EVSE_Shutdown");
  8199. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8200. }
  8201. pre->ResponseCode = FAILED_DIN70121;
  8202. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8203. errn = -1;
  8204. }
  8205. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8206. {
  8207. //Check for Alarm Code: CCS GFD trip (012235)
  8208. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8209. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8210. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8211. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8212. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8213. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8214. {
  8215. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]CCS GFD trip => EVSE_EmergencyShutdown");
  8216. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  8217. }
  8218. else
  8219. {
  8220. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]EVSE_EmergencyShutdown");
  8221. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8222. }
  8223. pre->ResponseCode = FAILED_DIN70121;
  8224. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8225. errn = -1;
  8226. }
  8227. else if (ShmInternalComm->ChargingPermission == FALSE)
  8228. {
  8229. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]ChargingPermission = FALSE");
  8230. pre->ResponseCode = FAILED_DIN70121;
  8231. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8232. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8233. errn = -1;
  8234. }
  8235. else
  8236. {
  8237. //null
  8238. }
  8239. //Response to CP Error
  8240. #if CP_PROTECTION_MECHANISM == ENABLE
  8241. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  8242. {
  8243. pre->ResponseCode = FAILED_DIN70121;
  8244. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8245. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8246. Update_V2G_Flow_Status(Other_Fault);
  8247. errn = -1;
  8248. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8249. //CCS_SECC_CP_State_Error (023889)
  8250. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8251. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8252. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8253. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  8254. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  8255. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  8256. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  8257. sprintf((char*)buf_log_evcomm,
  8258. "[Error][Proc_din_PreChargeRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  8259. sys->CpState,
  8260. sys->CpVoltage
  8261. );
  8262. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8263. }
  8264. #endif
  8265. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8266. #if PARAMETER_NORMAL_MODE == ENABLE
  8267. SHM_Read_din_PreChargeRes(&ccs_exi_doc_DIN, ShmCcsData);
  8268. #else
  8269. Sudo_Parameter_din_PreChargeRes();
  8270. #endif
  8271. //STEP 4: ============ Encode and Send Response Message ===========
  8272. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  8273. {
  8274. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][send_encoded_din_V2GTP_Stream]:Tx encoded msg error");
  8275. errn = -1;
  8276. }
  8277. //STEP 5: ============ Update Flags ===========
  8278. return errn;
  8279. }
  8280. /*===========================================================================
  8281. FUNCTION: Proc_iso1_PreChargeRes
  8282. DESCRIPTION:
  8283. PRE-CONDITION:
  8284. INPUT:
  8285. OUTPUT:
  8286. GLOBAL VARIABLES:
  8287. =============================================================================*/
  8288. int Proc_iso1_PreChargeRes(int AcceptFd)
  8289. {
  8290. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8291. //if it is not the same, the packet should be ignored.
  8292. //STEP 1: ============ Initialize ============
  8293. //int i = 0;
  8294. int errn = 0;
  8295. bitstream_t v2g_tx_stream;
  8296. static struct PreChargeResponse_ISO15118_2014 *pre;
  8297. static struct ChargingInfoData *sys;
  8298. size_t pos = 0;
  8299. v2g_tx_stream.pos = &pos;
  8300. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8301. v2g_tx_stream.data = V2GTP_Tx_buf;
  8302. pre = &ShmCcsData->V2GMessage_ISO15118_2014.PreChargeResponse;
  8303. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8304. pre->ResponseCode = OK_ISO15118_2014;
  8305. //EVSE Status Code
  8306. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8307. //[HEADER] Check Req SessionID
  8308. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  8309. {
  8310. pre->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  8311. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  8312. errn = -1;
  8313. }
  8314. //Check for SequenceError
  8315. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8316. {
  8317. pre->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  8318. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  8319. errn = -1;
  8320. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8321. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8322. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8323. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8324. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8325. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8326. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8327. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8328. }
  8329. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8330. SAVE_PhysicalValueType_ISO15118_2014(&pre->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  8331. //Isolation Status
  8332. if (sys->IsolationStatus == GFD_Invalid) //0: invalid(on going)
  8333. {
  8334. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  8335. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8336. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8337. Update_V2G_Flow_Status(Other_Fault);
  8338. errn = -1;
  8339. sprintf((char*)buf_log_evcomm, "[Error][Proc_iso1_PreChargeRes]IsolationStatus = %d", sys->IsolationStatus);
  8340. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8341. }
  8342. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  8343. {
  8344. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  8345. }
  8346. else if (sys->IsolationStatus == GFD_Warning) //2: waring
  8347. {
  8348. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Warning; //1
  8349. }
  8350. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  8351. {
  8352. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]GFD_Fault => Emergency Shutdown");
  8353. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  8354. pre->ResponseCode = FAILED_ISO15118_2014;
  8355. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8356. errn = -1;
  8357. }
  8358. else
  8359. {
  8360. pre->ResponseCode = FAILED_ISO15118_2014;
  8361. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  8362. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8363. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8364. Update_V2G_Flow_Status(Other_Fault);
  8365. errn = -1;
  8366. sprintf((char*)buf_log_evcomm,
  8367. "[Error][Proc_iso1_PreChargeRes]IsolationStatus = %d (undefined)",
  8368. sys->IsolationStatus);
  8369. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8370. }
  8371. //Check for CSU command of "Stop by EVSE"
  8372. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8373. {
  8374. //Check for Alarm Code: CCS GFD trip (012235)
  8375. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8376. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8377. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8378. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8379. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8380. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8381. {
  8382. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]CCS GFD trip => EVSE_Shutdown");
  8383. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  8384. }
  8385. else
  8386. {
  8387. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]EVSE_Shutdown");
  8388. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8389. }
  8390. pre->ResponseCode = FAILED_ISO15118_2014;
  8391. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8392. errn = -1;
  8393. }
  8394. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8395. {
  8396. //Check for Alarm Code: CCS GFD trip (012235)
  8397. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8398. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8399. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8400. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8401. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8402. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8403. {
  8404. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]CCS GFD trip => EVSE_EmergencyShutdown");
  8405. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  8406. }
  8407. else
  8408. {
  8409. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]EVSE_EmergencyShutdown");
  8410. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8411. }
  8412. pre->ResponseCode = FAILED_ISO15118_2014;
  8413. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8414. errn = -1;
  8415. }
  8416. else if (ShmInternalComm->ChargingPermission == FALSE)
  8417. {
  8418. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]ChargingPermission = FALSE");
  8419. pre->ResponseCode = FAILED_ISO15118_2014;
  8420. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8421. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8422. errn = -1;
  8423. }
  8424. else
  8425. {
  8426. //null
  8427. }
  8428. //Response to CP Error
  8429. #if CP_PROTECTION_MECHANISM == ENABLE
  8430. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  8431. {
  8432. pre->ResponseCode = FAILED_ISO15118_2014;
  8433. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8434. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8435. Update_V2G_Flow_Status(Other_Fault);
  8436. errn = -1;
  8437. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8438. //CCS_SECC_CP_State_Error (023889)
  8439. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8440. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8441. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8442. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  8443. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  8444. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  8445. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  8446. sprintf((char*)buf_log_evcomm,
  8447. "[Error][Proc_iso1_PreChargeRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  8448. sys->CpState,
  8449. sys->CpVoltage
  8450. );
  8451. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8452. }
  8453. #endif
  8454. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8455. #if PARAMETER_NORMAL_MODE == ENABLE
  8456. SHM_Read_iso1_PreChargeRes(&ccs_exi_doc_ISO1, ShmCcsData);
  8457. #else
  8458. Sudo_Parameter_iso1_PreChargeRes();
  8459. #endif
  8460. //STEP 4: ============ Encode and Send Response Message ===========
  8461. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  8462. {
  8463. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][send_encoded_iso1_V2GTP_Stream]:Tx encoded msg error");
  8464. errn = -1;
  8465. }
  8466. //STEP 5: ============ Update Flags ===========
  8467. return errn;
  8468. }
  8469. /*===========================================================================
  8470. FUNCTION: Proc_din_PreChargeReq
  8471. DESCRIPTION:
  8472. PRE-CONDITION:
  8473. INPUT:
  8474. OUTPUT:
  8475. GLOBAL VARIABLES:
  8476. =============================================================================*/
  8477. int Proc_din_PreChargeReq(int AcceptFd)
  8478. {
  8479. int errn = 0;
  8480. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_PreChargeReq] Got PreChargeReq");
  8481. //Print the decoded XML Document
  8482. PRINT_XML_DOC_DIN_PreChargeReq(&ccs_exi_doc_DIN);
  8483. //Save into Share Memory
  8484. SHM_Save_din_PreChargeReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  8485. //Check for EV Error Code
  8486. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PreChargeRequest.DC_EVStatus.EVErrorCode);
  8487. errn = Proc_din_PreChargeRes(AcceptFd);
  8488. if (errn == 0)
  8489. {
  8490. //send response successfully.
  8491. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]PreChargeRes");
  8492. }
  8493. else
  8494. {
  8495. sprintf((char*)buf_log_evcomm,
  8496. "[Error][SeccComm][Proc_din_PreChargeReq]Proc_iso1_PreChargeRes(): %d (DEC)",
  8497. errn);
  8498. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8499. }
  8500. return errn;
  8501. }
  8502. /*===========================================================================
  8503. FUNCTION: Proc_iso1_PreChargeReq
  8504. DESCRIPTION:
  8505. PRE-CONDITION:
  8506. INPUT:
  8507. OUTPUT:
  8508. GLOBAL VARIABLES:
  8509. =============================================================================*/
  8510. int Proc_iso1_PreChargeReq(int AcceptFd)
  8511. {
  8512. int errn = 0;
  8513. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_PreChargeReq] Got PreChargeReq");
  8514. //Print the decoded XML Document
  8515. PRINT_XML_DOC_ISO1_PreChargeReq(&ccs_exi_doc_ISO1);
  8516. //Save into Share Memory
  8517. SHM_Save_iso1_PreChargeReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  8518. //Check for EV Error Code
  8519. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PreChargeRequest.DC_EVStatus.EVErrorCode);
  8520. errn = Proc_iso1_PreChargeRes(AcceptFd);
  8521. if (errn == 0)
  8522. {
  8523. //send response successfully.
  8524. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]PreChargeRes");
  8525. }
  8526. else
  8527. {
  8528. sprintf((char*)buf_log_evcomm,
  8529. "[Error][SeccComm][Proc_iso1_PreChargeReq]Proc_iso1_PreChargeRes(): %d (DEC)",
  8530. errn);
  8531. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8532. }
  8533. return errn;
  8534. }
  8535. /*===========================================================================
  8536. FUNCTION: SHM_Init_din_PowerDeliveryRes
  8537. DESCRIPTION:
  8538. PRE-CONDITION:
  8539. INPUT:
  8540. 1. shm_ccs
  8541. OUTPUT:
  8542. 1. shm_ccs
  8543. GLOBAL VARIABLES:
  8544. =============================================================================*/
  8545. void SHM_Init_din_PowerDeliveryRes(struct CcsData *shm_ccs)
  8546. {
  8547. struct PowerDeliveryResponse_DIN70121 *in;
  8548. in = &shm_ccs->V2GMessage_DIN70121.PowerDeliveryResponse;
  8549. //----- [BODY (1/3)] ResponseCode -----
  8550. in->ResponseCode = dinresponseCodeType_OK;
  8551. //----- [BODY (2/3)] AC_EVSEStatus -----
  8552. //ignore, since DIN 70121 doesn't support AC, yet.
  8553. //----- [BODY (2/3)] DC_EVSEStatus -----
  8554. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  8555. }
  8556. /*===========================================================================
  8557. FUNCTION: Sudo_Parameter_din_PowerDeliveryRes
  8558. DESCRIPTION:
  8559. PRE-CONDITION:
  8560. INPUT:
  8561. OUTPUT:
  8562. GLOBAL VARIABLES:
  8563. =============================================================================*/
  8564. void Sudo_Parameter_din_PowerDeliveryRes()
  8565. {
  8566. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  8567. init_dinPowerDeliveryResType(&ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes);
  8568. ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes_isUsed = 1u;
  8569. //----- [BODY (1/3)] ResponseCode -----
  8570. struct dinPowerDeliveryResType *res;
  8571. res = &ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes;
  8572. res->ResponseCode = dinresponseCodeType_OK;
  8573. //----- [BODY (2/3)] AC_EVSEStatus -----
  8574. //ignore, since DIN 70121 doesn't support AC, yet.
  8575. //----- [BODY (2/3)] DC_EVSEStatus -----
  8576. res->DC_EVSEStatus_isUsed = 1u;
  8577. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8578. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  8579. // dinisolationLevelType_Invalid = 0,
  8580. // dinisolationLevelType_Valid = 1,
  8581. // dinisolationLevelType_Warning = 2,
  8582. // dinisolationLevelType_Fault = 3
  8583. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8584. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8585. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  8586. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8587. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8588. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8589. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8590. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8591. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  8592. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  8593. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  8594. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  8595. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  8596. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8597. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  8598. // dinEVSENotificationType_None = 0,
  8599. // dinEVSENotificationType_StopCharging = 1,
  8600. // dinEVSENotificationType_ReNegotiation = 2
  8601. }
  8602. /*===========================================================================
  8603. FUNCTION: Sudo_Parameter_iso1_PowerDeliveryRes
  8604. DESCRIPTION:
  8605. PRE-CONDITION:
  8606. INPUT:
  8607. OUTPUT:
  8608. GLOBAL VARIABLES:
  8609. =============================================================================*/
  8610. void Sudo_Parameter_iso1_PowerDeliveryRes()
  8611. {
  8612. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  8613. init_iso1PowerDeliveryResType(&ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes);
  8614. ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes_isUsed = 1u;
  8615. //----- [BODY (1/3)] ResponseCode -----
  8616. struct iso1PowerDeliveryResType *res;
  8617. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes;
  8618. res->ResponseCode = iso1responseCodeType_OK;
  8619. //----- [BODY (2/3)] AC_EVSEStatus -----
  8620. //ignore, since our ISO1 70121 doesn't support AC, yet.
  8621. //----- [BODY (2/3)] DC_EVSEStatus -----
  8622. res->DC_EVSEStatus_isUsed = 1u;
  8623. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8624. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  8625. //iso1isolationLevelType_Invalid = 0,
  8626. //iso1isolationLevelType_Valid = 1,
  8627. //iso1isolationLevelType_Warning = 2,
  8628. //iso1isolationLevelType_Fault = 3,
  8629. //iso1isolationLevelType_No_IMD = 4
  8630. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8631. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8632. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  8633. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8634. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8635. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8636. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8637. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8638. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  8639. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  8640. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  8641. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  8642. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  8643. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8644. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8645. //iso1EVSENotificationType_None = 0,
  8646. //iso1EVSENotificationType_StopCharging = 1,
  8647. //iso1EVSENotificationType_ReNegotiation = 2
  8648. }
  8649. /*===========================================================================
  8650. FUNCTION: Proc_din_PowerDeliveryStartRes
  8651. DESCRIPTION:
  8652. PRE-CONDITION:
  8653. INPUT:
  8654. OUTPUT:
  8655. GLOBAL VARIABLES:
  8656. =============================================================================*/
  8657. int Proc_din_PowerDeliveryStartRes(int AcceptFd)
  8658. {
  8659. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8660. //if it is not the same, the packet should be ignored.
  8661. //STEP 1: ============ Initialize ============
  8662. //int i = 0;
  8663. int errn = 0;
  8664. bitstream_t v2g_tx_stream;
  8665. struct dinPowerDeliveryResType *res;
  8666. struct ChargingInfoData *sys;
  8667. size_t pos = 0;
  8668. v2g_tx_stream.pos = &pos;
  8669. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8670. v2g_tx_stream.data = V2GTP_Tx_buf;
  8671. res = &ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes;
  8672. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8673. res->ResponseCode = OK_DIN70121;
  8674. //[HEADER] Check Req SessionID
  8675. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  8676. {
  8677. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  8678. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  8679. errn = -1;
  8680. }
  8681. //Check for SequenceError
  8682. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8683. {
  8684. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  8685. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  8686. errn = -1;
  8687. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8688. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8689. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8690. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8691. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8692. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8693. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8694. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8695. }
  8696. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8697. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8698. #if PARAMETER_NORMAL_MODE == ENABLE
  8699. SHM_Read_din_PowerDeliveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  8700. #else
  8701. Sudo_Parameter_din_PowerDeliveryRes();
  8702. #endif
  8703. //EVSE Status Code
  8704. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  8705. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8706. //Check for CSU command of "Stop by EVSE"
  8707. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8708. {
  8709. //res->ResponseCode = FAILED_DIN70121;
  8710. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8711. }
  8712. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8713. {
  8714. //res->ResponseCode = FAILED_DIN70121;
  8715. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8716. }
  8717. else if (ShmInternalComm->ChargingPermission == FALSE)
  8718. {
  8719. //res->ResponseCode = FAILED_DIN70121;
  8720. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8721. }
  8722. //STEP 4: ============ Encode and Send Response Message ===========
  8723. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  8724. {
  8725. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_PowerDeliveryStartRes][Error]Tx encoded msg error");
  8726. errn = -1;
  8727. }
  8728. //STEP 5: ============ Update Flags ===========
  8729. return errn;
  8730. }
  8731. /*===========================================================================
  8732. FUNCTION: Proc_iso1_PowerDeliveryStartRes
  8733. DESCRIPTION:
  8734. PRE-CONDITION:
  8735. INPUT:
  8736. OUTPUT:
  8737. GLOBAL VARIABLES:
  8738. =============================================================================*/
  8739. int Proc_iso1_PowerDeliveryStartRes(int AcceptFd)
  8740. {
  8741. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8742. //if it is not the same, the packet should be ignored.
  8743. //STEP 1: ============ Initialize ============
  8744. //int i = 0;
  8745. int errn = 0;
  8746. bitstream_t v2g_tx_stream;
  8747. struct iso1PowerDeliveryResType *res;
  8748. struct ChargingInfoData *sys;
  8749. size_t pos = 0;
  8750. v2g_tx_stream.pos = &pos;
  8751. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8752. v2g_tx_stream.data = V2GTP_Tx_buf;
  8753. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes;
  8754. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8755. res->ResponseCode = OK_ISO15118_2014;
  8756. //[HEADER] Check Req SessionID
  8757. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  8758. {
  8759. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  8760. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  8761. errn = -1;
  8762. }
  8763. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8764. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8765. #if PARAMETER_NORMAL_MODE == ENABLE
  8766. SHM_Read_iso1_PowerDeliveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  8767. #else
  8768. Sudo_Parameter_iso1_PowerDeliveryRes();
  8769. #endif
  8770. //EVSE Status Code
  8771. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  8772. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8773. //Check for CSU command of "Stop by EVSE"
  8774. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8775. {
  8776. //res->ResponseCode = FAILED_ISO15118_2014;
  8777. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8778. }
  8779. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8780. {
  8781. //res->ResponseCode = FAILED_ISO15118_2014;
  8782. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8783. }
  8784. else if (ShmInternalComm->ChargingPermission == FALSE)
  8785. {
  8786. //res->ResponseCode = FAILED_ISO15118_2014;
  8787. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8788. }
  8789. //STEP 4: ============ Encode and Send Response Message ===========
  8790. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  8791. {
  8792. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_PowerDeliveryStartRes][Error]Tx encoded msg error");
  8793. errn = -1;
  8794. }
  8795. //STEP 5: ============ Update Flags ===========
  8796. return errn;
  8797. }
  8798. /*===========================================================================
  8799. FUNCTION: Proc_din_PowerDeliveryStartReq
  8800. DESCRIPTION:
  8801. PRE-CONDITION:
  8802. INPUT:
  8803. OUTPUT:
  8804. GLOBAL VARIABLES:
  8805. =============================================================================*/
  8806. int Proc_din_PowerDeliveryStartReq(int AcceptFd)
  8807. {
  8808. int errn = 0;
  8809. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PowerDeliveryStartReq");
  8810. //Print the decoded XML Document
  8811. PRINT_XML_DOC_DIN_PowerDeliveryReq(&ccs_exi_doc_DIN);
  8812. //Save into Share Memory
  8813. SHM_Save_din_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  8814. //Check for EV Error Code
  8815. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  8816. errn = Proc_din_PowerDeliveryStartRes(AcceptFd);
  8817. if (errn == 0)
  8818. {
  8819. //send response successfully.
  8820. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]PowerDeliveryStartRes");
  8821. }
  8822. else
  8823. {
  8824. sprintf((char*)buf_log_evcomm,
  8825. "[Error][Proc_din_PowerDeliveryStartReq]Proc_iso1_PowerDeliveryStartRes(): %d (DEC)",
  8826. errn);
  8827. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8828. }
  8829. return errn;
  8830. }
  8831. /*===========================================================================
  8832. FUNCTION: Proc_iso1_PowerDeliveryStartReq
  8833. DESCRIPTION:
  8834. PRE-CONDITION:
  8835. INPUT:
  8836. OUTPUT:
  8837. GLOBAL VARIABLES:
  8838. =============================================================================*/
  8839. int Proc_iso1_PowerDeliveryStartReq(int AcceptFd)
  8840. {
  8841. int errn = 0;
  8842. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PowerDeliveryStartReq");
  8843. //Print the decoded XML Document
  8844. PRINT_XML_DOC_ISO1_PowerDeliveryReq(&ccs_exi_doc_ISO1);
  8845. //Save into Share Memory
  8846. SHM_Save_iso1_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  8847. //Check for EV Error Code
  8848. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  8849. errn = Proc_iso1_PowerDeliveryStartRes(AcceptFd);
  8850. if (errn == 0)
  8851. {
  8852. //send response successfully.
  8853. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]PowerDeliveryStartRes");
  8854. }
  8855. else
  8856. {
  8857. sprintf((char*)buf_log_evcomm,
  8858. "[Error][Proc_iso1_PowerDeliveryStartReq]Proc_iso1_PowerDeliveryStartRes(): %d (DEC)",
  8859. errn);
  8860. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8861. }
  8862. return errn;
  8863. }
  8864. /*===========================================================================
  8865. FUNCTION: SHM_Init_din_CurrentDemandRes
  8866. DESCRIPTION:
  8867. PRE-CONDITION:
  8868. INPUT:
  8869. 1. shm_ccs
  8870. OUTPUT:
  8871. 1. shm_ccs
  8872. GLOBAL VARIABLES:
  8873. =============================================================================*/
  8874. void SHM_Init_din_CurrentDemandRes(struct CcsData *shm_ccs)
  8875. {
  8876. struct CurrentDemandResponse_DIN70121 *in;
  8877. in = &shm_ccs->V2GMessage_DIN70121.CurrentDemandResponse;
  8878. //----- [BODY (1/10)] ResponseCode -----
  8879. in->ResponseCode = dinresponseCodeType_OK;
  8880. //----- [BODY (2/10)] DC_EVSEStatus -----
  8881. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  8882. //----- [BODY (3/10)] EVSEPresentVoltage -----
  8883. short value = 0; int multiplier = 0; unsigned char unit = 0;
  8884. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for test only.
  8885. value = 0; multiplier = 0; unit = V_DIN70121;
  8886. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  8887. //----- [BODY (4/10)] EVSEPresentCurrent -----
  8888. //value = 600; multiplier = -1; unit = A_DIN70121; //60A, for test only.
  8889. value = 0; multiplier = 0; unit = A_DIN70121;
  8890. SHM_Save_dinPhysicalValueType(&in->EVSEPresentCurrent, value, multiplier, unit);
  8891. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  8892. in->EVSECurrentLimitAchieved = FALSE;
  8893. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  8894. in->EVSEVoltageLimitAchieved = FALSE;
  8895. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  8896. in->EVSEPowerLimitAchieved = FALSE;
  8897. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  8898. //value = 7500; multiplier = -1; unit = V_DIN70121; //750V
  8899. value = 5000; multiplier = -1; unit = V_DIN70121; //500V
  8900. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumVoltageLimit, value, multiplier, unit);
  8901. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  8902. value = 600; multiplier = -1; unit = A_DIN70121; //60A
  8903. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumCurrentLimit, value, multiplier, unit);
  8904. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  8905. value = 3000; multiplier = 1; unit = W_DIN70121; //30KW
  8906. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumPowerLimit, value, multiplier, unit);
  8907. }
  8908. /*===========================================================================
  8909. FUNCTION: Sudo_Parameter_din_CurrentDemandRes
  8910. DESCRIPTION:
  8911. PRE-CONDITION:
  8912. INPUT:
  8913. OUTPUT:
  8914. GLOBAL VARIABLES:
  8915. =============================================================================*/
  8916. void Sudo_Parameter_din_CurrentDemandRes()
  8917. {
  8918. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  8919. init_dinCurrentDemandResType(&ccs_exi_doc_DIN.V2G_Message.Body.CurrentDemandRes);
  8920. ccs_exi_doc_DIN.V2G_Message.Body.CurrentDemandRes_isUsed = 1u;
  8921. //----- [BODY (1/10)] ResponseCode -----
  8922. struct dinCurrentDemandResType *res;
  8923. res = &ccs_exi_doc_DIN.V2G_Message.Body.CurrentDemandRes;
  8924. res->ResponseCode = dinresponseCodeType_OK;
  8925. //----- [BODY (2/10)] DC_EVSEStatus -----
  8926. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8927. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  8928. // dinisolationLevelType_Invalid = 0,
  8929. // dinisolationLevelType_Valid = 1,
  8930. // dinisolationLevelType_Warning = 2,
  8931. // dinisolationLevelType_Fault = 3
  8932. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8933. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8934. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  8935. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8936. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8937. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8938. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8939. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8940. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  8941. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  8942. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  8943. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  8944. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  8945. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8946. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  8947. // dinEVSENotificationType_None = 0,
  8948. // dinEVSENotificationType_StopCharging = 1,
  8949. // dinEVSENotificationType_ReNegotiation = 2
  8950. //----- [BODY (3/10)] EVSEPresentVoltage -----
  8951. res->EVSEPresentVoltage.Value = 3820;
  8952. res->EVSEPresentVoltage.Multiplier = -1;
  8953. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  8954. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  8955. //----- [BODY (4/10)] EVSEPresentCurrent -----
  8956. res->EVSEPresentCurrent.Value = 1200;
  8957. res->EVSEPresentCurrent.Multiplier = -1;
  8958. res->EVSEPresentCurrent.Unit_isUsed = 1u;
  8959. res->EVSEPresentCurrent.Unit = dinunitSymbolType_A;
  8960. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  8961. res->EVSECurrentLimitAchieved = 0;
  8962. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  8963. res->EVSEVoltageLimitAchieved = 0;
  8964. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  8965. res->EVSEPowerLimitAchieved = 0;
  8966. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  8967. res->EVSEMaximumVoltageLimit_isUsed = 1u;
  8968. res->EVSEMaximumVoltageLimit.Value = 7500;
  8969. res->EVSEMaximumVoltageLimit.Multiplier = -1;
  8970. res->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  8971. res->EVSEMaximumVoltageLimit.Unit = dinunitSymbolType_V;
  8972. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  8973. res->EVSEMaximumCurrentLimit_isUsed = 1u;
  8974. res->EVSEMaximumCurrentLimit.Value = 1200;
  8975. res->EVSEMaximumCurrentLimit.Multiplier = -1;
  8976. res->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  8977. res->EVSEMaximumCurrentLimit.Unit = dinunitSymbolType_A;
  8978. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  8979. res->EVSEMaximumPowerLimit_isUsed = 1u;
  8980. res->EVSEMaximumPowerLimit.Value = 6000;
  8981. res->EVSEMaximumPowerLimit.Multiplier = 1;
  8982. res->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  8983. res->EVSEMaximumPowerLimit.Unit = dinunitSymbolType_W;
  8984. }
  8985. /*===========================================================================
  8986. FUNCTION: Sudo_Parameter_iso1_CurrentDemandRes
  8987. DESCRIPTION:
  8988. PRE-CONDITION:
  8989. INPUT:
  8990. OUTPUT:
  8991. GLOBAL VARIABLES:
  8992. =============================================================================*/
  8993. void Sudo_Parameter_iso1_CurrentDemandRes()
  8994. {
  8995. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  8996. init_iso1CurrentDemandResType(&ccs_exi_doc_ISO1.V2G_Message.Body.CurrentDemandRes);
  8997. ccs_exi_doc_ISO1.V2G_Message.Body.CurrentDemandRes_isUsed = 1u;
  8998. //----- [BODY (1/10)] ResponseCode -----
  8999. struct iso1CurrentDemandResType *res;
  9000. res = &ccs_exi_doc_ISO1.V2G_Message.Body.CurrentDemandRes;
  9001. res->ResponseCode = iso1responseCodeType_OK;
  9002. //----- [BODY (2/10)] DC_EVSEStatus -----
  9003. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  9004. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  9005. //iso1isolationLevelType_Invalid = 0,
  9006. //iso1isolationLevelType_Valid = 1,
  9007. //iso1isolationLevelType_Warning = 2,
  9008. //iso1isolationLevelType_Fault = 3,
  9009. //iso1isolationLevelType_No_IMD = 4
  9010. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  9011. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  9012. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  9013. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  9014. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  9015. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  9016. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  9017. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  9018. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  9019. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  9020. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  9021. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  9022. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  9023. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  9024. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  9025. //iso1EVSENotificationType_None = 0,
  9026. //iso1EVSENotificationType_StopCharging = 1,
  9027. //iso1EVSENotificationType_ReNegotiation = 2
  9028. //----- [BODY (3/10)] EVSEPresentVoltage -----
  9029. res->EVSEPresentVoltage.Value = 3820;
  9030. res->EVSEPresentVoltage.Multiplier = -1;
  9031. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  9032. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  9033. //----- [BODY (4/10)] EVSEPresentCurrent -----
  9034. res->EVSEPresentCurrent.Value = 1200;
  9035. res->EVSEPresentCurrent.Multiplier = -1;
  9036. //res->EVSEPresentCurrent.Unit_isUsed = 1u;
  9037. res->EVSEPresentCurrent.Unit = iso1unitSymbolType_A;
  9038. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  9039. res->EVSECurrentLimitAchieved = 0;
  9040. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  9041. res->EVSEVoltageLimitAchieved = 0;
  9042. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  9043. res->EVSEPowerLimitAchieved = 0;
  9044. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  9045. res->EVSEMaximumVoltageLimit_isUsed = 1u;
  9046. res->EVSEMaximumVoltageLimit.Value = 7500;
  9047. res->EVSEMaximumVoltageLimit.Multiplier = -1;
  9048. //res->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  9049. res->EVSEMaximumVoltageLimit.Unit = iso1unitSymbolType_V;
  9050. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  9051. res->EVSEMaximumCurrentLimit_isUsed = 1u;
  9052. res->EVSEMaximumCurrentLimit.Value = 1200;
  9053. res->EVSEMaximumCurrentLimit.Multiplier = -1;
  9054. //res->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  9055. res->EVSEMaximumCurrentLimit.Unit = iso1unitSymbolType_A;
  9056. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  9057. res->EVSEMaximumPowerLimit_isUsed = 1u;
  9058. res->EVSEMaximumPowerLimit.Value = 6000;
  9059. res->EVSEMaximumPowerLimit.Multiplier = 1;
  9060. //res->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  9061. res->EVSEMaximumPowerLimit.Unit = iso1unitSymbolType_W;
  9062. }
  9063. /*===========================================================================
  9064. FUNCTION: Sudo_Parameter_iso1_ChargingStatusRes
  9065. DESCRIPTION:
  9066. PRE-CONDITION:
  9067. INPUT:
  9068. OUTPUT:
  9069. GLOBAL VARIABLES:
  9070. =============================================================================*/
  9071. void Sudo_Parameter_iso1_ChargingStatusRes()
  9072. {
  9073. //int i = 0;
  9074. struct iso1ChargingStatusResType *res;
  9075. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  9076. init_iso1ChargingStatusResType(&ccs_exi_doc_ISO1.V2G_Message.Body.ChargingStatusRes);
  9077. res = &ccs_exi_doc_ISO1.V2G_Message.Body.ChargingStatusRes;
  9078. ccs_exi_doc_ISO1.V2G_Message.Body.ChargingStatusRes_isUsed = 1u;
  9079. //----- [BODY (1/10)] ResponseCode -----
  9080. res->ResponseCode = OK_ISO15118_2014;
  9081. //----- [BODY (2/10)] AC_EVSEStatus -----
  9082. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  9083. res->AC_EVSEStatus.NotificationMaxDelay = 0;
  9084. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  9085. // iso1EVSENotificationType_None = 0,
  9086. // iso1EVSENotificationType_StopCharging = 1,
  9087. // iso1EVSENotificationType_ReNegotiation = 2
  9088. //----- [BODY (3/10)] EVSEMaxCurrent -----
  9089. res->EVSEMaxCurrent_isUsed = 1u;
  9090. res->EVSEMaxCurrent.Value = 32;
  9091. res->EVSEMaxCurrent.Multiplier = 0;
  9092. res->EVSEMaxCurrent.Unit = iso1unitSymbolType_A;
  9093. //----- [BODY (4/10)] SAScheduleTupleID -----
  9094. res->SAScheduleTupleID = 0;
  9095. //----- [BODY (5/10)] EVSEID -----
  9096. res->EVSEID.charactersLen = 37;
  9097. memset(res->EVSEID.characters, 0, sizeof(res->EVSEID.characters));
  9098. //sprintf((char*)res->EVSEID.characters, CCS_AC_EVSEID);
  9099. //----- [BODY (6/10)] MeterInfo -----
  9100. res->MeterInfo_isUsed = 1u;
  9101. memset(res->MeterInfo.MeterID.characters, 0, sizeof(res->MeterInfo.MeterID.characters));
  9102. memset(res->MeterInfo.SigMeterReading.bytes, 0, sizeof(res->MeterInfo.SigMeterReading.bytes));
  9103. //[MeterInfo][1/5] MeterID
  9104. //sprintf((char*)res->MeterInfo.MeterID , CCS_AC_METER_ID);
  9105. //[MeterInfo][2/5] SigMeterReading (optional)
  9106. //sprintf((char*)res->MeterInfo.SigMeterReading , CCS_AC_SIG_METER_READING);
  9107. //[MeterInfo][3/5] MeterStatus (optional)
  9108. res->MeterInfo.MeterStatus = 0;
  9109. //[MeterInfo][4/5] MeterReading (optional)
  9110. res->MeterInfo.MeterReading = 12345;
  9111. //[MeterInfo][5/5] TMeter (optional)
  9112. res->MeterInfo.TMeter = 1586243587; //Unix Time Stamp format
  9113. //----- [BODY (7/10)] ReceiptRequired -----
  9114. res->ReceiptRequired_isUsed = 1u;
  9115. res->ReceiptRequired = FALSE; //optional
  9116. }
  9117. /*===========================================================================
  9118. FUNCTION: Check_EVErrorCode
  9119. DESCRIPTION:
  9120. PRE-CONDITION:
  9121. INPUT:
  9122. OUTPUT:
  9123. GLOBAL VARIABLES:
  9124. =============================================================================*/
  9125. void Check_EVErrorCode(int code)
  9126. {
  9127. if (code != NO_ERROR) //NO_ERROR = 0
  9128. {
  9129. //Asking CSU to Stop
  9130. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9131. //Update_V2G_Flow_Status(Other_Fault);
  9132. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  9133. sprintf((char*)buf_log_evcomm,
  9134. "Stop by EV (EVErrorCode = %d (DEC))",
  9135. code);
  9136. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9137. }
  9138. }
  9139. /*===========================================================================
  9140. FUNCTION: Proc_din_CurrentDemandRes
  9141. DESCRIPTION:
  9142. PRE-CONDITION:
  9143. INPUT:
  9144. OUTPUT:
  9145. GLOBAL VARIABLES:
  9146. =============================================================================*/
  9147. int Proc_din_CurrentDemandRes(int AcceptFd)
  9148. {
  9149. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9150. //if it is not the same, the packet should be ignored.
  9151. //STEP 1: ============ Initialize ============
  9152. int errn = 0;
  9153. bitstream_t v2g_tx_stream;
  9154. static struct CurrentDemandResponse_DIN70121 *cur;
  9155. static struct ChargingInfoData *sys;
  9156. // int i = 0;
  9157. // static int EVSE_max_current;
  9158. // int tmp = 0;
  9159. size_t pos = 0;
  9160. v2g_tx_stream.pos = &pos;
  9161. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9162. v2g_tx_stream.data = V2GTP_Tx_buf;
  9163. cur = &ShmCcsData->V2GMessage_DIN70121.CurrentDemandResponse;
  9164. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9165. cur->ResponseCode = OK_DIN70121;
  9166. //EVSE Status Code
  9167. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  9168. //[HEADER] Check Req SessionID
  9169. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  9170. {
  9171. cur->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  9172. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  9173. errn = -1;
  9174. }
  9175. //Check for SequenceError
  9176. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9177. {
  9178. cur->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  9179. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  9180. errn = -1;
  9181. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9182. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9183. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9184. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9185. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9186. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9187. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9188. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9189. }
  9190. //Check for negative EvBatteryMaxCurrent
  9191. if (sys->EvBatteryMaxCurrent < 0)
  9192. {
  9193. sprintf((char*)buf_log_evcomm,
  9194. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  9195. sys->EvBatteryMaxCurrent);
  9196. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9197. cur->DC_EVSEStatus.EVSEStatusCode = dinresponseCodeType_FAILED_WrongChargeParameter; //16
  9198. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9199. errn = -1;
  9200. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9201. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  9202. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9203. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9204. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9205. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9206. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  9207. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  9208. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9209. }
  9210. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9211. SAVE_PhysicalValueType_DIN70121(&cur->EVSEPresentCurrent, (int) (sys->PresentChargingCurrent * 10), A_DIN70121);
  9212. SAVE_PhysicalValueType_DIN70121(&cur->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  9213. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == DISABLE
  9214. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_DIN70121);
  9215. #endif
  9216. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_DIN70121);
  9217. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_DIN70121);
  9218. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  9219. if (sys->EvBatteryMaxVoltage <= 500)
  9220. {
  9221. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumVoltageLimit, (int) (500 * 10), V_DIN70121); //500V
  9222. }
  9223. #endif
  9224. //Limit the EVTargetCurrent should be under EVSEMaximumCurrentLimit
  9225. if (sys->EvBatterytargetCurrent > sys->AvailableChargingCurrent)
  9226. {
  9227. sys->EvBatterytargetCurrent = sys->AvailableChargingCurrent;
  9228. //[To-Do] Limit is achieved flag
  9229. }
  9230. //Isolation Status
  9231. if (sys->IsolationStatus == GFD_Invalid) //0:invalid (on going)
  9232. {
  9233. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  9234. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9235. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9236. sprintf((char*)buf_log_evcomm, "[Error][Proc_din_CurrentDemandRes]IsolationStatus = %d", sys->IsolationStatus);
  9237. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9238. Update_V2G_Flow_Status(Other_Fault);
  9239. errn = -1;
  9240. }
  9241. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  9242. {
  9243. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  9244. }
  9245. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  9246. {
  9247. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  9248. }
  9249. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  9250. {
  9251. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  9252. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9253. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9254. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_CurrentDemandRes]GFD_Fault => Emergency Shutdown");
  9255. Update_V2G_Flow_Status(Other_Fault);
  9256. errn = -1;
  9257. }
  9258. else //GFD_No_IMD or other unexpected status
  9259. {
  9260. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  9261. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9262. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9263. sprintf((char*)buf_log_evcomm, "[Error][Proc_din_CurrentDemandRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  9264. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9265. Update_V2G_Flow_Status(Other_Fault);
  9266. errn = -1;
  9267. }
  9268. //For testing with Tesla Model 3
  9269. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == ENABLE
  9270. //[CAUTION][To-Do] Decresement of Current is not implemented.
  9271. //SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumCurrentLimit, 10, A_DIN70121);
  9272. if (sys->EvBatterytargetCurrent <= 0)
  9273. {
  9274. EVSE_max_current = 50; //10A
  9275. cur->EVSEMaximumCurrentLimit.Value = 50; //10A
  9276. /*
  9277. DEBUG_PRINTF_EVCOMM_DETAIL("1PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9278. sys->PresentChargingCurrent,
  9279. sys->EvBatterytargetCurrent,
  9280. EVSE_max_current,
  9281. cur->EVSEMaximumCurrentLimit.Value
  9282. );
  9283. */
  9284. }
  9285. else //1A
  9286. {
  9287. /*
  9288. DEBUG_PRINTF_EVCOMM_DETAIL("2PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9289. sys->PresentChargingCurrent,
  9290. sys->EvBatterytargetCurrent,
  9291. EVSE_max_current,
  9292. cur->EVSEMaximumCurrentLimit.Value
  9293. );
  9294. */
  9295. if ((abs((int)sys->PresentChargingCurrent - (int)sys->EvBatterytargetCurrent) < 3)&&(abs((int)(EVSE_max_current/10) - (int)sys->EvBatterytargetCurrent) < 3))
  9296. {
  9297. tmp = EVSE_max_current + 50; //10A
  9298. if (tmp <= (int)(sys->AvailableChargingCurrent * 10))
  9299. {
  9300. cur->EVSEMaximumCurrentLimit.Value = (int)tmp;
  9301. EVSE_max_current = tmp;
  9302. }
  9303. else
  9304. {
  9305. cur->EVSEMaximumCurrentLimit.Value = (int) (sys->AvailableChargingCurrent * 10); //max is set to 40A
  9306. EVSE_max_current = (int) (sys->AvailableChargingCurrent * 10);
  9307. }
  9308. }
  9309. }
  9310. #endif
  9311. //Check for CSU command of "Stop by EVSE"
  9312. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9313. {
  9314. //Check for Alarm Code: CCS GFD trip (012235)
  9315. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9316. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9317. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9318. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9319. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9320. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9321. {
  9322. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][Error][CurrentDemandRes]CCS GFD trip => EVSE_Shutdown");
  9323. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  9324. }
  9325. else
  9326. {
  9327. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][Error][CurrentDemandRes]EVSE_Shutdown");
  9328. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9329. }
  9330. //cur->ResponseCode = FAILED_DIN70121;
  9331. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9332. //errn = -1;
  9333. }
  9334. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9335. {
  9336. //Check for Alarm Code: CCS GFD trip (012235)
  9337. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9338. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9339. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9340. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9341. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9342. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9343. {
  9344. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][Error][CurrentDemandRes]CCS GFD trip => EVSE_EmergencyShutdown");
  9345. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  9346. }
  9347. else
  9348. {
  9349. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][Error][CurrentDemandRes]EVSE_EmergencyShutdown");
  9350. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9351. }
  9352. //cur->ResponseCode = FAILED_DIN70121;
  9353. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9354. //errn = -1;
  9355. }
  9356. else if (ShmInternalComm->ChargingPermission == FALSE)
  9357. {
  9358. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][Error][CurrentDemandRes]Permission OFF");
  9359. //cur->ResponseCode = FAILED_DIN70121;
  9360. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9361. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9362. //errn = -1;
  9363. }
  9364. else
  9365. {
  9366. //null
  9367. }
  9368. //Response to CP Error
  9369. #if CP_PROTECTION_MECHANISM == ENABLE
  9370. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  9371. {
  9372. cur->ResponseCode = FAILED_DIN70121;
  9373. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9374. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9375. Update_V2G_Flow_Status(Other_Fault);
  9376. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9377. //CCS_SECC_CP_State_Error (023889)
  9378. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9379. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9380. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9381. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  9382. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  9383. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  9384. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  9385. sprintf((char*)buf_log_evcomm,
  9386. "[CurrentDemand]Emergency Stop by CP Error (%d, %.02f V)",
  9387. sys->CpState,
  9388. sys->CpVoltage);
  9389. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9390. }
  9391. #endif
  9392. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9393. #if PARAMETER_NORMAL_MODE == ENABLE
  9394. SHM_Read_din_CurrentDemandRes(&ccs_exi_doc_DIN, ShmCcsData);
  9395. #else
  9396. Sudo_Parameter_din_CurrentDemandRes();
  9397. #endif
  9398. //STEP 4: ============ Encode and Send Response Message ===========
  9399. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  9400. {
  9401. errn = -1;
  9402. }
  9403. //STEP 5: ============ Update Flags ===========
  9404. return errn;
  9405. }
  9406. /*===========================================================================
  9407. FUNCTION: Proc_iso1_CurrentDemandRes
  9408. DESCRIPTION:
  9409. PRE-CONDITION:
  9410. INPUT:
  9411. OUTPUT:
  9412. GLOBAL VARIABLES:
  9413. =============================================================================*/
  9414. int Proc_iso1_CurrentDemandRes(int AcceptFd)
  9415. {
  9416. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9417. //if it is not the same, the packet should be ignored.
  9418. //STEP 1: ============ Initialize ============
  9419. int errn = 0;
  9420. bitstream_t v2g_tx_stream;
  9421. static struct CurrentDemandResponse_ISO15118_2014 *cur;
  9422. static struct ChargingInfoData *sys;
  9423. // int i = 0;
  9424. // static int EVSE_max_current;
  9425. // int tmp = 0;
  9426. size_t pos = 0;
  9427. v2g_tx_stream.pos = &pos;
  9428. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9429. v2g_tx_stream.data = V2GTP_Tx_buf;
  9430. cur = &ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandResponse;
  9431. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9432. cur->ResponseCode = OK_ISO15118_2014;
  9433. //EVSE Status Code
  9434. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  9435. //[HEADER] Check Req SessionID
  9436. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  9437. {
  9438. cur->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  9439. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  9440. errn = -1;
  9441. }
  9442. //Check for SequenceError
  9443. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9444. {
  9445. cur->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  9446. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  9447. errn = -1;
  9448. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9449. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9450. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9451. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9452. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9453. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9454. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9455. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9456. }
  9457. //Check for negative EvBatteryMaxCurrent
  9458. if (sys->EvBatteryMaxCurrent < 0)
  9459. {
  9460. sprintf((char*)buf_log_evcomm,
  9461. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  9462. sys->EvBatteryMaxCurrent);
  9463. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9464. cur->ResponseCode = iso1responseCodeType_FAILED_WrongChargeParameter; //16
  9465. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  9466. errn = -1;
  9467. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9468. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  9469. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9470. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9471. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9472. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9473. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  9474. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  9475. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9476. }
  9477. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9478. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEPresentCurrent, (int) (sys->PresentChargingCurrent * 10), A_ISO15118_2014);
  9479. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  9480. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == DISABLE
  9481. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  9482. #endif
  9483. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_ISO15118_2014);
  9484. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_ISO15118_2014);
  9485. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  9486. if (sys->EvBatteryMaxVoltage <= 500)
  9487. {
  9488. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumVoltageLimit, (int) (500 * 10), V_ISO15118_2014); //500V
  9489. }
  9490. #endif
  9491. //Limit the EVTargetCurrent should be under EVSEMaximumCurrentLimit
  9492. if (sys->EvBatterytargetCurrent > sys->AvailableChargingCurrent)
  9493. {
  9494. sys->EvBatterytargetCurrent = sys->AvailableChargingCurrent;
  9495. //[To-Do] Limit is achieved flag
  9496. }
  9497. //Isolation Status
  9498. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  9499. {
  9500. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  9501. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9502. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9503. sprintf((char*)buf_log_evcomm, "[Error][Proc_iso1_CurrentDemandRes]IsolationStatus = %d", sys->IsolationStatus);
  9504. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9505. Update_V2G_Flow_Status(Other_Fault);
  9506. errn = -1;
  9507. }
  9508. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  9509. {
  9510. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  9511. }
  9512. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  9513. {
  9514. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  9515. }
  9516. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  9517. {
  9518. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  9519. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9520. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9521. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_CurrentDemandRes]GFD_Fault => EmergencyShutdown");
  9522. Update_V2G_Flow_Status(Other_Fault);
  9523. errn = -1;
  9524. }
  9525. else
  9526. {
  9527. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  9528. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9529. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9530. sprintf((char*)buf_log_evcomm, "[Error][Proc_iso1_CurrentDemandRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  9531. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9532. Update_V2G_Flow_Status(Other_Fault);
  9533. errn = -1;
  9534. }
  9535. //For testing with Tesla Model 3
  9536. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == ENABLE
  9537. //[CAUTION][To-Do] Decresement of Current is not implemented.
  9538. //SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumCurrentLimit, 10, A_ISO15118_2014);
  9539. if (sys->EvBatterytargetCurrent <= 0)
  9540. {
  9541. EVSE_max_current = 50; //10A
  9542. cur->EVSEMaximumCurrentLimit.Value = 50; //10A
  9543. /*
  9544. DEBUG_PRINTF_EVCOMM_DETAIL("1PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9545. sys->PresentChargingCurrent,
  9546. sys->EvBatterytargetCurrent,
  9547. EVSE_max_current,
  9548. cur->EVSEMaximumCurrentLimit.Value
  9549. );
  9550. */
  9551. }
  9552. else //1A
  9553. {
  9554. /*
  9555. DEBUG_PRINTF_EVCOMM_DETAIL("2PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9556. sys->PresentChargingCurrent,
  9557. sys->EvBatterytargetCurrent,
  9558. EVSE_max_current,
  9559. cur->EVSEMaximumCurrentLimit.Value
  9560. );
  9561. */
  9562. if ((abs((int)sys->PresentChargingCurrent - (int)sys->EvBatterytargetCurrent) < 3)&&(abs((int)(EVSE_max_current/10) - (int)sys->EvBatterytargetCurrent) < 3))
  9563. {
  9564. tmp = EVSE_max_current + 50; //10A
  9565. if (tmp <= (int)(sys->AvailableChargingCurrent * 10))
  9566. {
  9567. cur->EVSEMaximumCurrentLimit.Value = (int)tmp;
  9568. EVSE_max_current = tmp;
  9569. }
  9570. else
  9571. {
  9572. cur->EVSEMaximumCurrentLimit.Value = (int) (sys->AvailableChargingCurrent * 10); //max is set to 40A
  9573. EVSE_max_current = (int) (sys->AvailableChargingCurrent * 10);
  9574. }
  9575. }
  9576. }
  9577. #endif
  9578. //Check for CSU command of "Stop by EVSE"
  9579. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9580. {
  9581. //Check for Alarm Code: CCS GFD trip (012235)
  9582. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9583. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9584. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9585. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9586. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9587. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9588. {
  9589. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][Error][CurrentDemandRes]CCS GFD trip => EVSE_Shutdown");
  9590. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  9591. }
  9592. else
  9593. {
  9594. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][Error][CurrentDemandRes]EVSE_Shutdown");
  9595. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9596. }
  9597. //cur->ResponseCode = FAILED_ISO15118_2014;
  9598. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  9599. //errn = -1;
  9600. }
  9601. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9602. {
  9603. //Check for Alarm Code: CCS GFD trip (012235)
  9604. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9605. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9606. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9607. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9608. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9609. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9610. {
  9611. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][Error][CurrentDemandRes]CCS GFD trip => EVSE_EmergencyShutdown");
  9612. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  9613. }
  9614. else
  9615. {
  9616. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][Error][CurrentDemandRes]EVSE_EmergencyShutdown");
  9617. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9618. }
  9619. //cur->ResponseCode = FAILED_ISO15118_2014;
  9620. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9621. //errn = -1;
  9622. }
  9623. else if (ShmInternalComm->ChargingPermission == FALSE)
  9624. {
  9625. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][Error][CurrentDemandRes]Permission OFF");
  9626. //cur->ResponseCode = FAILED_ISO15118_2014;
  9627. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9628. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  9629. //errn = -1;
  9630. }
  9631. else
  9632. {
  9633. //null
  9634. }
  9635. //Response to CP Error
  9636. #if CP_PROTECTION_MECHANISM == ENABLE
  9637. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  9638. {
  9639. cur->ResponseCode = FAILED_ISO15118_2014;
  9640. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9641. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9642. Update_V2G_Flow_Status(Other_Fault);
  9643. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9644. //CCS_SECC_CP_State_Error (023889)
  9645. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9646. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9647. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9648. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  9649. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  9650. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  9651. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  9652. sprintf((char*)buf_log_evcomm,
  9653. "[CurrentDemand]Emergency Stop by CP Error (%d, %.02f V)",
  9654. sys->CpState,
  9655. sys->CpVoltage);
  9656. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9657. }
  9658. #endif
  9659. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9660. #if PARAMETER_NORMAL_MODE == ENABLE
  9661. SHM_Read_iso1_CurrentDemandRes(&ccs_exi_doc_ISO1, ShmCcsData);
  9662. #else
  9663. Sudo_Parameter_iso1_CurrentDemandRes();
  9664. #endif
  9665. //STEP 4: ============ Encode and Send Response Message ===========
  9666. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  9667. {
  9668. errn = -1;
  9669. }
  9670. //STEP 5: ============ Update Flags ===========
  9671. return errn;
  9672. }
  9673. /*===========================================================================
  9674. FUNCTION: Proc_iso1_ChargingStatusRes
  9675. DESCRIPTION:
  9676. PRE-CONDITION:
  9677. INPUT:
  9678. OUTPUT:
  9679. GLOBAL VARIABLES:
  9680. =============================================================================*/
  9681. int Proc_iso1_ChargingStatusRes(int AcceptFd)
  9682. {
  9683. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9684. //if it is not the same, the packet should be ignored.
  9685. //STEP 1: ============ Initialize ============
  9686. //int i = 0;
  9687. int errn = 0;
  9688. bitstream_t v2g_tx_stream;
  9689. static struct ChargingStatusResponse_ISO15118_2014 *res;
  9690. static struct ChargingInfoData *sys;
  9691. size_t pos = 0;
  9692. v2g_tx_stream.pos = &pos;
  9693. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9694. v2g_tx_stream.data = V2GTP_Tx_buf;
  9695. res = &ShmCcsData->V2GMessage_ISO15118_2014.ChargingStatusResponse;
  9696. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9697. //Init
  9698. res->ResponseCode = OK_ISO15118_2014;
  9699. res->ReceiptRequired = FALSE; //optional
  9700. res->SAScheduleTupleID = 0;
  9701. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  9702. res->AC_EVSEStatus.NotificationMaxDelay = 0;
  9703. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  9704. // iso1EVSENotificationType_None = 0,
  9705. // iso1EVSENotificationType_StopCharging = 1,
  9706. // iso1EVSENotificationType_ReNegotiation = 2
  9707. //[HEADER] Check Req SessionID
  9708. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  9709. {
  9710. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  9711. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  9712. errn = -1;
  9713. }
  9714. //Check for SequenceError
  9715. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9716. {
  9717. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  9718. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  9719. errn = -1;
  9720. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9721. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9722. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9723. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9724. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9725. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9726. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9727. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9728. }
  9729. //EVSE ID
  9730. memset(res->EVSEID, 0, sizeof(res->EVSEID));
  9731. //sprintf((char*)res->EVSEID, CCS_AC_EVSEID);
  9732. //[MeterInfo][0/5] init
  9733. //memset(res->MeterInfo.MeterID.characters, 0, sizeof(res->MeterInfo.MeterID.characters));
  9734. //memset(res->MeterInfo.SigMeterReading.bytes, 0, sizeof(res->MeterInfo.SigMeterReading.bytes));
  9735. //[MeterInfo][1/5] MeterID
  9736. //sprintf((char*)res->MeterInfo.MeterID , CCS_AC_METER_ID);
  9737. //[MeterInfo][2/5] SigMeterReading (optional)
  9738. //sprintf((char*)res->MeterInfo.SigMeterReading , CCS_AC_SIG_METER_READING);
  9739. //[MeterInfo][3/5] MeterStatus (optional)
  9740. res->MeterInfo.MeterStatus = 0;
  9741. //[MeterInfo][4/5] MeterReading (optional)
  9742. res->MeterInfo.MeterReading = 12345;
  9743. //[MeterInfo][5/5] TMeter (optional)
  9744. res->MeterInfo.TMeter = 1586243587; //Unix Time Stamp format
  9745. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9746. SAVE_PhysicalValueType_ISO15118_2014(&res->EVSEMaxCurrent, (int)(sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  9747. //Isolation Status (RCD)
  9748. if (sys->IsolationStatus == 0) //Isolation is invalid
  9749. {
  9750. res->AC_EVSEStatus.RCD = TRUE; //FALSE(no error), TRUE(error is detected)
  9751. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9752. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9753. sprintf((char*)buf_log_evcomm, "[ISO1][Error][ChargingStatusRes]IsolationStatus = %d", sys->IsolationStatus);
  9754. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9755. Update_V2G_Flow_Status(Other_Fault);
  9756. errn = -1;
  9757. }
  9758. else if (sys->IsolationStatus == 1) //Isolation is valid
  9759. {
  9760. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  9761. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  9762. }
  9763. else
  9764. {
  9765. res->AC_EVSEStatus.RCD = TRUE; //FALSE(no error), TRUE(error is detected)
  9766. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9767. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9768. sprintf((char*)buf_log_evcomm, "[ISO1][Error][ChargingStatusRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  9769. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9770. Update_V2G_Flow_Status(Other_Fault);
  9771. errn = -1;
  9772. }
  9773. //Check for CSU command of "Stop by EVSE"
  9774. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9775. {
  9776. //res->ResponseCode = FAILED_ISO15118_2014;
  9777. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9778. //errn = -1;
  9779. }
  9780. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9781. {
  9782. //res->ResponseCode = FAILED_ISO15118_2014;
  9783. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9784. //errn = -1;
  9785. }
  9786. else if (ShmInternalComm->ChargingPermission == FALSE)
  9787. {
  9788. //res->ResponseCode = FAILED_ISO15118_2014;
  9789. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9790. //errn = -1;
  9791. }
  9792. //Response to CP Error
  9793. #if CP_PROTECTION_MECHANISM == ENABLE
  9794. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  9795. {
  9796. res->ResponseCode = FAILED_ISO15118_2014;
  9797. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9798. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9799. Update_V2G_Flow_Status(Other_Fault);
  9800. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9801. //CCS_SECC_CP_State_Error (023889)
  9802. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9803. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9804. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9805. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  9806. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  9807. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  9808. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  9809. sprintf((char*)buf_log_evcomm,
  9810. "[ChargingStatus]Emergency Stop by CP Error (%d, %.02f V)",
  9811. sys->CpState,
  9812. sys->CpVoltage);
  9813. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9814. }
  9815. #endif
  9816. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9817. #if PARAMETER_NORMAL_MODE == ENABLE
  9818. SHM_Read_iso1_ChargingStatusRes(&ccs_exi_doc_ISO1, ShmCcsData);
  9819. #else
  9820. Sudo_Parameter_iso1_ChargingStatusRes();
  9821. #endif
  9822. //STEP 4: ============ Encode and Send Response Message ===========
  9823. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  9824. {
  9825. errn = -1;
  9826. }
  9827. //STEP 5: ============ Update Flags ===========
  9828. return errn;
  9829. }
  9830. /*===========================================================================
  9831. FUNCTION: Proc_din_CurrentDemandReq
  9832. DESCRIPTION:
  9833. PRE-CONDITION:
  9834. INPUT:
  9835. OUTPUT:
  9836. GLOBAL VARIABLES:
  9837. =============================================================================*/
  9838. int Proc_din_CurrentDemandReq(int AcceptFd)
  9839. {
  9840. int errn = 0;
  9841. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_CurrentDemandReq] Got CurrentDemandReq\n");
  9842. //Print the decoded XML Document
  9843. PRINT_XML_DOC_DIN_CurrentDemandReq(&ccs_exi_doc_DIN);
  9844. //Save into Share Memory
  9845. SHM_Save_din_CurrentDemandReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  9846. //Check for EV Error Code
  9847. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.CurrentDemandRequest.DC_EVStatus.EVErrorCode);
  9848. errn = Proc_din_CurrentDemandRes(AcceptFd);
  9849. if (errn == 0)
  9850. {
  9851. //Response is sent successfully.
  9852. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]CurrentDemandRes");
  9853. }
  9854. else
  9855. {
  9856. sprintf((char*)buf_log_evcomm,
  9857. "[DIN][Error][CurrentDemandReq]CurrentDemandRes: fail(%d,DEC)",
  9858. errn);
  9859. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9860. }
  9861. return errn;
  9862. }
  9863. /*===========================================================================
  9864. FUNCTION: Proc_iso1_CurrentDemandReq
  9865. DESCRIPTION:
  9866. PRE-CONDITION:
  9867. INPUT:
  9868. OUTPUT:
  9869. GLOBAL VARIABLES:
  9870. =============================================================================*/
  9871. int Proc_iso1_CurrentDemandReq(int AcceptFd)
  9872. {
  9873. int errn = 0;
  9874. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CurrentDemandReq] Got CurrentDemandReq\n");
  9875. //Print the decoded XML Document
  9876. PRINT_XML_DOC_ISO1_CurrentDemandReq(&ccs_exi_doc_ISO1);
  9877. //Save into Share Memory
  9878. SHM_Save_iso1_CurrentDemandReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  9879. //Check for EV Error Code
  9880. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandRequest.DC_EVStatus.EVErrorCode);
  9881. errn = Proc_iso1_CurrentDemandRes(AcceptFd);
  9882. if (errn == 0)
  9883. {
  9884. //Response is sent successfully.
  9885. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]CurrentDemandRes");
  9886. }
  9887. else
  9888. {
  9889. sprintf((char*)buf_log_evcomm,
  9890. "[ISO1][Error][CurrentDemandReq]CurrentDemandRes: fail(%d,DEC)",
  9891. errn);
  9892. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9893. }
  9894. return errn;
  9895. }
  9896. /*===========================================================================
  9897. FUNCTION: Proc_iso1_ChargingStatusReq
  9898. DESCRIPTION:
  9899. PRE-CONDITION:
  9900. INPUT:
  9901. OUTPUT:
  9902. GLOBAL VARIABLES:
  9903. =============================================================================*/
  9904. int Proc_iso1_ChargingStatusReq(int AcceptFd)
  9905. {
  9906. int errn = 0;
  9907. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargingStatusReq] Got ChargingStatusReq\n");
  9908. //Print the decoded XML Document
  9909. PRINT_XML_DOC_ISO1_ChargingStatusReq(&ccs_exi_doc_ISO1);
  9910. //Save into Share Memory
  9911. SHM_Save_iso1_ChargingStatusReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  9912. //Check for EV Error Code
  9913. //no content in ISO1
  9914. errn = Proc_iso1_ChargingStatusRes(AcceptFd);
  9915. if (errn == 0)
  9916. {
  9917. //Response is sent successfully.
  9918. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]ChargingStatusRes");
  9919. }
  9920. else
  9921. {
  9922. sprintf((char*)buf_log_evcomm,
  9923. "[ISO1][Error][ChargingStatusReq]ChargingStatusRes: fail(%d,DEC)",
  9924. errn);
  9925. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9926. }
  9927. return errn;
  9928. }
  9929. /*===========================================================================
  9930. FUNCTION: Proc_din_PowerDeliveryStopRes
  9931. DESCRIPTION:
  9932. PRE-CONDITION:
  9933. INPUT:
  9934. OUTPUT:
  9935. GLOBAL VARIABLES:
  9936. =============================================================================*/
  9937. int Proc_din_PowerDeliveryStopRes(int AcceptFd)
  9938. {
  9939. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9940. //if it is not the same, the packet should be ignored.
  9941. //STEP 1: ============ Initialize ============
  9942. //int i = 0;
  9943. int errn = 0;
  9944. bitstream_t v2g_tx_stream;
  9945. struct dinPowerDeliveryResType *res;
  9946. struct ChargingInfoData *sys;
  9947. size_t pos = 0;
  9948. v2g_tx_stream.pos = &pos;
  9949. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9950. v2g_tx_stream.data = V2GTP_Tx_buf;
  9951. res = &ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes;
  9952. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9953. res->ResponseCode = OK_DIN70121;
  9954. //[HEADER] Check Req SessionID
  9955. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  9956. {
  9957. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  9958. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  9959. errn = -1;
  9960. }
  9961. //Check for SequenceError
  9962. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9963. {
  9964. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  9965. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  9966. errn = -1;
  9967. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9968. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9969. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9970. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9971. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9972. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9973. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9974. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9975. }
  9976. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9977. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9978. #if PARAMETER_NORMAL_MODE == ENABLE
  9979. ShmCcsData->V2GMessage_DIN70121.PowerDeliveryResponse.DC_EVSEStatus.EVSEStatusCode = EVSE_NotReady;
  9980. SHM_Read_din_PowerDeliveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  9981. #else
  9982. Sudo_Parameter_din_PreChargeRes();
  9983. #endif
  9984. //EVSE Status Code
  9985. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  9986. //Check for CSU command of "Stop by EVSE"
  9987. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9988. {
  9989. //res->ResponseCode = FAILED_DIN70121;
  9990. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9991. }
  9992. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9993. {
  9994. //res->ResponseCode = FAILED_DIN70121;
  9995. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9996. }
  9997. else if (ShmInternalComm->ChargingPermission == FALSE)
  9998. {
  9999. //res->ResponseCode = FAILED_DIN70121;
  10000. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  10001. }
  10002. //STEP 4: ============ Encode and Send Response Message ===========
  10003. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  10004. {
  10005. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_PowerDeliveryStopRes][Error]Tx encoded msg error");
  10006. errn = -1;
  10007. }
  10008. //STEP 5: ============ Update Flags ===========
  10009. return errn;
  10010. }
  10011. /*===========================================================================
  10012. FUNCTION: Proc_iso1_PowerDeliveryStopRes
  10013. DESCRIPTION:
  10014. PRE-CONDITION:
  10015. INPUT:
  10016. OUTPUT:
  10017. GLOBAL VARIABLES:
  10018. =============================================================================*/
  10019. int Proc_iso1_PowerDeliveryStopRes(int AcceptFd)
  10020. {
  10021. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10022. //if it is not the same, the packet should be ignored.
  10023. //STEP 1: ============ Initialize ============
  10024. //int i = 0;
  10025. int errn = 0;
  10026. bitstream_t v2g_tx_stream;
  10027. struct iso1PowerDeliveryResType *res;
  10028. struct ChargingInfoData *sys;
  10029. size_t pos = 0;
  10030. v2g_tx_stream.pos = &pos;
  10031. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10032. v2g_tx_stream.data = V2GTP_Tx_buf;
  10033. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes;
  10034. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  10035. res->ResponseCode = OK_ISO15118_2014;
  10036. //[HEADER] Check Req SessionID
  10037. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  10038. {
  10039. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  10040. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  10041. errn = -1;
  10042. }
  10043. //Check for SequenceError
  10044. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10045. {
  10046. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  10047. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  10048. errn = -1;
  10049. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10050. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10051. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10052. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10053. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10054. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10055. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10056. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10057. }
  10058. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10059. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10060. #if PARAMETER_NORMAL_MODE == ENABLE
  10061. ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryResponse.DC_EVSEStatus.DC_EVSEStatusCode = EVSE_NotReady;
  10062. SHM_Read_iso1_PowerDeliveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  10063. #else
  10064. Sudo_Parameter_iso1_PreChargeRes();
  10065. #endif
  10066. //EVSE Status Code
  10067. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  10068. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1 /*+++ 20200808, vern, Isolation Status should be valid during 2nd PowerDelivert ---*/
  10069. //Check for CSU command of "Stop by EVSE"
  10070. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  10071. {
  10072. //res->ResponseCode = FAILED_ISO15118_2014;
  10073. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready; /*+++ 20200808, vern, Isolation Status should be valid during 2nd PowerDelivert ---*/
  10074. }
  10075. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  10076. {
  10077. //res->ResponseCode = FAILED_ISO15118_2014;
  10078. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10079. }
  10080. else if (ShmInternalComm->ChargingPermission == FALSE)
  10081. {
  10082. //res->ResponseCode = FAILED_ISO15118_2014;
  10083. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready; /*+++ 20200808, vern, Isolation Status should be valid during 2nd PowerDelivert ---*/
  10084. }
  10085. //STEP 4: ============ Encode and Send Response Message ===========
  10086. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  10087. {
  10088. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_PowerDeliveryStopRes][Error]Tx encoded msg error");
  10089. errn = -1;
  10090. }
  10091. //STEP 5: ============ Update Flags ===========
  10092. return errn;
  10093. }
  10094. /*===========================================================================
  10095. FUNCTION: Proc_din_PowerDeliveryStopReq
  10096. DESCRIPTION:
  10097. PRE-CONDITION:
  10098. INPUT:
  10099. OUTPUT:
  10100. GLOBAL VARIABLES:
  10101. =============================================================================*/
  10102. int Proc_din_PowerDeliveryStopReq(int AcceptFd)
  10103. {
  10104. int errn = 0;
  10105. //Request CSU to STOP
  10106. //This should be reponsed as soon as possible once this message is received.
  10107. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10108. EVCOMM_SYS_INFO.EvBatterytargetVoltage = 0;
  10109. EVCOMM_SYS_INFO.EvBatterytargetCurrent = 0;
  10110. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PowerDeliveryReq (2nd)\n");
  10111. //Print the decoded XML Document
  10112. PRINT_XML_DOC_DIN_PowerDeliveryReq(&ccs_exi_doc_DIN);
  10113. //Save into Share Memory
  10114. SHM_Save_din_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  10115. //Check for EV Error Code
  10116. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  10117. //[To-Do] Sending response after the EVSE output voltage decreases to under 60V
  10118. usleep(1500000); //1.5 seconds
  10119. //sleep(1); //1 second
  10120. errn = Proc_din_PowerDeliveryStopRes(AcceptFd);
  10121. if (errn == 0)
  10122. {
  10123. //send response successfully.
  10124. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]PowerDeliveryRes (2nd)\n");
  10125. }
  10126. else
  10127. {
  10128. sprintf((char*)buf_log_evcomm,
  10129. "[Error][SeccComm][Proc_din_PowerDeliveryStopReq]Proc_iso1_PowerDeliveryStopRes(): %d (DEC)",
  10130. errn);
  10131. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10132. }
  10133. return errn;
  10134. }
  10135. /*===========================================================================
  10136. FUNCTION: Proc_iso1_PowerDeliveryStopReq
  10137. DESCRIPTION:
  10138. PRE-CONDITION:
  10139. INPUT:
  10140. OUTPUT:
  10141. GLOBAL VARIABLES:
  10142. =============================================================================*/
  10143. int Proc_iso1_PowerDeliveryStopReq(int AcceptFd)
  10144. {
  10145. int errn = 0;
  10146. //Request CSU to STOP
  10147. //This should be reponsed as soon as possible once this message is received.
  10148. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10149. EVCOMM_SYS_INFO.EvBatterytargetVoltage = 0;
  10150. EVCOMM_SYS_INFO.EvBatterytargetCurrent = 0;
  10151. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PowerDeliveryReq (2nd)\n");
  10152. //Print the decoded XML Document
  10153. PRINT_XML_DOC_ISO1_PowerDeliveryReq(&ccs_exi_doc_ISO1);
  10154. //Save into Share Memory
  10155. SHM_Save_iso1_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  10156. //Check for EV Error Code
  10157. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  10158. //[To-Do] Sending response after the EVSE output voltage decreases to under 60V
  10159. usleep(1500000); //1.5 seconds
  10160. //sleep(1); //1 second
  10161. errn = Proc_iso1_PowerDeliveryStopRes(AcceptFd);
  10162. if (errn == 0)
  10163. {
  10164. //send response successfully.
  10165. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]PowerDeliveryRes (2nd)");
  10166. }
  10167. else
  10168. {
  10169. sprintf((char*)buf_log_evcomm,
  10170. "[Error][SeccComm][Proc_iso1_PowerDeliveryStopReq]Proc_iso1_PowerDeliveryStopRes(): %d (DEC)",
  10171. errn);
  10172. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10173. }
  10174. return errn;
  10175. }
  10176. /*===========================================================================
  10177. FUNCTION: SHM_Init_din_WeldingDetectionRes
  10178. DESCRIPTION:
  10179. PRE-CONDITION:
  10180. INPUT:
  10181. 1. shm_ccs
  10182. OUTPUT:
  10183. 1. shm_ccs
  10184. GLOBAL VARIABLES:
  10185. =============================================================================*/
  10186. void SHM_Init_din_WeldingDetectionRes(struct CcsData *shm_ccs)
  10187. {
  10188. struct WeldingDetectionResponse_DIN70121 *in;
  10189. in = &shm_ccs->V2GMessage_DIN70121.WeldingDetectionResponse;
  10190. //----- [BODY (1/3)] ResponseCode -----
  10191. in->ResponseCode = dinresponseCodeType_OK;
  10192. //----- [BODY (2/3)] EVSEPresentVoltage -----
  10193. short value = 0; int multiplier = 0; unsigned char unit = 0;
  10194. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for test only.
  10195. value = 0; multiplier = 0; unit = V_DIN70121;
  10196. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  10197. //----- [BODY (3/3)] DC_EVSEStatus -----
  10198. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  10199. }
  10200. /*===========================================================================
  10201. FUNCTION: Sudo_Parameter_din_WeldingDetectionRes
  10202. DESCRIPTION:
  10203. PRE-CONDITION:
  10204. INPUT:
  10205. OUTPUT:
  10206. GLOBAL VARIABLES:
  10207. =============================================================================*/
  10208. void Sudo_Parameter_din_WeldingDetectionRes()
  10209. {
  10210. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  10211. init_dinWeldingDetectionResType(&ccs_exi_doc_DIN.V2G_Message.Body.WeldingDetectionRes);
  10212. ccs_exi_doc_DIN.V2G_Message.Body.WeldingDetectionRes_isUsed = 1u;
  10213. //----- [BODY (1/3)] ResponseCode -----
  10214. struct dinWeldingDetectionResType *res;
  10215. res = &ccs_exi_doc_DIN.V2G_Message.Body.WeldingDetectionRes;
  10216. res->ResponseCode = dinresponseCodeType_OK;
  10217. //----- [BODY (2/3)] EVSEPresentVoltage -----
  10218. res->EVSEPresentVoltage.Value = 3820;
  10219. res->EVSEPresentVoltage.Multiplier = -1;
  10220. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  10221. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  10222. // dinunitSymbolType_h = 0,
  10223. // dinunitSymbolType_m = 1,
  10224. // dinunitSymbolType_s = 2,
  10225. // dinunitSymbolType_A = 3,
  10226. // dinunitSymbolType_Ah = 4,
  10227. // dinunitSymbolType_V = 5,
  10228. // dinunitSymbolType_VA = 6,
  10229. // dinunitSymbolType_W = 7,
  10230. // dinunitSymbolType_W_s = 8,
  10231. // dinunitSymbolType_Wh = 9
  10232. //----- [BODY (3/3)] DC_EVSEStatus -----
  10233. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  10234. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  10235. // dinisolationLevelType_Invalid = 0,
  10236. // dinisolationLevelType_Valid = 1,
  10237. // dinisolationLevelType_Warning = 2,
  10238. // dinisolationLevelType_Fault = 3
  10239. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  10240. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  10241. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  10242. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  10243. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  10244. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  10245. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  10246. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  10247. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  10248. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  10249. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  10250. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  10251. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  10252. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  10253. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  10254. // dinEVSENotificationType_None = 0,
  10255. // dinEVSENotificationType_StopCharging = 1,
  10256. // dinEVSENotificationType_ReNegotiation = 2
  10257. }
  10258. /*===========================================================================
  10259. FUNCTION: Sudo_Parameter_iso1_WeldingDetectionRes
  10260. DESCRIPTION:
  10261. PRE-CONDITION:
  10262. INPUT:
  10263. OUTPUT:
  10264. GLOBAL VARIABLES:
  10265. =============================================================================*/
  10266. void Sudo_Parameter_iso1_WeldingDetectionRes()
  10267. {
  10268. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  10269. init_iso1WeldingDetectionResType(&ccs_exi_doc_ISO1.V2G_Message.Body.WeldingDetectionRes);
  10270. ccs_exi_doc_ISO1.V2G_Message.Body.WeldingDetectionRes_isUsed = 1u;
  10271. //----- [BODY (1/3)] ResponseCode -----
  10272. struct iso1WeldingDetectionResType *res;
  10273. res = &ccs_exi_doc_ISO1.V2G_Message.Body.WeldingDetectionRes;
  10274. res->ResponseCode = iso1responseCodeType_OK;
  10275. //----- [BODY (2/3)] EVSEPresentVoltage -----
  10276. res->EVSEPresentVoltage.Value = 3820;
  10277. res->EVSEPresentVoltage.Multiplier = -1;
  10278. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  10279. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  10280. //iso1unitSymbolType_h = 0,
  10281. //iso1unitSymbolType_m = 1,
  10282. //iso1unitSymbolType_s = 2,
  10283. //iso1unitSymbolType_A = 3,
  10284. //iso1unitSymbolType_V = 4,
  10285. //iso1unitSymbolType_W = 5,
  10286. //iso1unitSymbolType_Wh = 6
  10287. //----- [BODY (3/3)] DC_EVSEStatus -----
  10288. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  10289. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  10290. //iso1isolationLevelType_Invalid = 0,
  10291. //iso1isolationLevelType_Valid = 1,
  10292. //iso1isolationLevelType_Warning = 2,
  10293. //iso1isolationLevelType_Fault = 3,
  10294. //iso1isolationLevelType_No_IMD = 4
  10295. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  10296. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  10297. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  10298. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  10299. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  10300. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  10301. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  10302. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  10303. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  10304. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  10305. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  10306. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  10307. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  10308. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  10309. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  10310. //iso1EVSENotificationType_None = 0,
  10311. //iso1EVSENotificationType_StopCharging = 1,
  10312. //iso1EVSENotificationType_ReNegotiation = 2
  10313. }
  10314. /*===========================================================================
  10315. FUNCTION: Proc_din_WeldingDetectionRes
  10316. DESCRIPTION:
  10317. PRE-CONDITION:
  10318. INPUT:
  10319. OUTPUT:
  10320. GLOBAL VARIABLES:
  10321. =============================================================================*/
  10322. int Proc_din_WeldingDetectionRes(int AcceptFd)
  10323. {
  10324. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10325. //if it is not the same, the packet should be ignored.
  10326. //STEP 1: ============ Initialize ============
  10327. //int i = 0;
  10328. int errn = 0;
  10329. bitstream_t v2g_tx_stream;
  10330. static struct WeldingDetectionResponse_DIN70121 *wel;
  10331. static struct ChargingInfoData *sys;
  10332. size_t pos = 0;
  10333. v2g_tx_stream.pos = &pos;
  10334. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10335. v2g_tx_stream.data = V2GTP_Tx_buf;
  10336. wel = &ShmCcsData->V2GMessage_DIN70121.WeldingDetectionResponse;
  10337. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  10338. wel->ResponseCode = OK_DIN70121;
  10339. //[HEADER] Check Req SessionID
  10340. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  10341. {
  10342. wel->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  10343. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  10344. errn = -1;
  10345. }
  10346. //Check for SequenceError
  10347. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10348. {
  10349. wel->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  10350. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  10351. errn = -1;
  10352. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10353. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10354. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10355. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10356. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10357. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10358. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10359. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10360. }
  10361. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10362. SAVE_PhysicalValueType_DIN70121(&wel->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  10363. //EVSE Status Code
  10364. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  10365. //Check for CSU command of "Stop by EVSE"
  10366. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  10367. {
  10368. //wel->ResponseCode = FAILED_DIN70121;
  10369. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  10370. }
  10371. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  10372. {
  10373. //wel->ResponseCode = FAILED_DIN70121;
  10374. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10375. }
  10376. else if (ShmInternalComm->ChargingPermission == FALSE)
  10377. {
  10378. //wel->ResponseCode = FAILED_DIN70121;
  10379. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  10380. }
  10381. //Isolation Status
  10382. if (sys->IsolationStatus == GFD_Invalid) //0:invalid (on going)
  10383. {
  10384. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  10385. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10386. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10387. sprintf((char*)buf_log_evcomm, "[Error][Proc_din_WeldingDetectionRes]IsolationStatus = %d", sys->IsolationStatus);
  10388. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10389. Update_V2G_Flow_Status(Other_Fault);
  10390. errn = -1;
  10391. }
  10392. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  10393. {
  10394. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  10395. }
  10396. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  10397. {
  10398. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  10399. }
  10400. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  10401. {
  10402. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  10403. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10404. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10405. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_WeldingDetectionRes]GFD_Fault => Emergency Shutdown");
  10406. Update_V2G_Flow_Status(Other_Fault);
  10407. errn = -1;
  10408. }
  10409. else //GFD_No_IMD or other unexpected status
  10410. {
  10411. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  10412. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10413. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10414. sprintf((char*)buf_log_evcomm, "[Error][Proc_din_WeldingDetectionRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  10415. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10416. Update_V2G_Flow_Status(Other_Fault);
  10417. errn = -1;
  10418. }
  10419. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10420. #if PARAMETER_NORMAL_MODE == ENABLE
  10421. SHM_Read_din_WeldingDetectionRes(&ccs_exi_doc_DIN, ShmCcsData);
  10422. #else
  10423. Sudo_Parameter_din_WeldingDetectionRes();
  10424. #endif
  10425. //STEP 4: ============ Encode and Send Response Message ===========
  10426. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  10427. {
  10428. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_WeldingDetectionRes][Error]Tx encoded msg error");
  10429. errn = -1;
  10430. }
  10431. //STEP 5: ============ Update Flags ===========
  10432. return errn;
  10433. }
  10434. /*===========================================================================
  10435. FUNCTION: Proc_iso1_WeldingDetectionRes
  10436. DESCRIPTION:
  10437. PRE-CONDITION:
  10438. INPUT:
  10439. OUTPUT:
  10440. GLOBAL VARIABLES:
  10441. =============================================================================*/
  10442. int Proc_iso1_WeldingDetectionRes(int AcceptFd)
  10443. {
  10444. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10445. //if it is not the same, the packet should be ignored.
  10446. //STEP 1: ============ Initialize ============
  10447. //int i = 0;
  10448. int errn = 0;
  10449. bitstream_t v2g_tx_stream;
  10450. static struct WeldingDetectionResponse_ISO15118_2014 *wel;
  10451. static struct ChargingInfoData *sys;
  10452. size_t pos = 0;
  10453. v2g_tx_stream.pos = &pos;
  10454. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10455. v2g_tx_stream.data = V2GTP_Tx_buf;
  10456. wel = &ShmCcsData->V2GMessage_ISO15118_2014.WeldingDetectionResponse;
  10457. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  10458. wel->ResponseCode = OK_ISO15118_2014;
  10459. //[HEADER] Check Req SessionID
  10460. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  10461. {
  10462. wel->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  10463. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  10464. errn = -1;
  10465. }
  10466. //Check for SequenceError
  10467. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10468. {
  10469. wel->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  10470. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  10471. errn = -1;
  10472. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10473. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10474. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10475. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10476. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10477. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10478. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10479. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10480. }
  10481. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10482. SAVE_PhysicalValueType_ISO15118_2014(&wel->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  10483. //EVSE Status Code
  10484. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  10485. //Check for CSU command of "Stop by EVSE"
  10486. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  10487. {
  10488. //wel->ResponseCode = FAILED_ISO15118_2014;
  10489. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  10490. }
  10491. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  10492. {
  10493. //wel->ResponseCode = FAILED_ISO15118_2014;
  10494. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10495. }
  10496. else if (ShmInternalComm->ChargingPermission == FALSE)
  10497. {
  10498. //wel->ResponseCode = FAILED_ISO15118_2014;
  10499. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  10500. }
  10501. //Isolation Status
  10502. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  10503. {
  10504. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  10505. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10506. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10507. sprintf((char*)buf_log_evcomm, "[Error][Proc_iso1_WeldingDetectionRes]IsolationStatus = %d", sys->IsolationStatus);
  10508. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10509. Update_V2G_Flow_Status(Other_Fault);
  10510. errn = -1;
  10511. }
  10512. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  10513. {
  10514. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  10515. }
  10516. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  10517. {
  10518. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  10519. }
  10520. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  10521. {
  10522. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  10523. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10524. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10525. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_WeldingDetectionRes]GFD_Fault => EmergencyShutdown");
  10526. Update_V2G_Flow_Status(Other_Fault);
  10527. errn = -1;
  10528. }
  10529. else
  10530. {
  10531. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  10532. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10533. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10534. sprintf((char*)buf_log_evcomm, "[Error][Proc_iso1_WeldingDetectionRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  10535. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10536. Update_V2G_Flow_Status(Other_Fault);
  10537. errn = -1;
  10538. }
  10539. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10540. #if PARAMETER_NORMAL_MODE == ENABLE
  10541. SHM_Read_iso1_WeldingDetectionRes(&ccs_exi_doc_ISO1, ShmCcsData);
  10542. #else
  10543. Sudo_Parameter_iso1_WeldingDetectionRes();
  10544. #endif
  10545. //STEP 4: ============ Encode and Send Response Message ===========
  10546. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  10547. {
  10548. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_WeldingDetectionRes][Error]Tx encoded msg error");
  10549. errn = -1;
  10550. }
  10551. //STEP 5: ============ Update Flags ===========
  10552. return errn;
  10553. }
  10554. /*===========================================================================
  10555. FUNCTION: Proc_din_WeldingDetectionReq
  10556. DESCRIPTION:
  10557. PRE-CONDITION:
  10558. INPUT:
  10559. OUTPUT:
  10560. GLOBAL VARIABLES:
  10561. =============================================================================*/
  10562. int Proc_din_WeldingDetectionReq(int AcceptFd)
  10563. {
  10564. int errn = 0;
  10565. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_WeldingDetectionReq] Got WeldingDetectionReq\n");
  10566. //Print the decoded XML Document
  10567. PRINT_XML_DOC_DIN_WeldingDetectionReq(&ccs_exi_doc_DIN);
  10568. //Save into Share Memory
  10569. SHM_Save_din_WeldingDetectionReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  10570. //Check for EV Error Code
  10571. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.WeldingDetectionRequest.DC_EVStatus.EVErrorCode);
  10572. errn = Proc_din_WeldingDetectionRes(AcceptFd);
  10573. if (errn == 0)
  10574. {
  10575. //send response successfully.
  10576. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]WeldingDetectionRes");
  10577. }
  10578. else
  10579. {
  10580. sprintf((char*)buf_log_evcomm,
  10581. "[Error][SeccComm][Proc_din_WeldingDetectionReq]Proc_din_WeldingDetectionRes(): %d (DEC)",
  10582. errn);
  10583. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10584. }
  10585. return errn;
  10586. }
  10587. /*===========================================================================
  10588. FUNCTION: Proc_iso1_WeldingDetectionReq
  10589. DESCRIPTION:
  10590. PRE-CONDITION:
  10591. INPUT:
  10592. OUTPUT:
  10593. GLOBAL VARIABLES:
  10594. =============================================================================*/
  10595. int Proc_iso1_WeldingDetectionReq(int AcceptFd)
  10596. {
  10597. int errn = 0;
  10598. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_WeldingDetectionReq] Got WeldingDetectionReq\n");
  10599. //Print the decoded XML Document
  10600. PRINT_XML_DOC_ISO1_WeldingDetectionReq(&ccs_exi_doc_ISO1);
  10601. //Save into Share Memory
  10602. SHM_Save_iso1_WeldingDetectionReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  10603. //Check for EV Error Code
  10604. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.WeldingDetectionRequest.DC_EVStatus.EVErrorCode);
  10605. errn = Proc_iso1_WeldingDetectionRes(AcceptFd);
  10606. if (errn == 0)
  10607. {
  10608. //send response successfully.
  10609. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]WeldingDetectionRes");
  10610. }
  10611. else
  10612. {
  10613. sprintf((char*)buf_log_evcomm,
  10614. "[Error][SeccComm][Proc_iso1_WeldingDetectionReq]Proc_iso1_WeldingDetectionRes(): %d (DEC)",
  10615. errn);
  10616. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10617. }
  10618. return errn;
  10619. }
  10620. /*===========================================================================
  10621. FUNCTION: SHM_Init_din_SessionStopRes
  10622. DESCRIPTION:
  10623. PRE-CONDITION:
  10624. INPUT:
  10625. 1. shm_ccs
  10626. OUTPUT:
  10627. 1. shm_ccs
  10628. GLOBAL VARIABLES:
  10629. =============================================================================*/
  10630. void SHM_Init_din_SessionStopRes(struct CcsData *shm_ccs)
  10631. {
  10632. struct SessionStopResponse_DIN70121 *in;
  10633. in = &shm_ccs->V2GMessage_DIN70121.SessionStopResponse;
  10634. //----- [BODY (1/3)] ResponseCode -----
  10635. in->ResponseCode = dinresponseCodeType_OK;
  10636. }
  10637. /*===========================================================================
  10638. FUNCTION: Sudo_Parameter_din_SessionStopRes
  10639. DESCRIPTION:
  10640. PRE-CONDITION:
  10641. INPUT:
  10642. OUTPUT:
  10643. GLOBAL VARIABLES:
  10644. =============================================================================*/
  10645. void Sudo_Parameter_din_SessionStopRes()
  10646. {
  10647. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  10648. init_dinSessionStopResType(&ccs_exi_doc_DIN.V2G_Message.Body.SessionStopRes);
  10649. ccs_exi_doc_DIN.V2G_Message.Body.SessionStopRes_isUsed = 1u;
  10650. //----- [BODY (1/3)] ResponseCode -----
  10651. struct dinSessionStopResType *res;
  10652. res = &ccs_exi_doc_DIN.V2G_Message.Body.SessionStopRes;
  10653. res->ResponseCode = dinresponseCodeType_OK;
  10654. }
  10655. /*===========================================================================
  10656. FUNCTION: Sudo_Parameter_iso1_SessionStopRes
  10657. DESCRIPTION:
  10658. PRE-CONDITION:
  10659. INPUT:
  10660. OUTPUT:
  10661. GLOBAL VARIABLES:
  10662. =============================================================================*/
  10663. void Sudo_Parameter_iso1_SessionStopRes()
  10664. {
  10665. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  10666. init_iso1SessionStopResType(&ccs_exi_doc_ISO1.V2G_Message.Body.SessionStopRes);
  10667. ccs_exi_doc_ISO1.V2G_Message.Body.SessionStopRes_isUsed = 1u;
  10668. //----- [BODY (1/3)] ResponseCode -----
  10669. struct iso1SessionStopResType *res;
  10670. res = &ccs_exi_doc_ISO1.V2G_Message.Body.SessionStopRes;
  10671. res->ResponseCode = iso1responseCodeType_OK;
  10672. }
  10673. /*===========================================================================
  10674. FUNCTION: Proc_din_SessionStopRes
  10675. DESCRIPTION:
  10676. PRE-CONDITION:
  10677. INPUT:
  10678. OUTPUT:
  10679. GLOBAL VARIABLES:
  10680. =============================================================================*/
  10681. int Proc_din_SessionStopRes(int AcceptFd)
  10682. {
  10683. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10684. //if it is not the same, the packet should be ignored.
  10685. //STEP 1: ============ Initialize ============
  10686. //int i = 0;
  10687. int errn = 0;
  10688. bitstream_t v2g_tx_stream;
  10689. struct SessionStopResponse_DIN70121 *stp;
  10690. size_t pos = 0;
  10691. v2g_tx_stream.pos = &pos;
  10692. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10693. v2g_tx_stream.data = V2GTP_Tx_buf;
  10694. stp = &ShmCcsData->V2GMessage_DIN70121.SessionStopResponse;
  10695. stp->ResponseCode = OK_DIN70121;
  10696. //[HEADER] Check Req SessionID
  10697. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  10698. {
  10699. stp->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  10700. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  10701. errn = -1;
  10702. }
  10703. //Check for SequenceError
  10704. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10705. {
  10706. stp->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  10707. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  10708. errn = -1;
  10709. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10710. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10711. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10712. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10713. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10714. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10715. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10716. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10717. }
  10718. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10719. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10720. #if PARAMETER_NORMAL_MODE == ENABLE
  10721. SHM_Read_din_SessionStopRes(&ccs_exi_doc_DIN, ShmCcsData);
  10722. #else
  10723. Sudo_Parameter_din_SessionStopRes();
  10724. #endif
  10725. //STEP 4: ============ Encode and Send Response Message ===========
  10726. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  10727. {
  10728. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Tx]SessionStopRes: fail");
  10729. errn = -1;
  10730. }
  10731. else
  10732. {
  10733. //send response successfully.
  10734. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Tx]SessionStopRes");
  10735. }
  10736. //STEP 5: ============ Update Flags ===========
  10737. EVCOMM_SYS_INFO.ConnectorLocked = FALSE; //Indicating CSU that the Connector is unlocked.
  10738. //Once this is set, the CSU should
  10739. // =========== Annouce to CSU [To-Be Implemented]=============
  10740. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE; //This should be also added to EV Error events.
  10741. // =========== Re-initialized [To-Be Implemented]=============
  10742. //Keep 5% PWM for 2 seconds
  10743. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 2");
  10744. sleep(1);
  10745. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 1");
  10746. sleep(1);
  10747. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 0");
  10748. OutputCpPwmDuty(100);
  10749. //[To-Do] Reset All Share memory
  10750. //[To-Do] CCS Module enter "idle" mode and CSU should check for this state.
  10751. //system("reboot -f");
  10752. //sleep(5);
  10753. //system("reboot -f");
  10754. return errn;
  10755. }
  10756. /*===========================================================================
  10757. FUNCTION: Proc_iso1_SessionStopRes
  10758. DESCRIPTION:
  10759. PRE-CONDITION:
  10760. INPUT:
  10761. OUTPUT:
  10762. GLOBAL VARIABLES:
  10763. =============================================================================*/
  10764. int Proc_iso1_SessionStopRes(int AcceptFd)
  10765. {
  10766. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10767. //if it is not the same, the packet should be ignored.
  10768. //STEP 1: ============ Initialize ============
  10769. //int i = 0;
  10770. int errn = 0;
  10771. bitstream_t v2g_tx_stream;
  10772. struct SessionStopResponse_ISO15118_2014 *stp;
  10773. //struct ChargingInfoData *sys;
  10774. size_t pos = 0;
  10775. v2g_tx_stream.pos = &pos;
  10776. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10777. v2g_tx_stream.data = V2GTP_Tx_buf;
  10778. stp = &ShmCcsData->V2GMessage_ISO15118_2014.SessionStopResponse;
  10779. //sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  10780. stp->ResponseCode = OK_ISO15118_2014;
  10781. //[HEADER] Check Req SessionID
  10782. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  10783. {
  10784. stp->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  10785. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  10786. errn = -1;
  10787. }
  10788. //Check for SequenceError
  10789. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10790. {
  10791. stp->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  10792. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  10793. errn = -1;
  10794. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10795. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10796. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10797. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10798. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10799. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10800. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10801. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10802. }
  10803. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10804. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10805. #if PARAMETER_NORMAL_MODE == ENABLE
  10806. SHM_Read_iso1_SessionStopRes(&ccs_exi_doc_ISO1, ShmCcsData);
  10807. #else
  10808. Sudo_Parameter_iso1_SessionStopRes();
  10809. #endif
  10810. //STEP 4: ============ Encode and Send Response Message ===========
  10811. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  10812. {
  10813. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Tx]SessionStopRes: fail");
  10814. errn = -1;
  10815. }
  10816. else
  10817. {
  10818. //send response successfully.
  10819. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Tx]SessionStopRes");
  10820. }
  10821. //STEP 5: ============ Update Flags ===========
  10822. EVCOMM_SYS_INFO.ConnectorLocked = FALSE; //Indicating CSU that the Connector is unlocked.
  10823. //Once this is set, the CSU should
  10824. // =========== Annouce to CSU [To-Be Implemented]=============
  10825. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE; //This should be also added to EV Error events.
  10826. // =========== Re-initialized [To-Be Implemented]=============
  10827. //Keep 5% PWM for 2 seconds
  10828. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 2");
  10829. sleep(1);
  10830. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 1");
  10831. sleep(1);
  10832. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 0");
  10833. OutputCpPwmDuty(100);
  10834. //[To-Do] Reset All Share memory
  10835. //[To-Do] CCS Module enter "idle" mode and CSU should check for this state.
  10836. //system("reboot -f");
  10837. //sleep(5);
  10838. //system("reboot -f");
  10839. return errn;
  10840. }
  10841. /*===========================================================================
  10842. FUNCTION: Proc_din_SessionStopReq
  10843. DESCRIPTION:
  10844. PRE-CONDITION:
  10845. INPUT:
  10846. OUTPUT:
  10847. GLOBAL VARIABLES:
  10848. =============================================================================*/
  10849. int Proc_din_SessionStopReq(int AcceptFd)
  10850. {
  10851. int errn = 0;
  10852. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]SessionStopReq");
  10853. //Print the decoded XML Document
  10854. PRINT_XML_DOC_DIN_SessionStopReq(&ccs_exi_doc_DIN);
  10855. //Save into Share Memory
  10856. SHM_Save_din_SessionStopReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  10857. errn = Proc_din_SessionStopRes(AcceptFd);
  10858. if (errn == 0)
  10859. {
  10860. //send response successfully.
  10861. }
  10862. else
  10863. {
  10864. sprintf((char*)buf_log_evcomm,
  10865. "[Error][SeccComm][Proc_din_SessionStopReq]Proc_iso1_SessionStopRes(): %d (DEC)",
  10866. errn);
  10867. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10868. }
  10869. return errn;
  10870. }
  10871. /*===========================================================================
  10872. FUNCTION: Proc_iso1_SessionStopReq
  10873. DESCRIPTION:
  10874. PRE-CONDITION:
  10875. INPUT:
  10876. OUTPUT:
  10877. GLOBAL VARIABLES:
  10878. =============================================================================*/
  10879. int Proc_iso1_SessionStopReq(int AcceptFd)
  10880. {
  10881. int errn = 0;
  10882. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]SessionStopReq");
  10883. //Print the decoded XML Document
  10884. PRINT_XML_DOC_ISO1_SessionStopReq(&ccs_exi_doc_ISO1);
  10885. //Save into Share Memory
  10886. SHM_Save_iso1_SessionStopReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  10887. errn = Proc_iso1_SessionStopRes(AcceptFd);
  10888. if (errn == 0)
  10889. {
  10890. //send response successfully.
  10891. }
  10892. else
  10893. {
  10894. sprintf((char*)buf_log_evcomm,
  10895. "[Error][SeccComm][Proc_iso1_SessionStopReq]Proc_iso1_SessionStopRes(): %d (DEC)",
  10896. errn);
  10897. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10898. }
  10899. return errn;
  10900. }
  10901. /*===========================================================================
  10902. FUNCTION: V2gMsg_Process_din
  10903. DESCRIPTION:
  10904. PRE-CONDITION:
  10905. INPUT:
  10906. 1. V2gFlowStatus
  10907. OUTPUT:
  10908. GLOBAL VARIABLES:
  10909. 1. V2gFlowStatus
  10910. =============================================================================*/
  10911. int V2gMsg_Process_din(int AcceptFd)
  10912. {
  10913. unsigned char req_is_responsed = FALSE;
  10914. while (req_is_responsed == FALSE)
  10915. {
  10916. //Check if it is in End_Process
  10917. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  10918. {
  10919. break;
  10920. }
  10921. switch(V2gFlowStatus)
  10922. {
  10923. //-------------------------------------------
  10924. case SupportedAppProtocolRequest:
  10925. {
  10926. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  10927. {
  10928. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  10929. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  10930. {
  10931. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10932. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  10933. }
  10934. else
  10935. {
  10936. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]supportedAppProtocolResReq: fail");
  10937. Update_V2G_Flow_Status(Other_Fault);
  10938. }
  10939. }
  10940. req_is_responsed = TRUE;
  10941. break;
  10942. }
  10943. case SupportedAppProtocolResponse:
  10944. {
  10945. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  10946. {
  10947. Update_V2G_Flow_Status(SessionSetupRequest);
  10948. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10949. }
  10950. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10951. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10952. {
  10953. sprintf((char*)buf_log_evcomm,
  10954. "[ERROR]SequenceError(%d) => Tx Res MSG",
  10955. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10956. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10957. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10958. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10959. }
  10960. else
  10961. {
  10962. }
  10963. break;
  10964. }
  10965. //-------------------------------------------
  10966. case SessionSetupRequest: //19
  10967. {
  10968. if (Proc_din_SessionSetupReq(AcceptFd) == 0)
  10969. {
  10970. Update_V2G_Flow_Status(SessionSetupResponse);
  10971. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10972. }
  10973. else
  10974. {
  10975. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionSetupReqRes: fail");
  10976. Update_V2G_Flow_Status(Other_Fault);
  10977. }
  10978. req_is_responsed = TRUE;
  10979. break;
  10980. }
  10981. case SessionSetupResponse: //20
  10982. {
  10983. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  10984. {
  10985. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  10986. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10987. }
  10988. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10989. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10990. {
  10991. sprintf((char*)buf_log_evcomm,
  10992. "[ERROR]SequenceError(%d) => Tx Res MSG",
  10993. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10994. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10995. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10996. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10997. }
  10998. else
  10999. {
  11000. }
  11001. break;
  11002. }
  11003. //-------------------------------------------
  11004. case ServiceDiscoveryRequest: //21
  11005. {
  11006. if (Proc_din_ServiceDiscoveryReq(AcceptFd) == 0)
  11007. {
  11008. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  11009. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11010. }
  11011. else
  11012. {
  11013. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceDiscoveryReqRes: fail");
  11014. Update_V2G_Flow_Status(Other_Fault);
  11015. }
  11016. req_is_responsed = TRUE;
  11017. break;
  11018. }
  11019. case ServiceDiscoveryResponse: //22
  11020. {
  11021. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  11022. {
  11023. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  11024. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11025. }
  11026. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11027. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11028. {
  11029. sprintf((char*)buf_log_evcomm, "[ERROR]SequenceError(%d) => Tx Res MSG", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11030. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11031. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11032. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11033. }
  11034. else
  11035. {
  11036. }
  11037. break;
  11038. }
  11039. //-------------------------------------------
  11040. case ServiceAndPaymentSelectionRequest: //25
  11041. {
  11042. if (Proc_din_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  11043. {
  11044. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  11045. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11046. }
  11047. else
  11048. {
  11049. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceAndPaymentSelectionReqRes: fail");
  11050. Update_V2G_Flow_Status(Other_Fault);
  11051. }
  11052. req_is_responsed = TRUE;
  11053. break;
  11054. }
  11055. case ServiceAndPaymentSelectionResponse: //26
  11056. {
  11057. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11058. {
  11059. Update_V2G_Flow_Status(AuthorizationRequest);
  11060. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11061. }
  11062. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11063. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11064. {
  11065. sprintf((char*)buf_log_evcomm,
  11066. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11067. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11068. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11069. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11070. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11071. }
  11072. else
  11073. {
  11074. }
  11075. break;
  11076. }
  11077. //-------------------------------------------
  11078. //case ContractAuthenticationReq:
  11079. case AuthorizationRequest: //29
  11080. {
  11081. if (Proc_din_ContractAuthenticationReq(AcceptFd) == 0)
  11082. {
  11083. Update_V2G_Flow_Status(AuthorizationResponse);
  11084. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11085. }
  11086. else
  11087. {
  11088. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  11089. Update_V2G_Flow_Status(Other_Fault);
  11090. }
  11091. req_is_responsed = TRUE;
  11092. break;
  11093. }
  11094. case AuthorizationResponse: //30
  11095. {
  11096. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11097. {
  11098. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11099. if (Proc_din_ContractAuthenticationReq(AcceptFd) == 0)
  11100. {
  11101. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11102. }
  11103. else
  11104. {
  11105. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  11106. Update_V2G_Flow_Status(Other_Fault);
  11107. }
  11108. req_is_responsed = TRUE;
  11109. }
  11110. //Check for ChargeParameterDiscoveryReq
  11111. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11112. {
  11113. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11114. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  11115. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  11116. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11117. ftime(&SeqStartTime);
  11118. #endif
  11119. }
  11120. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11121. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11122. {
  11123. sprintf((char*)buf_log_evcomm,
  11124. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11125. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11126. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11127. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11128. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11129. }
  11130. else
  11131. {
  11132. }
  11133. break;
  11134. }
  11135. //-------------------------------------------
  11136. case ChargeParameterDiscoveryRequest: //35
  11137. {
  11138. if (Proc_din_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11139. {
  11140. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  11141. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11142. }
  11143. else
  11144. {
  11145. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  11146. Update_V2G_Flow_Status(Other_Fault);
  11147. }
  11148. req_is_responsed = TRUE;
  11149. break;
  11150. }
  11151. case ChargeParameterDiscoveryResponse:
  11152. {
  11153. //STEP 1: Check for Process Timeout
  11154. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11155. ftime(&SeqEndTime);
  11156. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  11157. {
  11158. sprintf((char*)buf_log_evcomm,
  11159. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  11160. DiffTimeb(SeqStartTime, SeqEndTime),
  11161. V2G_SECC_ChargingParameter_Performance_Time);
  11162. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11163. Update_V2G_Flow_Status(Sequence_Timeout);
  11164. break;
  11165. }
  11166. #endif
  11167. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11168. {
  11169. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11170. if (Proc_din_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11171. {
  11172. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11173. }
  11174. else
  11175. {
  11176. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  11177. Update_V2G_Flow_Status(Other_Fault);
  11178. }
  11179. req_is_responsed = TRUE;
  11180. break;
  11181. }
  11182. //STEP 2: Check for CableCheckReq message
  11183. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  11184. {
  11185. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11186. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CableCheckReqReq: isolated?");
  11187. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  11188. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  11189. Update_V2G_Flow_Status(CableCheckRequest);
  11190. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11191. ftime(&SeqStartTime);
  11192. #endif
  11193. }
  11194. //STEP 3: Wait for PowerDeliveryReq Message
  11195. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11196. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11197. {
  11198. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  11199. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11200. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  11201. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11202. ftime(&SeqStartTime);
  11203. #endif
  11204. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  11205. {
  11206. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  11207. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11208. }
  11209. else
  11210. {
  11211. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  11212. Update_V2G_Flow_Status(Other_Fault);
  11213. }
  11214. }
  11215. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11216. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11217. {
  11218. sprintf((char*)buf_log_evcomm,
  11219. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11220. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11221. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11222. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11223. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11224. }
  11225. else
  11226. {
  11227. }
  11228. break;
  11229. }
  11230. //-------------------------------------------
  11231. case CableCheckRequest: //37
  11232. {
  11233. //STEP 3: Execute Cable Check Process
  11234. if (Proc_din_CableCheckReq(AcceptFd) == 0)
  11235. {
  11236. Update_V2G_Flow_Status(CableCheckResponse);
  11237. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11238. }
  11239. else
  11240. {
  11241. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  11242. Update_V2G_Flow_Status(Other_Fault);
  11243. }
  11244. req_is_responsed = TRUE;
  11245. break;
  11246. }
  11247. case CableCheckResponse: //38
  11248. {
  11249. //STEP 1: Check for Process Timeout
  11250. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11251. ftime(&SeqEndTime);
  11252. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  11253. {
  11254. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CableCheck] end counting...");
  11255. sprintf((char*)buf_log_evcomm,
  11256. "CableCheck Timeout - (%.02lf of %d ms)\n",
  11257. DiffTimeb(SeqStartTime, SeqEndTime),
  11258. V2G_SECC_CableCheck_Performance_Time);
  11259. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11260. Update_V2G_Flow_Status(Sequence_Timeout);
  11261. //Update_ShmStatusCode(); //[To-Do] to be implemented
  11262. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  11263. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  11264. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  11265. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  11266. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  11267. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  11268. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  11269. break;
  11270. }
  11271. #endif
  11272. //STEP 2: Check for CableCheckReq message
  11273. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  11274. {
  11275. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11276. //STEP 3: Execute Cable Check Process
  11277. if (Proc_din_CableCheckReq(AcceptFd) == 0)
  11278. {
  11279. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11280. }
  11281. else
  11282. {
  11283. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  11284. Update_V2G_Flow_Status(Other_Fault);
  11285. }
  11286. req_is_responsed = TRUE;
  11287. }
  11288. //STEP 3: Check for PreChargeReq message
  11289. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  11290. {
  11291. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11292. Update_V2G_Flow_Status(PreChargeRequest);
  11293. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PreChargeReqReq: waiting for precharge voltage...");
  11294. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  11295. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  11296. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11297. ftime(&SeqStartTime);
  11298. #endif
  11299. }
  11300. //STEP 3: Wait for PowerDeliveryReq Message
  11301. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11302. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11303. {
  11304. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  11305. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11306. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  11307. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11308. ftime(&SeqStartTime);
  11309. #endif
  11310. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  11311. {
  11312. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  11313. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11314. }
  11315. else
  11316. {
  11317. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  11318. Update_V2G_Flow_Status(Other_Fault);
  11319. }
  11320. }
  11321. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11322. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11323. {
  11324. sprintf((char*)buf_log_evcomm,
  11325. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11326. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11327. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11328. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11329. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11330. }
  11331. else
  11332. {
  11333. }
  11334. break;
  11335. }
  11336. //-------------------------------------------
  11337. case PreChargeRequest: //39
  11338. {
  11339. if (Proc_din_PreChargeReq(AcceptFd) == 0)
  11340. {
  11341. Update_V2G_Flow_Status(PreChargeResponse);
  11342. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11343. }
  11344. else
  11345. {
  11346. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  11347. Update_V2G_Flow_Status(Other_Fault);
  11348. }
  11349. req_is_responsed = TRUE;
  11350. break;
  11351. }
  11352. case PreChargeResponse: //40
  11353. {
  11354. //STEP 1: Check for Process Timeout
  11355. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11356. ftime(&SeqEndTime);
  11357. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  11358. {
  11359. sprintf((char*)buf_log_evcomm,
  11360. "Precharge Timeout - (%.02lf of %d ms)\n",
  11361. DiffTimeb(SeqStartTime, SeqEndTime),
  11362. V2G_SECC_PreCharge_Performance_Time);
  11363. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11364. Update_V2G_Flow_Status(Sequence_Timeout);
  11365. //Update_ShmStatusCode(); //[To-Do] to be implemented
  11366. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  11367. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  11368. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  11369. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  11370. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  11371. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  11372. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  11373. break;
  11374. }
  11375. #endif
  11376. //STEP 2: Check for PreChargeReq message
  11377. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  11378. {
  11379. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11380. if (Proc_din_PreChargeReq(AcceptFd) == 0)
  11381. {
  11382. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11383. }
  11384. else
  11385. {
  11386. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  11387. Update_V2G_Flow_Status(Other_Fault);
  11388. }
  11389. req_is_responsed = TRUE;
  11390. }
  11391. //STEP 3: Check for PowerDeliveryReq message
  11392. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11393. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11394. {
  11395. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11396. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  11397. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  11398. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11399. ftime(&SeqStartTime);
  11400. #endif
  11401. }
  11402. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11403. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11404. {
  11405. sprintf((char*)buf_log_evcomm,
  11406. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11407. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11408. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11409. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11410. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11411. }
  11412. else
  11413. {
  11414. }
  11415. break;
  11416. }
  11417. //-------------------------------------------
  11418. case PowerDeliveryRequestStart: //41
  11419. {
  11420. if (Proc_din_PowerDeliveryStartReq(AcceptFd) == 0)
  11421. {
  11422. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  11423. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11424. }
  11425. else
  11426. {
  11427. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStartReqRes: fail");
  11428. Update_V2G_Flow_Status(Other_Fault);
  11429. }
  11430. req_is_responsed = TRUE;
  11431. break;
  11432. }
  11433. case PowerDeliveryResponsetStart: //42
  11434. {
  11435. //STEP 1: Check for Process Timeout
  11436. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11437. if (ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  11438. {
  11439. ftime(&SeqEndTime);
  11440. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  11441. {
  11442. sprintf((char*)buf_log_evcomm,
  11443. "Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n",
  11444. DiffTimeb(SeqStartTime, SeqEndTime),
  11445. 2000);
  11446. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11447. Update_V2G_Flow_Status(Sequence_Timeout);
  11448. }
  11449. break;
  11450. }
  11451. #endif
  11452. //STEP 2: Wait for CurrentDemandReq Message
  11453. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  11454. {
  11455. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11456. Update_V2G_Flow_Status(CurrentDemandRequest);
  11457. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CurrentDemandReqReq: energy transfering...");
  11458. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  11459. }
  11460. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11461. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11462. {
  11463. sprintf((char*)buf_log_evcomm,
  11464. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11465. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11466. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11467. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11468. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11469. }
  11470. else
  11471. {
  11472. }
  11473. break;
  11474. }
  11475. //-------------------------------------------
  11476. case CurrentDemandRequest: //45,
  11477. {
  11478. if (Proc_din_CurrentDemandReq(AcceptFd) == 0)
  11479. {
  11480. Update_V2G_Flow_Status(CurrentDemandResponse);
  11481. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11482. }
  11483. else
  11484. {
  11485. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  11486. Update_V2G_Flow_Status(Other_Fault);
  11487. }
  11488. req_is_responsed = TRUE;
  11489. break;
  11490. }
  11491. case CurrentDemandResponse: //46,
  11492. {
  11493. //STEP 1: Wait for CurrentDemandReq Message
  11494. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  11495. {
  11496. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11497. if (Proc_din_CurrentDemandReq(AcceptFd) == 0)
  11498. {
  11499. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11500. }
  11501. else
  11502. {
  11503. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  11504. Update_V2G_Flow_Status(Other_Fault);
  11505. }
  11506. req_is_responsed = TRUE;
  11507. break;
  11508. }
  11509. //STEP 2: Wait for PowerDeliveryReq Message
  11510. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11511. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11512. {
  11513. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  11514. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11515. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  11516. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11517. ftime(&SeqStartTime);
  11518. #endif
  11519. }
  11520. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11521. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11522. {
  11523. sprintf((char*)buf_log_evcomm,
  11524. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11525. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11526. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11527. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11528. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11529. }
  11530. else
  11531. {
  11532. }
  11533. break;
  11534. }
  11535. //-------------------------------------------
  11536. case PowerDeliveryRequestStop: //49,
  11537. {
  11538. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  11539. {
  11540. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  11541. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11542. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11543. ftime(&SeqStartTime);
  11544. #endif
  11545. }
  11546. else
  11547. {
  11548. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  11549. Update_V2G_Flow_Status(Other_Fault);
  11550. }
  11551. req_is_responsed = TRUE;
  11552. break;
  11553. }
  11554. case PowerDeliveryResponseStop: //50,
  11555. {
  11556. //STEP 1: Check for Process Timeout
  11557. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11558. ftime(&SeqEndTime);
  11559. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  11560. {
  11561. sprintf((char*)buf_log_evcomm,
  11562. "Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n",
  11563. DiffTimeb(SeqStartTime, SeqEndTime),
  11564. 2000);
  11565. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11566. Update_V2G_Flow_Status(Sequence_Timeout);
  11567. break;
  11568. }
  11569. #endif
  11570. //STEP 2: Check for WeldingDetectionReq Message
  11571. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  11572. {
  11573. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11574. Update_V2G_Flow_Status(WeldingDetectionRequest);
  11575. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]WeldingDetectionReq: ongoing...");
  11576. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  11577. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11578. ftime(&SeqStartTime);
  11579. #endif
  11580. }
  11581. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  11582. {
  11583. Update_V2G_Flow_Status(SessionStopRequest);
  11584. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11585. if (Proc_din_SessionStopReq(AcceptFd) == 0)
  11586. {
  11587. Update_V2G_Flow_Status(SessionStopResponse);
  11588. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11589. }
  11590. else
  11591. {
  11592. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  11593. Update_V2G_Flow_Status(Other_Fault);
  11594. }
  11595. break;
  11596. }
  11597. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11598. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11599. {
  11600. sprintf((char*)buf_log_evcomm,
  11601. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11602. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11603. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11604. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11605. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11606. }
  11607. else
  11608. {
  11609. }
  11610. break;
  11611. }
  11612. //-------------------------------------------
  11613. case WeldingDetectionRequest: //51,
  11614. {
  11615. if (Proc_din_WeldingDetectionReq(AcceptFd) == 0)
  11616. {
  11617. Update_V2G_Flow_Status(WeldingDetectionResponse);
  11618. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11619. }
  11620. else
  11621. {
  11622. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  11623. Update_V2G_Flow_Status(Other_Fault);
  11624. }
  11625. req_is_responsed = TRUE;
  11626. break;
  11627. }
  11628. case WeldingDetectionResponse: //52,
  11629. {
  11630. //STEP 1: Check for Process Timeout
  11631. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11632. ftime(&SeqEndTime);
  11633. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  11634. {
  11635. sprintf((char*)buf_log_evcomm,
  11636. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  11637. DiffTimeb(SeqStartTime, SeqEndTime),
  11638. V2G_SECC_WeldingDetection_Performance_Time);
  11639. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11640. Update_V2G_Flow_Status(Sequence_Timeout);
  11641. break;
  11642. }
  11643. #endif
  11644. //STEP 2: Check for WeldingDetectionReq Message
  11645. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  11646. {
  11647. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11648. if (Proc_din_WeldingDetectionReq(AcceptFd) == 0)
  11649. {
  11650. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11651. }
  11652. else
  11653. {
  11654. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  11655. Update_V2G_Flow_Status(Other_Fault);
  11656. }
  11657. req_is_responsed = TRUE;
  11658. }
  11659. //STEP 3: Check for SessionStopReq Message
  11660. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  11661. {
  11662. Update_V2G_Flow_Status(SessionStopRequest);
  11663. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11664. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  11665. }
  11666. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11667. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11668. {
  11669. sprintf((char*)buf_log_evcomm,
  11670. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11671. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11672. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11673. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11674. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11675. }
  11676. else
  11677. {
  11678. }
  11679. break;
  11680. }
  11681. //-------------------------------------------
  11682. case SessionStopRequest: //53,
  11683. {
  11684. if (Proc_din_SessionStopReq(AcceptFd) == 0)
  11685. {
  11686. Update_V2G_Flow_Status(SessionStopResponse);
  11687. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11688. }
  11689. else
  11690. {
  11691. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  11692. Update_V2G_Flow_Status(Other_Fault);
  11693. }
  11694. req_is_responsed = TRUE;
  11695. break;
  11696. }
  11697. case SessionStopResponse: //54,
  11698. {
  11699. break;
  11700. }
  11701. //-------------------------------------------
  11702. default:
  11703. {
  11704. break;
  11705. }
  11706. }
  11707. }
  11708. return 0;
  11709. }
  11710. /*===========================================================================
  11711. FUNCTION: V2gMsg_Process_iso1_DC
  11712. DESCRIPTION:
  11713. PRE-CONDITION:
  11714. INPUT:
  11715. 1. V2gFlowStatus
  11716. OUTPUT:
  11717. GLOBAL VARIABLES:
  11718. 1. V2gFlowStatus
  11719. =============================================================================*/
  11720. int V2gMsg_Process_iso1_DC(int AcceptFd)
  11721. {
  11722. unsigned char req_is_responsed = FALSE;
  11723. while (req_is_responsed == FALSE)
  11724. {
  11725. //Check if it is in End_Process
  11726. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  11727. {
  11728. break;
  11729. }
  11730. switch(V2gFlowStatus)
  11731. {
  11732. //-------------------------------------------
  11733. case SupportedAppProtocolRequest:
  11734. {
  11735. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  11736. {
  11737. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  11738. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  11739. {
  11740. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11741. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  11742. }
  11743. else
  11744. {
  11745. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]supportedAppProtocolResReq: fail");
  11746. Update_V2G_Flow_Status(Other_Fault);
  11747. }
  11748. }
  11749. req_is_responsed = TRUE;
  11750. break;
  11751. }
  11752. case SupportedAppProtocolResponse:
  11753. {
  11754. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  11755. {
  11756. Update_V2G_Flow_Status(SessionSetupRequest);
  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((char*)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. }
  11772. break;
  11773. }
  11774. //-------------------------------------------
  11775. case SessionSetupRequest: //19
  11776. {
  11777. if (Proc_iso1_SessionSetupReq(AcceptFd) == 0)
  11778. {
  11779. Update_V2G_Flow_Status(SessionSetupResponse);
  11780. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11781. }
  11782. else
  11783. {
  11784. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionSetupReqRes: fail");
  11785. Update_V2G_Flow_Status(Other_Fault);
  11786. }
  11787. req_is_responsed = TRUE;
  11788. break;
  11789. }
  11790. case SessionSetupResponse: //20
  11791. {
  11792. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  11793. {
  11794. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  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((char*)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. }
  11810. break;
  11811. }
  11812. //-------------------------------------------
  11813. case ServiceDiscoveryRequest: //21
  11814. {
  11815. if (Proc_iso1_ServiceDiscoveryReq(AcceptFd) == 0)
  11816. {
  11817. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  11818. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11819. }
  11820. else
  11821. {
  11822. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceDiscoveryReqRes: fail");
  11823. Update_V2G_Flow_Status(Other_Fault);
  11824. }
  11825. req_is_responsed = TRUE;
  11826. break;
  11827. }
  11828. case ServiceDiscoveryResponse: //22
  11829. {
  11830. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  11831. {
  11832. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  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((char*)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. }
  11848. break;
  11849. }
  11850. //-------------------------------------------
  11851. case ServiceAndPaymentSelectionRequest: //25
  11852. {
  11853. if (Proc_iso1_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  11854. {
  11855. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  11856. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11857. }
  11858. else
  11859. {
  11860. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceAndPaymentSelectionReqRes: fail");
  11861. Update_V2G_Flow_Status(Other_Fault);
  11862. }
  11863. req_is_responsed = TRUE;
  11864. break;
  11865. }
  11866. case ServiceAndPaymentSelectionResponse: //26
  11867. {
  11868. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11869. {
  11870. Update_V2G_Flow_Status(AuthorizationRequest);
  11871. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11872. }
  11873. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11874. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11875. {
  11876. sprintf((char*)buf_log_evcomm,
  11877. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11878. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11879. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11880. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11881. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11882. }
  11883. else
  11884. {
  11885. }
  11886. break;
  11887. }
  11888. //-------------------------------------------
  11889. //case ContractAuthenticationReq:
  11890. case AuthorizationRequest: //29
  11891. {
  11892. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  11893. {
  11894. Update_V2G_Flow_Status(AuthorizationResponse);
  11895. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11896. }
  11897. else
  11898. {
  11899. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  11900. Update_V2G_Flow_Status(Other_Fault);
  11901. }
  11902. req_is_responsed = TRUE;
  11903. break;
  11904. }
  11905. case AuthorizationResponse: //30
  11906. {
  11907. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11908. {
  11909. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11910. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  11911. {
  11912. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11913. }
  11914. else
  11915. {
  11916. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  11917. Update_V2G_Flow_Status(Other_Fault);
  11918. }
  11919. req_is_responsed = TRUE;
  11920. }
  11921. //Check for ChargeParameterDiscoveryReq
  11922. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11923. {
  11924. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11925. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  11926. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  11927. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11928. ftime(&SeqStartTime);
  11929. #endif
  11930. }
  11931. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11932. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11933. {
  11934. sprintf((char*)buf_log_evcomm,
  11935. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11936. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11937. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11938. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11939. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11940. }
  11941. else
  11942. {
  11943. }
  11944. break;
  11945. }
  11946. //-------------------------------------------
  11947. case ChargeParameterDiscoveryRequest: //35
  11948. {
  11949. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11950. {
  11951. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  11952. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11953. }
  11954. else
  11955. {
  11956. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  11957. Update_V2G_Flow_Status(Other_Fault);
  11958. }
  11959. req_is_responsed = TRUE;
  11960. break;
  11961. }
  11962. case ChargeParameterDiscoveryResponse:
  11963. {
  11964. //STEP 1: Check for Process Timeout
  11965. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11966. ftime(&SeqEndTime);
  11967. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  11968. {
  11969. sprintf((char*)buf_log_evcomm,
  11970. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  11971. DiffTimeb(SeqStartTime, SeqEndTime),
  11972. V2G_SECC_ChargingParameter_Performance_Time);
  11973. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11974. Update_V2G_Flow_Status(Sequence_Timeout);
  11975. break;
  11976. }
  11977. #endif
  11978. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11979. {
  11980. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11981. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11982. {
  11983. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11984. }
  11985. else
  11986. {
  11987. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  11988. Update_V2G_Flow_Status(Other_Fault);
  11989. }
  11990. req_is_responsed = TRUE;
  11991. }
  11992. //STEP 2: Check for CableCheckReq message
  11993. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  11994. {
  11995. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11996. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CableCheckReqReq: isolated?");
  11997. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  11998. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  11999. Update_V2G_Flow_Status(CableCheckRequest);
  12000. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12001. ftime(&SeqStartTime);
  12002. #endif
  12003. }
  12004. //STEP 3: Wait for PowerDeliveryReq Message
  12005. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12006. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12007. {
  12008. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  12009. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12010. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  12011. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12012. ftime(&SeqStartTime);
  12013. #endif
  12014. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12015. {
  12016. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12017. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12018. }
  12019. else
  12020. {
  12021. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  12022. Update_V2G_Flow_Status(Other_Fault);
  12023. }
  12024. break;
  12025. }
  12026. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12027. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12028. {
  12029. sprintf((char*)buf_log_evcomm,
  12030. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12031. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12032. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12033. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12034. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12035. }
  12036. else
  12037. {
  12038. }
  12039. break;
  12040. }
  12041. //-------------------------------------------
  12042. case CableCheckRequest: //37
  12043. {
  12044. //STEP 3: Execute Cable Check Process
  12045. if (Proc_iso1_CableCheckReq(AcceptFd) == 0)
  12046. {
  12047. Update_V2G_Flow_Status(CableCheckResponse);
  12048. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12049. }
  12050. else
  12051. {
  12052. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  12053. Update_V2G_Flow_Status(Other_Fault);
  12054. }
  12055. req_is_responsed = TRUE;
  12056. break;
  12057. }
  12058. case CableCheckResponse: //38
  12059. {
  12060. //STEP 1: Check for Process Timeout
  12061. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12062. ftime(&SeqEndTime);
  12063. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  12064. {
  12065. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CableCheck] end counting...");
  12066. sprintf((char*)buf_log_evcomm,
  12067. "CableCheck Timeout - (%.02lf of %d ms)\n",
  12068. DiffTimeb(SeqStartTime, SeqEndTime),
  12069. V2G_SECC_CableCheck_Performance_Time);
  12070. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12071. Update_V2G_Flow_Status(Sequence_Timeout);
  12072. //Update_ShmStatusCode(); //[To-Do] to be implemented
  12073. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  12074. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  12075. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  12076. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  12077. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  12078. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  12079. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  12080. break;
  12081. }
  12082. #endif
  12083. //STEP 2: Check for CableCheckReq message
  12084. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  12085. {
  12086. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12087. //STEP 3: Execute Cable Check Process
  12088. if (Proc_iso1_CableCheckReq(AcceptFd) == 0)
  12089. {
  12090. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12091. }
  12092. else
  12093. {
  12094. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  12095. Update_V2G_Flow_Status(Other_Fault);
  12096. }
  12097. req_is_responsed = TRUE;
  12098. }
  12099. //STEP 3: Check for PreChargeReq message
  12100. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  12101. {
  12102. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12103. Update_V2G_Flow_Status(PreChargeRequest);
  12104. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PreChargeReqReq: waiting for precharge voltage...");
  12105. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  12106. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  12107. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12108. ftime(&SeqStartTime);
  12109. #endif
  12110. }
  12111. //STEP 3: Wait for PowerDeliveryReq Message
  12112. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12113. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12114. {
  12115. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  12116. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12117. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  12118. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12119. ftime(&SeqStartTime);
  12120. #endif
  12121. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12122. {
  12123. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12124. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12125. }
  12126. else
  12127. {
  12128. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  12129. Update_V2G_Flow_Status(Other_Fault);
  12130. }
  12131. }
  12132. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12133. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12134. {
  12135. sprintf((char*)buf_log_evcomm,
  12136. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12137. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12138. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12139. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12140. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12141. }
  12142. else
  12143. {
  12144. }
  12145. break;
  12146. }
  12147. //-------------------------------------------
  12148. case PreChargeRequest: //39
  12149. {
  12150. if (Proc_iso1_PreChargeReq(AcceptFd) == 0)
  12151. {
  12152. Update_V2G_Flow_Status(PreChargeResponse);
  12153. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12154. }
  12155. else
  12156. {
  12157. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  12158. Update_V2G_Flow_Status(Other_Fault);
  12159. }
  12160. req_is_responsed = TRUE;
  12161. break;
  12162. }
  12163. case PreChargeResponse: //40
  12164. {
  12165. //STEP 1: Check for Process Timeout
  12166. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12167. ftime(&SeqEndTime);
  12168. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  12169. {
  12170. sprintf((char*)buf_log_evcomm,
  12171. "Precharge Timeout - (%.02lf of %d ms)\n",
  12172. DiffTimeb(SeqStartTime, SeqEndTime),
  12173. V2G_SECC_PreCharge_Performance_Time);
  12174. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12175. Update_V2G_Flow_Status(Sequence_Timeout);
  12176. //Update_ShmStatusCode(); //[To-Do] to be implemented
  12177. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  12178. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  12179. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  12180. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  12181. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  12182. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  12183. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  12184. break;
  12185. }
  12186. #endif
  12187. //STEP 2: Check for PreChargeReq message
  12188. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  12189. {
  12190. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12191. if (Proc_iso1_PreChargeReq(AcceptFd) == 0)
  12192. {
  12193. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12194. }
  12195. else
  12196. {
  12197. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  12198. Update_V2G_Flow_Status(Other_Fault);
  12199. }
  12200. req_is_responsed = TRUE;
  12201. }
  12202. //STEP 3: Check for PowerDeliveryReq message
  12203. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12204. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12205. {
  12206. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12207. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  12208. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  12209. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12210. ftime(&SeqStartTime);
  12211. #endif
  12212. }
  12213. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12214. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12215. {
  12216. sprintf((char*)buf_log_evcomm,
  12217. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12218. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12219. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12220. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12221. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12222. }
  12223. else
  12224. {
  12225. }
  12226. break;
  12227. }
  12228. //-------------------------------------------
  12229. case PowerDeliveryRequestStart: //41
  12230. {
  12231. if (Proc_iso1_PowerDeliveryStartReq(AcceptFd) == 0)
  12232. {
  12233. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  12234. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12235. }
  12236. else
  12237. {
  12238. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStartReqRes: fail");
  12239. Update_V2G_Flow_Status(Other_Fault);
  12240. }
  12241. req_is_responsed = TRUE;
  12242. break;
  12243. }
  12244. case PowerDeliveryResponsetStart: //42
  12245. {
  12246. //STEP 1: Check for Process Timeout
  12247. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12248. if (ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  12249. {
  12250. ftime(&SeqEndTime);
  12251. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  12252. {
  12253. sprintf((char*)buf_log_evcomm,
  12254. "Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n",
  12255. DiffTimeb(SeqStartTime, SeqEndTime),
  12256. 2000);
  12257. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12258. Update_V2G_Flow_Status(Sequence_Timeout);
  12259. }
  12260. break;
  12261. }
  12262. #endif
  12263. //STEP 2: Wait for CurrentDemandReq Message
  12264. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  12265. {
  12266. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12267. Update_V2G_Flow_Status(CurrentDemandRequest);
  12268. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CurrentDemandReqReq: energy transfering...");
  12269. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  12270. }
  12271. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12272. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12273. {
  12274. sprintf((char*)buf_log_evcomm,
  12275. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12276. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12277. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12278. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12279. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12280. }
  12281. else
  12282. {
  12283. }
  12284. break;
  12285. }
  12286. //-------------------------------------------
  12287. case CurrentDemandRequest: //45,
  12288. {
  12289. if (Proc_iso1_CurrentDemandReq(AcceptFd) == 0)
  12290. {
  12291. Update_V2G_Flow_Status(CurrentDemandResponse);
  12292. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12293. }
  12294. else
  12295. {
  12296. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  12297. Update_V2G_Flow_Status(Other_Fault);
  12298. }
  12299. req_is_responsed = TRUE;
  12300. break;
  12301. }
  12302. case CurrentDemandResponse: //46,
  12303. {
  12304. //STEP 1: Wait for CurrentDemandReq Message
  12305. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  12306. {
  12307. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12308. if (Proc_iso1_CurrentDemandReq(AcceptFd) == 0)
  12309. {
  12310. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12311. }
  12312. else
  12313. {
  12314. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  12315. Update_V2G_Flow_Status(Other_Fault);
  12316. }
  12317. req_is_responsed = TRUE;
  12318. }
  12319. //STEP 2: Wait for PowerDeliveryReq Message
  12320. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12321. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12322. {
  12323. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  12324. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12325. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  12326. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12327. ftime(&SeqStartTime);
  12328. #endif
  12329. }
  12330. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12331. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12332. {
  12333. sprintf((char*)buf_log_evcomm,
  12334. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12335. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12336. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12337. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12338. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12339. }
  12340. else
  12341. {
  12342. }
  12343. break;
  12344. }
  12345. //-------------------------------------------
  12346. case PowerDeliveryRequestStop: //49,
  12347. {
  12348. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12349. {
  12350. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12351. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12352. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12353. ftime(&SeqStartTime);
  12354. #endif
  12355. }
  12356. else
  12357. {
  12358. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  12359. Update_V2G_Flow_Status(Other_Fault);
  12360. }
  12361. req_is_responsed = TRUE;
  12362. break;
  12363. }
  12364. case PowerDeliveryResponseStop: //50,
  12365. {
  12366. //STEP 1: Check for Process Timeout
  12367. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12368. ftime(&SeqEndTime);
  12369. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  12370. {
  12371. sprintf((char*)buf_log_evcomm,
  12372. "Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n",
  12373. DiffTimeb(SeqStartTime, SeqEndTime),
  12374. 2000);
  12375. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12376. Update_V2G_Flow_Status(Sequence_Timeout);
  12377. break;
  12378. }
  12379. #endif
  12380. //STEP 2: Check for WeldingDetectionReq Message
  12381. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  12382. {
  12383. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12384. Update_V2G_Flow_Status(WeldingDetectionRequest);
  12385. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]WeldingDetectionReq: ongoing...");
  12386. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  12387. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12388. ftime(&SeqStartTime);
  12389. #endif
  12390. }
  12391. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  12392. {
  12393. Update_V2G_Flow_Status(SessionStopRequest);
  12394. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12395. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  12396. {
  12397. Update_V2G_Flow_Status(SessionStopResponse);
  12398. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12399. }
  12400. else
  12401. {
  12402. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  12403. Update_V2G_Flow_Status(Other_Fault);
  12404. }
  12405. }
  12406. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12407. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12408. {
  12409. sprintf((char*)buf_log_evcomm,
  12410. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12411. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12412. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12413. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12414. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12415. }
  12416. else
  12417. {
  12418. }
  12419. break;
  12420. }
  12421. //-------------------------------------------
  12422. case WeldingDetectionRequest: //51,
  12423. {
  12424. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  12425. {
  12426. Update_V2G_Flow_Status(WeldingDetectionResponse);
  12427. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12428. }
  12429. else
  12430. {
  12431. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  12432. Update_V2G_Flow_Status(Other_Fault);
  12433. }
  12434. req_is_responsed = TRUE;
  12435. break;
  12436. }
  12437. case WeldingDetectionResponse: //52,
  12438. {
  12439. //STEP 1: Check for Process Timeout
  12440. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12441. ftime(&SeqEndTime);
  12442. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  12443. {
  12444. sprintf((char*)buf_log_evcomm,
  12445. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  12446. DiffTimeb(SeqStartTime, SeqEndTime),
  12447. V2G_SECC_WeldingDetection_Performance_Time);
  12448. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12449. Update_V2G_Flow_Status(Sequence_Timeout);
  12450. break;
  12451. }
  12452. #endif
  12453. //STEP 2: Check for WeldingDetectionReq Message
  12454. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  12455. {
  12456. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12457. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  12458. {
  12459. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12460. }
  12461. else
  12462. {
  12463. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  12464. Update_V2G_Flow_Status(Other_Fault);
  12465. }
  12466. req_is_responsed = TRUE;
  12467. }
  12468. //STEP 3: Check for SessionStopReq Message
  12469. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  12470. {
  12471. Update_V2G_Flow_Status(SessionStopRequest);
  12472. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12473. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  12474. }
  12475. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12476. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12477. {
  12478. sprintf((char*)buf_log_evcomm,
  12479. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12480. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12481. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12482. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12483. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12484. }
  12485. else
  12486. {
  12487. }
  12488. break;
  12489. }
  12490. //-------------------------------------------
  12491. case SessionStopRequest: //53,
  12492. {
  12493. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  12494. {
  12495. Update_V2G_Flow_Status(SessionStopResponse);
  12496. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12497. }
  12498. else
  12499. {
  12500. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  12501. Update_V2G_Flow_Status(Other_Fault);
  12502. }
  12503. req_is_responsed = TRUE;
  12504. break;
  12505. }
  12506. case SessionStopResponse: //54,
  12507. {
  12508. break;
  12509. }
  12510. //-------------------------------------------
  12511. default:
  12512. {
  12513. break;
  12514. }
  12515. }
  12516. }
  12517. return 0;
  12518. }
  12519. /*===========================================================================
  12520. FUNCTION: V2gMsg_Process_iso1_AC
  12521. DESCRIPTION:
  12522. PRE-CONDITION:
  12523. INPUT:
  12524. 1. V2gFlowStatus
  12525. OUTPUT:
  12526. GLOBAL VARIABLES:
  12527. 1. V2gFlowStatus
  12528. =============================================================================*/
  12529. int V2gMsg_Process_iso1_AC(int AcceptFd)
  12530. {
  12531. unsigned char req_is_responsed = FALSE;
  12532. while (req_is_responsed == FALSE)
  12533. {
  12534. //Check if it is in End_Process
  12535. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  12536. {
  12537. break;
  12538. }
  12539. switch(V2gFlowStatus)
  12540. {
  12541. //-------------------------------------------
  12542. case SupportedAppProtocolRequest: //17
  12543. {
  12544. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  12545. {
  12546. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  12547. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  12548. {
  12549. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12550. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  12551. }
  12552. else
  12553. {
  12554. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]supportedAppProtocolResReq: fail");
  12555. Update_V2G_Flow_Status(Other_Fault);
  12556. }
  12557. }
  12558. req_is_responsed = TRUE;
  12559. break;
  12560. }
  12561. case SupportedAppProtocolResponse: //18
  12562. {
  12563. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  12564. {
  12565. Update_V2G_Flow_Status(SessionSetupRequest);
  12566. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12567. }
  12568. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12569. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12570. {
  12571. sprintf((char*)buf_log_evcomm,
  12572. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12573. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12574. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12575. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12576. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12577. }
  12578. else
  12579. {
  12580. }
  12581. break;
  12582. }
  12583. //-------------------------------------------
  12584. case SessionSetupRequest: //19
  12585. {
  12586. if (Proc_iso1_SessionSetupReq(AcceptFd) == 0)
  12587. {
  12588. Update_V2G_Flow_Status(SessionSetupResponse);
  12589. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12590. }
  12591. else
  12592. {
  12593. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionSetupReqRes: fail");
  12594. Update_V2G_Flow_Status(Other_Fault);
  12595. }
  12596. req_is_responsed = TRUE;
  12597. break;
  12598. }
  12599. case SessionSetupResponse: //20
  12600. {
  12601. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  12602. {
  12603. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  12604. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12605. }
  12606. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12607. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12608. {
  12609. sprintf((char*)buf_log_evcomm,
  12610. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12611. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12612. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12613. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12614. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12615. }
  12616. else
  12617. {
  12618. }
  12619. break;
  12620. }
  12621. //-------------------------------------------
  12622. case ServiceDiscoveryRequest: //21
  12623. {
  12624. if (Proc_iso1_ServiceDiscoveryReq(AcceptFd) == 0)
  12625. {
  12626. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  12627. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12628. }
  12629. else
  12630. {
  12631. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceDiscoveryReqRes: fail");
  12632. Update_V2G_Flow_Status(Other_Fault);
  12633. }
  12634. req_is_responsed = TRUE;
  12635. break;
  12636. }
  12637. case ServiceDiscoveryResponse: //22
  12638. {
  12639. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  12640. {
  12641. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  12642. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12643. }
  12644. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12645. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12646. {
  12647. sprintf((char*)buf_log_evcomm,
  12648. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12649. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12650. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12651. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12652. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12653. }
  12654. else
  12655. {
  12656. }
  12657. break;
  12658. }
  12659. //-------------------------------------------
  12660. case ServiceAndPaymentSelectionRequest: //25
  12661. {
  12662. if (Proc_iso1_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  12663. {
  12664. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  12665. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12666. }
  12667. else
  12668. {
  12669. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceAndPaymentSelectionReqRes: fail");
  12670. Update_V2G_Flow_Status(Other_Fault);
  12671. }
  12672. req_is_responsed = TRUE;
  12673. break;
  12674. }
  12675. case ServiceAndPaymentSelectionResponse://26
  12676. {
  12677. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  12678. {
  12679. Update_V2G_Flow_Status(AuthorizationRequest);
  12680. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12681. }
  12682. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12683. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12684. {
  12685. sprintf((char*)buf_log_evcomm,
  12686. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12687. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12688. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12689. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12690. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12691. }
  12692. else
  12693. {
  12694. }
  12695. break;
  12696. }
  12697. //-------------------------------------------
  12698. //case ContractAuthenticationReq:
  12699. case AuthorizationRequest: //29
  12700. {
  12701. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  12702. {
  12703. Update_V2G_Flow_Status(AuthorizationResponse);
  12704. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12705. }
  12706. else
  12707. {
  12708. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  12709. Update_V2G_Flow_Status(Other_Fault);
  12710. }
  12711. break;
  12712. }
  12713. case AuthorizationResponse: //30
  12714. {
  12715. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  12716. {
  12717. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12718. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  12719. {
  12720. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12721. }
  12722. else
  12723. {
  12724. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  12725. Update_V2G_Flow_Status(Other_Fault);
  12726. }
  12727. req_is_responsed = TRUE;
  12728. }
  12729. //Check for ChargeParameterDiscoveryReq
  12730. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  12731. {
  12732. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12733. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  12734. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  12735. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12736. ftime(&SeqStartTime);
  12737. #endif
  12738. }
  12739. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12740. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12741. {
  12742. sprintf((char*)buf_log_evcomm,
  12743. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12744. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12745. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12746. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12747. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12748. }
  12749. else
  12750. {
  12751. }
  12752. break;
  12753. }
  12754. //-------------------------------------------
  12755. case ChargeParameterDiscoveryRequest: //35
  12756. {
  12757. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  12758. {
  12759. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  12760. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12761. }
  12762. else
  12763. {
  12764. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  12765. Update_V2G_Flow_Status(Other_Fault);
  12766. }
  12767. req_is_responsed = TRUE;
  12768. break;
  12769. }
  12770. case ChargeParameterDiscoveryResponse: //36
  12771. {
  12772. //STEP 1: Check for Process Timeout
  12773. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12774. ftime(&SeqEndTime);
  12775. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  12776. {
  12777. sprintf((char*)buf_log_evcomm,
  12778. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  12779. DiffTimeb(SeqStartTime, SeqEndTime),
  12780. V2G_SECC_ChargingParameter_Performance_Time);
  12781. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12782. Update_V2G_Flow_Status(Sequence_Timeout);
  12783. break;
  12784. }
  12785. #endif
  12786. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  12787. {
  12788. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12789. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  12790. {
  12791. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12792. }
  12793. else
  12794. {
  12795. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  12796. Update_V2G_Flow_Status(Other_Fault);
  12797. }
  12798. req_is_responsed = TRUE;
  12799. }
  12800. //STEP 3: Wait for PowerDeliveryReq Message
  12801. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12802. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12803. {
  12804. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  12805. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12806. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  12807. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12808. ftime(&SeqStartTime);
  12809. #endif
  12810. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12811. {
  12812. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12813. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12814. }
  12815. else
  12816. {
  12817. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  12818. Update_V2G_Flow_Status(Other_Fault);
  12819. }
  12820. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12821. ftime(&SeqStartTime);
  12822. #endif
  12823. }
  12824. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12825. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12826. {
  12827. sprintf((char*)buf_log_evcomm,
  12828. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12829. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12830. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12831. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12832. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12833. }
  12834. else
  12835. {
  12836. }
  12837. break;
  12838. }
  12839. //-------------------------------------------
  12840. case PowerDeliveryRequestStart: //41
  12841. {
  12842. if (Proc_iso1_PowerDeliveryStartReq(AcceptFd) == 0)
  12843. {
  12844. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  12845. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12846. }
  12847. else
  12848. {
  12849. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStartReqRes: fail");
  12850. Update_V2G_Flow_Status(Other_Fault);
  12851. }
  12852. req_is_responsed = TRUE;
  12853. break;
  12854. }
  12855. case PowerDeliveryResponsetStart: //42
  12856. {
  12857. //STEP 1: Check for Process Timeout
  12858. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12859. if (ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  12860. {
  12861. ftime(&SeqEndTime);
  12862. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  12863. {
  12864. sprintf((char*)buf_log_evcomm,
  12865. "Wait for ChargingStatusReq Timeout - (%.02lf of %d ms)\n",
  12866. DiffTimeb(SeqStartTime, SeqEndTime),
  12867. 2000);
  12868. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12869. Update_V2G_Flow_Status(Sequence_Timeout);
  12870. }
  12871. break;
  12872. }
  12873. #endif
  12874. //STEP 2: Wait for ChargingStatusReq Message
  12875. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargingStatusRequest)
  12876. {
  12877. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12878. Update_V2G_Flow_Status(ChargingStatusRequest);
  12879. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]ChargingStatusReq: energy transfering...");
  12880. ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargingStatus_Timer_Start);
  12881. }
  12882. else
  12883. {
  12884. }
  12885. break;
  12886. }
  12887. //-------------------------------------------
  12888. case ChargingStatusRequest: //43
  12889. {
  12890. if (Proc_iso1_ChargingStatusReq(AcceptFd) == 0)
  12891. {
  12892. Update_V2G_Flow_Status(ChargingStatusResponse);
  12893. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12894. }
  12895. else
  12896. {
  12897. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargingStatusRes: fail");
  12898. Update_V2G_Flow_Status(Other_Fault);
  12899. }
  12900. req_is_responsed = TRUE;
  12901. break;
  12902. }
  12903. case ChargingStatusResponse: //44
  12904. {
  12905. //STEP 1: Wait for ChargingStatusReq Message
  12906. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargingStatusRequest)
  12907. {
  12908. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12909. if (Proc_iso1_ChargingStatusReq(AcceptFd) == 0)
  12910. {
  12911. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12912. }
  12913. else
  12914. {
  12915. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargingStatusRes: fail");
  12916. Update_V2G_Flow_Status(Other_Fault);
  12917. }
  12918. req_is_responsed = TRUE;
  12919. }
  12920. //STEP 2: Wait for PowerDeliveryReq Message
  12921. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12922. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12923. {
  12924. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  12925. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12926. ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargingStatus_Timer_End);
  12927. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12928. ftime(&SeqStartTime);
  12929. #endif
  12930. }
  12931. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12932. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12933. {
  12934. sprintf((char*)buf_log_evcomm,
  12935. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12936. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12937. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12938. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12939. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12940. }
  12941. else
  12942. {
  12943. }
  12944. break;
  12945. }
  12946. //-------------------------------------------
  12947. case PowerDeliveryRequestStop: //49
  12948. {
  12949. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12950. {
  12951. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12952. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12953. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12954. ftime(&SeqStartTime);
  12955. #endif
  12956. }
  12957. else
  12958. {
  12959. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  12960. Update_V2G_Flow_Status(Other_Fault);
  12961. }
  12962. req_is_responsed = TRUE;
  12963. break;
  12964. }
  12965. case PowerDeliveryResponseStop: //50
  12966. {
  12967. //STEP 1: Check for Process Timeout
  12968. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12969. ftime(&SeqEndTime);
  12970. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  12971. {
  12972. sprintf((char*)buf_log_evcomm,
  12973. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  12974. DiffTimeb(SeqStartTime, SeqEndTime),
  12975. 2000);
  12976. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12977. Update_V2G_Flow_Status(Sequence_Timeout);
  12978. break;
  12979. }
  12980. #endif
  12981. //STEP 3: Check for SessionStopReq Message
  12982. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  12983. {
  12984. Update_V2G_Flow_Status(SessionStopRequest);
  12985. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12986. }
  12987. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12988. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12989. {
  12990. sprintf((char*)buf_log_evcomm,
  12991. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12992. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12993. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12994. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12995. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12996. }
  12997. else
  12998. {
  12999. }
  13000. break;
  13001. }
  13002. //-------------------------------------------
  13003. case SessionStopRequest: //53
  13004. {
  13005. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  13006. {
  13007. Update_V2G_Flow_Status(SessionStopResponse);
  13008. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13009. }
  13010. else
  13011. {
  13012. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  13013. Update_V2G_Flow_Status(Other_Fault);
  13014. }
  13015. req_is_responsed = TRUE;
  13016. break;
  13017. }
  13018. case SessionStopResponse: //54
  13019. {
  13020. break;
  13021. }
  13022. //-------------------------------------------
  13023. default:
  13024. {
  13025. break;
  13026. }
  13027. }
  13028. }
  13029. return 0;
  13030. }
  13031. /*===========================================================================
  13032. FUNCTION: V2gMsg_Process_iso1
  13033. DESCRIPTION:
  13034. 1. EnergyTransferMode:
  13035. AC_single_phase_core = 0,
  13036. AC_three_phase_core = 1,
  13037. DC_core = 2,
  13038. DC_extended = 3,
  13039. DC_combo_core = 4,
  13040. DC_unique = 5
  13041. iso1EnergyTransferModeType_AC_single_phase_core = 0,
  13042. iso1EnergyTransferModeType_AC_three_phase_core = 1,
  13043. iso1EnergyTransferModeType_DC_core = 2,
  13044. iso1EnergyTransferModeType_DC_extended = 3,
  13045. iso1EnergyTransferModeType_DC_combo_core = 4,
  13046. iso1EnergyTransferModeType_DC_unique = 5
  13047. PRE-CONDITION:
  13048. INPUT:
  13049. 1. V2gFlowStatus
  13050. OUTPUT:
  13051. GLOBAL VARIABLES:
  13052. 1. V2gFlowStatus
  13053. =============================================================================*/
  13054. int V2gMsg_Process_iso1(int AcceptFd, unsigned char EnergyTransferMode)
  13055. {
  13056. switch (EnergyTransferMode)
  13057. {
  13058. case DC_extended:
  13059. {
  13060. V2gMsg_Process_iso1_DC(AcceptFd);
  13061. break;
  13062. }
  13063. case AC_single_phase_core:
  13064. case AC_three_phase_core:
  13065. {
  13066. V2gMsg_Process_iso1_AC(AcceptFd);
  13067. break;
  13068. }
  13069. default:
  13070. {
  13071. sprintf((char*)buf_log_evcomm,
  13072. "[ISO1][Warning]Unexpected EnergyTransferMode(%d)",
  13073. EnergyTransferMode);
  13074. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13075. break;
  13076. }
  13077. }
  13078. return 0;
  13079. }
  13080. /*===========================================================================
  13081. FUNCTION: V2gMsg_Process_iso2_DC
  13082. DESCRIPTION:
  13083. PRE-CONDITION:
  13084. INPUT:
  13085. 1. V2gFlowStatus
  13086. OUTPUT:
  13087. GLOBAL VARIABLES:
  13088. 1. V2gFlowStatus
  13089. =============================================================================*/
  13090. int V2gMsg_Process_iso2_DC(int AcceptFd)
  13091. {
  13092. unsigned char req_is_responsed = FALSE;
  13093. while (req_is_responsed == FALSE)
  13094. {
  13095. //Check if it is in End_Process
  13096. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  13097. {
  13098. break;
  13099. }
  13100. switch(V2gFlowStatus)
  13101. {
  13102. //-------------------------------------------
  13103. case SupportedAppProtocolRequest:
  13104. {
  13105. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  13106. {
  13107. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  13108. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  13109. {
  13110. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13111. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  13112. }
  13113. else
  13114. {
  13115. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]supportedAppProtocolResReq: fail");
  13116. Update_V2G_Flow_Status(Other_Fault);
  13117. }
  13118. }
  13119. req_is_responsed = TRUE;
  13120. break;
  13121. }
  13122. case SupportedAppProtocolResponse:
  13123. {
  13124. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  13125. {
  13126. Update_V2G_Flow_Status(SessionSetupRequest);
  13127. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13128. }
  13129. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13130. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13131. {
  13132. sprintf((char*)buf_log_evcomm,
  13133. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13134. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13135. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13136. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13137. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13138. }
  13139. else
  13140. {
  13141. }
  13142. break;
  13143. }
  13144. //-------------------------------------------
  13145. case SessionSetupRequest: //19
  13146. {
  13147. if (Proc_iso2_SessionSetupReq(AcceptFd) == 0)
  13148. {
  13149. Update_V2G_Flow_Status(SessionSetupResponse);
  13150. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13151. }
  13152. else
  13153. {
  13154. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionSetupReqRes: fail");
  13155. Update_V2G_Flow_Status(Other_Fault);
  13156. }
  13157. req_is_responsed = TRUE;
  13158. break;
  13159. }
  13160. case SessionSetupResponse: //20
  13161. {
  13162. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  13163. {
  13164. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  13165. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13166. }
  13167. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13168. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13169. {
  13170. sprintf((char*)buf_log_evcomm,
  13171. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13172. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13173. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13174. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13175. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13176. }
  13177. else
  13178. {
  13179. }
  13180. break;
  13181. }
  13182. #if 0
  13183. //-------------------------------------------
  13184. case ServiceDiscoveryRequest: //21
  13185. {
  13186. if (Proc_iso2_ServiceDiscoveryReq(AcceptFd) == 0)
  13187. {
  13188. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  13189. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13190. }
  13191. else
  13192. {
  13193. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceDiscoveryReqRes: fail");
  13194. Update_V2G_Flow_Status(Other_Fault);
  13195. }
  13196. req_is_responsed = TRUE;
  13197. break;
  13198. }
  13199. case ServiceDiscoveryResponse: //22
  13200. {
  13201. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  13202. {
  13203. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  13204. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13205. }
  13206. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13207. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13208. {
  13209. sprintf((char*)buf_log_evcomm,
  13210. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13211. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13212. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13213. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13214. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13215. }
  13216. else
  13217. {
  13218. break;
  13219. }
  13220. }
  13221. //-------------------------------------------
  13222. case ServiceAndPaymentSelectionRequest: //25
  13223. {
  13224. if (Proc_iso2_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  13225. {
  13226. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  13227. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13228. }
  13229. else
  13230. {
  13231. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceAndPaymentSelectionReqRes: fail");
  13232. Update_V2G_Flow_Status(Other_Fault);
  13233. }
  13234. req_is_responsed = TRUE;
  13235. break;
  13236. }
  13237. case ServiceAndPaymentSelectionResponse: //26
  13238. {
  13239. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  13240. {
  13241. Update_V2G_Flow_Status(AuthorizationRequest);
  13242. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13243. }
  13244. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13245. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13246. {
  13247. sprintf((char*)buf_log_evcomm,
  13248. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13249. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13250. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13251. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13252. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13253. }
  13254. else
  13255. {
  13256. break;
  13257. }
  13258. }
  13259. //-------------------------------------------
  13260. //case ContractAuthenticationReq:
  13261. case AuthorizationRequest: //29
  13262. {
  13263. if (Proc_iso2_AuthenticationReq(AcceptFd) == 0)
  13264. {
  13265. Update_V2G_Flow_Status(AuthorizationResponse);
  13266. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13267. }
  13268. else
  13269. {
  13270. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  13271. Update_V2G_Flow_Status(Other_Fault);
  13272. }
  13273. req_is_responsed = TRUE;
  13274. break;
  13275. }
  13276. case AuthorizationResponse: //30
  13277. {
  13278. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  13279. {
  13280. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13281. if (Proc_iso2_AuthenticationReq(AcceptFd) == 0)
  13282. {
  13283. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13284. }
  13285. else
  13286. {
  13287. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  13288. Update_V2G_Flow_Status(Other_Fault);
  13289. }
  13290. req_is_responsed = TRUE;
  13291. break;
  13292. }
  13293. //Check for ChargeParameterDiscoveryReq
  13294. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  13295. {
  13296. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13297. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  13298. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  13299. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13300. ftime(&SeqStartTime);
  13301. #endif
  13302. }
  13303. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13304. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13305. {
  13306. sprintf((char*)buf_log_evcomm,
  13307. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13308. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13309. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13310. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13311. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13312. }
  13313. else
  13314. {
  13315. break;
  13316. }
  13317. }
  13318. //-------------------------------------------
  13319. case ChargeParameterDiscoveryRequest: //35
  13320. {
  13321. if (Proc_iso2_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  13322. {
  13323. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  13324. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13325. }
  13326. else
  13327. {
  13328. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  13329. Update_V2G_Flow_Status(Other_Fault);
  13330. }
  13331. req_is_responsed = TRUE;
  13332. break;
  13333. }
  13334. case ChargeParameterDiscoveryResponse:
  13335. {
  13336. //STEP 1: Check for Process Timeout
  13337. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13338. ftime(&SeqEndTime);
  13339. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  13340. {
  13341. sprintf((char*)buf_log_evcomm,
  13342. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  13343. DiffTimeb(SeqStartTime, SeqEndTime),
  13344. V2G_SECC_ChargingParameter_Performance_Time);
  13345. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13346. Update_V2G_Flow_Status(Sequence_Timeout);
  13347. break;
  13348. }
  13349. #endif
  13350. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  13351. {
  13352. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13353. if (Proc_iso2_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  13354. {
  13355. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13356. }
  13357. else
  13358. {
  13359. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  13360. Update_V2G_Flow_Status(Other_Fault);
  13361. }
  13362. req_is_responsed = TRUE;
  13363. break;
  13364. }
  13365. //STEP 2: Check for CableCheckReq message
  13366. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  13367. {
  13368. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13369. Update_V2G_Flow_Status(CableCheckRequest);
  13370. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CableCheckReqReq: isolated?");
  13371. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  13372. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  13373. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13374. ftime(&SeqStartTime);
  13375. #endif
  13376. }
  13377. //STEP 3: Wait for PowerDeliveryReq Message
  13378. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13379. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13380. {
  13381. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  13382. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13383. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  13384. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13385. ftime(&SeqStartTime);
  13386. #endif
  13387. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  13388. {
  13389. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  13390. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13391. }
  13392. else
  13393. {
  13394. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  13395. Update_V2G_Flow_Status(Other_Fault);
  13396. }
  13397. break;
  13398. }
  13399. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13400. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13401. {
  13402. sprintf((char*)buf_log_evcomm,
  13403. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13404. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13405. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13406. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13407. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13408. }
  13409. else
  13410. {
  13411. break;
  13412. }
  13413. }
  13414. //-------------------------------------------
  13415. case CableCheckRequest: //37
  13416. {
  13417. //STEP 3: Execute Cable Check Process
  13418. if (Proc_iso2_CableCheckReq(AcceptFd) == 0)
  13419. {
  13420. Update_V2G_Flow_Status(CableCheckResponse);
  13421. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13422. }
  13423. else
  13424. {
  13425. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  13426. Update_V2G_Flow_Status(Other_Fault);
  13427. }
  13428. req_is_responsed = TRUE;
  13429. break;
  13430. }
  13431. case CableCheckResponse: //38
  13432. {
  13433. //STEP 1: Check for Process Timeout
  13434. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13435. ftime(&SeqEndTime);
  13436. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  13437. {
  13438. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CableCheck] end counting...");
  13439. sprintf((char*)buf_log_evcomm,
  13440. "CableCheck Timeout - (%.02lf of %d ms)\n",
  13441. DiffTimeb(SeqStartTime, SeqEndTime),
  13442. V2G_SECC_CableCheck_Performance_Time);
  13443. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13444. Update_V2G_Flow_Status(Sequence_Timeout);
  13445. //Update_ShmStatusCode(); //[To-Do] to be implemented
  13446. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  13447. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  13448. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  13449. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  13450. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  13451. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  13452. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  13453. break;
  13454. }
  13455. #endif
  13456. //STEP 2: Check for CableCheckReq message
  13457. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  13458. {
  13459. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13460. //STEP 3: Execute Cable Check Process
  13461. if (Proc_iso2_CableCheckReq(AcceptFd) == 0)
  13462. {
  13463. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13464. }
  13465. else
  13466. {
  13467. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  13468. Update_V2G_Flow_Status(Other_Fault);
  13469. }
  13470. req_is_responsed = TRUE;
  13471. break;
  13472. }
  13473. //STEP 3: Check for PreChargeReq message
  13474. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  13475. {
  13476. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13477. Update_V2G_Flow_Status(PreChargeRequest);
  13478. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PreChargeReqReq: waiting for precharge voltage...");
  13479. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  13480. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  13481. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13482. ftime(&SeqStartTime);
  13483. #endif
  13484. }
  13485. //STEP 3: Wait for PowerDeliveryReq Message
  13486. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13487. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13488. {
  13489. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  13490. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13491. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  13492. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13493. ftime(&SeqStartTime);
  13494. #endif
  13495. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  13496. {
  13497. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  13498. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13499. }
  13500. else
  13501. {
  13502. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  13503. Update_V2G_Flow_Status(Other_Fault);
  13504. }
  13505. break;
  13506. }
  13507. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13508. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13509. {
  13510. sprintf((char*)buf_log_evcomm,
  13511. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13512. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13513. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13514. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13515. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13516. }
  13517. else
  13518. {
  13519. break;
  13520. }
  13521. }
  13522. //-------------------------------------------
  13523. case PreChargeRequest: //39
  13524. {
  13525. if (Proc_iso2_PreChargeReq(AcceptFd) == 0)
  13526. {
  13527. Update_V2G_Flow_Status(PreChargeResponse);
  13528. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13529. }
  13530. else
  13531. {
  13532. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  13533. Update_V2G_Flow_Status(Other_Fault);
  13534. }
  13535. req_is_responsed = TRUE;
  13536. break;
  13537. }
  13538. case PreChargeResponse: //40
  13539. {
  13540. //STEP 1: Check for Process Timeout
  13541. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13542. ftime(&SeqEndTime);
  13543. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  13544. {
  13545. sprintf((char*)buf_log_evcomm,
  13546. "Precharge Timeout - (%.02lf of %d ms)\n",
  13547. DiffTimeb(SeqStartTime, SeqEndTime),
  13548. V2G_SECC_PreCharge_Performance_Time);
  13549. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13550. Update_V2G_Flow_Status(Sequence_Timeout);
  13551. //Update_ShmStatusCode(); //[To-Do] to be implemented
  13552. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  13553. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  13554. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  13555. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  13556. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  13557. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  13558. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  13559. break;
  13560. }
  13561. #endif
  13562. //STEP 2: Check for PreChargeReq message
  13563. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  13564. {
  13565. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13566. if (Proc_iso2_PreChargeReq(AcceptFd) == 0)
  13567. {
  13568. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13569. }
  13570. else
  13571. {
  13572. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  13573. Update_V2G_Flow_Status(Other_Fault);
  13574. }
  13575. req_is_responsed = TRUE;
  13576. break;
  13577. }
  13578. //STEP 3: Check for PowerDeliveryReq message
  13579. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13580. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13581. {
  13582. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13583. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  13584. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  13585. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13586. ftime(&SeqStartTime);
  13587. #endif
  13588. }
  13589. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13590. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13591. {
  13592. sprintf((char*)buf_log_evcomm,
  13593. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13594. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13595. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13596. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13597. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13598. }
  13599. else
  13600. {
  13601. break;
  13602. }
  13603. }
  13604. //-------------------------------------------
  13605. case PowerDeliveryRequestStart: //41
  13606. {
  13607. if (Proc_iso2_PowerDeliveryStartReq(AcceptFd) == 0)
  13608. {
  13609. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  13610. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13611. }
  13612. else
  13613. {
  13614. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStartReqRes: fail");
  13615. Update_V2G_Flow_Status(Other_Fault);
  13616. }
  13617. req_is_responsed = TRUE;
  13618. break;
  13619. }
  13620. case PowerDeliveryResponsetStart: //42
  13621. {
  13622. //STEP 1: Check for Process Timeout
  13623. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13624. if (ccs_exi_doc_ISO2.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  13625. {
  13626. ftime(&SeqEndTime);
  13627. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  13628. {
  13629. sprintf((char*)buf_log_evcomm,
  13630. "Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n",
  13631. DiffTimeb(SeqStartTime, SeqEndTime),
  13632. 2000);
  13633. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13634. Update_V2G_Flow_Status(Sequence_Timeout);
  13635. }
  13636. break;
  13637. }
  13638. #endif
  13639. //STEP 2: Wait for CurrentDemandReq Message
  13640. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  13641. {
  13642. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13643. Update_V2G_Flow_Status(CurrentDemandRequest);
  13644. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CurrentDemandReqReq: energy transfering...");
  13645. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  13646. }
  13647. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13648. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13649. {
  13650. sprintf((char*)buf_log_evcomm,
  13651. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13652. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13653. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13654. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13655. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13656. }
  13657. else
  13658. {
  13659. break;
  13660. }
  13661. }
  13662. //-------------------------------------------
  13663. case CurrentDemandRequest: //45,
  13664. {
  13665. if (Proc_iso2_CurrentDemandReq(AcceptFd) == 0)
  13666. {
  13667. Update_V2G_Flow_Status(CurrentDemandResponse);
  13668. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13669. }
  13670. else
  13671. {
  13672. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  13673. Update_V2G_Flow_Status(Other_Fault);
  13674. }
  13675. req_is_responsed = TRUE;
  13676. break;
  13677. }
  13678. case CurrentDemandResponse: //46,
  13679. {
  13680. //STEP 1: Wait for CurrentDemandReq Message
  13681. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  13682. {
  13683. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13684. if (Proc_iso2_CurrentDemandReq(AcceptFd) == 0)
  13685. {
  13686. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13687. }
  13688. else
  13689. {
  13690. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  13691. Update_V2G_Flow_Status(Other_Fault);
  13692. }
  13693. req_is_responsed = TRUE;
  13694. break;
  13695. }
  13696. //STEP 2: Wait for PowerDeliveryReq Message
  13697. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13698. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13699. {
  13700. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  13701. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13702. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  13703. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13704. ftime(&SeqStartTime);
  13705. #endif
  13706. }
  13707. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13708. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13709. {
  13710. sprintf((char*)buf_log_evcomm,
  13711. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13712. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13713. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13714. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13715. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13716. }
  13717. else
  13718. {
  13719. break;
  13720. }
  13721. }
  13722. //-------------------------------------------
  13723. case PowerDeliveryRequestStop: //49,
  13724. {
  13725. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  13726. {
  13727. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  13728. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13729. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13730. ftime(&SeqStartTime);
  13731. #endif
  13732. }
  13733. else
  13734. {
  13735. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  13736. Update_V2G_Flow_Status(Other_Fault);
  13737. }
  13738. req_is_responsed = TRUE;
  13739. break;
  13740. }
  13741. case PowerDeliveryResponseStop: //50,
  13742. {
  13743. //STEP 1: Check for Process Timeout
  13744. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13745. ftime(&SeqEndTime);
  13746. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  13747. {
  13748. sprintf((char*)buf_log_evcomm,
  13749. "Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n",
  13750. DiffTimeb(SeqStartTime, SeqEndTime),
  13751. 2000);
  13752. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13753. Update_V2G_Flow_Status(Sequence_Timeout);
  13754. break;
  13755. }
  13756. #endif
  13757. //STEP 2: Check for WeldingDetectionReq Message
  13758. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  13759. {
  13760. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13761. Update_V2G_Flow_Status(WeldingDetectionRequest);
  13762. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]WeldingDetectionReq: ongoing...");
  13763. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  13764. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13765. ftime(&SeqStartTime);
  13766. #endif
  13767. }
  13768. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  13769. {
  13770. Update_V2G_Flow_Status(SessionStopRequest);
  13771. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13772. if (Proc_iso2_SessionStopReq(AcceptFd) == 0)
  13773. {
  13774. Update_V2G_Flow_Status(SessionStopResponse);
  13775. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13776. }
  13777. else
  13778. {
  13779. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  13780. Update_V2G_Flow_Status(Other_Fault);
  13781. }
  13782. break;
  13783. }
  13784. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13785. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13786. {
  13787. sprintf((char*)buf_log_evcomm,
  13788. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13789. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13790. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13791. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13792. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13793. }
  13794. else
  13795. {
  13796. break;
  13797. }
  13798. }
  13799. //-------------------------------------------
  13800. case WeldingDetectionRequest: //51,
  13801. {
  13802. if (Proc_iso2_WeldingDetectionReq(AcceptFd) == 0)
  13803. {
  13804. Update_V2G_Flow_Status(WeldingDetectionResponse);
  13805. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13806. }
  13807. else
  13808. {
  13809. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  13810. Update_V2G_Flow_Status(Other_Fault);
  13811. }
  13812. req_is_responsed = TRUE;
  13813. break;
  13814. }
  13815. case WeldingDetectionResponse: //52,
  13816. {
  13817. //STEP 1: Check for Process Timeout
  13818. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13819. ftime(&SeqEndTime);
  13820. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  13821. {
  13822. sprintf((char*)buf_log_evcomm,
  13823. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  13824. DiffTimeb(SeqStartTime, SeqEndTime),
  13825. V2G_SECC_WeldingDetection_Performance_Time);
  13826. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13827. Update_V2G_Flow_Status(Sequence_Timeout);
  13828. break;
  13829. }
  13830. #endif
  13831. //STEP 2: Check for WeldingDetectionReq Message
  13832. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  13833. {
  13834. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13835. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  13836. {
  13837. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13838. }
  13839. else
  13840. {
  13841. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  13842. Update_V2G_Flow_Status(Other_Fault);
  13843. }
  13844. req_is_responsed = TRUE;
  13845. break;
  13846. }
  13847. //STEP 3: Check for SessionStopReq Message
  13848. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  13849. {
  13850. Update_V2G_Flow_Status(SessionStopRequest);
  13851. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13852. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  13853. }
  13854. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13855. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13856. {
  13857. sprintf((char*)buf_log_evcomm,
  13858. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13859. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13860. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13861. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13862. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13863. }
  13864. else
  13865. {
  13866. break;
  13867. }
  13868. }
  13869. //-------------------------------------------
  13870. case SessionStopRequest: //53,
  13871. {
  13872. if (Proc_iso2_SessionStopReq(AcceptFd) == 0)
  13873. {
  13874. Update_V2G_Flow_Status(SessionStopResponse);
  13875. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13876. }
  13877. else
  13878. {
  13879. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  13880. Update_V2G_Flow_Status(Other_Fault);
  13881. }
  13882. req_is_responsed = TRUE;
  13883. //break;
  13884. }
  13885. case SessionStopResponse: //54,
  13886. {
  13887. break;
  13888. }
  13889. #endif
  13890. //-------------------------------------------
  13891. default:
  13892. {
  13893. break;
  13894. }
  13895. }
  13896. }
  13897. return 0;
  13898. }
  13899. /*===========================================================================
  13900. FUNCTION: V2gMsg_Process_iso2_AC
  13901. DESCRIPTION:
  13902. PRE-CONDITION:
  13903. INPUT:
  13904. 1. V2gFlowStatus
  13905. OUTPUT:
  13906. GLOBAL VARIABLES:
  13907. 1. V2gFlowStatus
  13908. =============================================================================*/
  13909. int V2gMsg_Process_iso2_AC(int AcceptFd)
  13910. {
  13911. return 0;
  13912. }
  13913. /*===========================================================================
  13914. FUNCTION: V2gMsg_Process_iso2
  13915. DESCRIPTION:
  13916. PRE-CONDITION:
  13917. INPUT:
  13918. 1. V2gFlowStatus
  13919. OUTPUT:
  13920. GLOBAL VARIABLES:
  13921. 1. V2gFlowStatus
  13922. =============================================================================*/
  13923. int V2gMsg_Process_iso2(int AcceptFd, unsigned char EnergyTransferMode)
  13924. {
  13925. switch (EnergyTransferMode)
  13926. {
  13927. case DC_extended:
  13928. {
  13929. V2gMsg_Process_iso2_DC(AcceptFd);
  13930. break;
  13931. }
  13932. case AC_single_phase_core:
  13933. case AC_three_phase_core:
  13934. {
  13935. V2gMsg_Process_iso2_AC(AcceptFd);
  13936. break;
  13937. }
  13938. default:
  13939. {
  13940. sprintf((char*)buf_log_evcomm,
  13941. "[ISO2][Warning]Unexpected EnergyTransferMode(%d)",
  13942. EnergyTransferMode);
  13943. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13944. break;
  13945. }
  13946. }
  13947. return 0;
  13948. }
  13949. /*===========================================================================
  13950. FUNCTION: V2gMsg_Process
  13951. DESCRIPTION:
  13952. PRE-CONDITION:
  13953. INPUT:
  13954. OUTPUT:
  13955. GLOBAL VARIABLES:
  13956. =============================================================================*/
  13957. int V2gMsg_Process(int AcceptFd)
  13958. {
  13959. int errn = 0;
  13960. switch (ShmCcsData->CommProtocol)
  13961. {
  13962. case V2GT_MSG_PROTOCOL_DIN70121: //0
  13963. {
  13964. V2gMsg_Process_din(AcceptFd);
  13965. break;
  13966. }
  13967. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  13968. {
  13969. V2gMsg_Process_iso1(AcceptFd, ShmCcsData->EnergyTransferMode);
  13970. break;
  13971. }
  13972. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  13973. {
  13974. V2gMsg_Process_iso2(AcceptFd, ShmCcsData->EnergyTransferMode);
  13975. break;
  13976. }
  13977. default:
  13978. {
  13979. sprintf((char*)buf_log_evcomm,
  13980. "[Warning]Unexpected CommProtocol(%d)",
  13981. ShmCcsData->CommProtocol);
  13982. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13983. break;
  13984. }
  13985. }
  13986. return errn;
  13987. }
  13988. /*===========================================================================
  13989. FUNCTION: V2gMsg_Rx
  13990. DESCRIPTION:
  13991. PRE-CONDITION:
  13992. INPUT:
  13993. OUTPUT:
  13994. GLOBAL VARIABLES:
  13995. =============================================================================*/
  13996. int V2gMsg_Rx(int AcceptFd)
  13997. {
  13998. int errn = 0;
  13999. unsigned int packet_size = 0;
  14000. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  14001. packet_size = recv(AcceptFd, V2gtpMsgRxBuf, V2GTP_MSG_RX_BUFFER_SIZE, 0);
  14002. //[CAUTION] Everytime recv() is called, it will take a default time to wait for TCP packets.
  14003. //The more time you call recv(), the more time you will waste here.
  14004. //Here it is suggested that response immediatedly once you receive any packets.
  14005. //For configuring the Rx waiting time, please use setsockopt().
  14006. if(packet_size > 0) //[Joseph] If there is no data, ignore. (TBD)
  14007. {
  14008. //DEBUG_PRINTF_EVCOMM_DETAIL("\n\n Got TCP V2GTP Message: size = %d Bytes\n", packet_size);
  14009. errn = V2gMsgDecoder(V2gtpMsgRxBuf, packet_size, V2gFlowStatus);
  14010. if (errn < 0)
  14011. {
  14012. sprintf((char*)buf_log_evcomm, "[ERROR]V2gMsgDecoder (%d)", errn);
  14013. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14014. }
  14015. }
  14016. return errn;
  14017. }
  14018. /*===========================================================================
  14019. FUNCTION: V2gComm
  14020. DESCRIPTION:
  14021. PRE-CONDITION:
  14022. 1. TCP socket is connected.
  14023. INPUT:
  14024. 1. AcceptFd //TCP Connection ID
  14025. 2. V2gtpMsgRxBuf[] //V2GTP Messages (Header + Payload)
  14026. 3. V2GTP_MSG_RX_BUFFER_SIZE //64*1024 = 65,536 = 65.5K (Bytes)
  14027. OUTPUT:
  14028. GLOBAL VARIABLES:
  14029. 1. V2gtpMsgRxBuf[]
  14030. =============================================================================*/
  14031. int V2gComm(int AcceptFd)
  14032. {
  14033. int errn = 0;
  14034. if (V2gMsg_Rx(AcceptFd) < 0)
  14035. {
  14036. Update_V2G_Flow_Status(Other_Fault);
  14037. errn = -1;
  14038. }
  14039. //following are the response message handling according to status flag
  14040. if (V2gMsg_Process(AcceptFd) < 0)
  14041. {
  14042. errn = -1;
  14043. }
  14044. //Error Check
  14045. //V2G_Error_Monitor();
  14046. return errn;
  14047. }
  14048. /*===========================================================================
  14049. FUNCTION: SdpUdpConnected
  14050. DESCRIPTION:
  14051. PRE-CONDITION:
  14052. INPUT:
  14053. OUTPUT:
  14054. GLOBAL VARIABLES:
  14055. =============================================================================*/
  14056. int SdpUdpConnected()
  14057. {
  14058. int packet_size,Rtn;
  14059. struct sockaddr_in6 ServerAddr,ClientAddr;
  14060. struct V2gtpHeader *header;
  14061. unsigned char *payload;
  14062. if(UdpSock <= 0)
  14063. {
  14064. if ((UdpSock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
  14065. {
  14066. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SdpUdpConnected: Fail to open UdpSock");
  14067. return 0;
  14068. }
  14069. memset(&ServerAddr,0, sizeof(struct sockaddr_in));
  14070. ServerAddr.sin6_family = AF_INET6;
  14071. ServerAddr.sin6_addr = in6addr_any;
  14072. ServerAddr.sin6_port = htons(SdpUdpServerPort);
  14073. if(bind(UdpSock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) <0)
  14074. {
  14075. sprintf((char*)buf_log_evcomm,
  14076. "SdpUdpConnected: Fail to bind UdpSock(%d)",
  14077. UdpSock);
  14078. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14079. close(UdpSock);
  14080. UdpSock = -1;
  14081. return 0;
  14082. }
  14083. sprintf((char*)buf_log_evcomm, "[UdpSock]opend:%d", UdpSock);
  14084. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14085. sprintf((char*)buf_log_evcomm, "[SDP]Req from EVCC: waiting...(%d)", UdpSock);
  14086. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14087. }
  14088. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  14089. memset(&ClientAddr, 0, sizeof(struct sockaddr_in));
  14090. Rtn = sizeof(struct sockaddr_in6);
  14091. packet_size = recvfrom(UdpSock, V2gtpMsgRxBuf, V2GTP_MSG_RX_BUFFER_SIZE, MSG_DONTWAIT, (struct sockaddr *)&ClientAddr, (socklen_t *)&Rtn);
  14092. if(packet_size > 0)
  14093. {
  14094. header = (struct V2gtpHeader *) V2gtpMsgRxBuf;
  14095. payload = V2gtpMsgRxBuf+sizeof(struct V2gtpHeader);
  14096. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  14097. {
  14098. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n Got UDP packet_size = %d (Bytes)\n", packet_size);
  14099. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***********************************\n");
  14100. DEBUG_PRINTF_EVCOMM_DETAIL("***** Received SDP Packet *****\n");
  14101. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  14102. DEBUG_PRINTF_EVCOMM_DETAIL("ClientAddress=");
  14103. for(Rtn = 0; Rtn < 16; Rtn += 2)
  14104. {
  14105. DEBUG_PRINTF_EVCOMM_DETAIL("%02x%02x:",ClientAddr.sin6_addr.s6_addr[Rtn],ClientAddr.sin6_addr.s6_addr[Rtn+1]);
  14106. }
  14107. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14108. DEBUG_PRINTF_EVCOMM_DETAIL("ClientPort=%d\n",ClientAddr.sin6_port);
  14109. DEBUG_PRINTF_EVCOMM_DETAIL("ProtocolVersion=%d\n",header->ProtocolVersion);
  14110. DEBUG_PRINTF_EVCOMM_DETAIL("InverseProtocolVersion=0x%x\n",header->InverseProtocolVersion);
  14111. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadType=0x%x\n",htons(header->PayloadType));
  14112. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadLength=0x%x\n",htonl(header->PayloadLength));
  14113. DEBUG_PRINTF_EVCOMM_DETAIL("htons(header->PayloadType)=0x%x\n",htons(header->PayloadType));
  14114. }
  14115. #endif
  14116. if( (header->ProtocolVersion == 0x01) &&
  14117. (header->InverseProtocolVersion == 0xFE) &&
  14118. (htons(header->PayloadType) == V2GTP_PAYLOAD_TYPE_SDP_REQUEST))
  14119. {
  14120. sprintf((char*)buf_log_evcomm, "[SDP][Rx]Req from EVCC: GOT(%d)", UdpSock);
  14121. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14122. DEBUG_PRINTF_EVCOMM_DETAIL("Security=0x%x\n", *(payload + 0));
  14123. DEBUG_PRINTF_EVCOMM_DETAIL("TransportProtocol=0x%x\n", *(payload + 1));
  14124. header->PayloadType = htons(V2GTP_PAYLOAD_TYPE_SDP_RESPONSE);
  14125. header->PayloadLength = htonl(20); //Fixed Length=20
  14126. memset(payload, 0, 20);
  14127. // MAC address[0:2] + FFFE + MAC address[3:5]
  14128. payload[0] = (IPV6_LINK_LOCAL_PREFIX>>8) & 0xFF;
  14129. payload[1] = IPV6_LINK_LOCAL_PREFIX & 0xFF;
  14130. payload[8 ]= CsuMac[0];
  14131. payload[8] ^= 0x02;// bit 1 should complemented.
  14132. payload[9] = CsuMac[1];
  14133. payload[10] = CsuMac[2];
  14134. payload[11] = 0xFF;
  14135. payload[12] = 0xFE;
  14136. payload[13] = CsuMac[3];
  14137. payload[14] = CsuMac[4];
  14138. payload[15] = CsuMac[5];
  14139. //TCP port
  14140. payload[16] = (EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active>>8) & 0xFF;
  14141. payload[17] = EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active & 0xFF;
  14142. payload[18] = SDP_PAYLOAD_SECURITY_NONE; //Security
  14143. payload[19] = SDP_PAYLOAD_TRANS_PROTOCOL_TCP; //Transport protocol
  14144. //[To-Do] Setup the TCP in advance to receive the coming TCP handshke messages after SDP response.
  14145. Rtn = sendto(UdpSock, V2gtpMsgRxBuf, sizeof(struct V2gtpHeader) + htonl(header->PayloadLength), 0, (struct sockaddr *)&ClientAddr, sizeof(struct sockaddr_in6));
  14146. sprintf((char*)buf_log_evcomm, "[SDP][Tx]Res from SECC");
  14147. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14148. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  14149. {
  14150. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response SDP Packet *****\n");
  14151. DEBUG_PRINTF_EVCOMM_DETAIL("Send size=%d\n",Rtn);
  14152. DEBUG_PRINTF_EVCOMM_DETAIL("Destination Address=");
  14153. for(Rtn = 0; Rtn < 16; Rtn++)
  14154. {
  14155. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ", ClientAddr.sin6_addr.s6_addr[Rtn]);
  14156. }
  14157. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14158. DEBUG_PRINTF_EVCOMM_DETAIL("Destination Port=%d\n", ClientAddr.sin6_port);
  14159. DEBUG_PRINTF_EVCOMM_DETAIL("ProtocolVersion=%d\n", header->ProtocolVersion);
  14160. DEBUG_PRINTF_EVCOMM_DETAIL("InverseProtocolVersion=0x%x\n", header->InverseProtocolVersion);
  14161. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadType=0x%x\n", htons(header->PayloadType));
  14162. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadLength=0x%x\n", htonl(header->PayloadLength));
  14163. DEBUG_PRINTF_EVCOMM_DETAIL("SECC Ipv6 Address=");
  14164. for(Rtn = 0; Rtn < 16; Rtn++)
  14165. {
  14166. DEBUG_PRINTF_EVCOMM_DETAIL("%02x:",payload[Rtn]);
  14167. }
  14168. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14169. DEBUG_PRINTF_EVCOMM_DETAIL("SECC Port=%d\n", (payload[16]<<8 | payload[17]));
  14170. DEBUG_PRINTF_EVCOMM_DETAIL("Security=0x%x\n", payload[19]);
  14171. DEBUG_PRINTF_EVCOMM_DETAIL("TransportProtocol=0x%x\n", payload[20]);
  14172. }
  14173. #endif
  14174. if(Rtn > 0)
  14175. {
  14176. return 1;
  14177. }
  14178. }
  14179. }
  14180. return 0;
  14181. }
  14182. /*===========================================================================
  14183. FUNCTION: V2gTcpConnected
  14184. DESCRIPTION:
  14185. PRE-CONDITION:
  14186. INPUT:
  14187. OUTPUT:
  14188. GLOBAL VARIABLES:
  14189. =============================================================================*/
  14190. int V2gTcpConnected()
  14191. {
  14192. int Rtn,AcceptFd;
  14193. struct sockaddr_in6 ServerAddr,ClientAddr;
  14194. if(TcpSock <= 0)
  14195. {
  14196. if ((TcpSock = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
  14197. {
  14198. sprintf((char*)buf_log_evcomm,
  14199. "V2gTcpConnected: Fail to open TcpSock (%s)",
  14200. strerror(errno));
  14201. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14202. usleep(100000); //100ms
  14203. return 0;
  14204. }
  14205. fcntl(TcpSock, F_SETFL, O_NONBLOCK); //set to O_NONBLOCK
  14206. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]socket set: DONE");
  14207. memset(&ServerAddr,0, sizeof(struct sockaddr_in));
  14208. ServerAddr.sin6_family = PF_INET6;
  14209. ServerAddr.sin6_addr = in6addr_any;
  14210. ServerAddr.sin6_port = htons(EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  14211. //ServerAddr.sin6_port = htons(SdpTcpServerPort + (rand() % 10) + 1);
  14212. //printf("SdpTcpServerPort = %d(DEC)\n", htons(ServerAddr.sin6_port));
  14213. if(bind(TcpSock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) < 0)
  14214. {
  14215. sprintf((char*)buf_log_evcomm,
  14216. "V2gTcpConnected: Fail to bind TcpSock (%s), SdpTcpServerPort = %d",
  14217. strerror(errno),
  14218. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active
  14219. );
  14220. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14221. //Change to another TCP port
  14222. /*
  14223. memset(&ServerAddr, 0, sizeof(struct sockaddr_in));
  14224. ServerAddr.sin6_family = PF_INET6;
  14225. ServerAddr.sin6_addr = in6addr_any;
  14226. //ServerAddr.sin6_port = htons(SdpTcpServerPort + (rand() % 1000) + 1);
  14227. ServerAddr.sin6_port = htons(EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  14228. */
  14229. if(bind(TcpSock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) < 0)
  14230. {
  14231. sprintf((char*)buf_log_evcomm,
  14232. "V2gTcpConnected: Fail to bind TcpSock (%s), SdpTcpServerPort = %d",
  14233. strerror(errno),
  14234. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active
  14235. );
  14236. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14237. usleep(100000); //100ms
  14238. close(TcpSock);
  14239. TcpSock = -1;
  14240. return 0;
  14241. }
  14242. }
  14243. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]blid: DONE");
  14244. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]listen: START");
  14245. if(listen(TcpSock, 1) == -1) //only accept one connection
  14246. {
  14247. sprintf((char*)buf_log_evcomm,
  14248. "V2gTcpConnected: Fail to listen TcpSock (%s)",
  14249. strerror(errno));
  14250. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14251. usleep(100000); //100ms
  14252. close(TcpSock);
  14253. TcpSock = -1;
  14254. return 0;
  14255. }
  14256. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]listen: DONE");
  14257. sprintf((char*)buf_log_evcomm, "[TCP]TcpSock: opened(%d), port=%d", TcpSock, htons(ServerAddr.sin6_port));
  14258. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14259. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]accept: START");
  14260. }
  14261. Rtn = sizeof(struct sockaddr_in6);
  14262. if((AcceptFd = accept(TcpSock, (struct sockaddr *)&ClientAddr, (socklen_t *)&Rtn)) == -1)
  14263. {
  14264. static BOOL tmp = 0;
  14265. if (tmp == 0)
  14266. {
  14267. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]Wait TCP connection...");
  14268. tmp = 1;
  14269. }
  14270. else
  14271. {
  14272. //DEBUG_PRINTF_EVCOMM_DETAIL(".");
  14273. }
  14274. return 0;
  14275. }
  14276. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]accept: DONE\n");
  14277. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  14278. {
  14279. DEBUG_PRINTF_EVCOMM_DETAIL("\n\nAccept one TCP connection:\n");
  14280. DEBUG_PRINTF_EVCOMM_DETAIL("AcceptFd=%d\n",AcceptFd);
  14281. DEBUG_PRINTF_EVCOMM_DETAIL("ClientAddress=");
  14282. for(Rtn = 0; Rtn < 16; Rtn += 2)
  14283. {
  14284. DEBUG_PRINTF_EVCOMM_DETAIL("%02x%02x:", ClientAddr.sin6_addr.s6_addr[Rtn], ClientAddr.sin6_addr.s6_addr[Rtn+1]);
  14285. }
  14286. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14287. DEBUG_PRINTF_EVCOMM_DETAIL("ClientPort=%d\n", ClientAddr.sin6_port);
  14288. }
  14289. #endif
  14290. return AcceptFd;
  14291. }
  14292. /*===========================================================================
  14293. FUNCTION: End_Process
  14294. DESCRIPTION:
  14295. PRE-CONDITION:
  14296. 1. <CAUTION> This function could only be used in SeccComm fork2.
  14297. INPUT:
  14298. OUTPUT:
  14299. GLOBAL VARIABLES:
  14300. =============================================================================*/
  14301. int End_Process()
  14302. {
  14303. if(EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  14304. {
  14305. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "End_Process has been triggered by another event.");
  14306. return -1;
  14307. }
  14308. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Entering...");
  14309. //STEP 1: Ask CSU to Stop
  14310. EVCOMM_SYS_INFO.End_Process_inused = TRUE;
  14311. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  14312. EVCOMM_SYS_INFO.ConnectorLocked = FALSE;
  14313. ShmInternalComm->ChargingPermission = FALSE;
  14314. //Step 2: Close sockets
  14315. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Close sockets...");
  14316. if(RawSock > 0)
  14317. {
  14318. close(RawSock);
  14319. }
  14320. if(UdpSock > 0)
  14321. {
  14322. close(UdpSock);
  14323. }
  14324. if(TcpSock > 0)
  14325. {
  14326. close(TcpSock);
  14327. close(TcpAcceptFd);
  14328. }
  14329. RawSock = UdpSock = TcpSock = TcpAcceptFd = -1;
  14330. //STEP 3: Switch to State E
  14331. //SwitchCpStateE(ENABLE);
  14332. //STEP 4: Close tcpdump
  14333. Sniffer_Tcpdump(DISABLE);
  14334. //STEP 5: Keep 100% PWM for 5 seconds
  14335. OutputCpPwmDuty(100);
  14336. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 5");
  14337. sleep(1);
  14338. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 4");
  14339. sleep(1);
  14340. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 3");
  14341. sleep(1);
  14342. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 2");
  14343. sleep(1);
  14344. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 1");
  14345. sleep(1);
  14346. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 0");
  14347. AttenProfileCnt = 0;
  14348. init_appHandEXIDocument(&ccs_handshake);
  14349. // Qca7kPowerReset(); //reset QCA7000 /* +++ 20200808, vern, should disconnected PLC connection after session stop ---*/
  14350. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
  14351. {
  14352. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[End_Process]1st sync...");
  14353. system("sync");
  14354. }
  14355. #endif
  14356. //STEP 4: Switch to State E
  14357. //Keep State E for 5 seconds
  14358. #if 0
  14359. SwitchCpStateE(ENABLE); //Set PWM Duty as 0 and set State as E (0V)
  14360. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 5");
  14361. sleep(1);
  14362. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 4");
  14363. sleep(1);
  14364. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 3");
  14365. sleep(1);
  14366. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 2");
  14367. sleep(1);
  14368. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 1");
  14369. sleep(1);
  14370. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 0");
  14371. #endif
  14372. //Reset Memory
  14373. unsigned char SlaveAddress_backup;
  14374. unsigned int matched_backup;
  14375. //unsigned char state_backup;
  14376. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Soft reset (closing SeccComm)...");
  14377. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "---------------------------------------------");
  14378. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "-- EVCOMM: END --");
  14379. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "---------------------------------------------\n");
  14380. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
  14381. {
  14382. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "2nd sync...");
  14383. system("sync");
  14384. }
  14385. #endif
  14386. system("sleep 1");
  14387. //Backup CsuComm flags
  14388. SlaveAddress_backup = ShmInternalComm->SlaveAddress;
  14389. matched_backup = CSUCOMMDC_TASK_FLAG.matched;
  14390. //state_backup = Check_V2G_Flow_Status();
  14391. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  14392. memset(V2gtpMsgTxBuf, 0, V2GTP_MSG_TX_BUFFER_SIZE);
  14393. memset(&ccs_handshake, 0, sizeof(struct appHandEXIDocument));
  14394. memset(&ccs_exi_doc_DIN, 0, sizeof(struct dinEXIDocument));
  14395. memset(V2GTP_Tx_buf, 0, V2GTP_MSG_TX_BUFFER_SIZE);
  14396. memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  14397. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  14398. memset(ShmInternalComm, 0, sizeof(struct InternalComm));
  14399. memset(ShmCcsData, 0, sizeof(struct CcsData));
  14400. //Resume CsuComm flags
  14401. ShmInternalComm->SlaveAddress = SlaveAddress_backup;
  14402. CSUCOMMDC_TASK_FLAG.matched = matched_backup;
  14403. Sniffer_Candump(DISABLE);
  14404. Sniffer_Candump(ENABLE);
  14405. //memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo)); //EVCOMM_SYS_INFO.End_Process_inused = FALSE;
  14406. //EVCOMM_SYS_INFO.End_Process_inused = TRUE;
  14407. //Update_V2G_Flow_Status(state_backup);
  14408. //CP_Detection_Pid = 0;
  14409. //PP_Detection_Pid = 0;
  14410. //Error_Monitor_Pid = 0;
  14411. //system("cd /root;./reset_soft.sh");
  14412. system("pkill Module_CCS");
  14413. while(1)
  14414. {
  14415. //wait for CSU configrm
  14416. }
  14417. }
  14418. /*===========================================================================
  14419. FUNCTION: Parameters_Init
  14420. DESCRIPTION:
  14421. PRE-CONDITION:
  14422. INPUT:
  14423. OUTPUT:
  14424. GLOBAL VARIABLES:
  14425. =============================================================================*/
  14426. int Parameters_Init()
  14427. {
  14428. //Step 0: Generate random number
  14429. unsigned int value_random;
  14430. struct timeb time_seed;
  14431. ftime(&time_seed);
  14432. srand(time_seed.millitm);
  14433. //Step 1: Init SDP TCP Port
  14434. value_random = rand();
  14435. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active = (unsigned short)(SdpTcpServerPort + (value_random % 10000) + 1);
  14436. sprintf((char*)buf_log_evcomm,
  14437. "[Init]TCP Port:OK(%d)",
  14438. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  14439. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14440. //Step 2: Init SessionID
  14441. value_random = rand();
  14442. memcpy(&EVCOMM_SYS_INFO.SessionID[0], &value_random, 4);
  14443. value_random = rand();
  14444. memcpy(&EVCOMM_SYS_INFO.SessionID[4], &value_random, 4);
  14445. sprintf((char*)buf_log_evcomm,
  14446. "[Init]SessionID:OK(%02X%02X%02X%02X%02X%02X%02X%02X)",
  14447. EVCOMM_SYS_INFO.SessionID[0],
  14448. EVCOMM_SYS_INFO.SessionID[1],
  14449. EVCOMM_SYS_INFO.SessionID[2],
  14450. EVCOMM_SYS_INFO.SessionID[3],
  14451. EVCOMM_SYS_INFO.SessionID[4],
  14452. EVCOMM_SYS_INFO.SessionID[5],
  14453. EVCOMM_SYS_INFO.SessionID[6],
  14454. EVCOMM_SYS_INFO.SessionID[7]);
  14455. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14456. return 0;
  14457. }
  14458. /*===========================================================================
  14459. FUNCTION: AC share memory synchronize
  14460. DESCRIPTION:
  14461. PRE-CONDITION:
  14462. INPUT:
  14463. OUTPUT:
  14464. GLOBAL VARIABLES:
  14465. =============================================================================*/
  14466. int SyncAcShreaMemory()
  14467. {
  14468. #if (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE)
  14469. pid_t pid;
  14470. pid = fork();
  14471. if(pid == 0)
  14472. {
  14473. for(;;)
  14474. {
  14475. if(!EVCOMM_SYS_INFO.End_Process_inused)
  14476. {
  14477. for(uint8_t gun_index=0;gun_index<1;gun_index++)
  14478. {
  14479. //========================================
  14480. // CSU -> CCS setting
  14481. //========================================
  14482. // Permission
  14483. if(ShmInternalComm->ChargingPermission != ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission)
  14484. {
  14485. DEBUG_INFO("AC permission: %d -> %d\n", ShmInternalComm->ChargingPermission, ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission);
  14486. ShmInternalComm->ChargingPermission_new = ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission;
  14487. ShmInternalComm->ChargingPermission_pre = ShmInternalComm->ChargingPermission;
  14488. ShmInternalComm->ChargingPermission = ShmInternalComm->ChargingPermission_new;
  14489. }
  14490. // CP
  14491. ShmInternalComm->AC_CpPositiveVoltage = ShmCharger->gun_info[gun_index].acCcsInfo.CpPositiveVoltage;
  14492. if(ShmInternalComm->AC_CpPresentState != ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState)
  14493. {
  14494. DEBUG_INFO("CP state: %d -> %d\n", ShmInternalComm->AC_CpPresentState, ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState);
  14495. ShmInternalComm->AC_CpPresentState = ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState;
  14496. }
  14497. //========================================
  14498. // CCS -> CSU check
  14499. //========================================
  14500. // V2G message flow status
  14501. if(ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus != Check_V2G_Flow_Status())
  14502. {
  14503. //DEBUG_INFO("PresentMsgFlowStatus: %d -> %d\n", ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus, Check_V2G_Flow_Status());
  14504. ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus = Check_V2G_Flow_Status();
  14505. switch(ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus)
  14506. {
  14507. case PowerDeliveryRequestStart ... ChargingStatusResponse:
  14508. ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress = HLC_START_MODE;
  14509. break;
  14510. case CM_SLAC_PARM_REQ ... PreChargeResponse:
  14511. ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress = HLC_STANDBY_MODE;
  14512. break;
  14513. default:
  14514. ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress = HLC_STOP_MODE;
  14515. break;
  14516. }
  14517. }
  14518. }
  14519. }
  14520. usleep(1000);
  14521. }
  14522. }
  14523. #endif
  14524. return 0;
  14525. }
  14526. /*===========================================================================
  14527. FUNCTION: main
  14528. DESCRIPTION:
  14529. PRE-CONDITION:
  14530. INPUT:
  14531. OUTPUT:
  14532. GLOBAL VARIABLES:
  14533. =============================================================================*/
  14534. int main(int argc, char *argv[])
  14535. {
  14536. //unsigned char Rtn;
  14537. //Initialization
  14538. ShareMemory_Init();
  14539. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  14540. if(RawSock > 0)
  14541. {
  14542. close(RawSock);
  14543. }
  14544. if(UdpSock > 0)
  14545. {
  14546. close(UdpSock);
  14547. }
  14548. if(TcpSock > 0)
  14549. {
  14550. close(TcpSock);
  14551. }
  14552. RawSock = UdpSock = TcpSock = -1;
  14553. Update_V2G_Flow_Status(IDLE);
  14554. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "---------------------------------------------");
  14555. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "-- EVCOMM: START --");
  14556. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "---------------------------------------------");
  14557. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "init...");
  14558. //Init V2G TCP/IPv6 packets buffer
  14559. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  14560. memset(V2gtpMsgTxBuf, 0, V2GTP_MSG_TX_BUFFER_SIZE);
  14561. //Release State E Control
  14562. SwitchCpStateE(DISABLE);
  14563. OutputCpPwmDuty(100);
  14564. //start to detect CP pilot state
  14565. CP_Detection_Pid = 0;
  14566. CP_Detection(); //fork1
  14567. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork1]CP Detection: ON");
  14568. //start to detect errors
  14569. Error_Monitor(); //fork2
  14570. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]Error Monitor: ON");
  14571. //start to detect PP
  14572. PP_Detection_Pid = 0;
  14573. #if (PP_PROTECTION_MECHANISM == ENABLE)
  14574. PP_Detection();
  14575. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork3]PP Detection: ON");
  14576. #else
  14577. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork3]PP Detection: OFF");
  14578. #endif
  14579. //Init communication parameters
  14580. GetEthMac((uint8_t*)QcaInterface, CsuMac);
  14581. AttenProfileCnt = 0;
  14582. init_appHandEXIDocument(&ccs_handshake);
  14583. //Init Energy transfer mode
  14584. //[To-Do] Parsing Model Name
  14585. ShmCcsData->EnergyTransferMode = CCS_ENERGY_TRANSFER_MODE;
  14586. //struct ChargingInfoData *ccs;
  14587. //ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  14588. Parameters_Init();
  14589. Sniffer_Tcpdump(ENABLE);
  14590. #if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  14591. sleep(1); //wait for tcpdump getting ready
  14592. #endif
  14593. SyncAcShreaMemory();
  14594. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Init]DONE");
  14595. while(1)
  14596. {
  14597. //DEBUG_PRINTF_EVCOMM_DETAIL("\nV2gFlowStatus = %d\n", V2gFlowStatus);
  14598. //if((ShmInternalComm->ChargingPermission == 0x01) && (CheckConnectorPlugIn() == 1))
  14599. //if((CheckConnectorPlugIn() == TRUE) && (ShmInternalComm->ChargingPermission == TRUE))
  14600. //if(ShmInternalComm->ChargingPermission == 0x01)
  14601. //if(CheckConnectorPlugIn() == TRUE)
  14602. if (EVCOMM_SYS_INFO.End_Process_inused == FALSE)
  14603. {
  14604. if(V2gFlowStatus < SLACC_SDP_UDP_Connection)
  14605. {
  14606. SlacComm();
  14607. }
  14608. else if(V2gFlowStatus == SLACC_SDP_UDP_Connection)
  14609. {
  14610. if(SdpUdpConnected() == 1)
  14611. {
  14612. Update_V2G_Flow_Status(SLACC_SDP_TCP_Connection);
  14613. continue;
  14614. }
  14615. SlacComm(); //TC_SECC_VTB_CmSlacMatch_004
  14616. ftime(&SeqEndTime);
  14617. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_join)
  14618. {
  14619. sprintf((char*)buf_log_evcomm,
  14620. "Wait SLACC_SDP_UDP_Connection Timeout - TT_match_join (%.02lf of %d ms)\n",
  14621. DiffTimeb(SeqStartTime, SeqEndTime),
  14622. TT_match_join);
  14623. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14624. Update_V2G_Flow_Status(Sequence_Timeout);
  14625. //Update_ShmStatusCode(); //[To-Do] to be implemented
  14626. //CCS_SECC_TIMEOUT_SLACC_SDP_UDP_TT_match_join (023823)
  14627. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  14628. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  14629. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  14630. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  14631. ShmStatusCodeData->PresentStatusCode[0][4] = 2;
  14632. ShmStatusCodeData->PresentStatusCode[0][5] = 3;
  14633. //CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  14634. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  14635. }
  14636. }
  14637. else if(V2gFlowStatus == SLACC_SDP_TCP_Connection)
  14638. {
  14639. if((TcpAcceptFd = V2gTcpConnected()) > 0)
  14640. {
  14641. Update_V2G_Flow_Status(SupportedAppProtocolRequest);
  14642. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  14643. continue;
  14644. }
  14645. SlacComm();
  14646. ftime(&SeqEndTime);
  14647. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_join)
  14648. {
  14649. sprintf((char*)buf_log_evcomm,
  14650. "Wait SLACC_SDP_TCP_Connection Timeout - TT_match_join (%.02lf / %d ms)",
  14651. DiffTimeb(SeqStartTime, SeqEndTime),
  14652. TT_match_join);
  14653. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14654. Update_V2G_Flow_Status(Sequence_Timeout);
  14655. //Update_ShmStatusCode(); //[To-Do] to be implemented
  14656. //CCS_SECC_TIMEOUT_SLACC_SDP_TCP_TT_match_join (023824)
  14657. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  14658. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  14659. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  14660. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  14661. ShmStatusCodeData->PresentStatusCode[0][4] = 2;
  14662. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  14663. //CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  14664. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  14665. }
  14666. }
  14667. else if(V2gFlowStatus <= SessionStopResponse)
  14668. {
  14669. if (V2gComm(TcpAcceptFd) < 0)
  14670. {
  14671. //error occours
  14672. }
  14673. }
  14674. else if (V2gFlowStatus >= Performance_Timeout)
  14675. {
  14676. //End_Process
  14677. }
  14678. else
  14679. {
  14680. //null
  14681. }
  14682. }
  14683. usleep(1000);
  14684. } //while
  14685. }//main while