Module_CCS.c 658 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395
  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, *V2gtpMsgTxBuf;
  31. unsigned short Aag[64];
  32. struct MmeHeader SendMmePacket;
  33. int SendMmePacketSize;
  34. unsigned char CsuMac[6],QcaMac[6],EvMac[6],SlacRunId[8];
  35. struct sockaddr_ll DestSocketAddress;
  36. struct ifreq Req;
  37. unsigned int PwmStartTime;
  38. struct timeb SeqStartTime, SeqEndTime;
  39. unsigned char AagGroupsNum, MnbcSoundNum, AttenProfileCnt;
  40. unsigned char NewNmkKey[16], Nid[7];
  41. uint8_t get_V2G_Status_Name(uint8_t statusCode, char *statusName)
  42. {
  43. int result = PASS;
  44. switch(statusCode)
  45. {
  46. case IDLE:
  47. sprintf(statusName, "IDLE");
  48. break;
  49. case CM_SLAC_PARM_REQ:
  50. sprintf(statusName, "CM_SLAC_PARM_REQ");
  51. break;
  52. case CM_SLAC_PARM_CONF:
  53. sprintf(statusName, "CM_SLAC_PARM_CONF");
  54. break;
  55. case CM_START_ATTEN_CHAR_IND:
  56. sprintf(statusName, "CM_START_ATTEN_CHAR_IND");
  57. break;
  58. case CM_MNBC_SOUND_IND:
  59. sprintf(statusName, "CM_MNBC_SOUND_IND");
  60. break;
  61. case CM_ATTEN_CHAR_IND:
  62. sprintf(statusName, "CM_ATTEN_CHAR_IND");
  63. break;
  64. case CM_ATTEN_CHAR_RSP:
  65. sprintf(statusName, "CM_ATTEN_CHAR_RSP");
  66. break;
  67. case CM_VALIDATE_REQ:
  68. sprintf(statusName, "CM_VALIDATE_REQ");
  69. break;
  70. case CM_VALIDATE_CNF:
  71. sprintf(statusName, "CM_VALIDATE_CNF");
  72. break;
  73. case CM_SLAC_MATCH_REQ:
  74. sprintf(statusName, "CM_SLAC_MATCH_REQ");
  75. break;
  76. case CM_SLAC_MATCH_CNF:
  77. sprintf(statusName, "CM_SLAC_MATCH_CNF");
  78. break;
  79. case CM_AMP_MAP_REQ:
  80. sprintf(statusName, "CM_AMP_MAP_REQ");
  81. break;
  82. case CM_AMP_MAP_CNF:
  83. sprintf(statusName, "CM_AMP_MAP_CNF");
  84. break;
  85. case CM_SET_KEY_REQ:
  86. sprintf(statusName, "CM_SET_KEY_REQ");
  87. break;
  88. case CM_SET_KEY_CNF:
  89. sprintf(statusName, "CM_SET_KEY_CNF");
  90. break;
  91. case SLACC_SDP_UDP_Connection:
  92. sprintf(statusName, "SLACC_SDP_UDP_Connection");
  93. break;
  94. case SLACC_SDP_TCP_Connection:
  95. sprintf(statusName, "SLACC_SDP_TCP_Connection");
  96. break;
  97. case SupportedAppProtocolRequest:
  98. sprintf(statusName, "SupportedAppProtocolRequest");
  99. break;
  100. case SupportedAppProtocolResponse:
  101. sprintf(statusName, "SupportedAppProtocolResponse");
  102. break;
  103. case SessionSetupRequest:
  104. sprintf(statusName, "SessionSetupRequest");
  105. break;
  106. case SessionSetupResponse:
  107. sprintf(statusName, "UnSessionSetupResponseknown");
  108. break;
  109. case ServiceDiscoveryRequest:
  110. sprintf(statusName, "ServiceDiscoveryRequest");
  111. break;
  112. case ServiceDiscoveryResponse:
  113. sprintf(statusName, "ServiceDiscoveryResponse");
  114. break;
  115. case ServiceDetailRequest:
  116. sprintf(statusName, "ServiceDetailRequest");
  117. break;
  118. case ServiceDetailResponse:
  119. sprintf(statusName, "ServiceDetailResponse");
  120. break;
  121. case ServiceAndPaymentSelectionRequest:
  122. sprintf(statusName, "ServiceAndPaymentSelectionRequest");
  123. break;
  124. case ServiceAndPaymentSelectionResponse:
  125. sprintf(statusName, "ServiceAndPaymentSelectionResponse");
  126. break;
  127. case PaymentDetailsRequest:
  128. sprintf(statusName, "PaymentDetailsRequest");
  129. break;
  130. case PaymentDetailsResponse:
  131. sprintf(statusName, "PaymentDetailsResponse");
  132. break;
  133. case AuthorizationRequest:
  134. sprintf(statusName, "AuthorizationRequest");
  135. break;
  136. case AuthorizationResponse:
  137. sprintf(statusName, "AuthorizationResponse");
  138. break;
  139. case CertificateUpdateRequest:
  140. sprintf(statusName, "CertificateUpdateRequest");
  141. break;
  142. case CertificateUpdateResponse:
  143. sprintf(statusName, "CertificateUpdateResponse");
  144. break;
  145. case CertificateInstallationRequest:
  146. sprintf(statusName, "CertificateInstallationRequest");
  147. break;
  148. case CertificateInstallationResponse:
  149. sprintf(statusName, "CertificateInstallationResponse");
  150. break;
  151. case ChargeParameterDiscoveryRequest:
  152. sprintf(statusName, "ChargeParameterDiscoveryRequest");
  153. break;
  154. case ChargeParameterDiscoveryResponse:
  155. sprintf(statusName, "ChargeParameterDiscoveryResponse");
  156. break;
  157. case CableCheckRequest:
  158. sprintf(statusName, "CableCheckRequest");
  159. break;
  160. case CableCheckResponse:
  161. sprintf(statusName, "CableCheckResponse");
  162. break;
  163. case PreChargeRequest:
  164. sprintf(statusName, "PreChargeRequest");
  165. break;
  166. case PreChargeResponse:
  167. sprintf(statusName, "PreChargeResponse");
  168. break;
  169. case PowerDeliveryRequestStart:
  170. sprintf(statusName, "PowerDeliveryRequestStart");
  171. break;
  172. case PowerDeliveryResponsetStart:
  173. sprintf(statusName, "PowerDeliveryResponsetStart");
  174. break;
  175. case ChargingStatusRequest:
  176. sprintf(statusName, "ChargingStatusRequest");
  177. break;
  178. case ChargingStatusResponse:
  179. sprintf(statusName, "ChargingStatusResponse");
  180. break;
  181. case CurrentDemandRequest:
  182. sprintf(statusName, "CurrentDemandRequest");
  183. break;
  184. case CurrentDemandResponse:
  185. sprintf(statusName, "CurrentDemandResponse");
  186. break;
  187. case MeteringReceiptRequest:
  188. sprintf(statusName, "MeteringReceiptRequest");
  189. break;
  190. case MeteringReceiptResponse:
  191. sprintf(statusName, "MeteringReceiptResponse");
  192. break;
  193. case PowerDeliveryRequestStop:
  194. sprintf(statusName, "PowerDeliveryRequestStop");
  195. break;
  196. case PowerDeliveryResponseStop:
  197. sprintf(statusName, "PowerDeliveryResponseStop");
  198. break;
  199. case WeldingDetectionRequest:
  200. sprintf(statusName, "WeldingDetectionRequest");
  201. break;
  202. case WeldingDetectionResponse:
  203. sprintf(statusName, "WeldingDetectionResponse");
  204. break;
  205. case SessionStopRequest:
  206. sprintf(statusName, "SessionStopRequest");
  207. break;
  208. case SessionStopResponse:
  209. sprintf(statusName, "SessionStopResponse");
  210. break;
  211. case Performance_Timeout:
  212. sprintf(statusName, "Performance_Timeout");
  213. break;
  214. case Sequence_Timeout:
  215. sprintf(statusName, "Sequence_Timeout");
  216. break;
  217. case Other_Fault:
  218. sprintf(statusName, "Other_Fault");
  219. break;
  220. default:
  221. sprintf(statusName, "Unknown");
  222. result = FAIL;
  223. break;
  224. }
  225. return result;
  226. }
  227. /*===========================================================================
  228. FUNCTION: Check_V2G_Flow_Status
  229. DESCRIPTION:
  230. PRE-CONDITION:
  231. INPUT:
  232. OUTPUT:
  233. GLOBAL VARIABLES:
  234. =============================================================================*/
  235. unsigned char Check_V2G_Flow_Status()
  236. {
  237. unsigned char result = 0;
  238. switch (ShmCcsData->CommProtocol)
  239. {
  240. case V2GT_MSG_PROTOCOL_DIN70121: //0
  241. {
  242. result = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus;
  243. break;
  244. }
  245. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  246. {
  247. result = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus;
  248. break;
  249. }
  250. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  251. {
  252. result = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus;
  253. break;
  254. }
  255. default:
  256. break;
  257. }
  258. return result;
  259. }
  260. /*===========================================================================
  261. FUNCTION: CAN_Tx_MSG
  262. DESCRIPTION:
  263. PRE-CONDITION:
  264. INPUT:
  265. OUTPUT:
  266. GLOBAL VARIABLES:
  267. =============================================================================*/
  268. int CAN_Tx_MSG(int Fd, unsigned int MsgId, unsigned char SlaveAddress, unsigned char DataLength, unsigned char *SendData)
  269. {
  270. #if 0
  271. struct can_frame frame;
  272. struct timeb StartTime, EndTime;
  273. unsigned int tmp = 0;
  274. int nbytes = 0;
  275. int i = 0;
  276. //Protection: To avoid unexpected length for CAN bus payload.
  277. if (DataLength > 8)
  278. {
  279. DataLength = 8;
  280. }
  281. memset(&frame, 0, sizeof(struct can_frame));
  282. frame.can_id = 0x80000000 | CAN_SEND_DIRECTION | MsgId | SlaveAddress; //0x80000000: extension ID format
  283. frame.can_dlc = DataLength;
  284. memcpy(frame.data, SendData, DataLength);
  285. nbytes = write(Fd, &frame, sizeof(struct can_frame));
  286. #if 0
  287. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][CAN_Tx_MSG] <%X> ", frame.can_id);
  288. for (i = 0; i < frame.can_dlc; i++)
  289. {
  290. DEBUG_PRINTF_EVCOMM_DETAIL("%02X ", frame.data[i]);
  291. }
  292. DEBUG_PRINTF_EVCOMM_DETAIL("(%d Bytes)\n", frame.can_dlc);
  293. #endif
  294. return nbytes;
  295. #else
  296. return -1;
  297. #endif
  298. }
  299. /*===========================================================================
  300. FUNCTION: Proc_EVStopRes
  301. DESCRIPTION:
  302. PRE-CONDITION:
  303. INPUT:
  304. OUTPUT:
  305. GLOBAL VARIABLES:
  306. =============================================================================*/
  307. int Proc_EVStopRes(int Fd)
  308. {
  309. int nbytes;
  310. unsigned char Buffer[8];
  311. memset(Buffer, 0, sizeof(Buffer));
  312. if (CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency == TRUE)
  313. {
  314. Buffer[0] = EV_EMERGENCY_STOP; //2
  315. }
  316. else
  317. {
  318. Buffer[0] = EV_NORMAL_STOP; //1
  319. }
  320. /*
  321. if(strlen(ShmStatusCodeData->PresentStatusCode[0]) > 0)
  322. {
  323. memcpy(Buffer + 2, ShmStatusCodeData->PresentStatusCode[0], 6);
  324. }
  325. */
  326. Buffer[1] = ShmStatusCodeData->PresentStatusCode[0][0];
  327. Buffer[2] = ShmStatusCodeData->PresentStatusCode[0][1];
  328. Buffer[3] = ShmStatusCodeData->PresentStatusCode[0][2];
  329. Buffer[4] = ShmStatusCodeData->PresentStatusCode[0][3];
  330. Buffer[5] = ShmStatusCodeData->PresentStatusCode[0][4];
  331. Buffer[6] = ShmStatusCodeData->PresentStatusCode[0][5];
  332. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_EV_STOP_EVENT, ShmInternalComm->SlaveAddress, 7, Buffer);
  333. //system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"); //for test only
  334. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  335. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_EVStopRes] Sending STOP Command to CSU");
  336. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = FALSE;
  337. return nbytes;
  338. }
  339. /*===========================================================================
  340. FUNCTION: Sniffer_Candump
  341. DESCRIPTION:
  342. 1. Only used in End_Process(), which is in fork2 of SeccComm task.
  343. PRE-CONDITION:
  344. INPUT:
  345. OUTPUT:
  346. 0: accept
  347. -1: invalid
  348. GLOBAL VARIABLES:
  349. =============================================================================*/
  350. int Sniffer_Candump(char cmd)
  351. {
  352. #if (CANDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  353. if (cmd == ENABLE)
  354. {
  355. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[candump]init");
  356. system("cd /mnt/;rm -rf candump/");
  357. system("cd /mnt/;mkdir candump");
  358. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[candump]on");
  359. system("cd /mnt/candump;candump -l can0 &");
  360. return 0;
  361. }
  362. else if (cmd == DISABLE)
  363. {
  364. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[candump]off");
  365. system("killall candump");
  366. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[candump]save");
  367. system("cd /;cp -rfv /mnt/candump /Storage/SystemLog/");
  368. return 0;
  369. }
  370. else
  371. {
  372. sprintf((char*)buf_log_evcomm_fork2, "[candump]unexpected cmd(%d)", cmd);
  373. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  374. return -1;
  375. }
  376. #endif
  377. return 0;
  378. }
  379. /*===========================================================================
  380. FUNCTION: Sniffer_Tcpdump
  381. DESCRIPTION:
  382. PRE-CONDITION:
  383. INPUT:
  384. OUTPUT:
  385. GLOBAL VARIABLES:
  386. =============================================================================*/
  387. int Sniffer_Tcpdump(char cmd)
  388. {
  389. #if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  390. if (cmd == ENABLE)
  391. {
  392. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]init...");
  393. system("cd /mnt/;rm -rf tcpdump/");
  394. system("cd /mnt/;mkdir tcpdump");
  395. unsigned char buf[256];
  396. time_t CurrentTime;
  397. struct tm *tm;
  398. struct timeval tv;
  399. memset(buf, 0, sizeof(buf));
  400. CurrentTime = time(NULL);
  401. tm = localtime(&CurrentTime);
  402. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  403. sprintf((char*)buf,
  404. "tcpdump -i eth1 -w /mnt/tcpdump/[%s][%04d%02d%02d-%02d:%02d:%02d.%06d]packets.pcap&",
  405. FIRMWARE_VERSION,
  406. tm->tm_year + 1900,
  407. tm->tm_mon + 1,
  408. tm->tm_mday,
  409. tm->tm_hour,
  410. tm->tm_min,
  411. tm->tm_sec,
  412. tv.tv_usec
  413. );
  414. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]on");
  415. system(buf);
  416. return 0;
  417. }
  418. else if (cmd == DISABLE)
  419. {
  420. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]off");
  421. system("killall tcpdump");
  422. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]wait: 2s");
  423. sleep(1);
  424. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]wait: 1s");
  425. sleep(1);
  426. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[tcpdump]save");
  427. system("cd /;cp -rfv /mnt/tcpdump /Storage/SystemLog/");
  428. return 0;
  429. }
  430. else
  431. {
  432. sprintf((char*)buf_log_evcomm, "[tcpdump]unexpected cmd(%d)", cmd);
  433. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  434. return -1;
  435. }
  436. #endif
  437. return 0;
  438. }
  439. /*===========================================================================
  440. FUNCTION: Check_V2G_Flow_Status_pre
  441. DESCRIPTION:
  442. PRE-CONDITION:
  443. INPUT:
  444. OUTPUT:
  445. GLOBAL VARIABLES:
  446. =============================================================================*/
  447. unsigned char Check_V2G_Flow_Status_pre()
  448. {
  449. unsigned char result = 0;
  450. switch (ShmCcsData->CommProtocol)
  451. {
  452. case V2GT_MSG_PROTOCOL_DIN70121: //0
  453. {
  454. result = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus_pre;
  455. break;
  456. }
  457. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  458. {
  459. result = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus_pre;
  460. break;
  461. }
  462. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  463. {
  464. result = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus_pre;
  465. break;
  466. }
  467. default:
  468. break;
  469. }
  470. return result;
  471. }
  472. /*===========================================================================
  473. FUNCTION: Update_V2G_Flow_Status
  474. DESCRIPTION:
  475. PRE-CONDITION:
  476. INPUT:
  477. OUTPUT:
  478. 1. V2gFlowStatus
  479. GLOBAL VARIABLES:
  480. 1. V2gFlowStatus
  481. =============================================================================*/
  482. int Update_V2G_Flow_Status(unsigned int state_new)
  483. {
  484. unsigned char state_now;
  485. char nameNow[64], nameNew[64];
  486. state_now = Check_V2G_Flow_Status();
  487. get_V2G_Status_Name(state_now, nameNow);
  488. get_V2G_Status_Name(state_new, nameNew);
  489. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE ||
  490. CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused == TRUE)
  491. {
  492. if (state_now != Performance_Timeout && //253
  493. state_now != Sequence_Timeout && //254
  494. state_now != Other_Fault) //255
  495. {
  496. sprintf((char*)buf_log_evcomm,
  497. "[Warning]state-%s(%d) change: ignored (End_Process_inused:%d, FW_Update_Task_inused:%d)",
  498. nameNew, state_new,
  499. EVCOMM_SYS_INFO.End_Process_inused,
  500. CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused
  501. );
  502. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  503. return -1;
  504. }
  505. }
  506. if (state_now != state_new)
  507. {
  508. //Step 1: Check if Error Occours
  509. if (state_now == Performance_Timeout || //253
  510. state_now == Sequence_Timeout || //254
  511. state_now == Other_Fault) //255
  512. {
  513. if (state_new != IDLE)
  514. {
  515. if (EVCOMM_SYS_INFO.State_Change_Ignored_Notice == FALSE)
  516. {
  517. EVCOMM_SYS_INFO.State_Change_Ignored_Notice = TRUE;
  518. sprintf((char*)buf_log_evcomm,
  519. "[Warning]state-%s(%d) change: ignored(now in error state-%s(%d))",
  520. nameNow, state_new,
  521. nameNew, state_now);
  522. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  523. }
  524. return -1;
  525. }
  526. }
  527. //Updating the state formally.
  528. sprintf((char*)buf_log_evcomm,
  529. "%s(%02d) >> %s(%02d)",
  530. nameNow, state_now,
  531. nameNew, state_new
  532. );
  533. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  534. V2gFlowStatus = state_new;
  535. state_now = state_new;
  536. switch (ShmCcsData->CommProtocol)
  537. {
  538. case V2GT_MSG_PROTOCOL_DIN70121: //0
  539. {
  540. ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus;
  541. ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus = V2gFlowStatus;
  542. break;
  543. }
  544. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  545. {
  546. ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus;
  547. ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus = V2gFlowStatus;
  548. break;
  549. }
  550. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  551. {
  552. ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus;
  553. ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus = V2gFlowStatus;
  554. break;
  555. }
  556. default:
  557. break;
  558. }
  559. }
  560. return 0;
  561. }
  562. double DiffTimeb(struct timeb ST, struct timeb ET)
  563. {
  564. //return milli-second
  565. double StartTime, EndTime;
  566. double t_diff;
  567. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  568. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  569. t_diff = EndTime - StartTime;
  570. if (t_diff < 0)
  571. {
  572. #if 0
  573. if (t_diff < -1000) //1000ms
  574. {
  575. sprintf((char*)buf_log_evcomm,
  576. "[Warning]StartTime(%.02lf) > EndTime(%.02lf), d(%.02lf)",
  577. StartTime,
  578. EndTime,
  579. t_diff);
  580. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  581. }
  582. #endif
  583. return -1;
  584. }
  585. return t_diff;
  586. }
  587. double DiffTimeb_fork1_Error_Monitor(struct timeb ST, struct timeb ET)
  588. {
  589. //return milli-second
  590. static double StartTime, EndTime;
  591. static double t_diff;
  592. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  593. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  594. t_diff = EndTime - StartTime;
  595. if (t_diff < 0)
  596. {
  597. #if 0
  598. if (t_diff < -1000) //1000ms
  599. {
  600. sprintf((char*)buf_log_evcomm_fork2,
  601. "[fork1][Warning]S(%.02lf) > E(%.02lf), d(%.02lf)",
  602. StartTime,
  603. EndTime,
  604. t_diff);
  605. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  606. }
  607. #endif
  608. return -1;
  609. }
  610. return t_diff;
  611. }
  612. double DiffTimeb_fork2_Error_Monitor(struct timeb ST, struct timeb ET)
  613. {
  614. //return milli-second
  615. static double StartTime, EndTime;
  616. static double t_diff;
  617. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  618. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  619. t_diff = EndTime - StartTime;
  620. if (t_diff < 0)
  621. {
  622. #if 0
  623. if (t_diff < -1000) //1000ms
  624. {
  625. sprintf((char*)buf_log_evcomm_fork2,
  626. "[fork2][Warning]S(%.02lf) > E(%.02lf), d(%.02lf)",
  627. StartTime,
  628. EndTime,
  629. t_diff);
  630. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  631. }
  632. #endif
  633. return -1;
  634. }
  635. return t_diff;
  636. }
  637. int CreateShareMemory()
  638. {
  639. int MeterSMId;
  640. //create ShmSysConfigAndInfo
  641. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  642. {
  643. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmget ShmSysConfigAndInfo NG");
  644. return 0;
  645. }
  646. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  647. {
  648. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmat ShmSysConfigAndInfo NG");
  649. return 0;
  650. }
  651. memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
  652. //create ShmStatusCodeData
  653. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  654. {
  655. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmget ShmStatusCodeData NG");
  656. return 0;
  657. }
  658. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  659. {
  660. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmat ShmStatusCodeData NG");
  661. return 0;
  662. }
  663. memset(ShmStatusCodeData,0,sizeof(struct StatusCodeData));
  664. //create ShmCcsData
  665. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  666. {
  667. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmget ShmCcsData NG");
  668. return 0;
  669. }
  670. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  671. {
  672. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[main]CreateShareMemory:shmat ShmCcsData NG");
  673. return 0;
  674. }
  675. memset(ShmCcsData,0,sizeof(struct CcsData));
  676. return 1;
  677. }
  678. /*===========================================================================
  679. FUNCTION: ShareMemory_Init
  680. DESCRIPTION:
  681. PRE-CONDITION:
  682. INPUT:
  683. OUTPUT:
  684. GLOBAL VARIABLES:
  685. =============================================================================*/
  686. int ShareMemory_Init()
  687. {
  688. int MeterSMId;
  689. //create ShmSysConfigAndInfo
  690. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  691. {
  692. DEBUG_ERROR("ShareMemory_Init:shmget ShmSysConfigAndInfo NG");
  693. return 0;
  694. }
  695. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  696. {
  697. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "ShareMemory_Init:shmat ShmSysConfigAndInfo NG");
  698. return 0;
  699. }
  700. //create ShmStatusCodeData
  701. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  702. {
  703. DEBUG_ERROR("ShareMemory_Init:shmget ShmStatusCodeData NG");
  704. return 0;
  705. }
  706. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  707. {
  708. DEBUG_ERROR("ShareMemory_Init:shmat ShmStatusCodeData NG");
  709. return 0;
  710. }
  711. //create ShmCcsData
  712. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  713. {
  714. DEBUG_ERROR("ShareMemory_Init:shmget ShmCcsData NG");
  715. return 0;
  716. }
  717. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  718. {
  719. DEBUG_ERROR("ShareMemory_Init:shmat ShmCcsData NG");
  720. return 0;
  721. }
  722. //create ShmInternalComm
  723. if ((MeterSMId = shmget(ShmInternalCommKey, sizeof(struct InternalComm), IPC_CREAT | 0777)) < 0)
  724. {
  725. DEBUG_ERROR("ShareMemory_Init:shmget ShmInternalComm NG");
  726. return 0;
  727. }
  728. else if ((ShmInternalComm = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  729. {
  730. DEBUG_ERROR("ShareMemory_Init:shmat ShmInternalComm NG");
  731. return 0;
  732. }
  733. //Initial ShmCharger
  734. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), 0777)) < 0)
  735. {
  736. DEBUG_ERROR("shmget ShmChargerKey NG\r\n");
  737. return 0;
  738. }
  739. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  740. {
  741. DEBUG_ERROR("shmat ShmChargerKey NG\r\n");
  742. return 0;
  743. }
  744. //[To-Do] The initialization here is reduntant and should be removed partially.
  745. //SHM_Init_supportedAppProtocolRes(ShmCcsData);
  746. //SHM_Init_din_SessionSetupRes(ShmCcsData);
  747. //SHM_Init_din_ServiceDiscoveryRes(ShmCcsData);
  748. //SHM_Init_din_ServiceAndPaymentSelectionRes(ShmCcsData);
  749. //SHM_Init_din_ContractAuthenticationRes(ShmCcsData);
  750. SHM_Init_din_ChargeParameterDiscoveryRes(ShmCcsData);
  751. SHM_Init_din_CableCheckRes(ShmCcsData);
  752. SHM_Init_din_PreChargeRes(ShmCcsData);
  753. SHM_Init_din_PowerDeliveryRes(ShmCcsData);
  754. SHM_Init_din_CurrentDemandRes(ShmCcsData);
  755. SHM_Init_din_WeldingDetectionRes(ShmCcsData);
  756. SHM_Init_din_SessionStopRes(ShmCcsData);
  757. return 1;
  758. }
  759. /*===========================================================================
  760. FUNCTION: GetEthMac
  761. DESCRIPTION:
  762. 1. Accessing current CsuMac address, if Eth = eth1
  763. 2. The address of eth1(for QCA7000) is a random number, which will be modified
  764. after each time of booting up system.
  765. PRE-CONDITION:
  766. INPUT:
  767. OUTPUT:
  768. GLOBAL VARIABLES:
  769. =============================================================================*/
  770. int GetEthMac(unsigned char *Eth, unsigned char *mac)
  771. {
  772. //Parameters:MAC,IP,Mask,Gateway
  773. int fd;
  774. unsigned char addr[18], Buffer[128];
  775. memset(Buffer, 0, sizeof(Buffer));
  776. sprintf((char*)Buffer, "cat /sys/class/net/%s/address > /mnt/GetEthInfo", Eth); //CsuMac (Eth = eth1)
  777. system((char*)Buffer);
  778. fd = open("/mnt/GetEthInfo", O_RDONLY);
  779. if(fd < 0)
  780. {
  781. system("rm -f /mnt/GetEthInfo");
  782. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "GetEthMac: MAC Address open error");
  783. return 0;
  784. }
  785. memset(mac, 0, 6);
  786. memset(addr, 0, sizeof(addr));
  787. read(fd, addr, 17);
  788. close(fd);
  789. system("rm -f /mnt/GetEthInfo");
  790. 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)
  791. sprintf((char*)buf_log_evcomm,
  792. "[GetEthMac]EVSE MACAddr(CsuMac): %02x:%02x:%02x:%02x:%02x:%02x",
  793. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); //CsuMac (Eth = eth1)
  794. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  795. return 1;
  796. }
  797. /*===========================================================================
  798. FUNCTION: ReadAdcVolt
  799. DESCRIPTION:
  800. 1. fork1
  801. PRE-CONDITION:
  802. INPUT:
  803. OUTPUT:
  804. GLOBAL VARIABLES:
  805. =============================================================================*/
  806. float ReadAdcVolt(unsigned char AdcChannel)
  807. {
  808. //AIN0=CCS GUN Temp 1
  809. //AIN1=CCS GUN Temp 2
  810. //AIN2=CCS_Proximity/2
  811. //AIN3=pilot voltage
  812. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  813. return 0;
  814. #else
  815. if(AdcChannel == 3)
  816. {
  817. int fd,count,AvgTimes;
  818. unsigned char SampleBuf[4];
  819. float TmpVolt, MinSample, AvgSample = 0;
  820. fd = open("/sys/bus/iio/devices/iio\\:device0/in_voltage3_raw", O_RDONLY);
  821. if(fd > 0)
  822. {
  823. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  824. for(AvgTimes = 0; AvgTimes < 3; AvgTimes++) //period = 60~91 ms(renice -10, , CurrentDemand())
  825. {
  826. count = 0;
  827. MinSample = 2306;
  828. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  829. while(count < 30) //period = 21~42ms (renice -10, CurrentDemand())
  830. {
  831. //re-sampling period = 3~13ms (renice -10, SLAC())
  832. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  833. read(fd, SampleBuf, 4); //period = 3.2~10ms (renice -10, SLAC())
  834. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  835. TmpVolt = atoi((char*)SampleBuf);
  836. if((TmpVolt < 2306) && (TmpVolt > 0))//positive voltage
  837. {
  838. if(TmpVolt < MinSample)
  839. {
  840. MinSample = TmpVolt;
  841. }
  842. count++;
  843. }
  844. lseek(fd, 0, SEEK_SET);
  845. }
  846. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  847. AvgSample += MinSample;
  848. }
  849. AvgSample /= AvgTimes;
  850. close(fd);
  851. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  852. return ((0.954-(1.8*AvgSample/4095))/0.06);
  853. }
  854. else
  855. {
  856. return -1;
  857. }
  858. }
  859. else
  860. {
  861. FILE *fp;
  862. unsigned char str[64];
  863. unsigned char AdcValue[8];
  864. if(AdcChannel > 7)
  865. {
  866. return -1;
  867. }
  868. memset(str,0,sizeof(str));
  869. memset(AdcValue,0,sizeof(AdcValue));
  870. sprintf((char*)str, "cat /sys/bus/iio/devices/iio\\:device0/in_voltage%d_raw", AdcChannel);
  871. fp=popen((char*)str, "r");
  872. if(fgets((char*)AdcValue,sizeof(AdcValue),fp) == NULL)
  873. {
  874. pclose(fp);
  875. return -1;
  876. }
  877. pclose(fp);
  878. //Vin = Vref *D / (2^n - 1)
  879. return ((float)1.8*(float)atoi((char*)AdcValue))/4095;
  880. }
  881. #endif
  882. }
  883. /*===========================================================================
  884. FUNCTION: ReadAdcVolt
  885. DESCRIPTION:
  886. //AIN0=CCS GUN Temp 1
  887. //AIN1=CCS GUN Temp 2
  888. //AIN2=CCS_Proximity/2
  889. //AIN3=pilot voltage
  890. PRE-CONDITION:
  891. INPUT:
  892. OUTPUT:
  893. GLOBAL VARIABLES:
  894. =============================================================================*/
  895. float ReadAdcVolt_PP_fork3()
  896. {
  897. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  898. return 0;
  899. #else
  900. int fd, AvgTimes;
  901. unsigned char SampleBuf[4];
  902. float TmpVolt = 0;
  903. float AvgSample = 0;
  904. float V_pp = 0;
  905. fd = open("/sys/bus/iio/devices/iio\\:device0/in_voltage2_raw", O_RDONLY); //PP
  906. if(fd > 0)
  907. {
  908. for(AvgTimes = 0; AvgTimes < 5; AvgTimes++) //get 5 samples
  909. {
  910. read(fd, SampleBuf, 4);
  911. TmpVolt = atoi((char*)SampleBuf);
  912. lseek(fd, 0, SEEK_SET);
  913. AvgSample += TmpVolt;
  914. }
  915. close(fd);
  916. AvgSample /= AvgTimes;
  917. V_pp = (3.6*AvgSample)/4095; //PP
  918. #if 0
  919. printf("PP_adc = %.02f, PP_v = %.02f\n", AvgSample, V_pp);
  920. usleep(3000000); //300ms
  921. #endif
  922. usleep(20000); //20ms
  923. return V_pp;
  924. }
  925. else
  926. {
  927. return -1;
  928. }
  929. #endif
  930. }
  931. /*===========================================================================
  932. FUNCTION: Qca7kPowerReset
  933. DESCRIPTION:
  934. PRE-CONDITION:
  935. INPUT:
  936. OUTPUT:
  937. GLOBAL VARIABLES:
  938. =============================================================================*/
  939. void Qca7kPowerReset()
  940. {
  941. printf("\n[CCS Board]Reset QCA7000...\n");
  942. system("echo 1 > /sys/class/gpio/gpio88/value");
  943. usleep(500000);
  944. system("echo 0 > /sys/class/gpio/gpio88/value");
  945. }
  946. /*===========================================================================
  947. FUNCTION: SwitchCpStateE
  948. DESCRIPTION:
  949. PRE-CONDITION:
  950. INPUT:
  951. OUTPUT:
  952. GLOBAL VARIABLES:
  953. =============================================================================*/
  954. int SwitchCpStateE(unsigned char OnOff)
  955. {
  956. /*
  957. * TODO: Request CP change to state E
  958. */
  959. if((OnOff != ENABLE) && (OnOff != DISABLE))
  960. {
  961. return -1;
  962. }
  963. struct ChargingInfoData *ccs;
  964. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  965. ccs->SwitchCpStateE_status = OnOff;
  966. //OnOff = 1 => switch State to E
  967. //OnOff = 0 => return noraml
  968. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  969. {
  970. if(OnOff == DISABLE)
  971. {
  972. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SwitchCpStateE: released\n");
  973. CSUCOMMAC_SHM.CpSetStateE = DISABLE;
  974. }
  975. else
  976. {
  977. OutputCpPwmDuty(100); //set CP duty as 100, firstly.
  978. CSUCOMMAC_SHM.CpSetStateE = ENABLE;
  979. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SwitchCpStateE: enabled!\n");
  980. }
  981. }
  982. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  983. {
  984. if(OnOff == DISABLE)
  985. {
  986. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SwitchCpStateE: released\n");
  987. system("echo 0 > /sys/class/gpio/gpio86/value");
  988. }
  989. else
  990. {
  991. OutputCpPwmDuty(100); //set CP duty as 100, firstly.
  992. system("echo 1 > /sys/class/gpio/gpio86/value");
  993. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SwitchCpStateE: enabled!\n");
  994. }
  995. }
  996. #endif
  997. if (ccs->SwitchCpStateE_status_pre != ccs->SwitchCpStateE_status)
  998. {
  999. sprintf((char*)buf_log_evcomm,
  1000. "SwitchCpStateE: %d >> %d",
  1001. ccs->SwitchCpStateE_status_pre,
  1002. ccs->SwitchCpStateE_status
  1003. );
  1004. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  1005. ccs->SwitchCpStateE_status_pre = ccs->SwitchCpStateE_status;
  1006. }
  1007. return 0;
  1008. }
  1009. /*===========================================================================
  1010. FUNCTION: OutputCpPwmDuty
  1011. DESCRIPTION:
  1012. PRE-CONDITION:
  1013. INPUT:
  1014. OUTPUT:
  1015. GLOBAL VARIABLES:
  1016. =============================================================================*/
  1017. int OutputCpPwmDuty(unsigned char Duty)
  1018. {
  1019. #if ((CCS_ENERGY_TRANSFER_MODE != MODE_AC_SINGLE_PHASE_CORE) && (CCS_ENERGY_TRANSFER_MODE != MODE_AC_THREE_PHASE_CORE))
  1020. int DutyInNanoSec;
  1021. #endif
  1022. struct ChargingInfoData *ccs;
  1023. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1024. if((Duty < 0)||(Duty > 100))
  1025. {
  1026. return -1;
  1027. }
  1028. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  1029. {
  1030. CSUCOMMAC_SHM.CpSetPWMDuty = Duty;
  1031. }
  1032. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  1033. {
  1034. DutyInNanoSec = 10000 * Duty;
  1035. sprintf((char*)buf_log_evcomm, "echo %d > /sys/class/pwm/pwmchip0/pwm0/duty_cycle", DutyInNanoSec);//nanoseconds
  1036. system((char*)buf_log_evcomm);
  1037. }
  1038. #endif
  1039. ccs->CpDuty = Duty;
  1040. //CP Duty
  1041. if (ccs->CpDuty != ccs->CpDuty_pre)
  1042. {
  1043. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  1044. sprintf((char*)buf_log_evcomm, "CP Duty: %d %% >> %d %%", ccs->CpDuty_pre, ccs->CpDuty);
  1045. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  1046. ccs->CpDuty_pre = ccs->CpDuty;
  1047. }
  1048. return 0;
  1049. }
  1050. /*===========================================================================
  1051. FUNCTION: Check_Plugin_Status_Update_fork1
  1052. DESCRIPTION:
  1053. PRE-CONDITION:
  1054. INPUT:
  1055. OUTPUT:
  1056. GLOBAL VARIABLES:
  1057. =============================================================================*/
  1058. //#if SAVE_SYS_LOG_MSG_EVCOMM_SWITCH == ENABLE
  1059. void Check_Plugin_Status_Update_fork1(struct ChargingInfoData *ccs)
  1060. {
  1061. if (ccs->ConnectorPlugIn != ccs->ConnectorPlugIn_new)
  1062. {
  1063. sprintf((char*)buf_log_evcomm_fork1, "[fork1]Plugin: %d >> %d (CP=%.2fV, PP=%.2fV)",
  1064. ccs->ConnectorPlugIn,
  1065. ccs->ConnectorPlugIn_new,
  1066. ccs->CpVoltage,
  1067. ccs->PpVoltage
  1068. );
  1069. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork1);
  1070. ccs->ConnectorPlugIn_pre = ccs->ConnectorPlugIn;
  1071. ccs->ConnectorPlugIn = ccs->ConnectorPlugIn_new;
  1072. //ccs->CpVoltage_pre = ccs->CpVoltage;
  1073. }
  1074. }
  1075. //#endif
  1076. /*===========================================================================
  1077. FUNCTION: ConnectorPlugIn
  1078. DESCRIPTION:
  1079. PRE-CONDITION:
  1080. INPUT:
  1081. OUTPUT:
  1082. 0: Unplug
  1083. 1: Plug-in
  1084. GLOBAL VARIABLES:
  1085. =============================================================================*/
  1086. int CheckConnectorPlugIn()
  1087. {
  1088. /*
  1089. * TODO: Return connector status depedon CP
  1090. */
  1091. static struct ChargingInfoData *ccs;
  1092. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1093. #if (CP_PROTECTION_MECHANISM == ENABLE)
  1094. return (int)ccs->ConnectorPlugIn;
  1095. #else
  1096. return TRUE;
  1097. #endif
  1098. }
  1099. /*===========================================================================
  1100. FUNCTION: Check_CP_State_Error_fork1
  1101. DESCRIPTION:
  1102. PRE-CONDITION:
  1103. INPUT:
  1104. OUTPUT:
  1105. GLOBAL VARIABLES:
  1106. =============================================================================*/
  1107. int Check_CP_State_Error_fork1(struct ChargingInfoData *ccs)
  1108. {
  1109. #if (CP_PROTECTION_MECHANISM == DISABLE)
  1110. {
  1111. return -1;
  1112. }
  1113. #endif
  1114. unsigned char state = 0;
  1115. #if ((CCS_ENERGY_TRANSFER_MODE != MODE_AC_SINGLE_PHASE_CORE) && (CCS_ENERGY_TRANSFER_MODE != MODE_AC_THREE_PHASE_CORE))
  1116. double time_diff = 0;
  1117. #endif
  1118. state = Check_V2G_Flow_Status();
  1119. //SLAC, SLAAC, SDP, ..., ChargeParameterDiscovery
  1120. if (state >= CM_SLAC_PARM_CONF && //by considering 15118(SLAC first)
  1121. state < ChargeParameterDiscoveryRequest &&
  1122. state != IDLE &&
  1123. state != CM_SET_KEY_REQ &&
  1124. state != CM_SET_KEY_CNF &&
  1125. state != CM_VALIDATE_REQ &&
  1126. state != CM_VALIDATE_CNF)
  1127. {
  1128. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  1129. {
  1130. if((ccs->CpState != 2) && (ccs->CpState != 3))
  1131. {
  1132. if (ccs->CpState_err == FALSE)
  1133. {
  1134. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork1][CP][Error]before CPD"); //CPD: ChargeParameterDiscoveryRequest
  1135. //CPD: ChargeParameterDiscovery
  1136. }
  1137. ccs->CpState_err = TRUE;
  1138. }
  1139. }
  1140. #else
  1141. {
  1142. if(ccs->CpState != 3)
  1143. {
  1144. if (ccs->CpState_err == FALSE)
  1145. {
  1146. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork1][CP][Error]before CPD"); //CPD: ChargeParameterDiscoveryRequest
  1147. //CPD: ChargeParameterDiscovery
  1148. }
  1149. ccs->CpState_err = TRUE;
  1150. }
  1151. }
  1152. #endif
  1153. }
  1154. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  1155. {
  1156. //ChargeParameterDiscovery
  1157. if(state >= ChargeParameterDiscoveryRequest && //35
  1158. state <= ChargeParameterDiscoveryResponse) //36
  1159. {
  1160. if(ccs->CpState != 3)
  1161. {
  1162. if (ccs->CpState_err == FALSE)
  1163. {
  1164. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CP][Error]CPD");
  1165. //PRC: Precharge
  1166. //CUD: CurrentDemand
  1167. }
  1168. ccs->CpState_err = TRUE;
  1169. }
  1170. }
  1171. //ChargingStatus
  1172. if(state >= ChargingStatusRequest && //43
  1173. state <= ChargingStatusResponse) //44
  1174. {
  1175. if(ccs->CpState != 4 && ccs->CpState != 5)
  1176. {
  1177. if (ccs->CpState_err == FALSE)
  1178. {
  1179. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CP][Error]CGS");
  1180. //PRC: Precharge
  1181. //CUD: CurrentDemand
  1182. }
  1183. ccs->CpState_err = TRUE;
  1184. }
  1185. }
  1186. }
  1187. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  1188. {
  1189. //ChargeParameterDiscovery, CableCheck
  1190. if (state >= ChargeParameterDiscoveryRequest && //35
  1191. state <= CableCheckResponse) //38
  1192. {
  1193. if(ccs->CableCheckPreCountDownDone == FALSE &&
  1194. state >= CableCheckRequest)
  1195. {
  1196. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  1197. time_diff = DiffTimeb_fork1_Error_Monitor(EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start, EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  1198. if(time_diff >= V2G_SECC_CP_Timeout_CableCheck) //2 sec
  1199. {
  1200. sprintf((char*)buf_log_evcomm_fork1,
  1201. "[fork1][CP]check C(4),D(5): ON (%.02lf of %dms)",
  1202. time_diff,
  1203. V2G_SECC_CP_Timeout_CableCheck);
  1204. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork1);
  1205. ccs->CableCheckPreCountDownDone = TRUE;
  1206. }
  1207. }
  1208. if(ccs->CableCheckPreCountDownDone == TRUE)
  1209. {
  1210. if(ccs->CpState != 4 && ccs->CpState != 5)
  1211. {
  1212. if (ccs->CpState_err == FALSE)
  1213. {
  1214. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CP][Error]CPD and CCK (after CNT)");
  1215. //CPD: ChargeParameterDiscovery
  1216. //CCK: CableCheck
  1217. //CNT: count down
  1218. }
  1219. ccs->CpState_err = TRUE;
  1220. }
  1221. }
  1222. else
  1223. {
  1224. if(ccs->CpState != 3 && ccs->CpState != 4 && ccs->CpState != 5)
  1225. {
  1226. if (ccs->CpState_err == FALSE)
  1227. {
  1228. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CP][Error]CPD and CCK (before CNT)");
  1229. //CPD: ChargeParameterDiscovery
  1230. //CCK: CableCheck
  1231. //CNT: count down
  1232. }
  1233. ccs->CpState_err = TRUE;
  1234. }
  1235. }
  1236. }
  1237. //Precharge, CurrentDemand
  1238. if(state >= PreChargeRequest && //39
  1239. state <= CurrentDemandResponse) //46
  1240. {
  1241. if(ccs->CpState != 4 && ccs->CpState != 5)
  1242. {
  1243. if (ccs->CpState_err == FALSE)
  1244. {
  1245. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CP][Error]PRC and CUD");
  1246. //PRC: Precharge
  1247. //CUD: CurrentDemand
  1248. }
  1249. ccs->CpState_err = TRUE;
  1250. }
  1251. }
  1252. }
  1253. #endif
  1254. if(ccs->CpState_err == TRUE && ccs->CpState_err_logged == FALSE)
  1255. {
  1256. OutputCpPwmDuty(100);
  1257. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  1258. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1259. //CCS_SECC_CP_State_Error (023889)
  1260. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1261. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1262. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1263. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1264. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1265. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1266. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1267. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1268. sprintf((char*)buf_log_evcomm_fork1, "[fork1][Erorr]CP(%d) Error in state %d(%d)",
  1269. ccs->CpState,
  1270. state,
  1271. ccs->CableCheckPreCountDownDone
  1272. );
  1273. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork1);
  1274. ccs->CpState_err_logged = TRUE;
  1275. }
  1276. return 0;
  1277. }
  1278. /*===========================================================================
  1279. FUNCTION: Check_CP_State_Update_fork1
  1280. DESCRIPTION:
  1281. PRE-CONDITION:
  1282. INPUT:
  1283. OUTPUT:
  1284. GLOBAL VARIABLES:
  1285. =============================================================================*/
  1286. //#if SAVE_SYS_LOG_MSG_EVCOMM_SWITCH == ENABLE
  1287. void Check_CP_State_Update_fork1(struct ChargingInfoData *ccs)
  1288. {
  1289. //CP State
  1290. if (ccs->CpState != ccs->CpState_pre)
  1291. {
  1292. sprintf((char*)buf_log_evcomm_fork1, "[fork1]CP State: %d >> %d (%.2fV)",
  1293. ccs->CpState_pre,
  1294. ccs->CpState,
  1295. ccs->CpVoltage
  1296. );
  1297. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork1);
  1298. ccs->CpState_pre = ccs->CpState;
  1299. }
  1300. }
  1301. //#endif
  1302. /*===========================================================================
  1303. FUNCTION: CP_Detection
  1304. DESCRIPTION:
  1305. 1. In order to detect CP in efficient response time, we creat an independent
  1306. thead for this procedure.
  1307. 2. The priority of this thread is set as the same as other tasks.
  1308. 3. fork1
  1309. PRE-CONDITION:
  1310. INPUT:
  1311. OUTPUT:
  1312. GLOBAL VARIABLES:
  1313. =============================================================================*/
  1314. void CP_Detection()
  1315. {
  1316. pid_t tmp = 0;
  1317. // struct timeb StartTime, EndTime;
  1318. #if ((CCS_ENERGY_TRANSFER_MODE != MODE_AC_SINGLE_PHASE_CORE) && (CCS_ENERGY_TRANSFER_MODE != MODE_AC_THREE_PHASE_CORE))
  1319. unsigned char Statetmp;
  1320. float TotalTmpVolt;
  1321. #endif
  1322. struct ChargingInfoData *ccs;
  1323. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1324. if(CP_Detection_Pid == 0)
  1325. {
  1326. tmp = fork();
  1327. if(tmp > 0)
  1328. {
  1329. CP_Detection_Pid = tmp;
  1330. #if 1
  1331. unsigned char buf[64];
  1332. memset(buf, 0, sizeof(buf));
  1333. //sprintf((char*)buf, "renice -20 -p %d", tmp);
  1334. sprintf((char*)buf, "renice -10 -p %d", tmp);
  1335. system((char*)buf);
  1336. #endif
  1337. return;
  1338. }
  1339. }
  1340. while(1)
  1341. {
  1342. //ADC_Voltage = (1+RE62/RE63)*0.9 - (RE62/RE63)*Pilot_Voltage, RE62/RE63=0.06
  1343. //=>Pilot_Voltage=(0.954-ADC_Voltage)/0.06
  1344. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  1345. {
  1346. ccs->CpVoltage = ShmInternalComm->AC_CpPositiveVoltage;
  1347. ccs->CpState = ShmInternalComm->AC_CpPresentState;
  1348. }
  1349. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  1350. {
  1351. TotalTmpVolt = ReadAdcVolt(3);
  1352. ccs->CpVoltage = TotalTmpVolt;
  1353. if (ccs->CpVoltage_pre != ccs->CpVoltage)
  1354. {
  1355. ccs->CpVoltage_pre = ccs->CpVoltage;
  1356. }
  1357. //printf("TotalTmpVolt = %.02f\n", TotalTmpVolt);
  1358. //If CP voltage is higer than 13.5V
  1359. if(TotalTmpVolt >= 13.5)
  1360. {
  1361. //Pilot Error
  1362. if((ccs->CpState == 1) &&
  1363. (TotalTmpVolt < 13.75))
  1364. {
  1365. //V_cp = 13.5 ~ 13.75
  1366. Statetmp = 1;
  1367. }
  1368. else
  1369. {
  1370. //V_cp = 13.5 ~
  1371. Statetmp = 8;
  1372. }
  1373. }
  1374. else if((TotalTmpVolt >= 10.5) && (TotalTmpVolt < 13.5)) //V_cp = 10.5 ~ 13.5
  1375. {
  1376. //State A (12V)
  1377. if((ccs->CpState >= 2) &&
  1378. (ccs->CpState <= 3) &&
  1379. (TotalTmpVolt < 10.75))
  1380. {
  1381. if((ccs->CpDuty >= 5) &&
  1382. (ccs->CpDuty < 100))
  1383. {
  1384. Statetmp = 3;
  1385. }
  1386. else
  1387. {
  1388. Statetmp = 2;
  1389. }
  1390. }
  1391. else if((ccs->CpState == 8) &&
  1392. (TotalTmpVolt >= 13.25))
  1393. {
  1394. Statetmp = 8;
  1395. }
  1396. else
  1397. {
  1398. Statetmp = 1;
  1399. }
  1400. }
  1401. else if((TotalTmpVolt >= 7.5) && (TotalTmpVolt < 10.5))
  1402. {
  1403. //State B (9V)
  1404. if((ccs->CpState == 4) && (TotalTmpVolt < 7.75))
  1405. {
  1406. Statetmp = 4;
  1407. }
  1408. else if((ccs->CpState == 1) && (TotalTmpVolt >= 10.25))
  1409. {
  1410. Statetmp = 1;
  1411. }
  1412. else
  1413. {
  1414. if((ccs->CpDuty >= 5) && (ccs->CpDuty < 100))
  1415. {
  1416. Statetmp = 3;
  1417. }
  1418. else
  1419. {
  1420. Statetmp = 2;
  1421. }
  1422. }
  1423. }
  1424. else if((TotalTmpVolt >= 4.5) && (TotalTmpVolt < 7.5))
  1425. {
  1426. //State C (6V)
  1427. if((ccs->CpState == 5) && (TotalTmpVolt < 4.75))
  1428. {
  1429. Statetmp = 5;
  1430. }
  1431. else if((ccs->CpState >= 2) && (ccs->CpState <= 3) && (TotalTmpVolt >= 7.25))
  1432. {
  1433. if((ccs->CpDuty >= 5)&&(ccs->CpDuty < 100))
  1434. {
  1435. Statetmp = 3;
  1436. }
  1437. else
  1438. {
  1439. Statetmp = 2;
  1440. }
  1441. }
  1442. else
  1443. {
  1444. Statetmp = 4;
  1445. }
  1446. }
  1447. else if((TotalTmpVolt >= 1.5) && (TotalTmpVolt < 4.5))
  1448. {
  1449. //State D (3V)
  1450. if((ccs->CpState == 6)&&(TotalTmpVolt < 1.75))
  1451. {
  1452. Statetmp = 6;
  1453. }
  1454. else if((ccs->CpState == 4)&&(TotalTmpVolt >= 4.25))
  1455. {
  1456. Statetmp = 4;
  1457. }
  1458. else
  1459. {
  1460. Statetmp = 5;
  1461. }
  1462. }
  1463. else if((TotalTmpVolt >= -1.5) && (TotalTmpVolt < 1.5)) //V_cp = -1.5V ~ 1.5V
  1464. {
  1465. //State E (0V)
  1466. if((ccs->CpState == 8) &&
  1467. (TotalTmpVolt < -1.25))
  1468. {
  1469. Statetmp = 8;
  1470. }
  1471. else if((ccs->CpState == 5) &&
  1472. (TotalTmpVolt >= 1.25))
  1473. {
  1474. Statetmp = 5;
  1475. }
  1476. else
  1477. {
  1478. Statetmp = 6;
  1479. }
  1480. }
  1481. else if((TotalTmpVolt >= -13.5) && (TotalTmpVolt < -10.5)) //V_cp = -10.5V ~ -13.5V
  1482. {
  1483. //State F (-12V)
  1484. if((ccs->CpState == 8) &&
  1485. (TotalTmpVolt >= -10.75))
  1486. {
  1487. Statetmp = 8;
  1488. }
  1489. else
  1490. {
  1491. Statetmp = 7;
  1492. }
  1493. }
  1494. else
  1495. {
  1496. //null
  1497. }
  1498. ccs->CpState = Statetmp;
  1499. }
  1500. #endif
  1501. Check_CP_State_Update_fork1(ccs);
  1502. Check_CP_State_Error_fork1(ccs);
  1503. //Updating Plugin status
  1504. #if (PP_PROTECTION_MECHANISM == ENABLE)
  1505. if((ccs->CpState >= 2 ) && (ccs->CpState <= 5 ) && (ccs->PpVoltage >= 1.0)) //PP >= 1.0V
  1506. #else
  1507. if((ccs->CpState >= 2 ) && (ccs->CpState <= 5 ))
  1508. #endif
  1509. {
  1510. ccs->ConnectorPlugIn_new = TRUE;
  1511. }
  1512. else
  1513. {
  1514. ccs->ConnectorPlugIn_new = FALSE;
  1515. OutputCpPwmDuty(100);
  1516. }
  1517. Check_Plugin_Status_Update_fork1(ccs);
  1518. //ftime(&EndTime);
  1519. //DEBUG_PRINTF_EVCOMM_DETAIL("duration:%.02lf\n", DiffTimeb(StartTime, EndTime));
  1520. }//while
  1521. }
  1522. /*===========================================================================
  1523. FUNCTION: PP_Detection
  1524. DESCRIPTION:
  1525. 0. SeccComm fork3
  1526. 1. In order to detect CP in efficient response time, we creat an independent
  1527. thead for this procedure.
  1528. 2. The priority of this thread is set as the same as other tasks.
  1529. PRE-CONDITION:
  1530. INPUT:
  1531. OUTPUT:
  1532. GLOBAL VARIABLES:
  1533. =============================================================================*/
  1534. void PP_Detection()
  1535. {
  1536. pid_t tmp = 0;
  1537. // struct timeb StartTime, EndTime;
  1538. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED))
  1539. //unsigned char Statetmp;
  1540. #endif
  1541. float TotalTmpVolt;
  1542. if(PP_Detection_Pid == 0)
  1543. {
  1544. tmp = fork();
  1545. if(tmp > 0)
  1546. {
  1547. PP_Detection_Pid = tmp;
  1548. #if 0
  1549. unsigned char buf[64];
  1550. memset(buf, 0, sizeof(buf));
  1551. sprintf((char*)buf, "renice -20 -p %d", tmp);
  1552. system(buf);
  1553. #endif
  1554. return;
  1555. }
  1556. }
  1557. while(1)
  1558. {
  1559. TotalTmpVolt = ReadAdcVolt_PP_fork3();
  1560. EVCOMM_SYS_INFO.PpVoltage = TotalTmpVolt;
  1561. if (EVCOMM_SYS_INFO.PpVoltage_pre != EVCOMM_SYS_INFO.PpVoltage)
  1562. {
  1563. if ((EVCOMM_SYS_INFO.PpVoltage_pre < 0.5 && EVCOMM_SYS_INFO.PpVoltage >= 1.0) ||
  1564. (EVCOMM_SYS_INFO.PpVoltage_pre >= 1.0 && EVCOMM_SYS_INFO.PpVoltage < 0.5))
  1565. {
  1566. sprintf((char*)buf_log_evcomm_fork3,
  1567. "[fork3]PP(%.2f >> %.2fV)",
  1568. EVCOMM_SYS_INFO.PpVoltage_pre,
  1569. EVCOMM_SYS_INFO.PpVoltage
  1570. );
  1571. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork3);
  1572. EVCOMM_SYS_INFO.PpVoltage_pre = EVCOMM_SYS_INFO.PpVoltage;
  1573. }
  1574. }
  1575. #if 0
  1576. sleep(5);
  1577. printf("V_pp = %.02f\n", TotalTmpVolt);
  1578. #endif
  1579. }//while
  1580. }
  1581. /*===========================================================================
  1582. FUNCTION: Error_Monitor
  1583. DESCRIPTION:
  1584. 1. This function only works in SeccComm fork2.
  1585. PRE-CONDITION:
  1586. INPUT:
  1587. OUTPUT:
  1588. GLOBAL VARIABLES:
  1589. =============================================================================*/
  1590. void Error_Monitor()
  1591. {
  1592. pid_t tmp = 0;
  1593. double time_diff = 0;
  1594. unsigned char status = 0;
  1595. struct ChargingInfoData *ccs;
  1596. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1597. if(Error_Monitor_Pid == 0)
  1598. {
  1599. tmp = fork(); //SeccComm fork2
  1600. if(tmp > 0)
  1601. {
  1602. Error_Monitor_Pid = tmp;
  1603. #if 0
  1604. unsigned char buf[64];
  1605. memset(buf, 0, sizeof(buf));
  1606. sprintf((char*)buf, "renice -20 -p %d", tmp);
  1607. system(buf);
  1608. #endif
  1609. return;
  1610. }
  1611. }
  1612. while(1)
  1613. {
  1614. //Step 0
  1615. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  1616. {
  1617. //If the End_Process is in processing, disable Error_Monitor.
  1618. continue;
  1619. }
  1620. //Step1 1: Check and Response to Plugin Status
  1621. if(CheckConnectorPlugIn() == FALSE)
  1622. {
  1623. status = Check_V2G_Flow_Status();
  1624. if (status > IDLE &&
  1625. status < Performance_Timeout &&
  1626. status != CM_SET_KEY_REQ &&
  1627. status != CM_SET_KEY_CNF &&
  1628. EVCOMM_SYS_INFO.End_Process_inused == FALSE)
  1629. {
  1630. #if (CP_PROTECTION_MECHANISM == ENABLE)
  1631. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2] Emergency Stop (due to Connector is plugged out during communication.)");
  1632. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]Plug out Error => End_Process");
  1633. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1634. //CCS_SECC_CP_State_Error (023889)
  1635. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1636. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1637. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1638. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1639. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1640. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1641. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1642. End_Process();
  1643. #else
  1644. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "CP_PROTECTION_MECHANISM is disabled. Emergency Stop: skipped" );
  1645. #endif
  1646. }
  1647. }
  1648. //Step 2: Check for V2G_SECC_Sequence_Timeout
  1649. //#if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  1650. #if 1
  1651. status = Check_V2G_Flow_Status();
  1652. if (status >= SupportedAppProtocolRequest &&
  1653. status < SessionStopRequest)
  1654. {
  1655. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_End);
  1656. time_diff = DiffTimeb_fork2_Error_Monitor(EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start, EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_End);
  1657. if(time_diff > V2G_SECC_Sequence_Timeout) //60s
  1658. {
  1659. sprintf((char*)buf_log_evcomm_fork2,
  1660. "[fork2]V2G_SECC_Sequence_Timeout in state %d - (%.02lf of %d ms)\n",
  1661. status,
  1662. time_diff,
  1663. V2G_SECC_Sequence_Timeout);
  1664. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1665. Update_V2G_Flow_Status(Sequence_Timeout);
  1666. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1667. //CCS_SECC_TIMEOUT_V2G_Sequence_Time (023844)
  1668. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1669. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1670. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1671. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1672. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  1673. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  1674. End_Process();
  1675. break;
  1676. }
  1677. else if (time_diff > 4000) //4s
  1678. {
  1679. //Check for CSU command of "Stop by EVSE"
  1680. if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_Shutdown)
  1681. {
  1682. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error_Monitor]EVSE_Shutdown");
  1683. Update_V2G_Flow_Status(Other_Fault);
  1684. }
  1685. else if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_EmergencyShutdown)
  1686. {
  1687. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error_Monitor]EVSE_EmergencyShutdown");
  1688. Update_V2G_Flow_Status(Other_Fault);
  1689. }
  1690. else if (ShmInternalComm->ChargingPermission == FALSE)
  1691. {
  1692. if (status >= ChargeParameterDiscoveryRequest) //&& status < SessionStopRequest
  1693. {
  1694. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error_Monitor]ChargingPermission = FALSE");
  1695. Update_V2G_Flow_Status(Other_Fault);
  1696. }
  1697. }
  1698. else
  1699. {
  1700. //null
  1701. }
  1702. }
  1703. else
  1704. {
  1705. //null
  1706. }
  1707. }
  1708. #endif
  1709. //Step 3: Check and Response to Error V2gFlowStatus
  1710. status = Check_V2G_Flow_Status();
  1711. if (status == Performance_Timeout ||
  1712. status == Sequence_Timeout ||
  1713. status == Other_Fault)
  1714. {
  1715. sprintf((char*)buf_log_evcomm_fork2,
  1716. "[fork2]Timeout or Fault State(%d) => End_Process",
  1717. status);
  1718. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1719. //Normal Stop
  1720. //alarm and duty to 100%
  1721. //OutputCpPwmDuty(100);
  1722. //SwitchCpStateE(ENABLE); //Set CP duty as 100% and set CP state as E (0V).
  1723. //[Joseph/ To-Do] Set State E (0V) via GPIO setting
  1724. End_Process();
  1725. }
  1726. //Step 4: Check and Response to CP State Error
  1727. if(ccs->CpState_err == TRUE)
  1728. {
  1729. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]CP Error => End_Process");
  1730. Update_V2G_Flow_Status(Other_Fault);
  1731. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1732. //CCS_SECC_CP_State_Error (023889)
  1733. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1734. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1735. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1736. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1737. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1738. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1739. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1740. End_Process();
  1741. }
  1742. //Step 5: Check and Response to Shutdown Commnad from CSU
  1743. if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_Shutdown ||
  1744. EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_EmergencyShutdown)
  1745. {
  1746. if (Check_V2G_Flow_Status() <= SLACC_SDP_TCP_Connection)
  1747. {
  1748. sprintf((char*)buf_log_evcomm_fork2,
  1749. "[fork2]EVSE Shutdown(%d) => End_Process",
  1750. EVCOMM_SYS_INFO.DC_EVSEStatus);
  1751. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1752. Update_V2G_Flow_Status(Other_Fault);
  1753. End_Process();
  1754. }
  1755. }
  1756. //Step 6: Check and Response to SessionStop
  1757. status = Check_V2G_Flow_Status();
  1758. if (status == SessionStopResponse)
  1759. {
  1760. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]SessionStopResponse => End_Process");
  1761. End_Process();
  1762. }
  1763. //Step 7: Check for ChargingPermission from TRUE to FALSE before V2G Messages
  1764. if ((ShmInternalComm->ChargingPermission == FALSE) &&
  1765. (ShmInternalComm->ChargingPermission_pre == TRUE) &&
  1766. (ccs->CpState >= 3) && (ccs->CpState <=5))
  1767. {
  1768. if (status >= CM_SLAC_PARM_REQ &&
  1769. status != CM_SET_KEY_REQ &&
  1770. status != CM_SET_KEY_CNF &&
  1771. status <= SLACC_SDP_TCP_Connection)
  1772. {
  1773. sprintf((char*)buf_log_evcomm_fork2,
  1774. "[fork2]Permission OFF before V2G msg(%d) => End_Process",
  1775. ShmInternalComm->ChargingPermission);
  1776. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1777. Update_V2G_Flow_Status(Other_Fault);
  1778. End_Process();
  1779. }
  1780. }
  1781. //Step 8: DC OVP Protection
  1782. if (CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED &&
  1783. status > CableCheckResponse &&
  1784. status <= SessionStopRequest &&
  1785. status != ChargingStatusRequest &&
  1786. status != ChargingStatusResponse &&
  1787. EVCOMM_SYS_INFO.EvBatteryMaxVoltage != 0)
  1788. {
  1789. //Part A: OVP Protection
  1790. if (EVCOMM_SYS_INFO.PresentChargingVoltage >= (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02)) // 2%
  1791. {
  1792. sprintf((char*)buf_log_evcomm_fork2,
  1793. "[fork2][Error]OVP => End_Process (%.02f > %.02f)",
  1794. EVCOMM_SYS_INFO.PresentChargingVoltage,
  1795. (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02));
  1796. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1797. Update_V2G_Flow_Status(Other_Fault);
  1798. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1799. //System CCS output OVP (012219)
  1800. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1801. ShmStatusCodeData->PresentStatusCode[0][1] = 1;
  1802. ShmStatusCodeData->PresentStatusCode[0][2] = 2;
  1803. ShmStatusCodeData->PresentStatusCode[0][3] = 2;
  1804. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  1805. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1806. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1807. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1808. End_Process();
  1809. }
  1810. //Part B: Over Voltage Request Protection
  1811. if (EVCOMM_SYS_INFO.EvBatterytargetVoltage >= (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02))
  1812. {
  1813. sprintf((char*)buf_log_evcomm_fork2,
  1814. "[fork2]Over V Req => End_Process (%.02f > %.02f)",
  1815. EVCOMM_SYS_INFO.EvBatterytargetVoltage,
  1816. (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02)
  1817. );
  1818. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm_fork2);
  1819. Update_V2G_Flow_Status(Other_Fault);
  1820. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1821. //System CCS output OVP (012219)
  1822. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1823. ShmStatusCodeData->PresentStatusCode[0][1] = 1;
  1824. ShmStatusCodeData->PresentStatusCode[0][2] = 2;
  1825. ShmStatusCodeData->PresentStatusCode[0][3] = 2;
  1826. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  1827. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1828. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1829. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1830. End_Process();
  1831. }
  1832. }
  1833. //Step 9: Check 60V
  1834. if (CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED &&
  1835. ShmInternalComm->ChargingPermission == TRUE &&
  1836. status < CableCheckRequest)
  1837. {
  1838. if (EVCOMM_SYS_INFO.PresentChargingVoltage >= 60) //60V
  1839. {
  1840. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]DC Output Voltage is over 60V => End_Process");
  1841. Update_V2G_Flow_Status(Other_Fault);
  1842. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1843. //CCS_SECC_Unexpected_60V_Before_Charing_Error (023890)
  1844. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1845. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1846. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1847. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1848. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  1849. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  1850. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1851. //Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1852. End_Process();
  1853. }
  1854. }
  1855. //Step 10: Check if the connector is unplug from plugin
  1856. if (EVCOMM_SYS_INFO.ConnectorPlugIn_pre == TRUE && EVCOMM_SYS_INFO.ConnectorPlugIn == FALSE)
  1857. {
  1858. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]Unplug Reset => End_Process");
  1859. Update_V2G_Flow_Status(Other_Fault);
  1860. End_Process();
  1861. }
  1862. }//while
  1863. }
  1864. /*===========================================================================
  1865. FUNCTION: SendSetKey
  1866. DESCRIPTION:
  1867. PRE-CONDITION:
  1868. INPUT:
  1869. OUTPUT:
  1870. GLOBAL VARIABLES:
  1871. =============================================================================*/
  1872. int SendSetKey()
  1873. {
  1874. int i = 0;
  1875. unsigned char nRandValue = 0x0;
  1876. unsigned char ConstString[16] = "PhihongKey000000";
  1877. memset(&SendMmePacket,0,sizeof(struct MmeHeader));
  1878. memcpy(SendMmePacket.ODA,QcaMac,6);
  1879. memcpy(SendMmePacket.OSA,CsuMac,6);
  1880. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  1881. SendMmePacket.MMV = 0x01;
  1882. SendMmePacket.MMTYPE = MMTYPE_CM_SET_KEY_REQ;
  1883. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  1884. SendMmePacketSize = 0;
  1885. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x01;//Fixed value (0x01) to indicate ��NMK��
  1886. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,4);//My Nonce, Fixed value(0x00000000), encrypted payload not used
  1887. SendMmePacketSize+=4;
  1888. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,4);//Your Nonce, Fixed value(0x00000000), encrypted payload not used
  1889. SendMmePacketSize+=4;
  1890. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x04;//PID, Fixed value (0x04) to indicate ��HLE protocol��
  1891. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,2);//PRN, Fixed value(0x00), encrypted payload not used
  1892. SendMmePacketSize+=2;
  1893. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00;//PMN, Fixed value(0x00) encrypted payload not used
  1894. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x01;//CCo Capablility
  1895. srand(time(NULL));
  1896. for (i = 10; i < 16; i++)
  1897. {
  1898. nRandValue = (rand()%62) + 1;
  1899. if((nRandValue>=0)&&(nRandValue<=9)) // 0 ~ 9
  1900. {
  1901. ConstString[i]= nRandValue + 0x30;
  1902. }
  1903. else if((nRandValue>=10)&&(nRandValue<=35)) // A ~ Z
  1904. {
  1905. ConstString[i]= nRandValue -10 + 0x41;
  1906. }
  1907. else if((nRandValue>=36)&&(nRandValue<=61)) // a ~ z
  1908. {
  1909. ConstString[i]= nRandValue -37 + 0x61;
  1910. }
  1911. else
  1912. {
  1913. ConstString[i]= 0x30;
  1914. }
  1915. }
  1916. memset(NewNmkKey,0,sizeof(NewNmkKey));
  1917. memset(Nid,0,sizeof(Nid));
  1918. HPAVKeyNMK(NewNmkKey, (char*)ConstString);
  1919. HPAVKeyNID(Nid,NewNmkKey,DEFAULT_LEVEL);
  1920. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,Nid,sizeof(Nid)); //NID, 54 LSBs contain the NID 2 MSBs = 0b00
  1921. SendMmePacketSize+=sizeof(Nid);
  1922. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x01;//NewEKS,Fixed value (0x01)to indicate ��NMK��
  1923. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,NewNmkKey,sizeof(NewNmkKey));//NewKey
  1924. SendMmePacketSize += sizeof(NewNmkKey);
  1925. SendMmePacketSize += 19; //the size before MMENTRY
  1926. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[QCA7K][Tx]CM_SET_KEY_REQ");
  1927. i = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  1928. Update_V2G_Flow_Status(CM_SET_KEY_REQ);
  1929. DEBUG_PRINTF_EVCOMM_DETAIL("SendSetKey: send size =%d\n",i);
  1930. return 0;
  1931. }
  1932. /*===========================================================================
  1933. FUNCTION: GetQca7kMac
  1934. DESCRIPTION:
  1935. PRE-CONDITION:
  1936. INPUT:
  1937. OUTPUT:
  1938. GLOBAL VARIABLES:
  1939. =============================================================================*/
  1940. int GetQca7kMac()
  1941. {
  1942. struct QcaVendorMmeHeader SendPacket;
  1943. memset(&SendPacket, 0, sizeof(struct QcaVendorMmeHeader));
  1944. memset(SendPacket.ODA, 0xFF, 6); //broadcast
  1945. memcpy(SendPacket.OSA, CsuMac, 6);
  1946. SendPacket.MTYPE = htons(EtherType_HomePlug);
  1947. SendPacket.MMV = 0x00;
  1948. SendPacket.MMTYPE = MMTYPE_VENDOR_VS_NW_INFO;
  1949. SendPacket.OUI[0] = 0x00;
  1950. SendPacket.OUI[1] = 0xB0;
  1951. SendPacket.OUI[2] = 0x52;
  1952. DEBUG_PRINTF_EVCOMM_DETAIL("GetQca7kMac: send size =%d\n", sendto(RawSock, &SendPacket, 20, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll)));
  1953. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Req for QCA7K MacAddr");
  1954. return 0;
  1955. }
  1956. /*===========================================================================
  1957. FUNCTION: Array_Check_All_Zero
  1958. DESCRIPTION:
  1959. PRE-CONDITION:
  1960. INPUT:
  1961. OUTPUT:
  1962. result:
  1963. (1) TRUE: all zero
  1964. (2) FALSE: not all zero
  1965. GLOBAL VARIABLES:
  1966. =============================================================================*/
  1967. int Array_Check_All_Zero(unsigned char *ptr, int size)
  1968. {
  1969. int result = TRUE;
  1970. int i = 0;
  1971. for (i = 0; i < size; i++)
  1972. {
  1973. if (ptr[i] != 0)
  1974. {
  1975. result = FALSE;
  1976. break;
  1977. }
  1978. }
  1979. return result;
  1980. }
  1981. /*===========================================================================
  1982. FUNCTION: Array_Compare_Identity
  1983. DESCRIPTION:
  1984. PRE-CONDITION:
  1985. INPUT:
  1986. OUTPUT:
  1987. result = FALSE (not identical)
  1988. result = TRUE (identical)
  1989. GLOBAL VARIABLES:
  1990. =============================================================================*/
  1991. int Array_Compare_Identity(unsigned char *ptrA, unsigned char *ptrB, int size)
  1992. {
  1993. int result = TRUE;
  1994. int i = 0;
  1995. for (i = 0; i < size; i++)
  1996. {
  1997. if (ptrA[i] != ptrB[i])
  1998. {
  1999. result = FALSE;
  2000. #if 0
  2001. sprintf((char*)buf_log_evcomm,
  2002. "[Array_Compare_Identity]%02X%02X%02X%02X%02X%02X,%02X%02X%02X%02X%02X%02X(%d)\n",
  2003. ptrA[0], ptrA[1], ptrA[2], ptrA[3], ptrA[4], ptrA[5],
  2004. ptrB[0], ptrB[1], ptrB[2], ptrB[3], ptrB[4], ptrB[5],
  2005. result);
  2006. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2007. #endif
  2008. break;
  2009. }
  2010. }
  2011. return result;
  2012. }
  2013. /*===========================================================================
  2014. FUNCTION: SLAC_DB_Search_EvMac_idx
  2015. DESCRIPTION:
  2016. PRE-CONDITION:
  2017. INPUT:
  2018. OUTPUT:
  2019. idx = -1 (not found)
  2020. GLOBAL VARIABLES:
  2021. =============================================================================*/
  2022. int SLAC_DB_Search_EvMac_idx(struct EVCC_SLAC_DATA_TYPE *evcc, unsigned char *EvMac_in)
  2023. {
  2024. int idx = -1;
  2025. int i = 0;
  2026. if (evcc->arrayLen == 0)
  2027. {
  2028. //printf("[SLAC_DB_Search_EvMac_idx]arrayLen is empty (%d)\n", evcc->arrayLen);
  2029. //no need to search
  2030. }
  2031. else if (evcc->arrayLen > EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE)
  2032. {
  2033. //error
  2034. sprintf((char*)buf_log_evcomm,
  2035. "[ERROR][SLAC_DB_Search_EvMac_idx]DB length(%d) > %d",
  2036. evcc->arrayLen,
  2037. EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE);
  2038. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2039. evcc->arrayLen = 0; //reset
  2040. }
  2041. else
  2042. {
  2043. //start searching
  2044. for (i = 0; i < evcc->arrayLen; i++)
  2045. {
  2046. //printf("[SLAC_DB_Search_EvMac_idx]checking DB[%d]...\n", i);
  2047. if (Array_Compare_Identity(evcc->array[i].EvMac, EvMac_in, SLAC_EVMAC_LENGTH) == TRUE)
  2048. {
  2049. //printf("[SLAC_DB_Search_EvMac_idx]identical at DB[%d]...\n", i);
  2050. idx = i;
  2051. break;
  2052. }
  2053. }
  2054. }
  2055. //printf("[SLAC_DB_Search_EvMac_idx]return = %d\n", idx);
  2056. return idx;
  2057. }
  2058. /*===========================================================================
  2059. FUNCTION: SLAC_DB_Check_EvMac_RunID_Matching
  2060. DESCRIPTION:
  2061. PRE-CONDITION:
  2062. INPUT:
  2063. OUTPUT:
  2064. res = FALSE (unmatched)
  2065. res = TRUE (matched)
  2066. GLOBAL VARIABLES:
  2067. =============================================================================*/
  2068. int SLAC_DB_Check_EvMac_RunID_Matching(struct EVCC_SLAC_DATA_TYPE *evcc, unsigned char *EvMac_in, unsigned char *RunID_in)
  2069. {
  2070. int res = FALSE;
  2071. int idx = -1;
  2072. idx = SLAC_DB_Search_EvMac_idx(evcc, EvMac_in);
  2073. if (idx >= 0)
  2074. {
  2075. res = Array_Compare_Identity(evcc->array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH);
  2076. }
  2077. else
  2078. {
  2079. //not found the EvMac data in DB
  2080. res = FALSE;
  2081. }
  2082. return res;
  2083. }
  2084. /*===========================================================================
  2085. FUNCTION: SLAC_DB_Add
  2086. DESCRIPTION:
  2087. 1. Saving each CM_SLAC_PARM_REQ data into EVCC database if its
  2088. EvMac and RunID data are different, respectively.
  2089. PRE-CONDITION:
  2090. INPUT:
  2091. OUTPUT:
  2092. idx = saved index (must be a positive value)
  2093. GLOBAL VARIABLES:
  2094. =============================================================================*/
  2095. int SLAC_DB_Add(unsigned char *EvMac_in, unsigned char *RunID_in)
  2096. {
  2097. int idx = -1;
  2098. //Search if this EvMac and RunID already exists
  2099. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2100. if (idx < 0) //not exist, yet.
  2101. {
  2102. if (SLAC_INFO.arrayLen < EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE) //20
  2103. {
  2104. sprintf((char*)buf_log_evcomm,
  2105. "[SLAC_DB_Add]data does not exist => added to %d-th",
  2106. SLAC_INFO.arrayLen);
  2107. DEBUG_PRINTF_EVCOMM_DETAIL("%s\n", buf_log_evcomm);
  2108. if (SLAC_INFO.arrayLen >= 0)
  2109. {
  2110. memset(&SLAC_INFO.array[SLAC_INFO.arrayLen], 0, sizeof(struct EVCC_SLAC_DATA_ARRAY_TYPE));
  2111. memcpy(SLAC_INFO.array[SLAC_INFO.arrayLen].EvMac, EvMac_in, SLAC_EVMAC_LENGTH);
  2112. memcpy(SLAC_INFO.array[SLAC_INFO.arrayLen].RunID, RunID_in, SLAC_RUNID_LENGTH);
  2113. idx = SLAC_INFO.arrayLen;
  2114. SLAC_INFO.arrayLen++;
  2115. }
  2116. else
  2117. {
  2118. sprintf((char*)buf_log_evcomm,
  2119. "[ERROR][SLAC_DB_Add]arrayLen: unexpected(%d)",
  2120. SLAC_INFO.arrayLen);
  2121. DEBUG_PRINTF_EVCOMM_DETAIL("%s\n", buf_log_evcomm);
  2122. SLAC_INFO.arrayLen = 0;
  2123. }
  2124. }
  2125. else
  2126. {
  2127. //DB is full
  2128. sprintf((char*)buf_log_evcomm,
  2129. "[SLAC_DB_Add]DB is full(%d) => bypass",
  2130. SLAC_INFO.arrayLen);
  2131. DEBUG_PRINTF_EVCOMM_DETAIL("%s\n", buf_log_evcomm);
  2132. }
  2133. }
  2134. else
  2135. {
  2136. #if 0
  2137. sprintf((char*)buf_log_evcomm,
  2138. "[SLAC_DB_Add]EvMac: existed (%d)",
  2139. idx);
  2140. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2141. #endif
  2142. //Check RunID
  2143. if (Array_Compare_Identity(SLAC_INFO.array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH) == TRUE)
  2144. {
  2145. //RunID is the same
  2146. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC_DB_Add]RunID: same");
  2147. }
  2148. else
  2149. {
  2150. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC_DB_Add]RunID: diff");
  2151. }
  2152. //Reset all corresponding parameters
  2153. #if 0
  2154. sprintf((char*)buf_log_evcomm,
  2155. "[SLAC_DB_Add]EvMac: reset para(%d)",
  2156. idx);
  2157. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2158. #endif
  2159. memset(&SLAC_INFO.array[idx], 0, sizeof(struct EVCC_SLAC_DATA_ARRAY_TYPE));
  2160. memcpy(SLAC_INFO.array[idx].EvMac, EvMac_in, SLAC_EVMAC_LENGTH);
  2161. memcpy(SLAC_INFO.array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH);
  2162. }
  2163. return idx;
  2164. }
  2165. /*===========================================================================
  2166. FUNCTION: SLAC_DB_Reset
  2167. DESCRIPTION:
  2168. PRE-CONDITION:
  2169. INPUT:
  2170. OUTPUT:
  2171. GLOBAL VARIABLES:
  2172. =============================================================================*/
  2173. int SLAC_DB_Reset()
  2174. {
  2175. memset(&SLAC_INFO, 0, sizeof(struct EVCC_SLAC_DATA_TYPE));
  2176. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC_DB_Reset]DONE");
  2177. return 0;
  2178. }
  2179. /*===========================================================================
  2180. FUNCTION: MmeProcess
  2181. DESCRIPTION:
  2182. PRE-CONDITION:
  2183. INPUT:
  2184. OUTPUT:
  2185. GLOBAL VARIABLES:
  2186. =============================================================================*/
  2187. int MmeProcess(unsigned char *Buffer, int DataLength)
  2188. {
  2189. //struct ethhdr *EthPacket;
  2190. struct MmeHeader *MmePacket;
  2191. static unsigned char counter;
  2192. unsigned char state = 0;
  2193. unsigned char *EvMac_in;
  2194. unsigned char *RunID_in;
  2195. int Rtn = 0;
  2196. int idx = 0;
  2197. MmePacket = (struct MmeHeader *)Buffer;
  2198. state = Check_V2G_Flow_Status();
  2199. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2200. {
  2201. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  2202. DEBUG_PRINTF_EVCOMM_DETAIL("******* Received MME Packet *******\n");
  2203. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  2204. DEBUG_PRINTF_EVCOMM_DETAIL("DataLength=%d\n",DataLength);
  2205. DEBUG_PRINTF_EVCOMM_DETAIL("ODA: %02x:%02x:%02x:%02x:%02x:%02x\n", //Destination MAC Address
  2206. MmePacket->ODA[0], MmePacket->ODA[1],
  2207. MmePacket->ODA[2], MmePacket->ODA[3],
  2208. MmePacket->ODA[4], MmePacket->ODA[5]);
  2209. DEBUG_PRINTF_EVCOMM_DETAIL("OSA: %02x:%02x:%02x:%02x:%02x:%02x\n", //Source MAC Address (EV MAC)
  2210. MmePacket->OSA[0], MmePacket->OSA[1],
  2211. MmePacket->OSA[2], MmePacket->OSA[3],
  2212. MmePacket->OSA[4], MmePacket->OSA[5]);
  2213. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(MmePacket->MTYPE));
  2214. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", MmePacket->MMV);
  2215. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", MmePacket->MMTYPE);
  2216. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n",
  2217. MmePacket->FMI[0],MmePacket->FMI[1]);
  2218. }
  2219. #endif
  2220. #if (SLAC_FIRST_RESPONSE_METHOD == SET_NO_PWM_IF_NOT_GET_PERMISSION)
  2221. {
  2222. //Check CP as 5%
  2223. if (EVCOMM_SYS_INFO.CpState != 3 &&
  2224. EVCOMM_SYS_INFO.CpState != 4 &&
  2225. MmePacket->MMTYPE != MMTYPE_CM_SET_KEY_CNF &&
  2226. MmePacket->MMTYPE != MMTYPE_VENDOR_VS_HOST_ACTION &&
  2227. MmePacket->MMTYPE != MMTYPE_VENDOR_ATTEN_CHAR &&
  2228. MmePacket->MMTYPE != MMTYPE_VENDOR_VS_NW_INFO_CNF
  2229. )
  2230. {
  2231. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC]ignored(wrong CP state)");
  2232. return 0;
  2233. }
  2234. }
  2235. #endif
  2236. //[To-Do] Adding a pre-check filter mechanism for Source and Destination MAC Address
  2237. //check if the destination address is broadcast or EVSE itself, it can be ignore if not belong to itself.
  2238. switch(MmePacket->MMTYPE)
  2239. {
  2240. case MMTYPE_CM_SET_KEY_CNF:
  2241. {
  2242. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SET_KEY_CNF ---\n");
  2243. DEBUG_PRINTF_EVCOMM_DETAIL("Result: 0x%x\n", MmePacket->MMENTRY[0]);
  2244. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[QCA7K][Rx]CM_SET_KEY_CNF (SetKey: DONE)");
  2245. Update_V2G_Flow_Status(CM_SET_KEY_CNF);
  2246. break;
  2247. }
  2248. case MMTYPE_CM_SLAC_PARM_REQ:
  2249. {
  2250. //Check QCA7000 status
  2251. if (EVCOMM_SYS_INFO.QCA7K_SetKeyDone == FALSE)
  2252. {
  2253. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx]CM_SLAC_PARM_REQ: ignored(QCA7K init...)");
  2254. break;
  2255. }
  2256. //Check error state
  2257. state = Check_V2G_Flow_Status();
  2258. if (state == Performance_Timeout || //253
  2259. state == Sequence_Timeout || //254
  2260. state == Other_Fault) //255
  2261. {
  2262. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx]CM_SLAC_PARM_REQ: ignored(in error state)");
  2263. break;
  2264. }
  2265. //Printing EV MAC Address
  2266. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx]CM_SLAC_PARM_REQ");
  2267. //Avoid Coupled SLAC_PARM_REQ
  2268. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2269. {
  2270. #if 0
  2271. sprintf((char*)buf_log_evcomm,
  2272. "[WARNING][SLAC][Rx]CM_SLAC_PARM_REQ: ignored(coupled SLAC,%d)",
  2273. CheckConnectorPlugIn());
  2274. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2275. #endif
  2276. break;
  2277. }
  2278. //[TC_SECC_VTB_CmSlacParm_003] SECURITY_TYPE needs to be 0x00 (no security)
  2279. if (MmePacket->MMENTRY[1] != 0)
  2280. {
  2281. sprintf((char*)buf_log_evcomm,
  2282. "[WARNING][SLAC][Rx]CM_SLAC_PARM_REQ: ignored(invalid SECURITY_TYPE,%d)", //Source MAC Address (EV MAC)
  2283. MmePacket->MMENTRY[1]);
  2284. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2285. break;
  2286. }
  2287. //=================== Legal CM_SLAC_PARM_REQ Zone =================
  2288. Update_V2G_Flow_Status(CM_SLAC_PARM_REQ);
  2289. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_RX_CM_SLAC_PARM_REQ)
  2290. {
  2291. //Once receiving CM_SLAC_PARM_REQ, set PWM as 5%
  2292. SwitchCpStateE(DISABLE);
  2293. OutputCpPwmDuty(5);
  2294. }
  2295. #endif
  2296. //Checking if this SLAC Req comes before 5% PWM (for 15118)
  2297. if(ShmInternalComm->ChargingPermission == FALSE)
  2298. {
  2299. //Sniffer_Tcpdump(ENABLE);
  2300. sprintf((char*)buf_log_evcomm, "Check Permission: %d (SLAC first => START)", ShmInternalComm->ChargingPermission);
  2301. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2302. }
  2303. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2304. {
  2305. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SLAC_PARM_REQ ---\n");
  2306. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2307. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2308. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2309. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2310. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2311. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7],
  2312. MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2313. DEBUG_PRINTF_EVCOMM_DETAIL("CipherSuiteSetSize: 0x%x\n", MmePacket->MMENTRY[10]);
  2314. DEBUG_PRINTF_EVCOMM_DETAIL("CipherSuite [1]: 0x%x,0x%x\n",
  2315. MmePacket->MMENTRY[11], MmePacket->MMENTRY[12]);
  2316. }
  2317. #endif
  2318. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2319. {
  2320. //Saving each CM_SLAC_PARM_REQ data into EVCC database
  2321. EvMac_in = &MmePacket->OSA[0];
  2322. RunID_in = &MmePacket->MMENTRY[2];
  2323. idx = SLAC_DB_Add(EvMac_in, RunID_in); //(EvMac, RunID)
  2324. if (idx < 0)
  2325. {
  2326. sprintf((char*)buf_log_evcomm,
  2327. "[WARNING][CM_SLAC_PARM_REQ]DB is full or errors occour(%d) => ignore",
  2328. idx);
  2329. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2330. break;
  2331. }
  2332. //Select the 1st EV MAC address
  2333. if (SLAC_INFO.arrayLen == 1) //1st Req
  2334. {
  2335. #if 1
  2336. sprintf((char*)buf_log_evcomm,
  2337. "[SLAC][Rx]CM_SLAC_PARM_REQ[%d](%02X:%02X:%02X:%02X:%02X:%02X,%02X%02X%02X%02X%02X%02X%02X%02X):selected",
  2338. (idx + 1),
  2339. MmePacket->OSA[0], MmePacket->OSA[1], MmePacket->OSA[2], MmePacket->OSA[3], MmePacket->OSA[4], MmePacket->OSA[5],
  2340. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3], MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2341. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7], MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2342. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2343. #endif
  2344. /*
  2345. sprintf((char*)buf_log_evcomm,
  2346. "[SLAC][EVMAC][%d-th]%02X:%02X:%02X:%02X:%02X:%02X (selected)", //Source MAC Address (EV MAC)
  2347. SLAC_INFO.arrayLen,
  2348. MmePacket->OSA[0], MmePacket->OSA[1],
  2349. MmePacket->OSA[2], MmePacket->OSA[3],
  2350. MmePacket->OSA[4], MmePacket->OSA[5]);
  2351. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2352. sprintf((char*)buf_log_evcomm,
  2353. "[SLAC][RunID][%d-th]%02X%02X%02X%02X%02X%02X%02X%02X (selected)", //RunID (from EVCC)
  2354. SLAC_INFO.arrayLen,
  2355. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2356. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2357. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7],
  2358. MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2359. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2360. */
  2361. }
  2362. else //2nd Req
  2363. {
  2364. #if 1
  2365. sprintf((char*)buf_log_evcomm,
  2366. "[SLAC][Rx]CM_SLAC_PARM_REQ[%d](%02X:%02X:%02X:%02X:%02X:%02X,%02X%02X%02X%02X%02X%02X%02X%02X):not selected",
  2367. (idx + 1),
  2368. MmePacket->OSA[0], MmePacket->OSA[1], MmePacket->OSA[2], MmePacket->OSA[3], MmePacket->OSA[4], MmePacket->OSA[5],
  2369. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3], MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2370. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7], MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2371. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2372. #endif
  2373. /*
  2374. sprintf((char*)buf_log_evcomm,
  2375. "[SLAC][EVMAC][%d-th]%02X:%02X:%02X:%02X:%02X:%02X (not selected)", //Source MAC Address (EV MAC)
  2376. SLAC_INFO.arrayLen,
  2377. MmePacket->OSA[0], MmePacket->OSA[1],
  2378. MmePacket->OSA[2], MmePacket->OSA[3],
  2379. MmePacket->OSA[4], MmePacket->OSA[5]);
  2380. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2381. sprintf((char*)buf_log_evcomm,
  2382. "[SLAC][RunID][%d-th]%02X%02X%02X%02X%02X%02X%02X%02X (not selected)", //RunID (from EVCC)
  2383. SLAC_INFO.arrayLen,
  2384. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2385. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2386. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7],
  2387. MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2388. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2389. */
  2390. }
  2391. memcpy(EvMac, MmePacket->OSA, sizeof(EvMac));
  2392. memcpy(DestSocketAddress.sll_addr, MmePacket->OSA, SLAC_EVMAC_LENGTH);
  2393. memcpy(SlacRunId, MmePacket->MMENTRY + 2, SLAC_RUNID_LENGTH);
  2394. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  2395. memcpy(SendMmePacket.ODA, MmePacket->OSA, 6);
  2396. memcpy(SendMmePacket.OSA, CsuMac, 6);
  2397. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  2398. SendMmePacket.MMV = MmePacket->MMV;
  2399. SendMmePacket.MMTYPE = MMTYPE_CM_SLAC_PARM_CNF;
  2400. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  2401. SendMmePacketSize = 0;
  2402. memset(SendMmePacket.MMENTRY, 0xFF, 6); //M-SOUND_TARGET(6-byte:0xFFFFFFFFFFFF): Fixed value indicating that M-Sounds to be sent as Ethernet broadcast
  2403. SendMmePacketSize += 6;
  2404. 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
  2405. SendMmePacket.MMENTRY[SendMmePacketSize++] = 6; //Time_Out(0x06): unit = 100ms
  2406. 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
  2407. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, SLAC_INFO.array[idx].EvMac, 6); //FORWARDING_STA(MAC Address of the EV HLE)
  2408. SendMmePacketSize += 6;
  2409. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00): 0x00(PEV-EVSE Association), 0x01-0xFF(Reserved)
  2410. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00): 0x00(No Security), 0x01(Public Key Signature), 0x02-0xFF(Reserved)
  2411. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, SlacRunId, SLAC_RUNID_LENGTH); //RunID (8-byte)
  2412. SendMmePacketSize += SLAC_RUNID_LENGTH;
  2413. SendMmePacketSize += 19; //the size before MMENTRY
  2414. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2415. {
  2416. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response MME Packet *****\n");
  2417. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2418. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  2419. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2420. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  2421. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  2422. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  2423. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  2424. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  2425. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_PARM_CNF ---\n");
  2426. DEBUG_PRINTF_EVCOMM_DETAIL("M-SOUND_TARGET: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2427. SendMmePacket.MMENTRY[0],SendMmePacket.MMENTRY[1],SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],
  2428. SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5]);
  2429. DEBUG_PRINTF_EVCOMM_DETAIL("NUM_SOUNDS: 0x%x\n", SendMmePacket.MMENTRY[6]);
  2430. DEBUG_PRINTF_EVCOMM_DETAIL("Time_Out: 0x%x\n", SendMmePacket.MMENTRY[7]);
  2431. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_TYPE: 0x%x\n", SendMmePacket.MMENTRY[8]);
  2432. DEBUG_PRINTF_EVCOMM_DETAIL("M-FORWARDING_STA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2433. SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],SendMmePacket.MMENTRY[12],
  2434. SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14]);
  2435. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[15]);
  2436. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[16]);
  2437. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2438. SendMmePacket.MMENTRY[17],SendMmePacket.MMENTRY[18],SendMmePacket.MMENTRY[19],SendMmePacket.MMENTRY[20],
  2439. SendMmePacket.MMENTRY[21],SendMmePacket.MMENTRY[22],SendMmePacket.MMENTRY[23],SendMmePacket.MMENTRY[24]);
  2440. }
  2441. #endif
  2442. Update_V2G_Flow_Status(CM_SLAC_PARM_CONF);
  2443. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  2444. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  2445. ftime(&SeqStartTime);
  2446. break;
  2447. }
  2448. #else
  2449. {
  2450. memcpy(EvMac, MmePacket->OSA, sizeof(EvMac));
  2451. memcpy(DestSocketAddress.sll_addr, MmePacket->OSA, sizeof(EvMac));
  2452. memcpy(SlacRunId,MmePacket->MMENTRY + 2, sizeof(SlacRunId));
  2453. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  2454. memcpy(SendMmePacket.ODA, MmePacket->OSA, 6);
  2455. memcpy(SendMmePacket.OSA, CsuMac, 6);
  2456. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  2457. SendMmePacket.MMV = MmePacket->MMV;
  2458. SendMmePacket.MMTYPE = MMTYPE_CM_SLAC_PARM_CNF;
  2459. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  2460. SendMmePacketSize = 0;
  2461. memset(SendMmePacket.MMENTRY, 0xFF, 6); //M-SOUND_TARGET(6-byte:0xFFFFFFFFFFFF): Fixed value indicating that M-Sounds to be sent as Ethernet broadcast
  2462. SendMmePacketSize += 6;
  2463. 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
  2464. SendMmePacket.MMENTRY[SendMmePacketSize++] = 6; //Time_Out(0x06): unit = 100ms
  2465. 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
  2466. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac, 6); //FORWARDING_STA(MAC Address of the EV HLE)
  2467. SendMmePacketSize += 6;
  2468. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00): 0x00(PEV-EVSE Association), 0x01-0xFF(Reserved)
  2469. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00): 0x00(No Security), 0x01(Public Key Signature), 0x02-0xFF(Reserved)
  2470. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, SlacRunId, sizeof(SlacRunId)); //RunID (8-byte)
  2471. SendMmePacketSize += sizeof(SlacRunId);
  2472. SendMmePacketSize += 19; //the size before MMENTRY
  2473. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2474. {
  2475. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response MME Packet *****\n");
  2476. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2477. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  2478. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2479. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  2480. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  2481. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  2482. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  2483. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  2484. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_PARM_CNF ---\n");
  2485. DEBUG_PRINTF_EVCOMM_DETAIL("M-SOUND_TARGET: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2486. SendMmePacket.MMENTRY[0],SendMmePacket.MMENTRY[1],SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],
  2487. SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5]);
  2488. DEBUG_PRINTF_EVCOMM_DETAIL("NUM_SOUNDS: 0x%x\n", SendMmePacket.MMENTRY[6]);
  2489. DEBUG_PRINTF_EVCOMM_DETAIL("Time_Out: 0x%x\n", SendMmePacket.MMENTRY[7]);
  2490. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_TYPE: 0x%x\n", SendMmePacket.MMENTRY[8]);
  2491. DEBUG_PRINTF_EVCOMM_DETAIL("M-FORWARDING_STA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2492. SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],SendMmePacket.MMENTRY[12],
  2493. SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14]);
  2494. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[15]);
  2495. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[16]);
  2496. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2497. SendMmePacket.MMENTRY[17],SendMmePacket.MMENTRY[18],SendMmePacket.MMENTRY[19],SendMmePacket.MMENTRY[20],
  2498. SendMmePacket.MMENTRY[21],SendMmePacket.MMENTRY[22],SendMmePacket.MMENTRY[23],SendMmePacket.MMENTRY[24]);
  2499. }
  2500. #endif
  2501. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  2502. Update_V2G_Flow_Status(CM_SLAC_PARM_CONF);
  2503. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  2504. ftime(&SeqStartTime);
  2505. counter = 0;
  2506. break;
  2507. }
  2508. #endif
  2509. }
  2510. case MMTYPE_CM_START_ATTEN_CHAR_IND:
  2511. {
  2512. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2513. {
  2514. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[WARNING][SLAC][Rx]CM_START_ATTEN_CHAR_IND: ignored(timeup)");
  2515. break;
  2516. }
  2517. //Avoid Coupled CM_START_ATTEN_CHAR_IND
  2518. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2519. {
  2520. sprintf((char*)buf_log_evcomm,
  2521. "[WARNING][SLAC][Rx]CM_START_ATTEN_CHAR_IND: ignored(coupled SLAC,%d)",
  2522. CheckConnectorPlugIn());
  2523. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2524. break;
  2525. }
  2526. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2527. {
  2528. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_START_ATTEN_CHAR_IND (counter : %d/3 ) ---\n",counter + 1);
  2529. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2530. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2531. DEBUG_PRINTF_EVCOMM_DETAIL("NUM_SOUNDS: 0x%x\n", MmePacket->MMENTRY[2]);
  2532. DEBUG_PRINTF_EVCOMM_DETAIL("Time_Out 0x%x\n", MmePacket->MMENTRY[3]);
  2533. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_TYPE 0x%x\n", MmePacket->MMENTRY[4]); //Fixed value (0x01) indicating ��other Green PHY station��
  2534. DEBUG_PRINTF_EVCOMM_DETAIL("FORWARDING_STA: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2535. MmePacket->MMENTRY[5],MmePacket->MMENTRY[6],MmePacket->MMENTRY[7],MmePacket->MMENTRY[8],
  2536. MmePacket->MMENTRY[9],MmePacket->MMENTRY[10]);
  2537. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2538. MmePacket->MMENTRY[11],MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],MmePacket->MMENTRY[14],
  2539. MmePacket->MMENTRY[15],MmePacket->MMENTRY[16],MmePacket->MMENTRY[17],MmePacket->MMENTRY[18]);
  2540. }
  2541. #endif
  2542. //New SLAC architecture designed by Joseph
  2543. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2544. {
  2545. EvMac_in = &MmePacket->OSA[0];
  2546. RunID_in = &MmePacket->MMENTRY[11];
  2547. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2548. if (idx >= 0)
  2549. {
  2550. Update_V2G_Flow_Status(CM_START_ATTEN_CHAR_IND);
  2551. //[TC_SECC_VTB_AttenuationCharacterization_013]
  2552. if (MmePacket->MMENTRY[0] != 0) //APPLICATION_TYPE must be 0x00(EV-EVSE Matching)
  2553. {
  2554. sprintf((char*)buf_log_evcomm,
  2555. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]APPLICATION_TYPE(%d): invalid => ignore Req",
  2556. MmePacket->MMENTRY[0]);
  2557. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2558. break;
  2559. }
  2560. //[TC_SECC_VTB_AttenuationCharacterization_014]
  2561. if (MmePacket->MMENTRY[1] != 0) //SECURITY_TYPE must be 0x00(No Security)
  2562. {
  2563. sprintf((char*)buf_log_evcomm,
  2564. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]SECURITY_TYPE(%d): invalid => ignore Req",
  2565. MmePacket->MMENTRY[1]);
  2566. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2567. break;
  2568. }
  2569. //[TC_SECC_VTB_AttenuationCharacterization_017]
  2570. if (MmePacket->MMENTRY[4] != 0x01) //RESP_TYPE must be 0x01(Send to another GP STA(EV))
  2571. {
  2572. sprintf((char*)buf_log_evcomm,
  2573. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]RESP_TYPE(%d): invalid => ignore Req",
  2574. MmePacket->MMENTRY[4]);
  2575. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2576. break;
  2577. }
  2578. //[TC_SECC_VTB_AttenuationCharacterization_021]
  2579. //FORWARDING_STA: MAC Address of the EV HLE (station which the measurement results shall be sent to)
  2580. if (Array_Compare_Identity(EvMac_in, &MmePacket->MMENTRY[5], SLAC_EVMAC_LENGTH) == FALSE)
  2581. {
  2582. sprintf((char*)buf_log_evcomm,
  2583. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]FORWARDING_STA(%02X:%02X:%02X:%02X:%02X:%02X): invalid => ignore Req",
  2584. MmePacket->MMENTRY[5], MmePacket->MMENTRY[6],
  2585. MmePacket->MMENTRY[7], MmePacket->MMENTRY[8],
  2586. MmePacket->MMENTRY[9], MmePacket->MMENTRY[10]);
  2587. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2588. SLAC_INFO.array[idx].StartAttenCharErr = TRUE;
  2589. break;
  2590. }
  2591. //Check RunID
  2592. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE)
  2593. {
  2594. SLAC_INFO.array[idx].MnbcSoundNum = MmePacket->MMENTRY[2];
  2595. SLAC_INFO.array[idx].StartAttenCharCnt++;
  2596. #if 0
  2597. sprintf((char*)buf_log_evcomm,
  2598. "[SLAC][Rx]CM_START_ATTEN_CHAR_IND[%d]:%d-th",
  2599. (idx + 1),
  2600. SLAC_INFO.array[idx].StartAttenCharCnt);
  2601. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2602. #endif
  2603. if (SLAC_INFO.array[idx].StartAttenCharCnt == 1)
  2604. {
  2605. memset(SLAC_INFO.array[idx].AAG, 0, sizeof(SLAC_INFO.array[idx].AAG));
  2606. SLAC_INFO.array[idx].AttenProfileCnt = 0;
  2607. ftime(&SeqStartTime); //start TT_EVSE_match_MNBC
  2608. }
  2609. else if (SLAC_INFO.array[idx].StartAttenCharCnt >= 3)
  2610. {
  2611. sprintf((char*)buf_log_evcomm,
  2612. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]counter(%d): unexpected",
  2613. SLAC_INFO.array[idx].StartAttenCharCnt);
  2614. }
  2615. else
  2616. {
  2617. //null
  2618. }
  2619. }
  2620. else
  2621. {
  2622. //This RunID is not matched with this EvMac,
  2623. //or this RunID is not found in DB.
  2624. sprintf((char*)buf_log_evcomm,
  2625. "[WARNING][SLAC][Rx][CM_START_ATTEN_CHAR_IND]EvMac-RunID: unmatched => ignore Req");
  2626. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2627. //Response: ignore
  2628. }
  2629. }
  2630. else
  2631. {
  2632. //this msg source is not in database
  2633. //ignore
  2634. }
  2635. break;
  2636. }
  2637. #else //Old SLAC architecture designed by Vern
  2638. {
  2639. MnbcSoundNum = MmePacket->MMENTRY[2];
  2640. Update_V2G_Flow_Status(CM_START_ATTEN_CHAR_IND);
  2641. counter++;
  2642. if(counter == 1)
  2643. {
  2644. memset(Aag, 0, sizeof(Aag));
  2645. AttenProfileCnt = 0;
  2646. ftime(&SeqStartTime); //start TT_EVSE_match_MNBC
  2647. }
  2648. else if(counter >= 3)
  2649. {
  2650. counter = 0;
  2651. }
  2652. break;
  2653. }
  2654. #endif
  2655. }
  2656. case MMTYPE_CM_MNBC_SOUND_IND:
  2657. {
  2658. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2659. {
  2660. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND: ignored(timeup)");
  2661. break;
  2662. }
  2663. //Avoid Coupled CM_MNBC_SOUND_IND
  2664. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2665. {
  2666. sprintf((char*)buf_log_evcomm,
  2667. "[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND: ignored(coupled SLAC,%d)",
  2668. CheckConnectorPlugIn());
  2669. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2670. break;
  2671. }
  2672. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == TRUE)
  2673. {
  2674. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_MNBC_SOUND_IND (counter : %d/%d) ---\n",counter + 1 , MnbcSoundNum);
  2675. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2676. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2677. DEBUG_PRINTF_EVCOMM_DETAIL("SenderID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2678. MmePacket->MMENTRY[2],MmePacket->MMENTRY[3],MmePacket->MMENTRY[4],MmePacket->MMENTRY[5],
  2679. MmePacket->MMENTRY[6],MmePacket->MMENTRY[7],MmePacket->MMENTRY[8],MmePacket->MMENTRY[9],
  2680. MmePacket->MMENTRY[10],MmePacket->MMENTRY[11],MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],
  2681. MmePacket->MMENTRY[14],MmePacket->MMENTRY[15],MmePacket->MMENTRY[16],MmePacket->MMENTRY[17],
  2682. MmePacket->MMENTRY[18]);
  2683. DEBUG_PRINTF_EVCOMM_DETAIL("Cnt: 0x%x\n", MmePacket->MMENTRY[19]);
  2684. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2685. MmePacket->MMENTRY[20],MmePacket->MMENTRY[21],MmePacket->MMENTRY[22],MmePacket->MMENTRY[23],
  2686. MmePacket->MMENTRY[24],MmePacket->MMENTRY[25],MmePacket->MMENTRY[26],MmePacket->MMENTRY[27]);
  2687. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2688. MmePacket->MMENTRY[28],MmePacket->MMENTRY[29],MmePacket->MMENTRY[30],MmePacket->MMENTRY[31],
  2689. MmePacket->MMENTRY[32],MmePacket->MMENTRY[33],MmePacket->MMENTRY[34],MmePacket->MMENTRY[35]);
  2690. DEBUG_PRINTF_EVCOMM_DETAIL("Rnd: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2691. MmePacket->MMENTRY[36],MmePacket->MMENTRY[37],MmePacket->MMENTRY[38],MmePacket->MMENTRY[39],
  2692. MmePacket->MMENTRY[40],MmePacket->MMENTRY[41],MmePacket->MMENTRY[42],MmePacket->MMENTRY[43],
  2693. MmePacket->MMENTRY[44],MmePacket->MMENTRY[45],MmePacket->MMENTRY[46],MmePacket->MMENTRY[47],
  2694. MmePacket->MMENTRY[48],MmePacket->MMENTRY[49],MmePacket->MMENTRY[50],MmePacket->MMENTRY[51]);
  2695. }
  2696. #endif
  2697. //New SLAC architecture designed by Joseph
  2698. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2699. {
  2700. EvMac_in = &MmePacket->OSA[0];
  2701. RunID_in = &MmePacket->MMENTRY[20];
  2702. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2703. if (idx >= 0)
  2704. {
  2705. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2706. //Check for RunID
  2707. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE)
  2708. {
  2709. SLAC_INFO.array[idx].MnbcSoundCnt++;
  2710. /*
  2711. sprintf((char*)buf_log_evcomm,
  2712. "[SLAC][Rx]CM_MNBC_SOUND_IND[%d]:%d-th",
  2713. (idx + 1),
  2714. SLAC_INFO.array[idx].MnbcSoundCnt);
  2715. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2716. */
  2717. }
  2718. else
  2719. {
  2720. //RunID is not matched or does not exist.
  2721. sprintf((char*)buf_log_evcomm,
  2722. "[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND]EvMac-RunID: unmatched");
  2723. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2724. }
  2725. }
  2726. else
  2727. {
  2728. //ignore
  2729. sprintf((char*)buf_log_evcomm,
  2730. "[WARNING][SLAC][Rx]CM_MNBC_SOUND_IND]EvMac does not exist");
  2731. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2732. }
  2733. break;
  2734. }
  2735. #else //Old SLAC architecture designed by Vern
  2736. {
  2737. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2738. counter++;
  2739. break;
  2740. }
  2741. #endif
  2742. }
  2743. case MMTYPE_CM_ATTEN_PROFILE_IND:
  2744. {
  2745. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2746. {
  2747. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[WARNING][SLAC][Rx]CM_ATTEN_PROFILE_IND: ignore(timeup)");
  2748. break;
  2749. }
  2750. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == TRUE)
  2751. {
  2752. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_ATTEN_PROFILE_IND (counter : %d/%d) ---\n", counter, MnbcSoundNum);
  2753. DEBUG_PRINTF_EVCOMM_DETAIL("EV MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2754. MmePacket->MMENTRY[0], MmePacket->MMENTRY[1], MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2755. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5]);
  2756. DEBUG_PRINTF_EVCOMM_DETAIL("NumGroups: 0x%x\n", MmePacket->MMENTRY[6]); //NumGroups, Number of AAG Groups (ex: 0x3A = 58)
  2757. //NumGroups: Number of OFDM carrier groups used for the SLAC signal characterization.
  2758. //AAG[i]: Average Attenuation of Group i (i = 1 ~ 58)
  2759. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: 0x%x\n", MmePacket->MMENTRY[7]);
  2760. DEBUG_PRINTF_EVCOMM_DETAIL("AAG: \n");
  2761. for(Rtn = 0; Rtn < MmePacket->MMENTRY[6]; Rtn++)
  2762. {
  2763. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[8 + Rtn]);
  2764. }
  2765. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  2766. }
  2767. #endif
  2768. //New SLAC architecture designed by Joseph
  2769. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2770. {
  2771. EvMac_in = &MmePacket->MMENTRY[0]; //[CAUTION] The EvMac address is not &MmePacket->OSA[0]
  2772. //[CAUTION] There is no RunID information of CM_ATTEN_PROFILE_IND
  2773. // packet, which means SECC cannot use the RunID to
  2774. // distinguish those SLAC request with identical EvMac
  2775. // but with different RunID.
  2776. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2777. /*
  2778. printf("%d, %d, %d, %d\n",
  2779. idx,
  2780. SLAC_INFO.array[idx].AagGroupsNum,
  2781. MmePacket->MMENTRY[6],
  2782. SLAC_INFO.array[idx].AttenProfileCnt);
  2783. */
  2784. if (idx >= 0)
  2785. {
  2786. SLAC_INFO.array[idx].AttenProfileCnt++;
  2787. /*
  2788. sprintf((char*)buf_log_evcomm,
  2789. "[SLAC][Rx]CM_ATTEN_PROFILE_IND[%d]:%d-th",
  2790. (idx + 1),
  2791. SLAC_INFO.array[idx].AttenProfileCnt);
  2792. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2793. */
  2794. //printf("%d, %d\n", SLAC_INFO.array[idx].AagGroupsNum, MmePacket->MMENTRY[6]);
  2795. SLAC_INFO.array[idx].AagGroupsNum = MmePacket->MMENTRY[6];
  2796. ////NumGroups, Number of AAG Groups (ex: 0x3A = 58)
  2797. for(Rtn = 0; Rtn < SLAC_INFO.array[idx].AagGroupsNum; Rtn++) //ex:58
  2798. {
  2799. SLAC_INFO.array[idx].AAG[Rtn] += MmePacket->MMENTRY[8 + Rtn];
  2800. }
  2801. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2802. break;
  2803. }
  2804. else
  2805. {
  2806. //The EvMac is not in the database
  2807. //ignore
  2808. sprintf((char*)buf_log_evcomm,
  2809. "[SLAC][Rx][CM_ATTEN_PROFILE_IND]EvMac(%02X%02X%02X%02X%02X%02X): not exist => ignore",
  2810. EvMac_in[0], EvMac_in[1], EvMac_in[2], EvMac_in[3], EvMac_in[4], EvMac_in[5]);
  2811. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2812. break;
  2813. }
  2814. }
  2815. #else //Old SLAC architecture designed by Vern
  2816. {
  2817. AagGroupsNum = MmePacket->MMENTRY[6];
  2818. for(Rtn = 0; Rtn < MmePacket->MMENTRY[6]; Rtn++)
  2819. {
  2820. Aag[Rtn] += MmePacket->MMENTRY[8 + Rtn];
  2821. }
  2822. AttenProfileCnt++;
  2823. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2824. break;
  2825. }
  2826. #endif
  2827. }
  2828. case MMTYPE_CM_ATTEN_CHAR_RSP:
  2829. {
  2830. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2831. {
  2832. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_ATTEN_CHAR_RSP ---\n");
  2833. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2834. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2835. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2836. MmePacket->MMENTRY[2],MmePacket->MMENTRY[3],MmePacket->MMENTRY[4],MmePacket->MMENTRY[5],
  2837. MmePacket->MMENTRY[6],MmePacket->MMENTRY[7]);
  2838. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2839. MmePacket->MMENTRY[8],MmePacket->MMENTRY[9],MmePacket->MMENTRY[10],MmePacket->MMENTRY[11],
  2840. MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],MmePacket->MMENTRY[14],MmePacket->MMENTRY[15]);
  2841. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ID: \n");
  2842. for(Rtn = 0; Rtn < 17; Rtn++)
  2843. {
  2844. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[16 + Rtn]);
  2845. }
  2846. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  2847. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_ID: \n");
  2848. for(Rtn = 0; Rtn < 17; Rtn++)
  2849. {
  2850. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[33 + Rtn]);
  2851. }
  2852. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  2853. DEBUG_PRINTF_EVCOMM_DETAIL("Result: 0x%x\n", MmePacket->MMENTRY[50]); //Fixed value of 0x00 indicates a successful SLAC process
  2854. }
  2855. #endif
  2856. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2857. {
  2858. //Check ODA (Destination Address)
  2859. if (Array_Compare_Identity(CsuMac, MmePacket->ODA, SLAC_EVSE_MAC_LENGTH) == FALSE)
  2860. {
  2861. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx][CM_ATTEN_CHAR_RSP]wrong ODA: ignore");
  2862. break;
  2863. }
  2864. EvMac_in = &MmePacket->OSA[0];
  2865. RunID_in = &MmePacket->MMENTRY[8];
  2866. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2867. //Check Parameters
  2868. if ((MmePacket->MMENTRY[0] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_005] applicationType must be 0x00
  2869. (MmePacket->MMENTRY[1] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_006] securityType must be 0x00
  2870. (SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in) < 0) || //[TC_SECC_VTB_AttenuationCharacterization_007] sourceAddress cannot not be all zero
  2871. (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_008] check for invalid runID
  2872. (Array_Check_All_Zero(&MmePacket->MMENTRY[16], 17) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_009] SourceID(17 bytes) must be all zero.
  2873. (Array_Check_All_Zero(&MmePacket->MMENTRY[33], 17) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_009] RESP_ID (17 bytes) must be all zero.
  2874. (MmePacket->MMENTRY[50] != 0) //Result: must be 0x00(Success)
  2875. )
  2876. {
  2877. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry++;
  2878. sprintf((char*)buf_log_evcomm,
  2879. "[WARNING][SLAC][Rx]CM_ATTEN_CHAR_RSP:invalid para(%d,%d,%d,%d,%d) => %d-th retry",
  2880. MmePacket->MMENTRY[0], //applicationType must be 0x00
  2881. MmePacket->MMENTRY[1], //securityType must be 0x00
  2882. SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in),
  2883. SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in),
  2884. MmePacket->MMENTRY[50],
  2885. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry
  2886. );
  2887. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2888. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry <= 2) //limited to 2 retries
  2889. {
  2890. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2891. ftime(&SeqStartTime);
  2892. break;
  2893. }
  2894. else
  2895. {
  2896. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR][SLAC][Rx]CM_ATTEN_CHAR_RSP:invalid para => 2-retry fail => End_Process");
  2897. Update_V2G_Flow_Status(Other_Fault);
  2898. break;
  2899. }
  2900. }
  2901. else
  2902. {
  2903. //The CM_ATTEN_CHAR_IND is legal
  2904. SLAC_INFO.array[idx].AttenCharRspCnt++;
  2905. sprintf((char*)buf_log_evcomm,
  2906. "[SLAC][Rx]CM_ATTEN_CHAR_RSP[%d]:%d-th",
  2907. (idx + 1),
  2908. SLAC_INFO.array[idx].AttenCharRspCnt);
  2909. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2910. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry = 0;
  2911. Update_V2G_Flow_Status(CM_ATTEN_CHAR_RSP);
  2912. ftime(&SeqStartTime);
  2913. break;
  2914. }
  2915. }
  2916. #else
  2917. {
  2918. //Check Parameters
  2919. if ((MmePacket->MMENTRY[0] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_005] applicationType must be 0x00
  2920. (MmePacket->MMENTRY[1] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_006] securityType must be 0x00
  2921. (Array_Compare_Identity(SLAC_INFO.array[0].EvMac, &MmePacket->MMENTRY[2], 6) == TRUE) || //[TC_SECC_VTB_AttenuationCharacterization_007] sourceAddress cannot not be all zero
  2922. (Array_Compare_Identity(SLAC_INFO.array[0].RunID, &MmePacket->MMENTRY[8], 8) == TRUE) //[TC_SECC_VTB_AttenuationCharacterization_008] check for invalid runID
  2923. )
  2924. {
  2925. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry++;
  2926. sprintf((char*)buf_log_evcomm,
  2927. "[WARNING][SLAC][Rx]CM_ATTEN_CHAR_RSP: invalid parameters(%d-th)",
  2928. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry);
  2929. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2930. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry <= 2) //limited to 2 retries
  2931. {
  2932. Update_V2G_Flow_Status(CM_MNBC_SOUND_IND);
  2933. ftime(&SeqStartTime);
  2934. break;
  2935. }
  2936. else
  2937. {
  2938. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]CM_ATTEN_CHAR_RSP: invalid parameters => End_Process");
  2939. Update_V2G_Flow_Status(Other_Fault);
  2940. ftime(&SeqStartTime);
  2941. break;
  2942. }
  2943. }
  2944. else
  2945. {
  2946. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry = 0;
  2947. Update_V2G_Flow_Status(CM_ATTEN_CHAR_RSP);
  2948. ftime(&SeqStartTime);
  2949. break;
  2950. }
  2951. }
  2952. #endif
  2953. }
  2954. case MMTYPE_CM_VALIDATE_REQ: //BCB Toggle
  2955. {
  2956. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  2957. {
  2958. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_VALIDATE_REQ ---\n");
  2959. DEBUG_PRINTF_EVCOMM_DETAIL("Signal Type: 0x%x\n", MmePacket->MMENTRY[0]); //Fixed value (0x00) to indicate ��PEV S2 toggles on control pilot line��
  2960. 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.
  2961. 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��.
  2962. }
  2963. #endif
  2964. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  2965. {
  2966. counter = 0;
  2967. EvMac_in = &MmePacket->OSA[0];
  2968. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2969. if (idx >= 0)
  2970. {
  2971. Update_V2G_Flow_Status(CM_VALIDATE_CNF);
  2972. SLAC_INFO.array[idx].ValidateReqCnt++;
  2973. sprintf((char*)buf_log_evcomm,
  2974. "[SLAC][Rx]CM_VALIDATE_REQ[%d]:%d-th",
  2975. (idx + 1),
  2976. SLAC_INFO.array[idx].ValidateReqCnt);
  2977. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  2978. //[To-Do] Protection
  2979. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  2980. memcpy(SendMmePacket.ODA, EvMac_in, SLAC_EVMAC_LENGTH);
  2981. memcpy(SendMmePacket.OSA, CsuMac, 6);
  2982. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  2983. SendMmePacket.MMV = 0x01;
  2984. SendMmePacket.MMTYPE = MMTYPE_CM_VALIDATE_CNF;
  2985. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  2986. SendMmePacketSize = 0;
  2987. if(counter == 0)
  2988. {
  2989. //The First MMTYPE_CM_VALIDATE_REQ because Unicast
  2990. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SignalType(0x00): Fixed value to indicate "EV S2 toggles on control pilot line"
  2991. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //ToggleNum(0x00): Fixed value. In the first VALIDATE Request-Response exchange, the ToggleNum field shall be set to zero.
  2992. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  2993. SendMmePacket.MMENTRY[SendMmePacketSize++] = 1; //0x01 = Ready
  2994. #else
  2995. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  2996. #endif
  2997. }
  2998. else
  2999. {
  3000. //The Second MMTYPE_CM_VALIDATE_REQ because Broadcast
  3001. unsigned char PreStatus = 3;
  3002. unsigned char ToggleNum = 0;
  3003. ftime(&SeqStartTime);
  3004. while(1)
  3005. {
  3006. if((EVCOMM_SYS_INFO.CpState == 4) && (PreStatus == 3))
  3007. {
  3008. ToggleNum++;
  3009. PreStatus = 4;
  3010. }
  3011. else
  3012. {
  3013. PreStatus = 3;
  3014. }
  3015. ftime(&SeqEndTime);
  3016. if(DiffTimeb(SeqStartTime, SeqEndTime) >= (SendMmePacket.MMENTRY[1]*100 + 100))
  3017. {
  3018. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value to indicate "PEV S2 toggles on control pilot line"
  3019. SendMmePacket.MMENTRY[SendMmePacketSize++] = ToggleNum;
  3020. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  3021. SendMmePacket.MMENTRY[SendMmePacketSize++] = 2; //0x02 = Success
  3022. #else
  3023. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  3024. #endif
  3025. break;
  3026. }
  3027. }
  3028. }
  3029. SendMmePacketSize += 19; //the size before MMENTRY
  3030. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3031. ftime(&SeqStartTime);
  3032. }
  3033. else
  3034. {
  3035. //EvMac does not exist.
  3036. //ignore
  3037. }
  3038. break;
  3039. }
  3040. #else
  3041. {
  3042. counter = 0;
  3043. for(Rtn = 0; Rtn < 6; Rtn++)
  3044. {
  3045. if(MmePacket->ODA[Rtn] != CsuMac[Rtn])
  3046. {
  3047. counter = 1;
  3048. break;
  3049. }
  3050. }
  3051. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  3052. memcpy(SendMmePacket.ODA, EvMac,6);
  3053. memcpy(SendMmePacket.OSA, CsuMac,6);
  3054. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  3055. SendMmePacket.MMV = 0x01;
  3056. SendMmePacket.MMTYPE = MMTYPE_CM_VALIDATE_CNF;
  3057. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  3058. SendMmePacketSize = 0;
  3059. if(counter == 0)
  3060. {
  3061. //The First MMTYPE_CM_VALIDATE_REQ because Unicast
  3062. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value to indicate ��PEV S2 toggles on control pilot line��
  3063. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value In the first VALIDATE Request-Response exchange, the ToggleNum field shall be set to zero.
  3064. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  3065. SendMmePacket.MMENTRY[SendMmePacketSize++] = 1; //0x01 = Ready
  3066. #else
  3067. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  3068. #endif
  3069. }
  3070. else
  3071. {
  3072. //The Second MMTYPE_CM_VALIDATE_REQ because Broadcast
  3073. unsigned char PreStatus = 3, ToggleNum = 0;
  3074. ftime(&SeqStartTime);
  3075. while(1)
  3076. {
  3077. ftime(&SeqEndTime);
  3078. if((EVCOMM_SYS_INFO.CpState == 4) && (PreStatus == 3))
  3079. {
  3080. ToggleNum++;
  3081. PreStatus = 4;
  3082. }
  3083. else
  3084. {
  3085. PreStatus = 3;
  3086. }
  3087. if(DiffTimeb(SeqStartTime, SeqEndTime) >= (SendMmePacket.MMENTRY[1]*100 + 100))
  3088. {
  3089. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //Fixed value to indicate ��PEV S2 toggles on control pilot line��
  3090. SendMmePacket.MMENTRY[SendMmePacketSize++] = ToggleNum;
  3091. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  3092. SendMmePacket.MMENTRY[SendMmePacketSize++] = 2; //0x02 = Success
  3093. #else
  3094. SendMmePacket.MMENTRY[SendMmePacketSize++] = 4; //0x04 = Not Required
  3095. #endif
  3096. break;
  3097. }
  3098. }
  3099. }
  3100. SendMmePacketSize += 19; //the size before MMENTRY
  3101. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3102. Update_V2G_Flow_Status(CM_VALIDATE_CNF);
  3103. ftime(&SeqStartTime);
  3104. break;
  3105. }
  3106. #endif
  3107. }
  3108. case MMTYPE_CM_SLAC_MATCH_REQ:
  3109. {
  3110. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  3111. {
  3112. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SLAC_MATCH_REQ ---\n");
  3113. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  3114. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  3115. DEBUG_PRINTF_EVCOMM_DETAIL("MVFLength: 0x%x, 0x%x\n", MmePacket->MMENTRY[2],MmePacket->MMENTRY[3]); //Fixed value (0x3E) for matching
  3116. DEBUG_PRINTF_EVCOMM_DETAIL("PEV ID: \n");
  3117. for(Rtn=0; Rtn<17; Rtn++)
  3118. {
  3119. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[4+Rtn]);
  3120. }
  3121. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3122. DEBUG_PRINTF_EVCOMM_DETAIL("PEV MAC: \n");
  3123. for(Rtn=0; Rtn<6; Rtn++)
  3124. {
  3125. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[21+Rtn]);
  3126. }
  3127. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3128. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE ID: \n");
  3129. for(Rtn=0; Rtn<17; Rtn++)
  3130. {
  3131. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[27+Rtn]);
  3132. }
  3133. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3134. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE MAC: \n");
  3135. for(Rtn=0; Rtn<6; Rtn++)
  3136. {
  3137. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[44+Rtn]);
  3138. }
  3139. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3140. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: \n");
  3141. for(Rtn=0; Rtn<8; Rtn++)
  3142. {
  3143. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[50+Rtn]);
  3144. }
  3145. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3146. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: \n");
  3147. for(Rtn=0; Rtn<8; Rtn++)
  3148. {
  3149. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",MmePacket->MMENTRY[58+Rtn]);
  3150. }
  3151. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3152. }
  3153. #endif
  3154. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  3155. {
  3156. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx]CM_SLAC_MATCH_REQ");
  3157. //Check ODA (Destination Address)
  3158. if (Array_Compare_Identity(CsuMac, MmePacket->ODA, SLAC_EVSE_MAC_LENGTH) == FALSE)
  3159. {
  3160. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx][CM_SLAC_MATCH_REQ]wrong ODA: ignore");
  3161. //break; //patch for DEKRA I2SE EVCC (another EVSE MAC Address protection comes as below.)
  3162. }
  3163. EvMac_in = &MmePacket->OSA[0];
  3164. RunID_in = &MmePacket->MMENTRY[50];
  3165. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  3166. if (idx >= 0)
  3167. {
  3168. Update_V2G_Flow_Status(CM_SLAC_MATCH_REQ);
  3169. SLAC_INFO.array[idx].MatchReqNum++;
  3170. sprintf((char*)buf_log_evcomm,
  3171. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:%d-th",
  3172. (idx + 1),
  3173. SLAC_INFO.array[idx].MatchReqNum);
  3174. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3175. //[TC_SECC_VTB_CmSlacMatch_007] APPLICATION_TYPE must be 0x00(EV-EVSE Matching)
  3176. //[TC_SECC_VTB_CmSlacMatch_008]
  3177. if (MmePacket->MMENTRY[0] != 0)
  3178. {
  3179. sprintf((char*)buf_log_evcomm,
  3180. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:wrong APPLICATION_TYPE(%d)",
  3181. (idx + 1),
  3182. MmePacket->MMENTRY[0]);
  3183. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3184. break;
  3185. }
  3186. //[TC_SECC_VTB_CmSlacMatch_009] SECURITY_TYPE must be 0x00(No Security)
  3187. //[TC_SECC_VTB_CmSlacMatch_010]
  3188. if (MmePacket->MMENTRY[1] != 0)
  3189. {
  3190. sprintf((char*)buf_log_evcomm,
  3191. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:wrong SECURITY_TYPE(%d)",
  3192. (idx + 1),
  3193. MmePacket->MMENTRY[1]);
  3194. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3195. break;
  3196. }
  3197. //[TC_SECC_VTB_CmSlacMatch_011] MVFLength must be 0x3E(MatchVarField Length)
  3198. //[TC_SECC_VTB_CmSlacMatch_012]
  3199. if (MmePacket->MMENTRY[2] != 0x3E || MmePacket->MMENTRY[3] != 0x00)
  3200. {
  3201. sprintf((char*)buf_log_evcomm,
  3202. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]:wrong MVFLength(%d,%d)",
  3203. (idx + 1),
  3204. MmePacket->MMENTRY[2],
  3205. MmePacket->MMENTRY[3]);
  3206. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3207. break;
  3208. }
  3209. //[TC_SECC_VTB_CmSlacMatch_013] EV ID (muxt be all zero)
  3210. //[TC_SECC_VTB_CmSlacMatch_014]
  3211. if (Array_Check_All_Zero(&MmePacket->MMENTRY[4], 17) == FALSE)
  3212. {
  3213. sprintf((char*)buf_log_evcomm,
  3214. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EV ID",
  3215. (idx + 1));
  3216. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3217. break;
  3218. }
  3219. //[TC_SECC_VTB_CmSlacMatch_015] EV MAC
  3220. //[TC_SECC_VTB_CmSlacMatch_016]
  3221. if (Array_Compare_Identity(EvMac_in, &MmePacket->MMENTRY[21], SLAC_EVMAC_LENGTH) == FALSE)
  3222. {
  3223. sprintf((char*)buf_log_evcomm,
  3224. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EV MAC(%02X:%02X:%02X:%02X:%02X:%02X)",
  3225. (idx + 1),
  3226. MmePacket->MMENTRY[21], MmePacket->MMENTRY[22], MmePacket->MMENTRY[23],
  3227. MmePacket->MMENTRY[24], MmePacket->MMENTRY[25], MmePacket->MMENTRY[26]);
  3228. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3229. break;
  3230. }
  3231. //[TC_SECC_VTB_CmSlacMatch_017] EVSE ID should be all zero
  3232. //[TC_SECC_VTB_CmSlacMatch_018]
  3233. if (Array_Check_All_Zero(&MmePacket->MMENTRY[27], 17) == FALSE)
  3234. {
  3235. sprintf((char*)buf_log_evcomm,
  3236. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EVSE ID",
  3237. (idx + 1));
  3238. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3239. break;
  3240. }
  3241. //[TC_SECC_VTB_CmSlacMatch_019] EVSE MAC
  3242. //[TC_SECC_VTB_CmSlacMatch_020]
  3243. if (Array_Compare_Identity(CsuMac, &MmePacket->MMENTRY[44], SLAC_EVSE_MAC_LENGTH) == FALSE)
  3244. {
  3245. sprintf((char*)buf_log_evcomm,
  3246. "[SLAC][Rx]CM_SLAC_MATCH_REQ[%d]: wrong EVSE MAC(%02X:%02X:%02X:%02X:%02X:%02X)",
  3247. (idx + 1),
  3248. MmePacket->MMENTRY[44], MmePacket->MMENTRY[45], MmePacket->MMENTRY[46],
  3249. MmePacket->MMENTRY[47], MmePacket->MMENTRY[48], MmePacket->MMENTRY[49]);
  3250. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3251. break;
  3252. }
  3253. //[TC_SECC_VTB_CmSlacMatch_021]runID: Check RunID
  3254. //[TC_SECC_VTB_CmSlacMatch_022]
  3255. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE &&
  3256. (idx == 0)) //only setup a successful link establishment with the first SLAC Req instance
  3257. {
  3258. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  3259. memcpy(SendMmePacket.ODA, MmePacket->OSA, 6);
  3260. memcpy(SendMmePacket.OSA, CsuMac, 6);
  3261. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  3262. SendMmePacket.MMV = MmePacket->MMV;
  3263. SendMmePacket.MMTYPE = MMTYPE_CM_SLAC_MATCH_CNF;
  3264. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  3265. SendMmePacketSize = 0;
  3266. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //APPLICATION_TYPE: Fixed value (0x00: EV-EVSE matching)
  3267. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //SECURITY_TYPE: Fixed value (0x00: No Security)
  3268. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x56; //MVFLength: MatchVarField Length (Fixed value: 0x0056)
  3269. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //MVFLength: MatchVarField Length (Fixed value: 0x0056)
  3270. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //EV ID
  3271. SendMmePacketSize += 17;
  3272. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac_in, SLAC_EVMAC_LENGTH); //EV MAC
  3273. SendMmePacketSize += 6;
  3274. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //EVSE ID
  3275. SendMmePacketSize += 17;
  3276. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, CsuMac, 6); //EVSE MAC
  3277. SendMmePacketSize += 6;
  3278. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, RunID_in, SLAC_RUNID_LENGTH);
  3279. SendMmePacketSize += SLAC_RUNID_LENGTH;
  3280. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 8); //RSVD
  3281. SendMmePacketSize += 8;
  3282. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, Nid, sizeof(Nid)); //NID: Network ID given by the CCo(EVSE)
  3283. SendMmePacketSize += sizeof(Nid); //NID caculated from the random NMK that will be set.
  3284. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0x00; //RSVD
  3285. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, NewNmkKey, sizeof(NewNmkKey)); //NMK: Random value
  3286. SendMmePacketSize += sizeof(NewNmkKey); //NMK: Private NMK of the EVSE (random value)
  3287. SendMmePacketSize += 19; //the size before MMENTRY
  3288. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  3289. {
  3290. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response MME Packet *****\n");
  3291. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3292. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  3293. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3294. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  3295. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  3296. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  3297. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  3298. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  3299. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_MATCH_CNF ---\n");
  3300. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  3301. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  3302. DEBUG_PRINTF_EVCOMM_DETAIL("MVFLength: 0x%x, 0x%x\n", SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3]);
  3303. DEBUG_PRINTF_EVCOMM_DETAIL("PEV ID: \n");
  3304. for(Rtn=0; Rtn<17; Rtn++)
  3305. {
  3306. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[4+Rtn]);
  3307. }
  3308. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3309. DEBUG_PRINTF_EVCOMM_DETAIL("PEV MAC: \n");
  3310. for(Rtn=0; Rtn<6; Rtn++)
  3311. {
  3312. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[21+Rtn]);
  3313. }
  3314. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3315. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE ID: \n");
  3316. for(Rtn=0; Rtn<17; Rtn++)
  3317. {
  3318. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[27+Rtn]);
  3319. }
  3320. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3321. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE MAC: \n");
  3322. for(Rtn=0; Rtn<6; Rtn++)
  3323. {
  3324. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[44+Rtn]);
  3325. }
  3326. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3327. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: \n");
  3328. for(Rtn=0; Rtn<8; Rtn++)
  3329. {
  3330. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[50+Rtn]);
  3331. }
  3332. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3333. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: \n");
  3334. for(Rtn=0; Rtn<8; Rtn++)
  3335. {
  3336. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[58+Rtn]);
  3337. }
  3338. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3339. DEBUG_PRINTF_EVCOMM_DETAIL("NID: \n");
  3340. for(Rtn=0; Rtn<7; Rtn++)
  3341. {
  3342. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[66+Rtn]);
  3343. }
  3344. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3345. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: 0x%x\n", SendMmePacket.MMENTRY[73]);
  3346. DEBUG_PRINTF_EVCOMM_DETAIL("NMK: \n");
  3347. for(Rtn=0; Rtn<16; Rtn++)
  3348. {
  3349. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[74+Rtn]);
  3350. }
  3351. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3352. }
  3353. #endif
  3354. Update_V2G_Flow_Status(CM_SLAC_MATCH_CNF);
  3355. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3356. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  3357. sprintf((char*)buf_log_evcomm,
  3358. "[SLAC][Tx]CM_SLAC_MATCH_CNF[%d]",
  3359. (idx + 1));
  3360. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3361. ftime(&SeqStartTime);
  3362. }
  3363. else
  3364. {
  3365. //RunID does not match and it's not the first SLAC request
  3366. //Reset the SLAC database to embrace SLAC retry
  3367. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CM_SLAC_MATCH_REQ]No Real MATCH_REQ");
  3368. SLAC_DB_Reset();
  3369. }
  3370. }
  3371. else
  3372. {
  3373. //OSA(EvMac) does not exist
  3374. }
  3375. break;
  3376. }
  3377. #else
  3378. {
  3379. Update_V2G_Flow_Status(CM_SLAC_MATCH_REQ);
  3380. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Rx]CM_SLAC_MATCH_REQ");
  3381. memset(&SendMmePacket,0,sizeof(struct MmeHeader));
  3382. memcpy(SendMmePacket.ODA,MmePacket->OSA,6);
  3383. memcpy(SendMmePacket.OSA,CsuMac,6);
  3384. SendMmePacket.MTYPE=htons(EtherType_HomePlug);
  3385. SendMmePacket.MMV=MmePacket->MMV;
  3386. SendMmePacket.MMTYPE=MMTYPE_CM_SLAC_MATCH_CNF;
  3387. SendMmePacket.FMI[0]=SendMmePacket.FMI[1]=0;
  3388. SendMmePacketSize=0;
  3389. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //Fixed value (0x00) indicating ��PEV-EVSE matching��
  3390. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //Fixed value (0x00) indicating ��No Security��
  3391. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x56; //Fixed value (0x0056) for matching
  3392. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //Fixed value (0x0056) for matching
  3393. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,17); //PEV ID
  3394. SendMmePacketSize+=17;
  3395. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,EvMac,6);
  3396. SendMmePacketSize+=6;
  3397. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,17); //EVSE ID
  3398. SendMmePacketSize+=17;
  3399. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,CsuMac,6);
  3400. SendMmePacketSize+=6;
  3401. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,SlacRunId,sizeof(SlacRunId));
  3402. SendMmePacketSize+=sizeof(SlacRunId);
  3403. memset(SendMmePacket.MMENTRY+SendMmePacketSize,0,8); //RSVD
  3404. SendMmePacketSize+=8;
  3405. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,Nid,sizeof(Nid));
  3406. SendMmePacketSize+=sizeof(Nid);
  3407. SendMmePacket.MMENTRY[SendMmePacketSize++]=0x00; //RSVD
  3408. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize,NewNmkKey,sizeof(NewNmkKey));
  3409. SendMmePacketSize+=sizeof(NewNmkKey);
  3410. SendMmePacketSize+=19; //the size before MMENTRY
  3411. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  3412. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response MME Packet *****\n");
  3413. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3414. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  3415. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3416. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  3417. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  3418. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  3419. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  3420. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  3421. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_MATCH_CNF ---\n");
  3422. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  3423. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  3424. DEBUG_PRINTF_EVCOMM_DETAIL("MVFLength: 0x%x, 0x%x\n", SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3]);
  3425. DEBUG_PRINTF_EVCOMM_DETAIL("PEV ID: \n");
  3426. for(Rtn=0; Rtn<17; Rtn++)
  3427. {
  3428. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[4+Rtn]);
  3429. }
  3430. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3431. DEBUG_PRINTF_EVCOMM_DETAIL("PEV MAC: \n");
  3432. for(Rtn=0; Rtn<6; Rtn++)
  3433. {
  3434. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[21+Rtn]);
  3435. }
  3436. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3437. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE ID: \n");
  3438. for(Rtn=0; Rtn<17; Rtn++)
  3439. {
  3440. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[27+Rtn]);
  3441. }
  3442. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3443. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE MAC: \n");
  3444. for(Rtn=0; Rtn<6; Rtn++)
  3445. {
  3446. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[44+Rtn]);
  3447. }
  3448. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3449. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: \n");
  3450. for(Rtn=0; Rtn<8; Rtn++)
  3451. {
  3452. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[50+Rtn]);
  3453. }
  3454. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3455. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: \n");
  3456. for(Rtn=0; Rtn<8; Rtn++)
  3457. {
  3458. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[58+Rtn]);
  3459. }
  3460. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3461. DEBUG_PRINTF_EVCOMM_DETAIL("NID: \n");
  3462. for(Rtn=0; Rtn<7; Rtn++)
  3463. {
  3464. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[66+Rtn]);
  3465. }
  3466. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3467. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: 0x%x\n", SendMmePacket.MMENTRY[73]);
  3468. DEBUG_PRINTF_EVCOMM_DETAIL("NMK: \n");
  3469. for(Rtn=0; Rtn<16; Rtn++)
  3470. {
  3471. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[74+Rtn]);
  3472. }
  3473. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3474. #endif
  3475. Update_V2G_Flow_Status(CM_SLAC_MATCH_CNF);
  3476. Rtn = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3477. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n",SendMmePacketSize,Rtn);
  3478. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[SLAC][Tx]CM_SLAC_MATCH_CNF");
  3479. ftime(&SeqStartTime);
  3480. break;
  3481. }
  3482. #endif
  3483. }
  3484. case MMTYPE_VENDOR_VS_HOST_ACTION:
  3485. {
  3486. struct QcaVendorMmeHeader *RecvPacket;
  3487. RecvPacket = (struct QcaVendorMmeHeader *)Buffer;
  3488. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_VS_HOST_ACTION ---\n");
  3489. switch (RecvPacket->MBODY[0])
  3490. {
  3491. case 0x00:
  3492. //Loader (Device Softloader or Bootloader) ready
  3493. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Loader Ready\n");
  3494. break;
  3495. case 0x01:
  3496. //Firmware Upgrade Ready
  3497. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Firmware Upgrade Ready\n");
  3498. break;
  3499. case 0x02:
  3500. //PIB Update Ready
  3501. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: PIB Update Ready\n");
  3502. break;
  3503. case 0x03:
  3504. //Firmware Upgrade and PIB Update ready
  3505. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Firmware Upgrade and PIB Update ready\n");
  3506. break;
  3507. case 0x04:
  3508. //Loader (Bootloader) ready to receive SDRAM configuration.
  3509. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Loader ready to receive SDRAM configuration\n");
  3510. break;
  3511. case 0x05:
  3512. //Reset to Factory Defaults.
  3513. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Reset to Factory Defaults\n");
  3514. break;
  3515. default:
  3516. //Reserved
  3517. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Reserved\n");
  3518. break;
  3519. }
  3520. break;
  3521. }
  3522. case MMTYPE_VENDOR_ATTEN_CHAR:
  3523. {
  3524. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_ATTEN_CHAR ---\n");
  3525. break;
  3526. }
  3527. case MMTYPE_VENDOR_VS_NW_INFO_CNF:
  3528. {
  3529. memcpy(QcaMac, MmePacket->OSA, 6);
  3530. sprintf((char*)buf_log_evcomm,
  3531. "[QCA7K][Rx][VENDOR_VS_NW_INFO_CNF]Got QCA7K MacAddr:%02X:%02X:%02X:%02X:%02X:%02X (comm:OK)",
  3532. QcaMac[0], QcaMac[1], QcaMac[2], QcaMac[3], QcaMac[4], QcaMac[5]);
  3533. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3534. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_VS_NW_INFO_CNF ---\n");
  3535. DEBUG_PRINTF_EVCOMM_DETAIL("QcaMac: %02x:%02x:%02x:%02x:%02x:%02x\n",
  3536. QcaMac[0],
  3537. QcaMac[1],
  3538. QcaMac[2],
  3539. QcaMac[3],
  3540. QcaMac[4],
  3541. QcaMac[5]);
  3542. Update_V2G_Flow_Status(CM_SET_KEY_REQ);
  3543. ftime(&SeqStartTime);
  3544. break;
  3545. }
  3546. default:
  3547. {
  3548. break;
  3549. }
  3550. }
  3551. return 0;
  3552. }
  3553. /*===========================================================================
  3554. FUNCTION: SlacComm
  3555. DESCRIPTION:
  3556. PRE-CONDITION:
  3557. INPUT:
  3558. OUTPUT:
  3559. GLOBAL VARIABLES:
  3560. =============================================================================*/
  3561. int SlacComm()
  3562. {
  3563. static unsigned char qca7k_comm_retry = 0;
  3564. unsigned char *EvMac_in;
  3565. unsigned char *RunID_in;
  3566. double t_diff = 0;
  3567. int packet_size = 0;
  3568. int count = 0;
  3569. //int idx = 0;
  3570. int i = 0;
  3571. if(RawSock >= 0)
  3572. {
  3573. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  3574. packet_size = recvfrom(RawSock, V2gtpMsgRxBuf, V2GTP_MSG_RX_BUFFER_SIZE, 0, NULL, NULL);
  3575. if(packet_size > 0)
  3576. {
  3577. /*#ifdef Debug
  3578. DEBUG_PRINTF_EVCOMM_DETAIL("Raw Data: ");
  3579. for(count=0;count<packet_size;count++)
  3580. DEBUG_PRINTF_EVCOMM_DETAIL("0x%x, ",V2gtpMsgRxBuf[count]);
  3581. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3582. #endif*/
  3583. MmeProcess(V2gtpMsgRxBuf, packet_size);
  3584. }
  3585. }
  3586. switch(Check_V2G_Flow_Status())
  3587. {
  3588. case IDLE:
  3589. {
  3590. if(RawSock < 0)
  3591. {
  3592. RawSock = socket(PF_PACKET, SOCK_RAW, htons(EtherType_HomePlug));
  3593. sprintf((char*)buf_log_evcomm, "[RawSock]opened(%d)", RawSock);
  3594. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3595. if(RawSock == -1)
  3596. {
  3597. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SlacComm:Failed to create socket");
  3598. Update_V2G_Flow_Status(Other_Fault);
  3599. return -1;
  3600. }
  3601. if (setsockopt(RawSock, SOL_SOCKET, SO_BINDTODEVICE, QcaInterface, 4) < 0)
  3602. {
  3603. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SlacComm:Set SO_BINDTODEVICE NG");
  3604. Update_V2G_Flow_Status(Other_Fault);
  3605. return -1;
  3606. }
  3607. struct timeval tv;
  3608. tv.tv_sec = 0;
  3609. tv.tv_usec = 100000; //100ms (Rx timeout)
  3610. if (setsockopt(RawSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  3611. {
  3612. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SlacComm:Set SO_RCVTIMEO NG");
  3613. Update_V2G_Flow_Status(Other_Fault);
  3614. return -1;
  3615. }
  3616. tv.tv_usec = 100000; //100ms (Tx timeout)
  3617. if (setsockopt(RawSock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  3618. {
  3619. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SlacComm:Set SO_SNDTIMEO NG");
  3620. Update_V2G_Flow_Status(Other_Fault);
  3621. return -1;
  3622. }
  3623. memset(&Req, 0, sizeof(struct ifreq));
  3624. strcpy( (char*)Req.ifr_name, QcaInterface);
  3625. if (ioctl(RawSock, SIOCGIFINDEX, &Req) < 0)
  3626. {
  3627. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SlacComm: ioctl NG");
  3628. Update_V2G_Flow_Status(Other_Fault);
  3629. return -1;
  3630. }
  3631. memset( &DestSocketAddress, 0, sizeof(struct sockaddr_ll));
  3632. DestSocketAddress.sll_ifindex = Req.ifr_ifindex;
  3633. DestSocketAddress.sll_halen = ETH_ALEN;
  3634. PwmStartTime = 0;
  3635. EVCOMM_SYS_INFO.QCA7K_SetKeyDone = FALSE;
  3636. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[QCA7K]connecting...");
  3637. //Get QCA7K MAC address
  3638. GetQca7kMac();
  3639. ftime(&SeqStartTime);
  3640. break;
  3641. }
  3642. else //RawSock: opened
  3643. {
  3644. if(EVCOMM_SYS_INFO.QCA7K_SetKeyDone == FALSE)
  3645. {
  3646. ftime(&SeqEndTime);
  3647. t_diff = DiffTimeb(SeqStartTime, SeqEndTime);
  3648. if (t_diff > V2G_SECC_QCA7000_GET_MAC_ADDR_REQ_RETRY_PERIOD) //3 secs
  3649. {
  3650. qca7k_comm_retry++;
  3651. sprintf((char*)buf_log_evcomm,
  3652. "Re-try connecting...(%.02lf/%dms)",
  3653. t_diff,
  3654. V2G_SECC_QCA7000_GET_MAC_ADDR_REQ_RETRY_PERIOD);
  3655. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3656. GetQca7kMac(); //re-send req
  3657. ftime(&SeqStartTime);
  3658. break;
  3659. }
  3660. else
  3661. {
  3662. //null
  3663. }
  3664. //Retry by 3 times
  3665. if (qca7k_comm_retry >= 3)
  3666. {
  3667. sprintf((char*)buf_log_evcomm,
  3668. "[Error]Comm: fail (retry by %d times)",
  3669. qca7k_comm_retry);
  3670. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3671. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3672. //CCS_SECCC_TIMEOUT_QCA7000_COMM (023892): The firmware code of QCA7000 may not be installed, yet
  3673. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3674. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3675. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3676. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3677. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  3678. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  3679. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3680. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3681. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  3682. Update_V2G_Flow_Status(Sequence_Timeout);
  3683. qca7k_comm_retry = 0;
  3684. break;
  3685. }
  3686. }
  3687. else //RawSock: opened; Set Key: DONE
  3688. {
  3689. if((CheckConnectorPlugIn() == TRUE) && (ShmInternalComm->ChargingPermission == TRUE))
  3690. {
  3691. if(PwmStartTime <= 0)
  3692. {
  3693. //Sniffer_Tcpdump(ENABLE);
  3694. //#if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  3695. //sleep(1); //wait for tcpdump to be ready.
  3696. //#endif
  3697. SwitchCpStateE(DISABLE);
  3698. OutputCpPwmDuty(5);
  3699. PwmStartTime = time(NULL);
  3700. }
  3701. else
  3702. {
  3703. if((time(NULL) - PwmStartTime) > TT_EVSE_SLAC_init)
  3704. {
  3705. sprintf((char*)buf_log_evcomm,
  3706. "SlacComm: Wait CM_SLAC_PARM_REQ Timeout - TT_EVSE_SLAC_init, NowTime(%ld)-PwmStartTime(%d)>%d (sec)",
  3707. time(NULL),
  3708. PwmStartTime,
  3709. TT_EVSE_SLAC_init);
  3710. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3711. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3712. //CCS_SECC_TIMEOUT_SLAC_TT_EVSE_SLAC_init (023809)
  3713. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3714. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3715. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3716. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3717. ShmStatusCodeData->PresentStatusCode[0][4] = 0;
  3718. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  3719. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  3720. Update_V2G_Flow_Status(Sequence_Timeout);
  3721. PwmStartTime = 0;
  3722. return -1;
  3723. }
  3724. else
  3725. {
  3726. //waiting for CM_SLAC_PARM_REQ
  3727. }
  3728. }
  3729. }
  3730. else
  3731. {
  3732. PwmStartTime = 0;
  3733. }
  3734. }
  3735. }
  3736. break;
  3737. }
  3738. case CM_SET_KEY_REQ: //13
  3739. {
  3740. //CM_SET_KEY_REQ
  3741. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[QCA7K][Rx]CM_SET_KEY_REQ");
  3742. ftime(&SeqEndTime);
  3743. t_diff = DiffTimeb(SeqStartTime, SeqEndTime);
  3744. if (t_diff > V2G_SECC_QCA7000_COMM_TIMEOUT) //10 seconds
  3745. {
  3746. sprintf((char*)buf_log_evcomm,
  3747. "[QCA7K]Failed on SetKey => End_Process (%.02lf/%dms)",
  3748. t_diff,
  3749. V2G_SECC_QCA7000_COMM_TIMEOUT);
  3750. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3751. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3752. //CCS_SECCC_TIMEOUT_QCA7000_COMM (023892): The firmware code of QCA7000 may not be installed, yet
  3753. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3754. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3755. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3756. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3757. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  3758. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  3759. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3760. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3761. Update_V2G_Flow_Status(Sequence_Timeout);
  3762. }
  3763. else if (t_diff > V2G_SECC_QCA7000_SEND_SET_KEY_PERIOD) //2 seconds
  3764. {
  3765. sprintf((char*)buf_log_evcomm,
  3766. "[QCA7K]SetKey: proceed (%.02lf/%dms)",
  3767. t_diff,
  3768. V2G_SECC_QCA7000_SEND_SET_KEY_PERIOD);
  3769. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3770. SendSetKey();
  3771. ftime(&SeqStartTime);
  3772. }
  3773. else
  3774. {
  3775. //null
  3776. }
  3777. break;
  3778. }
  3779. case CM_SET_KEY_CNF: //14
  3780. {
  3781. sprintf((char*)buf_log_evcomm,
  3782. "[SECCReady]Wait: plugin(%d), matached(%d), permission(%d)...",
  3783. CheckConnectorPlugIn(),
  3784. CSUCOMMDC_TASK_FLAG.matched,
  3785. ShmInternalComm->ChargingPermission
  3786. );
  3787. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3788. EVCOMM_SYS_INFO.QCA7K_SetKeyDone = TRUE;
  3789. PwmStartTime = 0;
  3790. Update_V2G_Flow_Status(IDLE);
  3791. break;
  3792. }
  3793. case CM_SLAC_PARM_CONF:
  3794. {
  3795. ftime(&SeqEndTime);
  3796. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_sequence)
  3797. {
  3798. sprintf((char*)buf_log_evcomm,
  3799. "SlacComm: Wait CM_START_ATTEN_CHAR_IND Timeout - TT_match_sequence (%.02lf of %dms)",
  3800. DiffTimeb(SeqStartTime, SeqEndTime),
  3801. TT_match_sequence);
  3802. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3803. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3804. //CCS_SECC_TIMEOUT_CM_START_ATTEN_CHAR_IND (023811):
  3805. //[Possible Reason] CCS Board image might not be updated properly. The 2nd reboot process is not finished.
  3806. //[Verification Method] By tying reboot command to the terminal and see if it could be executed immediately.
  3807. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3808. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3809. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3810. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3811. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  3812. ShmStatusCodeData->PresentStatusCode[0][5] = 1;
  3813. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3814. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3815. Update_V2G_Flow_Status(Sequence_Timeout);
  3816. return -1;
  3817. }
  3818. break;
  3819. }
  3820. case CM_START_ATTEN_CHAR_IND:
  3821. {
  3822. ftime(&SeqEndTime);
  3823. if(DiffTimeb(SeqStartTime, SeqEndTime) > (TP_EV_batch_msg_interval)) //one more time interval for tolerance
  3824. {
  3825. sprintf((char*)buf_log_evcomm,
  3826. "SlacComm: Wait CM_MNBC_SOUND_IND Timeout - TP_EV_batch_msg_interval (%.02lf of %dms)",
  3827. DiffTimeb(SeqStartTime, SeqEndTime),
  3828. TP_EV_batch_msg_interval);
  3829. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3830. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3831. //CCS_SECC_TIMEOUT_SLAC_CM_MNBC_SOUND_IND (023818):
  3832. //[Possible Reason] CCS Board image might not be updated properly. The 2nd reboot process is not finished.
  3833. //[Verification Method] By tying reboot command to the terminal and see if it could be executed immediately.
  3834. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3835. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3836. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3837. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3838. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  3839. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  3840. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3841. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3842. Update_V2G_Flow_Status(Sequence_Timeout);
  3843. return -1;
  3844. }
  3845. break;
  3846. }
  3847. case CM_MNBC_SOUND_IND:
  3848. {
  3849. #if (NEW_SLAC_ARCHITECTURE_SWITCH == ENABLE)
  3850. {
  3851. ftime(&SeqEndTime);
  3852. t_diff = DiffTimeb(SeqStartTime, SeqEndTime);
  3853. //printf("time:%.02lf, profilNum:%d\n",DiffTimeb(SeqStartTime, SeqEndTime),AttenProfileCnt); //added by Vern
  3854. if(t_diff > TT_EVSE_match_MNBC ||
  3855. (SLAC_INFO.array[0].AttenProfileCnt >= SLAC_INFO.array[0].MnbcSoundNum) ||
  3856. (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0)) //TC_SECC_VTB_AttenuationCharacterization_003
  3857. {
  3858. //Wait for other SLAC Req sets
  3859. if ((SLAC_INFO.arrayLen >= 2) &&
  3860. (t_diff < TT_EVSE_match_MNBC) &&
  3861. (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry == 0)) //not a retry
  3862. {
  3863. break;
  3864. }
  3865. //Check if it is a timeup response
  3866. if (t_diff > TT_EVSE_match_MNBC)
  3867. {
  3868. sprintf((char*)buf_log_evcomm,
  3869. "[WARNING][SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: timeup(%.2f/%dms) => send",
  3870. (i + 1),
  3871. t_diff,
  3872. TT_EVSE_match_MNBC);
  3873. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3874. }
  3875. //Sending all CM_ATTEN_CHAR_IND according to all corresponding SLAC Req sets
  3876. for (i = 0; i < SLAC_INFO.arrayLen; i++)
  3877. {
  3878. if ((SLAC_INFO.array[i].AttenProfileCnt == 0) ||
  3879. (SLAC_INFO.array[i].AagGroupsNum == 0) ||
  3880. (SLAC_INFO.array[i].StartAttenCharCnt == 0) ||
  3881. (SLAC_INFO.array[i].MnbcSoundNum != 10) || //received in CM_START_ATTEN_CHAR_IND
  3882. (SLAC_INFO.array[i].StartAttenCharErr == TRUE)
  3883. )
  3884. {
  3885. //Ignoring those SLAC request sets without sending CM_MNBC_SOUND_IND(CM_ATTEN_PROFILE_IND)
  3886. sprintf((char*)buf_log_evcomm,
  3887. "[WARNING][SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: para err(%d,%d,%d,%d,%d) => canceled",
  3888. (i + 1),
  3889. SLAC_INFO.array[i].AttenProfileCnt,
  3890. SLAC_INFO.array[i].AagGroupsNum,
  3891. SLAC_INFO.array[i].StartAttenCharCnt,
  3892. SLAC_INFO.array[i].MnbcSoundNum,
  3893. SLAC_INFO.array[i].StartAttenCharErr);
  3894. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3895. continue;
  3896. }
  3897. //In CM_ATTEN_CHAR_IND retry process, here only re-send this message according to the 1st coming SLAC request
  3898. if ((EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0) && (i != 0))
  3899. {
  3900. sprintf((char*)buf_log_evcomm,
  3901. "[NOTE][SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: canceled",
  3902. (i + 1));
  3903. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  3904. break;
  3905. }
  3906. EvMac_in = SLAC_INFO.array[i].EvMac;
  3907. RunID_in = SLAC_INFO.array[i].RunID;
  3908. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  3909. memcpy(SendMmePacket.ODA, EvMac_in, SLAC_EVMAC_LENGTH);
  3910. memcpy(SendMmePacket.OSA, CsuMac, 6);
  3911. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  3912. SendMmePacket.MMV = 0x01;
  3913. SendMmePacket.MMTYPE = MMTYPE_CM_ATTEN_CHAR_IND;
  3914. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  3915. SendMmePacketSize = 0;
  3916. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00: EV-EVSE Matching)
  3917. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00: No Security)
  3918. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac_in, SLAC_EVMAC_LENGTH); //SOURCE_ADDRESS, MAC address of the EV Host
  3919. SendMmePacketSize += SLAC_EVMAC_LENGTH;
  3920. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, RunID_in, SLAC_RUNID_LENGTH);
  3921. SendMmePacketSize += SLAC_RUNID_LENGTH;
  3922. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //SOURCE_ID(0x00)
  3923. SendMmePacketSize += 17;
  3924. memset(SendMmePacket.MMENTRY + SendMmePacketSize, 0, 17); //RESP_ID(0x00)
  3925. SendMmePacketSize += 17;
  3926. SendMmePacket.MMENTRY[SendMmePacketSize++] = SLAC_INFO.array[i].AttenProfileCnt; //NumSounds: Number of M-Sounds used for generation of the ATTEN_PROFILE
  3927. SendMmePacket.MMENTRY[SendMmePacketSize++] = SLAC_INFO.array[i].AagGroupsNum; //NumGroups
  3928. for(count = 0; count < SLAC_INFO.array[i].AagGroupsNum; count++)
  3929. {
  3930. unsigned char TmpAag;
  3931. TmpAag = ((SLAC_INFO.array[i].AAG[count] / SLAC_INFO.array[i].AttenProfileCnt) & 0xFF);
  3932. SLAC_INFO.array[i].AAG_quality_ori += (float) TmpAag; //original signal quality
  3933. #if (SUDO_PSD_PARAMETER_MECHANISM == ENABLE) //default: ENABLE
  3934. #if 1
  3935. //TC_SECC_VTB_AttenuationCharacterization_019
  3936. TmpAag = TmpAag >> 1; //new method proposed by Joseph (divided by 2)
  3937. #else
  3938. if(TmpAag >= 39) //original method proposed by Vern
  3939. {
  3940. TmpAag = 37;
  3941. }
  3942. #endif
  3943. #endif
  3944. SendMmePacket.MMENTRY[SendMmePacketSize++] = TmpAag;
  3945. SLAC_INFO.array[i].AAG_quality_refined += (float) TmpAag; //refined signal quality
  3946. }
  3947. SendMmePacketSize += 19; //the size before MMENTRY
  3948. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == TRUE)
  3949. {
  3950. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Send MME Packet *****\n");
  3951. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3952. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  3953. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  3954. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  3955. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  3956. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  3957. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  3958. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  3959. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_ATTEN_CHAR_IND ---\n");
  3960. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  3961. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  3962. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
  3963. SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5],
  3964. SendMmePacket.MMENTRY[6],SendMmePacket.MMENTRY[7]);
  3965. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  3966. SendMmePacket.MMENTRY[8],SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],
  3967. SendMmePacket.MMENTRY[12],SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14],SendMmePacket.MMENTRY[15]);
  3968. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ID: \n");
  3969. for(count=0; count<17; count++)
  3970. {
  3971. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[16+count]);
  3972. }
  3973. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3974. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_ID: \n");
  3975. for(count=0; count<17; count++)
  3976. {
  3977. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[33+count]);
  3978. }
  3979. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3980. DEBUG_PRINTF_EVCOMM_DETAIL("NumSounds: 0x%x\n", SendMmePacket.MMENTRY[50]);
  3981. DEBUG_PRINTF_EVCOMM_DETAIL("ATTEN_PROFILE: \n");
  3982. for(count=0; count<AagGroupsNum; count++)
  3983. {
  3984. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[51+count]);
  3985. }
  3986. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  3987. }
  3988. #endif
  3989. count = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  3990. SLAC_INFO.array[i].AAG_quality_ori /= SLAC_INFO.array[i].AagGroupsNum;
  3991. SLAC_INFO.array[i].AAG_quality_refined /= SLAC_INFO.array[i].AagGroupsNum;
  3992. //Print log if this CM_ATTEN_CHAR_IND belongs to RETRY Tx message.
  3993. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0)
  3994. {
  3995. sprintf((char*)buf_log_evcomm,
  3996. "[SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: %d-th resend (Q=%.2f/%.2f)(%d/%d)",
  3997. (i + 1),
  3998. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry,
  3999. SLAC_INFO.array[i].AAG_quality_refined,
  4000. SLAC_INFO.array[i].AAG_quality_ori,
  4001. SLAC_INFO.array[i].AttenProfileCnt,
  4002. SLAC_INFO.array[i].MnbcSoundNum);
  4003. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4004. }
  4005. else if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry == 0)
  4006. {
  4007. sprintf((char*)buf_log_evcomm,
  4008. "[SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: Q=%.2f/%.2f(%d/%d)",
  4009. (i + 1),
  4010. SLAC_INFO.array[i].AAG_quality_refined,
  4011. SLAC_INFO.array[i].AAG_quality_ori,
  4012. SLAC_INFO.array[i].AttenProfileCnt,
  4013. SLAC_INFO.array[i].MnbcSoundNum);
  4014. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4015. }
  4016. else
  4017. {
  4018. sprintf((char*)buf_log_evcomm,
  4019. "[SLAC][Tx]CM_ATTEN_CHAR_IND[%d]: unexpected CM_ATTEN_CHAR_IND_retry(%d))",
  4020. i,
  4021. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry);
  4022. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4023. }
  4024. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n", SendMmePacketSize,count);
  4025. } //end of for loop
  4026. Update_V2G_Flow_Status(CM_ATTEN_CHAR_IND);
  4027. ftime(&SeqStartTime);
  4028. }
  4029. break;
  4030. }
  4031. #else
  4032. {
  4033. ftime(&SeqEndTime);
  4034. //printf("time:%.02lf, profilNum:%d\n",DiffTimeb(SeqStartTime, SeqEndTime),AttenProfileCnt); //added by Vern
  4035. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_EVSE_match_MNBC || (AttenProfileCnt >= MnbcSoundNum) || (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0))
  4036. {
  4037. memset(&SendMmePacket, 0, sizeof(struct MmeHeader));
  4038. memcpy(SendMmePacket.ODA, EvMac, 6);
  4039. memcpy(SendMmePacket.OSA, CsuMac, 6);
  4040. SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  4041. SendMmePacket.MMV = 0x01;
  4042. SendMmePacket.MMTYPE = MMTYPE_CM_ATTEN_CHAR_IND;
  4043. SendMmePacket.FMI[0] = SendMmePacket.FMI[1] = 0;
  4044. SendMmePacketSize = 0;
  4045. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00: EV-EVSE Matching)
  4046. SendMmePacket.MMENTRY[SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00: No Security)
  4047. memcpy(SendMmePacket.MMENTRY + SendMmePacketSize, EvMac, 6); //SOURCE_ADDRESS, MAC address of the EV Host
  4048. SendMmePacketSize += 6;
  4049. memcpy(SendMmePacket.MMENTRY+SendMmePacketSize, SlacRunId, sizeof(SlacRunId));
  4050. SendMmePacketSize += sizeof(SlacRunId);
  4051. memset(SendMmePacket.MMENTRY+SendMmePacketSize, 0, 17); //SOURCE_ID
  4052. SendMmePacketSize += 17;
  4053. memset(SendMmePacket.MMENTRY+SendMmePacketSize, 0, 17); //RESP_ID
  4054. SendMmePacketSize += 17;
  4055. SendMmePacket.MMENTRY[SendMmePacketSize++] = AttenProfileCnt; //NumSounds
  4056. SendMmePacket.MMENTRY[SendMmePacketSize++] = AagGroupsNum; //NumGroups
  4057. for(count=0; count < AagGroupsNum; count++)
  4058. {
  4059. unsigned char TmpAag;
  4060. TmpAag = ((Aag[count] / AttenProfileCnt) & 0xFF);
  4061. #if (SUDO_PSD_PARAMETER_MECHANISM == ENABLE) //default: ENABLE
  4062. #if 1
  4063. //TC_SECC_VTB_AttenuationCharacterization_019
  4064. TmpAag = TmpAag >> 1; //new method proposed by Joseph (divided by 2)
  4065. #else
  4066. if(TmpAag >= 39) //original method proposed by Vern
  4067. {
  4068. /*
  4069. // [To-do] If this statement is enabled, SLAC will fail due to timeout.
  4070. {
  4071. unsigned char TmpBuf[64];
  4072. memset(TmpBuf,0,sizeof(TmpBuf));
  4073. sprintf((char*)TmpBuf,"SlacComm: bad Aag[%d]=%d",count,TmpAag);
  4074. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", TmpBuf);
  4075. }
  4076. */
  4077. printf("%d,", TmpAag);
  4078. TmpAag = 37;
  4079. }
  4080. #endif
  4081. #endif
  4082. SendMmePacket.MMENTRY[SendMmePacketSize++] = TmpAag;
  4083. }
  4084. SendMmePacketSize += 19; //the size before MMENTRY
  4085. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == TRUE)
  4086. {
  4087. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Send MME Packet *****\n");
  4088. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  4089. SendMmePacket.ODA[0],SendMmePacket.ODA[1],SendMmePacket.ODA[2],SendMmePacket.ODA[3],SendMmePacket.ODA[4],SendMmePacket.ODA[5]);
  4090. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  4091. SendMmePacket.OSA[0],SendMmePacket.OSA[1],SendMmePacket.OSA[2],SendMmePacket.OSA[3],SendMmePacket.OSA[4],SendMmePacket.OSA[5]);
  4092. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(SendMmePacket.MTYPE));
  4093. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", SendMmePacket.MMV);
  4094. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", SendMmePacket.MMTYPE);
  4095. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", SendMmePacket.FMI[0],SendMmePacket.FMI[1]);
  4096. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_ATTEN_CHAR_IND ---\n");
  4097. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", SendMmePacket.MMENTRY[0]);
  4098. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", SendMmePacket.MMENTRY[1]);
  4099. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
  4100. SendMmePacket.MMENTRY[2],SendMmePacket.MMENTRY[3],SendMmePacket.MMENTRY[4],SendMmePacket.MMENTRY[5],
  4101. SendMmePacket.MMENTRY[6],SendMmePacket.MMENTRY[7]);
  4102. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  4103. SendMmePacket.MMENTRY[8],SendMmePacket.MMENTRY[9],SendMmePacket.MMENTRY[10],SendMmePacket.MMENTRY[11],
  4104. SendMmePacket.MMENTRY[12],SendMmePacket.MMENTRY[13],SendMmePacket.MMENTRY[14],SendMmePacket.MMENTRY[15]);
  4105. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ID: \n");
  4106. for(count=0; count<17; count++)
  4107. {
  4108. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[16+count]);
  4109. }
  4110. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  4111. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_ID: \n");
  4112. for(count=0; count<17; count++)
  4113. {
  4114. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[33+count]);
  4115. }
  4116. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  4117. DEBUG_PRINTF_EVCOMM_DETAIL("NumSounds: 0x%x\n", SendMmePacket.MMENTRY[50]);
  4118. DEBUG_PRINTF_EVCOMM_DETAIL("ATTEN_PROFILE: \n");
  4119. for(count=0; count<AagGroupsNum; count++)
  4120. {
  4121. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ",SendMmePacket.MMENTRY[51+count]);
  4122. }
  4123. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  4124. }
  4125. #endif
  4126. Update_V2G_Flow_Status(CM_ATTEN_CHAR_IND);
  4127. count = sendto(RawSock, &SendMmePacket, SendMmePacketSize, 0, (struct sockaddr*)&DestSocketAddress, sizeof(struct sockaddr_ll));
  4128. //Print log if this CM_ATTEN_CHAR_IND belongs to RETRY Tx message.
  4129. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0)
  4130. {
  4131. sprintf((char*)buf_log_evcomm,
  4132. "[SLAC][Tx]CM_ATTEN_CHAR_IND: resend(%d-th)",
  4133. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry);
  4134. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4135. }
  4136. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n", SendMmePacketSize,count);
  4137. ftime(&SeqStartTime);
  4138. }
  4139. break;
  4140. }
  4141. #endif
  4142. }
  4143. case CM_ATTEN_CHAR_IND:
  4144. {
  4145. ftime(&SeqEndTime);
  4146. //if(DiffTimeb(SeqStartTime, SeqEndTime)>TT_match_response)
  4147. 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.
  4148. {
  4149. sprintf((char*)buf_log_evcomm,
  4150. "SlacComm: Wait CM_ATTEN_CHAR_RSP Timeout - TT_match_response (%.02lf of %dms)",
  4151. DiffTimeb(SeqStartTime, SeqEndTime),
  4152. TT_match_response);
  4153. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4154. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4155. //CCS_SECC_TIMEOUT_SLAC_CM_ATTEN_CHAR_RSP (023814):
  4156. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  4157. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4158. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4159. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4160. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  4161. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  4162. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  4163. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  4164. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4165. Update_V2G_Flow_Status(Sequence_Timeout);
  4166. return -1;
  4167. }
  4168. break;
  4169. }
  4170. case CM_ATTEN_CHAR_RSP:
  4171. {
  4172. ftime(&SeqEndTime);
  4173. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_EVSE_match_session)
  4174. {
  4175. sprintf((char*)buf_log_evcomm,
  4176. "SlacComm: Wait CM_VALIDATE_REQ_1ST or CM_SLAC_MATCH_REQ Timeout - TT_EVSE_match_session (%.02lf of %dms)",
  4177. DiffTimeb(SeqStartTime, SeqEndTime),
  4178. TT_EVSE_match_session);
  4179. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4180. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4181. //CCS_SECC_TIMEOUT_SLAC_CM_VALIDATE_REQ_1ST__CM_SLAC_MATCH_REQ (023815):
  4182. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  4183. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4184. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4185. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4186. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  4187. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  4188. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  4189. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  4190. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4191. Update_V2G_Flow_Status(Sequence_Timeout);
  4192. return -1;
  4193. }
  4194. break;
  4195. }
  4196. case CM_VALIDATE_CNF:
  4197. {
  4198. ftime(&SeqEndTime);
  4199. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_sequence)
  4200. {
  4201. sprintf((char*)buf_log_evcomm,
  4202. "SlacComm: Wait CM_VALIDATE_REQ_2ND or CM_SLAC_MATCH_REQ Timeout - TT_match_sequence (%.02lf of %dms)",
  4203. DiffTimeb(SeqStartTime, SeqEndTime),
  4204. TT_match_sequence);
  4205. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4206. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4207. //CCS_SECC_TIMEOUT_SLAC_CM_VALIDATE_REQ_2ND__CM_SLAC_MATCH_REQ (023819):
  4208. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  4209. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4210. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4211. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4212. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  4213. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  4214. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  4215. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  4216. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4217. Update_V2G_Flow_Status(Sequence_Timeout);
  4218. return -1;
  4219. }
  4220. break;
  4221. }
  4222. case CM_SLAC_MATCH_CNF:
  4223. {
  4224. if(UdpSock > 0)
  4225. {
  4226. close(UdpSock);
  4227. UdpSock = -1;
  4228. }
  4229. if(TcpSock > 0)
  4230. {
  4231. close(TcpSock);
  4232. TcpSock = -1;
  4233. }
  4234. ftime(&SeqStartTime);
  4235. V2gTcpConnected();
  4236. Update_V2G_Flow_Status(SLACC_SDP_UDP_Connection);
  4237. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SLAAC,SDP,UDP: connecting...");
  4238. break;
  4239. }
  4240. default:
  4241. {
  4242. break;
  4243. }
  4244. }
  4245. return 0;
  4246. }
  4247. /*===========================================================================
  4248. FUNCTION: V2gMsgDecoder
  4249. DESCRIPTION:
  4250. 1. Decode the V2GTP messages inside "msg" and save the decoded
  4251. messages in ccs_exi_doc_DIN, ccs_exi_doc_ISO1, and ccs_exi_doc_ISO2,
  4252. respectively.
  4253. 2. After decoding, V2gMsg_Process() could then use
  4254. ccs_exi_doc_DIN, ccs_exi_doc_ISO1, or ccs_exi_doc_ISO2
  4255. to deal with the corresponding Response messages, respectively.
  4256. PRE-CONDITION:
  4257. 1. msg_length > 0
  4258. INPUT:
  4259. 1. msg
  4260. 2. msg_length
  4261. OUTPUT:
  4262. 1. ccs_exi_doc_DIN //global variable
  4263. ccs_exi_doc_ISO1
  4264. ccs_exi_doc_ISO2
  4265. 2. v2g_state //Status Flag
  4266. //indicating the V2gMsg_Process_din to proceed
  4267. the next process.
  4268. 3. return value // < 0: ERROR
  4269. // > 0: Message Type
  4270. GLOBAL VARIABLES:
  4271. =============================================================================*/
  4272. int V2gMsgDecoder(unsigned char *msg, unsigned int msg_length, unsigned int v2g_state)
  4273. {
  4274. int errn = 0;
  4275. //Checking the minimum Header size requirement
  4276. if(msg_length < V2GTP_MSG_HEADER_LENGTH) //The minimum requirement should be 8-byte header.
  4277. {
  4278. errn = -1;
  4279. return errn;
  4280. }
  4281. //Decode the 1st V2GMSG: AppProtocol
  4282. if(v2g_state == SupportedAppProtocolRequest) //17
  4283. {
  4284. if ((errn = API_V2GMSG_EXI_Decoder_AppProtocol(msg, msg_length, &ccs_handshake)) < 0)
  4285. {
  4286. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR = %d (DEC)]V2gMsgDecoder: SupportedAppProtocolRequest()\n", errn);
  4287. }
  4288. else //decoded successfully.
  4289. {
  4290. //PRINT_XML_DOC_supportedAppProtocolReq(&ccs_handshake);
  4291. }
  4292. }
  4293. //Decode the subsequent V2GMSG (DIN 70121, ISO 15118-2, ISO 15118-20)
  4294. else if(v2g_state > SupportedAppProtocolRequest && v2g_state <= SessionStopResponse)
  4295. {
  4296. //Decoding according to its own protocol
  4297. switch (ShmCcsData->CommProtocol)
  4298. {
  4299. case V2GT_MSG_PROTOCOL_DIN70121: //0
  4300. {
  4301. //DIN
  4302. if((errn = API_V2GMSG_EXI_Decoder_DIN(msg, msg_length, &ccs_exi_doc_DIN)) < 0)
  4303. {
  4304. sprintf((char*)buf_log_evcomm,
  4305. "[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_DIN()",
  4306. errn);
  4307. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4308. }
  4309. break;
  4310. }
  4311. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  4312. {
  4313. //ISO1
  4314. if((errn = API_V2GMSG_EXI_Decoder_ISO1(msg, msg_length, &ccs_exi_doc_ISO1)) < 0)
  4315. {
  4316. sprintf((char*)buf_log_evcomm,
  4317. "[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_ISO1()",
  4318. errn);
  4319. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4320. }
  4321. break;
  4322. }
  4323. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  4324. {
  4325. //ISO2
  4326. if((errn = API_V2GMSG_EXI_Decoder_ISO2(msg, msg_length, &ccs_exi_doc_ISO2)) < 0)
  4327. {
  4328. sprintf((char*)buf_log_evcomm,
  4329. "[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_ISO2()",
  4330. errn);
  4331. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4332. }
  4333. break;
  4334. }
  4335. default:
  4336. break;
  4337. }
  4338. }
  4339. else
  4340. {
  4341. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR = %d (DEC)]V2gMsgDecoder: Unexpected v2g_state\n", errn);
  4342. errn = -1;
  4343. }
  4344. return errn;
  4345. }
  4346. /*===========================================================================
  4347. FUNCTION: encode_din_V2GTP_stream
  4348. DESCRIPTION:
  4349. PRE-CONDITION:
  4350. INPUT:
  4351. OUTPUT:
  4352. GLOBAL VARIABLES:
  4353. =============================================================================*/
  4354. int encode_din_V2GTP_stream(bitstream_t *v2g_tx_stream, struct dinEXIDocument *ccs_exi_doc_DIN)
  4355. {
  4356. int errn = 0;
  4357. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  4358. errn = encode_dinExiDocument(v2g_tx_stream, ccs_exi_doc_DIN);
  4359. if (errn == 0)
  4360. {
  4361. //successfully encoded
  4362. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  4363. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  4364. if (errn != 0)
  4365. {
  4366. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_din_V2GTP_stream]write_v2gtpHeader(): %d (DEC)\n", errn);
  4367. }
  4368. }
  4369. else
  4370. {
  4371. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_din_V2GTP_stream]encode_dinExiDocument(): %d (DEC)\n", errn);
  4372. }
  4373. return errn;
  4374. }
  4375. /*===========================================================================
  4376. FUNCTION: encode_iso1_V2GTP_stream
  4377. DESCRIPTION:
  4378. PRE-CONDITION:
  4379. INPUT:
  4380. OUTPUT:
  4381. GLOBAL VARIABLES:
  4382. =============================================================================*/
  4383. int encode_iso1_V2GTP_stream(bitstream_t *v2g_tx_stream, struct iso1EXIDocument *ccs_exi_doc_ISO1)
  4384. {
  4385. int errn = 0;
  4386. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  4387. errn = encode_iso1ExiDocument(v2g_tx_stream, ccs_exi_doc_ISO1);
  4388. if (errn == 0)
  4389. {
  4390. //successfully encoded
  4391. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  4392. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  4393. if (errn != 0)
  4394. {
  4395. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso1_V2GTP_stream]write_v2gtpHeader(): %d (DEC)\n", errn);
  4396. }
  4397. }
  4398. else
  4399. {
  4400. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso1_V2GTP_stream]encode_iso1ExiDocument(): %d (DEC)\n", errn);
  4401. }
  4402. return errn;
  4403. }
  4404. /*===========================================================================
  4405. FUNCTION: encode_iso2_V2GTP_stream
  4406. DESCRIPTION:
  4407. PRE-CONDITION:
  4408. INPUT:
  4409. OUTPUT:
  4410. GLOBAL VARIABLES:
  4411. =============================================================================*/
  4412. int encode_iso2_V2GTP_stream(bitstream_t *v2g_tx_stream, struct iso2EXIDocument *ccs_exi_doc_ISO2)
  4413. {
  4414. int errn = 0;
  4415. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  4416. errn = encode_iso2ExiDocument(v2g_tx_stream, ccs_exi_doc_ISO2);
  4417. if (errn == 0)
  4418. {
  4419. //successfully encoded
  4420. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  4421. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  4422. if (errn != 0)
  4423. {
  4424. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso2_V2GTP_stream]write_v2gtpHeader(): %d (DEC)\n", errn);
  4425. }
  4426. }
  4427. else
  4428. {
  4429. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][encode_iso2_V2GTP_stream]encode_iso2ExiDocument(): %d (DEC)\n", errn);
  4430. }
  4431. return errn;
  4432. }
  4433. /*===========================================================================
  4434. FUNCTION: send_encoded_din_V2GTP_Stream
  4435. DESCRIPTION:
  4436. PRE-CONDITION:
  4437. INPUT:
  4438. OUTPUT:
  4439. GLOBAL VARIABLES:
  4440. =============================================================================*/
  4441. int send_encoded_din_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct dinEXIDocument *ccs_exi_doc_DIN)
  4442. {
  4443. int errn = 0;
  4444. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  4445. errn = encode_din_V2GTP_stream(v2g_tx_stream, ccs_exi_doc_DIN);
  4446. // STEP 2: =========== Send Response Packet ===========
  4447. int rtn = 0;
  4448. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  4449. if (rtn == v2g_tx_stream->size)
  4450. {
  4451. /*
  4452. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][send_encoded_din_V2GTP_Stream] Response Message is sent ( %d / %d ). (Bytes, DEC): OK\n",
  4453. rtn, v2g_tx_stream->size);
  4454. */
  4455. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  4456. }
  4457. else if (rtn >= 0)
  4458. {
  4459. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][send_encoded_din_V2GTP_Stream]Imcomplete Tx ( %d / %d ). (Bytes, DEC): FAIL\n", rtn, v2g_tx_stream->size);
  4460. }
  4461. else
  4462. {
  4463. errn = rtn;
  4464. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][send_encoded_din_V2GTP_Stream]send(): %d (DEC)\n", errn);
  4465. }
  4466. //STEP 3: ========= Reset V2G MSG Flags ==========
  4467. init_dinBodyType(&ccs_exi_doc_DIN->V2G_Message.Body);
  4468. return errn;
  4469. }
  4470. /*===========================================================================
  4471. FUNCTION: send_encoded_iso1_V2GTP_Stream
  4472. DESCRIPTION:
  4473. PRE-CONDITION:
  4474. INPUT:
  4475. OUTPUT:
  4476. GLOBAL VARIABLES:
  4477. =============================================================================*/
  4478. int send_encoded_iso1_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct iso1EXIDocument *ccs_exi_doc_ISO1)
  4479. {
  4480. int errn = 0;
  4481. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  4482. errn = encode_iso1_V2GTP_stream(v2g_tx_stream, ccs_exi_doc_ISO1);
  4483. // STEP 2: =========== Send Response Packet ===========
  4484. int rtn = 0;
  4485. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  4486. if (rtn == v2g_tx_stream->size)
  4487. {
  4488. /*
  4489. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][send_encoded_iso1_V2GTP_Stream] Response Message is sent ( %d / %d ). (Bytes, DEC): OK\n",
  4490. rtn, v2g_tx_stream->size);
  4491. */
  4492. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  4493. }
  4494. else if (rtn >= 0)
  4495. {
  4496. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][send_encoded_iso1_V2GTP_Stream]Imcomplete Tx ( %d / %d ). (Bytes, DEC): FAIL\n", rtn, v2g_tx_stream->size);
  4497. }
  4498. else
  4499. {
  4500. errn = rtn;
  4501. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][send_encoded_iso1_V2GTP_Stream]send(): %d (DEC)\n", errn);
  4502. }
  4503. //STEP 3: ========= Reset V2G MSG Flags ==========
  4504. init_iso1BodyType(&ccs_exi_doc_ISO1->V2G_Message.Body);
  4505. return errn;
  4506. }
  4507. /*===========================================================================
  4508. FUNCTION: send_encoded_iso2_V2GTP_Stream
  4509. DESCRIPTION:
  4510. PRE-CONDITION:
  4511. INPUT:
  4512. OUTPUT:
  4513. GLOBAL VARIABLES:
  4514. =============================================================================*/
  4515. int send_encoded_iso2_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct iso2EXIDocument *ccs_exi_doc_ISO2)
  4516. {
  4517. int errn = 0;
  4518. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  4519. errn = encode_iso2_V2GTP_stream(v2g_tx_stream, ccs_exi_doc_ISO2);
  4520. // STEP 2: =========== Send Response Packet ===========
  4521. int rtn = 0;
  4522. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  4523. if (rtn == v2g_tx_stream->size)
  4524. {
  4525. /*
  4526. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][send_encoded_iso2_V2GTP_Stream] Response Message is sent ( %d / %d ). (Bytes, DEC): OK\n",
  4527. rtn, v2g_tx_stream->size);
  4528. */
  4529. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  4530. }
  4531. else if (rtn >= 0)
  4532. {
  4533. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm][send_encoded_iso2_V2GTP_Stream]Imcomplete Tx ( %d / %d ). (Bytes, DEC): FAIL\n",
  4534. rtn, v2g_tx_stream->size);
  4535. }
  4536. else
  4537. {
  4538. errn = rtn;
  4539. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][send_encoded_iso2_V2GTP_Stream]send(): %d (DEC)\n", errn);
  4540. }
  4541. //STEP 3: ========= Reset V2G MSG Flags ==========
  4542. init_iso2BodyType(&ccs_exi_doc_ISO2->V2G_Message.Body);
  4543. return errn;
  4544. }
  4545. /*===========================================================================
  4546. FUNCTION: Check_din_V2G_Rx_MSG_SessionID
  4547. DESCRIPTION:
  4548. PRE-CONDITION:
  4549. INPUT:
  4550. OUTPUT:
  4551. GLOBAL VARIABLES:
  4552. =============================================================================*/
  4553. int Check_din_V2G_Rx_MSG_SessionID(struct dinEXIDocument *exi_doc_DIN)
  4554. {
  4555. int i = 0;
  4556. int leng = 0;
  4557. int errn = 0;
  4558. leng = exi_doc_DIN->V2G_Message.Header.SessionID.bytesLen;
  4559. //Step 1: Check SessionID Length
  4560. if (leng != 8) //8-byte
  4561. {
  4562. sprintf((char*)buf_log_evcomm, "[ERROR]SessionID: incorrect length(%d)", leng);
  4563. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4564. errn = -1;
  4565. }
  4566. else
  4567. {
  4568. //Step 2-1: Check SessionID content
  4569. for (i = 0; i < leng; i++)
  4570. {
  4571. if (exi_doc_DIN->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  4572. {
  4573. errn = -2;
  4574. break;
  4575. }
  4576. }
  4577. }
  4578. //Step 2-2: Print Incorrect ID
  4579. if (errn == -2) //incorrect ID
  4580. {
  4581. sprintf((char*)buf_log_evcomm,
  4582. "[ERROR]SessionID: incorrect ID(RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)",
  4583. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[0],
  4584. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[1],
  4585. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[2],
  4586. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[3],
  4587. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[4],
  4588. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[5],
  4589. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[6],
  4590. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[7],
  4591. EVCOMM_SYS_INFO.SessionID[0],
  4592. EVCOMM_SYS_INFO.SessionID[1],
  4593. EVCOMM_SYS_INFO.SessionID[2],
  4594. EVCOMM_SYS_INFO.SessionID[3],
  4595. EVCOMM_SYS_INFO.SessionID[4],
  4596. EVCOMM_SYS_INFO.SessionID[5],
  4597. EVCOMM_SYS_INFO.SessionID[6],
  4598. EVCOMM_SYS_INFO.SessionID[7]
  4599. );
  4600. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4601. }
  4602. //Step 3: Correct SessionID for Res Message
  4603. if (errn != 0)
  4604. {
  4605. exi_doc_DIN->V2G_Message.Header.SessionID.bytesLen = 8;
  4606. memset(exi_doc_DIN->V2G_Message.Header.SessionID.bytes, 0, 8);
  4607. memcpy(exi_doc_DIN->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4608. }
  4609. return errn;
  4610. }
  4611. /*===========================================================================
  4612. FUNCTION: Check_iso1_V2G_Rx_MSG_SessionID
  4613. DESCRIPTION:
  4614. PRE-CONDITION:
  4615. INPUT:
  4616. OUTPUT:
  4617. GLOBAL VARIABLES:
  4618. =============================================================================*/
  4619. int Check_iso1_V2G_Rx_MSG_SessionID(struct iso1EXIDocument *exi_doc_ISO1)
  4620. {
  4621. int i = 0;
  4622. int leng = 0;
  4623. int errn = 0;
  4624. leng = exi_doc_ISO1->V2G_Message.Header.SessionID.bytesLen;
  4625. //Step 1: Check SessionID Length
  4626. if (leng != 8) //8-byte
  4627. {
  4628. sprintf((char*)buf_log_evcomm, "[ERROR]SessionID: incorrect length(%d)", leng);
  4629. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4630. errn = -1;
  4631. }
  4632. else
  4633. {
  4634. //Step 2-1: Check SessionID content
  4635. for (i = 0; i < leng; i++)
  4636. {
  4637. if (exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  4638. {
  4639. errn = -2;
  4640. break;
  4641. }
  4642. }
  4643. }
  4644. //Step 2-2: Print Incorrect ID
  4645. if (errn == -2) //incorrect ID
  4646. {
  4647. sprintf((char*)buf_log_evcomm,
  4648. "[ERROR]SessionID: incorrect ID(RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)",
  4649. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[0],
  4650. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[1],
  4651. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[2],
  4652. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[3],
  4653. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[4],
  4654. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[5],
  4655. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[6],
  4656. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[7],
  4657. EVCOMM_SYS_INFO.SessionID[0],
  4658. EVCOMM_SYS_INFO.SessionID[1],
  4659. EVCOMM_SYS_INFO.SessionID[2],
  4660. EVCOMM_SYS_INFO.SessionID[3],
  4661. EVCOMM_SYS_INFO.SessionID[4],
  4662. EVCOMM_SYS_INFO.SessionID[5],
  4663. EVCOMM_SYS_INFO.SessionID[6],
  4664. EVCOMM_SYS_INFO.SessionID[7]
  4665. );
  4666. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4667. }
  4668. //Step 3: Correct SessionID for Res Message
  4669. if (errn != 0)
  4670. {
  4671. exi_doc_ISO1->V2G_Message.Header.SessionID.bytesLen = 8;
  4672. memset(exi_doc_ISO1->V2G_Message.Header.SessionID.bytes, 0, 8);
  4673. memcpy(exi_doc_ISO1->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4674. }
  4675. return errn;
  4676. }
  4677. /*===========================================================================
  4678. FUNCTION: Check_iso2_V2G_Rx_MSG_SessionID
  4679. DESCRIPTION:
  4680. PRE-CONDITION:
  4681. INPUT:
  4682. OUTPUT:
  4683. GLOBAL VARIABLES:
  4684. =============================================================================*/
  4685. int Check_iso2_V2G_Rx_MSG_SessionID(struct iso2EXIDocument *exi_doc_ISO2)
  4686. {
  4687. int i = 0;
  4688. int leng = 0;
  4689. int errn = 0;
  4690. leng = exi_doc_ISO2->V2G_Message.Header.SessionID.bytesLen;
  4691. //Step 1: Check SessionID Length
  4692. if (leng != 8) //8-byte
  4693. {
  4694. sprintf((char*)buf_log_evcomm, "[ERROR]SessionID: incorrect length(%d)", leng);
  4695. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4696. errn = -1;
  4697. }
  4698. else
  4699. {
  4700. //Step 2-1: Check SessionID content
  4701. for (i = 0; i < leng; i++)
  4702. {
  4703. if (exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  4704. {
  4705. errn = -2;
  4706. break;
  4707. }
  4708. }
  4709. }
  4710. //Step 2-2: Print Incorrect ID
  4711. if (errn == -2) //incorrect ID
  4712. {
  4713. sprintf((char*)buf_log_evcomm,
  4714. "[ERROR]SessionID: incorrect ID(RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)",
  4715. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[0],
  4716. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[1],
  4717. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[2],
  4718. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[3],
  4719. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[4],
  4720. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[5],
  4721. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[6],
  4722. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[7],
  4723. EVCOMM_SYS_INFO.SessionID[0],
  4724. EVCOMM_SYS_INFO.SessionID[1],
  4725. EVCOMM_SYS_INFO.SessionID[2],
  4726. EVCOMM_SYS_INFO.SessionID[3],
  4727. EVCOMM_SYS_INFO.SessionID[4],
  4728. EVCOMM_SYS_INFO.SessionID[5],
  4729. EVCOMM_SYS_INFO.SessionID[6],
  4730. EVCOMM_SYS_INFO.SessionID[7]
  4731. );
  4732. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4733. }
  4734. //Step 3: Correct SessionID for Res Message
  4735. if (errn != 0)
  4736. {
  4737. exi_doc_ISO2->V2G_Message.Header.SessionID.bytesLen = 8;
  4738. memset(exi_doc_ISO2->V2G_Message.Header.SessionID.bytes, 0, 8);
  4739. memcpy(exi_doc_ISO2->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4740. }
  4741. return errn;
  4742. }
  4743. /*===========================================================================
  4744. FUNCTION: GetSchemaID_of_Protocol
  4745. DESCRIPTION:
  4746. 1. Get the SchemaID accroding to the input target
  4747. *V2GT_MSG_PROTOCOL_DIN70121: choose DIN 70121
  4748. *V2GT_MSG_PROTOCOL_ISO15118_2014: choose ISO 15118-2 (ed1)
  4749. *V2GT_MSG_PROTOCOL_ISO15118_2018: choose ISO 15118-20 (ed2)
  4750. *V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY: choose the one with the highest priority
  4751. 2. [To-do] Checking Major and Minor version
  4752. 3. The parsing method will not support those private protocols,
  4753. such as "usr:tesla...," since "tesla" is 5 bytes but "din" and "iso" are 3 bytes.
  4754. 4. [To-do] If the target is selected as DIN," but there is no DIN.
  4755. However, if EV and EVSE all support ISO, how to use ISO instead?
  4756. PRE-CONDITION:
  4757. INPUT:
  4758. 1. target: target protocol
  4759. OUTPUT:
  4760. 1. id: SchemaID of selected protocol by EVSE
  4761. 2. ShmCcsData->CommProtocol (selected protocol)
  4762. 3. SupportedAppProtocol_result
  4763. (1) appHandresponseCodeType_OK_SuccessfulNegotiation = 0,
  4764. (2) appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation = 1,
  4765. (3) appHandresponseCodeType_Failed_NoNegotiation = 2
  4766. GLOBAL VARIABLES:
  4767. 1. ccs_handshake
  4768. 2. ShmCcsData
  4769. =============================================================================*/
  4770. int GetSchemaID_of_Protocol(unsigned char target)
  4771. {
  4772. int i = 0;
  4773. int ii = 0;
  4774. int id = 0;
  4775. //unsigned char tmp = 0;
  4776. unsigned char pri = 20; //priority = 1(highest)~20(lowerest)
  4777. char num[10];
  4778. //struct CCS_ProtocolNamespacestructCharacters pro;
  4779. //Choose the 1st protocol as default.
  4780. //id = CCS_HANDSHAKE_PROTOCOLS.array[0].SchemaID;
  4781. id = -1;
  4782. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  4783. for(i = 0; i < CCS_HANDSHAKE_PROTOCOLS.arrayLen; i++)
  4784. {
  4785. //Checking for <ProtocolNamespace>urn:din:70121:2012:MsgDef</ProtocolNamespace>
  4786. //[To-Do] Ignoring the priority from EV and force DIN 70121 as our only option. (for temp)
  4787. //[CAUTION] The parsing method will not support those private protocols, such as "usr:tesla..."
  4788. num[0] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[8];
  4789. num[1] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[9];
  4790. num[2] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[10];
  4791. num[3] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[11];
  4792. num[4] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[12];
  4793. num[5] = '\0';
  4794. if (atoi(num) == 70121)
  4795. {
  4796. sprintf((char*)buf_log_evcomm,
  4797. "[GetSchemaID_of_Protocol]supported(%d-th/%d): DIN 70121(%d:v%d.%d;id=%d,pri=%d)",
  4798. (i+1),
  4799. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4800. atoi(num),
  4801. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4802. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4803. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4804. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4805. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4806. if (target == V2GT_MSG_PROTOCOL_DIN70121)
  4807. {
  4808. sprintf((char*)buf_log_evcomm,
  4809. "[GetSchemaID_of_Protocol]selected(%d-th/%d): DIN 70121(%d:v%d.%d;id=%d,pri=%d)",
  4810. (i+1),
  4811. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4812. atoi(num),
  4813. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4814. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4815. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4816. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4817. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4818. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == DIN_SPEC_70121_2012_VersionNumberMajor)
  4819. {
  4820. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == DIN_SPEC_70121_2012_VersionNumberMinor)
  4821. {
  4822. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  4823. }
  4824. else
  4825. { //[TC_SECC_VTB_SupportedAppProtocol_005]
  4826. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  4827. }
  4828. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4829. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  4830. return id;
  4831. }
  4832. else
  4833. {
  4834. //keep looking for the suitable protocol
  4835. }
  4836. }
  4837. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  4838. {
  4839. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  4840. {
  4841. ii = i;
  4842. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4843. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  4844. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  4845. }
  4846. else
  4847. {
  4848. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  4849. }
  4850. }
  4851. else
  4852. {
  4853. //null
  4854. }
  4855. }
  4856. else if (atoi(num) == 15118)
  4857. {
  4858. //urn:din:70121:2012:MsgDef
  4859. //urn:iso:15118:2:2013:MsgDef
  4860. memset(num, 0, sizeof(num));
  4861. num[0] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[16];
  4862. num[1] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[17];
  4863. num[2] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[18];
  4864. num[3] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[19];
  4865. num[4] = '\0';
  4866. if (atoi(num) < 2018 && atoi(num) >= 2010)
  4867. {
  4868. sprintf((char*)buf_log_evcomm,
  4869. "[GetSchemaID_of_Protocol]supported(%d-th/%d): ISO 15118-2(ed1,%d:v%d.%d;id=%d,pri=%d)",
  4870. (i+1),
  4871. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4872. atoi(num),
  4873. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4874. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4875. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4876. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4877. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4878. if (target == V2GT_MSG_PROTOCOL_ISO15118_2014)
  4879. {
  4880. sprintf((char*)buf_log_evcomm,
  4881. "[GetSchemaID_of_Protocol]selected(%d-th/%d): ISO 15118-2,ed1(%d:v%d.%d;id=%d,pri=%d)",
  4882. (i+1),
  4883. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4884. atoi(num),
  4885. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4886. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4887. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4888. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4889. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4890. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == ISO1_15118_2013_VersionNumberMajor)
  4891. {
  4892. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == ISO1_15118_2013_VersionNumberMinor)
  4893. {
  4894. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  4895. }
  4896. else
  4897. {
  4898. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  4899. }
  4900. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4901. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2014;
  4902. return id;
  4903. }
  4904. else
  4905. {
  4906. //keep looking for the suitable protocol
  4907. }
  4908. }
  4909. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  4910. {
  4911. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  4912. {
  4913. ii = i;
  4914. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4915. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  4916. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2014;
  4917. }
  4918. else
  4919. {
  4920. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  4921. }
  4922. }
  4923. else
  4924. {
  4925. //null
  4926. }
  4927. }
  4928. else if (atoi(num) >= 2018 && atoi(num) <= 2100) // >= 2018
  4929. {
  4930. sprintf((char*)buf_log_evcomm,
  4931. "[GetSchemaID_of_Protocol]supported(%d-th/%d): ISO 15118-20(ed2,%d:v%d.%d;id=%d,pri=%d)",
  4932. (i+1),
  4933. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4934. atoi(num),
  4935. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4936. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4937. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4938. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4939. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4940. if (target == V2GT_MSG_PROTOCOL_ISO15118_2018)
  4941. {
  4942. sprintf((char*)buf_log_evcomm,
  4943. "[GetSchemaID_of_Protocol]selected(%d-th/%d): ISO 15118-20,ed2(%d:v%d.%d;id=%d,pri=%d)",
  4944. (i+1),
  4945. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4946. atoi(num),
  4947. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4948. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  4949. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  4950. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  4951. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  4952. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == ISO2_15118_2018_VersionNumberMajor)
  4953. {
  4954. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == ISO2_15118_2018_VersionNumberMinor)
  4955. {
  4956. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  4957. }
  4958. else
  4959. {
  4960. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  4961. }
  4962. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4963. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2018;
  4964. return id;
  4965. }
  4966. else
  4967. {
  4968. //keep looking for the suitable protocol
  4969. }
  4970. }
  4971. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  4972. {
  4973. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  4974. {
  4975. ii = i;
  4976. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  4977. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  4978. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2018;
  4979. }
  4980. else
  4981. {
  4982. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  4983. }
  4984. }
  4985. else
  4986. {
  4987. //null
  4988. }
  4989. }
  4990. else
  4991. {
  4992. //Unexpected Year
  4993. sprintf((char*)buf_log_evcomm,
  4994. "[GetSchemaID_of_Protocol]unsupported(%d-th/%d): ISO 15118-X(unexpected year:%d,v%d.%d),id=%d,pri=%d",
  4995. (i+1),
  4996. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  4997. atoi(num),
  4998. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  4999. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  5000. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  5001. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  5002. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5003. //return -1;
  5004. }
  5005. }
  5006. else
  5007. {
  5008. sprintf((char*)buf_log_evcomm,
  5009. "[GetSchemaID_of_Protocol]unsupported protocol(%d-th/%d)(%d:v%d.%d;id=%d,pri=%d)",
  5010. (i+1),
  5011. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  5012. atoi(num),
  5013. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  5014. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  5015. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  5016. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  5017. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5018. //return -1;
  5019. }
  5020. }
  5021. //The final result of highest priority protocol
  5022. sprintf((char*)buf_log_evcomm,
  5023. "[GetSchemaID_of_Protocol]selected(%d-th/%d): pro=%d(0:DIN,1:ISO1,2:ISO2);id=%d,pri=%d",
  5024. (ii+1),
  5025. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  5026. ShmCcsData->CommProtocol,
  5027. id,
  5028. pri);
  5029. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5030. if (id < 0)
  5031. {
  5032. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_Failed_NoNegotiation;
  5033. }
  5034. return id;
  5035. }
  5036. /*===========================================================================
  5037. FUNCTION: Proc_supportedAppProtocolRes
  5038. DESCRIPTION:
  5039. PRE-CONDITION:
  5040. INPUT:
  5041. OUTPUT:
  5042. GLOBAL VARIABLES:
  5043. =============================================================================*/
  5044. int Proc_supportedAppProtocolRes(int AcceptFd)
  5045. {
  5046. int errn = 0;
  5047. bitstream_t v2g_tx_stream;
  5048. static struct ChargingInfoData *sys;
  5049. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5050. //STEP 1: =========== Setting the Response Message ===========
  5051. init_appHandEXIDocument(&ccs_handshake);
  5052. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_OK_SuccessfulNegotiation;
  5053. ccs_handshake.supportedAppProtocolRes_isUsed = 1u;
  5054. //select the 1st one as the default
  5055. ccs_handshake.supportedAppProtocolRes.SchemaID = CCS_HANDSHAKE_PROTOCOLS.array[0].SchemaID;
  5056. ccs_handshake.supportedAppProtocolRes.SchemaID_isUsed = 1u;
  5057. int id = 0;
  5058. id = GetSchemaID_of_Protocol(V2GT_MSG_PROTOCOL_PREFERENCE); //output: EVCOMM_SYS_INFO.SupportedAppProtocol_result
  5059. ccs_handshake.supportedAppProtocolRes.ResponseCode = EVCOMM_SYS_INFO.SupportedAppProtocol_result; //updating the response code
  5060. if (id < 0)
  5061. {
  5062. sprintf((char*)buf_log_evcomm,
  5063. "[Error]No avalible CCS protocol (id = %d, preference = %d)",
  5064. id,
  5065. V2GT_MSG_PROTOCOL_PREFERENCE);
  5066. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5067. }
  5068. else
  5069. {
  5070. //selected SchemaID
  5071. ccs_handshake.supportedAppProtocolRes.SchemaID = (unsigned char) id;
  5072. }
  5073. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  5074. {
  5075. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_supportedAppProtocolRes]EVSE_Shutdown => End_Process");
  5076. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5077. errn = -1;
  5078. }
  5079. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5080. {
  5081. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_supportedAppProtocolRes]EVSE_EmergencyShutdown => End_Process");
  5082. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5083. errn = -1;
  5084. }
  5085. else
  5086. {
  5087. //null
  5088. }
  5089. //Check for Permission Changing from TRUE to FALSE
  5090. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  5091. ShmInternalComm->ChargingPermission == FALSE)
  5092. {
  5093. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][supportedAppProtocolRes]Permission OFF");
  5094. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5095. //errn = -1;
  5096. }
  5097. #if (CP_PROTECTION_MECHANISM == ENABLE)
  5098. {
  5099. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  5100. {
  5101. //Detect for CP State should be 9V with 5% PWM or 100%PWM (State B1, B2)
  5102. if (sys->CpState != 2 && sys->CpState != 3) //State B1, B2
  5103. {
  5104. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5105. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5106. Update_V2G_Flow_Status(Other_Fault);
  5107. sprintf((char*)buf_log_evcomm,
  5108. "[Proc_supportedAppProtocolRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  5109. sys->CpState,
  5110. sys->CpVoltage);
  5111. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5112. }
  5113. }
  5114. #else
  5115. {
  5116. //Detect for CP State should be 9V (State B)
  5117. if (sys->CpState != 3) //B2
  5118. {
  5119. ccs_handshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  5120. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5121. Update_V2G_Flow_Status(Other_Fault);
  5122. sprintf((char*)buf_log_evcomm,
  5123. "[Proc_supportedAppProtocolRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  5124. sys->CpState,
  5125. sys->CpVoltage);
  5126. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5127. }
  5128. }
  5129. #endif
  5130. }
  5131. #endif
  5132. //STEP 2: =========== Encode into EXI ===========
  5133. if ((errn = API_V2GMSG_EXI_Encoder_AppProtocol(&ccs_handshake, &v2g_tx_stream)) !=0)
  5134. {
  5135. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Error]API_V2GMSG_EXI_Encoder_AppProtocol\n");
  5136. return errn;
  5137. }
  5138. //STEP 3: =========== Send Response Packet ===========
  5139. int Rtn = 0;
  5140. Rtn = send(AcceptFd, v2g_tx_stream.data, v2g_tx_stream.size, 0);
  5141. //Rtn = send(6, v2g_tx_stream.data, v2g_tx_stream.size, 0);
  5142. DEBUG_PRINTF_EVCOMM_DETAIL("Send V2GTP Packet Size = %d, Rtn = %d (Bytes, DEC)\n", v2g_tx_stream.size, Rtn);
  5143. if (Rtn < 0)
  5144. {
  5145. return Rtn;
  5146. }
  5147. //STEP 4: =========== Save into Share Memory =========
  5148. //[Joseph, To-Do] Adding a mechanism to choose DIN 70121 as our 1st priority. (for multiple protocols)
  5149. //STEP 5: =========== Updating the Flow State Flag =========
  5150. if (id < 0)
  5151. {
  5152. errn = -1;
  5153. }
  5154. //STEP 6: =========== Reset Flags ============
  5155. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  5156. init_appHandEXIDocument(&ccs_handshake);
  5157. return errn;
  5158. }
  5159. /*===========================================================================
  5160. FUNCTION: Proc_supportedAppProtocolReq
  5161. DESCRIPTION:
  5162. PRE-CONDITION:
  5163. INPUT:
  5164. OUTPUT:
  5165. GLOBAL VARIABLES:
  5166. =============================================================================*/
  5167. int Proc_supportedAppProtocolReq(int AcceptFd)
  5168. {
  5169. //[Joseph, To-Do] analysis on Req message and choose the prefered protocol
  5170. //Default: DIN 70121 (find SchemaID)
  5171. int errn = 0;
  5172. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]supportedAppProtocolReq");
  5173. SHM_Save_din_supportedAppProtocolReq(ShmCcsData, &ccs_handshake, ShmSysConfigAndInfo);
  5174. errn = Proc_supportedAppProtocolRes(AcceptFd);
  5175. if (errn == 0)
  5176. {
  5177. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Tx]supportedAppProtocolRes");
  5178. }
  5179. else
  5180. {
  5181. DEBUG_PRINTF_EVCOMM_DETAIL("[Error]Proc_supportedAppProtocolRes(): %d (DEC)\n", errn);
  5182. }
  5183. return errn;
  5184. }
  5185. /*===========================================================================
  5186. FUNCTION: Proc_din_SessionSetupRes
  5187. DESCRIPTION:
  5188. PRE-CONDITION:
  5189. INPUT:
  5190. OUTPUT:
  5191. GLOBAL VARIABLES:
  5192. =============================================================================*/
  5193. int Proc_din_SessionSetupRes(int AcceptFd)
  5194. {
  5195. //int i = 0;
  5196. int errn = 0;
  5197. bitstream_t v2g_tx_stream;
  5198. static struct ChargingInfoData *sys;
  5199. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5200. size_t pos = 0;
  5201. v2g_tx_stream.pos = &pos;
  5202. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5203. v2g_tx_stream.data = V2GTP_Tx_buf;
  5204. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  5205. init_dinSessionSetupResType(&ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes);
  5206. // ====== [BODY (1/2) ResponseCode ======
  5207. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  5208. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_OK_NewSessionEstablished;
  5209. //[HEADER] Assign Res SessionID
  5210. ccs_exi_doc_DIN.V2G_Message.Header.SessionID.bytesLen = 8;
  5211. memset(ccs_exi_doc_DIN.V2G_Message.Header.SessionID.bytes, 0, 8);
  5212. memcpy(ccs_exi_doc_DIN.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  5213. //Check for SequenceError
  5214. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5215. {
  5216. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError;
  5217. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5218. errn = -1;
  5219. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5220. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5221. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5222. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5223. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5224. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5225. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5226. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5227. }
  5228. //#if PARAMETER_NORMAL_MODE == ENABLE
  5229. //SHM_Read_din_V2GMSG_Header(&ccs_exi_doc_DIN, ShmCcsData);
  5230. //#endif
  5231. //Detect for CP State should be 9V (State B)
  5232. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5233. {
  5234. #if CP_PROTECTION_MECHANISM == ENABLE
  5235. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  5236. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5237. Update_V2G_Flow_Status(Other_Fault);
  5238. errn = -1;
  5239. sprintf((char*)buf_log_evcomm,
  5240. "[Proc_din_SessionSetupRes]Emergency Stop by CP Error (%d)\n",
  5241. sys->CpState);
  5242. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5243. #else
  5244. sprintf((char*)buf_log_evcomm,
  5245. "[Proc_din_SessionSetupRes]Emergency Stop by CP Error (%d): ignored\n",
  5246. sys->CpState);
  5247. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5248. #endif
  5249. }
  5250. //Check for shutdown commands from EVSE(DC Main Board)
  5251. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5252. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5253. {
  5254. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  5255. sprintf((char*)buf_log_evcomm, "[Proc_din_SessionSetupRes]Stop by EVSE(%d:normal, %d:emergency): (%d)\n",
  5256. EVSE_Shutdown,
  5257. EVSE_EmergencyShutdown,
  5258. sys->DC_EVSEStatus);
  5259. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5260. errn = -1;
  5261. }
  5262. //Check for Permission Changing from TRUE to FALSE
  5263. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  5264. ShmInternalComm->ChargingPermission == FALSE)
  5265. {
  5266. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][SessionSetupRes]Permission OFF");
  5267. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  5268. errn = -1;
  5269. }
  5270. // ====== [BODY (2/3) EVSEID ======
  5271. //EVSEID = all zero
  5272. memset(ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytes, 0, sizeof(ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytes));
  5273. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytesLen = 15; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  5274. // ====== [BODY (3/3) DateTimeNow ======
  5275. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.DateTimeNow_isUsed = 1u;
  5276. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.DateTimeNow = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  5277. #if PARAMETER_NORMAL_MODE == ENABLE
  5278. ///////////SHM_Read_din_SessionSetupRes(&ccs_exi_doc_DIN, ShmCcsData);
  5279. #endif
  5280. // ============ Encode and Send Response Message ===========
  5281. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  5282. {
  5283. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_SessionSetupRes][Error]Tx encoded msg error");
  5284. errn = -1;
  5285. }
  5286. return errn;
  5287. }
  5288. /*===========================================================================
  5289. FUNCTION: Proc_iso1_SessionSetupRes
  5290. DESCRIPTION:
  5291. PRE-CONDITION:
  5292. INPUT:
  5293. OUTPUT:
  5294. GLOBAL VARIABLES:
  5295. =============================================================================*/
  5296. int Proc_iso1_SessionSetupRes(int AcceptFd)
  5297. {
  5298. //int i = 0;
  5299. int errn = 0;
  5300. bitstream_t v2g_tx_stream;
  5301. static struct ChargingInfoData *sys;
  5302. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5303. size_t pos = 0;
  5304. v2g_tx_stream.pos = &pos;
  5305. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5306. v2g_tx_stream.data = V2GTP_Tx_buf;
  5307. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  5308. init_iso1SessionSetupResType(&ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes);
  5309. // ====== [BODY (1/2) ResponseCode ======
  5310. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  5311. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_OK_NewSessionEstablished;
  5312. //[HEADER] Assign Res SessionID
  5313. ccs_exi_doc_ISO1.V2G_Message.Header.SessionID.bytesLen = 8;
  5314. memset(ccs_exi_doc_ISO1.V2G_Message.Header.SessionID.bytes, 0, 8);
  5315. memcpy(ccs_exi_doc_ISO1.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  5316. //Check for SequenceError
  5317. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5318. {
  5319. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  5320. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5321. errn = -1;
  5322. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5323. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5324. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5325. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5326. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5327. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5328. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5329. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5330. }
  5331. //#if PARAMETER_NORMAL_MODE == ENABLE
  5332. //SHM_Read_iso1_V2GMSG_Header(&ccs_exi_doc_ISO1, ShmCcsData);
  5333. //#endif
  5334. //Detect for CP State should be 9V (State B)
  5335. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5336. {
  5337. #if CP_PROTECTION_MECHANISM == ENABLE
  5338. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  5339. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5340. Update_V2G_Flow_Status(Other_Fault);
  5341. errn = -1;
  5342. sprintf((char*)buf_log_evcomm,
  5343. "[Proc_iso1_SessionSetupRes]Emergency Stop by CP Error (%d)\n",
  5344. sys->CpState);
  5345. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5346. #else
  5347. sprintf((char*)buf_log_evcomm,
  5348. "[Proc_iso1_SessionSetupRes]Emergency Stop by CP Error (%d): ignored\n",
  5349. sys->CpState);
  5350. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5351. #endif
  5352. }
  5353. //Check for shutdown commands from EVSE(DC Main Board)
  5354. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5355. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5356. {
  5357. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  5358. sprintf((char*)buf_log_evcomm, "[Proc_iso1_SessionSetupRes]Stop by EVSE(%d:normal, %d:emergency): (%d)\n",
  5359. EVSE_Shutdown,
  5360. EVSE_EmergencyShutdown,
  5361. sys->DC_EVSEStatus);
  5362. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5363. errn = -1;
  5364. }
  5365. //Check for Permission Changing from TRUE to FALSE
  5366. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  5367. ShmInternalComm->ChargingPermission == FALSE)
  5368. {
  5369. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO1][SessionSetupRes]Permission OFF");
  5370. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  5371. errn = -1;
  5372. }
  5373. // ====== [BODY (2/3) EVSEID ======
  5374. //EVSEID = all zero
  5375. memset(ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters, 0, sizeof(ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters));
  5376. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 15; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  5377. // ====== [BODY (3/3) DateTimeNow ======
  5378. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSETimeStamp_isUsed = 1u;
  5379. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.EVSETimeStamp = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  5380. #if PARAMETER_NORMAL_MODE == ENABLE
  5381. ///////////SHM_Read_iso1_SessionSetupRes(&ccs_exi_doc_ISO1, ShmCcsData);
  5382. #endif
  5383. // ============ Encode and Send Response Message ===========
  5384. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  5385. {
  5386. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_SessionSetupRes][Error]Tx encoded msg error");
  5387. errn = -1;
  5388. }
  5389. return errn;
  5390. }
  5391. /*===========================================================================
  5392. FUNCTION: Proc_iso2_SessionSetupRes
  5393. DESCRIPTION:
  5394. PRE-CONDITION:
  5395. INPUT:
  5396. OUTPUT:
  5397. GLOBAL VARIABLES:
  5398. =============================================================================*/
  5399. int Proc_iso2_SessionSetupRes(int AcceptFd)
  5400. {
  5401. //int i = 0;
  5402. int errn = 0;
  5403. bitstream_t v2g_tx_stream;
  5404. static struct ChargingInfoData *sys;
  5405. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5406. size_t pos = 0;
  5407. v2g_tx_stream.pos = &pos;
  5408. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5409. v2g_tx_stream.data = V2GTP_Tx_buf;
  5410. init_iso2BodyType(&ccs_exi_doc_ISO2.V2G_Message.Body);
  5411. init_iso2SessionSetupResType(&ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes);
  5412. // ====== [BODY (1/2) ResponseCode ======
  5413. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  5414. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_OK_NewSessionEstablished;
  5415. //[HEADER] Assign Res SessionID
  5416. ccs_exi_doc_ISO2.V2G_Message.Header.SessionID.bytesLen = 8;
  5417. memset(ccs_exi_doc_ISO2.V2G_Message.Header.SessionID.bytes, 0, 8);
  5418. memcpy(ccs_exi_doc_ISO2.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  5419. //Check for SequenceError
  5420. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5421. {
  5422. ccs_exi_doc_ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  5423. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5424. errn = -1;
  5425. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5426. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5427. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5428. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5429. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5430. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5431. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5432. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5433. }
  5434. //#if PARAMETER_NORMAL_MODE == ENABLE
  5435. //SHM_Read_iso2_V2GMSG_Header(&ccs_exi_doc_ISO2, ShmCcsData);
  5436. //#endif
  5437. //Detect for CP State should be 9V (State B)
  5438. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5439. {
  5440. #if CP_PROTECTION_MECHANISM == ENABLE
  5441. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  5442. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5443. Update_V2G_Flow_Status(Other_Fault);
  5444. errn = -1;
  5445. sprintf((char*)buf_log_evcomm,
  5446. "[Proc_iso2_SessionSetupRes]Emergency Stop by CP Error (%d)",
  5447. sys->CpState);
  5448. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5449. #else
  5450. sprintf((char*)buf_log_evcomm,
  5451. "[Proc_iso2_SessionSetupRes]Emergency Stop by CP Error (%d): ignored",
  5452. sys->CpState);
  5453. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5454. #endif
  5455. }
  5456. //Check for shutdown commands from EVSE(DC Main Board)
  5457. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5458. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5459. {
  5460. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  5461. sprintf((char*)buf_log_evcomm, "[Proc_iso2_SessionSetupRes]Stop by EVSE(%d:normal, %d:emergency): (%d)\n",
  5462. EVSE_Shutdown,
  5463. EVSE_EmergencyShutdown,
  5464. sys->DC_EVSEStatus);
  5465. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5466. errn = -1;
  5467. }
  5468. //Check for Permission Changing from TRUE to FALSE
  5469. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  5470. ShmInternalComm->ChargingPermission == FALSE)
  5471. {
  5472. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO2][SessionSetupRes]Permission OFF");
  5473. ccs_exi_doc_DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  5474. errn = -1;
  5475. }
  5476. // ====== [BODY (2/3) EVSEID ======
  5477. //EVSEID = all zero
  5478. memset(ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.characters, 0, sizeof(ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.characters));
  5479. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 15; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  5480. // ====== [BODY (3/3) DateTimeNow ======
  5481. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSETimeStamp_isUsed = 1u;
  5482. ccs_exi_doc_ISO2.V2G_Message.Body.SessionSetupRes.EVSETimeStamp = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  5483. #if PARAMETER_NORMAL_MODE == ENABLE
  5484. ///////////SHM_Read_iso2_SessionSetupRes(&ccs_exi_doc_ISO2, ShmCcsData);
  5485. #endif
  5486. // ============ Encode and Send Response Message ===========
  5487. if (send_encoded_iso2_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO2) != 0)
  5488. {
  5489. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso2_SessionSetupRes][Error]Tx encoded msg error");
  5490. errn = -1;
  5491. }
  5492. return errn;
  5493. }
  5494. /*===========================================================================
  5495. FUNCTION: Proc_din_SessionSetupReq
  5496. DESCRIPTION:
  5497. PRE-CONDITION:
  5498. INPUT:
  5499. OUTPUT:
  5500. GLOBAL VARIABLES:
  5501. 2. ccs_exi_doc_DIN
  5502. =============================================================================*/
  5503. int Proc_din_SessionSetupReq(int AcceptFd)
  5504. {
  5505. int errn = 0;
  5506. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Proc_din_SessionSetupReq]Got SessionSetupReq\n");
  5507. //Print the decoded XML Document
  5508. PRINT_XML_DOC_DIN_SessionSetupReq(&ccs_exi_doc_DIN);
  5509. //Save into Share Memory
  5510. SHM_Save_din_SessionSetupReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  5511. errn = Proc_din_SessionSetupRes(AcceptFd);
  5512. if (errn == 0)
  5513. {
  5514. //successfully send response.
  5515. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]SessionSetupRes");
  5516. }
  5517. else
  5518. {
  5519. sprintf((char*)buf_log_evcomm,
  5520. "[Error]Proc_din_SessionSetupRes(): %d (DEC)",
  5521. errn);
  5522. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5523. }
  5524. return errn;
  5525. }
  5526. /*===========================================================================
  5527. FUNCTION: Proc_iso1_SessionSetupReq
  5528. DESCRIPTION:
  5529. PRE-CONDITION:
  5530. INPUT:
  5531. OUTPUT:
  5532. GLOBAL VARIABLES:
  5533. 2. ccs_exi_doc_ISO1
  5534. =============================================================================*/
  5535. int Proc_iso1_SessionSetupReq(int AcceptFd)
  5536. {
  5537. int errn = 0;
  5538. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Proc_iso1_SessionSetupReq]Got SessionSetupReq\n");
  5539. //Print the decoded XML Document
  5540. PRINT_XML_DOC_ISO1_SessionSetupReq(&ccs_exi_doc_ISO1);
  5541. //Save into Share Memory
  5542. SHM_Save_iso1_SessionSetupReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  5543. errn = Proc_iso1_SessionSetupRes(AcceptFd);
  5544. if (errn == 0)
  5545. {
  5546. //successfully send response.
  5547. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]SessionSetupRes");
  5548. }
  5549. else
  5550. {
  5551. sprintf((char*)buf_log_evcomm,
  5552. "[Error]Proc_iso1_SessionSetupRes(): %d (DEC)",
  5553. errn);
  5554. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5555. }
  5556. return errn;
  5557. }
  5558. /*===========================================================================
  5559. FUNCTION: Proc_iso2_SessionSetupReq
  5560. DESCRIPTION:
  5561. PRE-CONDITION:
  5562. INPUT:
  5563. OUTPUT:
  5564. GLOBAL VARIABLES:
  5565. 2. ccs_exi_doc_ISO2
  5566. =============================================================================*/
  5567. int Proc_iso2_SessionSetupReq(int AcceptFd)
  5568. {
  5569. int errn = 0;
  5570. DEBUG_PRINTF_EVCOMM_DETAIL("\n[Proc_iso2_SessionSetupReq]Got SessionSetupReq\n");
  5571. //Print the decoded XML Document
  5572. PRINT_XML_DOC_ISO2_SessionSetupReq(&ccs_exi_doc_ISO2);
  5573. //Save into Share Memory
  5574. SHM_Save_iso2_SessionSetupReq(ShmCcsData, &ccs_exi_doc_ISO2, ShmSysConfigAndInfo);
  5575. errn = Proc_iso2_SessionSetupRes(AcceptFd);
  5576. if (errn == 0)
  5577. {
  5578. //successfully send response.
  5579. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO2][V2G][Tx]SessionSetupRes");
  5580. }
  5581. else
  5582. {
  5583. sprintf((char*)buf_log_evcomm,
  5584. "[Error]Proc_iso2_SessionSetupRes(): %d (DEC)",
  5585. errn);
  5586. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5587. }
  5588. return errn;
  5589. }
  5590. /*===========================================================================
  5591. FUNCTION: Proc_din_ServiceDiscoveryRes
  5592. DESCRIPTION:
  5593. PRE-CONDITION:
  5594. INPUT:
  5595. OUTPUT:
  5596. GLOBAL VARIABLES:
  5597. =============================================================================*/
  5598. int Proc_din_ServiceDiscoveryRes(int AcceptFd)
  5599. {
  5600. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  5601. //if it is not the same, the packet should be ignored.
  5602. //int i = 0;
  5603. int errn = 0;
  5604. bitstream_t v2g_tx_stream;
  5605. struct ChargingInfoData *sys;
  5606. //struct ServiceDiscoveryRequest_DIN70121 *req;
  5607. struct ServiceDiscoveryResponse_DIN70121 *res;
  5608. size_t pos = 0;
  5609. v2g_tx_stream.pos = &pos;
  5610. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5611. v2g_tx_stream.data = V2GTP_Tx_buf;
  5612. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5613. //req = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryRequest;
  5614. res = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryResponse;
  5615. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  5616. init_dinServiceDiscoveryResType(&ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes);
  5617. //[1/4] Response Code
  5618. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes_isUsed = 1u;
  5619. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_OK;
  5620. //[HEADER] Check Req SessionID
  5621. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  5622. {
  5623. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  5624. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  5625. errn = -1;
  5626. }
  5627. //Check for SequenceError
  5628. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5629. {
  5630. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  5631. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5632. errn = -1;
  5633. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5634. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5635. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5636. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5637. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5638. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5639. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5640. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5641. }
  5642. //Detect for CP State should be 9V (State B)
  5643. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5644. {
  5645. #if CP_PROTECTION_MECHANISM == ENABLE
  5646. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  5647. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5648. Update_V2G_Flow_Status(Other_Fault);
  5649. errn = -1;
  5650. sprintf((char*)buf_log_evcomm, "[Proc_din_ServiceDiscoveryRes]Emergency Stop by CP Error (%d)", sys->CpState);
  5651. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5652. #else
  5653. sprintf((char*)buf_log_evcomm,
  5654. "[Proc_din_ServiceDiscoveryRes]Emergency Stop by CP Error (%d): ignored",
  5655. sys->CpState);
  5656. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5657. #endif
  5658. }
  5659. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5660. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5661. {
  5662. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  5663. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ServiceDiscoveryRes]shutdown by EVSE");
  5664. errn = -1;
  5665. }
  5666. //Check for Permission Changing from TRUE to FALSE
  5667. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  5668. ShmInternalComm->ChargingPermission == FALSE)
  5669. {
  5670. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][ServiceDiscoveryRes]Permission OFF");
  5671. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  5672. errn = -1;
  5673. }
  5674. //[2/4] PaymentOptions
  5675. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptions.PaymentOption.arrayLen = 1u;
  5676. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptions.PaymentOption.array[0] = dinpaymentOptionType_ExternalPayment; //1
  5677. //[3/4] Charge Service
  5678. res->ChargeService_DIN70121.Services.ServiceTag.ServiceID = 1;
  5679. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceTag.ServiceID = (unsigned short) res->ChargeService_DIN70121.Services.ServiceTag.ServiceID;
  5680. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceTag.ServiceCategory = dinserviceCategoryType_EVCharging;
  5681. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.FreeService = 1; //[Joseph] for test
  5682. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.EnergyTransferType = dinEVSESupportedEnergyTransferType_DC_extended;
  5683. //[4/4] Service List (null, not be uesed for now.)
  5684. //#if PARAMETER_NORMAL_MODE == ENABLE
  5685. ///////////////SHM_Read_din_ServiceDiscoveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  5686. //#endif
  5687. // ============ Encode and Send Response Message ===========
  5688. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  5689. {
  5690. errn = -1;
  5691. }
  5692. return errn;
  5693. }
  5694. /*===========================================================================
  5695. FUNCTION: Proc_iso1_ServiceDiscoveryRes
  5696. DESCRIPTION:
  5697. PRE-CONDITION:
  5698. INPUT:
  5699. OUTPUT:
  5700. GLOBAL VARIABLES:
  5701. =============================================================================*/
  5702. int Proc_iso1_ServiceDiscoveryRes(int AcceptFd)
  5703. {
  5704. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  5705. //if it is not the same, the packet should be ignored.
  5706. //int i = 0;
  5707. int errn = 0;
  5708. bitstream_t v2g_tx_stream;
  5709. struct ChargingInfoData *sys;
  5710. //struct ServiceDiscoveryRequest_ISO15118_2014 *req;
  5711. struct ServiceDiscoveryResponse_ISO15118_2014 *res;
  5712. size_t pos = 0;
  5713. v2g_tx_stream.pos = &pos;
  5714. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5715. v2g_tx_stream.data = V2GTP_Tx_buf;
  5716. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5717. //req = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryRequest;
  5718. res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryResponse;
  5719. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  5720. init_iso1ServiceDiscoveryResType(&ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes);
  5721. //[1/4] Response Code
  5722. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes_isUsed = 1u;
  5723. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_OK;
  5724. //[HEADER] Check Req SessionID
  5725. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  5726. {
  5727. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  5728. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  5729. errn = -1;
  5730. }
  5731. //Check for SequenceError
  5732. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5733. {
  5734. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  5735. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5736. errn = -1;
  5737. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5738. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5739. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5740. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5741. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5742. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5743. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5744. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5745. }
  5746. //Detect for CP State should be 9V (State B)
  5747. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5748. {
  5749. #if CP_PROTECTION_MECHANISM == ENABLE
  5750. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  5751. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5752. Update_V2G_Flow_Status(Other_Fault);
  5753. errn = -1;
  5754. sprintf((char*)buf_log_evcomm,
  5755. "[Proc_iso1_ServiceDiscoveryRes]Emergency Stop by CP Error (%d)",
  5756. sys->CpState);
  5757. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5758. #else
  5759. sprintf((char*)buf_log_evcomm,
  5760. "[Proc_iso1_ServiceDiscoveryRes]Emergency Stop by CP Error (%d): ignored",
  5761. sys->CpState);
  5762. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5763. #endif
  5764. }
  5765. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  5766. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5767. {
  5768. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  5769. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ServiceDiscoveryRes]shutdown by EVSE");
  5770. errn = -1;
  5771. }
  5772. //Check for Permission Changing from TRUE to FALSE
  5773. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  5774. ShmInternalComm->ChargingPermission == FALSE)
  5775. {
  5776. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO1][ServiceDiscoveryRes]Permission OFF");
  5777. ccs_exi_doc_DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  5778. errn = -1;
  5779. }
  5780. //[2/4] PaymentOptionList
  5781. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptionList.PaymentOption.arrayLen = 1u;
  5782. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptionList.PaymentOption.array[0] = iso1paymentOptionType_ExternalPayment; //1
  5783. //[3/4] Charge Service
  5784. res->ChargeService.Services.ServiceID = 1;
  5785. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceID = (unsigned short) res->ChargeService.Services.ServiceID;
  5786. //ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName_isUsed = 1; //new in ISO1, not be used, yet.
  5787. //ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName.charactersLen = xxx;
  5788. //ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName.characters[i] = xxx;
  5789. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceCategory = iso1serviceCategoryType_EVCharging;
  5790. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.FreeService = 1; //[Joseph] for test
  5791. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.arrayLen = 1u; //[ISO1] max = 6
  5792. switch (ShmCcsData->EnergyTransferMode)
  5793. {
  5794. case DC_extended:
  5795. {
  5796. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_DC_extended;
  5797. break;
  5798. }
  5799. case AC_single_phase_core:
  5800. {
  5801. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_AC_single_phase_core;
  5802. break;
  5803. }
  5804. case AC_three_phase_core:
  5805. {
  5806. ccs_exi_doc_ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_AC_three_phase_core;
  5807. break;
  5808. }
  5809. default:
  5810. {
  5811. sprintf((char*)buf_log_evcomm,
  5812. "[ISO1][ServiceDiscoveryRes][Warning]unexpected EnergyTransferMode(%d)",
  5813. ShmCcsData->EnergyTransferMode);
  5814. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5815. break;
  5816. }
  5817. }
  5818. //[4/4] Service List (null, not be uesed for now.)
  5819. //#if PARAMETER_NORMAL_MODE == ENABLE
  5820. ///////////////SHM_Read_iso1_ServiceDiscoveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  5821. //#endif
  5822. // ============ Encode and Send Response Message ===========
  5823. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  5824. {
  5825. errn = -1;
  5826. }
  5827. return errn;
  5828. }
  5829. /*===========================================================================
  5830. FUNCTION: Proc_din_ServiceDiscoveryReq
  5831. DESCRIPTION:
  5832. PRE-CONDITION:
  5833. INPUT:
  5834. OUTPUT:
  5835. GLOBAL VARIABLES:
  5836. =============================================================================*/
  5837. int Proc_din_ServiceDiscoveryReq(int AcceptFd)
  5838. {
  5839. int errn = 0;
  5840. DEBUG_PRINTF_EVCOMM_DETAIL("\n[SeccComm][Proc_din_ServiceDiscoveryReq]Got ServiceDiscoveryReq\n");
  5841. //Print the decoded XML Document
  5842. PRINT_XML_DOC_DIN_ServiceDiscoveryReq(&ccs_exi_doc_DIN);
  5843. //Save into Share Memory
  5844. SHM_Save_din_ServiceDiscoveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  5845. errn = Proc_din_ServiceDiscoveryRes(AcceptFd);
  5846. if (errn == 0)
  5847. {
  5848. //send response successfully.
  5849. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]ServiceDiscoveryRes");
  5850. }
  5851. else
  5852. {
  5853. sprintf((char*)buf_log_evcomm,
  5854. "[Error][SeccComm][Proc_din_ServiceDiscoveryReq]Proc_din_ServiceDiscoveryRes(): %d (DEC)",
  5855. errn
  5856. );
  5857. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5858. }
  5859. return errn;
  5860. }
  5861. /*===========================================================================
  5862. FUNCTION: Proc_iso1_ServiceDiscoveryReq
  5863. DESCRIPTION:
  5864. PRE-CONDITION:
  5865. INPUT:
  5866. OUTPUT:
  5867. GLOBAL VARIABLES:
  5868. =============================================================================*/
  5869. int Proc_iso1_ServiceDiscoveryReq(int AcceptFd)
  5870. {
  5871. int errn = 0;
  5872. DEBUG_PRINTF_EVCOMM_DETAIL("\n[SeccComm][Proc_iso1_ServiceDiscoveryReq]Got ServiceDiscoveryReq\n");
  5873. //Print the decoded XML Document
  5874. PRINT_XML_DOC_ISO1_ServiceDiscoveryReq(&ccs_exi_doc_ISO1);
  5875. //Save into Share Memory
  5876. SHM_Save_iso1_ServiceDiscoveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  5877. errn = Proc_iso1_ServiceDiscoveryRes(AcceptFd);
  5878. if (errn == 0)
  5879. {
  5880. //send response successfully.
  5881. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]ServiceDiscoveryRes");
  5882. }
  5883. else
  5884. {
  5885. sprintf((char*)buf_log_evcomm,
  5886. "[Error][SeccComm][Proc_iso1_ServiceDiscoveryReq]Proc_iso1_ServiceDiscoveryRes(): %d (DEC)",
  5887. errn
  5888. );
  5889. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5890. }
  5891. return errn;
  5892. }
  5893. /*===========================================================================
  5894. FUNCTION: Proc_din_ServiceAndPaymentSelectionRes
  5895. DESCRIPTION:
  5896. PRE-CONDITION:
  5897. INPUT:
  5898. OUTPUT:
  5899. GLOBAL VARIABLES:
  5900. =============================================================================*/
  5901. int Proc_din_ServiceAndPaymentSelectionRes(int AcceptFd)
  5902. {
  5903. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  5904. //if it is not the same, the packet should be ignored.
  5905. //int i = 0;
  5906. int errn = 0;
  5907. bitstream_t v2g_tx_stream;
  5908. struct ServiceAndPaymentSelectionRequest_DIN70121 *req;
  5909. //struct ServiceAndPaymentSelectionResponse_DIN70121 *res;
  5910. struct ServiceDiscoveryResponse_DIN70121 *sd_res;
  5911. struct ChargingInfoData *sys;
  5912. size_t pos = 0;
  5913. v2g_tx_stream.pos = &pos;
  5914. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5915. v2g_tx_stream.data = V2GTP_Tx_buf;
  5916. req = &ShmCcsData->V2GMessage_DIN70121.ServiceAndPaymentSelectionRequest;
  5917. //res = &ShmCcsData->V2GMessage_DIN70121.ServiceAndPaymentSelectionResponse;
  5918. sd_res = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryResponse;
  5919. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5920. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  5921. init_dinServicePaymentSelectionResType(&ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes);
  5922. //[1/1] Response Code
  5923. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes_isUsed = 1u;
  5924. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_OK;
  5925. //[HEADER] Check Req SessionID
  5926. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  5927. {
  5928. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  5929. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  5930. errn = -1;
  5931. }
  5932. //Check for SequenceError
  5933. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5934. {
  5935. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  5936. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  5937. errn = -1;
  5938. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5939. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5940. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5941. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5942. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5943. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5944. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5945. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5946. }
  5947. //Check for SelectedPaymentOption (TC_SECC_VTB_ServicePaymentSelection_007)
  5948. if (req->SelectedPaymentOption != ExternalPayment) //1
  5949. {
  5950. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_PaymentSelectionInvalid;
  5951. sprintf((char*)buf_log_evcomm,
  5952. "[ERROR]unexpected SelectedPaymentOption(%d) => End_Process (EIM only, no PnC, yet.)",
  5953. req->SelectedPaymentOption);
  5954. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5955. errn = -1;
  5956. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5957. //CCS_SECC_ResponseCode_FAILED_Payment SelectionInvalid (023762)
  5958. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5959. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5960. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5961. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5962. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  5963. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  5964. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5965. }
  5966. //Check for ServiceID (TC_SECC_VTB_ServicePaymentSelection_004)
  5967. if (req->SelectedServiceList.SelectedService[0].ServiceID != sd_res->ChargeService_DIN70121.Services.ServiceTag.ServiceID)
  5968. {
  5969. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_ServiceSelectionInvalid; //8
  5970. sprintf((char*)buf_log_evcomm,
  5971. "[ERROR]Wrong selected ServiceID(%d) => End_Process",
  5972. req->SelectedServiceList.SelectedService[0].ServiceID);
  5973. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5974. errn = -1;
  5975. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5976. //CCS_SECC_ResponseCode_FAILED_ServiceSelectionInvalid (023764)
  5977. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5978. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5979. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5980. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5981. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  5982. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  5983. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5984. }
  5985. //Detect for CP State should be 9V (State B)
  5986. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5987. {
  5988. #if CP_PROTECTION_MECHANISM == ENABLE
  5989. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  5990. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5991. Update_V2G_Flow_Status(Other_Fault);
  5992. errn = -1;
  5993. sprintf((char*)buf_log_evcomm,
  5994. "[Proc_din_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d)",
  5995. sys->CpState);
  5996. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  5997. #else
  5998. sprintf((char*)buf_log_evcomm,
  5999. "[Proc_din_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d): ignored due to function is disabled",
  6000. sys->CpState);
  6001. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6002. #endif
  6003. }
  6004. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  6005. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6006. {
  6007. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ServiceAndPaymentSelectionRes]shutdown by EVSE");
  6008. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  6009. errn = -1;
  6010. }
  6011. //Check for Permission Changing from TRUE to FALSE
  6012. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  6013. ShmInternalComm->ChargingPermission == FALSE)
  6014. {
  6015. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][ServicePaymentSelectionRes]Permission OFF");
  6016. ccs_exi_doc_DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  6017. errn = -1;
  6018. }
  6019. // ============ Encode and Send Response Message ===========
  6020. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  6021. {
  6022. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ServiceAndPaymentSelectionRes]Tx encoded msg error");
  6023. errn = -1;
  6024. }
  6025. return errn;
  6026. }
  6027. /*===========================================================================
  6028. FUNCTION: Proc_iso1_ServiceAndPaymentSelectionRes
  6029. DESCRIPTION:
  6030. PRE-CONDITION:
  6031. INPUT:
  6032. OUTPUT:
  6033. GLOBAL VARIABLES:
  6034. =============================================================================*/
  6035. int Proc_iso1_ServiceAndPaymentSelectionRes(int AcceptFd)
  6036. {
  6037. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6038. //if it is not the same, the packet should be ignored.
  6039. //int i = 0;
  6040. int errn = 0;
  6041. bitstream_t v2g_tx_stream;
  6042. struct ServiceAndPaymentSelectionRequest_ISO15118_2014 *req;
  6043. //struct ServiceAndPaymentSelectionResponse_ISO15118_2014 *res;
  6044. struct ServiceDiscoveryResponse_ISO15118_2014 *sd_res;
  6045. static struct ChargingInfoData *sys;
  6046. size_t pos = 0;
  6047. v2g_tx_stream.pos = &pos;
  6048. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6049. v2g_tx_stream.data = V2GTP_Tx_buf;
  6050. req = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceAndPaymentSelectionRequest;
  6051. //res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceAndPaymentSelectionResponse;
  6052. sd_res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryResponse;
  6053. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6054. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  6055. init_iso1PaymentServiceSelectionResType(&ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes);
  6056. //[1/1] Response Code
  6057. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes_isUsed = 1u;
  6058. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_OK;
  6059. //[HEADER] Check Req SessionID
  6060. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  6061. {
  6062. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  6063. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  6064. errn = -1;
  6065. }
  6066. //Check for SequenceError
  6067. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6068. {
  6069. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  6070. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  6071. errn = -1;
  6072. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6073. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6074. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6075. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6076. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6077. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6078. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6079. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6080. }
  6081. //Check for SelectedPaymentOption (TC_SECC_VTB_ServicePaymentSelection_007)
  6082. if (req->SelectedPaymentOption != ExternalPayment) //1
  6083. {
  6084. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_PaymentSelectionInvalid;
  6085. sprintf((char*)buf_log_evcomm,
  6086. "[ERROR]unexpected SelectedPaymentOption(%d) => End_Process (EIM only, no PnC, yet.)",
  6087. req->SelectedPaymentOption);
  6088. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6089. errn = -1;
  6090. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6091. //CCS_SECC_ResponseCode_FAILED_Payment SelectionInvalid (023762)
  6092. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6093. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6094. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6095. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6096. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  6097. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  6098. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6099. }
  6100. //Check for ServiceID (TC_SECC_VTB_ServicePaymentSelection_004)
  6101. if (req->SelectedServiceList.SelectedService[0].ServiceID != sd_res->ChargeService.Services.ServiceID)
  6102. {
  6103. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_ServiceSelectionInvalid; //8
  6104. sprintf((char*)buf_log_evcomm,
  6105. "[ERROR]Wrong selected ServiceID(%d) => End_Process",
  6106. req->SelectedServiceList.SelectedService[0].ServiceID);
  6107. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6108. errn = -1;
  6109. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6110. //CCS_SECC_ResponseCode_FAILED_ServiceSelectionInvalid (023764)
  6111. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6112. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6113. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6114. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6115. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  6116. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  6117. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6118. }
  6119. //Detect for CP State should be 9V (State B)
  6120. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  6121. {
  6122. #if CP_PROTECTION_MECHANISM == ENABLE
  6123. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  6124. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6125. Update_V2G_Flow_Status(Other_Fault);
  6126. errn = -1;
  6127. sprintf((char*)buf_log_evcomm,
  6128. "[Proc_iso1_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d)",
  6129. sys->CpState);
  6130. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6131. #else
  6132. sprintf((char*)buf_log_evcomm,
  6133. "[Proc_iso1_ServiceAndPaymentSelectionRes]Emergency Stop by CP Error (%d): ignored due to function is disabled",
  6134. sys->CpState);
  6135. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6136. #endif
  6137. }
  6138. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  6139. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6140. {
  6141. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ServiceAndPaymentSelectionRes]shutdown by EVSE");
  6142. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  6143. errn = -1;
  6144. }
  6145. //Check for Permission Changing from TRUE to FALSE
  6146. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  6147. ShmInternalComm->ChargingPermission == FALSE)
  6148. {
  6149. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO1][PaymentServiceSelectionRes]Permission OFF");
  6150. ccs_exi_doc_ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  6151. errn = -1;
  6152. }
  6153. // ============ Encode and Send Response Message ===========
  6154. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  6155. {
  6156. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ServiceAndPaymentSelectionRes]Tx encoded msg error");
  6157. errn = -1;
  6158. }
  6159. return errn;
  6160. }
  6161. /*===========================================================================
  6162. FUNCTION: Proc_din_ServiceAndPaymentSelectionReq
  6163. DESCRIPTION:
  6164. PRE-CONDITION:
  6165. INPUT:
  6166. OUTPUT:
  6167. GLOBAL VARIABLES:
  6168. =============================================================================*/
  6169. int Proc_din_ServiceAndPaymentSelectionReq(int AcceptFd)
  6170. {
  6171. int errn = 0;
  6172. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][Proc_din_ServiceAndPaymentSelectionReq]Got ServiceAndPaymentSelectionReq");
  6173. //Print the decoded XML Document
  6174. PRINT_XML_DOC_DIN_ServiceAndPaymentSelectionReq(&ccs_exi_doc_DIN);
  6175. //Save into Share Memory
  6176. SHM_Save_din_ServiceAndPaymentSelectionReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  6177. errn = Proc_din_ServiceAndPaymentSelectionRes(AcceptFd);
  6178. if (errn == 0)
  6179. {
  6180. //send response successfully.
  6181. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]ServiceAndPaymentSelectionRes");
  6182. }
  6183. else
  6184. {
  6185. sprintf((char*)buf_log_evcomm,
  6186. "[Error][Proc_iso1_ServiceAndPaymentSelectionReq]Proc_iso1_ServiceAndPaymentSelectionRes(): %d (DEC)",
  6187. errn);
  6188. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6189. }
  6190. return errn;
  6191. }
  6192. /*===========================================================================
  6193. FUNCTION: Proc_iso1_ServiceAndPaymentSelectionReq
  6194. DESCRIPTION:
  6195. PRE-CONDITION:
  6196. INPUT:
  6197. OUTPUT:
  6198. GLOBAL VARIABLES:
  6199. =============================================================================*/
  6200. int Proc_iso1_ServiceAndPaymentSelectionReq(int AcceptFd)
  6201. {
  6202. int errn = 0;
  6203. DEBUG_PRINTF_EVCOMM_DETAIL("[SeccComm][Proc_iso1_ServiceAndPaymentSelectionReq]Got ServiceAndPaymentSelectionReq");
  6204. //Print the decoded XML Document
  6205. PRINT_XML_DOC_ISO1_ServiceAndPaymentSelectionReq(&ccs_exi_doc_ISO1);
  6206. //Save into Share Memory
  6207. SHM_Save_iso1_ServiceAndPaymentSelectionReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  6208. errn = Proc_iso1_ServiceAndPaymentSelectionRes(AcceptFd);
  6209. if (errn == 0)
  6210. {
  6211. //send response successfully.
  6212. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]ServiceAndPaymentSelectionRes");
  6213. }
  6214. else
  6215. {
  6216. sprintf((char*)buf_log_evcomm,
  6217. "[Error][Proc_iso1_ServiceAndPaymentSelectionReq]Proc_iso1_ServiceAndPaymentSelectionRes(): %d (DEC)",
  6218. errn);
  6219. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6220. }
  6221. return errn;
  6222. }
  6223. /*===========================================================================
  6224. FUNCTION: Proc_din_ContractAuthenticationRes
  6225. DESCRIPTION:
  6226. PRE-CONDITION:
  6227. INPUT:
  6228. OUTPUT:
  6229. GLOBAL VARIABLES:
  6230. =============================================================================*/
  6231. int Proc_din_ContractAuthenticationRes(int AcceptFd)
  6232. {
  6233. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6234. //if it is not the same, the packet should be ignored.
  6235. //int i = 0;
  6236. int errn = 0;
  6237. bitstream_t v2g_tx_stream;
  6238. static struct ChargingInfoData *sys;
  6239. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6240. size_t pos = 0;
  6241. v2g_tx_stream.pos = &pos;
  6242. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6243. v2g_tx_stream.data = V2GTP_Tx_buf;
  6244. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  6245. init_dinContractAuthenticationResType(&ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes);
  6246. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes_isUsed = 1u;
  6247. //[BODY (1/2)] ResponseCode
  6248. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_OK;
  6249. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Ongoing; //0
  6250. //[HEADER] Check Req SessionID
  6251. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  6252. {
  6253. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  6254. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  6255. errn = -1;
  6256. }
  6257. //Check for SequenceError
  6258. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6259. {
  6260. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  6261. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  6262. errn = -1;
  6263. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6264. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6265. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6266. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6267. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6268. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6269. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6270. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6271. }
  6272. //Detect for CP State should be 9V (State B)
  6273. #if CP_PROTECTION_MECHANISM == ENABLE
  6274. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  6275. {
  6276. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6277. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6278. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6279. Update_V2G_Flow_Status(Other_Fault);
  6280. errn = -1;
  6281. sprintf((char*)buf_log_evcomm,
  6282. "[Proc_din_ContractAuthenticationRes]Emergency Stop by CP Error (%d)",
  6283. sys->CpState);
  6284. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6285. }
  6286. #endif
  6287. //Check for CSU command of "Stop by EVSE"
  6288. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6289. {
  6290. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6291. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6292. errn = -1;
  6293. }
  6294. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6295. {
  6296. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6297. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6298. errn = -1;
  6299. }
  6300. //[BODY (2/2)] EVSEProcessing
  6301. if(ShmInternalComm->ChargingPermission == TRUE)
  6302. {
  6303. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished; //0
  6304. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "CSU Permission: OK");
  6305. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  6306. {
  6307. //Set PWM as 5% (for SLAC first case)
  6308. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Set PWM as 5%%");
  6309. SwitchCpStateE(DISABLE);
  6310. OutputCpPwmDuty(5);
  6311. }
  6312. #endif
  6313. }
  6314. //Check for Permission Changing from TRUE to FALSE
  6315. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  6316. ShmInternalComm->ChargingPermission == FALSE)
  6317. {
  6318. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][ContractAuthenticationRes]Permission OFF");
  6319. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  6320. ccs_exi_doc_DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  6321. errn = -1;
  6322. }
  6323. #if PARAMETER_NORMAL_MODE == ENABLE
  6324. ////////////SHM_Read_din_ContractAuthenticationRes(&ccs_exi_doc_DIN, ShmCcsData);
  6325. #endif
  6326. // ============ Encode and Send Response Message ===========
  6327. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  6328. {
  6329. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  6330. errn = -1;
  6331. }
  6332. return errn;
  6333. }
  6334. /*===========================================================================
  6335. FUNCTION: Proc_iso1_AuthorizationRes
  6336. DESCRIPTION:
  6337. PRE-CONDITION:
  6338. INPUT:
  6339. OUTPUT:
  6340. GLOBAL VARIABLES:
  6341. =============================================================================*/
  6342. int Proc_iso1_AuthorizationRes(int AcceptFd)
  6343. {
  6344. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6345. //if it is not the same, the packet should be ignored.
  6346. //int i = 0;
  6347. int errn = 0;
  6348. bitstream_t v2g_tx_stream;
  6349. static struct ChargingInfoData *sys;
  6350. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6351. size_t pos = 0;
  6352. v2g_tx_stream.pos = &pos;
  6353. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6354. v2g_tx_stream.data = V2GTP_Tx_buf;
  6355. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  6356. init_iso1AuthorizationResType(&ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes);
  6357. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes_isUsed = 1u;
  6358. //[BODY (1/2)] ResponseCode
  6359. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_OK;
  6360. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Ongoing; //0
  6361. //[HEADER] Check Req SessionID
  6362. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  6363. {
  6364. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  6365. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  6366. errn = -1;
  6367. }
  6368. //Check for SequenceError
  6369. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6370. {
  6371. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  6372. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  6373. errn = -1;
  6374. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6375. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6376. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6377. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6378. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6379. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6380. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6381. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6382. }
  6383. //Detect for CP State should be 9V (State B)
  6384. #if CP_PROTECTION_MECHANISM == ENABLE
  6385. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  6386. {
  6387. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED;
  6388. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6389. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6390. Update_V2G_Flow_Status(Other_Fault);
  6391. errn = -1;
  6392. sprintf((char*)buf_log_evcomm,
  6393. "[Proc_iso1_AuthorizationRes]Emergency Stop by CP Error (%d)",
  6394. sys->CpState);
  6395. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6396. }
  6397. #endif
  6398. //Check for CSU command of "Stop by EVSE"
  6399. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6400. {
  6401. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  6402. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6403. errn = -1;
  6404. }
  6405. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6406. {
  6407. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  6408. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6409. errn = -1;
  6410. }
  6411. //[BODY (2/2)] EVSEProcessing
  6412. //Check for Permission from CSU
  6413. if(ShmInternalComm->ChargingPermission == TRUE)
  6414. {
  6415. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  6416. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "CSU Permission: OK");
  6417. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  6418. {
  6419. //Set PWM as 5% (for SLAC first case)
  6420. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Set PWM as 5%%");
  6421. SwitchCpStateE(DISABLE);
  6422. OutputCpPwmDuty(5);
  6423. }
  6424. #endif
  6425. }
  6426. //Check for Permission Changing from TRUE to FALSE
  6427. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  6428. ShmInternalComm->ChargingPermission == FALSE)
  6429. {
  6430. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO1][AuthorizationRes]Permission OFF");
  6431. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  6432. ccs_exi_doc_ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  6433. errn = -1;
  6434. }
  6435. #if PARAMETER_NORMAL_MODE == ENABLE
  6436. ////////////SHM_Read_iso1_AuthorizationRes(&ccs_exi_doc_ISO1, ShmCcsData);
  6437. #endif
  6438. // ============ Encode and Send Response Message ===========
  6439. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  6440. {
  6441. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  6442. errn = -1;
  6443. }
  6444. return errn;
  6445. }
  6446. /*===========================================================================
  6447. FUNCTION: Proc_din_ContractAuthenticationReq
  6448. DESCRIPTION:
  6449. PRE-CONDITION:
  6450. INPUT:
  6451. OUTPUT:
  6452. GLOBAL VARIABLES:
  6453. =============================================================================*/
  6454. int Proc_din_ContractAuthenticationReq(int AcceptFd)
  6455. {
  6456. int errn = 0;
  6457. DEBUG_PRINTF_EVCOMM_DETAIL("\n[SeccComm][Proc_din_ContractAuthenticationReq]Got Proc_din_ContractAuthenticationReq\n");
  6458. //Print the decoded XML Document
  6459. PRINT_XML_DOC_DIN_ContractAuthenticationReq(&ccs_exi_doc_DIN);
  6460. //Save into Share Memory
  6461. SHM_Save_din_ContractAuthenticationReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  6462. errn = Proc_din_ContractAuthenticationRes(AcceptFd);
  6463. if (errn == 0)
  6464. {
  6465. //send response successfully.
  6466. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]ContractAuthenticationRes");
  6467. }
  6468. else
  6469. {
  6470. sprintf((char*)buf_log_evcomm,
  6471. "[Error][SeccComm][Proc_din_ContractAuthenticationReq]Proc_iso1_AuthenticationRes(): %d (DEC)",
  6472. errn);
  6473. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6474. }
  6475. return errn;
  6476. }
  6477. /*===========================================================================
  6478. FUNCTION: Proc_iso1_AuthenticationReq
  6479. DESCRIPTION:
  6480. PRE-CONDITION:
  6481. INPUT:
  6482. OUTPUT:
  6483. GLOBAL VARIABLES:
  6484. =============================================================================*/
  6485. int Proc_iso1_AuthenticationReq(int AcceptFd)
  6486. {
  6487. int errn = 0;
  6488. DEBUG_PRINTF_EVCOMM_DETAIL("\n[SeccComm][Proc_iso1_AuthenticationReq]Got AuthorizationReq\n");
  6489. //Print the decoded XML Document
  6490. PRINT_XML_DOC_ISO1_AuthorizationReq(&ccs_exi_doc_ISO1);
  6491. //Save into Share Memory
  6492. SHM_Save_iso1_AuthorizationReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  6493. errn = Proc_iso1_AuthorizationRes(AcceptFd);
  6494. if (errn == 0)
  6495. {
  6496. //send response successfully.
  6497. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]AuthorizationRes");
  6498. }
  6499. else
  6500. {
  6501. sprintf((char*)buf_log_evcomm,
  6502. "[Error][SeccComm][Proc_iso1_AuthenticationReq]Proc_iso1_AuthenticationRes(): %d (DEC)",
  6503. errn);
  6504. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6505. }
  6506. return errn;
  6507. }
  6508. /*===========================================================================
  6509. FUNCTION: SHM_Init_dinDC_EVSEStatusType
  6510. DESCRIPTION:
  6511. PRE-CONDITION:
  6512. INPUT:
  6513. 1. in
  6514. OUTPUT:
  6515. 2. out
  6516. GLOBAL VARIABLES:
  6517. =============================================================================*/
  6518. void SHM_Init_dinDC_EVSEStatusType(struct DC_EVSEStatusType_DIN70121 *in)
  6519. {
  6520. in->EVSEIsolationStatus = dinisolationLevelType_Valid;
  6521. // dinisolationLevelType_Invalid = 0,
  6522. // dinisolationLevelType_Valid = 1, (default)
  6523. // dinisolationLevelType_Warning = 2,
  6524. // dinisolationLevelType_Fault = 3
  6525. in->EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  6526. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6527. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1, (default)
  6528. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6529. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6530. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6531. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6532. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6533. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  6534. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  6535. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  6536. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  6537. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  6538. in->EVSENotification = dinEVSENotificationType_None;
  6539. // dinEVSENotificationType_None = 0, (default)
  6540. // dinEVSENotificationType_StopCharging = 1,
  6541. // dinEVSENotificationType_ReNegotiation = 2
  6542. }
  6543. /*===========================================================================
  6544. FUNCTION: SHM_Save_dinPhysicalValueType
  6545. DESCRIPTION:
  6546. PRE-CONDITION:
  6547. INPUT:
  6548. OUTPUT:
  6549. GLOBAL VARIABLES:
  6550. =============================================================================*/
  6551. void SHM_Save_dinPhysicalValueType(struct PhysicalValueType_DIN70121 *obj, short value, int multiplier, unsigned char unit)
  6552. {
  6553. obj->Value = value;
  6554. obj->Multiplier = multiplier;
  6555. obj->Unit =unit;
  6556. }
  6557. /*===========================================================================
  6558. FUNCTION: SHM_Init_din_ChargeParameterDiscoveryRes
  6559. DESCRIPTION:
  6560. PRE-CONDITION:
  6561. INPUT:
  6562. 1. shm_ccs
  6563. OUTPUT:
  6564. 2. shm_ccs
  6565. GLOBAL VARIABLES:
  6566. =============================================================================*/
  6567. void SHM_Init_din_ChargeParameterDiscoveryRes(struct CcsData *shm_ccs)
  6568. {
  6569. struct ChargeParameterDiscoveryResponse_DIN70121 *in;
  6570. in = &shm_ccs->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse;
  6571. //----- [BODY (1/5)] ResponseCode -----
  6572. in->ResponseCode = dinresponseCodeType_OK;
  6573. //----- [BODY (2/5)] EVSEProcessing -----
  6574. in->EVSEProcessing = dinEVSEProcessingType_Ongoing; //0
  6575. // dinEVSEProcessingType_Finished = 0,
  6576. // dinEVSEProcessingType_Ongoing = 1
  6577. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  6578. //be fixed in another function.
  6579. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  6580. //ignore, since DIN doesn't support AC
  6581. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  6582. struct DC_EVSEChargeParameterType_DIN70121 *in_para;
  6583. in_para = &in->DC_EVSEChargeParameter;
  6584. //DC_EVSEStatus
  6585. SHM_Init_dinDC_EVSEStatusType(&in_para->DC_EVSEStatus);
  6586. in_para->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  6587. short value = 0; int multiplier = 0; unsigned char unit = 0;
  6588. //EVSEMaximumCurrentLimit
  6589. value = 600; multiplier = -1; unit = A_DIN70121; //60A
  6590. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumCurrentLimit, value, multiplier, unit);
  6591. //EVSEMaximumPowerLimit
  6592. value = 3000; multiplier = 1; unit = W_DIN70121; //30KW
  6593. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumPowerLimit, value, multiplier, unit);
  6594. //EVSEMaximumVoltageLimit
  6595. value = 7500; multiplier = -1; unit = V_DIN70121; //750V
  6596. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumVoltageLimit, value, multiplier, unit);
  6597. //EVSEMinimumVoltageLimit
  6598. value = 1500; multiplier = -1; unit = V_DIN70121; //150V
  6599. SHM_Save_dinPhysicalValueType(&in_para->EVSEMinimumVoltageLimit, value, multiplier, unit);
  6600. //EVSEMinimumCurrentLimit
  6601. value = 10; multiplier = -1; unit = A_DIN70121; //1A
  6602. SHM_Save_dinPhysicalValueType(&in_para->EVSEMinimumCurrentLimit, value, multiplier, unit);
  6603. //EVSECurrentRegulationTolerance
  6604. value = 10; multiplier = -1; unit = A_DIN70121; //1A
  6605. SHM_Save_dinPhysicalValueType(&in_para->EVSECurrentRegulationTolerance, value, multiplier, unit);
  6606. //EVSEPeakCurrentRipple
  6607. value = 2; multiplier = -1; unit = A_DIN70121; //0.2A
  6608. SHM_Save_dinPhysicalValueType(&in_para->EVSEPeakCurrentRipple, value, multiplier, unit);
  6609. //EVSEEnergyToBeDelivered (optional)
  6610. //SHM_Save_dinPhysicalValueType(&out_para->EVSEEnergyToBeDelivered, &in_para->EVSEEnergyToBeDelivered);
  6611. }
  6612. /*===========================================================================
  6613. FUNCTION: Sudo_Parameter_din_ChargeParameterDiscoveryRes
  6614. DESCRIPTION:
  6615. PRE-CONDITION:
  6616. INPUT:
  6617. OUTPUT:
  6618. GLOBAL VARIABLES:
  6619. =============================================================================*/
  6620. void Sudo_Parameter_din_ChargeParameterDiscoveryRes()
  6621. {
  6622. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  6623. init_dinChargeParameterDiscoveryResType(&ccs_exi_doc_DIN.V2G_Message.Body.ChargeParameterDiscoveryRes);
  6624. ccs_exi_doc_DIN.V2G_Message.Body.ChargeParameterDiscoveryRes_isUsed = 1u;
  6625. //----- [BODY (1/5)] ResponseCode -----
  6626. struct dinChargeParameterDiscoveryResType *res;
  6627. res = &ccs_exi_doc_DIN.V2G_Message.Body.ChargeParameterDiscoveryRes;
  6628. res->ResponseCode = dinresponseCodeType_OK;
  6629. //----- [BODY (2/5)] EVSEProcessing -----
  6630. res->EVSEProcessing = dinEVSEProcessingType_Finished; //0
  6631. // dinEVSEProcessingType_Finished = 0,
  6632. // dinEVSEProcessingType_Ongoing = 1
  6633. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  6634. res->SAScheduleList_isUsed = 1u;
  6635. struct dinSAScheduleListType *list;
  6636. list = &res->SAScheduleList;
  6637. //
  6638. list->SAScheduleTuple.arrayLen = 1;
  6639. list->SAScheduleTuple.array[0].SAScheduleTupleID = 0;
  6640. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleID = 0;
  6641. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.arrayLen = 1;
  6642. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval_isUsed = 1u;
  6643. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.start = 0;
  6644. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration = xxx
  6645. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration_isUsed = 1u;
  6646. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval_isUsed = 0u;
  6647. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval.xxx
  6648. //list->SAScheduleTuple.array[0].SalesTariff.xxx
  6649. //list->SAScheduleTuple.array[0].SalesTariff_isUsed = 0u;
  6650. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  6651. //ignore, since DIN doesn't support AC
  6652. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  6653. res->DC_EVSEChargeParameter_isUsed = 1u;
  6654. struct dinDC_EVSEChargeParameterType *para;
  6655. para = &res->DC_EVSEChargeParameter;
  6656. //DC_EVSEStatus
  6657. para->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  6658. para->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  6659. // dinisolationLevelType_Invalid = 0,
  6660. // dinisolationLevelType_Valid = 1,
  6661. // dinisolationLevelType_Warning = 2,
  6662. // dinisolationLevelType_Fault = 3
  6663. para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  6664. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6665. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  6666. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6667. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6668. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6669. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6670. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6671. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  6672. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  6673. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  6674. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  6675. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  6676. para->DC_EVSEStatus.NotificationMaxDelay = 0u;
  6677. para->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  6678. // dinEVSENotificationType_None = 0,
  6679. // dinEVSENotificationType_StopCharging = 1,
  6680. // dinEVSENotificationType_ReNegotiation = 2
  6681. //EVSEMaximumCurrentLimit
  6682. para->EVSEMaximumCurrentLimit.Value = 2400;
  6683. para->EVSEMaximumCurrentLimit.Multiplier = -1;
  6684. para->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  6685. para->EVSEMaximumCurrentLimit.Unit = dinunitSymbolType_A;
  6686. // dinunitSymbolType_h = 0,
  6687. // dinunitSymbolType_m = 1,
  6688. // dinunitSymbolType_s = 2,
  6689. // dinunitSymbolType_A = 3,
  6690. // dinunitSymbolType_Ah = 4,
  6691. // dinunitSymbolType_V = 5,
  6692. // dinunitSymbolType_VA = 6,
  6693. // dinunitSymbolType_W = 7,
  6694. // dinunitSymbolType_W_s = 8,
  6695. // dinunitSymbolType_Wh = 9
  6696. //EVSEMaximumPowerLimit
  6697. para->EVSEMaximumPowerLimit.Value = 6000;
  6698. para->EVSEMaximumPowerLimit.Multiplier = 1;
  6699. para->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  6700. para->EVSEMaximumPowerLimit.Unit = dinunitSymbolType_W;
  6701. //EVSEMaximumVoltageLimit
  6702. para->EVSEMaximumVoltageLimit.Value = 7500;
  6703. para->EVSEMaximumVoltageLimit.Multiplier = -1;
  6704. para->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  6705. para->EVSEMaximumVoltageLimit.Unit = dinunitSymbolType_V;
  6706. //EVSEMinimumVoltageLimit
  6707. para->EVSEMinimumVoltageLimit.Value = 1500;
  6708. para->EVSEMinimumVoltageLimit.Multiplier = -1;
  6709. para->EVSEMinimumVoltageLimit.Unit_isUsed = 1u;
  6710. para->EVSEMinimumVoltageLimit.Unit = dinunitSymbolType_V;
  6711. //EVSEMinimumCurrentLimit
  6712. para->EVSEMinimumCurrentLimit.Value = 20;
  6713. para->EVSEMinimumCurrentLimit.Multiplier = -1;
  6714. para->EVSEMinimumCurrentLimit.Unit_isUsed = 1u;
  6715. para->EVSEMinimumCurrentLimit.Unit = dinunitSymbolType_A;
  6716. //EVSECurrentRegulationTolerance_isUsed
  6717. para->EVSECurrentRegulationTolerance_isUsed = 1u;
  6718. //EVSECurrentRegulationTolerance
  6719. para->EVSECurrentRegulationTolerance.Value = 10;
  6720. para->EVSECurrentRegulationTolerance.Multiplier = -1;
  6721. para->EVSECurrentRegulationTolerance.Unit_isUsed = 1u;
  6722. para->EVSECurrentRegulationTolerance.Unit = dinunitSymbolType_A;
  6723. //EVSEEnergyToBeDelivered_isUsed
  6724. para->EVSEEnergyToBeDelivered_isUsed = 1u;
  6725. //EVSEPeakCurrentRipple
  6726. para->EVSEPeakCurrentRipple.Value = 2;
  6727. para->EVSEPeakCurrentRipple.Multiplier = -1;
  6728. para->EVSEPeakCurrentRipple.Unit_isUsed = 1u;
  6729. para->EVSEPeakCurrentRipple.Unit = dinunitSymbolType_A;
  6730. //EVSEEnergyToBeDelivered (optional)
  6731. /*
  6732. para->EVSEEnergyToBeDelivered.Value = 360;
  6733. para->EVSEEnergyToBeDelivered.Multiplier = 3;
  6734. para->EVSEEnergyToBeDelivered.Unit_isUsed = 1u;
  6735. para->EVSEEnergyToBeDelivered.Unit = dinunitSymbolType_Wh;
  6736. */
  6737. }
  6738. /*===========================================================================
  6739. FUNCTION: Sudo_Parameter_iso1_ChargeParameterDiscoveryRes
  6740. DESCRIPTION:
  6741. PRE-CONDITION:
  6742. INPUT:
  6743. OUTPUT:
  6744. GLOBAL VARIABLES:
  6745. =============================================================================*/
  6746. void Sudo_Parameter_iso1_ChargeParameterDiscoveryRes()
  6747. {
  6748. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  6749. init_iso1ChargeParameterDiscoveryResType(&ccs_exi_doc_ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes);
  6750. ccs_exi_doc_ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes_isUsed = 1u;
  6751. //----- [BODY (1/5)] ResponseCode -----
  6752. struct iso1ChargeParameterDiscoveryResType *res;
  6753. res = &ccs_exi_doc_ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes;
  6754. res->ResponseCode = iso1responseCodeType_OK;
  6755. //----- [BODY (2/5)] EVSEProcessing -----
  6756. res->EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  6757. // iso1EVSEProcessingType_Finished = 0,
  6758. // iso1EVSEProcessingType_Ongoing = 1
  6759. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  6760. res->SAScheduleList_isUsed = 1u;
  6761. struct iso1SAScheduleListType *list;
  6762. list = &res->SAScheduleList;
  6763. //
  6764. list->SAScheduleTuple.arrayLen = 1;
  6765. list->SAScheduleTuple.array[0].SAScheduleTupleID = 0;
  6766. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.arrayLen = 1;
  6767. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval_isUsed = 1u;
  6768. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.start = 0;
  6769. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration = xxx
  6770. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration_isUsed = 1u;
  6771. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval_isUsed = 0u;
  6772. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval.xxx
  6773. //list->SAScheduleTuple.array[0].SalesTariff.xxx
  6774. //list->SAScheduleTuple.array[0].SalesTariff_isUsed = 0u;
  6775. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  6776. //ignore, since our ISO1 doesn't support AC, yet
  6777. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  6778. res->DC_EVSEChargeParameter_isUsed = 1u;
  6779. struct iso1DC_EVSEChargeParameterType *para;
  6780. para = &res->DC_EVSEChargeParameter;
  6781. //DC_EVSEStatus
  6782. para->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  6783. para->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  6784. // iso1isolationLevelType_Invalid = 0,
  6785. // iso1isolationLevelType_Valid = 1,
  6786. // iso1isolationLevelType_Warning = 2,
  6787. // iso1isolationLevelType_Fault = 3
  6788. para->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  6789. // iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6790. // iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  6791. // iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6792. // iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6793. // iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6794. // iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6795. // iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6796. // iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  6797. // iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  6798. // iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  6799. // iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  6800. // iso1DC_EVSEStatusCodeType_Reserved_C = 11
  6801. para->DC_EVSEStatus.NotificationMaxDelay = 0u;
  6802. para->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  6803. // iso1EVSENotificationType_None = 0,
  6804. // iso1EVSENotificationType_StopCharging = 1,
  6805. // iso1EVSENotificationType_ReNegotiation = 2
  6806. //EVSEMaximumCurrentLimit
  6807. para->EVSEMaximumCurrentLimit.Value = 2400;
  6808. para->EVSEMaximumCurrentLimit.Multiplier = -1;
  6809. //para->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  6810. para->EVSEMaximumCurrentLimit.Unit = iso1unitSymbolType_A;
  6811. //iso1unitSymbolType_h = 0,
  6812. //iso1unitSymbolType_m = 1,
  6813. //iso1unitSymbolType_s = 2,
  6814. //iso1unitSymbolType_A = 3,
  6815. //iso1unitSymbolType_V = 4,
  6816. //iso1unitSymbolType_W = 5,
  6817. //iso1unitSymbolType_Wh = 6
  6818. //EVSEMaximumPowerLimit
  6819. para->EVSEMaximumPowerLimit.Value = 6000;
  6820. para->EVSEMaximumPowerLimit.Multiplier = 1;
  6821. //para->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  6822. para->EVSEMaximumPowerLimit.Unit = iso1unitSymbolType_W;
  6823. //EVSEMaximumVoltageLimit
  6824. para->EVSEMaximumVoltageLimit.Value = 7500;
  6825. para->EVSEMaximumVoltageLimit.Multiplier = -1;
  6826. //para->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  6827. para->EVSEMaximumVoltageLimit.Unit = iso1unitSymbolType_V;
  6828. //EVSEMinimumVoltageLimit
  6829. para->EVSEMinimumVoltageLimit.Value = 1500;
  6830. para->EVSEMinimumVoltageLimit.Multiplier = -1;
  6831. //para->EVSEMinimumVoltageLimit.Unit_isUsed = 1u;
  6832. para->EVSEMinimumVoltageLimit.Unit = iso1unitSymbolType_V;
  6833. //EVSEMinimumCurrentLimit
  6834. para->EVSEMinimumCurrentLimit.Value = 20;
  6835. para->EVSEMinimumCurrentLimit.Multiplier = -1;
  6836. //para->EVSEMinimumCurrentLimit.Unit_isUsed = 1u;
  6837. para->EVSEMinimumCurrentLimit.Unit = iso1unitSymbolType_A;
  6838. //EVSECurrentRegulationTolerance_isUsed
  6839. para->EVSECurrentRegulationTolerance_isUsed = 1u;
  6840. //EVSECurrentRegulationTolerance
  6841. para->EVSECurrentRegulationTolerance.Value = 10;
  6842. para->EVSECurrentRegulationTolerance.Multiplier = -1;
  6843. //para->EVSECurrentRegulationTolerance.Unit_isUsed = 1u;
  6844. para->EVSECurrentRegulationTolerance.Unit = iso1unitSymbolType_A;
  6845. //EVSEEnergyToBeDelivered_isUsed
  6846. para->EVSEEnergyToBeDelivered_isUsed = 1u;
  6847. //EVSEPeakCurrentRipple
  6848. para->EVSEPeakCurrentRipple.Value = 2;
  6849. para->EVSEPeakCurrentRipple.Multiplier = -1;
  6850. //para->EVSEPeakCurrentRipple.Unit_isUsed = 1u;
  6851. para->EVSEPeakCurrentRipple.Unit = iso1unitSymbolType_A;
  6852. //EVSEEnergyToBeDelivered (optional)
  6853. /*
  6854. para->EVSEEnergyToBeDelivered.Value = 360;
  6855. para->EVSEEnergyToBeDelivered.Multiplier = 3;
  6856. para->EVSEEnergyToBeDelivered.Unit_isUsed = 1u;
  6857. para->EVSEEnergyToBeDelivered.Unit = iso1unitSymbolType_Wh;
  6858. */
  6859. }
  6860. /*===========================================================================
  6861. FUNCTION: Proc_din_ChargeParameterDiscoveryRes
  6862. DESCRIPTION:
  6863. PRE-CONDITION:
  6864. INPUT:
  6865. OUTPUT:
  6866. GLOBAL VARIABLES:
  6867. =============================================================================*/
  6868. int Proc_din_ChargeParameterDiscoveryRes(int AcceptFd)
  6869. {
  6870. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  6871. //if it is not the same, the packet should be ignored.
  6872. //STEP 1: ============ Initialize ============
  6873. int errn = 0;
  6874. bitstream_t v2g_tx_stream;
  6875. struct ChargeParameterDiscoveryResponse_DIN70121 *res;
  6876. struct ChargeParameterDiscoveryRequest_DIN70121 *req;
  6877. struct DC_EVSEChargeParameterType_DIN70121 *dc_para;
  6878. struct ChargingInfoData *sys;
  6879. size_t pos = 0;
  6880. v2g_tx_stream.pos = &pos;
  6881. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6882. v2g_tx_stream.data = V2GTP_Tx_buf;
  6883. res = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse;
  6884. req = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest;
  6885. dc_para = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse.DC_EVSEChargeParameter;
  6886. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6887. res->ResponseCode = OK_DIN70121;
  6888. res->EVSEProcessing = Ongoing_DIN70121;
  6889. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready; //1
  6890. //[HEADER] Check Req SessionID
  6891. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  6892. {
  6893. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  6894. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  6895. errn = -1;
  6896. }
  6897. //Check for SequenceError
  6898. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6899. {
  6900. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  6901. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  6902. errn = -1;
  6903. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6904. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6905. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6906. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6907. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6908. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6909. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6910. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6911. }
  6912. //[TC_SECC_VTB_ChargeParameterDiscovery_005]
  6913. if (sys->EvBatteryMaxCurrent < 0)
  6914. {
  6915. sprintf((char*)buf_log_evcomm,
  6916. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  6917. sys->EvBatteryMaxCurrent);
  6918. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6919. res->ResponseCode = dinresponseCodeType_FAILED_WrongChargeParameter; //16
  6920. res->EVSEProcessing = Finished_DIN70121;
  6921. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  6922. errn = -1;
  6923. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6924. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  6925. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6926. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6927. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6928. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6929. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  6930. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  6931. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6932. }
  6933. //STEP 2: ============ Modify Parameters of ShmCcsData ============
  6934. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_DIN70121);
  6935. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_DIN70121);
  6936. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_DIN70121);
  6937. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMinimumVoltageLimit, 1500, V_DIN70121); //150V
  6938. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMinimumCurrentLimit, 10, A_DIN70121); //1A
  6939. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSECurrentRegulationTolerance, 10, A_DIN70121); //1A
  6940. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEPeakCurrentRipple, 2, A_DIN70121); //0.2A
  6941. //SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEEnergyToBeDelivered, 0, WH_DIN70121); //optional
  6942. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  6943. if (sys->EvBatteryMaxVoltage <= 500)
  6944. {
  6945. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumVoltageLimit, (int) (500 * 10), V_DIN70121); //500V
  6946. }
  6947. #endif
  6948. //for test with Tesla Model 3, 10A
  6949. //SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumCurrentLimit, (int) (10 * 10), A_DIN70121); //10A, for testing with Tesla Model 3
  6950. //Check for EnergyTransferMode [TC_SECC_VTB_ChargeParameterDiscovery_004]
  6951. if (req->EVRequestedEnergyTransferType != DC_extended)
  6952. {
  6953. sprintf((char*)buf_log_evcomm,
  6954. "[ERROR]Wrong EVRequestedEnergyTransferType(%d,%d)",
  6955. req->EVRequestedEnergyTransferType,
  6956. dinEVSESupportedEnergyTransferType_DC_extended);
  6957. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  6958. res->ResponseCode = FAILED_WrongEnergyTransferType_DIN70121;
  6959. res->EVSEProcessing = Finished_DIN70121;
  6960. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  6961. errn = -1;
  6962. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6963. Update_V2G_Flow_Status(Other_Fault);
  6964. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6965. //CCS_SECC_ResponseCode_FAILED_WrongEnergyTransferMode (023774)
  6966. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6967. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6968. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6969. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6970. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  6971. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  6972. }
  6973. //Check for CSU command of "Stop by EVSE"
  6974. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6975. {
  6976. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ChargeParameterDiscoveryRes]EVSE_Shutdown");
  6977. res->ResponseCode = FAILED_DIN70121;
  6978. res->EVSEProcessing = Finished_DIN70121;
  6979. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  6980. errn = -1;
  6981. }
  6982. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6983. {
  6984. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ChargeParameterDiscoveryRes]EVSE_EmergencyShutdown");
  6985. res->ResponseCode = FAILED_DIN70121;
  6986. res->EVSEProcessing = Finished_DIN70121;
  6987. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6988. errn = -1;
  6989. }
  6990. //Detect for CP State should be 9V (State B)
  6991. #if CP_PROTECTION_MECHANISM == ENABLE
  6992. if (sys->CpState != 3) //State B1, B2
  6993. {
  6994. res->ResponseCode = FAILED_DIN70121;
  6995. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6996. res->EVSEProcessing = Finished_DIN70121;
  6997. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6998. Update_V2G_Flow_Status(Other_Fault);
  6999. errn = -1;
  7000. sprintf((char*)buf_log_evcomm,
  7001. "[Proc_din_ChargeParameterDiscoveryRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  7002. sys->CpState,
  7003. sys->CpVoltage);
  7004. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7005. }
  7006. #endif
  7007. //Check for Permission from CSU
  7008. if (ShmInternalComm->ChargingPermission == TRUE)
  7009. {
  7010. res->EVSEProcessing = Finished_DIN70121;
  7011. }
  7012. //Check for Permission Off
  7013. if (ShmInternalComm->ChargingPermission == FALSE)
  7014. {
  7015. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][DIN][ChargeParameterDiscoveryRes]Permission OFF");
  7016. res->ResponseCode = FAILED_DIN70121;
  7017. res->EVSEProcessing = Finished_DIN70121;
  7018. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7019. errn = -1;
  7020. }
  7021. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7022. #if PARAMETER_NORMAL_MODE == ENABLE
  7023. SHM_Read_din_ChargeParameterDiscoveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  7024. #else
  7025. Sudo_Parameter_din_ChargeParameterDiscoveryRes();
  7026. #endif
  7027. //STEP 4: ============ Encode and Send Response Message ===========
  7028. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  7029. {
  7030. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  7031. errn = -1;
  7032. }
  7033. //STPE 5: ============ Update Flags ============
  7034. return errn;
  7035. }
  7036. /*===========================================================================
  7037. FUNCTION: Proc_iso1_ChargeParameterDiscoveryRes
  7038. DESCRIPTION:
  7039. PRE-CONDITION:
  7040. INPUT:
  7041. OUTPUT:
  7042. GLOBAL VARIABLES:
  7043. =============================================================================*/
  7044. int Proc_iso1_ChargeParameterDiscoveryRes(int AcceptFd)
  7045. {
  7046. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  7047. //if it is not the same, the packet should be ignored.
  7048. //STEP 1: ============ Initialize ============
  7049. int errn = 0;
  7050. bitstream_t v2g_tx_stream;
  7051. struct ChargeParameterDiscoveryResponse_ISO15118_2014 *res;
  7052. struct ChargeParameterDiscoveryRequest_ISO15118_2014 *req;
  7053. struct DC_EVSEChargeParameterType_ISO15118_2014 *dc_para;
  7054. struct AC_EVSEChargeParameterType_ISO15118_2014 *ac_para;
  7055. struct ChargingInfoData *sys;
  7056. size_t pos = 0;
  7057. v2g_tx_stream.pos = &pos;
  7058. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7059. v2g_tx_stream.data = V2GTP_Tx_buf;
  7060. res = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse;
  7061. req = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest;
  7062. dc_para = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse.DC_EVSEChargeParameter;
  7063. ac_para = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse.AC_EVSEChargeParameter;
  7064. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7065. res->ResponseCode = OK_ISO15118_2014;
  7066. res->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  7067. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready; //1
  7068. ac_para->AC_EVSEStatus.RCD = ShmInternalComm->AC_RcdStatus; //0:no error, 1:error
  7069. ac_para->AC_EVSEStatus.NotificationMaxDelay = 0; //unit: 1s
  7070. ac_para->AC_EVSEStatus.EVSENotification = ShmInternalComm->AC_EVSENotification; //0:none, 1:StopCharging, 2:RenNgotiation
  7071. //[HEADER] Check Req SessionID
  7072. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  7073. {
  7074. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  7075. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  7076. errn = -1;
  7077. }
  7078. //Check for SequenceError
  7079. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7080. {
  7081. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  7082. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  7083. errn = -1;
  7084. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7085. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7086. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7087. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7088. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7089. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7090. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7091. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7092. }
  7093. //[TC_SECC_VTB_ChargeParameterDiscovery_005]
  7094. if (sys->EvBatteryMaxCurrent < 0)
  7095. {
  7096. sprintf((char*)buf_log_evcomm,
  7097. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  7098. sys->EvBatteryMaxCurrent);
  7099. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7100. res->ResponseCode = iso1responseCodeType_FAILED_WrongChargeParameter; //16
  7101. res->EVSEProcessing = Finished_ISO15118_2014;
  7102. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7103. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7104. errn = -1;
  7105. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7106. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  7107. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7108. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7109. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7110. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7111. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  7112. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  7113. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7114. }
  7115. //STEP 2: ============ Modify Parameters of ShmCcsData ============
  7116. //DC
  7117. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_ISO15118_2014);
  7118. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  7119. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_ISO15118_2014);
  7120. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMinimumVoltageLimit, 1500, V_ISO15118_2014); //150V
  7121. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMinimumCurrentLimit, 10, A_ISO15118_2014); //1A
  7122. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSECurrentRegulationTolerance, 10, A_ISO15118_2014); //1A
  7123. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEPeakCurrentRipple, 2, A_ISO15118_2014); //0.2A
  7124. //SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEEnergyToBeDelivered, 0, Wh_ISO15118_2014); //optional
  7125. //AC
  7126. SAVE_PhysicalValueType_ISO15118_2014(&ac_para->EVSENominalVoltage, (int) (sys->AC_EVSENominalVoltage * 10), V_ISO15118_2014);
  7127. SAVE_PhysicalValueType_ISO15118_2014(&ac_para->EVSEMaxCurrent, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  7128. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  7129. if (sys->EvBatteryMaxVoltage <= 500)
  7130. {
  7131. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumVoltageLimit, (int) (500 * 10), V_ISO15118_2014); //500V
  7132. }
  7133. #endif
  7134. //for test with Tesla Model 3, 10A
  7135. //SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumCurrentLimit, (int) (10 * 10), A_ISO15118_2014); //10A, for testing with Tesla Model 3
  7136. //Check for EnergyTransferMode
  7137. if (req->RequestedEnergyTransferMode != ShmCcsData->EnergyTransferMode) //[CAUTION] Their table should be kept as the same.
  7138. {
  7139. sprintf((char*)buf_log_evcomm,
  7140. "[ERROR]Unmatched RequestedEnergyTransferMode(%d,%d)",
  7141. req->RequestedEnergyTransferMode,
  7142. ShmCcsData->EnergyTransferMode);
  7143. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7144. res->ResponseCode = FAILED_WrongEnergyTransferMode_ISO15118_2014;
  7145. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7146. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7147. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7148. errn = -1;
  7149. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7150. Update_V2G_Flow_Status(Other_Fault);
  7151. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7152. //CCS_SECC_ResponseCode_FAILED_WrongEnergyTransferMode (023774)
  7153. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7154. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7155. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7156. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7157. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  7158. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  7159. }
  7160. //Check for CSU command of "Stop by EVSE"
  7161. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7162. {
  7163. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargeParameterDiscoveryRes]EVSE_Shutdown");
  7164. res->ResponseCode = FAILED_ISO15118_2014;
  7165. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7166. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7167. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7168. errn = -1;
  7169. }
  7170. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7171. {
  7172. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargeParameterDiscoveryRes]EVSE_EmergencyShutdown");
  7173. res->ResponseCode = FAILED_ISO15118_2014;
  7174. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7175. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7176. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7177. errn = -1;
  7178. }
  7179. //Detect for CP State should be 9V (State B)
  7180. #if CP_PROTECTION_MECHANISM == ENABLE
  7181. if (sys->CpState != 3) //State B1, B2
  7182. {
  7183. res->ResponseCode = FAILED_ISO15118_2014;
  7184. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7185. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7186. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7187. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7188. Update_V2G_Flow_Status(Other_Fault);
  7189. errn = -1;
  7190. sprintf((char*)buf_log_evcomm,
  7191. "[Proc_iso1_ChargeParameterDiscoveryRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  7192. sys->CpState,
  7193. sys->CpVoltage);
  7194. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7195. }
  7196. #endif
  7197. //Check for Permission from CSU
  7198. if(ShmInternalComm->ChargingPermission == TRUE)
  7199. {
  7200. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7201. }
  7202. //Check for Permission Off
  7203. if (ShmInternalComm->ChargingPermission == FALSE)
  7204. {
  7205. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][ISO1][ChargeParameterDiscoveryRes]Permission OFF");
  7206. res->ResponseCode = FAILED_ISO15118_2014;
  7207. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7208. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7209. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  7210. errn = -1;
  7211. }
  7212. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7213. #if PARAMETER_NORMAL_MODE == ENABLE
  7214. SHM_Read_iso1_ChargeParameterDiscoveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  7215. #else
  7216. Sudo_Parameter_iso1_ChargeParameterDiscoveryRes();
  7217. #endif
  7218. //STEP 4: ============ Encode and Send Response Message ===========
  7219. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  7220. {
  7221. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargeParameterDiscoveryRes][Error]Tx encoded msg error");
  7222. errn = -1;
  7223. }
  7224. //STPE 5: ============ Update Flags ============
  7225. return errn;
  7226. }
  7227. /*===========================================================================
  7228. FUNCTION: Proc_din_ChargeParameterDiscoveryReq
  7229. DESCRIPTION:
  7230. PRE-CONDITION:
  7231. INPUT:
  7232. OUTPUT:
  7233. GLOBAL VARIABLES:
  7234. =============================================================================*/
  7235. int Proc_din_ChargeParameterDiscoveryReq(int AcceptFd)
  7236. {
  7237. int errn = 0;
  7238. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_ChargeParameterDiscoveryReq] Got ChargeParameterDiscoveryReq");
  7239. //ftime(&SeqStartTime);
  7240. //Print the decoded XML Document
  7241. PRINT_XML_DOC_DIN_ChargeParameterDiscoveryReq(&ccs_exi_doc_DIN);
  7242. //Save into Share Memory
  7243. SHM_Save_din_ChargeParameterDiscoveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  7244. //Check for EV Error Code
  7245. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.DC_EVStatus.EVErrorCode);
  7246. errn = Proc_din_ChargeParameterDiscoveryRes(AcceptFd);
  7247. //ftime(&SeqEndTime);
  7248. //DEBUG_PRINTF_EVCOMM_DETAIL("\ndelta = %.02lf ms\n", DiffTimeb(SeqStartTime, SeqEndTime));
  7249. if ( errn == 0)
  7250. {
  7251. //send response successfully.
  7252. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]ChargeParameterDiscoveryRes");
  7253. }
  7254. else
  7255. {
  7256. sprintf((char*)buf_log_evcomm,
  7257. "[Error][DIN][ChargeParameterDiscoveryRes]%d (DEC)",
  7258. errn);
  7259. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7260. }
  7261. return errn;
  7262. }
  7263. /*===========================================================================
  7264. FUNCTION: Proc_iso1_ChargeParameterDiscoveryReq
  7265. DESCRIPTION:
  7266. PRE-CONDITION:
  7267. INPUT:
  7268. OUTPUT:
  7269. GLOBAL VARIABLES:
  7270. =============================================================================*/
  7271. int Proc_iso1_ChargeParameterDiscoveryReq(int AcceptFd)
  7272. {
  7273. int errn = 0;
  7274. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargeParameterDiscoveryReq] Got ChargeParameterDiscoveryReq");
  7275. //ftime(&SeqStartTime);
  7276. //Print the decoded XML Document
  7277. PRINT_XML_DOC_ISO1_ChargeParameterDiscoveryReq(&ccs_exi_doc_ISO1);
  7278. //Save into Share Memory
  7279. SHM_Save_iso1_ChargeParameterDiscoveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  7280. //Check for EV Error Code
  7281. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.DC_EVStatus.EVErrorCode);
  7282. errn = Proc_iso1_ChargeParameterDiscoveryRes(AcceptFd);
  7283. //ftime(&SeqEndTime);
  7284. //DEBUG_PRINTF_EVCOMM_DETAIL("\ndelta = %.02lf ms\n", DiffTimeb(SeqStartTime, SeqEndTime));
  7285. if (errn == 0)
  7286. {
  7287. //send response successfully.
  7288. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]ChargeParameterDiscoveryRes");
  7289. }
  7290. else
  7291. {
  7292. sprintf((char*)buf_log_evcomm,
  7293. "[Error][ISO1][ChargeParameterDiscoveryRes]%d (DEC)",
  7294. errn);
  7295. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7296. }
  7297. return errn;
  7298. }
  7299. /*===========================================================================
  7300. FUNCTION: SHM_Init_din_CableCheckRes
  7301. DESCRIPTION:
  7302. PRE-CONDITION:
  7303. INPUT:
  7304. 1. shm_ccs
  7305. OUTPUT:
  7306. 1. shm_ccs
  7307. GLOBAL VARIABLES:
  7308. =============================================================================*/
  7309. void SHM_Init_din_CableCheckRes(struct CcsData *shm_ccs)
  7310. {
  7311. struct CableCheckResponse_DIN70121 *in;
  7312. in = &shm_ccs->V2GMessage_DIN70121.CableCheckResponse;
  7313. //----- [BODY (1/3)] ResponseCode -----
  7314. in->ResponseCode = dinresponseCodeType_OK;
  7315. //----- [BODY (2/3)] EVSEProcessing -----
  7316. //in->EVSEProcessing = dinEVSEProcessingType_Finished; //for test
  7317. in->EVSEProcessing = dinEVSEProcessingType_Ongoing; //default
  7318. // dinEVSEProcessingType_Finished = 0,
  7319. // dinEVSEProcessingType_Ongoing = 1
  7320. //----- [BODY (3/3)] DC_EVSEStatus -----
  7321. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  7322. in->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0 (default)
  7323. // dinisolationLevelType_Invalid = 0,
  7324. // dinisolationLevelType_Valid = 1, (default)
  7325. // dinisolationLevelType_Warning = 2,
  7326. // dinisolationLevelType_Fault = 3
  7327. }
  7328. /*===========================================================================
  7329. FUNCTION: Sudo_Parameter_din_CableCheckRes
  7330. DESCRIPTION:
  7331. PRE-CONDITION:
  7332. INPUT:
  7333. OUTPUT:
  7334. GLOBAL VARIABLES:
  7335. =============================================================================*/
  7336. void Sudo_Parameter_din_CableCheckRes()
  7337. {
  7338. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  7339. init_dinCableCheckResType(&ccs_exi_doc_DIN.V2G_Message.Body.CableCheckRes);
  7340. ccs_exi_doc_DIN.V2G_Message.Body.CableCheckRes_isUsed = 1u;
  7341. //----- [BODY (1/3)] ResponseCode -----
  7342. struct dinCableCheckResType *res;
  7343. res = &ccs_exi_doc_DIN.V2G_Message.Body.CableCheckRes;
  7344. res->ResponseCode = dinresponseCodeType_OK;
  7345. //----- [BODY (2/3)] EVSEProcessing -----
  7346. res->EVSEProcessing = dinEVSEProcessingType_Finished; //0
  7347. // dinEVSEProcessingType_Finished = 0,
  7348. // dinEVSEProcessingType_Ongoing = 1
  7349. //----- [BODY (3/3)] DC_EVSEStatus -----
  7350. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7351. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  7352. // dinisolationLevelType_Invalid = 0,
  7353. // dinisolationLevelType_Valid = 1,
  7354. // dinisolationLevelType_Warning = 2,
  7355. // dinisolationLevelType_Fault = 3
  7356. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7357. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7358. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  7359. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7360. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7361. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7362. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7363. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7364. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  7365. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  7366. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  7367. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  7368. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  7369. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7370. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  7371. // dinEVSENotificationType_None = 0,
  7372. // dinEVSENotificationType_StopCharging = 1,
  7373. // dinEVSENotificationType_ReNegotiation = 2
  7374. }
  7375. /*===========================================================================
  7376. FUNCTION: Sudo_Parameter_iso1_CableCheckRes
  7377. DESCRIPTION:
  7378. PRE-CONDITION:
  7379. INPUT:
  7380. OUTPUT:
  7381. GLOBAL VARIABLES:
  7382. =============================================================================*/
  7383. void Sudo_Parameter_iso1_CableCheckRes()
  7384. {
  7385. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  7386. init_iso1CableCheckResType(&ccs_exi_doc_ISO1.V2G_Message.Body.CableCheckRes);
  7387. ccs_exi_doc_ISO1.V2G_Message.Body.CableCheckRes_isUsed = 1u;
  7388. //----- [BODY (1/3)] ResponseCode -----
  7389. struct iso1CableCheckResType *res;
  7390. res = &ccs_exi_doc_ISO1.V2G_Message.Body.CableCheckRes;
  7391. res->ResponseCode = iso1responseCodeType_OK;
  7392. //----- [BODY (2/3)] EVSEProcessing -----
  7393. res->EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  7394. //iso1EVSEProcessingType_Finished = 0,
  7395. //iso1EVSEProcessingType_Ongoing = 1,
  7396. //iso1EVSEProcessingType_Ongoing_WaitingForCustomerInteraction = 2
  7397. //----- [BODY (3/3)] DC_EVSEStatus -----
  7398. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7399. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  7400. //iso1isolationLevelType_Invalid = 0,
  7401. //iso1isolationLevelType_Valid = 1,
  7402. //iso1isolationLevelType_Warning = 2,
  7403. //iso1isolationLevelType_Fault = 3,
  7404. //iso1isolationLevelType_No_IMD = 4
  7405. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  7406. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7407. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  7408. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7409. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7410. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7411. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7412. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7413. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  7414. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  7415. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  7416. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  7417. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  7418. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7419. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  7420. //iso1EVSENotificationType_None = 0,
  7421. //iso1EVSENotificationType_StopCharging = 1,
  7422. //iso1EVSENotificationType_ReNegotiation = 2
  7423. }
  7424. /*===========================================================================
  7425. FUNCTION: Proc_din_CableCheckRes
  7426. DESCRIPTION:
  7427. PRE-CONDITION:
  7428. INPUT:
  7429. OUTPUT:
  7430. GLOBAL VARIABLES:
  7431. =============================================================================*/
  7432. int Proc_din_CableCheckRes(int AcceptFd)
  7433. {
  7434. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  7435. //if it is not the same, the packet should be ignored.
  7436. //STEP 1: ============ Initialize ============
  7437. //int i = 0;
  7438. int errn = 0;
  7439. bitstream_t v2g_tx_stream;
  7440. static struct CableCheckResponse_DIN70121 *cab;
  7441. static struct ChargingInfoData *sys;
  7442. size_t pos = 0;
  7443. v2g_tx_stream.pos = &pos;
  7444. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7445. v2g_tx_stream.data = V2GTP_Tx_buf;
  7446. cab = &ShmCcsData->V2GMessage_DIN70121.CableCheckResponse;
  7447. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7448. cab->ResponseCode = OK_DIN70121;
  7449. cab->cnt++;
  7450. DEBUG_PRINTF_EVCOMM_DETAIL("CP_State = %d (%.02f V), V_now = %.02f, Isolation Status = %d, EVSEProcessing = %d\n",
  7451. sys->CpState,
  7452. sys->CpVoltage,
  7453. sys->PresentChargingVoltage,
  7454. sys->IsolationStatus,
  7455. cab->EVSEProcessing);
  7456. //[HEADER] Check Req SessionID
  7457. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  7458. {
  7459. cab->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  7460. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  7461. errn = -1;
  7462. }
  7463. //Check for SequenceError
  7464. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7465. {
  7466. cab->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  7467. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  7468. errn = -1;
  7469. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7470. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7471. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7472. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7473. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7474. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7475. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7476. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7477. }
  7478. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  7479. if (sys->IsolationStatus == GFD_Invalid) //0: on going
  7480. {
  7481. //For PSU
  7482. sys->EvBatterytargetVoltage = CABLECHECK_TARGET_VOLTAGE; //500V //[To-Do] this should be modified to auto decision.
  7483. sys->EvBatterytargetCurrent = CABLECHECK_TARGET_CURRENT; //2A(default), unit: 1A
  7484. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  7485. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive; //4
  7486. cab->EVSEProcessing = dinEVSEProcessingType_Ongoing;
  7487. }
  7488. else if ((sys->IsolationStatus == GFD_Valid) || (sys->IsolationStatus == GFD_Warning)) //1: valid, 2: warning
  7489. {
  7490. //For PSU
  7491. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7492. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7493. if (sys->PresentChargingVoltage < 60) // < 60V
  7494. {
  7495. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  7496. sprintf((char*)buf_log_evcomm, "[V2G][CableCheck]Pass (V_now = %.02f, Isolated = %d)",
  7497. sys->PresentChargingVoltage,
  7498. sys->IsolationStatus);
  7499. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7500. //IsolationStatus
  7501. if (sys->IsolationStatus == GFD_Valid)
  7502. {
  7503. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  7504. }
  7505. else if (sys->IsolationStatus == GFD_Warning)
  7506. {
  7507. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning;
  7508. }
  7509. else
  7510. {
  7511. sprintf((char*)buf_log_evcomm,
  7512. "[WARNING]unexpected IsolationStatus(%d)",
  7513. sys->IsolationStatus);
  7514. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7515. }
  7516. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7517. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7518. }
  7519. else
  7520. {
  7521. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  7522. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  7523. cab->EVSEProcessing = dinEVSEProcessingType_Ongoing;
  7524. }
  7525. }
  7526. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  7527. {
  7528. //For PSU
  7529. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7530. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7531. cab->ResponseCode = FAILED_DIN70121;
  7532. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7533. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_NotReady;
  7534. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7535. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_CableCheckRes]IsolationStatus = 3 (fault)");
  7536. errn = -1;
  7537. }
  7538. else
  7539. {
  7540. //For PSU
  7541. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7542. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7543. cab->ResponseCode = FAILED_DIN70121;
  7544. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  7545. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_NotReady;
  7546. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7547. sprintf((char*)buf_log_evcomm,
  7548. "[Proc_din_CableCheckRes]Undefined Isolation Status(%d)",
  7549. sys->IsolationStatus);
  7550. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7551. }
  7552. //Check for CSU command of "Stop by EVSE"
  7553. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7554. {
  7555. cab->ResponseCode = FAILED_DIN70121;
  7556. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7557. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7558. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7559. errn = -1;
  7560. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_CableCheckRes]EVSE_Shutdown");
  7561. }
  7562. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7563. {
  7564. cab->ResponseCode = FAILED_DIN70121;
  7565. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7566. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7567. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7568. errn = -1;
  7569. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_CableCheckRes]EVSE_EmergencyShutdown");
  7570. }
  7571. else if (ShmInternalComm->ChargingPermission == FALSE)
  7572. {
  7573. cab->ResponseCode = FAILED_DIN70121;
  7574. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7575. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7576. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  7577. errn = -1;
  7578. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_CableCheckRes]ChargingPermission = FALSE");
  7579. }
  7580. else
  7581. {
  7582. //null
  7583. }
  7584. //Response to CP Error
  7585. #if CP_PROTECTION_MECHANISM == ENABLE
  7586. //#if 1
  7587. if (sys->CableCheckPreCountDownDone == TRUE) //[To-Do] Here should be modified to 2 seconds
  7588. {
  7589. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  7590. {
  7591. cab->ResponseCode = FAILED_DIN70121;
  7592. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7593. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7594. Update_V2G_Flow_Status(Other_Fault);
  7595. errn = -1;
  7596. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7597. //CCS_SECC_CP_State_Error (023889)
  7598. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7599. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7600. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7601. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  7602. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  7603. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  7604. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  7605. sprintf((char*)buf_log_evcomm,
  7606. "[CableCheck]Emergency Stop by CP Error (%d, %.02f V, %d)\n",
  7607. sys->CpState,
  7608. sys->CpVoltage,
  7609. cab->cnt
  7610. );
  7611. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7612. //cab->cnt = 0;
  7613. }
  7614. }
  7615. #endif
  7616. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7617. #if PARAMETER_NORMAL_MODE == ENABLE
  7618. SHM_Read_din_CableCheckRes(&ccs_exi_doc_DIN, ShmCcsData);
  7619. #else
  7620. Sudo_Parameter_din_CableCheckRes();
  7621. #endif
  7622. //STEP 4: ============ Encode and Send Response Message ===========
  7623. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  7624. {
  7625. errn = -1;
  7626. }
  7627. //STEP 5: ============ Update Flags ===========
  7628. sys->ConnectorLocked = TRUE; //Inicating EVSE that the CCS Connector is Locked.
  7629. return errn;
  7630. }
  7631. /*===========================================================================
  7632. FUNCTION: Proc_iso1_CableCheckRes
  7633. DESCRIPTION:
  7634. PRE-CONDITION:
  7635. INPUT:
  7636. OUTPUT:
  7637. GLOBAL VARIABLES:
  7638. =============================================================================*/
  7639. int Proc_iso1_CableCheckRes(int AcceptFd)
  7640. {
  7641. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  7642. //if it is not the same, the packet should be ignored.
  7643. //STEP 1: ============ Initialize ============
  7644. //int i = 0;
  7645. int errn = 0;
  7646. bitstream_t v2g_tx_stream;
  7647. static struct CableCheckResponse_ISO15118_2014 *cab;
  7648. static struct ChargingInfoData *sys;
  7649. size_t pos = 0;
  7650. v2g_tx_stream.pos = &pos;
  7651. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7652. v2g_tx_stream.data = V2GTP_Tx_buf;
  7653. cab = &ShmCcsData->V2GMessage_ISO15118_2014.CableCheckResponse;
  7654. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7655. cab->ResponseCode = OK_ISO15118_2014;
  7656. cab->cnt++;
  7657. DEBUG_PRINTF_EVCOMM_DETAIL("CP_State = %d (%.02f V), V_now = %.02f, Isolation Status = %d, EVSEProcessing = %d\n",
  7658. sys->CpState,
  7659. sys->CpVoltage,
  7660. sys->PresentChargingVoltage,
  7661. sys->IsolationStatus,
  7662. cab->EVSEProcessing);
  7663. //[HEADER] Check Req SessionID
  7664. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  7665. {
  7666. cab->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  7667. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  7668. errn = -1;
  7669. }
  7670. //Check for SequenceError
  7671. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7672. {
  7673. cab->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  7674. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  7675. errn = -1;
  7676. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7677. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7678. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7679. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7680. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7681. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7682. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7683. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7684. }
  7685. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  7686. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  7687. {
  7688. //For PSU
  7689. sys->EvBatterytargetVoltage = CABLECHECK_TARGET_VOLTAGE; //500V //[To-Do] this should be modified to auto decision.
  7690. sys->EvBatterytargetCurrent = CABLECHECK_TARGET_CURRENT; //2A(default), unit: 1A
  7691. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  7692. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  7693. cab->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  7694. }
  7695. else if ((sys->IsolationStatus == GFD_Valid) || (sys->IsolationStatus == GFD_Warning)) //1: valid, 2:waring
  7696. {
  7697. //For PSU
  7698. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7699. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7700. if (sys->PresentChargingVoltage < 60) // < 60V
  7701. {
  7702. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  7703. sprintf((char*)buf_log_evcomm, "[V2G][CableCheck]Pass (V_now = %.02f, Isolated = %d)",
  7704. sys->PresentChargingVoltage,
  7705. sys->IsolationStatus);
  7706. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7707. //IsolationStatus
  7708. if (sys->IsolationStatus == GFD_Valid)
  7709. {
  7710. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  7711. }
  7712. else if (sys->IsolationStatus == GFD_Warning)
  7713. {
  7714. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Warning;
  7715. }
  7716. else
  7717. {
  7718. sprintf((char*)buf_log_evcomm,
  7719. "[WARNING]unexpected IsolationStatus(%d)",
  7720. sys->IsolationStatus);
  7721. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7722. }
  7723. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  7724. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7725. }
  7726. else
  7727. {
  7728. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  7729. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  7730. cab->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  7731. }
  7732. }
  7733. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  7734. {
  7735. //For PSU
  7736. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7737. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7738. cab->ResponseCode = FAILED_ISO15118_2014;
  7739. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7740. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_NotReady;
  7741. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7742. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CableCheckRes]IsolationStatus = 3 (fault)");
  7743. errn = -1;
  7744. }
  7745. //else if (sys->IsolationStatus == GFD_No_IMD){} //only for ISO15118
  7746. else
  7747. {
  7748. //For PSU
  7749. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  7750. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  7751. cab->ResponseCode = FAILED_ISO15118_2014;
  7752. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7753. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_NotReady;
  7754. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7755. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CableCheckRes]Undefined Isolation Status.");
  7756. }
  7757. //Check for CSU command of "Stop by EVSE"
  7758. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7759. {
  7760. cab->ResponseCode = FAILED_ISO15118_2014;
  7761. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7762. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7763. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7764. errn = -1;
  7765. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CableCheckRes]EVSE_Shutdown");
  7766. }
  7767. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7768. {
  7769. cab->ResponseCode = FAILED_ISO15118_2014;
  7770. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7771. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7772. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7773. errn = -1;
  7774. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CableCheckRes]EVSE_EmergencyShutdown");
  7775. }
  7776. else if (ShmInternalComm->ChargingPermission == FALSE)
  7777. {
  7778. cab->ResponseCode = FAILED_ISO15118_2014;
  7779. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7780. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7781. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  7782. errn = -1;
  7783. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_CableCheckRes]ChargingPermission = FALSE");
  7784. }
  7785. else
  7786. {
  7787. //null
  7788. }
  7789. //Response to CP Error
  7790. #if CP_PROTECTION_MECHANISM == ENABLE
  7791. if (sys->CableCheckPreCountDownDone == TRUE) //[To-Do] Here should be modified to 2 seconds
  7792. {
  7793. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  7794. {
  7795. cab->ResponseCode = FAILED_ISO15118_2014;
  7796. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7797. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7798. Update_V2G_Flow_Status(Other_Fault);
  7799. errn = -1;
  7800. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7801. //CCS_SECC_CP_State_Error (023889)
  7802. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7803. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7804. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7805. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  7806. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  7807. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  7808. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  7809. sprintf((char*)buf_log_evcomm,
  7810. "[CableCheck]Emergency Stop by CP Error (%d, %.02f V, %d)\n",
  7811. sys->CpState,
  7812. sys->CpVoltage,
  7813. cab->cnt
  7814. );
  7815. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7816. //cab->cnt = 0;
  7817. }
  7818. }
  7819. #endif
  7820. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7821. #if PARAMETER_NORMAL_MODE == ENABLE
  7822. SHM_Read_iso1_CableCheckRes(&ccs_exi_doc_ISO1, ShmCcsData);
  7823. #else
  7824. Sudo_Parameter_iso1_CableCheckRes();
  7825. #endif
  7826. //STEP 4: ============ Encode and Send Response Message ===========
  7827. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  7828. {
  7829. errn = -1;
  7830. }
  7831. //STEP 5: ============ Update Flags ===========
  7832. sys->ConnectorLocked = TRUE; //Inicating EVSE that the CCS Connector is Locked.
  7833. return errn;
  7834. }
  7835. /*===========================================================================
  7836. FUNCTION: Proc_din_CableCheckReq
  7837. DESCRIPTION:
  7838. PRE-CONDITION:
  7839. INPUT:
  7840. OUTPUT:
  7841. GLOBAL VARIABLES:
  7842. =============================================================================*/
  7843. int Proc_din_CableCheckReq(int AcceptFd)
  7844. {
  7845. int errn = 0;
  7846. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_CableCheckReq] Got CableCheckReq\n");
  7847. //Print the decoded XML Document
  7848. PRINT_XML_DOC_DIN_CableCheckReq(&ccs_exi_doc_DIN);
  7849. //Save into Share Memory
  7850. SHM_Save_din_CableCheckReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  7851. //Check for EV Error Code
  7852. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.CableCheckRequest.DC_EVStatus.EVErrorCode);
  7853. errn = Proc_din_CableCheckRes(AcceptFd);
  7854. if (errn == 0)
  7855. {
  7856. //send response successfully.
  7857. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]CableCheckRes");
  7858. }
  7859. else
  7860. {
  7861. sprintf((char*)buf_log_evcomm,
  7862. "[Error][SeccComm][Proc_din_CableCheckReq]Proc_iso1_CableCheckRes(): %d (DEC)",
  7863. errn);
  7864. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7865. }
  7866. return errn;
  7867. }
  7868. /*===========================================================================
  7869. FUNCTION: Proc_iso1_CableCheckReq
  7870. DESCRIPTION:
  7871. PRE-CONDITION:
  7872. INPUT:
  7873. OUTPUT:
  7874. GLOBAL VARIABLES:
  7875. =============================================================================*/
  7876. int Proc_iso1_CableCheckReq(int AcceptFd)
  7877. {
  7878. int errn = 0;
  7879. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CableCheckReq] Got CableCheckReq");
  7880. //Print the decoded XML Document
  7881. PRINT_XML_DOC_ISO1_CableCheckReq(&ccs_exi_doc_ISO1);
  7882. //Save into Share Memory
  7883. SHM_Save_iso1_CableCheckReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  7884. //Check for EV Error Code
  7885. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.CableCheckRequest.DC_EVStatus.EVErrorCode);
  7886. errn = Proc_iso1_CableCheckRes(AcceptFd);
  7887. if (errn == 0)
  7888. {
  7889. //send response successfully.
  7890. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]CableCheckRes");
  7891. }
  7892. else
  7893. {
  7894. sprintf((char*)buf_log_evcomm,
  7895. "[Error][SeccComm][Proc_iso1_CableCheckReq]Proc_iso1_CableCheckRes(): %d (DEC)",
  7896. errn);
  7897. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  7898. }
  7899. return errn;
  7900. }
  7901. /*===========================================================================
  7902. FUNCTION: SHM_Init_din_PreChargeRes
  7903. DESCRIPTION:
  7904. PRE-CONDITION:
  7905. INPUT:
  7906. 1. shm_ccs
  7907. OUTPUT:
  7908. 1. shm_ccs
  7909. GLOBAL VARIABLES:
  7910. =============================================================================*/
  7911. void SHM_Init_din_PreChargeRes(struct CcsData *shm_ccs)
  7912. {
  7913. struct PreChargeResponse_DIN70121 *in;
  7914. in = &shm_ccs->V2GMessage_DIN70121.PreChargeResponse;
  7915. //----- [BODY (1/3)] ResponseCode -----
  7916. in->ResponseCode = dinresponseCodeType_OK;
  7917. //----- [BODY (2/3)] EVSEPresentVoltage -----
  7918. short value = 0; int multiplier = 0; unsigned char unit = 0;
  7919. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for Test
  7920. value = 0; multiplier = 0; unit = V_DIN70121; //waiting for CsuComm to update V to fit EV Target
  7921. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  7922. //----- [BODY (3/3)] DC_EVSEStatus -----
  7923. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  7924. }
  7925. /*===========================================================================
  7926. FUNCTION: Sudo_Parameter_din_PreChargeRes
  7927. DESCRIPTION:
  7928. PRE-CONDITION:
  7929. INPUT:
  7930. OUTPUT:
  7931. GLOBAL VARIABLES:
  7932. =============================================================================*/
  7933. void Sudo_Parameter_din_PreChargeRes()
  7934. {
  7935. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  7936. init_dinPreChargeResType(&ccs_exi_doc_DIN.V2G_Message.Body.PreChargeRes);
  7937. ccs_exi_doc_DIN.V2G_Message.Body.PreChargeRes_isUsed = 1u;
  7938. //----- [BODY (1/3)] ResponseCode -----
  7939. struct dinPreChargeResType *res;
  7940. res = &ccs_exi_doc_DIN.V2G_Message.Body.PreChargeRes;
  7941. res->ResponseCode = dinresponseCodeType_OK;
  7942. //----- [BODY (2/3)] EVSEPresentVoltage -----
  7943. res->EVSEPresentVoltage.Value = 3820;
  7944. res->EVSEPresentVoltage.Multiplier = -1;
  7945. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  7946. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  7947. // dinunitSymbolType_h = 0,
  7948. // dinunitSymbolType_m = 1,
  7949. // dinunitSymbolType_s = 2,
  7950. // dinunitSymbolType_A = 3,
  7951. // dinunitSymbolType_Ah = 4,
  7952. // dinunitSymbolType_V = 5,
  7953. // dinunitSymbolType_VA = 6,
  7954. // dinunitSymbolType_W = 7,
  7955. // dinunitSymbolType_W_s = 8,
  7956. // dinunitSymbolType_Wh = 9
  7957. //----- [BODY (3/3)] DC_EVSEStatus -----
  7958. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7959. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  7960. // dinisolationLevelType_Invalid = 0,
  7961. // dinisolationLevelType_Valid = 1,
  7962. // dinisolationLevelType_Warning = 2,
  7963. // dinisolationLevelType_Fault = 3
  7964. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7965. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7966. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  7967. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7968. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7969. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7970. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7971. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7972. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  7973. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  7974. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  7975. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  7976. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  7977. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7978. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  7979. // dinEVSENotificationType_None = 0,
  7980. // dinEVSENotificationType_StopCharging = 1,
  7981. // dinEVSENotificationType_ReNegotiation = 2
  7982. }
  7983. /*===========================================================================
  7984. FUNCTION: Sudo_Parameter_iso1_PreChargeRes
  7985. DESCRIPTION:
  7986. PRE-CONDITION:
  7987. INPUT:
  7988. OUTPUT:
  7989. GLOBAL VARIABLES:
  7990. =============================================================================*/
  7991. void Sudo_Parameter_iso1_PreChargeRes()
  7992. {
  7993. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  7994. init_iso1PreChargeResType(&ccs_exi_doc_ISO1.V2G_Message.Body.PreChargeRes);
  7995. ccs_exi_doc_ISO1.V2G_Message.Body.PreChargeRes_isUsed = 1u;
  7996. //----- [BODY (1/3)] ResponseCode -----
  7997. struct iso1PreChargeResType *res;
  7998. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PreChargeRes;
  7999. res->ResponseCode = iso1responseCodeType_OK;
  8000. //----- [BODY (2/3)] EVSEPresentVoltage -----
  8001. res->EVSEPresentVoltage.Value = 3820;
  8002. res->EVSEPresentVoltage.Multiplier = -1;
  8003. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  8004. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  8005. //iso1unitSymbolType_h = 0,
  8006. //iso1unitSymbolType_m = 1,
  8007. //iso1unitSymbolType_s = 2,
  8008. //iso1unitSymbolType_A = 3,
  8009. //iso1unitSymbolType_V = 4,
  8010. //iso1unitSymbolType_W = 5,
  8011. //iso1unitSymbolType_Wh = 6
  8012. //----- [BODY (3/3)] DC_EVSEStatus -----
  8013. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8014. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  8015. //iso1isolationLevelType_Invalid = 0,
  8016. //iso1isolationLevelType_Valid = 1,
  8017. //iso1isolationLevelType_Warning = 2,
  8018. //iso1isolationLevelType_Fault = 3,
  8019. //iso1isolationLevelType_No_IMD = 4
  8020. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8021. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8022. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  8023. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8024. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8025. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8026. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8027. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8028. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  8029. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  8030. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  8031. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  8032. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  8033. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8034. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8035. //iso1EVSENotificationType_None = 0,
  8036. //iso1EVSENotificationType_StopCharging = 1,
  8037. //iso1EVSENotificationType_ReNegotiation = 2
  8038. }
  8039. /*===========================================================================
  8040. FUNCTION: Proc_din_PreChargeRes
  8041. DESCRIPTION:
  8042. PRE-CONDITION:
  8043. INPUT:
  8044. OUTPUT:
  8045. GLOBAL VARIABLES:
  8046. =============================================================================*/
  8047. int Proc_din_PreChargeRes(int AcceptFd)
  8048. {
  8049. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8050. //if it is not the same, the packet should be ignored.
  8051. //STEP 1: ============ Initialize ============
  8052. //int i = 0;
  8053. int errn = 0;
  8054. bitstream_t v2g_tx_stream;
  8055. static struct PreChargeResponse_DIN70121 *pre;
  8056. static struct ChargingInfoData *sys;
  8057. size_t pos = 0;
  8058. v2g_tx_stream.pos = &pos;
  8059. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8060. v2g_tx_stream.data = V2GTP_Tx_buf;
  8061. pre = &ShmCcsData->V2GMessage_DIN70121.PreChargeResponse;
  8062. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8063. pre->ResponseCode = OK_DIN70121;
  8064. //EVSE Status Code
  8065. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8066. //[HEADER] Check Req SessionID
  8067. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  8068. {
  8069. pre->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  8070. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  8071. errn = -1;
  8072. }
  8073. //Check for SequenceError
  8074. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8075. {
  8076. pre->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  8077. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  8078. errn = -1;
  8079. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8080. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8081. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8082. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8083. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8084. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8085. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8086. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8087. }
  8088. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8089. SAVE_PhysicalValueType_DIN70121(&pre->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  8090. //Isolation Status
  8091. if (sys->IsolationStatus == GFD_Invalid) //0: invalid
  8092. {
  8093. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  8094. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8095. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8096. Update_V2G_Flow_Status(Other_Fault);
  8097. errn = -1;
  8098. sprintf((char*)buf_log_evcomm, "[Error][Proc_din_PreChargeRes]IsolationStatus = %d", sys->IsolationStatus);
  8099. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8100. }
  8101. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  8102. {
  8103. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  8104. }
  8105. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  8106. {
  8107. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  8108. }
  8109. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  8110. {
  8111. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]GFD_Fault => Emergency Shutdown");
  8112. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  8113. pre->ResponseCode = FAILED_DIN70121;
  8114. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8115. errn = -1;
  8116. }
  8117. else //GFD_No_IMD or other unexpected status
  8118. {
  8119. pre->ResponseCode = FAILED_DIN70121;
  8120. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  8121. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8122. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8123. Update_V2G_Flow_Status(Other_Fault);
  8124. errn = -1;
  8125. sprintf((char*)buf_log_evcomm,
  8126. "[Error][Proc_din_PreChargeRes]IsolationStatus = %d (undefined)",
  8127. sys->IsolationStatus);
  8128. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8129. }
  8130. //Check for CSU command of "Stop by EVSE"
  8131. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8132. {
  8133. //Check for Alarm Code: CCS GFD trip (012235)
  8134. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8135. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8136. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8137. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8138. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8139. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8140. {
  8141. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]CCS GFD trip => EVSE_Shutdown");
  8142. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  8143. }
  8144. else
  8145. {
  8146. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]EVSE_Shutdown");
  8147. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8148. }
  8149. pre->ResponseCode = FAILED_DIN70121;
  8150. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8151. errn = -1;
  8152. }
  8153. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8154. {
  8155. //Check for Alarm Code: CCS GFD trip (012235)
  8156. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8157. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8158. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8159. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8160. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8161. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8162. {
  8163. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]CCS GFD trip => EVSE_EmergencyShutdown");
  8164. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  8165. }
  8166. else
  8167. {
  8168. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]EVSE_EmergencyShutdown");
  8169. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8170. }
  8171. pre->ResponseCode = FAILED_DIN70121;
  8172. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8173. errn = -1;
  8174. }
  8175. else if (ShmInternalComm->ChargingPermission == FALSE)
  8176. {
  8177. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_PreChargeRes]ChargingPermission = FALSE");
  8178. pre->ResponseCode = FAILED_DIN70121;
  8179. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8180. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8181. errn = -1;
  8182. }
  8183. else
  8184. {
  8185. //null
  8186. }
  8187. //Response to CP Error
  8188. #if CP_PROTECTION_MECHANISM == ENABLE
  8189. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  8190. {
  8191. pre->ResponseCode = FAILED_DIN70121;
  8192. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8193. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8194. Update_V2G_Flow_Status(Other_Fault);
  8195. errn = -1;
  8196. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8197. //CCS_SECC_CP_State_Error (023889)
  8198. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8199. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8200. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8201. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  8202. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  8203. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  8204. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  8205. sprintf((char*)buf_log_evcomm,
  8206. "[Error][Proc_din_PreChargeRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  8207. sys->CpState,
  8208. sys->CpVoltage
  8209. );
  8210. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8211. }
  8212. #endif
  8213. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8214. #if PARAMETER_NORMAL_MODE == ENABLE
  8215. SHM_Read_din_PreChargeRes(&ccs_exi_doc_DIN, ShmCcsData);
  8216. #else
  8217. Sudo_Parameter_din_PreChargeRes();
  8218. #endif
  8219. //STEP 4: ============ Encode and Send Response Message ===========
  8220. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  8221. {
  8222. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][send_encoded_din_V2GTP_Stream]:Tx encoded msg error");
  8223. errn = -1;
  8224. }
  8225. //STEP 5: ============ Update Flags ===========
  8226. return errn;
  8227. }
  8228. /*===========================================================================
  8229. FUNCTION: Proc_iso1_PreChargeRes
  8230. DESCRIPTION:
  8231. PRE-CONDITION:
  8232. INPUT:
  8233. OUTPUT:
  8234. GLOBAL VARIABLES:
  8235. =============================================================================*/
  8236. int Proc_iso1_PreChargeRes(int AcceptFd)
  8237. {
  8238. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8239. //if it is not the same, the packet should be ignored.
  8240. //STEP 1: ============ Initialize ============
  8241. //int i = 0;
  8242. int errn = 0;
  8243. bitstream_t v2g_tx_stream;
  8244. static struct PreChargeResponse_ISO15118_2014 *pre;
  8245. static struct ChargingInfoData *sys;
  8246. size_t pos = 0;
  8247. v2g_tx_stream.pos = &pos;
  8248. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8249. v2g_tx_stream.data = V2GTP_Tx_buf;
  8250. pre = &ShmCcsData->V2GMessage_ISO15118_2014.PreChargeResponse;
  8251. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8252. pre->ResponseCode = OK_ISO15118_2014;
  8253. //EVSE Status Code
  8254. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8255. //[HEADER] Check Req SessionID
  8256. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  8257. {
  8258. pre->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  8259. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  8260. errn = -1;
  8261. }
  8262. //Check for SequenceError
  8263. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8264. {
  8265. pre->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  8266. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  8267. errn = -1;
  8268. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8269. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8270. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8271. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8272. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8273. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8274. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8275. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8276. }
  8277. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8278. SAVE_PhysicalValueType_ISO15118_2014(&pre->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  8279. //Isolation Status
  8280. if (sys->IsolationStatus == GFD_Invalid) //0: invalid(on going)
  8281. {
  8282. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  8283. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8284. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8285. Update_V2G_Flow_Status(Other_Fault);
  8286. errn = -1;
  8287. sprintf((char*)buf_log_evcomm, "[Error][Proc_iso1_PreChargeRes]IsolationStatus = %d", sys->IsolationStatus);
  8288. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8289. }
  8290. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  8291. {
  8292. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  8293. }
  8294. else if (sys->IsolationStatus == GFD_Warning) //2: waring
  8295. {
  8296. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Warning; //1
  8297. }
  8298. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  8299. {
  8300. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]GFD_Fault => Emergency Shutdown");
  8301. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  8302. pre->ResponseCode = FAILED_ISO15118_2014;
  8303. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8304. errn = -1;
  8305. }
  8306. else
  8307. {
  8308. pre->ResponseCode = FAILED_ISO15118_2014;
  8309. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  8310. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8311. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8312. Update_V2G_Flow_Status(Other_Fault);
  8313. errn = -1;
  8314. sprintf((char*)buf_log_evcomm,
  8315. "[Error][Proc_iso1_PreChargeRes]IsolationStatus = %d (undefined)",
  8316. sys->IsolationStatus);
  8317. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8318. }
  8319. //Check for CSU command of "Stop by EVSE"
  8320. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8321. {
  8322. //Check for Alarm Code: CCS GFD trip (012235)
  8323. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8324. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8325. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8326. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8327. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8328. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8329. {
  8330. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]CCS GFD trip => EVSE_Shutdown");
  8331. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  8332. }
  8333. else
  8334. {
  8335. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]EVSE_Shutdown");
  8336. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8337. }
  8338. pre->ResponseCode = FAILED_ISO15118_2014;
  8339. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8340. errn = -1;
  8341. }
  8342. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8343. {
  8344. //Check for Alarm Code: CCS GFD trip (012235)
  8345. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8346. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8347. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8348. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8349. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8350. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8351. {
  8352. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]CCS GFD trip => EVSE_EmergencyShutdown");
  8353. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  8354. }
  8355. else
  8356. {
  8357. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]EVSE_EmergencyShutdown");
  8358. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8359. }
  8360. pre->ResponseCode = FAILED_ISO15118_2014;
  8361. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8362. errn = -1;
  8363. }
  8364. else if (ShmInternalComm->ChargingPermission == FALSE)
  8365. {
  8366. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_PreChargeRes]ChargingPermission = FALSE");
  8367. pre->ResponseCode = FAILED_ISO15118_2014;
  8368. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8369. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8370. errn = -1;
  8371. }
  8372. else
  8373. {
  8374. //null
  8375. }
  8376. //Response to CP Error
  8377. #if CP_PROTECTION_MECHANISM == ENABLE
  8378. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  8379. {
  8380. pre->ResponseCode = FAILED_ISO15118_2014;
  8381. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8382. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8383. Update_V2G_Flow_Status(Other_Fault);
  8384. errn = -1;
  8385. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8386. //CCS_SECC_CP_State_Error (023889)
  8387. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8388. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8389. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8390. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  8391. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  8392. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  8393. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  8394. sprintf((char*)buf_log_evcomm,
  8395. "[Error][Proc_iso1_PreChargeRes]Emergency Stop by CP Error (%d, %.02f V)\n",
  8396. sys->CpState,
  8397. sys->CpVoltage
  8398. );
  8399. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8400. }
  8401. #endif
  8402. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8403. #if PARAMETER_NORMAL_MODE == ENABLE
  8404. SHM_Read_iso1_PreChargeRes(&ccs_exi_doc_ISO1, ShmCcsData);
  8405. #else
  8406. Sudo_Parameter_iso1_PreChargeRes();
  8407. #endif
  8408. //STEP 4: ============ Encode and Send Response Message ===========
  8409. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  8410. {
  8411. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][send_encoded_iso1_V2GTP_Stream]:Tx encoded msg error");
  8412. errn = -1;
  8413. }
  8414. //STEP 5: ============ Update Flags ===========
  8415. return errn;
  8416. }
  8417. /*===========================================================================
  8418. FUNCTION: Proc_din_PreChargeReq
  8419. DESCRIPTION:
  8420. PRE-CONDITION:
  8421. INPUT:
  8422. OUTPUT:
  8423. GLOBAL VARIABLES:
  8424. =============================================================================*/
  8425. int Proc_din_PreChargeReq(int AcceptFd)
  8426. {
  8427. int errn = 0;
  8428. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_PreChargeReq] Got PreChargeReq");
  8429. //Print the decoded XML Document
  8430. PRINT_XML_DOC_DIN_PreChargeReq(&ccs_exi_doc_DIN);
  8431. //Save into Share Memory
  8432. SHM_Save_din_PreChargeReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  8433. //Check for EV Error Code
  8434. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PreChargeRequest.DC_EVStatus.EVErrorCode);
  8435. errn = Proc_din_PreChargeRes(AcceptFd);
  8436. if (errn == 0)
  8437. {
  8438. //send response successfully.
  8439. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]PreChargeRes");
  8440. }
  8441. else
  8442. {
  8443. sprintf((char*)buf_log_evcomm,
  8444. "[Error][SeccComm][Proc_din_PreChargeReq]Proc_iso1_PreChargeRes(): %d (DEC)",
  8445. errn);
  8446. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8447. }
  8448. return errn;
  8449. }
  8450. /*===========================================================================
  8451. FUNCTION: Proc_iso1_PreChargeReq
  8452. DESCRIPTION:
  8453. PRE-CONDITION:
  8454. INPUT:
  8455. OUTPUT:
  8456. GLOBAL VARIABLES:
  8457. =============================================================================*/
  8458. int Proc_iso1_PreChargeReq(int AcceptFd)
  8459. {
  8460. int errn = 0;
  8461. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_PreChargeReq] Got PreChargeReq");
  8462. //Print the decoded XML Document
  8463. PRINT_XML_DOC_ISO1_PreChargeReq(&ccs_exi_doc_ISO1);
  8464. //Save into Share Memory
  8465. SHM_Save_iso1_PreChargeReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  8466. //Check for EV Error Code
  8467. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PreChargeRequest.DC_EVStatus.EVErrorCode);
  8468. errn = Proc_iso1_PreChargeRes(AcceptFd);
  8469. if (errn == 0)
  8470. {
  8471. //send response successfully.
  8472. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]PreChargeRes");
  8473. }
  8474. else
  8475. {
  8476. sprintf((char*)buf_log_evcomm,
  8477. "[Error][SeccComm][Proc_iso1_PreChargeReq]Proc_iso1_PreChargeRes(): %d (DEC)",
  8478. errn);
  8479. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8480. }
  8481. return errn;
  8482. }
  8483. /*===========================================================================
  8484. FUNCTION: SHM_Init_din_PowerDeliveryRes
  8485. DESCRIPTION:
  8486. PRE-CONDITION:
  8487. INPUT:
  8488. 1. shm_ccs
  8489. OUTPUT:
  8490. 1. shm_ccs
  8491. GLOBAL VARIABLES:
  8492. =============================================================================*/
  8493. void SHM_Init_din_PowerDeliveryRes(struct CcsData *shm_ccs)
  8494. {
  8495. struct PowerDeliveryResponse_DIN70121 *in;
  8496. in = &shm_ccs->V2GMessage_DIN70121.PowerDeliveryResponse;
  8497. //----- [BODY (1/3)] ResponseCode -----
  8498. in->ResponseCode = dinresponseCodeType_OK;
  8499. //----- [BODY (2/3)] AC_EVSEStatus -----
  8500. //ignore, since DIN 70121 doesn't support AC, yet.
  8501. //----- [BODY (2/3)] DC_EVSEStatus -----
  8502. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  8503. }
  8504. /*===========================================================================
  8505. FUNCTION: Sudo_Parameter_din_PowerDeliveryRes
  8506. DESCRIPTION:
  8507. PRE-CONDITION:
  8508. INPUT:
  8509. OUTPUT:
  8510. GLOBAL VARIABLES:
  8511. =============================================================================*/
  8512. void Sudo_Parameter_din_PowerDeliveryRes()
  8513. {
  8514. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  8515. init_dinPowerDeliveryResType(&ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes);
  8516. ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes_isUsed = 1u;
  8517. //----- [BODY (1/3)] ResponseCode -----
  8518. struct dinPowerDeliveryResType *res;
  8519. res = &ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes;
  8520. res->ResponseCode = dinresponseCodeType_OK;
  8521. //----- [BODY (2/3)] AC_EVSEStatus -----
  8522. //ignore, since DIN 70121 doesn't support AC, yet.
  8523. //----- [BODY (2/3)] DC_EVSEStatus -----
  8524. res->DC_EVSEStatus_isUsed = 1u;
  8525. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8526. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  8527. // dinisolationLevelType_Invalid = 0,
  8528. // dinisolationLevelType_Valid = 1,
  8529. // dinisolationLevelType_Warning = 2,
  8530. // dinisolationLevelType_Fault = 3
  8531. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8532. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8533. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  8534. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8535. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8536. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8537. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8538. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8539. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  8540. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  8541. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  8542. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  8543. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  8544. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8545. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  8546. // dinEVSENotificationType_None = 0,
  8547. // dinEVSENotificationType_StopCharging = 1,
  8548. // dinEVSENotificationType_ReNegotiation = 2
  8549. }
  8550. /*===========================================================================
  8551. FUNCTION: Sudo_Parameter_iso1_PowerDeliveryRes
  8552. DESCRIPTION:
  8553. PRE-CONDITION:
  8554. INPUT:
  8555. OUTPUT:
  8556. GLOBAL VARIABLES:
  8557. =============================================================================*/
  8558. void Sudo_Parameter_iso1_PowerDeliveryRes()
  8559. {
  8560. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  8561. init_iso1PowerDeliveryResType(&ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes);
  8562. ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes_isUsed = 1u;
  8563. //----- [BODY (1/3)] ResponseCode -----
  8564. struct iso1PowerDeliveryResType *res;
  8565. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes;
  8566. res->ResponseCode = iso1responseCodeType_OK;
  8567. //----- [BODY (2/3)] AC_EVSEStatus -----
  8568. //ignore, since our ISO1 70121 doesn't support AC, yet.
  8569. //----- [BODY (2/3)] DC_EVSEStatus -----
  8570. res->DC_EVSEStatus_isUsed = 1u;
  8571. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8572. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  8573. //iso1isolationLevelType_Invalid = 0,
  8574. //iso1isolationLevelType_Valid = 1,
  8575. //iso1isolationLevelType_Warning = 2,
  8576. //iso1isolationLevelType_Fault = 3,
  8577. //iso1isolationLevelType_No_IMD = 4
  8578. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8579. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8580. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  8581. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8582. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8583. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8584. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8585. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8586. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  8587. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  8588. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  8589. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  8590. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  8591. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8592. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8593. //iso1EVSENotificationType_None = 0,
  8594. //iso1EVSENotificationType_StopCharging = 1,
  8595. //iso1EVSENotificationType_ReNegotiation = 2
  8596. }
  8597. /*===========================================================================
  8598. FUNCTION: Proc_din_PowerDeliveryStartRes
  8599. DESCRIPTION:
  8600. PRE-CONDITION:
  8601. INPUT:
  8602. OUTPUT:
  8603. GLOBAL VARIABLES:
  8604. =============================================================================*/
  8605. int Proc_din_PowerDeliveryStartRes(int AcceptFd)
  8606. {
  8607. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8608. //if it is not the same, the packet should be ignored.
  8609. //STEP 1: ============ Initialize ============
  8610. //int i = 0;
  8611. int errn = 0;
  8612. bitstream_t v2g_tx_stream;
  8613. struct dinPowerDeliveryResType *res;
  8614. struct ChargingInfoData *sys;
  8615. size_t pos = 0;
  8616. v2g_tx_stream.pos = &pos;
  8617. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8618. v2g_tx_stream.data = V2GTP_Tx_buf;
  8619. res = &ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes;
  8620. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8621. res->ResponseCode = OK_DIN70121;
  8622. //[HEADER] Check Req SessionID
  8623. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  8624. {
  8625. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  8626. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  8627. errn = -1;
  8628. }
  8629. //Check for SequenceError
  8630. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8631. {
  8632. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  8633. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  8634. errn = -1;
  8635. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8636. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8637. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8638. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8639. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8640. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8641. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8642. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8643. }
  8644. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8645. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8646. #if PARAMETER_NORMAL_MODE == ENABLE
  8647. SHM_Read_din_PowerDeliveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  8648. #else
  8649. Sudo_Parameter_din_PowerDeliveryRes();
  8650. #endif
  8651. //EVSE Status Code
  8652. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  8653. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8654. //Check for CSU command of "Stop by EVSE"
  8655. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8656. {
  8657. //res->ResponseCode = FAILED_DIN70121;
  8658. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8659. }
  8660. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8661. {
  8662. //res->ResponseCode = FAILED_DIN70121;
  8663. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8664. }
  8665. else if (ShmInternalComm->ChargingPermission == FALSE)
  8666. {
  8667. //res->ResponseCode = FAILED_DIN70121;
  8668. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8669. }
  8670. //STEP 4: ============ Encode and Send Response Message ===========
  8671. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  8672. {
  8673. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_PowerDeliveryStartRes][Error]Tx encoded msg error");
  8674. errn = -1;
  8675. }
  8676. //STEP 5: ============ Update Flags ===========
  8677. return errn;
  8678. }
  8679. /*===========================================================================
  8680. FUNCTION: Proc_iso1_PowerDeliveryStartRes
  8681. DESCRIPTION:
  8682. PRE-CONDITION:
  8683. INPUT:
  8684. OUTPUT:
  8685. GLOBAL VARIABLES:
  8686. =============================================================================*/
  8687. int Proc_iso1_PowerDeliveryStartRes(int AcceptFd)
  8688. {
  8689. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  8690. //if it is not the same, the packet should be ignored.
  8691. //STEP 1: ============ Initialize ============
  8692. //int i = 0;
  8693. int errn = 0;
  8694. bitstream_t v2g_tx_stream;
  8695. struct iso1PowerDeliveryResType *res;
  8696. struct ChargingInfoData *sys;
  8697. size_t pos = 0;
  8698. v2g_tx_stream.pos = &pos;
  8699. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8700. v2g_tx_stream.data = V2GTP_Tx_buf;
  8701. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes;
  8702. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8703. res->ResponseCode = OK_ISO15118_2014;
  8704. //[HEADER] Check Req SessionID
  8705. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  8706. {
  8707. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  8708. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  8709. errn = -1;
  8710. }
  8711. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8712. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8713. #if PARAMETER_NORMAL_MODE == ENABLE
  8714. SHM_Read_iso1_PowerDeliveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  8715. #else
  8716. Sudo_Parameter_iso1_PowerDeliveryRes();
  8717. #endif
  8718. //EVSE Status Code
  8719. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  8720. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8721. //Check for CSU command of "Stop by EVSE"
  8722. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8723. {
  8724. //res->ResponseCode = FAILED_ISO15118_2014;
  8725. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8726. }
  8727. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8728. {
  8729. //res->ResponseCode = FAILED_ISO15118_2014;
  8730. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8731. }
  8732. else if (ShmInternalComm->ChargingPermission == FALSE)
  8733. {
  8734. //res->ResponseCode = FAILED_ISO15118_2014;
  8735. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8736. }
  8737. //STEP 4: ============ Encode and Send Response Message ===========
  8738. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  8739. {
  8740. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_PowerDeliveryStartRes][Error]Tx encoded msg error");
  8741. errn = -1;
  8742. }
  8743. //STEP 5: ============ Update Flags ===========
  8744. return errn;
  8745. }
  8746. /*===========================================================================
  8747. FUNCTION: Proc_din_PowerDeliveryStartReq
  8748. DESCRIPTION:
  8749. PRE-CONDITION:
  8750. INPUT:
  8751. OUTPUT:
  8752. GLOBAL VARIABLES:
  8753. =============================================================================*/
  8754. int Proc_din_PowerDeliveryStartReq(int AcceptFd)
  8755. {
  8756. int errn = 0;
  8757. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PowerDeliveryStartReq");
  8758. //Print the decoded XML Document
  8759. PRINT_XML_DOC_DIN_PowerDeliveryReq(&ccs_exi_doc_DIN);
  8760. //Save into Share Memory
  8761. SHM_Save_din_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  8762. //Check for EV Error Code
  8763. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  8764. errn = Proc_din_PowerDeliveryStartRes(AcceptFd);
  8765. if (errn == 0)
  8766. {
  8767. //send response successfully.
  8768. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]PowerDeliveryStartRes");
  8769. }
  8770. else
  8771. {
  8772. sprintf((char*)buf_log_evcomm,
  8773. "[Error][Proc_din_PowerDeliveryStartReq]Proc_iso1_PowerDeliveryStartRes(): %d (DEC)",
  8774. errn);
  8775. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8776. }
  8777. return errn;
  8778. }
  8779. /*===========================================================================
  8780. FUNCTION: Proc_iso1_PowerDeliveryStartReq
  8781. DESCRIPTION:
  8782. PRE-CONDITION:
  8783. INPUT:
  8784. OUTPUT:
  8785. GLOBAL VARIABLES:
  8786. =============================================================================*/
  8787. int Proc_iso1_PowerDeliveryStartReq(int AcceptFd)
  8788. {
  8789. int errn = 0;
  8790. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PowerDeliveryStartReq");
  8791. //Print the decoded XML Document
  8792. PRINT_XML_DOC_ISO1_PowerDeliveryReq(&ccs_exi_doc_ISO1);
  8793. //Save into Share Memory
  8794. SHM_Save_iso1_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  8795. //Check for EV Error Code
  8796. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  8797. errn = Proc_iso1_PowerDeliveryStartRes(AcceptFd);
  8798. if (errn == 0)
  8799. {
  8800. //send response successfully.
  8801. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]PowerDeliveryStartRes");
  8802. }
  8803. else
  8804. {
  8805. sprintf((char*)buf_log_evcomm,
  8806. "[Error][Proc_iso1_PowerDeliveryStartReq]Proc_iso1_PowerDeliveryStartRes(): %d (DEC)",
  8807. errn);
  8808. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  8809. }
  8810. return errn;
  8811. }
  8812. /*===========================================================================
  8813. FUNCTION: SHM_Init_din_CurrentDemandRes
  8814. DESCRIPTION:
  8815. PRE-CONDITION:
  8816. INPUT:
  8817. 1. shm_ccs
  8818. OUTPUT:
  8819. 1. shm_ccs
  8820. GLOBAL VARIABLES:
  8821. =============================================================================*/
  8822. void SHM_Init_din_CurrentDemandRes(struct CcsData *shm_ccs)
  8823. {
  8824. struct CurrentDemandResponse_DIN70121 *in;
  8825. in = &shm_ccs->V2GMessage_DIN70121.CurrentDemandResponse;
  8826. //----- [BODY (1/10)] ResponseCode -----
  8827. in->ResponseCode = dinresponseCodeType_OK;
  8828. //----- [BODY (2/10)] DC_EVSEStatus -----
  8829. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  8830. //----- [BODY (3/10)] EVSEPresentVoltage -----
  8831. short value = 0; int multiplier = 0; unsigned char unit = 0;
  8832. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for test only.
  8833. value = 0; multiplier = 0; unit = V_DIN70121;
  8834. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  8835. //----- [BODY (4/10)] EVSEPresentCurrent -----
  8836. //value = 600; multiplier = -1; unit = A_DIN70121; //60A, for test only.
  8837. value = 0; multiplier = 0; unit = A_DIN70121;
  8838. SHM_Save_dinPhysicalValueType(&in->EVSEPresentCurrent, value, multiplier, unit);
  8839. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  8840. in->EVSECurrentLimitAchieved = FALSE;
  8841. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  8842. in->EVSEVoltageLimitAchieved = FALSE;
  8843. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  8844. in->EVSEPowerLimitAchieved = FALSE;
  8845. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  8846. //value = 7500; multiplier = -1; unit = V_DIN70121; //750V
  8847. value = 5000; multiplier = -1; unit = V_DIN70121; //500V
  8848. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumVoltageLimit, value, multiplier, unit);
  8849. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  8850. value = 600; multiplier = -1; unit = A_DIN70121; //60A
  8851. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumCurrentLimit, value, multiplier, unit);
  8852. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  8853. value = 3000; multiplier = 1; unit = W_DIN70121; //30KW
  8854. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumPowerLimit, value, multiplier, unit);
  8855. }
  8856. /*===========================================================================
  8857. FUNCTION: Sudo_Parameter_din_CurrentDemandRes
  8858. DESCRIPTION:
  8859. PRE-CONDITION:
  8860. INPUT:
  8861. OUTPUT:
  8862. GLOBAL VARIABLES:
  8863. =============================================================================*/
  8864. void Sudo_Parameter_din_CurrentDemandRes()
  8865. {
  8866. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  8867. init_dinCurrentDemandResType(&ccs_exi_doc_DIN.V2G_Message.Body.CurrentDemandRes);
  8868. ccs_exi_doc_DIN.V2G_Message.Body.CurrentDemandRes_isUsed = 1u;
  8869. //----- [BODY (1/10)] ResponseCode -----
  8870. struct dinCurrentDemandResType *res;
  8871. res = &ccs_exi_doc_DIN.V2G_Message.Body.CurrentDemandRes;
  8872. res->ResponseCode = dinresponseCodeType_OK;
  8873. //----- [BODY (2/10)] DC_EVSEStatus -----
  8874. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8875. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  8876. // dinisolationLevelType_Invalid = 0,
  8877. // dinisolationLevelType_Valid = 1,
  8878. // dinisolationLevelType_Warning = 2,
  8879. // dinisolationLevelType_Fault = 3
  8880. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8881. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8882. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  8883. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8884. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8885. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8886. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8887. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8888. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  8889. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  8890. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  8891. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  8892. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  8893. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8894. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  8895. // dinEVSENotificationType_None = 0,
  8896. // dinEVSENotificationType_StopCharging = 1,
  8897. // dinEVSENotificationType_ReNegotiation = 2
  8898. //----- [BODY (3/10)] EVSEPresentVoltage -----
  8899. res->EVSEPresentVoltage.Value = 3820;
  8900. res->EVSEPresentVoltage.Multiplier = -1;
  8901. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  8902. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  8903. //----- [BODY (4/10)] EVSEPresentCurrent -----
  8904. res->EVSEPresentCurrent.Value = 1200;
  8905. res->EVSEPresentCurrent.Multiplier = -1;
  8906. res->EVSEPresentCurrent.Unit_isUsed = 1u;
  8907. res->EVSEPresentCurrent.Unit = dinunitSymbolType_A;
  8908. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  8909. res->EVSECurrentLimitAchieved = 0;
  8910. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  8911. res->EVSEVoltageLimitAchieved = 0;
  8912. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  8913. res->EVSEPowerLimitAchieved = 0;
  8914. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  8915. res->EVSEMaximumVoltageLimit_isUsed = 1u;
  8916. res->EVSEMaximumVoltageLimit.Value = 7500;
  8917. res->EVSEMaximumVoltageLimit.Multiplier = -1;
  8918. res->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  8919. res->EVSEMaximumVoltageLimit.Unit = dinunitSymbolType_V;
  8920. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  8921. res->EVSEMaximumCurrentLimit_isUsed = 1u;
  8922. res->EVSEMaximumCurrentLimit.Value = 1200;
  8923. res->EVSEMaximumCurrentLimit.Multiplier = -1;
  8924. res->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  8925. res->EVSEMaximumCurrentLimit.Unit = dinunitSymbolType_A;
  8926. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  8927. res->EVSEMaximumPowerLimit_isUsed = 1u;
  8928. res->EVSEMaximumPowerLimit.Value = 6000;
  8929. res->EVSEMaximumPowerLimit.Multiplier = 1;
  8930. res->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  8931. res->EVSEMaximumPowerLimit.Unit = dinunitSymbolType_W;
  8932. }
  8933. /*===========================================================================
  8934. FUNCTION: Sudo_Parameter_iso1_CurrentDemandRes
  8935. DESCRIPTION:
  8936. PRE-CONDITION:
  8937. INPUT:
  8938. OUTPUT:
  8939. GLOBAL VARIABLES:
  8940. =============================================================================*/
  8941. void Sudo_Parameter_iso1_CurrentDemandRes()
  8942. {
  8943. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  8944. init_iso1CurrentDemandResType(&ccs_exi_doc_ISO1.V2G_Message.Body.CurrentDemandRes);
  8945. ccs_exi_doc_ISO1.V2G_Message.Body.CurrentDemandRes_isUsed = 1u;
  8946. //----- [BODY (1/10)] ResponseCode -----
  8947. struct iso1CurrentDemandResType *res;
  8948. res = &ccs_exi_doc_ISO1.V2G_Message.Body.CurrentDemandRes;
  8949. res->ResponseCode = iso1responseCodeType_OK;
  8950. //----- [BODY (2/10)] DC_EVSEStatus -----
  8951. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8952. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  8953. //iso1isolationLevelType_Invalid = 0,
  8954. //iso1isolationLevelType_Valid = 1,
  8955. //iso1isolationLevelType_Warning = 2,
  8956. //iso1isolationLevelType_Fault = 3,
  8957. //iso1isolationLevelType_No_IMD = 4
  8958. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8959. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8960. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  8961. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8962. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8963. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8964. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8965. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8966. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  8967. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  8968. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  8969. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  8970. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  8971. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8972. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8973. //iso1EVSENotificationType_None = 0,
  8974. //iso1EVSENotificationType_StopCharging = 1,
  8975. //iso1EVSENotificationType_ReNegotiation = 2
  8976. //----- [BODY (3/10)] EVSEPresentVoltage -----
  8977. res->EVSEPresentVoltage.Value = 3820;
  8978. res->EVSEPresentVoltage.Multiplier = -1;
  8979. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  8980. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  8981. //----- [BODY (4/10)] EVSEPresentCurrent -----
  8982. res->EVSEPresentCurrent.Value = 1200;
  8983. res->EVSEPresentCurrent.Multiplier = -1;
  8984. //res->EVSEPresentCurrent.Unit_isUsed = 1u;
  8985. res->EVSEPresentCurrent.Unit = iso1unitSymbolType_A;
  8986. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  8987. res->EVSECurrentLimitAchieved = 0;
  8988. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  8989. res->EVSEVoltageLimitAchieved = 0;
  8990. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  8991. res->EVSEPowerLimitAchieved = 0;
  8992. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  8993. res->EVSEMaximumVoltageLimit_isUsed = 1u;
  8994. res->EVSEMaximumVoltageLimit.Value = 7500;
  8995. res->EVSEMaximumVoltageLimit.Multiplier = -1;
  8996. //res->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  8997. res->EVSEMaximumVoltageLimit.Unit = iso1unitSymbolType_V;
  8998. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  8999. res->EVSEMaximumCurrentLimit_isUsed = 1u;
  9000. res->EVSEMaximumCurrentLimit.Value = 1200;
  9001. res->EVSEMaximumCurrentLimit.Multiplier = -1;
  9002. //res->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  9003. res->EVSEMaximumCurrentLimit.Unit = iso1unitSymbolType_A;
  9004. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  9005. res->EVSEMaximumPowerLimit_isUsed = 1u;
  9006. res->EVSEMaximumPowerLimit.Value = 6000;
  9007. res->EVSEMaximumPowerLimit.Multiplier = 1;
  9008. //res->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  9009. res->EVSEMaximumPowerLimit.Unit = iso1unitSymbolType_W;
  9010. }
  9011. /*===========================================================================
  9012. FUNCTION: Sudo_Parameter_iso1_ChargingStatusRes
  9013. DESCRIPTION:
  9014. PRE-CONDITION:
  9015. INPUT:
  9016. OUTPUT:
  9017. GLOBAL VARIABLES:
  9018. =============================================================================*/
  9019. void Sudo_Parameter_iso1_ChargingStatusRes()
  9020. {
  9021. //int i = 0;
  9022. struct iso1ChargingStatusResType *res;
  9023. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  9024. init_iso1ChargingStatusResType(&ccs_exi_doc_ISO1.V2G_Message.Body.ChargingStatusRes);
  9025. res = &ccs_exi_doc_ISO1.V2G_Message.Body.ChargingStatusRes;
  9026. ccs_exi_doc_ISO1.V2G_Message.Body.ChargingStatusRes_isUsed = 1u;
  9027. //----- [BODY (1/10)] ResponseCode -----
  9028. res->ResponseCode = OK_ISO15118_2014;
  9029. //----- [BODY (2/10)] AC_EVSEStatus -----
  9030. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  9031. res->AC_EVSEStatus.NotificationMaxDelay = 0;
  9032. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  9033. // iso1EVSENotificationType_None = 0,
  9034. // iso1EVSENotificationType_StopCharging = 1,
  9035. // iso1EVSENotificationType_ReNegotiation = 2
  9036. //----- [BODY (3/10)] EVSEMaxCurrent -----
  9037. res->EVSEMaxCurrent_isUsed = 1u;
  9038. res->EVSEMaxCurrent.Value = 32;
  9039. res->EVSEMaxCurrent.Multiplier = 0;
  9040. res->EVSEMaxCurrent.Unit = iso1unitSymbolType_A;
  9041. //----- [BODY (4/10)] SAScheduleTupleID -----
  9042. res->SAScheduleTupleID = 0;
  9043. //----- [BODY (5/10)] EVSEID -----
  9044. res->EVSEID.charactersLen = 37;
  9045. memset(res->EVSEID.characters, 0, sizeof(res->EVSEID.characters));
  9046. //sprintf((char*)res->EVSEID.characters, CCS_AC_EVSEID);
  9047. //----- [BODY (6/10)] MeterInfo -----
  9048. res->MeterInfo_isUsed = 1u;
  9049. memset(res->MeterInfo.MeterID.characters, 0, sizeof(res->MeterInfo.MeterID.characters));
  9050. memset(res->MeterInfo.SigMeterReading.bytes, 0, sizeof(res->MeterInfo.SigMeterReading.bytes));
  9051. //[MeterInfo][1/5] MeterID
  9052. //sprintf((char*)res->MeterInfo.MeterID , CCS_AC_METER_ID);
  9053. //[MeterInfo][2/5] SigMeterReading (optional)
  9054. //sprintf((char*)res->MeterInfo.SigMeterReading , CCS_AC_SIG_METER_READING);
  9055. //[MeterInfo][3/5] MeterStatus (optional)
  9056. res->MeterInfo.MeterStatus = 0;
  9057. //[MeterInfo][4/5] MeterReading (optional)
  9058. res->MeterInfo.MeterReading = 12345;
  9059. //[MeterInfo][5/5] TMeter (optional)
  9060. res->MeterInfo.TMeter = 1586243587; //Unix Time Stamp format
  9061. //----- [BODY (7/10)] ReceiptRequired -----
  9062. res->ReceiptRequired_isUsed = 1u;
  9063. res->ReceiptRequired = FALSE; //optional
  9064. }
  9065. /*===========================================================================
  9066. FUNCTION: Check_EVErrorCode
  9067. DESCRIPTION:
  9068. PRE-CONDITION:
  9069. INPUT:
  9070. OUTPUT:
  9071. GLOBAL VARIABLES:
  9072. =============================================================================*/
  9073. void Check_EVErrorCode(int code)
  9074. {
  9075. if (code != NO_ERROR) //NO_ERROR = 0
  9076. {
  9077. //Asking CSU to Stop
  9078. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9079. //Update_V2G_Flow_Status(Other_Fault);
  9080. //memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  9081. sprintf((char*)buf_log_evcomm,
  9082. "Stop by EV (EVErrorCode = %d (DEC))",
  9083. code);
  9084. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9085. }
  9086. }
  9087. /*===========================================================================
  9088. FUNCTION: Proc_din_CurrentDemandRes
  9089. DESCRIPTION:
  9090. PRE-CONDITION:
  9091. INPUT:
  9092. OUTPUT:
  9093. GLOBAL VARIABLES:
  9094. =============================================================================*/
  9095. int Proc_din_CurrentDemandRes(int AcceptFd)
  9096. {
  9097. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9098. //if it is not the same, the packet should be ignored.
  9099. //STEP 1: ============ Initialize ============
  9100. int errn = 0;
  9101. bitstream_t v2g_tx_stream;
  9102. static struct CurrentDemandResponse_DIN70121 *cur;
  9103. static struct ChargingInfoData *sys;
  9104. // int i = 0;
  9105. // static int EVSE_max_current;
  9106. // int tmp = 0;
  9107. size_t pos = 0;
  9108. v2g_tx_stream.pos = &pos;
  9109. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9110. v2g_tx_stream.data = V2GTP_Tx_buf;
  9111. cur = &ShmCcsData->V2GMessage_DIN70121.CurrentDemandResponse;
  9112. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9113. cur->ResponseCode = OK_DIN70121;
  9114. //EVSE Status Code
  9115. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  9116. //[HEADER] Check Req SessionID
  9117. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  9118. {
  9119. cur->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  9120. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  9121. errn = -1;
  9122. }
  9123. //Check for SequenceError
  9124. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9125. {
  9126. cur->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  9127. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  9128. errn = -1;
  9129. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9130. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9131. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9132. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9133. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9134. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9135. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9136. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9137. }
  9138. //Check for negative EvBatteryMaxCurrent
  9139. if (sys->EvBatteryMaxCurrent < 0)
  9140. {
  9141. sprintf((char*)buf_log_evcomm,
  9142. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  9143. sys->EvBatteryMaxCurrent);
  9144. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9145. cur->DC_EVSEStatus.EVSEStatusCode = dinresponseCodeType_FAILED_WrongChargeParameter; //16
  9146. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9147. errn = -1;
  9148. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9149. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  9150. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9151. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9152. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9153. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9154. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  9155. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  9156. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9157. }
  9158. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9159. SAVE_PhysicalValueType_DIN70121(&cur->EVSEPresentCurrent, (int) (sys->PresentChargingCurrent * 10), A_DIN70121);
  9160. SAVE_PhysicalValueType_DIN70121(&cur->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  9161. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == DISABLE
  9162. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_DIN70121);
  9163. #endif
  9164. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_DIN70121);
  9165. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_DIN70121);
  9166. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  9167. if (sys->EvBatteryMaxVoltage <= 500)
  9168. {
  9169. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumVoltageLimit, (int) (500 * 10), V_DIN70121); //500V
  9170. }
  9171. #endif
  9172. //Limit the EVTargetCurrent should be under EVSEMaximumCurrentLimit
  9173. if (sys->EvBatterytargetCurrent > sys->AvailableChargingCurrent)
  9174. {
  9175. sys->EvBatterytargetCurrent = sys->AvailableChargingCurrent;
  9176. //[To-Do] Limit is achieved flag
  9177. }
  9178. //Isolation Status
  9179. if (sys->IsolationStatus == GFD_Invalid) //0:invalid (on going)
  9180. {
  9181. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  9182. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9183. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9184. sprintf((char*)buf_log_evcomm, "[Error][Proc_din_CurrentDemandRes]IsolationStatus = %d", sys->IsolationStatus);
  9185. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9186. Update_V2G_Flow_Status(Other_Fault);
  9187. errn = -1;
  9188. }
  9189. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  9190. {
  9191. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  9192. }
  9193. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  9194. {
  9195. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  9196. }
  9197. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  9198. {
  9199. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  9200. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9201. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9202. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_CurrentDemandRes]GFD_Fault => Emergency Shutdown");
  9203. Update_V2G_Flow_Status(Other_Fault);
  9204. errn = -1;
  9205. }
  9206. else //GFD_No_IMD or other unexpected status
  9207. {
  9208. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  9209. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9210. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9211. sprintf((char*)buf_log_evcomm, "[Error][Proc_din_CurrentDemandRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  9212. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9213. Update_V2G_Flow_Status(Other_Fault);
  9214. errn = -1;
  9215. }
  9216. //For testing with Tesla Model 3
  9217. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == ENABLE
  9218. //[CAUTION][To-Do] Decresement of Current is not implemented.
  9219. //SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumCurrentLimit, 10, A_DIN70121);
  9220. if (sys->EvBatterytargetCurrent <= 0)
  9221. {
  9222. EVSE_max_current = 50; //10A
  9223. cur->EVSEMaximumCurrentLimit.Value = 50; //10A
  9224. /*
  9225. DEBUG_PRINTF_EVCOMM_DETAIL("1PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9226. sys->PresentChargingCurrent,
  9227. sys->EvBatterytargetCurrent,
  9228. EVSE_max_current,
  9229. cur->EVSEMaximumCurrentLimit.Value
  9230. );
  9231. */
  9232. }
  9233. else //1A
  9234. {
  9235. /*
  9236. DEBUG_PRINTF_EVCOMM_DETAIL("2PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9237. sys->PresentChargingCurrent,
  9238. sys->EvBatterytargetCurrent,
  9239. EVSE_max_current,
  9240. cur->EVSEMaximumCurrentLimit.Value
  9241. );
  9242. */
  9243. if ((abs((int)sys->PresentChargingCurrent - (int)sys->EvBatterytargetCurrent) < 3)&&(abs((int)(EVSE_max_current/10) - (int)sys->EvBatterytargetCurrent) < 3))
  9244. {
  9245. tmp = EVSE_max_current + 50; //10A
  9246. if (tmp <= (int)(sys->AvailableChargingCurrent * 10))
  9247. {
  9248. cur->EVSEMaximumCurrentLimit.Value = (int)tmp;
  9249. EVSE_max_current = tmp;
  9250. }
  9251. else
  9252. {
  9253. cur->EVSEMaximumCurrentLimit.Value = (int) (sys->AvailableChargingCurrent * 10); //max is set to 40A
  9254. EVSE_max_current = (int) (sys->AvailableChargingCurrent * 10);
  9255. }
  9256. }
  9257. }
  9258. #endif
  9259. //Check for CSU command of "Stop by EVSE"
  9260. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9261. {
  9262. //Check for Alarm Code: CCS GFD trip (012235)
  9263. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9264. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9265. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9266. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9267. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9268. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9269. {
  9270. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][Error][CurrentDemandRes]CCS GFD trip => EVSE_Shutdown");
  9271. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  9272. }
  9273. else
  9274. {
  9275. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][Error][CurrentDemandRes]EVSE_Shutdown");
  9276. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9277. }
  9278. //cur->ResponseCode = FAILED_DIN70121;
  9279. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9280. //errn = -1;
  9281. }
  9282. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9283. {
  9284. //Check for Alarm Code: CCS GFD trip (012235)
  9285. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9286. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9287. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9288. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9289. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9290. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9291. {
  9292. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][Error][CurrentDemandRes]CCS GFD trip => EVSE_EmergencyShutdown");
  9293. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  9294. }
  9295. else
  9296. {
  9297. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][Error][CurrentDemandRes]EVSE_EmergencyShutdown");
  9298. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9299. }
  9300. //cur->ResponseCode = FAILED_DIN70121;
  9301. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9302. //errn = -1;
  9303. }
  9304. else if (ShmInternalComm->ChargingPermission == FALSE)
  9305. {
  9306. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][Error][CurrentDemandRes]Permission OFF");
  9307. //cur->ResponseCode = FAILED_DIN70121;
  9308. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9309. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9310. //errn = -1;
  9311. }
  9312. else
  9313. {
  9314. //null
  9315. }
  9316. //Response to CP Error
  9317. #if CP_PROTECTION_MECHANISM == ENABLE
  9318. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  9319. {
  9320. cur->ResponseCode = FAILED_DIN70121;
  9321. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9322. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9323. Update_V2G_Flow_Status(Other_Fault);
  9324. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9325. //CCS_SECC_CP_State_Error (023889)
  9326. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9327. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9328. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9329. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  9330. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  9331. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  9332. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  9333. sprintf((char*)buf_log_evcomm,
  9334. "[CurrentDemand]Emergency Stop by CP Error (%d, %.02f V)",
  9335. sys->CpState,
  9336. sys->CpVoltage);
  9337. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9338. }
  9339. #endif
  9340. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9341. #if PARAMETER_NORMAL_MODE == ENABLE
  9342. SHM_Read_din_CurrentDemandRes(&ccs_exi_doc_DIN, ShmCcsData);
  9343. #else
  9344. Sudo_Parameter_din_CurrentDemandRes();
  9345. #endif
  9346. //STEP 4: ============ Encode and Send Response Message ===========
  9347. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  9348. {
  9349. errn = -1;
  9350. }
  9351. //STEP 5: ============ Update Flags ===========
  9352. return errn;
  9353. }
  9354. /*===========================================================================
  9355. FUNCTION: Proc_iso1_CurrentDemandRes
  9356. DESCRIPTION:
  9357. PRE-CONDITION:
  9358. INPUT:
  9359. OUTPUT:
  9360. GLOBAL VARIABLES:
  9361. =============================================================================*/
  9362. int Proc_iso1_CurrentDemandRes(int AcceptFd)
  9363. {
  9364. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9365. //if it is not the same, the packet should be ignored.
  9366. //STEP 1: ============ Initialize ============
  9367. int errn = 0;
  9368. bitstream_t v2g_tx_stream;
  9369. static struct CurrentDemandResponse_ISO15118_2014 *cur;
  9370. static struct ChargingInfoData *sys;
  9371. // int i = 0;
  9372. // static int EVSE_max_current;
  9373. // int tmp = 0;
  9374. size_t pos = 0;
  9375. v2g_tx_stream.pos = &pos;
  9376. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9377. v2g_tx_stream.data = V2GTP_Tx_buf;
  9378. cur = &ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandResponse;
  9379. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9380. cur->ResponseCode = OK_ISO15118_2014;
  9381. //EVSE Status Code
  9382. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  9383. //[HEADER] Check Req SessionID
  9384. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  9385. {
  9386. cur->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  9387. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  9388. errn = -1;
  9389. }
  9390. //Check for SequenceError
  9391. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9392. {
  9393. cur->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  9394. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  9395. errn = -1;
  9396. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9397. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9398. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9399. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9400. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9401. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9402. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9403. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9404. }
  9405. //Check for negative EvBatteryMaxCurrent
  9406. if (sys->EvBatteryMaxCurrent < 0)
  9407. {
  9408. sprintf((char*)buf_log_evcomm,
  9409. "[ERROR]EvBatteryMaxCurrent is negative(%.02f) => End_Process",
  9410. sys->EvBatteryMaxCurrent);
  9411. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9412. cur->ResponseCode = iso1responseCodeType_FAILED_WrongChargeParameter; //16
  9413. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  9414. errn = -1;
  9415. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9416. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  9417. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9418. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9419. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9420. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9421. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  9422. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  9423. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9424. }
  9425. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9426. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEPresentCurrent, (int) (sys->PresentChargingCurrent * 10), A_ISO15118_2014);
  9427. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  9428. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == DISABLE
  9429. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  9430. #endif
  9431. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_ISO15118_2014);
  9432. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_ISO15118_2014);
  9433. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  9434. if (sys->EvBatteryMaxVoltage <= 500)
  9435. {
  9436. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumVoltageLimit, (int) (500 * 10), V_ISO15118_2014); //500V
  9437. }
  9438. #endif
  9439. //Limit the EVTargetCurrent should be under EVSEMaximumCurrentLimit
  9440. if (sys->EvBatterytargetCurrent > sys->AvailableChargingCurrent)
  9441. {
  9442. sys->EvBatterytargetCurrent = sys->AvailableChargingCurrent;
  9443. //[To-Do] Limit is achieved flag
  9444. }
  9445. //Isolation Status
  9446. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  9447. {
  9448. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  9449. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9450. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9451. sprintf((char*)buf_log_evcomm, "[Error][Proc_iso1_CurrentDemandRes]IsolationStatus = %d", sys->IsolationStatus);
  9452. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9453. Update_V2G_Flow_Status(Other_Fault);
  9454. errn = -1;
  9455. }
  9456. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  9457. {
  9458. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  9459. }
  9460. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  9461. {
  9462. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  9463. }
  9464. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  9465. {
  9466. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  9467. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9468. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9469. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_CurrentDemandRes]GFD_Fault => EmergencyShutdown");
  9470. Update_V2G_Flow_Status(Other_Fault);
  9471. errn = -1;
  9472. }
  9473. else
  9474. {
  9475. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  9476. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9477. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9478. sprintf((char*)buf_log_evcomm, "[Error][Proc_iso1_CurrentDemandRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  9479. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9480. Update_V2G_Flow_Status(Other_Fault);
  9481. errn = -1;
  9482. }
  9483. //For testing with Tesla Model 3
  9484. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == ENABLE
  9485. //[CAUTION][To-Do] Decresement of Current is not implemented.
  9486. //SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumCurrentLimit, 10, A_ISO15118_2014);
  9487. if (sys->EvBatterytargetCurrent <= 0)
  9488. {
  9489. EVSE_max_current = 50; //10A
  9490. cur->EVSEMaximumCurrentLimit.Value = 50; //10A
  9491. /*
  9492. DEBUG_PRINTF_EVCOMM_DETAIL("1PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9493. sys->PresentChargingCurrent,
  9494. sys->EvBatterytargetCurrent,
  9495. EVSE_max_current,
  9496. cur->EVSEMaximumCurrentLimit.Value
  9497. );
  9498. */
  9499. }
  9500. else //1A
  9501. {
  9502. /*
  9503. DEBUG_PRINTF_EVCOMM_DETAIL("2PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  9504. sys->PresentChargingCurrent,
  9505. sys->EvBatterytargetCurrent,
  9506. EVSE_max_current,
  9507. cur->EVSEMaximumCurrentLimit.Value
  9508. );
  9509. */
  9510. if ((abs((int)sys->PresentChargingCurrent - (int)sys->EvBatterytargetCurrent) < 3)&&(abs((int)(EVSE_max_current/10) - (int)sys->EvBatterytargetCurrent) < 3))
  9511. {
  9512. tmp = EVSE_max_current + 50; //10A
  9513. if (tmp <= (int)(sys->AvailableChargingCurrent * 10))
  9514. {
  9515. cur->EVSEMaximumCurrentLimit.Value = (int)tmp;
  9516. EVSE_max_current = tmp;
  9517. }
  9518. else
  9519. {
  9520. cur->EVSEMaximumCurrentLimit.Value = (int) (sys->AvailableChargingCurrent * 10); //max is set to 40A
  9521. EVSE_max_current = (int) (sys->AvailableChargingCurrent * 10);
  9522. }
  9523. }
  9524. }
  9525. #endif
  9526. //Check for CSU command of "Stop by EVSE"
  9527. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9528. {
  9529. //Check for Alarm Code: CCS GFD trip (012235)
  9530. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9531. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9532. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9533. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9534. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9535. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9536. {
  9537. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][Error][CurrentDemandRes]CCS GFD trip => EVSE_Shutdown");
  9538. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  9539. }
  9540. else
  9541. {
  9542. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][Error][CurrentDemandRes]EVSE_Shutdown");
  9543. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9544. }
  9545. //cur->ResponseCode = FAILED_ISO15118_2014;
  9546. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  9547. //errn = -1;
  9548. }
  9549. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9550. {
  9551. //Check for Alarm Code: CCS GFD trip (012235)
  9552. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  9553. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  9554. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  9555. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  9556. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  9557. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  9558. {
  9559. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][Error][CurrentDemandRes]CCS GFD trip => EVSE_EmergencyShutdown");
  9560. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  9561. }
  9562. else
  9563. {
  9564. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][Error][CurrentDemandRes]EVSE_EmergencyShutdown");
  9565. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9566. }
  9567. //cur->ResponseCode = FAILED_ISO15118_2014;
  9568. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9569. //errn = -1;
  9570. }
  9571. else if (ShmInternalComm->ChargingPermission == FALSE)
  9572. {
  9573. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][Error][CurrentDemandRes]Permission OFF");
  9574. //cur->ResponseCode = FAILED_ISO15118_2014;
  9575. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  9576. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  9577. //errn = -1;
  9578. }
  9579. else
  9580. {
  9581. //null
  9582. }
  9583. //Response to CP Error
  9584. #if CP_PROTECTION_MECHANISM == ENABLE
  9585. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  9586. {
  9587. cur->ResponseCode = FAILED_ISO15118_2014;
  9588. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9589. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9590. Update_V2G_Flow_Status(Other_Fault);
  9591. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9592. //CCS_SECC_CP_State_Error (023889)
  9593. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9594. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9595. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9596. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  9597. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  9598. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  9599. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  9600. sprintf((char*)buf_log_evcomm,
  9601. "[CurrentDemand]Emergency Stop by CP Error (%d, %.02f V)",
  9602. sys->CpState,
  9603. sys->CpVoltage);
  9604. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9605. }
  9606. #endif
  9607. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9608. #if PARAMETER_NORMAL_MODE == ENABLE
  9609. SHM_Read_iso1_CurrentDemandRes(&ccs_exi_doc_ISO1, ShmCcsData);
  9610. #else
  9611. Sudo_Parameter_iso1_CurrentDemandRes();
  9612. #endif
  9613. //STEP 4: ============ Encode and Send Response Message ===========
  9614. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  9615. {
  9616. errn = -1;
  9617. }
  9618. //STEP 5: ============ Update Flags ===========
  9619. return errn;
  9620. }
  9621. /*===========================================================================
  9622. FUNCTION: Proc_iso1_ChargingStatusRes
  9623. DESCRIPTION:
  9624. PRE-CONDITION:
  9625. INPUT:
  9626. OUTPUT:
  9627. GLOBAL VARIABLES:
  9628. =============================================================================*/
  9629. int Proc_iso1_ChargingStatusRes(int AcceptFd)
  9630. {
  9631. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9632. //if it is not the same, the packet should be ignored.
  9633. //STEP 1: ============ Initialize ============
  9634. //int i = 0;
  9635. int errn = 0;
  9636. bitstream_t v2g_tx_stream;
  9637. static struct ChargingStatusResponse_ISO15118_2014 *res;
  9638. static struct ChargingInfoData *sys;
  9639. size_t pos = 0;
  9640. v2g_tx_stream.pos = &pos;
  9641. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9642. v2g_tx_stream.data = V2GTP_Tx_buf;
  9643. res = &ShmCcsData->V2GMessage_ISO15118_2014.ChargingStatusResponse;
  9644. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9645. //Init
  9646. res->ResponseCode = OK_ISO15118_2014;
  9647. res->ReceiptRequired = FALSE; //optional
  9648. res->SAScheduleTupleID = 0;
  9649. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  9650. res->AC_EVSEStatus.NotificationMaxDelay = 0;
  9651. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  9652. // iso1EVSENotificationType_None = 0,
  9653. // iso1EVSENotificationType_StopCharging = 1,
  9654. // iso1EVSENotificationType_ReNegotiation = 2
  9655. //[HEADER] Check Req SessionID
  9656. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  9657. {
  9658. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  9659. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  9660. errn = -1;
  9661. }
  9662. //Check for SequenceError
  9663. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9664. {
  9665. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  9666. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  9667. errn = -1;
  9668. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9669. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9670. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9671. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9672. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9673. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9674. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9675. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9676. }
  9677. //EVSE ID
  9678. memset(res->EVSEID, 0, sizeof(res->EVSEID));
  9679. //sprintf((char*)res->EVSEID, CCS_AC_EVSEID);
  9680. //[MeterInfo][0/5] init
  9681. //memset(res->MeterInfo.MeterID.characters, 0, sizeof(res->MeterInfo.MeterID.characters));
  9682. //memset(res->MeterInfo.SigMeterReading.bytes, 0, sizeof(res->MeterInfo.SigMeterReading.bytes));
  9683. //[MeterInfo][1/5] MeterID
  9684. //sprintf((char*)res->MeterInfo.MeterID , CCS_AC_METER_ID);
  9685. //[MeterInfo][2/5] SigMeterReading (optional)
  9686. //sprintf((char*)res->MeterInfo.SigMeterReading , CCS_AC_SIG_METER_READING);
  9687. //[MeterInfo][3/5] MeterStatus (optional)
  9688. res->MeterInfo.MeterStatus = 0;
  9689. //[MeterInfo][4/5] MeterReading (optional)
  9690. res->MeterInfo.MeterReading = 12345;
  9691. //[MeterInfo][5/5] TMeter (optional)
  9692. res->MeterInfo.TMeter = 1586243587; //Unix Time Stamp format
  9693. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9694. SAVE_PhysicalValueType_ISO15118_2014(&res->EVSEMaxCurrent, (int)(sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  9695. //Isolation Status (RCD)
  9696. if (sys->IsolationStatus == 0) //Isolation is invalid
  9697. {
  9698. res->AC_EVSEStatus.RCD = TRUE; //FALSE(no error), TRUE(error is detected)
  9699. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9700. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9701. sprintf((char*)buf_log_evcomm, "[ISO1][Error][ChargingStatusRes]IsolationStatus = %d", sys->IsolationStatus);
  9702. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9703. Update_V2G_Flow_Status(Other_Fault);
  9704. errn = -1;
  9705. }
  9706. else if (sys->IsolationStatus == 1) //Isolation is valid
  9707. {
  9708. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  9709. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  9710. }
  9711. else
  9712. {
  9713. res->AC_EVSEStatus.RCD = TRUE; //FALSE(no error), TRUE(error is detected)
  9714. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9715. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9716. sprintf((char*)buf_log_evcomm, "[ISO1][Error][ChargingStatusRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  9717. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9718. Update_V2G_Flow_Status(Other_Fault);
  9719. errn = -1;
  9720. }
  9721. //Check for CSU command of "Stop by EVSE"
  9722. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9723. {
  9724. //res->ResponseCode = FAILED_ISO15118_2014;
  9725. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9726. //errn = -1;
  9727. }
  9728. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9729. {
  9730. //res->ResponseCode = FAILED_ISO15118_2014;
  9731. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9732. //errn = -1;
  9733. }
  9734. else if (ShmInternalComm->ChargingPermission == FALSE)
  9735. {
  9736. //res->ResponseCode = FAILED_ISO15118_2014;
  9737. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9738. //errn = -1;
  9739. }
  9740. //Response to CP Error
  9741. #if CP_PROTECTION_MECHANISM == ENABLE
  9742. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  9743. {
  9744. res->ResponseCode = FAILED_ISO15118_2014;
  9745. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  9746. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9747. Update_V2G_Flow_Status(Other_Fault);
  9748. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9749. //CCS_SECC_CP_State_Error (023889)
  9750. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9751. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9752. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9753. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  9754. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  9755. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  9756. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  9757. sprintf((char*)buf_log_evcomm,
  9758. "[ChargingStatus]Emergency Stop by CP Error (%d, %.02f V)",
  9759. sys->CpState,
  9760. sys->CpVoltage);
  9761. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9762. }
  9763. #endif
  9764. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9765. #if PARAMETER_NORMAL_MODE == ENABLE
  9766. SHM_Read_iso1_ChargingStatusRes(&ccs_exi_doc_ISO1, ShmCcsData);
  9767. #else
  9768. Sudo_Parameter_iso1_ChargingStatusRes();
  9769. #endif
  9770. //STEP 4: ============ Encode and Send Response Message ===========
  9771. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  9772. {
  9773. errn = -1;
  9774. }
  9775. //STEP 5: ============ Update Flags ===========
  9776. return errn;
  9777. }
  9778. /*===========================================================================
  9779. FUNCTION: Proc_din_CurrentDemandReq
  9780. DESCRIPTION:
  9781. PRE-CONDITION:
  9782. INPUT:
  9783. OUTPUT:
  9784. GLOBAL VARIABLES:
  9785. =============================================================================*/
  9786. int Proc_din_CurrentDemandReq(int AcceptFd)
  9787. {
  9788. int errn = 0;
  9789. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_CurrentDemandReq] Got CurrentDemandReq\n");
  9790. //Print the decoded XML Document
  9791. PRINT_XML_DOC_DIN_CurrentDemandReq(&ccs_exi_doc_DIN);
  9792. //Save into Share Memory
  9793. SHM_Save_din_CurrentDemandReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  9794. //Check for EV Error Code
  9795. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.CurrentDemandRequest.DC_EVStatus.EVErrorCode);
  9796. errn = Proc_din_CurrentDemandRes(AcceptFd);
  9797. if (errn == 0)
  9798. {
  9799. //Response is sent successfully.
  9800. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]CurrentDemandRes");
  9801. }
  9802. else
  9803. {
  9804. sprintf((char*)buf_log_evcomm,
  9805. "[DIN][Error][CurrentDemandReq]CurrentDemandRes: fail(%d,DEC)",
  9806. errn);
  9807. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9808. }
  9809. return errn;
  9810. }
  9811. /*===========================================================================
  9812. FUNCTION: Proc_iso1_CurrentDemandReq
  9813. DESCRIPTION:
  9814. PRE-CONDITION:
  9815. INPUT:
  9816. OUTPUT:
  9817. GLOBAL VARIABLES:
  9818. =============================================================================*/
  9819. int Proc_iso1_CurrentDemandReq(int AcceptFd)
  9820. {
  9821. int errn = 0;
  9822. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_CurrentDemandReq] Got CurrentDemandReq\n");
  9823. //Print the decoded XML Document
  9824. PRINT_XML_DOC_ISO1_CurrentDemandReq(&ccs_exi_doc_ISO1);
  9825. //Save into Share Memory
  9826. SHM_Save_iso1_CurrentDemandReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  9827. //Check for EV Error Code
  9828. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandRequest.DC_EVStatus.EVErrorCode);
  9829. errn = Proc_iso1_CurrentDemandRes(AcceptFd);
  9830. if (errn == 0)
  9831. {
  9832. //Response is sent successfully.
  9833. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]CurrentDemandRes");
  9834. }
  9835. else
  9836. {
  9837. sprintf((char*)buf_log_evcomm,
  9838. "[ISO1][Error][CurrentDemandReq]CurrentDemandRes: fail(%d,DEC)",
  9839. errn);
  9840. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9841. }
  9842. return errn;
  9843. }
  9844. /*===========================================================================
  9845. FUNCTION: Proc_iso1_ChargingStatusReq
  9846. DESCRIPTION:
  9847. PRE-CONDITION:
  9848. INPUT:
  9849. OUTPUT:
  9850. GLOBAL VARIABLES:
  9851. =============================================================================*/
  9852. int Proc_iso1_ChargingStatusReq(int AcceptFd)
  9853. {
  9854. int errn = 0;
  9855. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_ChargingStatusReq] Got ChargingStatusReq\n");
  9856. //Print the decoded XML Document
  9857. PRINT_XML_DOC_ISO1_ChargingStatusReq(&ccs_exi_doc_ISO1);
  9858. //Save into Share Memory
  9859. SHM_Save_iso1_ChargingStatusReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  9860. //Check for EV Error Code
  9861. //no content in ISO1
  9862. errn = Proc_iso1_ChargingStatusRes(AcceptFd);
  9863. if (errn == 0)
  9864. {
  9865. //Response is sent successfully.
  9866. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]ChargingStatusRes");
  9867. }
  9868. else
  9869. {
  9870. sprintf((char*)buf_log_evcomm,
  9871. "[ISO1][Error][ChargingStatusReq]ChargingStatusRes: fail(%d,DEC)",
  9872. errn);
  9873. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  9874. }
  9875. return errn;
  9876. }
  9877. /*===========================================================================
  9878. FUNCTION: Proc_din_PowerDeliveryStopRes
  9879. DESCRIPTION:
  9880. PRE-CONDITION:
  9881. INPUT:
  9882. OUTPUT:
  9883. GLOBAL VARIABLES:
  9884. =============================================================================*/
  9885. int Proc_din_PowerDeliveryStopRes(int AcceptFd)
  9886. {
  9887. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9888. //if it is not the same, the packet should be ignored.
  9889. //STEP 1: ============ Initialize ============
  9890. //int i = 0;
  9891. int errn = 0;
  9892. bitstream_t v2g_tx_stream;
  9893. struct dinPowerDeliveryResType *res;
  9894. struct ChargingInfoData *sys;
  9895. size_t pos = 0;
  9896. v2g_tx_stream.pos = &pos;
  9897. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9898. v2g_tx_stream.data = V2GTP_Tx_buf;
  9899. res = &ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryRes;
  9900. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9901. res->ResponseCode = OK_DIN70121;
  9902. //[HEADER] Check Req SessionID
  9903. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  9904. {
  9905. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  9906. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  9907. errn = -1;
  9908. }
  9909. //Check for SequenceError
  9910. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9911. {
  9912. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  9913. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  9914. errn = -1;
  9915. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9916. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9917. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9918. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9919. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9920. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9921. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9922. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9923. }
  9924. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9925. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9926. #if PARAMETER_NORMAL_MODE == ENABLE
  9927. ShmCcsData->V2GMessage_DIN70121.PowerDeliveryResponse.DC_EVSEStatus.EVSEStatusCode = EVSE_NotReady;
  9928. SHM_Read_din_PowerDeliveryRes(&ccs_exi_doc_DIN, ShmCcsData);
  9929. #else
  9930. Sudo_Parameter_din_PreChargeRes();
  9931. #endif
  9932. //EVSE Status Code
  9933. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  9934. //Check for CSU command of "Stop by EVSE"
  9935. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  9936. {
  9937. //res->ResponseCode = FAILED_DIN70121;
  9938. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9939. }
  9940. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  9941. {
  9942. //res->ResponseCode = FAILED_DIN70121;
  9943. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9944. }
  9945. else if (ShmInternalComm->ChargingPermission == FALSE)
  9946. {
  9947. //res->ResponseCode = FAILED_DIN70121;
  9948. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  9949. }
  9950. //STEP 4: ============ Encode and Send Response Message ===========
  9951. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  9952. {
  9953. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_PowerDeliveryStopRes][Error]Tx encoded msg error");
  9954. errn = -1;
  9955. }
  9956. //STEP 5: ============ Update Flags ===========
  9957. return errn;
  9958. }
  9959. /*===========================================================================
  9960. FUNCTION: Proc_iso1_PowerDeliveryStopRes
  9961. DESCRIPTION:
  9962. PRE-CONDITION:
  9963. INPUT:
  9964. OUTPUT:
  9965. GLOBAL VARIABLES:
  9966. =============================================================================*/
  9967. int Proc_iso1_PowerDeliveryStopRes(int AcceptFd)
  9968. {
  9969. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  9970. //if it is not the same, the packet should be ignored.
  9971. //STEP 1: ============ Initialize ============
  9972. //int i = 0;
  9973. int errn = 0;
  9974. bitstream_t v2g_tx_stream;
  9975. struct iso1PowerDeliveryResType *res;
  9976. struct ChargingInfoData *sys;
  9977. size_t pos = 0;
  9978. v2g_tx_stream.pos = &pos;
  9979. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9980. v2g_tx_stream.data = V2GTP_Tx_buf;
  9981. res = &ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryRes;
  9982. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9983. res->ResponseCode = OK_ISO15118_2014;
  9984. //[HEADER] Check Req SessionID
  9985. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  9986. {
  9987. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  9988. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  9989. errn = -1;
  9990. }
  9991. //Check for SequenceError
  9992. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9993. {
  9994. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  9995. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  9996. errn = -1;
  9997. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9998. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9999. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10000. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10001. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10002. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10003. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10004. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10005. }
  10006. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10007. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10008. #if PARAMETER_NORMAL_MODE == ENABLE
  10009. ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryResponse.DC_EVSEStatus.DC_EVSEStatusCode = EVSE_NotReady;
  10010. SHM_Read_iso1_PowerDeliveryRes(&ccs_exi_doc_ISO1, ShmCcsData);
  10011. #else
  10012. Sudo_Parameter_iso1_PreChargeRes();
  10013. #endif
  10014. //EVSE Status Code
  10015. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  10016. //Check for CSU command of "Stop by EVSE"
  10017. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  10018. {
  10019. //res->ResponseCode = FAILED_ISO15118_2014;
  10020. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  10021. }
  10022. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  10023. {
  10024. //res->ResponseCode = FAILED_ISO15118_2014;
  10025. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10026. }
  10027. else if (ShmInternalComm->ChargingPermission == FALSE)
  10028. {
  10029. //res->ResponseCode = FAILED_ISO15118_2014;
  10030. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  10031. }
  10032. //STEP 4: ============ Encode and Send Response Message ===========
  10033. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  10034. {
  10035. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_PowerDeliveryStopRes][Error]Tx encoded msg error");
  10036. errn = -1;
  10037. }
  10038. //STEP 5: ============ Update Flags ===========
  10039. return errn;
  10040. }
  10041. /*===========================================================================
  10042. FUNCTION: Proc_din_PowerDeliveryStopReq
  10043. DESCRIPTION:
  10044. PRE-CONDITION:
  10045. INPUT:
  10046. OUTPUT:
  10047. GLOBAL VARIABLES:
  10048. =============================================================================*/
  10049. int Proc_din_PowerDeliveryStopReq(int AcceptFd)
  10050. {
  10051. int errn = 0;
  10052. //Request CSU to STOP
  10053. //This should be reponsed as soon as possible once this message is received.
  10054. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10055. EVCOMM_SYS_INFO.EvBatterytargetVoltage = 0;
  10056. EVCOMM_SYS_INFO.EvBatterytargetCurrent = 0;
  10057. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PowerDeliveryReq (2nd)\n");
  10058. //Print the decoded XML Document
  10059. PRINT_XML_DOC_DIN_PowerDeliveryReq(&ccs_exi_doc_DIN);
  10060. //Save into Share Memory
  10061. SHM_Save_din_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  10062. //Check for EV Error Code
  10063. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  10064. //[To-Do] Sending response after the EVSE output voltage decreases to under 60V
  10065. usleep(1500000); //1.5 seconds
  10066. //sleep(1); //1 second
  10067. errn = Proc_din_PowerDeliveryStopRes(AcceptFd);
  10068. if (errn == 0)
  10069. {
  10070. //send response successfully.
  10071. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]PowerDeliveryRes (2nd)\n");
  10072. }
  10073. else
  10074. {
  10075. sprintf((char*)buf_log_evcomm,
  10076. "[Error][SeccComm][Proc_din_PowerDeliveryStopReq]Proc_iso1_PowerDeliveryStopRes(): %d (DEC)",
  10077. errn);
  10078. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10079. }
  10080. return errn;
  10081. }
  10082. /*===========================================================================
  10083. FUNCTION: Proc_iso1_PowerDeliveryStopReq
  10084. DESCRIPTION:
  10085. PRE-CONDITION:
  10086. INPUT:
  10087. OUTPUT:
  10088. GLOBAL VARIABLES:
  10089. =============================================================================*/
  10090. int Proc_iso1_PowerDeliveryStopReq(int AcceptFd)
  10091. {
  10092. int errn = 0;
  10093. //Request CSU to STOP
  10094. //This should be reponsed as soon as possible once this message is received.
  10095. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10096. EVCOMM_SYS_INFO.EvBatterytargetVoltage = 0;
  10097. EVCOMM_SYS_INFO.EvBatterytargetCurrent = 0;
  10098. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PowerDeliveryReq (2nd)\n");
  10099. //Print the decoded XML Document
  10100. PRINT_XML_DOC_ISO1_PowerDeliveryReq(&ccs_exi_doc_ISO1);
  10101. //Save into Share Memory
  10102. SHM_Save_iso1_PowerDeliveryReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  10103. //Check for EV Error Code
  10104. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  10105. //[To-Do] Sending response after the EVSE output voltage decreases to under 60V
  10106. usleep(1500000); //1.5 seconds
  10107. //sleep(1); //1 second
  10108. errn = Proc_iso1_PowerDeliveryStopRes(AcceptFd);
  10109. if (errn == 0)
  10110. {
  10111. //send response successfully.
  10112. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]PowerDeliveryRes (2nd)");
  10113. }
  10114. else
  10115. {
  10116. sprintf((char*)buf_log_evcomm,
  10117. "[Error][SeccComm][Proc_iso1_PowerDeliveryStopReq]Proc_iso1_PowerDeliveryStopRes(): %d (DEC)",
  10118. errn);
  10119. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10120. }
  10121. return errn;
  10122. }
  10123. /*===========================================================================
  10124. FUNCTION: SHM_Init_din_WeldingDetectionRes
  10125. DESCRIPTION:
  10126. PRE-CONDITION:
  10127. INPUT:
  10128. 1. shm_ccs
  10129. OUTPUT:
  10130. 1. shm_ccs
  10131. GLOBAL VARIABLES:
  10132. =============================================================================*/
  10133. void SHM_Init_din_WeldingDetectionRes(struct CcsData *shm_ccs)
  10134. {
  10135. struct WeldingDetectionResponse_DIN70121 *in;
  10136. in = &shm_ccs->V2GMessage_DIN70121.WeldingDetectionResponse;
  10137. //----- [BODY (1/3)] ResponseCode -----
  10138. in->ResponseCode = dinresponseCodeType_OK;
  10139. //----- [BODY (2/3)] EVSEPresentVoltage -----
  10140. short value = 0; int multiplier = 0; unsigned char unit = 0;
  10141. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for test only.
  10142. value = 0; multiplier = 0; unit = V_DIN70121;
  10143. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  10144. //----- [BODY (3/3)] DC_EVSEStatus -----
  10145. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  10146. }
  10147. /*===========================================================================
  10148. FUNCTION: Sudo_Parameter_din_WeldingDetectionRes
  10149. DESCRIPTION:
  10150. PRE-CONDITION:
  10151. INPUT:
  10152. OUTPUT:
  10153. GLOBAL VARIABLES:
  10154. =============================================================================*/
  10155. void Sudo_Parameter_din_WeldingDetectionRes()
  10156. {
  10157. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  10158. init_dinWeldingDetectionResType(&ccs_exi_doc_DIN.V2G_Message.Body.WeldingDetectionRes);
  10159. ccs_exi_doc_DIN.V2G_Message.Body.WeldingDetectionRes_isUsed = 1u;
  10160. //----- [BODY (1/3)] ResponseCode -----
  10161. struct dinWeldingDetectionResType *res;
  10162. res = &ccs_exi_doc_DIN.V2G_Message.Body.WeldingDetectionRes;
  10163. res->ResponseCode = dinresponseCodeType_OK;
  10164. //----- [BODY (2/3)] EVSEPresentVoltage -----
  10165. res->EVSEPresentVoltage.Value = 3820;
  10166. res->EVSEPresentVoltage.Multiplier = -1;
  10167. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  10168. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  10169. // dinunitSymbolType_h = 0,
  10170. // dinunitSymbolType_m = 1,
  10171. // dinunitSymbolType_s = 2,
  10172. // dinunitSymbolType_A = 3,
  10173. // dinunitSymbolType_Ah = 4,
  10174. // dinunitSymbolType_V = 5,
  10175. // dinunitSymbolType_VA = 6,
  10176. // dinunitSymbolType_W = 7,
  10177. // dinunitSymbolType_W_s = 8,
  10178. // dinunitSymbolType_Wh = 9
  10179. //----- [BODY (3/3)] DC_EVSEStatus -----
  10180. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  10181. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  10182. // dinisolationLevelType_Invalid = 0,
  10183. // dinisolationLevelType_Valid = 1,
  10184. // dinisolationLevelType_Warning = 2,
  10185. // dinisolationLevelType_Fault = 3
  10186. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  10187. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  10188. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  10189. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  10190. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  10191. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  10192. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  10193. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  10194. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  10195. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  10196. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  10197. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  10198. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  10199. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  10200. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  10201. // dinEVSENotificationType_None = 0,
  10202. // dinEVSENotificationType_StopCharging = 1,
  10203. // dinEVSENotificationType_ReNegotiation = 2
  10204. }
  10205. /*===========================================================================
  10206. FUNCTION: Sudo_Parameter_iso1_WeldingDetectionRes
  10207. DESCRIPTION:
  10208. PRE-CONDITION:
  10209. INPUT:
  10210. OUTPUT:
  10211. GLOBAL VARIABLES:
  10212. =============================================================================*/
  10213. void Sudo_Parameter_iso1_WeldingDetectionRes()
  10214. {
  10215. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  10216. init_iso1WeldingDetectionResType(&ccs_exi_doc_ISO1.V2G_Message.Body.WeldingDetectionRes);
  10217. ccs_exi_doc_ISO1.V2G_Message.Body.WeldingDetectionRes_isUsed = 1u;
  10218. //----- [BODY (1/3)] ResponseCode -----
  10219. struct iso1WeldingDetectionResType *res;
  10220. res = &ccs_exi_doc_ISO1.V2G_Message.Body.WeldingDetectionRes;
  10221. res->ResponseCode = iso1responseCodeType_OK;
  10222. //----- [BODY (2/3)] EVSEPresentVoltage -----
  10223. res->EVSEPresentVoltage.Value = 3820;
  10224. res->EVSEPresentVoltage.Multiplier = -1;
  10225. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  10226. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  10227. //iso1unitSymbolType_h = 0,
  10228. //iso1unitSymbolType_m = 1,
  10229. //iso1unitSymbolType_s = 2,
  10230. //iso1unitSymbolType_A = 3,
  10231. //iso1unitSymbolType_V = 4,
  10232. //iso1unitSymbolType_W = 5,
  10233. //iso1unitSymbolType_Wh = 6
  10234. //----- [BODY (3/3)] DC_EVSEStatus -----
  10235. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  10236. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  10237. //iso1isolationLevelType_Invalid = 0,
  10238. //iso1isolationLevelType_Valid = 1,
  10239. //iso1isolationLevelType_Warning = 2,
  10240. //iso1isolationLevelType_Fault = 3,
  10241. //iso1isolationLevelType_No_IMD = 4
  10242. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  10243. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  10244. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  10245. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  10246. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  10247. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  10248. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  10249. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  10250. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  10251. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  10252. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  10253. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  10254. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  10255. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  10256. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  10257. //iso1EVSENotificationType_None = 0,
  10258. //iso1EVSENotificationType_StopCharging = 1,
  10259. //iso1EVSENotificationType_ReNegotiation = 2
  10260. }
  10261. /*===========================================================================
  10262. FUNCTION: Proc_din_WeldingDetectionRes
  10263. DESCRIPTION:
  10264. PRE-CONDITION:
  10265. INPUT:
  10266. OUTPUT:
  10267. GLOBAL VARIABLES:
  10268. =============================================================================*/
  10269. int Proc_din_WeldingDetectionRes(int AcceptFd)
  10270. {
  10271. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10272. //if it is not the same, the packet should be ignored.
  10273. //STEP 1: ============ Initialize ============
  10274. //int i = 0;
  10275. int errn = 0;
  10276. bitstream_t v2g_tx_stream;
  10277. static struct WeldingDetectionResponse_DIN70121 *wel;
  10278. static struct ChargingInfoData *sys;
  10279. size_t pos = 0;
  10280. v2g_tx_stream.pos = &pos;
  10281. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10282. v2g_tx_stream.data = V2GTP_Tx_buf;
  10283. wel = &ShmCcsData->V2GMessage_DIN70121.WeldingDetectionResponse;
  10284. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  10285. wel->ResponseCode = OK_DIN70121;
  10286. //[HEADER] Check Req SessionID
  10287. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  10288. {
  10289. wel->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  10290. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  10291. errn = -1;
  10292. }
  10293. //Check for SequenceError
  10294. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10295. {
  10296. wel->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  10297. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  10298. errn = -1;
  10299. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10300. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10301. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10302. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10303. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10304. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10305. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10306. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10307. }
  10308. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10309. SAVE_PhysicalValueType_DIN70121(&wel->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  10310. //EVSE Status Code
  10311. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  10312. //Check for CSU command of "Stop by EVSE"
  10313. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  10314. {
  10315. //wel->ResponseCode = FAILED_DIN70121;
  10316. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  10317. }
  10318. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  10319. {
  10320. //wel->ResponseCode = FAILED_DIN70121;
  10321. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10322. }
  10323. else if (ShmInternalComm->ChargingPermission == FALSE)
  10324. {
  10325. //wel->ResponseCode = FAILED_DIN70121;
  10326. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  10327. }
  10328. //Isolation Status
  10329. if (sys->IsolationStatus == GFD_Invalid) //0:invalid (on going)
  10330. {
  10331. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  10332. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10333. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10334. sprintf((char*)buf_log_evcomm, "[Error][Proc_din_WeldingDetectionRes]IsolationStatus = %d", sys->IsolationStatus);
  10335. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10336. Update_V2G_Flow_Status(Other_Fault);
  10337. errn = -1;
  10338. }
  10339. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  10340. {
  10341. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  10342. }
  10343. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  10344. {
  10345. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  10346. }
  10347. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  10348. {
  10349. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  10350. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10351. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10352. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_din_WeldingDetectionRes]GFD_Fault => Emergency Shutdown");
  10353. Update_V2G_Flow_Status(Other_Fault);
  10354. errn = -1;
  10355. }
  10356. else //GFD_No_IMD or other unexpected status
  10357. {
  10358. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  10359. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10360. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10361. sprintf((char*)buf_log_evcomm, "[Error][Proc_din_WeldingDetectionRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  10362. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10363. Update_V2G_Flow_Status(Other_Fault);
  10364. errn = -1;
  10365. }
  10366. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10367. #if PARAMETER_NORMAL_MODE == ENABLE
  10368. SHM_Read_din_WeldingDetectionRes(&ccs_exi_doc_DIN, ShmCcsData);
  10369. #else
  10370. Sudo_Parameter_din_WeldingDetectionRes();
  10371. #endif
  10372. //STEP 4: ============ Encode and Send Response Message ===========
  10373. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  10374. {
  10375. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_WeldingDetectionRes][Error]Tx encoded msg error");
  10376. errn = -1;
  10377. }
  10378. //STEP 5: ============ Update Flags ===========
  10379. return errn;
  10380. }
  10381. /*===========================================================================
  10382. FUNCTION: Proc_iso1_WeldingDetectionRes
  10383. DESCRIPTION:
  10384. PRE-CONDITION:
  10385. INPUT:
  10386. OUTPUT:
  10387. GLOBAL VARIABLES:
  10388. =============================================================================*/
  10389. int Proc_iso1_WeldingDetectionRes(int AcceptFd)
  10390. {
  10391. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10392. //if it is not the same, the packet should be ignored.
  10393. //STEP 1: ============ Initialize ============
  10394. //int i = 0;
  10395. int errn = 0;
  10396. bitstream_t v2g_tx_stream;
  10397. static struct WeldingDetectionResponse_ISO15118_2014 *wel;
  10398. static struct ChargingInfoData *sys;
  10399. size_t pos = 0;
  10400. v2g_tx_stream.pos = &pos;
  10401. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10402. v2g_tx_stream.data = V2GTP_Tx_buf;
  10403. wel = &ShmCcsData->V2GMessage_ISO15118_2014.WeldingDetectionResponse;
  10404. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  10405. wel->ResponseCode = OK_ISO15118_2014;
  10406. //[HEADER] Check Req SessionID
  10407. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  10408. {
  10409. wel->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  10410. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  10411. errn = -1;
  10412. }
  10413. //Check for SequenceError
  10414. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10415. {
  10416. wel->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  10417. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  10418. errn = -1;
  10419. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10420. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10421. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10422. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10423. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10424. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10425. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10426. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10427. }
  10428. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10429. SAVE_PhysicalValueType_ISO15118_2014(&wel->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  10430. //EVSE Status Code
  10431. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  10432. //Check for CSU command of "Stop by EVSE"
  10433. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  10434. {
  10435. //wel->ResponseCode = FAILED_ISO15118_2014;
  10436. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  10437. }
  10438. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  10439. {
  10440. //wel->ResponseCode = FAILED_ISO15118_2014;
  10441. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10442. }
  10443. else if (ShmInternalComm->ChargingPermission == FALSE)
  10444. {
  10445. //wel->ResponseCode = FAILED_ISO15118_2014;
  10446. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  10447. }
  10448. //Isolation Status
  10449. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  10450. {
  10451. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  10452. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10453. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10454. sprintf((char*)buf_log_evcomm, "[Error][Proc_iso1_WeldingDetectionRes]IsolationStatus = %d", sys->IsolationStatus);
  10455. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10456. Update_V2G_Flow_Status(Other_Fault);
  10457. errn = -1;
  10458. }
  10459. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  10460. {
  10461. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  10462. }
  10463. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  10464. {
  10465. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  10466. }
  10467. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  10468. {
  10469. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  10470. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10471. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10472. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error][Proc_iso1_WeldingDetectionRes]GFD_Fault => EmergencyShutdown");
  10473. Update_V2G_Flow_Status(Other_Fault);
  10474. errn = -1;
  10475. }
  10476. else
  10477. {
  10478. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  10479. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  10480. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  10481. sprintf((char*)buf_log_evcomm, "[Error][Proc_iso1_WeldingDetectionRes]IsolationStatus = %d(undefined)", sys->IsolationStatus);
  10482. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10483. Update_V2G_Flow_Status(Other_Fault);
  10484. errn = -1;
  10485. }
  10486. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10487. #if PARAMETER_NORMAL_MODE == ENABLE
  10488. SHM_Read_iso1_WeldingDetectionRes(&ccs_exi_doc_ISO1, ShmCcsData);
  10489. #else
  10490. Sudo_Parameter_iso1_WeldingDetectionRes();
  10491. #endif
  10492. //STEP 4: ============ Encode and Send Response Message ===========
  10493. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  10494. {
  10495. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_WeldingDetectionRes][Error]Tx encoded msg error");
  10496. errn = -1;
  10497. }
  10498. //STEP 5: ============ Update Flags ===========
  10499. return errn;
  10500. }
  10501. /*===========================================================================
  10502. FUNCTION: Proc_din_WeldingDetectionReq
  10503. DESCRIPTION:
  10504. PRE-CONDITION:
  10505. INPUT:
  10506. OUTPUT:
  10507. GLOBAL VARIABLES:
  10508. =============================================================================*/
  10509. int Proc_din_WeldingDetectionReq(int AcceptFd)
  10510. {
  10511. int errn = 0;
  10512. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_din_WeldingDetectionReq] Got WeldingDetectionReq\n");
  10513. //Print the decoded XML Document
  10514. PRINT_XML_DOC_DIN_WeldingDetectionReq(&ccs_exi_doc_DIN);
  10515. //Save into Share Memory
  10516. SHM_Save_din_WeldingDetectionReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  10517. //Check for EV Error Code
  10518. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.WeldingDetectionRequest.DC_EVStatus.EVErrorCode);
  10519. errn = Proc_din_WeldingDetectionRes(AcceptFd);
  10520. if (errn == 0)
  10521. {
  10522. //send response successfully.
  10523. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[DIN][V2G][Tx]WeldingDetectionRes");
  10524. }
  10525. else
  10526. {
  10527. sprintf((char*)buf_log_evcomm,
  10528. "[Error][SeccComm][Proc_din_WeldingDetectionReq]Proc_din_WeldingDetectionRes(): %d (DEC)",
  10529. errn);
  10530. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10531. }
  10532. return errn;
  10533. }
  10534. /*===========================================================================
  10535. FUNCTION: Proc_iso1_WeldingDetectionReq
  10536. DESCRIPTION:
  10537. PRE-CONDITION:
  10538. INPUT:
  10539. OUTPUT:
  10540. GLOBAL VARIABLES:
  10541. =============================================================================*/
  10542. int Proc_iso1_WeldingDetectionReq(int AcceptFd)
  10543. {
  10544. int errn = 0;
  10545. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Proc_iso1_WeldingDetectionReq] Got WeldingDetectionReq\n");
  10546. //Print the decoded XML Document
  10547. PRINT_XML_DOC_ISO1_WeldingDetectionReq(&ccs_exi_doc_ISO1);
  10548. //Save into Share Memory
  10549. SHM_Save_iso1_WeldingDetectionReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  10550. //Check for EV Error Code
  10551. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.WeldingDetectionRequest.DC_EVStatus.EVErrorCode);
  10552. errn = Proc_iso1_WeldingDetectionRes(AcceptFd);
  10553. if (errn == 0)
  10554. {
  10555. //send response successfully.
  10556. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ISO1][V2G][Tx]WeldingDetectionRes");
  10557. }
  10558. else
  10559. {
  10560. sprintf((char*)buf_log_evcomm,
  10561. "[Error][SeccComm][Proc_iso1_WeldingDetectionReq]Proc_iso1_WeldingDetectionRes(): %d (DEC)",
  10562. errn);
  10563. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10564. }
  10565. return errn;
  10566. }
  10567. /*===========================================================================
  10568. FUNCTION: SHM_Init_din_SessionStopRes
  10569. DESCRIPTION:
  10570. PRE-CONDITION:
  10571. INPUT:
  10572. 1. shm_ccs
  10573. OUTPUT:
  10574. 1. shm_ccs
  10575. GLOBAL VARIABLES:
  10576. =============================================================================*/
  10577. void SHM_Init_din_SessionStopRes(struct CcsData *shm_ccs)
  10578. {
  10579. struct SessionStopResponse_DIN70121 *in;
  10580. in = &shm_ccs->V2GMessage_DIN70121.SessionStopResponse;
  10581. //----- [BODY (1/3)] ResponseCode -----
  10582. in->ResponseCode = dinresponseCodeType_OK;
  10583. }
  10584. /*===========================================================================
  10585. FUNCTION: Sudo_Parameter_din_SessionStopRes
  10586. DESCRIPTION:
  10587. PRE-CONDITION:
  10588. INPUT:
  10589. OUTPUT:
  10590. GLOBAL VARIABLES:
  10591. =============================================================================*/
  10592. void Sudo_Parameter_din_SessionStopRes()
  10593. {
  10594. init_dinBodyType(&ccs_exi_doc_DIN.V2G_Message.Body);
  10595. init_dinSessionStopResType(&ccs_exi_doc_DIN.V2G_Message.Body.SessionStopRes);
  10596. ccs_exi_doc_DIN.V2G_Message.Body.SessionStopRes_isUsed = 1u;
  10597. //----- [BODY (1/3)] ResponseCode -----
  10598. struct dinSessionStopResType *res;
  10599. res = &ccs_exi_doc_DIN.V2G_Message.Body.SessionStopRes;
  10600. res->ResponseCode = dinresponseCodeType_OK;
  10601. }
  10602. /*===========================================================================
  10603. FUNCTION: Sudo_Parameter_iso1_SessionStopRes
  10604. DESCRIPTION:
  10605. PRE-CONDITION:
  10606. INPUT:
  10607. OUTPUT:
  10608. GLOBAL VARIABLES:
  10609. =============================================================================*/
  10610. void Sudo_Parameter_iso1_SessionStopRes()
  10611. {
  10612. init_iso1BodyType(&ccs_exi_doc_ISO1.V2G_Message.Body);
  10613. init_iso1SessionStopResType(&ccs_exi_doc_ISO1.V2G_Message.Body.SessionStopRes);
  10614. ccs_exi_doc_ISO1.V2G_Message.Body.SessionStopRes_isUsed = 1u;
  10615. //----- [BODY (1/3)] ResponseCode -----
  10616. struct iso1SessionStopResType *res;
  10617. res = &ccs_exi_doc_ISO1.V2G_Message.Body.SessionStopRes;
  10618. res->ResponseCode = iso1responseCodeType_OK;
  10619. }
  10620. /*===========================================================================
  10621. FUNCTION: Proc_din_SessionStopRes
  10622. DESCRIPTION:
  10623. PRE-CONDITION:
  10624. INPUT:
  10625. OUTPUT:
  10626. GLOBAL VARIABLES:
  10627. =============================================================================*/
  10628. int Proc_din_SessionStopRes(int AcceptFd)
  10629. {
  10630. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10631. //if it is not the same, the packet should be ignored.
  10632. //STEP 1: ============ Initialize ============
  10633. //int i = 0;
  10634. int errn = 0;
  10635. bitstream_t v2g_tx_stream;
  10636. struct SessionStopResponse_DIN70121 *stp;
  10637. size_t pos = 0;
  10638. v2g_tx_stream.pos = &pos;
  10639. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10640. v2g_tx_stream.data = V2GTP_Tx_buf;
  10641. stp = &ShmCcsData->V2GMessage_DIN70121.SessionStopResponse;
  10642. stp->ResponseCode = OK_DIN70121;
  10643. //[HEADER] Check Req SessionID
  10644. if (Check_din_V2G_Rx_MSG_SessionID(&ccs_exi_doc_DIN) < 0)
  10645. {
  10646. stp->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  10647. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  10648. errn = -1;
  10649. }
  10650. //Check for SequenceError
  10651. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10652. {
  10653. stp->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  10654. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  10655. errn = -1;
  10656. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10657. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10658. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10659. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10660. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10661. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10662. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10663. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10664. }
  10665. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10666. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10667. #if PARAMETER_NORMAL_MODE == ENABLE
  10668. SHM_Read_din_SessionStopRes(&ccs_exi_doc_DIN, ShmCcsData);
  10669. #else
  10670. Sudo_Parameter_din_SessionStopRes();
  10671. #endif
  10672. //STEP 4: ============ Encode and Send Response Message ===========
  10673. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_DIN) != 0)
  10674. {
  10675. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Tx]SessionStopRes: fail");
  10676. errn = -1;
  10677. }
  10678. else
  10679. {
  10680. //send response successfully.
  10681. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Tx]SessionStopRes");
  10682. }
  10683. //STEP 5: ============ Update Flags ===========
  10684. EVCOMM_SYS_INFO.ConnectorLocked = FALSE; //Indicating CSU that the Connector is unlocked.
  10685. //Once this is set, the CSU should
  10686. // =========== Annouce to CSU [To-Be Implemented]=============
  10687. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE; //This should be also added to EV Error events.
  10688. // =========== Re-initialized [To-Be Implemented]=============
  10689. //Keep 5% PWM for 2 seconds
  10690. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 2");
  10691. sleep(1);
  10692. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 1");
  10693. sleep(1);
  10694. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 0");
  10695. OutputCpPwmDuty(100);
  10696. //[To-Do] Reset All Share memory
  10697. //[To-Do] CCS Module enter "idle" mode and CSU should check for this state.
  10698. //system("reboot -f");
  10699. //sleep(5);
  10700. //system("reboot -f");
  10701. return errn;
  10702. }
  10703. /*===========================================================================
  10704. FUNCTION: Proc_iso1_SessionStopRes
  10705. DESCRIPTION:
  10706. PRE-CONDITION:
  10707. INPUT:
  10708. OUTPUT:
  10709. GLOBAL VARIABLES:
  10710. =============================================================================*/
  10711. int Proc_iso1_SessionStopRes(int AcceptFd)
  10712. {
  10713. //[Joseph, To-Do] Check SessionID of ServiceDiscoveryReq,
  10714. //if it is not the same, the packet should be ignored.
  10715. //STEP 1: ============ Initialize ============
  10716. //int i = 0;
  10717. int errn = 0;
  10718. bitstream_t v2g_tx_stream;
  10719. struct SessionStopResponse_ISO15118_2014 *stp;
  10720. //struct ChargingInfoData *sys;
  10721. size_t pos = 0;
  10722. v2g_tx_stream.pos = &pos;
  10723. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  10724. v2g_tx_stream.data = V2GTP_Tx_buf;
  10725. stp = &ShmCcsData->V2GMessage_ISO15118_2014.SessionStopResponse;
  10726. //sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  10727. stp->ResponseCode = OK_ISO15118_2014;
  10728. //[HEADER] Check Req SessionID
  10729. if (Check_iso1_V2G_Rx_MSG_SessionID(&ccs_exi_doc_ISO1) < 0)
  10730. {
  10731. stp->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  10732. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]unmatched SessionID => End_Process");
  10733. errn = -1;
  10734. }
  10735. //Check for SequenceError
  10736. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  10737. {
  10738. stp->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  10739. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[ERROR]SequenceError => End_Process");
  10740. errn = -1;
  10741. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10742. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  10743. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10744. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10745. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10746. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  10747. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10748. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  10749. }
  10750. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  10751. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  10752. #if PARAMETER_NORMAL_MODE == ENABLE
  10753. SHM_Read_iso1_SessionStopRes(&ccs_exi_doc_ISO1, ShmCcsData);
  10754. #else
  10755. Sudo_Parameter_iso1_SessionStopRes();
  10756. #endif
  10757. //STEP 4: ============ Encode and Send Response Message ===========
  10758. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &ccs_exi_doc_ISO1) != 0)
  10759. {
  10760. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Tx]SessionStopRes: fail");
  10761. errn = -1;
  10762. }
  10763. else
  10764. {
  10765. //send response successfully.
  10766. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Tx]SessionStopRes");
  10767. }
  10768. //STEP 5: ============ Update Flags ===========
  10769. EVCOMM_SYS_INFO.ConnectorLocked = FALSE; //Indicating CSU that the Connector is unlocked.
  10770. //Once this is set, the CSU should
  10771. // =========== Annouce to CSU [To-Be Implemented]=============
  10772. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE; //This should be also added to EV Error events.
  10773. // =========== Re-initialized [To-Be Implemented]=============
  10774. //Keep 5% PWM for 2 seconds
  10775. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 2");
  10776. sleep(1);
  10777. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 1");
  10778. sleep(1);
  10779. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 5\% countdown: 0");
  10780. OutputCpPwmDuty(100);
  10781. //[To-Do] Reset All Share memory
  10782. //[To-Do] CCS Module enter "idle" mode and CSU should check for this state.
  10783. //system("reboot -f");
  10784. //sleep(5);
  10785. //system("reboot -f");
  10786. return errn;
  10787. }
  10788. /*===========================================================================
  10789. FUNCTION: Proc_din_SessionStopReq
  10790. DESCRIPTION:
  10791. PRE-CONDITION:
  10792. INPUT:
  10793. OUTPUT:
  10794. GLOBAL VARIABLES:
  10795. =============================================================================*/
  10796. int Proc_din_SessionStopReq(int AcceptFd)
  10797. {
  10798. int errn = 0;
  10799. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]SessionStopReq");
  10800. //Print the decoded XML Document
  10801. PRINT_XML_DOC_DIN_SessionStopReq(&ccs_exi_doc_DIN);
  10802. //Save into Share Memory
  10803. SHM_Save_din_SessionStopReq(ShmCcsData, &ccs_exi_doc_DIN, ShmSysConfigAndInfo);
  10804. errn = Proc_din_SessionStopRes(AcceptFd);
  10805. if (errn == 0)
  10806. {
  10807. //send response successfully.
  10808. }
  10809. else
  10810. {
  10811. sprintf((char*)buf_log_evcomm,
  10812. "[Error][SeccComm][Proc_din_SessionStopReq]Proc_iso1_SessionStopRes(): %d (DEC)",
  10813. errn);
  10814. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10815. }
  10816. return errn;
  10817. }
  10818. /*===========================================================================
  10819. FUNCTION: Proc_iso1_SessionStopReq
  10820. DESCRIPTION:
  10821. PRE-CONDITION:
  10822. INPUT:
  10823. OUTPUT:
  10824. GLOBAL VARIABLES:
  10825. =============================================================================*/
  10826. int Proc_iso1_SessionStopReq(int AcceptFd)
  10827. {
  10828. int errn = 0;
  10829. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]SessionStopReq");
  10830. //Print the decoded XML Document
  10831. PRINT_XML_DOC_ISO1_SessionStopReq(&ccs_exi_doc_ISO1);
  10832. //Save into Share Memory
  10833. SHM_Save_iso1_SessionStopReq(ShmCcsData, &ccs_exi_doc_ISO1, ShmSysConfigAndInfo);
  10834. errn = Proc_iso1_SessionStopRes(AcceptFd);
  10835. if (errn == 0)
  10836. {
  10837. //send response successfully.
  10838. }
  10839. else
  10840. {
  10841. sprintf((char*)buf_log_evcomm,
  10842. "[Error][SeccComm][Proc_iso1_SessionStopReq]Proc_iso1_SessionStopRes(): %d (DEC)",
  10843. errn);
  10844. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  10845. }
  10846. return errn;
  10847. }
  10848. /*===========================================================================
  10849. FUNCTION: V2gMsg_Process_din
  10850. DESCRIPTION:
  10851. PRE-CONDITION:
  10852. INPUT:
  10853. 1. V2gFlowStatus
  10854. OUTPUT:
  10855. GLOBAL VARIABLES:
  10856. 1. V2gFlowStatus
  10857. =============================================================================*/
  10858. int V2gMsg_Process_din(int AcceptFd)
  10859. {
  10860. unsigned char req_is_responsed = FALSE;
  10861. while (req_is_responsed == FALSE)
  10862. {
  10863. //Check if it is in End_Process
  10864. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  10865. {
  10866. break;
  10867. }
  10868. switch(V2gFlowStatus)
  10869. {
  10870. //-------------------------------------------
  10871. case SupportedAppProtocolRequest:
  10872. {
  10873. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  10874. {
  10875. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  10876. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  10877. {
  10878. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10879. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  10880. }
  10881. else
  10882. {
  10883. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]supportedAppProtocolResReq: fail");
  10884. Update_V2G_Flow_Status(Other_Fault);
  10885. }
  10886. }
  10887. req_is_responsed = TRUE;
  10888. break;
  10889. }
  10890. case SupportedAppProtocolResponse:
  10891. {
  10892. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  10893. {
  10894. Update_V2G_Flow_Status(SessionSetupRequest);
  10895. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10896. }
  10897. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10898. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10899. {
  10900. sprintf((char*)buf_log_evcomm,
  10901. "[ERROR]SequenceError(%d) => Tx Res MSG",
  10902. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10903. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10904. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10905. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10906. }
  10907. else
  10908. {
  10909. }
  10910. break;
  10911. }
  10912. //-------------------------------------------
  10913. case SessionSetupRequest: //19
  10914. {
  10915. if (Proc_din_SessionSetupReq(AcceptFd) == 0)
  10916. {
  10917. Update_V2G_Flow_Status(SessionSetupResponse);
  10918. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10919. }
  10920. else
  10921. {
  10922. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionSetupReqRes: fail");
  10923. Update_V2G_Flow_Status(Other_Fault);
  10924. }
  10925. req_is_responsed = TRUE;
  10926. break;
  10927. }
  10928. case SessionSetupResponse: //20
  10929. {
  10930. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  10931. {
  10932. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  10933. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10934. }
  10935. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10936. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10937. {
  10938. sprintf((char*)buf_log_evcomm,
  10939. "[ERROR]SequenceError(%d) => Tx Res MSG",
  10940. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10941. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10942. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10943. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10944. }
  10945. else
  10946. {
  10947. }
  10948. break;
  10949. }
  10950. //-------------------------------------------
  10951. case ServiceDiscoveryRequest: //21
  10952. {
  10953. if (Proc_din_ServiceDiscoveryReq(AcceptFd) == 0)
  10954. {
  10955. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  10956. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10957. }
  10958. else
  10959. {
  10960. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceDiscoveryReqRes: fail");
  10961. Update_V2G_Flow_Status(Other_Fault);
  10962. }
  10963. req_is_responsed = TRUE;
  10964. break;
  10965. }
  10966. case ServiceDiscoveryResponse: //22
  10967. {
  10968. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  10969. {
  10970. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  10971. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10972. }
  10973. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10974. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10975. {
  10976. sprintf((char*)buf_log_evcomm, "[ERROR]SequenceError(%d) => Tx Res MSG", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10977. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10978. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10979. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10980. }
  10981. else
  10982. {
  10983. }
  10984. break;
  10985. }
  10986. //-------------------------------------------
  10987. case ServiceAndPaymentSelectionRequest: //25
  10988. {
  10989. if (Proc_din_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  10990. {
  10991. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  10992. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10993. }
  10994. else
  10995. {
  10996. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceAndPaymentSelectionReqRes: fail");
  10997. Update_V2G_Flow_Status(Other_Fault);
  10998. }
  10999. req_is_responsed = TRUE;
  11000. break;
  11001. }
  11002. case ServiceAndPaymentSelectionResponse: //26
  11003. {
  11004. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11005. {
  11006. Update_V2G_Flow_Status(AuthorizationRequest);
  11007. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11008. }
  11009. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11010. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11011. {
  11012. sprintf((char*)buf_log_evcomm,
  11013. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11014. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11015. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11016. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11017. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11018. }
  11019. else
  11020. {
  11021. }
  11022. break;
  11023. }
  11024. //-------------------------------------------
  11025. //case ContractAuthenticationReq:
  11026. case AuthorizationRequest: //29
  11027. {
  11028. if (Proc_din_ContractAuthenticationReq(AcceptFd) == 0)
  11029. {
  11030. Update_V2G_Flow_Status(AuthorizationResponse);
  11031. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11032. }
  11033. else
  11034. {
  11035. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  11036. Update_V2G_Flow_Status(Other_Fault);
  11037. }
  11038. req_is_responsed = TRUE;
  11039. break;
  11040. }
  11041. case AuthorizationResponse: //30
  11042. {
  11043. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11044. {
  11045. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11046. if (Proc_din_ContractAuthenticationReq(AcceptFd) == 0)
  11047. {
  11048. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11049. }
  11050. else
  11051. {
  11052. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  11053. Update_V2G_Flow_Status(Other_Fault);
  11054. }
  11055. req_is_responsed = TRUE;
  11056. }
  11057. //Check for ChargeParameterDiscoveryReq
  11058. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11059. {
  11060. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11061. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  11062. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  11063. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11064. ftime(&SeqStartTime);
  11065. #endif
  11066. }
  11067. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11068. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11069. {
  11070. sprintf((char*)buf_log_evcomm,
  11071. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11072. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11073. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11074. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11075. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11076. }
  11077. else
  11078. {
  11079. }
  11080. break;
  11081. }
  11082. //-------------------------------------------
  11083. case ChargeParameterDiscoveryRequest: //35
  11084. {
  11085. if (Proc_din_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11086. {
  11087. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  11088. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11089. }
  11090. else
  11091. {
  11092. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  11093. Update_V2G_Flow_Status(Other_Fault);
  11094. }
  11095. req_is_responsed = TRUE;
  11096. break;
  11097. }
  11098. case ChargeParameterDiscoveryResponse:
  11099. {
  11100. //STEP 1: Check for Process Timeout
  11101. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11102. ftime(&SeqEndTime);
  11103. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  11104. {
  11105. sprintf((char*)buf_log_evcomm,
  11106. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  11107. DiffTimeb(SeqStartTime, SeqEndTime),
  11108. V2G_SECC_ChargingParameter_Performance_Time);
  11109. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11110. Update_V2G_Flow_Status(Sequence_Timeout);
  11111. break;
  11112. }
  11113. #endif
  11114. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11115. {
  11116. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11117. if (Proc_din_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11118. {
  11119. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11120. }
  11121. else
  11122. {
  11123. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  11124. Update_V2G_Flow_Status(Other_Fault);
  11125. }
  11126. req_is_responsed = TRUE;
  11127. break;
  11128. }
  11129. //STEP 2: Check for CableCheckReq message
  11130. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  11131. {
  11132. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11133. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CableCheckReqReq: isolated?");
  11134. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  11135. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  11136. Update_V2G_Flow_Status(CableCheckRequest);
  11137. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11138. ftime(&SeqStartTime);
  11139. #endif
  11140. }
  11141. //STEP 3: Wait for PowerDeliveryReq Message
  11142. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11143. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11144. {
  11145. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  11146. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11147. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  11148. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11149. ftime(&SeqStartTime);
  11150. #endif
  11151. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  11152. {
  11153. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  11154. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11155. }
  11156. else
  11157. {
  11158. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  11159. Update_V2G_Flow_Status(Other_Fault);
  11160. }
  11161. }
  11162. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11163. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11164. {
  11165. sprintf((char*)buf_log_evcomm,
  11166. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11167. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11168. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11169. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11170. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11171. }
  11172. else
  11173. {
  11174. }
  11175. break;
  11176. }
  11177. //-------------------------------------------
  11178. case CableCheckRequest: //37
  11179. {
  11180. //STEP 3: Execute Cable Check Process
  11181. if (Proc_din_CableCheckReq(AcceptFd) == 0)
  11182. {
  11183. Update_V2G_Flow_Status(CableCheckResponse);
  11184. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11185. }
  11186. else
  11187. {
  11188. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  11189. Update_V2G_Flow_Status(Other_Fault);
  11190. }
  11191. req_is_responsed = TRUE;
  11192. break;
  11193. }
  11194. case CableCheckResponse: //38
  11195. {
  11196. //STEP 1: Check for Process Timeout
  11197. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11198. ftime(&SeqEndTime);
  11199. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  11200. {
  11201. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CableCheck] end counting...");
  11202. sprintf((char*)buf_log_evcomm,
  11203. "CableCheck Timeout - (%.02lf of %d ms)\n",
  11204. DiffTimeb(SeqStartTime, SeqEndTime),
  11205. V2G_SECC_CableCheck_Performance_Time);
  11206. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11207. Update_V2G_Flow_Status(Sequence_Timeout);
  11208. //Update_ShmStatusCode(); //[To-Do] to be implemented
  11209. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  11210. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  11211. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  11212. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  11213. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  11214. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  11215. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  11216. break;
  11217. }
  11218. #endif
  11219. //STEP 2: Check for CableCheckReq message
  11220. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  11221. {
  11222. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11223. //STEP 3: Execute Cable Check Process
  11224. if (Proc_din_CableCheckReq(AcceptFd) == 0)
  11225. {
  11226. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11227. }
  11228. else
  11229. {
  11230. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  11231. Update_V2G_Flow_Status(Other_Fault);
  11232. }
  11233. req_is_responsed = TRUE;
  11234. }
  11235. //STEP 3: Check for PreChargeReq message
  11236. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  11237. {
  11238. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11239. Update_V2G_Flow_Status(PreChargeRequest);
  11240. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PreChargeReqReq: waiting for precharge voltage...");
  11241. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  11242. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  11243. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11244. ftime(&SeqStartTime);
  11245. #endif
  11246. }
  11247. //STEP 3: Wait for PowerDeliveryReq Message
  11248. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11249. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11250. {
  11251. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  11252. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11253. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  11254. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11255. ftime(&SeqStartTime);
  11256. #endif
  11257. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  11258. {
  11259. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  11260. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11261. }
  11262. else
  11263. {
  11264. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  11265. Update_V2G_Flow_Status(Other_Fault);
  11266. }
  11267. }
  11268. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11269. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11270. {
  11271. sprintf((char*)buf_log_evcomm,
  11272. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11273. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11274. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11275. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11276. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11277. }
  11278. else
  11279. {
  11280. }
  11281. break;
  11282. }
  11283. //-------------------------------------------
  11284. case PreChargeRequest: //39
  11285. {
  11286. if (Proc_din_PreChargeReq(AcceptFd) == 0)
  11287. {
  11288. Update_V2G_Flow_Status(PreChargeResponse);
  11289. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11290. }
  11291. else
  11292. {
  11293. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  11294. Update_V2G_Flow_Status(Other_Fault);
  11295. }
  11296. req_is_responsed = TRUE;
  11297. break;
  11298. }
  11299. case PreChargeResponse: //40
  11300. {
  11301. //STEP 1: Check for Process Timeout
  11302. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11303. ftime(&SeqEndTime);
  11304. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  11305. {
  11306. sprintf((char*)buf_log_evcomm,
  11307. "Precharge Timeout - (%.02lf of %d ms)\n",
  11308. DiffTimeb(SeqStartTime, SeqEndTime),
  11309. V2G_SECC_PreCharge_Performance_Time);
  11310. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11311. Update_V2G_Flow_Status(Sequence_Timeout);
  11312. //Update_ShmStatusCode(); //[To-Do] to be implemented
  11313. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  11314. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  11315. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  11316. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  11317. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  11318. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  11319. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  11320. break;
  11321. }
  11322. #endif
  11323. //STEP 2: Check for PreChargeReq message
  11324. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  11325. {
  11326. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11327. if (Proc_din_PreChargeReq(AcceptFd) == 0)
  11328. {
  11329. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11330. }
  11331. else
  11332. {
  11333. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  11334. Update_V2G_Flow_Status(Other_Fault);
  11335. }
  11336. req_is_responsed = TRUE;
  11337. }
  11338. //STEP 3: Check for PowerDeliveryReq message
  11339. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11340. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11341. {
  11342. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11343. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  11344. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  11345. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11346. ftime(&SeqStartTime);
  11347. #endif
  11348. }
  11349. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11350. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11351. {
  11352. sprintf((char*)buf_log_evcomm,
  11353. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11354. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11355. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11356. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11357. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11358. }
  11359. else
  11360. {
  11361. }
  11362. break;
  11363. }
  11364. //-------------------------------------------
  11365. case PowerDeliveryRequestStart: //41
  11366. {
  11367. if (Proc_din_PowerDeliveryStartReq(AcceptFd) == 0)
  11368. {
  11369. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  11370. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11371. }
  11372. else
  11373. {
  11374. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStartReqRes: fail");
  11375. Update_V2G_Flow_Status(Other_Fault);
  11376. }
  11377. req_is_responsed = TRUE;
  11378. break;
  11379. }
  11380. case PowerDeliveryResponsetStart: //42
  11381. {
  11382. //STEP 1: Check for Process Timeout
  11383. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11384. if (ccs_exi_doc_DIN.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  11385. {
  11386. ftime(&SeqEndTime);
  11387. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  11388. {
  11389. sprintf((char*)buf_log_evcomm,
  11390. "Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n",
  11391. DiffTimeb(SeqStartTime, SeqEndTime),
  11392. 2000);
  11393. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11394. Update_V2G_Flow_Status(Sequence_Timeout);
  11395. }
  11396. break;
  11397. }
  11398. #endif
  11399. //STEP 2: Wait for CurrentDemandReq Message
  11400. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  11401. {
  11402. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11403. Update_V2G_Flow_Status(CurrentDemandRequest);
  11404. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CurrentDemandReqReq: energy transfering...");
  11405. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  11406. }
  11407. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11408. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11409. {
  11410. sprintf((char*)buf_log_evcomm,
  11411. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11412. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11413. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11414. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11415. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11416. }
  11417. else
  11418. {
  11419. }
  11420. break;
  11421. }
  11422. //-------------------------------------------
  11423. case CurrentDemandRequest: //45,
  11424. {
  11425. if (Proc_din_CurrentDemandReq(AcceptFd) == 0)
  11426. {
  11427. Update_V2G_Flow_Status(CurrentDemandResponse);
  11428. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11429. }
  11430. else
  11431. {
  11432. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  11433. Update_V2G_Flow_Status(Other_Fault);
  11434. }
  11435. req_is_responsed = TRUE;
  11436. break;
  11437. }
  11438. case CurrentDemandResponse: //46,
  11439. {
  11440. //STEP 1: Wait for CurrentDemandReq Message
  11441. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  11442. {
  11443. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11444. if (Proc_din_CurrentDemandReq(AcceptFd) == 0)
  11445. {
  11446. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11447. }
  11448. else
  11449. {
  11450. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  11451. Update_V2G_Flow_Status(Other_Fault);
  11452. }
  11453. req_is_responsed = TRUE;
  11454. break;
  11455. }
  11456. //STEP 2: Wait for PowerDeliveryReq Message
  11457. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11458. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11459. {
  11460. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  11461. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11462. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  11463. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11464. ftime(&SeqStartTime);
  11465. #endif
  11466. }
  11467. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11468. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11469. {
  11470. sprintf((char*)buf_log_evcomm,
  11471. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11472. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11473. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11474. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11475. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11476. }
  11477. else
  11478. {
  11479. }
  11480. break;
  11481. }
  11482. //-------------------------------------------
  11483. case PowerDeliveryRequestStop: //49,
  11484. {
  11485. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  11486. {
  11487. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  11488. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11489. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11490. ftime(&SeqStartTime);
  11491. #endif
  11492. }
  11493. else
  11494. {
  11495. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  11496. Update_V2G_Flow_Status(Other_Fault);
  11497. }
  11498. req_is_responsed = TRUE;
  11499. break;
  11500. }
  11501. case PowerDeliveryResponseStop: //50,
  11502. {
  11503. //STEP 1: Check for Process Timeout
  11504. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11505. ftime(&SeqEndTime);
  11506. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  11507. {
  11508. sprintf((char*)buf_log_evcomm,
  11509. "Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n",
  11510. DiffTimeb(SeqStartTime, SeqEndTime),
  11511. 2000);
  11512. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11513. Update_V2G_Flow_Status(Sequence_Timeout);
  11514. break;
  11515. }
  11516. #endif
  11517. //STEP 2: Check for WeldingDetectionReq Message
  11518. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  11519. {
  11520. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11521. Update_V2G_Flow_Status(WeldingDetectionRequest);
  11522. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]WeldingDetectionReq: ongoing...");
  11523. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  11524. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11525. ftime(&SeqStartTime);
  11526. #endif
  11527. }
  11528. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  11529. {
  11530. Update_V2G_Flow_Status(SessionStopRequest);
  11531. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11532. if (Proc_din_SessionStopReq(AcceptFd) == 0)
  11533. {
  11534. Update_V2G_Flow_Status(SessionStopResponse);
  11535. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11536. }
  11537. else
  11538. {
  11539. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  11540. Update_V2G_Flow_Status(Other_Fault);
  11541. }
  11542. break;
  11543. }
  11544. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11545. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11546. {
  11547. sprintf((char*)buf_log_evcomm,
  11548. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11549. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11550. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11551. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11552. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11553. }
  11554. else
  11555. {
  11556. }
  11557. break;
  11558. }
  11559. //-------------------------------------------
  11560. case WeldingDetectionRequest: //51,
  11561. {
  11562. if (Proc_din_WeldingDetectionReq(AcceptFd) == 0)
  11563. {
  11564. Update_V2G_Flow_Status(WeldingDetectionResponse);
  11565. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11566. }
  11567. else
  11568. {
  11569. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  11570. Update_V2G_Flow_Status(Other_Fault);
  11571. }
  11572. req_is_responsed = TRUE;
  11573. break;
  11574. }
  11575. case WeldingDetectionResponse: //52,
  11576. {
  11577. //STEP 1: Check for Process Timeout
  11578. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11579. ftime(&SeqEndTime);
  11580. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  11581. {
  11582. sprintf((char*)buf_log_evcomm,
  11583. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  11584. DiffTimeb(SeqStartTime, SeqEndTime),
  11585. V2G_SECC_WeldingDetection_Performance_Time);
  11586. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11587. Update_V2G_Flow_Status(Sequence_Timeout);
  11588. break;
  11589. }
  11590. #endif
  11591. //STEP 2: Check for WeldingDetectionReq Message
  11592. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  11593. {
  11594. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11595. if (Proc_din_WeldingDetectionReq(AcceptFd) == 0)
  11596. {
  11597. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11598. }
  11599. else
  11600. {
  11601. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  11602. Update_V2G_Flow_Status(Other_Fault);
  11603. }
  11604. req_is_responsed = TRUE;
  11605. }
  11606. //STEP 3: Check for SessionStopReq Message
  11607. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  11608. {
  11609. Update_V2G_Flow_Status(SessionStopRequest);
  11610. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11611. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  11612. }
  11613. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11614. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11615. {
  11616. sprintf((char*)buf_log_evcomm,
  11617. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11618. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11619. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11620. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11621. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11622. }
  11623. else
  11624. {
  11625. }
  11626. break;
  11627. }
  11628. //-------------------------------------------
  11629. case SessionStopRequest: //53,
  11630. {
  11631. if (Proc_din_SessionStopReq(AcceptFd) == 0)
  11632. {
  11633. Update_V2G_Flow_Status(SessionStopResponse);
  11634. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11635. }
  11636. else
  11637. {
  11638. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  11639. Update_V2G_Flow_Status(Other_Fault);
  11640. }
  11641. req_is_responsed = TRUE;
  11642. break;
  11643. }
  11644. case SessionStopResponse: //54,
  11645. {
  11646. break;
  11647. }
  11648. //-------------------------------------------
  11649. default:
  11650. {
  11651. break;
  11652. }
  11653. }
  11654. }
  11655. return 0;
  11656. }
  11657. /*===========================================================================
  11658. FUNCTION: V2gMsg_Process_iso1_DC
  11659. DESCRIPTION:
  11660. PRE-CONDITION:
  11661. INPUT:
  11662. 1. V2gFlowStatus
  11663. OUTPUT:
  11664. GLOBAL VARIABLES:
  11665. 1. V2gFlowStatus
  11666. =============================================================================*/
  11667. int V2gMsg_Process_iso1_DC(int AcceptFd)
  11668. {
  11669. unsigned char req_is_responsed = FALSE;
  11670. while (req_is_responsed == FALSE)
  11671. {
  11672. //Check if it is in End_Process
  11673. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  11674. {
  11675. break;
  11676. }
  11677. switch(V2gFlowStatus)
  11678. {
  11679. //-------------------------------------------
  11680. case SupportedAppProtocolRequest:
  11681. {
  11682. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  11683. {
  11684. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  11685. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  11686. {
  11687. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11688. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  11689. }
  11690. else
  11691. {
  11692. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]supportedAppProtocolResReq: fail");
  11693. Update_V2G_Flow_Status(Other_Fault);
  11694. }
  11695. }
  11696. req_is_responsed = TRUE;
  11697. break;
  11698. }
  11699. case SupportedAppProtocolResponse:
  11700. {
  11701. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  11702. {
  11703. Update_V2G_Flow_Status(SessionSetupRequest);
  11704. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11705. }
  11706. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11707. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11708. {
  11709. sprintf((char*)buf_log_evcomm,
  11710. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11711. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11712. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11713. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11714. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11715. }
  11716. else
  11717. {
  11718. }
  11719. break;
  11720. }
  11721. //-------------------------------------------
  11722. case SessionSetupRequest: //19
  11723. {
  11724. if (Proc_iso1_SessionSetupReq(AcceptFd) == 0)
  11725. {
  11726. Update_V2G_Flow_Status(SessionSetupResponse);
  11727. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11728. }
  11729. else
  11730. {
  11731. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionSetupReqRes: fail");
  11732. Update_V2G_Flow_Status(Other_Fault);
  11733. }
  11734. req_is_responsed = TRUE;
  11735. break;
  11736. }
  11737. case SessionSetupResponse: //20
  11738. {
  11739. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  11740. {
  11741. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  11742. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11743. }
  11744. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11745. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11746. {
  11747. sprintf((char*)buf_log_evcomm,
  11748. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11749. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11750. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11751. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11752. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11753. }
  11754. else
  11755. {
  11756. }
  11757. break;
  11758. }
  11759. //-------------------------------------------
  11760. case ServiceDiscoveryRequest: //21
  11761. {
  11762. if (Proc_iso1_ServiceDiscoveryReq(AcceptFd) == 0)
  11763. {
  11764. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  11765. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11766. }
  11767. else
  11768. {
  11769. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceDiscoveryReqRes: fail");
  11770. Update_V2G_Flow_Status(Other_Fault);
  11771. }
  11772. req_is_responsed = TRUE;
  11773. break;
  11774. }
  11775. case ServiceDiscoveryResponse: //22
  11776. {
  11777. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  11778. {
  11779. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  11780. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11781. }
  11782. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11783. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11784. {
  11785. sprintf((char*)buf_log_evcomm,
  11786. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11787. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11788. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11789. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11790. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11791. }
  11792. else
  11793. {
  11794. }
  11795. break;
  11796. }
  11797. //-------------------------------------------
  11798. case ServiceAndPaymentSelectionRequest: //25
  11799. {
  11800. if (Proc_iso1_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  11801. {
  11802. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  11803. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11804. }
  11805. else
  11806. {
  11807. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceAndPaymentSelectionReqRes: fail");
  11808. Update_V2G_Flow_Status(Other_Fault);
  11809. }
  11810. req_is_responsed = TRUE;
  11811. break;
  11812. }
  11813. case ServiceAndPaymentSelectionResponse: //26
  11814. {
  11815. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11816. {
  11817. Update_V2G_Flow_Status(AuthorizationRequest);
  11818. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11819. }
  11820. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11821. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11822. {
  11823. sprintf((char*)buf_log_evcomm,
  11824. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11825. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11826. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11827. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11828. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11829. }
  11830. else
  11831. {
  11832. }
  11833. break;
  11834. }
  11835. //-------------------------------------------
  11836. //case ContractAuthenticationReq:
  11837. case AuthorizationRequest: //29
  11838. {
  11839. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  11840. {
  11841. Update_V2G_Flow_Status(AuthorizationResponse);
  11842. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11843. }
  11844. else
  11845. {
  11846. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  11847. Update_V2G_Flow_Status(Other_Fault);
  11848. }
  11849. req_is_responsed = TRUE;
  11850. break;
  11851. }
  11852. case AuthorizationResponse: //30
  11853. {
  11854. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11855. {
  11856. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11857. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  11858. {
  11859. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11860. }
  11861. else
  11862. {
  11863. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  11864. Update_V2G_Flow_Status(Other_Fault);
  11865. }
  11866. req_is_responsed = TRUE;
  11867. }
  11868. //Check for ChargeParameterDiscoveryReq
  11869. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11870. {
  11871. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11872. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  11873. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  11874. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11875. ftime(&SeqStartTime);
  11876. #endif
  11877. }
  11878. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11879. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11880. {
  11881. sprintf((char*)buf_log_evcomm,
  11882. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11883. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11884. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11885. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11886. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11887. }
  11888. else
  11889. {
  11890. }
  11891. break;
  11892. }
  11893. //-------------------------------------------
  11894. case ChargeParameterDiscoveryRequest: //35
  11895. {
  11896. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11897. {
  11898. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  11899. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11900. }
  11901. else
  11902. {
  11903. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  11904. Update_V2G_Flow_Status(Other_Fault);
  11905. }
  11906. req_is_responsed = TRUE;
  11907. break;
  11908. }
  11909. case ChargeParameterDiscoveryResponse:
  11910. {
  11911. //STEP 1: Check for Process Timeout
  11912. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11913. ftime(&SeqEndTime);
  11914. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  11915. {
  11916. sprintf((char*)buf_log_evcomm,
  11917. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  11918. DiffTimeb(SeqStartTime, SeqEndTime),
  11919. V2G_SECC_ChargingParameter_Performance_Time);
  11920. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  11921. Update_V2G_Flow_Status(Sequence_Timeout);
  11922. break;
  11923. }
  11924. #endif
  11925. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11926. {
  11927. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11928. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11929. {
  11930. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11931. }
  11932. else
  11933. {
  11934. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  11935. Update_V2G_Flow_Status(Other_Fault);
  11936. }
  11937. req_is_responsed = TRUE;
  11938. }
  11939. //STEP 2: Check for CableCheckReq message
  11940. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  11941. {
  11942. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11943. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CableCheckReqReq: isolated?");
  11944. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  11945. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  11946. Update_V2G_Flow_Status(CableCheckRequest);
  11947. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11948. ftime(&SeqStartTime);
  11949. #endif
  11950. }
  11951. //STEP 3: Wait for PowerDeliveryReq Message
  11952. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11953. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11954. {
  11955. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  11956. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11957. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  11958. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11959. ftime(&SeqStartTime);
  11960. #endif
  11961. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  11962. {
  11963. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  11964. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11965. }
  11966. else
  11967. {
  11968. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  11969. Update_V2G_Flow_Status(Other_Fault);
  11970. }
  11971. break;
  11972. }
  11973. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11974. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11975. {
  11976. sprintf((char*)buf_log_evcomm,
  11977. "[ERROR]SequenceError(%d) => Tx Res MSG",
  11978. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11979. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11980. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11981. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11982. }
  11983. else
  11984. {
  11985. }
  11986. break;
  11987. }
  11988. //-------------------------------------------
  11989. case CableCheckRequest: //37
  11990. {
  11991. //STEP 3: Execute Cable Check Process
  11992. if (Proc_iso1_CableCheckReq(AcceptFd) == 0)
  11993. {
  11994. Update_V2G_Flow_Status(CableCheckResponse);
  11995. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11996. }
  11997. else
  11998. {
  11999. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  12000. Update_V2G_Flow_Status(Other_Fault);
  12001. }
  12002. req_is_responsed = TRUE;
  12003. break;
  12004. }
  12005. case CableCheckResponse: //38
  12006. {
  12007. //STEP 1: Check for Process Timeout
  12008. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12009. ftime(&SeqEndTime);
  12010. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  12011. {
  12012. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CableCheck] end counting...");
  12013. sprintf((char*)buf_log_evcomm,
  12014. "CableCheck Timeout - (%.02lf of %d ms)\n",
  12015. DiffTimeb(SeqStartTime, SeqEndTime),
  12016. V2G_SECC_CableCheck_Performance_Time);
  12017. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12018. Update_V2G_Flow_Status(Sequence_Timeout);
  12019. //Update_ShmStatusCode(); //[To-Do] to be implemented
  12020. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  12021. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  12022. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  12023. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  12024. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  12025. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  12026. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  12027. break;
  12028. }
  12029. #endif
  12030. //STEP 2: Check for CableCheckReq message
  12031. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  12032. {
  12033. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12034. //STEP 3: Execute Cable Check Process
  12035. if (Proc_iso1_CableCheckReq(AcceptFd) == 0)
  12036. {
  12037. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12038. }
  12039. else
  12040. {
  12041. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  12042. Update_V2G_Flow_Status(Other_Fault);
  12043. }
  12044. req_is_responsed = TRUE;
  12045. }
  12046. //STEP 3: Check for PreChargeReq message
  12047. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  12048. {
  12049. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12050. Update_V2G_Flow_Status(PreChargeRequest);
  12051. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PreChargeReqReq: waiting for precharge voltage...");
  12052. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  12053. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  12054. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12055. ftime(&SeqStartTime);
  12056. #endif
  12057. }
  12058. //STEP 3: Wait for PowerDeliveryReq Message
  12059. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12060. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12061. {
  12062. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  12063. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12064. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  12065. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12066. ftime(&SeqStartTime);
  12067. #endif
  12068. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12069. {
  12070. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12071. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12072. }
  12073. else
  12074. {
  12075. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  12076. Update_V2G_Flow_Status(Other_Fault);
  12077. }
  12078. }
  12079. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12080. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12081. {
  12082. sprintf((char*)buf_log_evcomm,
  12083. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12084. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12085. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12086. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12087. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12088. }
  12089. else
  12090. {
  12091. }
  12092. break;
  12093. }
  12094. //-------------------------------------------
  12095. case PreChargeRequest: //39
  12096. {
  12097. if (Proc_iso1_PreChargeReq(AcceptFd) == 0)
  12098. {
  12099. Update_V2G_Flow_Status(PreChargeResponse);
  12100. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12101. }
  12102. else
  12103. {
  12104. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  12105. Update_V2G_Flow_Status(Other_Fault);
  12106. }
  12107. req_is_responsed = TRUE;
  12108. break;
  12109. }
  12110. case PreChargeResponse: //40
  12111. {
  12112. //STEP 1: Check for Process Timeout
  12113. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12114. ftime(&SeqEndTime);
  12115. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  12116. {
  12117. sprintf((char*)buf_log_evcomm,
  12118. "Precharge Timeout - (%.02lf of %d ms)\n",
  12119. DiffTimeb(SeqStartTime, SeqEndTime),
  12120. V2G_SECC_PreCharge_Performance_Time);
  12121. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12122. Update_V2G_Flow_Status(Sequence_Timeout);
  12123. //Update_ShmStatusCode(); //[To-Do] to be implemented
  12124. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  12125. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  12126. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  12127. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  12128. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  12129. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  12130. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  12131. break;
  12132. }
  12133. #endif
  12134. //STEP 2: Check for PreChargeReq message
  12135. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  12136. {
  12137. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12138. if (Proc_iso1_PreChargeReq(AcceptFd) == 0)
  12139. {
  12140. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12141. }
  12142. else
  12143. {
  12144. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  12145. Update_V2G_Flow_Status(Other_Fault);
  12146. }
  12147. req_is_responsed = TRUE;
  12148. }
  12149. //STEP 3: Check for PowerDeliveryReq message
  12150. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12151. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12152. {
  12153. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12154. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  12155. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  12156. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12157. ftime(&SeqStartTime);
  12158. #endif
  12159. }
  12160. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12161. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12162. {
  12163. sprintf((char*)buf_log_evcomm,
  12164. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12165. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12166. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12167. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12168. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12169. }
  12170. else
  12171. {
  12172. }
  12173. break;
  12174. }
  12175. //-------------------------------------------
  12176. case PowerDeliveryRequestStart: //41
  12177. {
  12178. if (Proc_iso1_PowerDeliveryStartReq(AcceptFd) == 0)
  12179. {
  12180. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  12181. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12182. }
  12183. else
  12184. {
  12185. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStartReqRes: fail");
  12186. Update_V2G_Flow_Status(Other_Fault);
  12187. }
  12188. req_is_responsed = TRUE;
  12189. break;
  12190. }
  12191. case PowerDeliveryResponsetStart: //42
  12192. {
  12193. //STEP 1: Check for Process Timeout
  12194. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12195. if (ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  12196. {
  12197. ftime(&SeqEndTime);
  12198. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  12199. {
  12200. sprintf((char*)buf_log_evcomm,
  12201. "Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n",
  12202. DiffTimeb(SeqStartTime, SeqEndTime),
  12203. 2000);
  12204. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12205. Update_V2G_Flow_Status(Sequence_Timeout);
  12206. }
  12207. break;
  12208. }
  12209. #endif
  12210. //STEP 2: Wait for CurrentDemandReq Message
  12211. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  12212. {
  12213. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12214. Update_V2G_Flow_Status(CurrentDemandRequest);
  12215. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CurrentDemandReqReq: energy transfering...");
  12216. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  12217. }
  12218. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12219. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12220. {
  12221. sprintf((char*)buf_log_evcomm,
  12222. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12223. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12224. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12225. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12226. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12227. }
  12228. else
  12229. {
  12230. }
  12231. break;
  12232. }
  12233. //-------------------------------------------
  12234. case CurrentDemandRequest: //45,
  12235. {
  12236. if (Proc_iso1_CurrentDemandReq(AcceptFd) == 0)
  12237. {
  12238. Update_V2G_Flow_Status(CurrentDemandResponse);
  12239. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12240. }
  12241. else
  12242. {
  12243. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  12244. Update_V2G_Flow_Status(Other_Fault);
  12245. }
  12246. req_is_responsed = TRUE;
  12247. break;
  12248. }
  12249. case CurrentDemandResponse: //46,
  12250. {
  12251. //STEP 1: Wait for CurrentDemandReq Message
  12252. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  12253. {
  12254. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12255. if (Proc_iso1_CurrentDemandReq(AcceptFd) == 0)
  12256. {
  12257. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12258. }
  12259. else
  12260. {
  12261. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  12262. Update_V2G_Flow_Status(Other_Fault);
  12263. }
  12264. req_is_responsed = TRUE;
  12265. }
  12266. //STEP 2: Wait for PowerDeliveryReq Message
  12267. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12268. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12269. {
  12270. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  12271. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12272. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  12273. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12274. ftime(&SeqStartTime);
  12275. #endif
  12276. }
  12277. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12278. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12279. {
  12280. sprintf((char*)buf_log_evcomm,
  12281. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12282. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12283. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12284. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12285. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12286. }
  12287. else
  12288. {
  12289. }
  12290. break;
  12291. }
  12292. //-------------------------------------------
  12293. case PowerDeliveryRequestStop: //49,
  12294. {
  12295. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12296. {
  12297. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12298. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12299. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12300. ftime(&SeqStartTime);
  12301. #endif
  12302. }
  12303. else
  12304. {
  12305. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  12306. Update_V2G_Flow_Status(Other_Fault);
  12307. }
  12308. req_is_responsed = TRUE;
  12309. break;
  12310. }
  12311. case PowerDeliveryResponseStop: //50,
  12312. {
  12313. //STEP 1: Check for Process Timeout
  12314. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12315. ftime(&SeqEndTime);
  12316. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  12317. {
  12318. sprintf((char*)buf_log_evcomm,
  12319. "Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n",
  12320. DiffTimeb(SeqStartTime, SeqEndTime),
  12321. 2000);
  12322. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12323. Update_V2G_Flow_Status(Sequence_Timeout);
  12324. break;
  12325. }
  12326. #endif
  12327. //STEP 2: Check for WeldingDetectionReq Message
  12328. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  12329. {
  12330. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12331. Update_V2G_Flow_Status(WeldingDetectionRequest);
  12332. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]WeldingDetectionReq: ongoing...");
  12333. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  12334. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12335. ftime(&SeqStartTime);
  12336. #endif
  12337. }
  12338. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  12339. {
  12340. Update_V2G_Flow_Status(SessionStopRequest);
  12341. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12342. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  12343. {
  12344. Update_V2G_Flow_Status(SessionStopResponse);
  12345. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12346. }
  12347. else
  12348. {
  12349. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  12350. Update_V2G_Flow_Status(Other_Fault);
  12351. }
  12352. }
  12353. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12354. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12355. {
  12356. sprintf((char*)buf_log_evcomm,
  12357. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12358. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12359. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12360. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12361. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12362. }
  12363. else
  12364. {
  12365. }
  12366. break;
  12367. }
  12368. //-------------------------------------------
  12369. case WeldingDetectionRequest: //51,
  12370. {
  12371. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  12372. {
  12373. Update_V2G_Flow_Status(WeldingDetectionResponse);
  12374. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12375. }
  12376. else
  12377. {
  12378. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  12379. Update_V2G_Flow_Status(Other_Fault);
  12380. }
  12381. req_is_responsed = TRUE;
  12382. break;
  12383. }
  12384. case WeldingDetectionResponse: //52,
  12385. {
  12386. //STEP 1: Check for Process Timeout
  12387. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12388. ftime(&SeqEndTime);
  12389. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  12390. {
  12391. sprintf((char*)buf_log_evcomm,
  12392. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  12393. DiffTimeb(SeqStartTime, SeqEndTime),
  12394. V2G_SECC_WeldingDetection_Performance_Time);
  12395. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12396. Update_V2G_Flow_Status(Sequence_Timeout);
  12397. break;
  12398. }
  12399. #endif
  12400. //STEP 2: Check for WeldingDetectionReq Message
  12401. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  12402. {
  12403. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12404. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  12405. {
  12406. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12407. }
  12408. else
  12409. {
  12410. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  12411. Update_V2G_Flow_Status(Other_Fault);
  12412. }
  12413. req_is_responsed = TRUE;
  12414. }
  12415. //STEP 3: Check for SessionStopReq Message
  12416. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  12417. {
  12418. Update_V2G_Flow_Status(SessionStopRequest);
  12419. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12420. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  12421. }
  12422. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12423. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12424. {
  12425. sprintf((char*)buf_log_evcomm,
  12426. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12427. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12428. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12429. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12430. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12431. }
  12432. else
  12433. {
  12434. }
  12435. break;
  12436. }
  12437. //-------------------------------------------
  12438. case SessionStopRequest: //53,
  12439. {
  12440. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  12441. {
  12442. Update_V2G_Flow_Status(SessionStopResponse);
  12443. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12444. }
  12445. else
  12446. {
  12447. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  12448. Update_V2G_Flow_Status(Other_Fault);
  12449. }
  12450. req_is_responsed = TRUE;
  12451. break;
  12452. }
  12453. case SessionStopResponse: //54,
  12454. {
  12455. break;
  12456. }
  12457. //-------------------------------------------
  12458. default:
  12459. {
  12460. break;
  12461. }
  12462. }
  12463. }
  12464. return 0;
  12465. }
  12466. /*===========================================================================
  12467. FUNCTION: V2gMsg_Process_iso1_AC
  12468. DESCRIPTION:
  12469. PRE-CONDITION:
  12470. INPUT:
  12471. 1. V2gFlowStatus
  12472. OUTPUT:
  12473. GLOBAL VARIABLES:
  12474. 1. V2gFlowStatus
  12475. =============================================================================*/
  12476. int V2gMsg_Process_iso1_AC(int AcceptFd)
  12477. {
  12478. unsigned char req_is_responsed = FALSE;
  12479. while (req_is_responsed == FALSE)
  12480. {
  12481. //Check if it is in End_Process
  12482. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  12483. {
  12484. break;
  12485. }
  12486. switch(V2gFlowStatus)
  12487. {
  12488. //-------------------------------------------
  12489. case SupportedAppProtocolRequest: //17
  12490. {
  12491. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  12492. {
  12493. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  12494. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  12495. {
  12496. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12497. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  12498. }
  12499. else
  12500. {
  12501. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]supportedAppProtocolResReq: fail");
  12502. Update_V2G_Flow_Status(Other_Fault);
  12503. }
  12504. }
  12505. req_is_responsed = TRUE;
  12506. break;
  12507. }
  12508. case SupportedAppProtocolResponse: //18
  12509. {
  12510. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  12511. {
  12512. Update_V2G_Flow_Status(SessionSetupRequest);
  12513. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12514. }
  12515. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12516. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12517. {
  12518. sprintf((char*)buf_log_evcomm,
  12519. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12520. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12521. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12522. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12523. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12524. }
  12525. else
  12526. {
  12527. }
  12528. break;
  12529. }
  12530. //-------------------------------------------
  12531. case SessionSetupRequest: //19
  12532. {
  12533. if (Proc_iso1_SessionSetupReq(AcceptFd) == 0)
  12534. {
  12535. Update_V2G_Flow_Status(SessionSetupResponse);
  12536. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12537. }
  12538. else
  12539. {
  12540. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionSetupReqRes: fail");
  12541. Update_V2G_Flow_Status(Other_Fault);
  12542. }
  12543. req_is_responsed = TRUE;
  12544. break;
  12545. }
  12546. case SessionSetupResponse: //20
  12547. {
  12548. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  12549. {
  12550. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  12551. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12552. }
  12553. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12554. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12555. {
  12556. sprintf((char*)buf_log_evcomm,
  12557. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12558. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12559. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12560. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12561. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12562. }
  12563. else
  12564. {
  12565. }
  12566. break;
  12567. }
  12568. //-------------------------------------------
  12569. case ServiceDiscoveryRequest: //21
  12570. {
  12571. if (Proc_iso1_ServiceDiscoveryReq(AcceptFd) == 0)
  12572. {
  12573. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  12574. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12575. }
  12576. else
  12577. {
  12578. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceDiscoveryReqRes: fail");
  12579. Update_V2G_Flow_Status(Other_Fault);
  12580. }
  12581. req_is_responsed = TRUE;
  12582. break;
  12583. }
  12584. case ServiceDiscoveryResponse: //22
  12585. {
  12586. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  12587. {
  12588. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  12589. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12590. }
  12591. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12592. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12593. {
  12594. sprintf((char*)buf_log_evcomm,
  12595. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12596. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12597. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12598. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12599. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12600. }
  12601. else
  12602. {
  12603. }
  12604. break;
  12605. }
  12606. //-------------------------------------------
  12607. case ServiceAndPaymentSelectionRequest: //25
  12608. {
  12609. if (Proc_iso1_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  12610. {
  12611. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  12612. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12613. }
  12614. else
  12615. {
  12616. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceAndPaymentSelectionReqRes: fail");
  12617. Update_V2G_Flow_Status(Other_Fault);
  12618. }
  12619. req_is_responsed = TRUE;
  12620. break;
  12621. }
  12622. case ServiceAndPaymentSelectionResponse://26
  12623. {
  12624. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  12625. {
  12626. Update_V2G_Flow_Status(AuthorizationRequest);
  12627. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12628. }
  12629. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12630. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12631. {
  12632. sprintf((char*)buf_log_evcomm,
  12633. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12634. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12635. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12636. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12637. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12638. }
  12639. else
  12640. {
  12641. }
  12642. break;
  12643. }
  12644. //-------------------------------------------
  12645. //case ContractAuthenticationReq:
  12646. case AuthorizationRequest: //29
  12647. {
  12648. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  12649. {
  12650. Update_V2G_Flow_Status(AuthorizationResponse);
  12651. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12652. }
  12653. else
  12654. {
  12655. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  12656. Update_V2G_Flow_Status(Other_Fault);
  12657. }
  12658. break;
  12659. }
  12660. case AuthorizationResponse: //30
  12661. {
  12662. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  12663. {
  12664. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12665. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  12666. {
  12667. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12668. }
  12669. else
  12670. {
  12671. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  12672. Update_V2G_Flow_Status(Other_Fault);
  12673. }
  12674. req_is_responsed = TRUE;
  12675. }
  12676. //Check for ChargeParameterDiscoveryReq
  12677. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  12678. {
  12679. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12680. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  12681. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  12682. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12683. ftime(&SeqStartTime);
  12684. #endif
  12685. }
  12686. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12687. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12688. {
  12689. sprintf((char*)buf_log_evcomm,
  12690. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12691. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12692. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12693. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12694. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12695. }
  12696. else
  12697. {
  12698. }
  12699. break;
  12700. }
  12701. //-------------------------------------------
  12702. case ChargeParameterDiscoveryRequest: //35
  12703. {
  12704. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  12705. {
  12706. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  12707. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12708. }
  12709. else
  12710. {
  12711. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  12712. Update_V2G_Flow_Status(Other_Fault);
  12713. }
  12714. req_is_responsed = TRUE;
  12715. break;
  12716. }
  12717. case ChargeParameterDiscoveryResponse: //36
  12718. {
  12719. //STEP 1: Check for Process Timeout
  12720. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12721. ftime(&SeqEndTime);
  12722. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  12723. {
  12724. sprintf((char*)buf_log_evcomm,
  12725. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  12726. DiffTimeb(SeqStartTime, SeqEndTime),
  12727. V2G_SECC_ChargingParameter_Performance_Time);
  12728. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12729. Update_V2G_Flow_Status(Sequence_Timeout);
  12730. break;
  12731. }
  12732. #endif
  12733. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  12734. {
  12735. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12736. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  12737. {
  12738. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12739. }
  12740. else
  12741. {
  12742. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  12743. Update_V2G_Flow_Status(Other_Fault);
  12744. }
  12745. req_is_responsed = TRUE;
  12746. }
  12747. //STEP 3: Wait for PowerDeliveryReq Message
  12748. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12749. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12750. {
  12751. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  12752. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12753. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  12754. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12755. ftime(&SeqStartTime);
  12756. #endif
  12757. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12758. {
  12759. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12760. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12761. }
  12762. else
  12763. {
  12764. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  12765. Update_V2G_Flow_Status(Other_Fault);
  12766. }
  12767. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12768. ftime(&SeqStartTime);
  12769. #endif
  12770. }
  12771. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12772. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12773. {
  12774. sprintf((char*)buf_log_evcomm,
  12775. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12776. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12777. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12778. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12779. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12780. }
  12781. else
  12782. {
  12783. }
  12784. break;
  12785. }
  12786. //-------------------------------------------
  12787. case PowerDeliveryRequestStart: //41
  12788. {
  12789. if (Proc_iso1_PowerDeliveryStartReq(AcceptFd) == 0)
  12790. {
  12791. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  12792. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12793. }
  12794. else
  12795. {
  12796. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStartReqRes: fail");
  12797. Update_V2G_Flow_Status(Other_Fault);
  12798. }
  12799. req_is_responsed = TRUE;
  12800. break;
  12801. }
  12802. case PowerDeliveryResponsetStart: //42
  12803. {
  12804. //STEP 1: Check for Process Timeout
  12805. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12806. if (ccs_exi_doc_ISO1.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  12807. {
  12808. ftime(&SeqEndTime);
  12809. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  12810. {
  12811. sprintf((char*)buf_log_evcomm,
  12812. "Wait for ChargingStatusReq Timeout - (%.02lf of %d ms)\n",
  12813. DiffTimeb(SeqStartTime, SeqEndTime),
  12814. 2000);
  12815. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12816. Update_V2G_Flow_Status(Sequence_Timeout);
  12817. }
  12818. break;
  12819. }
  12820. #endif
  12821. //STEP 2: Wait for ChargingStatusReq Message
  12822. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargingStatusRequest)
  12823. {
  12824. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12825. Update_V2G_Flow_Status(ChargingStatusRequest);
  12826. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]ChargingStatusReq: energy transfering...");
  12827. ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargingStatus_Timer_Start);
  12828. }
  12829. else
  12830. {
  12831. }
  12832. break;
  12833. }
  12834. //-------------------------------------------
  12835. case ChargingStatusRequest: //43
  12836. {
  12837. if (Proc_iso1_ChargingStatusReq(AcceptFd) == 0)
  12838. {
  12839. Update_V2G_Flow_Status(ChargingStatusResponse);
  12840. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12841. }
  12842. else
  12843. {
  12844. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargingStatusRes: fail");
  12845. Update_V2G_Flow_Status(Other_Fault);
  12846. }
  12847. req_is_responsed = TRUE;
  12848. break;
  12849. }
  12850. case ChargingStatusResponse: //44
  12851. {
  12852. //STEP 1: Wait for ChargingStatusReq Message
  12853. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargingStatusRequest)
  12854. {
  12855. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12856. if (Proc_iso1_ChargingStatusReq(AcceptFd) == 0)
  12857. {
  12858. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12859. }
  12860. else
  12861. {
  12862. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargingStatusRes: fail");
  12863. Update_V2G_Flow_Status(Other_Fault);
  12864. }
  12865. req_is_responsed = TRUE;
  12866. }
  12867. //STEP 2: Wait for PowerDeliveryReq Message
  12868. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  12869. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  12870. {
  12871. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  12872. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12873. ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargingStatus_Timer_End);
  12874. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12875. ftime(&SeqStartTime);
  12876. #endif
  12877. }
  12878. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12879. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12880. {
  12881. sprintf((char*)buf_log_evcomm,
  12882. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12883. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12884. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12885. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12886. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12887. }
  12888. else
  12889. {
  12890. }
  12891. break;
  12892. }
  12893. //-------------------------------------------
  12894. case PowerDeliveryRequestStop: //49
  12895. {
  12896. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  12897. {
  12898. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  12899. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12900. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12901. ftime(&SeqStartTime);
  12902. #endif
  12903. }
  12904. else
  12905. {
  12906. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  12907. Update_V2G_Flow_Status(Other_Fault);
  12908. }
  12909. req_is_responsed = TRUE;
  12910. break;
  12911. }
  12912. case PowerDeliveryResponseStop: //50
  12913. {
  12914. //STEP 1: Check for Process Timeout
  12915. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12916. ftime(&SeqEndTime);
  12917. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  12918. {
  12919. sprintf((char*)buf_log_evcomm,
  12920. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  12921. DiffTimeb(SeqStartTime, SeqEndTime),
  12922. 2000);
  12923. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  12924. Update_V2G_Flow_Status(Sequence_Timeout);
  12925. break;
  12926. }
  12927. #endif
  12928. //STEP 3: Check for SessionStopReq Message
  12929. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  12930. {
  12931. Update_V2G_Flow_Status(SessionStopRequest);
  12932. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12933. }
  12934. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12935. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12936. {
  12937. sprintf((char*)buf_log_evcomm,
  12938. "[ERROR]SequenceError(%d) => Tx Res MSG",
  12939. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12940. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12941. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12942. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12943. }
  12944. else
  12945. {
  12946. }
  12947. break;
  12948. }
  12949. //-------------------------------------------
  12950. case SessionStopRequest: //53
  12951. {
  12952. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  12953. {
  12954. Update_V2G_Flow_Status(SessionStopResponse);
  12955. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12956. }
  12957. else
  12958. {
  12959. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  12960. Update_V2G_Flow_Status(Other_Fault);
  12961. }
  12962. req_is_responsed = TRUE;
  12963. break;
  12964. }
  12965. case SessionStopResponse: //54
  12966. {
  12967. break;
  12968. }
  12969. //-------------------------------------------
  12970. default:
  12971. {
  12972. break;
  12973. }
  12974. }
  12975. }
  12976. return 0;
  12977. }
  12978. /*===========================================================================
  12979. FUNCTION: V2gMsg_Process_iso1
  12980. DESCRIPTION:
  12981. 1. EnergyTransferMode:
  12982. AC_single_phase_core = 0,
  12983. AC_three_phase_core = 1,
  12984. DC_core = 2,
  12985. DC_extended = 3,
  12986. DC_combo_core = 4,
  12987. DC_unique = 5
  12988. iso1EnergyTransferModeType_AC_single_phase_core = 0,
  12989. iso1EnergyTransferModeType_AC_three_phase_core = 1,
  12990. iso1EnergyTransferModeType_DC_core = 2,
  12991. iso1EnergyTransferModeType_DC_extended = 3,
  12992. iso1EnergyTransferModeType_DC_combo_core = 4,
  12993. iso1EnergyTransferModeType_DC_unique = 5
  12994. PRE-CONDITION:
  12995. INPUT:
  12996. 1. V2gFlowStatus
  12997. OUTPUT:
  12998. GLOBAL VARIABLES:
  12999. 1. V2gFlowStatus
  13000. =============================================================================*/
  13001. int V2gMsg_Process_iso1(int AcceptFd, unsigned char EnergyTransferMode)
  13002. {
  13003. switch (EnergyTransferMode)
  13004. {
  13005. case DC_extended:
  13006. {
  13007. V2gMsg_Process_iso1_DC(AcceptFd);
  13008. break;
  13009. }
  13010. case AC_single_phase_core:
  13011. case AC_three_phase_core:
  13012. {
  13013. V2gMsg_Process_iso1_AC(AcceptFd);
  13014. break;
  13015. }
  13016. default:
  13017. {
  13018. sprintf((char*)buf_log_evcomm,
  13019. "[ISO1][Warning]Unexpected EnergyTransferMode(%d)",
  13020. EnergyTransferMode);
  13021. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13022. break;
  13023. }
  13024. }
  13025. return 0;
  13026. }
  13027. /*===========================================================================
  13028. FUNCTION: V2gMsg_Process_iso2_DC
  13029. DESCRIPTION:
  13030. PRE-CONDITION:
  13031. INPUT:
  13032. 1. V2gFlowStatus
  13033. OUTPUT:
  13034. GLOBAL VARIABLES:
  13035. 1. V2gFlowStatus
  13036. =============================================================================*/
  13037. int V2gMsg_Process_iso2_DC(int AcceptFd)
  13038. {
  13039. unsigned char req_is_responsed = FALSE;
  13040. while (req_is_responsed == FALSE)
  13041. {
  13042. //Check if it is in End_Process
  13043. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  13044. {
  13045. break;
  13046. }
  13047. switch(V2gFlowStatus)
  13048. {
  13049. //-------------------------------------------
  13050. case SupportedAppProtocolRequest:
  13051. {
  13052. if (ccs_handshake.supportedAppProtocolReq_isUsed == 1u)
  13053. {
  13054. ccs_handshake.supportedAppProtocolReq_isUsed = 0;
  13055. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  13056. {
  13057. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13058. Update_V2G_Flow_Status(SupportedAppProtocolResponse);
  13059. }
  13060. else
  13061. {
  13062. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]supportedAppProtocolResReq: fail");
  13063. Update_V2G_Flow_Status(Other_Fault);
  13064. }
  13065. }
  13066. req_is_responsed = TRUE;
  13067. break;
  13068. }
  13069. case SupportedAppProtocolResponse:
  13070. {
  13071. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  13072. {
  13073. Update_V2G_Flow_Status(SessionSetupRequest);
  13074. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13075. }
  13076. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13077. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13078. {
  13079. sprintf((char*)buf_log_evcomm,
  13080. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13081. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13082. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13083. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13084. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13085. }
  13086. else
  13087. {
  13088. }
  13089. break;
  13090. }
  13091. //-------------------------------------------
  13092. case SessionSetupRequest: //19
  13093. {
  13094. if (Proc_iso2_SessionSetupReq(AcceptFd) == 0)
  13095. {
  13096. Update_V2G_Flow_Status(SessionSetupResponse);
  13097. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13098. }
  13099. else
  13100. {
  13101. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionSetupReqRes: fail");
  13102. Update_V2G_Flow_Status(Other_Fault);
  13103. }
  13104. req_is_responsed = TRUE;
  13105. break;
  13106. }
  13107. case SessionSetupResponse: //20
  13108. {
  13109. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  13110. {
  13111. Update_V2G_Flow_Status(ServiceDiscoveryRequest);
  13112. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13113. }
  13114. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13115. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13116. {
  13117. sprintf((char*)buf_log_evcomm,
  13118. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13119. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13120. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13121. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13122. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13123. }
  13124. else
  13125. {
  13126. }
  13127. break;
  13128. }
  13129. #if 0
  13130. //-------------------------------------------
  13131. case ServiceDiscoveryRequest: //21
  13132. {
  13133. if (Proc_iso2_ServiceDiscoveryReq(AcceptFd) == 0)
  13134. {
  13135. Update_V2G_Flow_Status(ServiceDiscoveryResponse);
  13136. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13137. }
  13138. else
  13139. {
  13140. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceDiscoveryReqRes: fail");
  13141. Update_V2G_Flow_Status(Other_Fault);
  13142. }
  13143. req_is_responsed = TRUE;
  13144. break;
  13145. }
  13146. case ServiceDiscoveryResponse: //22
  13147. {
  13148. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  13149. {
  13150. Update_V2G_Flow_Status(ServiceAndPaymentSelectionRequest);
  13151. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13152. }
  13153. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13154. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13155. {
  13156. sprintf((char*)buf_log_evcomm,
  13157. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13158. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13159. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13160. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13161. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13162. }
  13163. else
  13164. {
  13165. break;
  13166. }
  13167. }
  13168. //-------------------------------------------
  13169. case ServiceAndPaymentSelectionRequest: //25
  13170. {
  13171. if (Proc_iso2_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  13172. {
  13173. Update_V2G_Flow_Status(ServiceAndPaymentSelectionResponse);
  13174. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13175. }
  13176. else
  13177. {
  13178. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ServiceAndPaymentSelectionReqRes: fail");
  13179. Update_V2G_Flow_Status(Other_Fault);
  13180. }
  13181. req_is_responsed = TRUE;
  13182. break;
  13183. }
  13184. case ServiceAndPaymentSelectionResponse: //26
  13185. {
  13186. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  13187. {
  13188. Update_V2G_Flow_Status(AuthorizationRequest);
  13189. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13190. }
  13191. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13192. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13193. {
  13194. sprintf((char*)buf_log_evcomm,
  13195. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13196. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13197. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13198. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13199. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13200. }
  13201. else
  13202. {
  13203. break;
  13204. }
  13205. }
  13206. //-------------------------------------------
  13207. //case ContractAuthenticationReq:
  13208. case AuthorizationRequest: //29
  13209. {
  13210. if (Proc_iso2_AuthenticationReq(AcceptFd) == 0)
  13211. {
  13212. Update_V2G_Flow_Status(AuthorizationResponse);
  13213. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13214. }
  13215. else
  13216. {
  13217. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  13218. Update_V2G_Flow_Status(Other_Fault);
  13219. }
  13220. req_is_responsed = TRUE;
  13221. break;
  13222. }
  13223. case AuthorizationResponse: //30
  13224. {
  13225. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  13226. {
  13227. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13228. if (Proc_iso2_AuthenticationReq(AcceptFd) == 0)
  13229. {
  13230. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13231. }
  13232. else
  13233. {
  13234. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]AuthorizationReqRes: fail");
  13235. Update_V2G_Flow_Status(Other_Fault);
  13236. }
  13237. req_is_responsed = TRUE;
  13238. break;
  13239. }
  13240. //Check for ChargeParameterDiscoveryReq
  13241. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  13242. {
  13243. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13244. Update_V2G_Flow_Status(ChargeParameterDiscoveryRequest);
  13245. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]ChargeParameterDiscoveryReq: CSU Permission?");
  13246. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13247. ftime(&SeqStartTime);
  13248. #endif
  13249. }
  13250. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13251. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13252. {
  13253. sprintf((char*)buf_log_evcomm,
  13254. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13255. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13256. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13257. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13258. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13259. }
  13260. else
  13261. {
  13262. break;
  13263. }
  13264. }
  13265. //-------------------------------------------
  13266. case ChargeParameterDiscoveryRequest: //35
  13267. {
  13268. if (Proc_iso2_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  13269. {
  13270. Update_V2G_Flow_Status(ChargeParameterDiscoveryResponse);
  13271. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13272. }
  13273. else
  13274. {
  13275. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  13276. Update_V2G_Flow_Status(Other_Fault);
  13277. }
  13278. req_is_responsed = TRUE;
  13279. break;
  13280. }
  13281. case ChargeParameterDiscoveryResponse:
  13282. {
  13283. //STEP 1: Check for Process Timeout
  13284. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13285. ftime(&SeqEndTime);
  13286. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  13287. {
  13288. sprintf((char*)buf_log_evcomm,
  13289. "ChargingParameter Timeout - (%.02lf of %d ms)\n",
  13290. DiffTimeb(SeqStartTime, SeqEndTime),
  13291. V2G_SECC_ChargingParameter_Performance_Time);
  13292. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13293. Update_V2G_Flow_Status(Sequence_Timeout);
  13294. break;
  13295. }
  13296. #endif
  13297. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  13298. {
  13299. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13300. if (Proc_iso2_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  13301. {
  13302. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13303. }
  13304. else
  13305. {
  13306. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]ChargeParameterDiscoveryReqRes: fail");
  13307. Update_V2G_Flow_Status(Other_Fault);
  13308. }
  13309. req_is_responsed = TRUE;
  13310. break;
  13311. }
  13312. //STEP 2: Check for CableCheckReq message
  13313. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  13314. {
  13315. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13316. Update_V2G_Flow_Status(CableCheckRequest);
  13317. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CableCheckReqReq: isolated?");
  13318. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  13319. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  13320. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13321. ftime(&SeqStartTime);
  13322. #endif
  13323. }
  13324. //STEP 3: Wait for PowerDeliveryReq Message
  13325. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13326. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13327. {
  13328. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  13329. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13330. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  13331. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13332. ftime(&SeqStartTime);
  13333. #endif
  13334. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  13335. {
  13336. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  13337. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13338. }
  13339. else
  13340. {
  13341. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  13342. Update_V2G_Flow_Status(Other_Fault);
  13343. }
  13344. break;
  13345. }
  13346. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13347. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13348. {
  13349. sprintf((char*)buf_log_evcomm,
  13350. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13351. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13352. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13353. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13354. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13355. }
  13356. else
  13357. {
  13358. break;
  13359. }
  13360. }
  13361. //-------------------------------------------
  13362. case CableCheckRequest: //37
  13363. {
  13364. //STEP 3: Execute Cable Check Process
  13365. if (Proc_iso2_CableCheckReq(AcceptFd) == 0)
  13366. {
  13367. Update_V2G_Flow_Status(CableCheckResponse);
  13368. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13369. }
  13370. else
  13371. {
  13372. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  13373. Update_V2G_Flow_Status(Other_Fault);
  13374. }
  13375. req_is_responsed = TRUE;
  13376. break;
  13377. }
  13378. case CableCheckResponse: //38
  13379. {
  13380. //STEP 1: Check for Process Timeout
  13381. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13382. ftime(&SeqEndTime);
  13383. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  13384. {
  13385. //SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[CableCheck] end counting...");
  13386. sprintf((char*)buf_log_evcomm,
  13387. "CableCheck Timeout - (%.02lf of %d ms)\n",
  13388. DiffTimeb(SeqStartTime, SeqEndTime),
  13389. V2G_SECC_CableCheck_Performance_Time);
  13390. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13391. Update_V2G_Flow_Status(Sequence_Timeout);
  13392. //Update_ShmStatusCode(); //[To-Do] to be implemented
  13393. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  13394. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  13395. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  13396. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  13397. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  13398. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  13399. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  13400. break;
  13401. }
  13402. #endif
  13403. //STEP 2: Check for CableCheckReq message
  13404. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  13405. {
  13406. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13407. //STEP 3: Execute Cable Check Process
  13408. if (Proc_iso2_CableCheckReq(AcceptFd) == 0)
  13409. {
  13410. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13411. }
  13412. else
  13413. {
  13414. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CableCheckReqRes: fail");
  13415. Update_V2G_Flow_Status(Other_Fault);
  13416. }
  13417. req_is_responsed = TRUE;
  13418. break;
  13419. }
  13420. //STEP 3: Check for PreChargeReq message
  13421. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  13422. {
  13423. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13424. Update_V2G_Flow_Status(PreChargeRequest);
  13425. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]PreChargeReqReq: waiting for precharge voltage...");
  13426. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  13427. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  13428. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13429. ftime(&SeqStartTime);
  13430. #endif
  13431. }
  13432. //STEP 3: Wait for PowerDeliveryReq Message
  13433. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13434. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13435. {
  13436. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  13437. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13438. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  13439. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13440. ftime(&SeqStartTime);
  13441. #endif
  13442. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  13443. {
  13444. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  13445. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13446. }
  13447. else
  13448. {
  13449. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  13450. Update_V2G_Flow_Status(Other_Fault);
  13451. }
  13452. break;
  13453. }
  13454. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13455. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13456. {
  13457. sprintf((char*)buf_log_evcomm,
  13458. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13459. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13460. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13461. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13462. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13463. }
  13464. else
  13465. {
  13466. break;
  13467. }
  13468. }
  13469. //-------------------------------------------
  13470. case PreChargeRequest: //39
  13471. {
  13472. if (Proc_iso2_PreChargeReq(AcceptFd) == 0)
  13473. {
  13474. Update_V2G_Flow_Status(PreChargeResponse);
  13475. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13476. }
  13477. else
  13478. {
  13479. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  13480. Update_V2G_Flow_Status(Other_Fault);
  13481. }
  13482. req_is_responsed = TRUE;
  13483. break;
  13484. }
  13485. case PreChargeResponse: //40
  13486. {
  13487. //STEP 1: Check for Process Timeout
  13488. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13489. ftime(&SeqEndTime);
  13490. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  13491. {
  13492. sprintf((char*)buf_log_evcomm,
  13493. "Precharge Timeout - (%.02lf of %d ms)\n",
  13494. DiffTimeb(SeqStartTime, SeqEndTime),
  13495. V2G_SECC_PreCharge_Performance_Time);
  13496. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13497. Update_V2G_Flow_Status(Sequence_Timeout);
  13498. //Update_ShmStatusCode(); //[To-Do] to be implemented
  13499. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  13500. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  13501. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  13502. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  13503. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  13504. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  13505. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  13506. break;
  13507. }
  13508. #endif
  13509. //STEP 2: Check for PreChargeReq message
  13510. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  13511. {
  13512. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13513. if (Proc_iso2_PreChargeReq(AcceptFd) == 0)
  13514. {
  13515. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13516. }
  13517. else
  13518. {
  13519. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PreChargeReqRes: fail");
  13520. Update_V2G_Flow_Status(Other_Fault);
  13521. }
  13522. req_is_responsed = TRUE;
  13523. break;
  13524. }
  13525. //STEP 3: Check for PowerDeliveryReq message
  13526. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13527. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13528. {
  13529. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13530. Update_V2G_Flow_Status(PowerDeliveryRequestStart);
  13531. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  13532. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13533. ftime(&SeqStartTime);
  13534. #endif
  13535. }
  13536. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13537. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13538. {
  13539. sprintf((char*)buf_log_evcomm,
  13540. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13541. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13542. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13543. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13544. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13545. }
  13546. else
  13547. {
  13548. break;
  13549. }
  13550. }
  13551. //-------------------------------------------
  13552. case PowerDeliveryRequestStart: //41
  13553. {
  13554. if (Proc_iso2_PowerDeliveryStartReq(AcceptFd) == 0)
  13555. {
  13556. Update_V2G_Flow_Status(PowerDeliveryResponsetStart);
  13557. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13558. }
  13559. else
  13560. {
  13561. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStartReqRes: fail");
  13562. Update_V2G_Flow_Status(Other_Fault);
  13563. }
  13564. req_is_responsed = TRUE;
  13565. break;
  13566. }
  13567. case PowerDeliveryResponsetStart: //42
  13568. {
  13569. //STEP 1: Check for Process Timeout
  13570. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13571. if (ccs_exi_doc_ISO2.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  13572. {
  13573. ftime(&SeqEndTime);
  13574. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //5 seconds
  13575. {
  13576. sprintf((char*)buf_log_evcomm,
  13577. "Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n",
  13578. DiffTimeb(SeqStartTime, SeqEndTime),
  13579. 2000);
  13580. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13581. Update_V2G_Flow_Status(Sequence_Timeout);
  13582. }
  13583. break;
  13584. }
  13585. #endif
  13586. //STEP 2: Wait for CurrentDemandReq Message
  13587. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  13588. {
  13589. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13590. Update_V2G_Flow_Status(CurrentDemandRequest);
  13591. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]CurrentDemandReqReq: energy transfering...");
  13592. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  13593. }
  13594. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13595. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13596. {
  13597. sprintf((char*)buf_log_evcomm,
  13598. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13599. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13600. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13601. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13602. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13603. }
  13604. else
  13605. {
  13606. break;
  13607. }
  13608. }
  13609. //-------------------------------------------
  13610. case CurrentDemandRequest: //45,
  13611. {
  13612. if (Proc_iso2_CurrentDemandReq(AcceptFd) == 0)
  13613. {
  13614. Update_V2G_Flow_Status(CurrentDemandResponse);
  13615. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13616. }
  13617. else
  13618. {
  13619. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  13620. Update_V2G_Flow_Status(Other_Fault);
  13621. }
  13622. req_is_responsed = TRUE;
  13623. break;
  13624. }
  13625. case CurrentDemandResponse: //46,
  13626. {
  13627. //STEP 1: Wait for CurrentDemandReq Message
  13628. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  13629. {
  13630. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13631. if (Proc_iso2_CurrentDemandReq(AcceptFd) == 0)
  13632. {
  13633. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13634. }
  13635. else
  13636. {
  13637. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]CurrentDemandReqRes: fail");
  13638. Update_V2G_Flow_Status(Other_Fault);
  13639. }
  13640. req_is_responsed = TRUE;
  13641. break;
  13642. }
  13643. //STEP 2: Wait for PowerDeliveryReq Message
  13644. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  13645. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  13646. {
  13647. Update_V2G_Flow_Status(PowerDeliveryRequestStop);
  13648. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13649. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  13650. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13651. ftime(&SeqStartTime);
  13652. #endif
  13653. }
  13654. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13655. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13656. {
  13657. sprintf((char*)buf_log_evcomm,
  13658. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13659. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13660. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13661. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13662. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13663. }
  13664. else
  13665. {
  13666. break;
  13667. }
  13668. }
  13669. //-------------------------------------------
  13670. case PowerDeliveryRequestStop: //49,
  13671. {
  13672. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  13673. {
  13674. Update_V2G_Flow_Status(PowerDeliveryResponseStop);
  13675. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13676. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13677. ftime(&SeqStartTime);
  13678. #endif
  13679. }
  13680. else
  13681. {
  13682. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]PowerDeliveryStopReqRes: fail");
  13683. Update_V2G_Flow_Status(Other_Fault);
  13684. }
  13685. req_is_responsed = TRUE;
  13686. break;
  13687. }
  13688. case PowerDeliveryResponseStop: //50,
  13689. {
  13690. //STEP 1: Check for Process Timeout
  13691. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13692. ftime(&SeqEndTime);
  13693. if(DiffTimeb(SeqStartTime, SeqEndTime) > 2000) //2 seconds
  13694. {
  13695. sprintf((char*)buf_log_evcomm,
  13696. "Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n",
  13697. DiffTimeb(SeqStartTime, SeqEndTime),
  13698. 2000);
  13699. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13700. Update_V2G_Flow_Status(Sequence_Timeout);
  13701. break;
  13702. }
  13703. #endif
  13704. //STEP 2: Check for WeldingDetectionReq Message
  13705. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  13706. {
  13707. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13708. Update_V2G_Flow_Status(WeldingDetectionRequest);
  13709. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[V2G][Rx]WeldingDetectionReq: ongoing...");
  13710. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  13711. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13712. ftime(&SeqStartTime);
  13713. #endif
  13714. }
  13715. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  13716. {
  13717. Update_V2G_Flow_Status(SessionStopRequest);
  13718. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13719. if (Proc_iso2_SessionStopReq(AcceptFd) == 0)
  13720. {
  13721. Update_V2G_Flow_Status(SessionStopResponse);
  13722. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13723. }
  13724. else
  13725. {
  13726. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  13727. Update_V2G_Flow_Status(Other_Fault);
  13728. }
  13729. break;
  13730. }
  13731. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13732. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13733. {
  13734. sprintf((char*)buf_log_evcomm,
  13735. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13736. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13737. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13738. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13739. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13740. }
  13741. else
  13742. {
  13743. break;
  13744. }
  13745. }
  13746. //-------------------------------------------
  13747. case WeldingDetectionRequest: //51,
  13748. {
  13749. if (Proc_iso2_WeldingDetectionReq(AcceptFd) == 0)
  13750. {
  13751. Update_V2G_Flow_Status(WeldingDetectionResponse);
  13752. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13753. }
  13754. else
  13755. {
  13756. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  13757. Update_V2G_Flow_Status(Other_Fault);
  13758. }
  13759. req_is_responsed = TRUE;
  13760. break;
  13761. }
  13762. case WeldingDetectionResponse: //52,
  13763. {
  13764. //STEP 1: Check for Process Timeout
  13765. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  13766. ftime(&SeqEndTime);
  13767. if(DiffTimeb(SeqStartTime, SeqEndTime) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  13768. {
  13769. sprintf((char*)buf_log_evcomm,
  13770. "Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n",
  13771. DiffTimeb(SeqStartTime, SeqEndTime),
  13772. V2G_SECC_WeldingDetection_Performance_Time);
  13773. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13774. Update_V2G_Flow_Status(Sequence_Timeout);
  13775. break;
  13776. }
  13777. #endif
  13778. //STEP 2: Check for WeldingDetectionReq Message
  13779. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  13780. {
  13781. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13782. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  13783. {
  13784. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13785. }
  13786. else
  13787. {
  13788. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]WeldingDetectionReqRes: fail");
  13789. Update_V2G_Flow_Status(Other_Fault);
  13790. }
  13791. req_is_responsed = TRUE;
  13792. break;
  13793. }
  13794. //STEP 3: Check for SessionStopReq Message
  13795. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  13796. {
  13797. Update_V2G_Flow_Status(SessionStopRequest);
  13798. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13799. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  13800. }
  13801. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  13802. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  13803. {
  13804. sprintf((char*)buf_log_evcomm,
  13805. "[ERROR]SequenceError(%d) => Tx Res MSG",
  13806. EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13807. EVCOMM_SYS_INFO.SequenceError = TRUE;
  13808. Update_V2G_Flow_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  13809. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  13810. }
  13811. else
  13812. {
  13813. break;
  13814. }
  13815. }
  13816. //-------------------------------------------
  13817. case SessionStopRequest: //53,
  13818. {
  13819. if (Proc_iso2_SessionStopReq(AcceptFd) == 0)
  13820. {
  13821. Update_V2G_Flow_Status(SessionStopResponse);
  13822. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  13823. }
  13824. else
  13825. {
  13826. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Error]SessionStopReqRes: fail");
  13827. Update_V2G_Flow_Status(Other_Fault);
  13828. }
  13829. req_is_responsed = TRUE;
  13830. //break;
  13831. }
  13832. case SessionStopResponse: //54,
  13833. {
  13834. break;
  13835. }
  13836. #endif
  13837. //-------------------------------------------
  13838. default:
  13839. {
  13840. break;
  13841. }
  13842. }
  13843. }
  13844. return 0;
  13845. }
  13846. /*===========================================================================
  13847. FUNCTION: V2gMsg_Process_iso2_AC
  13848. DESCRIPTION:
  13849. PRE-CONDITION:
  13850. INPUT:
  13851. 1. V2gFlowStatus
  13852. OUTPUT:
  13853. GLOBAL VARIABLES:
  13854. 1. V2gFlowStatus
  13855. =============================================================================*/
  13856. int V2gMsg_Process_iso2_AC(int AcceptFd)
  13857. {
  13858. return 0;
  13859. }
  13860. /*===========================================================================
  13861. FUNCTION: V2gMsg_Process_iso2
  13862. DESCRIPTION:
  13863. PRE-CONDITION:
  13864. INPUT:
  13865. 1. V2gFlowStatus
  13866. OUTPUT:
  13867. GLOBAL VARIABLES:
  13868. 1. V2gFlowStatus
  13869. =============================================================================*/
  13870. int V2gMsg_Process_iso2(int AcceptFd, unsigned char EnergyTransferMode)
  13871. {
  13872. switch (EnergyTransferMode)
  13873. {
  13874. case DC_extended:
  13875. {
  13876. V2gMsg_Process_iso2_DC(AcceptFd);
  13877. break;
  13878. }
  13879. case AC_single_phase_core:
  13880. case AC_three_phase_core:
  13881. {
  13882. V2gMsg_Process_iso2_AC(AcceptFd);
  13883. break;
  13884. }
  13885. default:
  13886. {
  13887. sprintf((char*)buf_log_evcomm,
  13888. "[ISO2][Warning]Unexpected EnergyTransferMode(%d)",
  13889. EnergyTransferMode);
  13890. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13891. break;
  13892. }
  13893. }
  13894. return 0;
  13895. }
  13896. /*===========================================================================
  13897. FUNCTION: V2gMsg_Process
  13898. DESCRIPTION:
  13899. PRE-CONDITION:
  13900. INPUT:
  13901. OUTPUT:
  13902. GLOBAL VARIABLES:
  13903. =============================================================================*/
  13904. int V2gMsg_Process(int AcceptFd)
  13905. {
  13906. int errn = 0;
  13907. switch (ShmCcsData->CommProtocol)
  13908. {
  13909. case V2GT_MSG_PROTOCOL_DIN70121: //0
  13910. {
  13911. V2gMsg_Process_din(AcceptFd);
  13912. break;
  13913. }
  13914. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  13915. {
  13916. V2gMsg_Process_iso1(AcceptFd, ShmCcsData->EnergyTransferMode);
  13917. break;
  13918. }
  13919. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  13920. {
  13921. V2gMsg_Process_iso2(AcceptFd, ShmCcsData->EnergyTransferMode);
  13922. break;
  13923. }
  13924. default:
  13925. {
  13926. sprintf((char*)buf_log_evcomm,
  13927. "[Warning]Unexpected CommProtocol(%d)",
  13928. ShmCcsData->CommProtocol);
  13929. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13930. break;
  13931. }
  13932. }
  13933. return errn;
  13934. }
  13935. /*===========================================================================
  13936. FUNCTION: V2gMsg_Rx
  13937. DESCRIPTION:
  13938. PRE-CONDITION:
  13939. INPUT:
  13940. OUTPUT:
  13941. GLOBAL VARIABLES:
  13942. =============================================================================*/
  13943. int V2gMsg_Rx(int AcceptFd)
  13944. {
  13945. int errn = 0;
  13946. unsigned int packet_size = 0;
  13947. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  13948. packet_size = recv(AcceptFd, V2gtpMsgRxBuf, V2GTP_MSG_RX_BUFFER_SIZE, 0);
  13949. //[CAUTION] Everytime recv() is called, it will take a default time to wait for TCP packets.
  13950. //The more time you call recv(), the more time you will waste here.
  13951. //Here it is suggested that response immediatedly once you receive any packets.
  13952. //For configuring the Rx waiting time, please use setsockopt().
  13953. if(packet_size > 0) //[Joseph] If there is no data, ignore. (TBD)
  13954. {
  13955. //DEBUG_PRINTF_EVCOMM_DETAIL("\n\n Got TCP V2GTP Message: size = %d Bytes\n", packet_size);
  13956. errn = V2gMsgDecoder(V2gtpMsgRxBuf, packet_size, V2gFlowStatus);
  13957. if (errn < 0)
  13958. {
  13959. sprintf((char*)buf_log_evcomm, "[ERROR]V2gMsgDecoder (%d)", errn);
  13960. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  13961. }
  13962. }
  13963. return errn;
  13964. }
  13965. /*===========================================================================
  13966. FUNCTION: V2gComm
  13967. DESCRIPTION:
  13968. PRE-CONDITION:
  13969. 1. TCP socket is connected.
  13970. INPUT:
  13971. 1. AcceptFd //TCP Connection ID
  13972. 2. V2gtpMsgRxBuf[] //V2GTP Messages (Header + Payload)
  13973. 3. V2GTP_MSG_RX_BUFFER_SIZE //64*1024 = 65,536 = 65.5K (Bytes)
  13974. OUTPUT:
  13975. GLOBAL VARIABLES:
  13976. 1. V2gtpMsgRxBuf[]
  13977. =============================================================================*/
  13978. int V2gComm(int AcceptFd)
  13979. {
  13980. int errn = 0;
  13981. if (V2gMsg_Rx(AcceptFd) < 0)
  13982. {
  13983. Update_V2G_Flow_Status(Other_Fault);
  13984. errn = -1;
  13985. }
  13986. //following are the response message handling according to status flag
  13987. if (V2gMsg_Process(AcceptFd) < 0)
  13988. {
  13989. errn = -1;
  13990. }
  13991. //Error Check
  13992. //V2G_Error_Monitor();
  13993. return errn;
  13994. }
  13995. /*===========================================================================
  13996. FUNCTION: SdpUdpConnected
  13997. DESCRIPTION:
  13998. PRE-CONDITION:
  13999. INPUT:
  14000. OUTPUT:
  14001. GLOBAL VARIABLES:
  14002. =============================================================================*/
  14003. int SdpUdpConnected()
  14004. {
  14005. int packet_size,Rtn;
  14006. struct sockaddr_in6 ServerAddr,ClientAddr;
  14007. struct V2gtpHeader *header;
  14008. unsigned char *payload;
  14009. if(UdpSock <= 0)
  14010. {
  14011. if ((UdpSock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
  14012. {
  14013. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "SdpUdpConnected: Fail to open UdpSock");
  14014. return 0;
  14015. }
  14016. memset(&ServerAddr,0, sizeof(struct sockaddr_in));
  14017. ServerAddr.sin6_family = AF_INET6;
  14018. ServerAddr.sin6_addr = in6addr_any;
  14019. ServerAddr.sin6_port = htons(SdpUdpServerPort);
  14020. if(bind(UdpSock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) <0)
  14021. {
  14022. sprintf((char*)buf_log_evcomm,
  14023. "SdpUdpConnected: Fail to bind UdpSock(%d)",
  14024. UdpSock);
  14025. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14026. close(UdpSock);
  14027. UdpSock = -1;
  14028. return 0;
  14029. }
  14030. sprintf((char*)buf_log_evcomm, "[UdpSock]opend:%d", UdpSock);
  14031. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14032. sprintf((char*)buf_log_evcomm, "[SDP]Req from EVCC: waiting...(%d)", UdpSock);
  14033. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14034. }
  14035. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  14036. memset(&ClientAddr, 0, sizeof(struct sockaddr_in));
  14037. Rtn = sizeof(struct sockaddr_in6);
  14038. packet_size = recvfrom(UdpSock, V2gtpMsgRxBuf, V2GTP_MSG_RX_BUFFER_SIZE, MSG_DONTWAIT, (struct sockaddr *)&ClientAddr, (socklen_t *)&Rtn);
  14039. if(packet_size > 0)
  14040. {
  14041. header = (struct V2gtpHeader *) V2gtpMsgRxBuf;
  14042. payload = V2gtpMsgRxBuf+sizeof(struct V2gtpHeader);
  14043. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  14044. {
  14045. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n Got UDP packet_size = %d (Bytes)\n", packet_size);
  14046. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***********************************\n");
  14047. DEBUG_PRINTF_EVCOMM_DETAIL("***** Received SDP Packet *****\n");
  14048. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  14049. DEBUG_PRINTF_EVCOMM_DETAIL("ClientAddress=");
  14050. for(Rtn = 0; Rtn < 16; Rtn += 2)
  14051. {
  14052. DEBUG_PRINTF_EVCOMM_DETAIL("%02x%02x:",ClientAddr.sin6_addr.s6_addr[Rtn],ClientAddr.sin6_addr.s6_addr[Rtn+1]);
  14053. }
  14054. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14055. DEBUG_PRINTF_EVCOMM_DETAIL("ClientPort=%d\n",ClientAddr.sin6_port);
  14056. DEBUG_PRINTF_EVCOMM_DETAIL("ProtocolVersion=%d\n",header->ProtocolVersion);
  14057. DEBUG_PRINTF_EVCOMM_DETAIL("InverseProtocolVersion=0x%x\n",header->InverseProtocolVersion);
  14058. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadType=0x%x\n",htons(header->PayloadType));
  14059. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadLength=0x%x\n",htonl(header->PayloadLength));
  14060. DEBUG_PRINTF_EVCOMM_DETAIL("htons(header->PayloadType)=0x%x\n",htons(header->PayloadType));
  14061. }
  14062. #endif
  14063. if( (header->ProtocolVersion == 0x01) &&
  14064. (header->InverseProtocolVersion == 0xFE) &&
  14065. (htons(header->PayloadType) == V2GTP_PAYLOAD_TYPE_SDP_REQUEST))
  14066. {
  14067. sprintf((char*)buf_log_evcomm, "[SDP][Rx]Req from EVCC: GOT(%d)", UdpSock);
  14068. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14069. DEBUG_PRINTF_EVCOMM_DETAIL("Security=0x%x\n", *(payload + 0));
  14070. DEBUG_PRINTF_EVCOMM_DETAIL("TransportProtocol=0x%x\n", *(payload + 1));
  14071. header->PayloadType = htons(V2GTP_PAYLOAD_TYPE_SDP_RESPONSE);
  14072. header->PayloadLength = htonl(20); //Fixed Length=20
  14073. memset(payload, 0, 20);
  14074. // MAC address[0:2] + FFFE + MAC address[3:5]
  14075. payload[0] = (IPV6_LINK_LOCAL_PREFIX>>8) & 0xFF;
  14076. payload[1] = IPV6_LINK_LOCAL_PREFIX & 0xFF;
  14077. payload[8 ]= CsuMac[0];
  14078. payload[8] ^= 0x02;// bit 1 should complemented.
  14079. payload[9] = CsuMac[1];
  14080. payload[10] = CsuMac[2];
  14081. payload[11] = 0xFF;
  14082. payload[12] = 0xFE;
  14083. payload[13] = CsuMac[3];
  14084. payload[14] = CsuMac[4];
  14085. payload[15] = CsuMac[5];
  14086. //TCP port
  14087. payload[16] = (EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active>>8) & 0xFF;
  14088. payload[17] = EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active & 0xFF;
  14089. payload[18] = SDP_PAYLOAD_SECURITY_NONE; //Security
  14090. payload[19] = SDP_PAYLOAD_TRANS_PROTOCOL_TCP; //Transport protocol
  14091. //[To-Do] Setup the TCP in advance to receive the coming TCP handshke messages after SDP response.
  14092. Rtn = sendto(UdpSock, V2gtpMsgRxBuf, sizeof(struct V2gtpHeader) + htonl(header->PayloadLength), 0, (struct sockaddr *)&ClientAddr, sizeof(struct sockaddr_in6));
  14093. sprintf((char*)buf_log_evcomm, "[SDP][Tx]Res from SECC");
  14094. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14095. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  14096. {
  14097. DEBUG_PRINTF_EVCOMM_DETAIL("\n\n***** Response SDP Packet *****\n");
  14098. DEBUG_PRINTF_EVCOMM_DETAIL("Send size=%d\n",Rtn);
  14099. DEBUG_PRINTF_EVCOMM_DETAIL("Destination Address=");
  14100. for(Rtn = 0; Rtn < 16; Rtn++)
  14101. {
  14102. DEBUG_PRINTF_EVCOMM_DETAIL("%02x, ", ClientAddr.sin6_addr.s6_addr[Rtn]);
  14103. }
  14104. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14105. DEBUG_PRINTF_EVCOMM_DETAIL("Destination Port=%d\n", ClientAddr.sin6_port);
  14106. DEBUG_PRINTF_EVCOMM_DETAIL("ProtocolVersion=%d\n", header->ProtocolVersion);
  14107. DEBUG_PRINTF_EVCOMM_DETAIL("InverseProtocolVersion=0x%x\n", header->InverseProtocolVersion);
  14108. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadType=0x%x\n", htons(header->PayloadType));
  14109. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadLength=0x%x\n", htonl(header->PayloadLength));
  14110. DEBUG_PRINTF_EVCOMM_DETAIL("SECC Ipv6 Address=");
  14111. for(Rtn = 0; Rtn < 16; Rtn++)
  14112. {
  14113. DEBUG_PRINTF_EVCOMM_DETAIL("%02x:",payload[Rtn]);
  14114. }
  14115. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14116. DEBUG_PRINTF_EVCOMM_DETAIL("SECC Port=%d\n", (payload[16]<<8 | payload[17]));
  14117. DEBUG_PRINTF_EVCOMM_DETAIL("Security=0x%x\n", payload[19]);
  14118. DEBUG_PRINTF_EVCOMM_DETAIL("TransportProtocol=0x%x\n", payload[20]);
  14119. }
  14120. #endif
  14121. if(Rtn > 0)
  14122. {
  14123. return 1;
  14124. }
  14125. }
  14126. }
  14127. return 0;
  14128. }
  14129. /*===========================================================================
  14130. FUNCTION: V2gTcpConnected
  14131. DESCRIPTION:
  14132. PRE-CONDITION:
  14133. INPUT:
  14134. OUTPUT:
  14135. GLOBAL VARIABLES:
  14136. =============================================================================*/
  14137. int V2gTcpConnected()
  14138. {
  14139. int Rtn,AcceptFd;
  14140. struct sockaddr_in6 ServerAddr,ClientAddr;
  14141. if(TcpSock <= 0)
  14142. {
  14143. if ((TcpSock = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
  14144. {
  14145. sprintf((char*)buf_log_evcomm,
  14146. "V2gTcpConnected: Fail to open TcpSock (%s)",
  14147. strerror(errno));
  14148. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14149. usleep(100000); //100ms
  14150. return 0;
  14151. }
  14152. fcntl(TcpSock, F_SETFL, O_NONBLOCK); //set to O_NONBLOCK
  14153. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]socket set: DONE");
  14154. memset(&ServerAddr,0, sizeof(struct sockaddr_in));
  14155. ServerAddr.sin6_family = PF_INET6;
  14156. ServerAddr.sin6_addr = in6addr_any;
  14157. ServerAddr.sin6_port = htons(EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  14158. //ServerAddr.sin6_port = htons(SdpTcpServerPort + (rand() % 10) + 1);
  14159. //printf("SdpTcpServerPort = %d(DEC)\n", htons(ServerAddr.sin6_port));
  14160. if(bind(TcpSock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) < 0)
  14161. {
  14162. sprintf((char*)buf_log_evcomm,
  14163. "V2gTcpConnected: Fail to bind TcpSock (%s), SdpTcpServerPort = %d",
  14164. strerror(errno),
  14165. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active
  14166. );
  14167. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14168. //Change to another TCP port
  14169. /*
  14170. memset(&ServerAddr, 0, sizeof(struct sockaddr_in));
  14171. ServerAddr.sin6_family = PF_INET6;
  14172. ServerAddr.sin6_addr = in6addr_any;
  14173. //ServerAddr.sin6_port = htons(SdpTcpServerPort + (rand() % 1000) + 1);
  14174. ServerAddr.sin6_port = htons(EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  14175. */
  14176. if(bind(TcpSock, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) < 0)
  14177. {
  14178. sprintf((char*)buf_log_evcomm,
  14179. "V2gTcpConnected: Fail to bind TcpSock (%s), SdpTcpServerPort = %d",
  14180. strerror(errno),
  14181. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active
  14182. );
  14183. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14184. usleep(100000); //100ms
  14185. close(TcpSock);
  14186. TcpSock = -1;
  14187. return 0;
  14188. }
  14189. }
  14190. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]blid: DONE");
  14191. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]listen: START");
  14192. if(listen(TcpSock, 1) == -1) //only accept one connection
  14193. {
  14194. sprintf((char*)buf_log_evcomm,
  14195. "V2gTcpConnected: Fail to listen TcpSock (%s)",
  14196. strerror(errno));
  14197. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14198. usleep(100000); //100ms
  14199. close(TcpSock);
  14200. TcpSock = -1;
  14201. return 0;
  14202. }
  14203. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]listen: DONE");
  14204. sprintf((char*)buf_log_evcomm, "[TCP]TcpSock: opened(%d), port=%d", TcpSock, htons(ServerAddr.sin6_port));
  14205. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14206. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]accept: START");
  14207. }
  14208. Rtn = sizeof(struct sockaddr_in6);
  14209. if((AcceptFd = accept(TcpSock, (struct sockaddr *)&ClientAddr, (socklen_t *)&Rtn)) == -1)
  14210. {
  14211. static BOOL tmp = 0;
  14212. if (tmp == 0)
  14213. {
  14214. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]Wait TCP connection...");
  14215. tmp = 1;
  14216. }
  14217. else
  14218. {
  14219. //DEBUG_PRINTF_EVCOMM_DETAIL(".");
  14220. }
  14221. return 0;
  14222. }
  14223. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[TCP]accept: DONE\n");
  14224. #if (DEBUG_PRINTF_EVCOMM_DETAIL_SHOW == ENABLE)
  14225. {
  14226. DEBUG_PRINTF_EVCOMM_DETAIL("\n\nAccept one TCP connection:\n");
  14227. DEBUG_PRINTF_EVCOMM_DETAIL("AcceptFd=%d\n",AcceptFd);
  14228. DEBUG_PRINTF_EVCOMM_DETAIL("ClientAddress=");
  14229. for(Rtn = 0; Rtn < 16; Rtn += 2)
  14230. {
  14231. DEBUG_PRINTF_EVCOMM_DETAIL("%02x%02x:", ClientAddr.sin6_addr.s6_addr[Rtn], ClientAddr.sin6_addr.s6_addr[Rtn+1]);
  14232. }
  14233. DEBUG_PRINTF_EVCOMM_DETAIL("\n");
  14234. DEBUG_PRINTF_EVCOMM_DETAIL("ClientPort=%d\n", ClientAddr.sin6_port);
  14235. }
  14236. #endif
  14237. return AcceptFd;
  14238. }
  14239. /*===========================================================================
  14240. FUNCTION: End_Process
  14241. DESCRIPTION:
  14242. PRE-CONDITION:
  14243. 1. <CAUTION> This function could only be used in SeccComm fork2.
  14244. INPUT:
  14245. OUTPUT:
  14246. GLOBAL VARIABLES:
  14247. =============================================================================*/
  14248. int End_Process()
  14249. {
  14250. if(EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  14251. {
  14252. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "End_Process has been triggered by another event.");
  14253. return -1;
  14254. }
  14255. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Entering...");
  14256. //STEP 1: Ask CSU to Stop
  14257. EVCOMM_SYS_INFO.End_Process_inused = TRUE;
  14258. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  14259. EVCOMM_SYS_INFO.ConnectorLocked = FALSE;
  14260. ShmInternalComm->ChargingPermission = FALSE;
  14261. //Step 2: Close sockets
  14262. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Close sockets...");
  14263. if(RawSock > 0)
  14264. {
  14265. close(RawSock);
  14266. }
  14267. if(UdpSock > 0)
  14268. {
  14269. close(UdpSock);
  14270. }
  14271. if(TcpSock > 0)
  14272. {
  14273. close(TcpSock);
  14274. close(TcpAcceptFd);
  14275. }
  14276. RawSock = UdpSock = TcpSock = TcpAcceptFd = -1;
  14277. //STEP 3: Switch to State E
  14278. //SwitchCpStateE(ENABLE);
  14279. //STEP 4: Close tcpdump
  14280. Sniffer_Tcpdump(DISABLE);
  14281. //STEP 5: Keep 100% PWM for 5 seconds
  14282. OutputCpPwmDuty(100);
  14283. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 5");
  14284. sleep(1);
  14285. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 4");
  14286. sleep(1);
  14287. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 3");
  14288. sleep(1);
  14289. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 2");
  14290. sleep(1);
  14291. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 1");
  14292. sleep(1);
  14293. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "PWM 100\% countdown: 0");
  14294. AttenProfileCnt = 0;
  14295. init_appHandEXIDocument(&ccs_handshake);
  14296. //Qca7kPowerReset(); //reset QCA7000
  14297. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
  14298. {
  14299. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[End_Process]1st sync...");
  14300. system("sync");
  14301. }
  14302. #endif
  14303. //STEP 4: Switch to State E
  14304. //Keep State E for 5 seconds
  14305. #if 0
  14306. SwitchCpStateE(ENABLE); //Set PWM Duty as 0 and set State as E (0V)
  14307. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 5");
  14308. sleep(1);
  14309. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 4");
  14310. sleep(1);
  14311. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 3");
  14312. sleep(1);
  14313. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 2");
  14314. sleep(1);
  14315. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 1");
  14316. sleep(1);
  14317. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "State E countdown: 0");
  14318. #endif
  14319. //Reset Memory
  14320. unsigned char SlaveAddress_backup;
  14321. unsigned int matched_backup;
  14322. //unsigned char state_backup;
  14323. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "Soft reset (closing SeccComm)...");
  14324. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "---------------------------------------------");
  14325. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "-- EVCOMM: END --");
  14326. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "---------------------------------------------\n");
  14327. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
  14328. {
  14329. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "2nd sync...");
  14330. system("sync");
  14331. }
  14332. #endif
  14333. system("sleep 1");
  14334. //Backup CsuComm flags
  14335. SlaveAddress_backup = ShmInternalComm->SlaveAddress;
  14336. matched_backup = CSUCOMMDC_TASK_FLAG.matched;
  14337. //state_backup = Check_V2G_Flow_Status();
  14338. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  14339. memset(V2gtpMsgTxBuf, 0, V2GTP_MSG_TX_BUFFER_SIZE);
  14340. memset(&ccs_handshake, 0, sizeof(struct appHandEXIDocument));
  14341. memset(&ccs_exi_doc_DIN, 0, sizeof(struct dinEXIDocument));
  14342. memset(V2GTP_Tx_buf, 0, V2GTP_MSG_TX_BUFFER_SIZE);
  14343. memset(buf_log_evcomm, 0, sizeof(buf_log_evcomm));
  14344. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  14345. memset(ShmInternalComm, 0, sizeof(struct InternalComm));
  14346. memset(ShmCcsData, 0, sizeof(struct CcsData));
  14347. //Resume CsuComm flags
  14348. ShmInternalComm->SlaveAddress = SlaveAddress_backup;
  14349. CSUCOMMDC_TASK_FLAG.matched = matched_backup;
  14350. Sniffer_Candump(DISABLE);
  14351. Sniffer_Candump(ENABLE);
  14352. //memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo)); //EVCOMM_SYS_INFO.End_Process_inused = FALSE;
  14353. //EVCOMM_SYS_INFO.End_Process_inused = TRUE;
  14354. //Update_V2G_Flow_Status(state_backup);
  14355. //CP_Detection_Pid = 0;
  14356. //PP_Detection_Pid = 0;
  14357. //Error_Monitor_Pid = 0;
  14358. //system("cd /root;./reset_soft.sh");
  14359. system("pkill Module_CCS");
  14360. while(1)
  14361. {
  14362. //wait for CSU configrm
  14363. }
  14364. }
  14365. /*===========================================================================
  14366. FUNCTION: Parameters_Init
  14367. DESCRIPTION:
  14368. PRE-CONDITION:
  14369. INPUT:
  14370. OUTPUT:
  14371. GLOBAL VARIABLES:
  14372. =============================================================================*/
  14373. int Parameters_Init()
  14374. {
  14375. //Step 0: Generate random number
  14376. unsigned int value_random;
  14377. struct timeb time_seed;
  14378. ftime(&time_seed);
  14379. srand(time_seed.millitm);
  14380. //Step 1: Init SDP TCP Port
  14381. value_random = rand();
  14382. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active = (unsigned short)(SdpTcpServerPort + (value_random % 10000) + 1);
  14383. sprintf((char*)buf_log_evcomm,
  14384. "[Init]TCP Port:OK(%d)",
  14385. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  14386. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14387. //Step 2: Init SessionID
  14388. value_random = rand();
  14389. memcpy(&EVCOMM_SYS_INFO.SessionID[0], &value_random, 4);
  14390. value_random = rand();
  14391. memcpy(&EVCOMM_SYS_INFO.SessionID[4], &value_random, 4);
  14392. sprintf((char*)buf_log_evcomm,
  14393. "[Init]SessionID:OK(%02X%02X%02X%02X%02X%02X%02X%02X)",
  14394. EVCOMM_SYS_INFO.SessionID[0],
  14395. EVCOMM_SYS_INFO.SessionID[1],
  14396. EVCOMM_SYS_INFO.SessionID[2],
  14397. EVCOMM_SYS_INFO.SessionID[3],
  14398. EVCOMM_SYS_INFO.SessionID[4],
  14399. EVCOMM_SYS_INFO.SessionID[5],
  14400. EVCOMM_SYS_INFO.SessionID[6],
  14401. EVCOMM_SYS_INFO.SessionID[7]);
  14402. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14403. return 0;
  14404. }
  14405. /*===========================================================================
  14406. FUNCTION: main
  14407. DESCRIPTION:
  14408. PRE-CONDITION:
  14409. INPUT:
  14410. OUTPUT:
  14411. GLOBAL VARIABLES:
  14412. =============================================================================*/
  14413. int main(int argc, char *argv[])
  14414. {
  14415. //unsigned char Rtn;
  14416. //Initialization
  14417. ShareMemory_Init();
  14418. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  14419. if(RawSock > 0)
  14420. {
  14421. close(RawSock);
  14422. }
  14423. if(UdpSock > 0)
  14424. {
  14425. close(UdpSock);
  14426. }
  14427. if(TcpSock > 0)
  14428. {
  14429. close(TcpSock);
  14430. }
  14431. RawSock = UdpSock = TcpSock = -1;
  14432. Update_V2G_Flow_Status(IDLE);
  14433. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "---------------------------------------------");
  14434. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "-- EVCOMM: START --");
  14435. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "---------------------------------------------");
  14436. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "init...");
  14437. //Init V2G TCP/IPv6 packets buffer
  14438. V2gtpMsgRxBuf = (unsigned char *)malloc(V2GTP_MSG_RX_BUFFER_SIZE);
  14439. memset(V2gtpMsgRxBuf, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  14440. V2gtpMsgTxBuf = (unsigned char *)malloc(V2GTP_MSG_TX_BUFFER_SIZE);
  14441. memset(V2gtpMsgTxBuf, 0, V2GTP_MSG_TX_BUFFER_SIZE);
  14442. //Release State E Control
  14443. SwitchCpStateE(DISABLE);
  14444. OutputCpPwmDuty(100);
  14445. //start to detect CP pilot state
  14446. CP_Detection_Pid = 0;
  14447. CP_Detection(); //fork1
  14448. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork1]CP Detection: ON");
  14449. //start to detect errors
  14450. Error_Monitor(); //fork2
  14451. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork2]Error Monitor: ON");
  14452. //start to detect PP
  14453. PP_Detection_Pid = 0;
  14454. #if (PP_PROTECTION_MECHANISM == ENABLE)
  14455. PP_Detection();
  14456. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork3]PP Detection: ON");
  14457. #else
  14458. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[fork3]PP Detection: OFF");
  14459. #endif
  14460. //Init communication parameters
  14461. GetEthMac((uint8_t*)QcaInterface, CsuMac);
  14462. AttenProfileCnt = 0;
  14463. init_appHandEXIDocument(&ccs_handshake);
  14464. //Init Energy transfer mode
  14465. //[To-Do] Parsing Model Name
  14466. ShmCcsData->EnergyTransferMode = CCS_ENERGY_TRANSFER_MODE;
  14467. //struct ChargingInfoData *ccs;
  14468. //ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  14469. Parameters_Init();
  14470. Sniffer_Tcpdump(ENABLE);
  14471. #if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  14472. sleep(1); //wait for tcpdump getting ready
  14473. #endif
  14474. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", "[Init]DONE");
  14475. while(1)
  14476. {
  14477. //DEBUG_PRINTF_EVCOMM_DETAIL("\nV2gFlowStatus = %d\n", V2gFlowStatus);
  14478. //if((ShmInternalComm->ChargingPermission == 0x01) && (CheckConnectorPlugIn() == 1))
  14479. //if((CheckConnectorPlugIn() == TRUE) && (ShmInternalComm->ChargingPermission == TRUE))
  14480. //if(ShmInternalComm->ChargingPermission == 0x01)
  14481. //if(CheckConnectorPlugIn() == TRUE)
  14482. if (EVCOMM_SYS_INFO.End_Process_inused == FALSE)
  14483. {
  14484. if(V2gFlowStatus < SLACC_SDP_UDP_Connection)
  14485. {
  14486. SlacComm();
  14487. }
  14488. else if(V2gFlowStatus == SLACC_SDP_UDP_Connection)
  14489. {
  14490. if(SdpUdpConnected() == 1)
  14491. {
  14492. Update_V2G_Flow_Status(SLACC_SDP_TCP_Connection);
  14493. continue;
  14494. }
  14495. SlacComm(); //TC_SECC_VTB_CmSlacMatch_004
  14496. ftime(&SeqEndTime);
  14497. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_join)
  14498. {
  14499. sprintf((char*)buf_log_evcomm,
  14500. "Wait SLACC_SDP_UDP_Connection Timeout - TT_match_join (%.02lf of %d ms)\n",
  14501. DiffTimeb(SeqStartTime, SeqEndTime),
  14502. TT_match_join);
  14503. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14504. Update_V2G_Flow_Status(Sequence_Timeout);
  14505. //Update_ShmStatusCode(); //[To-Do] to be implemented
  14506. //CCS_SECC_TIMEOUT_SLACC_SDP_UDP_TT_match_join (023823)
  14507. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  14508. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  14509. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  14510. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  14511. ShmStatusCodeData->PresentStatusCode[0][4] = 2;
  14512. ShmStatusCodeData->PresentStatusCode[0][5] = 3;
  14513. //CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  14514. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  14515. }
  14516. }
  14517. else if(V2gFlowStatus == SLACC_SDP_TCP_Connection)
  14518. {
  14519. if((TcpAcceptFd = V2gTcpConnected()) > 0)
  14520. {
  14521. Update_V2G_Flow_Status(SupportedAppProtocolRequest);
  14522. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  14523. continue;
  14524. }
  14525. SlacComm();
  14526. ftime(&SeqEndTime);
  14527. if(DiffTimeb(SeqStartTime, SeqEndTime) > TT_match_join)
  14528. {
  14529. sprintf((char*)buf_log_evcomm,
  14530. "Wait SLACC_SDP_TCP_Connection Timeout - TT_match_join (%.02lf / %d ms)",
  14531. DiffTimeb(SeqStartTime, SeqEndTime),
  14532. TT_match_join);
  14533. SAVE_SYS_LOG_MSG_EVCOMM("%s\n", buf_log_evcomm);
  14534. Update_V2G_Flow_Status(Sequence_Timeout);
  14535. //Update_ShmStatusCode(); //[To-Do] to be implemented
  14536. //CCS_SECC_TIMEOUT_SLACC_SDP_TCP_TT_match_join (023824)
  14537. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  14538. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  14539. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  14540. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  14541. ShmStatusCodeData->PresentStatusCode[0][4] = 2;
  14542. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  14543. //CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  14544. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  14545. }
  14546. }
  14547. else if(V2gFlowStatus <= SessionStopResponse)
  14548. {
  14549. if (V2gComm(TcpAcceptFd) < 0)
  14550. {
  14551. //error occours
  14552. }
  14553. }
  14554. else if (V2gFlowStatus >= Performance_Timeout)
  14555. {
  14556. //End_Process
  14557. }
  14558. else
  14559. {
  14560. //null
  14561. }
  14562. }
  14563. } //while
  14564. }//main while