Module_CCS.c 537 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537
  1. /*
  2. * Module_CCS.c
  3. *
  4. * Created on: 2020/03/16
  5. * Author: foluswen
  6. */
  7. #include "Module_CCS.h"
  8. #include "define_ccs.h"
  9. #include "main.h"
  10. enum MsgFlowStatus V2gFlowStatus;
  11. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  12. struct StatusCodeData *ShmStatusCodeData;
  13. struct CcsData *ShmCcsData;
  14. struct InternalComm *ShmInternalComm;
  15. struct DISPENSER *ShmDispenser;
  16. struct V2G_OBJECT
  17. {
  18. struct appHandEXIDocument appHandshake;
  19. struct dinEXIDocument DIN;
  20. struct iso1EXIDocument ISO1;
  21. struct iso2EXIDocument ISO2;
  22. }v2gObject;
  23. struct V2G_BUFFER
  24. {
  25. unsigned char rx[V2GTP_MSG_RX_BUFFER_SIZE];
  26. unsigned char tx[V2GTP_MSG_TX_BUFFER_SIZE];
  27. }v2gBuffer;
  28. struct SOCKET_HANDLE
  29. {
  30. int Raw;
  31. int Udp;
  32. int Tcp;
  33. int TcpAccept;
  34. }socketFd;
  35. struct PID_T
  36. {
  37. pid_t CP_Detection;
  38. pid_t PP_Detection;
  39. pid_t Error_Monitor;
  40. }pid;
  41. struct MAC
  42. {
  43. unsigned char eth0[6];
  44. unsigned char eth1[6];
  45. unsigned char qca[6];
  46. unsigned char evcc[6];
  47. }macAddr;
  48. struct QCA_INFO
  49. {
  50. struct MmeHeader SendMmePacket;
  51. struct sockaddr_ll DestSocketAddress;
  52. int SendMmePacketSize;
  53. unsigned char SlacRunId[8];
  54. struct ifreq Req;
  55. unsigned char AagGroupsNum;
  56. unsigned char MnbcSoundNum;
  57. unsigned char AttenProfileCnt;
  58. unsigned char NewNmkKey[16];
  59. unsigned char Nid[7];
  60. }qcaInfo;
  61. struct TIMER
  62. {
  63. unsigned int PwmStart;
  64. struct timeb SeqStart;
  65. struct timeb SeqEnd;
  66. }timerStart;
  67. /**
  68. *
  69. * @param statusCode
  70. * @param statusName
  71. * @return
  72. */
  73. uint8_t get_V2G_Status_Name(uint8_t statusCode, char *statusName)
  74. {
  75. int result = PASS;
  76. switch(statusCode)
  77. {
  78. case IDLE:
  79. sprintf(statusName, "IDLE");
  80. break;
  81. case CM_SLAC_PARM_REQ:
  82. sprintf(statusName, "CM_SLAC_PARM_REQ");
  83. break;
  84. case CM_SLAC_PARM_CONF:
  85. sprintf(statusName, "CM_SLAC_PARM_CONF");
  86. break;
  87. case CM_START_ATTEN_CHAR_IND:
  88. sprintf(statusName, "CM_START_ATTEN_CHAR_IND");
  89. break;
  90. case CM_MNBC_SOUND_IND:
  91. sprintf(statusName, "CM_MNBC_SOUND_IND");
  92. break;
  93. case CM_ATTEN_CHAR_IND:
  94. sprintf(statusName, "CM_ATTEN_CHAR_IND");
  95. break;
  96. case CM_ATTEN_CHAR_RSP:
  97. sprintf(statusName, "CM_ATTEN_CHAR_RSP");
  98. break;
  99. case CM_VALIDATE_REQ:
  100. sprintf(statusName, "CM_VALIDATE_REQ");
  101. break;
  102. case CM_VALIDATE_CNF:
  103. sprintf(statusName, "CM_VALIDATE_CNF");
  104. break;
  105. case CM_SLAC_MATCH_REQ:
  106. sprintf(statusName, "CM_SLAC_MATCH_REQ");
  107. break;
  108. case CM_SLAC_MATCH_CNF:
  109. sprintf(statusName, "CM_SLAC_MATCH_CNF");
  110. break;
  111. case CM_AMP_MAP_REQ:
  112. sprintf(statusName, "CM_AMP_MAP_REQ");
  113. break;
  114. case CM_AMP_MAP_CNF:
  115. sprintf(statusName, "CM_AMP_MAP_CNF");
  116. break;
  117. case CM_SET_KEY_REQ:
  118. sprintf(statusName, "CM_SET_KEY_REQ");
  119. break;
  120. case CM_SET_KEY_CNF:
  121. sprintf(statusName, "CM_SET_KEY_CNF");
  122. break;
  123. case SLACC_SDP_UDP_Connection:
  124. sprintf(statusName, "SLACC_SDP_UDP_Connection");
  125. break;
  126. case SLACC_SDP_TCP_Connection:
  127. sprintf(statusName, "SLACC_SDP_TCP_Connection");
  128. break;
  129. case SupportedAppProtocolRequest:
  130. sprintf(statusName, "SupportedAppProtocolReq");
  131. break;
  132. case SupportedAppProtocolResponse:
  133. sprintf(statusName, "SupportedAppProtocolRes");
  134. break;
  135. case SessionSetupRequest:
  136. sprintf(statusName, "SessionSetupReq");
  137. break;
  138. case SessionSetupResponse:
  139. sprintf(statusName, "UnSessionSetupResknown");
  140. break;
  141. case ServiceDiscoveryRequest:
  142. sprintf(statusName, "ServiceDiscoveryReq");
  143. break;
  144. case ServiceDiscoveryResponse:
  145. sprintf(statusName, "ServiceDiscoveryRes");
  146. break;
  147. case ServiceDetailRequest:
  148. sprintf(statusName, "ServiceDetailReq");
  149. break;
  150. case ServiceDetailResponse:
  151. sprintf(statusName, "ServiceDetailRes");
  152. break;
  153. case ServiceAndPaymentSelectionRequest:
  154. sprintf(statusName, "ServiceAndPaymentSelectionReq");
  155. break;
  156. case ServiceAndPaymentSelectionResponse:
  157. sprintf(statusName, "ServiceAndPaymentSelectionRes");
  158. break;
  159. case PaymentDetailsRequest:
  160. sprintf(statusName, "PaymentDetailsReq");
  161. break;
  162. case PaymentDetailsResponse:
  163. sprintf(statusName, "PaymentDetailsRes");
  164. break;
  165. case AuthorizationRequest:
  166. sprintf(statusName, "AuthorizationReq");
  167. break;
  168. case AuthorizationResponse:
  169. sprintf(statusName, "AuthorizationRes");
  170. break;
  171. case CertificateUpdateRequest:
  172. sprintf(statusName, "CertificateUpdateReq");
  173. break;
  174. case CertificateUpdateResponse:
  175. sprintf(statusName, "CertificateUpdateRes");
  176. break;
  177. case CertificateInstallationRequest:
  178. sprintf(statusName, "CertificateInstallationReq");
  179. break;
  180. case CertificateInstallationResponse:
  181. sprintf(statusName, "CertificateInstallationRes");
  182. break;
  183. case ChargeParameterDiscoveryRequest:
  184. sprintf(statusName, "ChargeParameterDiscoveryReq");
  185. break;
  186. case ChargeParameterDiscoveryResponse:
  187. sprintf(statusName, "ChargeParameterDiscoveryRes");
  188. break;
  189. case CableCheckRequest:
  190. sprintf(statusName, "CableCheckReq");
  191. break;
  192. case CableCheckResponse:
  193. sprintf(statusName, "CableCheckRes");
  194. break;
  195. case PreChargeRequest:
  196. sprintf(statusName, "PreChargeReq");
  197. break;
  198. case PreChargeResponse:
  199. sprintf(statusName, "PreChargeRes");
  200. break;
  201. case PowerDeliveryRequestStart:
  202. sprintf(statusName, "PowerDeliveryReqStart");
  203. break;
  204. case PowerDeliveryResponsetStart:
  205. sprintf(statusName, "PowerDeliveryResStart");
  206. break;
  207. case ChargingStatusRequest:
  208. sprintf(statusName, "ChargingStatusReq");
  209. break;
  210. case ChargingStatusResponse:
  211. sprintf(statusName, "ChargingStatusRes");
  212. break;
  213. case CurrentDemandRequest:
  214. sprintf(statusName, "CurrentDemandReq");
  215. break;
  216. case CurrentDemandResponse:
  217. sprintf(statusName, "CurrentDemandRes");
  218. break;
  219. case MeteringReceiptRequest:
  220. sprintf(statusName, "MeteringReceiptReq");
  221. break;
  222. case MeteringReceiptResponse:
  223. sprintf(statusName, "MeteringReceiptRes");
  224. break;
  225. case PowerDeliveryRequestStop:
  226. sprintf(statusName, "PowerDeliveryReqStop");
  227. break;
  228. case PowerDeliveryResponseStop:
  229. sprintf(statusName, "PowerDeliveryResStop");
  230. break;
  231. case WeldingDetectionRequest:
  232. sprintf(statusName, "WeldingDetectionReq");
  233. break;
  234. case WeldingDetectionResponse:
  235. sprintf(statusName, "WeldingDetectionRes");
  236. break;
  237. case SessionStopRequest:
  238. sprintf(statusName, "SessionStopReq");
  239. break;
  240. case SessionStopResponse:
  241. sprintf(statusName, "SessionStopRes");
  242. break;
  243. case Performance_Timeout:
  244. sprintf(statusName, "Performance_Timeout");
  245. break;
  246. case Sequence_Timeout:
  247. sprintf(statusName, "Sequence_Timeout");
  248. break;
  249. case Other_Fault:
  250. sprintf(statusName, "Other_Fault");
  251. break;
  252. default:
  253. sprintf(statusName, "Unknown");
  254. result = FAIL;
  255. break;
  256. }
  257. return result;
  258. }
  259. /**
  260. *
  261. * @return
  262. */
  263. unsigned char Get_V2G_Status()
  264. {
  265. unsigned char result = 0;
  266. switch (ShmCcsData->CommProtocol)
  267. {
  268. case V2GT_MSG_PROTOCOL_DIN70121: //0
  269. {
  270. result = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus;
  271. break;
  272. }
  273. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  274. {
  275. result = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus;
  276. break;
  277. }
  278. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  279. {
  280. result = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus;
  281. break;
  282. }
  283. default:
  284. break;
  285. }
  286. return result;
  287. }
  288. /**
  289. *
  290. * @return
  291. */
  292. unsigned char Get_V2G_Status_pre()
  293. {
  294. unsigned char result = 0;
  295. switch (ShmCcsData->CommProtocol)
  296. {
  297. case V2GT_MSG_PROTOCOL_DIN70121: //0
  298. {
  299. result = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus_pre;
  300. break;
  301. }
  302. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  303. {
  304. result = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus_pre;
  305. break;
  306. }
  307. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  308. {
  309. result = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus_pre;
  310. break;
  311. }
  312. default:
  313. break;
  314. }
  315. return result;
  316. }
  317. /**
  318. *
  319. * @param Fd
  320. * @param MsgId
  321. * @param SlaveAddress
  322. * @param DataLength
  323. * @param SendData
  324. * @return
  325. */
  326. int CAN_Tx_MSG(int Fd, unsigned int MsgId, unsigned char SlaveAddress, unsigned char DataLength, unsigned char *SendData)
  327. {
  328. #if 0
  329. struct can_frame frame;
  330. struct timeb StartTime, EndTime;
  331. unsigned int tmp = 0;
  332. int nbytes = 0;
  333. int i = 0;
  334. //Protection: To avoid unexpected length for CAN bus payload.
  335. if (DataLength > 8)
  336. {
  337. DataLength = 8;
  338. }
  339. memset(&frame, 0, sizeof(struct can_frame));
  340. frame.can_id = 0x80000000 | CAN_SEND_DIRECTION | MsgId | SlaveAddress; //0x80000000: extension ID format
  341. frame.can_dlc = DataLength;
  342. memcpy(frame.data, SendData, DataLength);
  343. nbytes = write(Fd, &frame, sizeof(struct can_frame));
  344. return nbytes;
  345. #else
  346. return -1;
  347. #endif
  348. }
  349. /**
  350. *
  351. * @param Fd
  352. * @return
  353. */
  354. int Proc_EVStopRes(int Fd)
  355. {
  356. int nbytes;
  357. unsigned char Buffer[8];
  358. memset(Buffer, 0, sizeof(Buffer));
  359. if (CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency == TRUE)
  360. {
  361. Buffer[0] = EV_EMERGENCY_STOP; //2
  362. }
  363. else
  364. {
  365. Buffer[0] = EV_NORMAL_STOP; //1
  366. }
  367. Buffer[1] = ShmStatusCodeData->PresentStatusCode[0][0];
  368. Buffer[2] = ShmStatusCodeData->PresentStatusCode[0][1];
  369. Buffer[3] = ShmStatusCodeData->PresentStatusCode[0][2];
  370. Buffer[4] = ShmStatusCodeData->PresentStatusCode[0][3];
  371. Buffer[5] = ShmStatusCodeData->PresentStatusCode[0][4];
  372. Buffer[6] = ShmStatusCodeData->PresentStatusCode[0][5];
  373. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_EV_STOP_EVENT, ShmInternalComm->SlaveAddress, 7, Buffer);
  374. //system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"); //for test only
  375. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  376. DEBUG_INFO("Sending STOP Command to CSU\n");
  377. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = FALSE;
  378. return nbytes;
  379. }
  380. /**
  381. *
  382. * @param cmd
  383. * @return
  384. */
  385. int Sniffer_Candump(char cmd)
  386. {
  387. #if (CANDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  388. if (cmd == ENABLE)
  389. {
  390. DEBUG_INFO("Candump init\n");
  391. system("cd /mnt/;rm -rf candump/");
  392. system("cd /mnt/;mkdir candump");
  393. DEBUG_INFO("Candump on\n");
  394. system("cd /mnt/candump;candump -l can0 &");
  395. return 0;
  396. }
  397. else if (cmd == DISABLE)
  398. {
  399. DEBUG_INFO("Candump off\n");
  400. system("killall candump");
  401. DEBUG_INFO("Candump save\n");
  402. system("cd /;cp -rfv /mnt/candump /Storage/SystemLog/");
  403. return 0;
  404. }
  405. else
  406. {
  407. DEBUG_INFO("Candump unexpected cmd(%d)\n", cmd);
  408. return -1;
  409. }
  410. #endif
  411. return 0;
  412. }
  413. /**
  414. *
  415. * @param cmd
  416. * @return
  417. */
  418. int Sniffer_Tcpdump(char cmd)
  419. {
  420. #if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  421. if (cmd == ENABLE)
  422. {
  423. DEBUG_INFO("Tcpdump init...\n");
  424. system("cd /mnt/;rm -rf tcpdump/");
  425. system("cd /mnt/;mkdir tcpdump");
  426. unsigned char cmdBuf[256];
  427. time_t CurrentTime;
  428. struct tm *tm;
  429. struct timeval tv;
  430. memset(cmdBuf, 0, sizeof(cmdBuf));
  431. CurrentTime = time(NULL);
  432. tm = localtime(&CurrentTime);
  433. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  434. sprintf((char*)cmdBuf, "tcpdump -i eth1 -p \"udp or ether proto 0x88e1\" -w /mnt/tcpdump/[%04d.%02d]CCS_SlacPackets_%02X%02X%02X%02X%02X%02X%02X%02X.log &",
  435. (tm->tm_year+1900),
  436. (tm->tm_mon+1),
  437. EVCOMM_SYS_INFO.SessionID[0],
  438. EVCOMM_SYS_INFO.SessionID[1],
  439. EVCOMM_SYS_INFO.SessionID[2],
  440. EVCOMM_SYS_INFO.SessionID[3],
  441. EVCOMM_SYS_INFO.SessionID[4],
  442. EVCOMM_SYS_INFO.SessionID[5],
  443. EVCOMM_SYS_INFO.SessionID[6],
  444. EVCOMM_SYS_INFO.SessionID[7]);
  445. DEBUG_INFO("Tcpdump on\n");
  446. system((char*)cmdBuf);
  447. return 0;
  448. }
  449. else if (cmd == DISABLE)
  450. {
  451. DEBUG_INFO("Tcpdump off\n");
  452. system("killall tcpdump");
  453. for(int idx=2;idx>0;idx--)
  454. {
  455. DEBUG_INFO("Tcpdump wait: %ds\n", idx);
  456. sleep(1);
  457. }
  458. DEBUG_INFO("Tcpdump save\n");
  459. system("cd /;cp -rfv /mnt/tcpdump/* /Storage/SystemLog/");
  460. system("cd /mnt/;rm -rf tcpdump/");
  461. return 0;
  462. }
  463. else
  464. {
  465. DEBUG_INFO("Tcpdump unexpected cmd(%d)\n", cmd);
  466. return -1;
  467. }
  468. #endif
  469. return 0;
  470. }
  471. /**
  472. *
  473. * @param state_new
  474. * @return
  475. */
  476. int Update_V2G_Status(unsigned int state_new)
  477. {
  478. unsigned char state_now;
  479. char nameNow[64], nameNew[64];
  480. state_now = Get_V2G_Status();
  481. get_V2G_Status_Name(state_now, nameNow);
  482. get_V2G_Status_Name(state_new, nameNew);
  483. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE ||
  484. CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused == TRUE)
  485. {
  486. if (state_now != Performance_Timeout && //253
  487. state_now != Sequence_Timeout && //254
  488. state_now != Other_Fault) //255
  489. {
  490. DEBUG_WARN("state-%s(%d) change: ignored (End_Process_inused:%d, FW_Update_Task_inused:%d)\n",
  491. nameNew, state_new,
  492. EVCOMM_SYS_INFO.End_Process_inused,
  493. CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused);
  494. return -1;
  495. }
  496. }
  497. if (state_now != state_new)
  498. {
  499. //Step 1: Check if Error Occours
  500. if (state_now == Performance_Timeout || //253
  501. state_now == Sequence_Timeout || //254
  502. state_now == Other_Fault) //255
  503. {
  504. if (state_new != IDLE)
  505. {
  506. if (EVCOMM_SYS_INFO.State_Change_Ignored_Notice == FALSE)
  507. {
  508. EVCOMM_SYS_INFO.State_Change_Ignored_Notice = TRUE;
  509. DEBUG_WARN("state-%s(%d) change: ignored(now in error state-%s(%d))\n", nameNow, state_new, nameNew, state_now);
  510. }
  511. return -1;
  512. }
  513. }
  514. //Updating the state formally.
  515. DEBUG_INFO("%s(%02d) > %s(%02d)\n", nameNow, state_now, nameNew, state_new);
  516. V2gFlowStatus = state_new;
  517. state_now = state_new;
  518. switch (ShmCcsData->CommProtocol)
  519. {
  520. case V2GT_MSG_PROTOCOL_DIN70121: //0
  521. {
  522. ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus;
  523. ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus = V2gFlowStatus;
  524. break;
  525. }
  526. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  527. {
  528. ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus;
  529. ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus = V2gFlowStatus;
  530. break;
  531. }
  532. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  533. {
  534. ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus_pre = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus;
  535. ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus = V2gFlowStatus;
  536. break;
  537. }
  538. default:
  539. break;
  540. }
  541. }
  542. return 0;
  543. }
  544. /**
  545. *
  546. * @param ST
  547. * @param ET
  548. * @return
  549. */
  550. double DiffTimeb(struct timeb ST, struct timeb ET)
  551. {
  552. //return milli-second
  553. double StartTime, EndTime;
  554. double t_diff;
  555. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  556. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  557. t_diff = EndTime - StartTime;
  558. if (t_diff < 0)
  559. {
  560. return -1;
  561. }
  562. return t_diff;
  563. }
  564. /**
  565. *
  566. * @param ST
  567. * @param ET
  568. * @return
  569. */
  570. double DiffTimeb_fork1_Error_Monitor(struct timeb ST, struct timeb ET)
  571. {
  572. //return milli-second
  573. static double StartTime, EndTime;
  574. static double t_diff;
  575. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  576. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  577. t_diff = EndTime - StartTime;
  578. if (t_diff < 0)
  579. {
  580. return -1;
  581. }
  582. return t_diff;
  583. }
  584. /**
  585. *
  586. * @param ST
  587. * @param ET
  588. * @return
  589. */
  590. double DiffTimeb_fork2_Error_Monitor(struct timeb ST, struct timeb ET)
  591. {
  592. //return milli-second
  593. static double StartTime, EndTime;
  594. static double t_diff;
  595. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  596. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  597. t_diff = EndTime - StartTime;
  598. if (t_diff < 0)
  599. {
  600. return -1;
  601. }
  602. return t_diff;
  603. }
  604. /**
  605. *
  606. * @return
  607. */
  608. int ShareMemory_Init()
  609. {
  610. int MeterSMId;
  611. //create ShmSysConfigAndInfo
  612. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  613. {
  614. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  615. return 0;
  616. }
  617. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  618. {
  619. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  620. return 0;
  621. }
  622. //create ShmStatusCodeData
  623. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  624. {
  625. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  626. return 0;
  627. }
  628. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  629. {
  630. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  631. return 0;
  632. }
  633. //create ShmCcsData
  634. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  635. {
  636. DEBUG_ERROR("shmget ShmCcsData NG\n");
  637. return 0;
  638. }
  639. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  640. {
  641. DEBUG_ERROR("shmat ShmCcsData NG\n");
  642. return 0;
  643. }
  644. //create ShmInternalComm
  645. if ((MeterSMId = shmget(ShmInternalCommKey, sizeof(struct InternalComm), IPC_CREAT | 0777)) < 0)
  646. {
  647. DEBUG_ERROR("shmget ShmInternalComm NG\n");
  648. return 0;
  649. }
  650. else if ((ShmInternalComm = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  651. {
  652. DEBUG_ERROR("shmat ShmInternalComm NG\n");
  653. return 0;
  654. }
  655. //Initial ShmSDispenser
  656. if ((MeterSMId = shmget(ShmDispenserKey, sizeof(struct DISPENSER), 0777)) < 0)
  657. {
  658. DEBUG_ERROR("shmget ShmDispenser NG %d\n", MeterSMId);
  659. return 0;
  660. }
  661. else if ((ShmDispenser = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  662. {
  663. DEBUG_ERROR("shmat ShmDispenser NG\n");
  664. return 0;
  665. }
  666. //[To-Do] The initialization here is reduntant and should be removed partially.
  667. //SHM_Init_supportedAppProtocolRes(ShmCcsData);
  668. //SHM_Init_din_SessionSetupRes(ShmCcsData);
  669. //SHM_Init_din_ServiceDiscoveryRes(ShmCcsData);
  670. //SHM_Init_din_ServiceAndPaymentSelectionRes(ShmCcsData);
  671. //SHM_Init_din_ContractAuthenticationRes(ShmCcsData);
  672. SHM_Init_din_ChargeParameterDiscoveryRes(ShmCcsData);
  673. SHM_Init_din_CableCheckRes(ShmCcsData);
  674. SHM_Init_din_PreChargeRes(ShmCcsData);
  675. SHM_Init_din_PowerDeliveryRes(ShmCcsData);
  676. SHM_Init_din_CurrentDemandRes(ShmCcsData);
  677. SHM_Init_din_WeldingDetectionRes(ShmCcsData);
  678. SHM_Init_din_SessionStopRes(ShmCcsData);
  679. return 1;
  680. }
  681. /**
  682. * 1. Accessing current CsuMac address, if Eth = eth1
  683. 2. The address of eth1(for QCA7000) is a random number, which will be modified
  684. after each time of booting up system.
  685. * @param Eth
  686. * @param mac
  687. * @return
  688. */
  689. int GetEthMac(unsigned char *Eth, unsigned char *mac)
  690. {
  691. //Parameters:MAC,IP,Mask,Gateway
  692. int fd;
  693. unsigned char addr[18], Buffer[128];
  694. memset(Buffer, 0, sizeof(Buffer));
  695. sprintf((char*)Buffer, "cat /sys/class/net/%s/address > /mnt/GetEthInfo", Eth); //CsuMac (Eth = eth1)
  696. system((char*)Buffer);
  697. fd = open("/mnt/GetEthInfo", O_RDONLY);
  698. if(fd < 0)
  699. {
  700. system("rm -f /mnt/GetEthInfo");
  701. DEBUG_ERROR("GetEthMac: MAC Address open error\n");
  702. return 0;
  703. }
  704. memset(mac, 0, 6);
  705. memset(addr, 0, sizeof(addr));
  706. read(fd, addr, 17);
  707. close(fd);
  708. system("rm -f /mnt/GetEthInfo");
  709. 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)
  710. DEBUG_INFO("EVSE MAC address(%s): %02X:%02X:%02X:%02X:%02X:%02X\n", Eth, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  711. return 1;
  712. }
  713. /**
  714. *
  715. * @param AdcChannel
  716. * @return
  717. */
  718. float ReadAdcVolt(unsigned char AdcChannel)
  719. {
  720. //AIN0=CCS GUN Temp 1
  721. //AIN1=CCS GUN Temp 2
  722. //AIN2=CCS_Proximity/2
  723. //AIN3=pilot voltage
  724. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  725. return 0;
  726. #else
  727. if(AdcChannel == 3)
  728. {
  729. int fd,count,AvgTimes;
  730. unsigned char SampleBuf[4];
  731. float TmpVolt, MinSample, AvgSample = 0;
  732. fd = open("/sys/bus/iio/devices/iio\\:device0/in_voltage3_raw", O_RDONLY);
  733. if(fd > 0)
  734. {
  735. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  736. for(AvgTimes = 0; AvgTimes < 3; AvgTimes++) //period = 60~91 ms(renice -10, , CurrentDemand()) /*+++ 20200909, vern, extend detection time for interference ---*/
  737. {
  738. count = 0;
  739. MinSample = 2306;
  740. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  741. while(count < 40) //period = 21~42ms (renice -10, CurrentDemand()) /*+++ 20200909, vern, extend detection time for interference ---*/
  742. {
  743. //re-sampling period = 3~13ms (renice -10, SLAC())
  744. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  745. read(fd, SampleBuf, 4); //period = 3.2~10ms (renice -10, SLAC())
  746. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  747. TmpVolt = atoi((char*)SampleBuf);
  748. if((TmpVolt < 2306) && (TmpVolt > 0))//positive voltage
  749. {
  750. if(TmpVolt < MinSample)
  751. {
  752. MinSample = TmpVolt;
  753. }
  754. count++;
  755. }
  756. lseek(fd, 0, SEEK_SET);
  757. }
  758. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  759. AvgSample += MinSample;
  760. }
  761. AvgSample /= AvgTimes;
  762. close(fd);
  763. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  764. return ((0.954-(1.8*AvgSample/4095))/0.06);
  765. }
  766. else
  767. {
  768. return -1;
  769. }
  770. }
  771. else
  772. {
  773. FILE *fp;
  774. unsigned char str[64];
  775. unsigned char AdcValue[8];
  776. if(AdcChannel > 7)
  777. {
  778. return -1;
  779. }
  780. memset(str,0,sizeof(str));
  781. memset(AdcValue,0,sizeof(AdcValue));
  782. sprintf((char*)str, "cat /sys/bus/iio/devices/iio\\:device0/in_voltage%d_raw", AdcChannel);
  783. fp=popen((char*)str, "r");
  784. if(fgets((char*)AdcValue,sizeof(AdcValue),fp) == NULL)
  785. {
  786. pclose(fp);
  787. return -1;
  788. }
  789. pclose(fp);
  790. //Vin = Vref *D / (2^n - 1)
  791. return ((float)1.8*(float)atoi((char*)AdcValue))/4095;
  792. }
  793. #endif
  794. }
  795. /**
  796. *
  797. * @return
  798. */
  799. float ReadAdcVolt_PP_fork3()
  800. {
  801. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  802. return 0;
  803. #else
  804. int fd, AvgTimes;
  805. unsigned char SampleBuf[4];
  806. float TmpVolt = 0;
  807. float AvgSample = 0;
  808. float V_pp = 0;
  809. fd = open("/sys/bus/iio/devices/iio\\:device0/in_voltage2_raw", O_RDONLY); //PP
  810. if(fd > 0)
  811. {
  812. for(AvgTimes = 0; AvgTimes < 5; AvgTimes++) //get 5 samples
  813. {
  814. read(fd, SampleBuf, 4);
  815. TmpVolt = atoi((char*)SampleBuf);
  816. lseek(fd, 0, SEEK_SET);
  817. AvgSample += TmpVolt;
  818. }
  819. close(fd);
  820. AvgSample /= AvgTimes;
  821. V_pp = (3.6*AvgSample)/4095; //PP
  822. usleep(20000); //20ms
  823. return V_pp;
  824. }
  825. else
  826. {
  827. return -1;
  828. }
  829. #endif
  830. }
  831. /**
  832. *
  833. */
  834. void Qca7kPowerReset()
  835. {
  836. DEBUG_INFO("QCA7000 reset...\n");
  837. system("ifconfig eth1 down");
  838. usleep(500000);
  839. system("echo 0 > /sys/class/gpio/gpio115/value");
  840. usleep(500000);
  841. system("echo 1 > /sys/class/gpio/gpio115/value");
  842. usleep(500000);
  843. system("ifconfig eth1 up");
  844. usleep(500000);
  845. }
  846. /**
  847. *
  848. * @param OnOff
  849. * @return
  850. */
  851. int SwitchCpStateE(unsigned char OnOff)
  852. {
  853. /*
  854. * TODO: Request CP change to state E
  855. */
  856. if((OnOff != ENABLE) && (OnOff != DISABLE))
  857. {
  858. return -1;
  859. }
  860. struct ChargingInfoData *ccs;
  861. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  862. ccs->SwitchCpStateE_status = OnOff;
  863. //OnOff = 1 => switch State to E
  864. //OnOff = 0 => return noraml
  865. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  866. {
  867. if(OnOff == DISABLE)
  868. {
  869. //DEBUG_INFO("SwitchCpStateE: released\n");
  870. CSUCOMMAC_SHM.CpSetStateE = DISABLE;
  871. }
  872. else
  873. {
  874. OutputCpPwmDuty(100); //set CP duty as 100, firstly.
  875. CSUCOMMAC_SHM.CpSetStateE = ENABLE;
  876. //DEBUG_INFO("SwitchCpStateE: enabled!\n");
  877. }
  878. }
  879. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  880. {
  881. if(OnOff == DISABLE)
  882. {
  883. //DEBUG_INFO("SwitchCpStateE: released\n");
  884. system("echo 0 > /sys/class/gpio/gpio86/value");
  885. }
  886. else
  887. {
  888. OutputCpPwmDuty(100); //set CP duty as 100, firstly.
  889. system("echo 1 > /sys/class/gpio/gpio86/value");
  890. //DEBUG_INFO("SwitchCpStateE: enabled!\n");
  891. }
  892. }
  893. #endif
  894. if (ccs->SwitchCpStateE_status_pre != ccs->SwitchCpStateE_status)
  895. {
  896. DEBUG_INFO("SwitchCpStateE: %d >> %d\n",
  897. ccs->SwitchCpStateE_status_pre,
  898. ccs->SwitchCpStateE_status);
  899. ccs->SwitchCpStateE_status_pre = ccs->SwitchCpStateE_status;
  900. }
  901. return 0;
  902. }
  903. /**
  904. *
  905. * @param Duty
  906. * @return
  907. */
  908. int OutputCpPwmDuty(unsigned char Duty)
  909. {
  910. #if ((CCS_ENERGY_TRANSFER_MODE != MODE_AC_SINGLE_PHASE_CORE) && (CCS_ENERGY_TRANSFER_MODE != MODE_AC_THREE_PHASE_CORE))
  911. char cmdBuf[1024];
  912. int DutyInNanoSec;
  913. #endif
  914. struct ChargingInfoData *ccs;
  915. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  916. if((Duty < 0)||(Duty > 100))
  917. {
  918. return -1;
  919. }
  920. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  921. {
  922. CSUCOMMAC_SHM.CpSetPWMDuty = Duty;
  923. }
  924. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  925. {
  926. DutyInNanoSec = 10000 * Duty;
  927. sprintf((char*)cmdBuf, "echo %d > /sys/class/pwm/pwmchip0/pwm0/duty_cycle", DutyInNanoSec);//nanoseconds
  928. system((char*)cmdBuf);
  929. }
  930. #endif
  931. ccs->CpDuty = Duty;
  932. //CP Duty
  933. if (ccs->CpDuty != ccs->CpDuty_pre)
  934. {
  935. DEBUG_INFO("CP Duty: %d%% >> %d%%\n", ccs->CpDuty_pre, ccs->CpDuty);
  936. ccs->CpDuty_pre = ccs->CpDuty;
  937. }
  938. return 0;
  939. }
  940. /**
  941. *
  942. * @param ccs
  943. */
  944. //#if DEBUG_INFO_SWITCH == ENABLE
  945. void Check_Plugin_Status_Update(struct ChargingInfoData *ccs)
  946. {
  947. if (ccs->ConnectorPlugIn != ccs->ConnectorPlugIn_new)
  948. {
  949. DEBUG_INFO("[fork1]Plugin: %d >> %d (CP=%.2fV, PP=%.2fV)\n",
  950. ccs->ConnectorPlugIn,
  951. ccs->ConnectorPlugIn_new,
  952. ccs->CpVoltage,
  953. ccs->PpVoltage);
  954. ccs->ConnectorPlugIn_pre = ccs->ConnectorPlugIn;
  955. ccs->ConnectorPlugIn = ccs->ConnectorPlugIn_new;
  956. //ccs->CpVoltage_pre = ccs->CpVoltage;
  957. }
  958. }
  959. //#endif
  960. /**
  961. *
  962. * @return
  963. */
  964. int CheckConnectorPlugIn()
  965. {
  966. #if (CP_PROTECTION_MECHANISM == ENABLE)
  967. return ((ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_B) || (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_C) || (ShmDispenser->gun_info.primaryMcuState.cp_state == CP_STATE_D) ? TRUE : FALSE);
  968. #else
  969. return TRUE;
  970. #endif
  971. }
  972. /**
  973. *
  974. * @param ccs
  975. * @return
  976. */
  977. int Check_CP_State_Error(struct ChargingInfoData *ccs)
  978. {
  979. #if (CP_PROTECTION_MECHANISM == DISABLE)
  980. {
  981. return -1;
  982. }
  983. #endif
  984. unsigned char state = 0;
  985. #if ((CCS_ENERGY_TRANSFER_MODE != MODE_AC_SINGLE_PHASE_CORE) && (CCS_ENERGY_TRANSFER_MODE != MODE_AC_THREE_PHASE_CORE))
  986. double time_diff = 0;
  987. #endif
  988. state = Get_V2G_Status();
  989. //SLAC, SLAAC, SDP, ..., ChargeParameterDiscovery
  990. if (state >= CM_SLAC_PARM_CONF && //by considering 15118(SLAC first)
  991. state < ChargeParameterDiscoveryRequest &&
  992. state != IDLE &&
  993. state != CM_SET_KEY_REQ &&
  994. state != CM_SET_KEY_CNF &&
  995. state != CM_VALIDATE_REQ &&
  996. state != CM_VALIDATE_CNF)
  997. {
  998. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  999. {
  1000. #ifdef TEST_WITH_ETH0
  1001. if((ShmCcsData->EnergyTransferMode == MODE_DC_EXTENDED) && ((ccs->CpState != CCS_CP_STATE_B1) && (ccs->CpState != CCS_CP_STATE_B2)))
  1002. #else
  1003. if((ccs->CpState != CCS_CP_STATE_B1) && (ccs->CpState != CCS_CP_STATE_B2))
  1004. #endif
  1005. {
  1006. if (ccs->CpState_err == FALSE)
  1007. {
  1008. DEBUG_ERROR("[fork1][CP]before CPD\n"); //CPD: ChargeParameterDiscoveryRequest
  1009. //CPD: ChargeParameterDiscovery
  1010. }
  1011. ccs->CpState_err = TRUE;
  1012. }
  1013. }
  1014. #else
  1015. {
  1016. if(ccs->CpState != CCS_CP_STATE_B2)
  1017. {
  1018. if (ccs->CpState_err == FALSE)
  1019. {
  1020. DEBUG_ERROR("[fork1][CP]]before CPD\n"); //CPD: ChargeParameterDiscoveryRequest
  1021. //CPD: ChargeParameterDiscovery
  1022. }
  1023. ccs->CpState_err = TRUE;
  1024. }
  1025. }
  1026. #endif
  1027. }
  1028. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  1029. {
  1030. //ChargeParameterDiscovery
  1031. if(state >= ChargeParameterDiscoveryRequest && //35
  1032. state <= ChargeParameterDiscoveryResponse) //36
  1033. {
  1034. #ifdef TEST_WITH_ETH0
  1035. if((ccs->CpState != CCS_CP_STATE_B2) && ((ShmCcsData->EnergyTransferMode != MODE_DC_EXTENDED) && (ccs->CpState != CCS_CP_STATE_C)))
  1036. #else
  1037. if(ccs->CpState != CCS_CP_STATE_B2)
  1038. #endif
  1039. {
  1040. if (ccs->CpState_err == FALSE)
  1041. {
  1042. DEBUG_ERROR("[fork1][CP]]CPD\n");
  1043. //PRC: Precharge
  1044. //CUD: CurrentDemand
  1045. }
  1046. ccs->CpState_err = TRUE;
  1047. }
  1048. }
  1049. //ChargingStatus
  1050. if(state >= ChargingStatusRequest && //43
  1051. state <= ChargingStatusResponse) //44
  1052. {
  1053. if((ccs->CpState != CCS_CP_STATE_B2) && (ccs->CpState != CCS_CP_STATE_C) && (ccs->CpState != CCS_CP_STATE_D))
  1054. {
  1055. if (ccs->CpState_err == FALSE)
  1056. {
  1057. DEBUG_ERROR("[fork1][CP]CGS\n");
  1058. //PRC: Precharge
  1059. //CUD: CurrentDemand
  1060. }
  1061. ccs->CpState_err = TRUE;
  1062. }
  1063. }
  1064. }
  1065. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  1066. {
  1067. //ChargeParameterDiscovery, CableCheck
  1068. if (state >= ChargeParameterDiscoveryRequest && //35
  1069. state <= CableCheckResponse) //38
  1070. {
  1071. if(ccs->CableCheckPreCountDownDone == FALSE &&
  1072. state >= CableCheckRequest)
  1073. {
  1074. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  1075. time_diff = DiffTimeb_fork1_Error_Monitor(EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start, EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  1076. if(time_diff >= V2G_SECC_CP_Timeout_CableCheck) //2 sec
  1077. {
  1078. DEBUG_INFO("[fork1][CP]check C(4),D(5): ON (%.02lf of %dms)\n", time_diff, V2G_SECC_CP_Timeout_CableCheck);
  1079. ccs->CableCheckPreCountDownDone = TRUE;
  1080. }
  1081. }
  1082. if(ccs->CableCheckPreCountDownDone == TRUE)
  1083. {
  1084. if(ccs->CpState != CCS_CP_STATE_C && ccs->CpState != CCS_CP_STATE_D)
  1085. {
  1086. if (ccs->CpState_err == FALSE)
  1087. {
  1088. DEBUG_ERROR("[fork1][CP]]CPD and CCK (after CNT)\n");
  1089. //CPD: ChargeParameterDiscovery
  1090. //CCK: CableCheck
  1091. //CNT: count down
  1092. }
  1093. ccs->CpState_err = TRUE;
  1094. }
  1095. }
  1096. else
  1097. {
  1098. if((ccs->CpState != CCS_CP_STATE_B2) && (ccs->CpState != CCS_CP_STATE_C) && (ccs->CpState != CCS_CP_STATE_D))
  1099. {
  1100. if (ccs->CpState_err == FALSE)
  1101. {
  1102. DEBUG_ERROR("[fork1][CP]CPD and CCK (before CNT)\n");
  1103. //CPD: ChargeParameterDiscovery
  1104. //CCK: CableCheck
  1105. //CNT: count down
  1106. }
  1107. ccs->CpState_err = TRUE;
  1108. }
  1109. }
  1110. }
  1111. //Precharge, CurrentDemand
  1112. if(state >= PreChargeRequest && //39
  1113. state <= CurrentDemandResponse) //46
  1114. {
  1115. if((ccs->CpState != CCS_CP_STATE_C) && (ccs->CpState != CCS_CP_STATE_D))
  1116. {
  1117. if (ccs->CpState_err == FALSE)
  1118. {
  1119. DEBUG_ERROR("[fork1][CP]PRC and CUD\n");
  1120. //PRC: Precharge
  1121. //CUD: CurrentDemand
  1122. }
  1123. ccs->CpState_err = TRUE;
  1124. }
  1125. }
  1126. }
  1127. #endif
  1128. if(ccs->CpState_err == TRUE && ccs->CpState_err_logged == FALSE)
  1129. {
  1130. OutputCpPwmDuty(100);
  1131. //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
  1132. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1133. //CCS_SECC_CP_State_Error (023889)
  1134. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1135. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1136. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1137. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1138. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1139. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1140. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1141. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1142. DEBUG_ERROR("[fork1]CP(%d) Error in state %d(%d)\n", ccs->CpState, state, ccs->CableCheckPreCountDownDone);
  1143. ccs->CpState_err_logged = TRUE;
  1144. }
  1145. return 0;
  1146. }
  1147. /**
  1148. *
  1149. * @param ccs
  1150. */
  1151. void Check_CP_State_Update(struct ChargingInfoData *ccs)
  1152. {
  1153. //CP State
  1154. if (ccs->CpState != ccs->CpState_pre)
  1155. {
  1156. DEBUG_INFO("[fork1]CP State: %d >> %d (%.2fV)\n",
  1157. ccs->CpState_pre,
  1158. ccs->CpState,
  1159. ccs->CpVoltage);
  1160. ccs->CpState_pre = ccs->CpState;
  1161. }
  1162. }
  1163. /**
  1164. *
  1165. */
  1166. void CP_Detection()
  1167. {
  1168. pid_t tmp = 0;
  1169. #if ((CCS_ENERGY_TRANSFER_MODE != MODE_AC_SINGLE_PHASE_CORE) && (CCS_ENERGY_TRANSFER_MODE != MODE_AC_THREE_PHASE_CORE))
  1170. unsigned char Statetmp;
  1171. float TotalTmpVolt;
  1172. #endif
  1173. struct ChargingInfoData *ccs;
  1174. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1175. if(pid.CP_Detection == 0)
  1176. {
  1177. tmp = fork();
  1178. if(tmp > 0)
  1179. {
  1180. pid.CP_Detection = tmp;
  1181. #if 1
  1182. unsigned char buf[64];
  1183. memset(buf, 0, sizeof(buf));
  1184. //sprintf((char*)buf, "renice -20 -p %d", tmp);
  1185. sprintf((char*)buf, "renice -10 -p %d", tmp);
  1186. system((char*)buf);
  1187. #endif
  1188. return;
  1189. }
  1190. }
  1191. while(1)
  1192. {
  1193. //ADC_Voltage = (1+RE62/RE63)*0.9 - (RE62/RE63)*Pilot_Voltage, RE62/RE63=0.06
  1194. //=>Pilot_Voltage=(0.954-ADC_Voltage)/0.06
  1195. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  1196. {
  1197. ccs->CpVoltage = ShmInternalComm->AC_CpPositiveVoltage;
  1198. ccs->CpState = ShmInternalComm->AC_CpPresentState;
  1199. }
  1200. #else //CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED
  1201. {
  1202. TotalTmpVolt = ReadAdcVolt(3);
  1203. ccs->CpVoltage = TotalTmpVolt;
  1204. if (ccs->CpVoltage_pre != ccs->CpVoltage)
  1205. {
  1206. ccs->CpVoltage_pre = ccs->CpVoltage;
  1207. }
  1208. //printf("TotalTmpVolt = %.02f\n", TotalTmpVolt);
  1209. //If CP voltage is higer than 13.5V
  1210. if(TotalTmpVolt >= 13.5)
  1211. {
  1212. //Pilot Error
  1213. if((ccs->CpState == CCS_CP_STATE_A) &&
  1214. (TotalTmpVolt < 13.75))
  1215. {
  1216. //V_cp = 13.5 ~ 13.75
  1217. Statetmp = 1;
  1218. }
  1219. else
  1220. {
  1221. //V_cp = 13.5 ~
  1222. Statetmp = 8;
  1223. }
  1224. }
  1225. else if((TotalTmpVolt >= 10.5) && (TotalTmpVolt < 13.5)) //V_cp = 10.5 ~ 13.5
  1226. {
  1227. //State A (12V)
  1228. if((ccs->CpState >= CCS_CP_STATE_B1) &&
  1229. (ccs->CpState <= CCS_CP_STATE_B2) &&
  1230. (TotalTmpVolt < 10.75))
  1231. {
  1232. if((ccs->CpDuty >= 5) &&
  1233. (ccs->CpDuty < 100))
  1234. {
  1235. Statetmp = 3;
  1236. }
  1237. else
  1238. {
  1239. Statetmp = 2;
  1240. }
  1241. }
  1242. else if((ccs->CpState == CCS_CP_STATE_G) &&
  1243. (TotalTmpVolt >= 13.25))
  1244. {
  1245. Statetmp = 8;
  1246. }
  1247. else
  1248. {
  1249. Statetmp = 1;
  1250. }
  1251. }
  1252. else if((TotalTmpVolt >= 7.5) && (TotalTmpVolt < 10.5))
  1253. {
  1254. //State B (9V)
  1255. if((ccs->CpState == CCS_CP_STATE_C) && (TotalTmpVolt < 7.75))
  1256. {
  1257. Statetmp = 4;
  1258. }
  1259. else if((ccs->CpState == CCS_CP_STATE_A) && (TotalTmpVolt >= 10.25))
  1260. {
  1261. Statetmp = 1;
  1262. }
  1263. else
  1264. {
  1265. if((ccs->CpDuty >= 5) && (ccs->CpDuty < 100))
  1266. {
  1267. Statetmp = 3;
  1268. }
  1269. else
  1270. {
  1271. Statetmp = 2;
  1272. }
  1273. }
  1274. }
  1275. else if((TotalTmpVolt >= 4.5) && (TotalTmpVolt < 7.5))
  1276. {
  1277. //State C (6V)
  1278. if((ccs->CpState == CCS_CP_STATE_D) && (TotalTmpVolt < 4.75))
  1279. {
  1280. Statetmp = 5;
  1281. }
  1282. else if((ccs->CpState >= CCS_CP_STATE_B1) && (ccs->CpState <= CCS_CP_STATE_B2) && (TotalTmpVolt >= 7.25))
  1283. {
  1284. if((ccs->CpDuty >= 5)&&(ccs->CpDuty < 100))
  1285. {
  1286. Statetmp = 3;
  1287. }
  1288. else
  1289. {
  1290. Statetmp = 2;
  1291. }
  1292. }
  1293. else
  1294. {
  1295. Statetmp = 4;
  1296. }
  1297. }
  1298. else if((TotalTmpVolt >= 1.5) && (TotalTmpVolt < 4.5))
  1299. {
  1300. //State D (3V)
  1301. if((ccs->CpState == CCS_CP_STATE_E)&&(TotalTmpVolt < 1.75))
  1302. {
  1303. Statetmp = 6;
  1304. }
  1305. else if((ccs->CpState == CCS_CP_STATE_C)&&(TotalTmpVolt >= 4.25))
  1306. {
  1307. Statetmp = 4;
  1308. }
  1309. else
  1310. {
  1311. Statetmp = 5;
  1312. }
  1313. }
  1314. else if((TotalTmpVolt >= -1.5) && (TotalTmpVolt < 1.5)) //V_cp = -1.5V ~ 1.5V
  1315. {
  1316. //State E (0V)
  1317. if((ccs->CpState == CCS_CP_STATE_G) &&
  1318. (TotalTmpVolt < -1.25))
  1319. {
  1320. Statetmp = 8;
  1321. }
  1322. else if((ccs->CpState == CCS_CP_STATE_D) &&
  1323. (TotalTmpVolt >= 1.25))
  1324. {
  1325. Statetmp = 5;
  1326. }
  1327. else
  1328. {
  1329. Statetmp = 6;
  1330. }
  1331. }
  1332. else if((TotalTmpVolt >= -13.5) && (TotalTmpVolt < -10.5)) //V_cp = -10.5V ~ -13.5V
  1333. {
  1334. //State F (-12V)
  1335. if((ccs->CpState == CCS_CP_STATE_G) &&
  1336. (TotalTmpVolt >= -10.75))
  1337. {
  1338. Statetmp = 8;
  1339. }
  1340. else
  1341. {
  1342. Statetmp = 7;
  1343. }
  1344. }
  1345. else
  1346. {
  1347. //null
  1348. }
  1349. ccs->CpState = Statetmp;
  1350. }
  1351. #endif
  1352. Check_CP_State_Update(ccs);
  1353. Check_CP_State_Error(ccs);
  1354. //Updating Plugin status
  1355. #if (PP_PROTECTION_MECHANISM == ENABLE)
  1356. if((ccs->CpState >= CCS_CP_STATE_B1 ) && (ccs->CpState <= CCS_CP_STATE_D ) && (ccs->PpVoltage >= 1.0)) //PP >= 1.0V
  1357. #else
  1358. if((ccs->CpState >= CCS_CP_STATE_B1 ) && (ccs->CpState <= CCS_CP_STATE_D ))
  1359. #endif
  1360. {
  1361. ccs->ConnectorPlugIn_new = TRUE;
  1362. }
  1363. else
  1364. {
  1365. ccs->ConnectorPlugIn_new = FALSE;
  1366. OutputCpPwmDuty(100);
  1367. }
  1368. Check_Plugin_Status_Update(ccs);
  1369. usleep(1000);
  1370. }//while
  1371. }
  1372. /**
  1373. * 1. In order to detect CP in efficient response time, we create an independent
  1374. * thread for this procedure.
  1375. 2. The priority of this thread is set as the same as other tasks.
  1376. */
  1377. void PP_Detection()
  1378. {
  1379. pid_t tmp = 0;
  1380. // struct timeb StartTime, EndTime;
  1381. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED))
  1382. //unsigned char Statetmp;
  1383. #endif
  1384. float TotalTmpVolt;
  1385. if(pid.PP_Detection == 0)
  1386. {
  1387. tmp = fork();
  1388. if(tmp > 0)
  1389. {
  1390. pid.PP_Detection = tmp;
  1391. #if 0
  1392. unsigned char buf[64];
  1393. memset(buf, 0, sizeof(buf));
  1394. sprintf((char*)buf, "renice -20 -p %d", tmp);
  1395. system(buf);
  1396. #endif
  1397. return;
  1398. }
  1399. }
  1400. while(1)
  1401. {
  1402. TotalTmpVolt = ReadAdcVolt_PP_fork3();
  1403. EVCOMM_SYS_INFO.PpVoltage = TotalTmpVolt;
  1404. if (EVCOMM_SYS_INFO.PpVoltage_pre != EVCOMM_SYS_INFO.PpVoltage)
  1405. {
  1406. if ((EVCOMM_SYS_INFO.PpVoltage_pre < 0.5 && EVCOMM_SYS_INFO.PpVoltage >= 1.0) ||
  1407. (EVCOMM_SYS_INFO.PpVoltage_pre >= 1.0 && EVCOMM_SYS_INFO.PpVoltage < 0.5))
  1408. {
  1409. DEBUG_INFO("[fork3]PP(%.2f >> %.2fV)\n", EVCOMM_SYS_INFO.PpVoltage_pre, EVCOMM_SYS_INFO.PpVoltage);
  1410. EVCOMM_SYS_INFO.PpVoltage_pre = EVCOMM_SYS_INFO.PpVoltage;
  1411. }
  1412. }
  1413. usleep(1000);
  1414. }//while
  1415. }
  1416. /**
  1417. *
  1418. */
  1419. void Error_Monitor()
  1420. {
  1421. pid_t tmp = 0;
  1422. double time_diff = 0;
  1423. unsigned char status = 0;
  1424. struct ChargingInfoData *ccs;
  1425. ccs = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1426. if(pid.Error_Monitor == 0)
  1427. {
  1428. tmp = fork(); //SeccComm fork2
  1429. if(tmp > 0)
  1430. {
  1431. pid.Error_Monitor = tmp;
  1432. #if 0
  1433. unsigned char buf[64];
  1434. memset(buf, 0, sizeof(buf));
  1435. sprintf((char*)buf, "renice -20 -p %d", tmp);
  1436. system(buf);
  1437. #endif
  1438. return;
  1439. }
  1440. }
  1441. for(;;)
  1442. {
  1443. //Step 0
  1444. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  1445. {
  1446. //If the End_Process is in processing, disable Error_Monitor.
  1447. continue;
  1448. }
  1449. //Step1 1: Check and Response to Plugin Status
  1450. if(CheckConnectorPlugIn() == FALSE)
  1451. {
  1452. status = Get_V2G_Status();
  1453. if (status > IDLE &&
  1454. status < Performance_Timeout &&
  1455. status != CM_SET_KEY_REQ &&
  1456. status != CM_SET_KEY_CNF &&
  1457. EVCOMM_SYS_INFO.End_Process_inused == FALSE)
  1458. {
  1459. #if (CP_PROTECTION_MECHANISM == ENABLE)
  1460. //DEBUG_INFO("[fork2] Emergency Stop (due to Connector is plugged out during communication.)\n");
  1461. DEBUG_ERROR("[fork2]Plug out Error => End_Process\n");
  1462. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1463. //CCS_SECC_CP_State_Error (023889)
  1464. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1465. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1466. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1467. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1468. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1469. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1470. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1471. End_Process();
  1472. #else
  1473. //DEBUG_INFO("CP_PROTECTION_MECHANISM is disabled. Emergency Stop: skipped\n");
  1474. #endif
  1475. }
  1476. }
  1477. //Step 2: Check for V2G_SECC_Sequence_Timeout
  1478. //#if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  1479. status = Get_V2G_Status();
  1480. if (status >= SupportedAppProtocolRequest &&
  1481. status < SessionStopRequest)
  1482. {
  1483. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_End);
  1484. time_diff = DiffTimeb_fork2_Error_Monitor(EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start, EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_End);
  1485. if(time_diff > V2G_SECC_Sequence_Timeout) //60s
  1486. {
  1487. DEBUG_ERROR("[fork2]V2G_SECC_Sequence_Timeout in state %d - (%.02lf of %d ms)\n", status, time_diff, V2G_SECC_Sequence_Timeout);
  1488. Update_V2G_Status(Sequence_Timeout);
  1489. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1490. //CCS_SECC_TIMEOUT_V2G_Sequence_Time (023844)
  1491. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1492. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1493. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1494. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1495. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  1496. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  1497. End_Process();
  1498. break;
  1499. }
  1500. else if (time_diff > 4000) //4s
  1501. {
  1502. //Check for CSU command of "Stop by EVSE"
  1503. if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_Shutdown)
  1504. {
  1505. DEBUG_INFO("[Error_Monitor]EVSE_Shutdown\n");
  1506. Update_V2G_Status(Other_Fault);
  1507. }
  1508. else if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_EmergencyShutdown)
  1509. {
  1510. DEBUG_INFO("[Error_Monitor]EVSE_EmergencyShutdown\n");
  1511. Update_V2G_Status(Other_Fault);
  1512. }
  1513. else if (ShmInternalComm->ChargingPermission == FALSE)
  1514. {
  1515. if (status >= ChargeParameterDiscoveryRequest) //&& status < SessionStopRequest
  1516. {
  1517. DEBUG_INFO("[Error_Monitor]ChargingPermission = FALSE\n");
  1518. Update_V2G_Status(Other_Fault);
  1519. }
  1520. }
  1521. else
  1522. {
  1523. //null
  1524. }
  1525. }
  1526. else
  1527. {
  1528. //null
  1529. }
  1530. }
  1531. //Step 3: Check and Response to Error V2gFlowStatus
  1532. status = Get_V2G_Status();
  1533. if (status == Performance_Timeout ||
  1534. status == Sequence_Timeout ||
  1535. status == Other_Fault)
  1536. {
  1537. //Normal Stop
  1538. DEBUG_ERROR("[fork2]Timeout or Fault State(%d) => End_Process\n", status);
  1539. End_Process();
  1540. }
  1541. //Step 4: Check and Response to CP State Error
  1542. if(ccs->CpState_err == TRUE)
  1543. {
  1544. DEBUG_ERROR("[fork2]CP Error => End_Process\n");
  1545. Update_V2G_Status(Other_Fault);
  1546. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1547. //CCS_SECC_CP_State_Error (023889)
  1548. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1549. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1550. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1551. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1552. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  1553. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1554. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1555. End_Process();
  1556. }
  1557. //Step 5: Check and Response to Shutdown Commnad from CSU
  1558. if (EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_Shutdown ||
  1559. EVCOMM_SYS_INFO.DC_EVSEStatus == EVSE_EmergencyShutdown)
  1560. {
  1561. if (Get_V2G_Status() <= SLACC_SDP_TCP_Connection)
  1562. {
  1563. DEBUG_INFO("[fork2]EVSE Shutdown(%d) => End_Process\n", EVCOMM_SYS_INFO.DC_EVSEStatus);
  1564. Update_V2G_Status(Other_Fault);
  1565. End_Process();
  1566. }
  1567. }
  1568. //Step 6: Check and Response to SessionStop
  1569. status = Get_V2G_Status();
  1570. if (status == SessionStopResponse)
  1571. {
  1572. DEBUG_INFO("[fork2]SessionStopResponse => End_Process\n");
  1573. End_Process();
  1574. }
  1575. //Step 7: Check for ChargingPermission from TRUE to FALSE before V2G Messages
  1576. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  1577. if ((ShmInternalComm->ChargingPermission == FALSE) &&
  1578. (ShmInternalComm->ChargingPermission_pre >= 1) &&
  1579. (ccs->CpState >= CCS_CP_STATE_B2) && (ccs->CpState <= CCS_CP_STATE_D))
  1580. {
  1581. if (status >= CM_SLAC_PARM_REQ &&
  1582. status != CM_SET_KEY_REQ &&
  1583. status != CM_SET_KEY_CNF &&
  1584. status <= SLACC_SDP_TCP_Connection)
  1585. {
  1586. DEBUG_INFO("[fork2]Permission OFF before V2G msg(%d) => End_Process\n", ShmInternalComm->ChargingPermission);
  1587. Update_V2G_Status(Other_Fault);
  1588. End_Process();
  1589. }
  1590. }
  1591. //Step 8: DC OVP Protection
  1592. if (CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED &&
  1593. status > CableCheckResponse &&
  1594. status <= SessionStopRequest &&
  1595. status != ChargingStatusRequest &&
  1596. status != ChargingStatusResponse &&
  1597. EVCOMM_SYS_INFO.EvBatteryMaxVoltage != 0)
  1598. {
  1599. //Part A: OVP Protection
  1600. if (EVCOMM_SYS_INFO.PresentChargingVoltage >= (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02)) // 2%
  1601. {
  1602. DEBUG_ERROR("[fork2]OVP => End_Process (%.02f > %.02f)\n",
  1603. EVCOMM_SYS_INFO.PresentChargingVoltage,
  1604. (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02));
  1605. Update_V2G_Status(Other_Fault);
  1606. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1607. //System CCS output OVP (012219)
  1608. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1609. ShmStatusCodeData->PresentStatusCode[0][1] = 1;
  1610. ShmStatusCodeData->PresentStatusCode[0][2] = 2;
  1611. ShmStatusCodeData->PresentStatusCode[0][3] = 2;
  1612. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  1613. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1614. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1615. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1616. End_Process();
  1617. }
  1618. //Part B: Over Voltage Request Protection
  1619. if (EVCOMM_SYS_INFO.EvBatterytargetVoltage >= (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02))
  1620. {
  1621. DEBUG_INFO("[fork2]Over V Req => End_Process (%.02f > %.02f)\n",
  1622. EVCOMM_SYS_INFO.EvBatterytargetVoltage,
  1623. (EVCOMM_SYS_INFO.EvBatteryMaxVoltage * 1.02));
  1624. Update_V2G_Status(Other_Fault);
  1625. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1626. //System CCS output OVP (012219)
  1627. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1628. ShmStatusCodeData->PresentStatusCode[0][1] = 1;
  1629. ShmStatusCodeData->PresentStatusCode[0][2] = 2;
  1630. ShmStatusCodeData->PresentStatusCode[0][3] = 2;
  1631. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  1632. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  1633. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1634. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1635. End_Process();
  1636. }
  1637. }
  1638. //Step 9: Check 60V
  1639. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  1640. if (CCS_ENERGY_TRANSFER_MODE == MODE_DC_EXTENDED &&
  1641. ShmInternalComm->ChargingPermission >= 1 &&
  1642. status < CableCheckRequest)
  1643. {
  1644. if (EVCOMM_SYS_INFO.PresentChargingVoltage >= 60) //60V
  1645. {
  1646. DEBUG_INFO("[fork2]DC Output Voltage is over 60V => End_Process\n");
  1647. Update_V2G_Status(Other_Fault);
  1648. //Update_ShmStatusCode(); //[To-Do] to be implemented
  1649. //CCS_SECC_Unexpected_60V_Before_Charing_Error (023890)
  1650. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  1651. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  1652. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  1653. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  1654. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  1655. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  1656. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  1657. //Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  1658. End_Process();
  1659. }
  1660. }
  1661. //Step 10: Check if the connector is unplug from plugin
  1662. if (EVCOMM_SYS_INFO.ConnectorPlugIn_pre == TRUE && EVCOMM_SYS_INFO.ConnectorPlugIn == FALSE)
  1663. {
  1664. DEBUG_INFO("[fork2]Unplug Reset => End_Process\n");
  1665. Update_V2G_Status(Other_Fault);
  1666. End_Process();
  1667. }
  1668. usleep(1000);
  1669. }//while
  1670. }
  1671. /**
  1672. *
  1673. * @return
  1674. */
  1675. int SendSetKey()
  1676. {
  1677. unsigned char nRandValue = 0x0;
  1678. unsigned char ConstString[16] = "PhihongKey000000";
  1679. memset(&qcaInfo.SendMmePacket, 0, sizeof(struct MmeHeader));
  1680. memcpy(qcaInfo.SendMmePacket.ODA, macAddr.qca, 6);
  1681. memcpy(qcaInfo.SendMmePacket.OSA, macAddr.eth1, 6);
  1682. qcaInfo.SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  1683. qcaInfo.SendMmePacket.MMV = 0x01;
  1684. qcaInfo.SendMmePacket.MMTYPE = MMTYPE_CM_SET_KEY_REQ;
  1685. qcaInfo.SendMmePacket.FMI[0] = qcaInfo.SendMmePacket.FMI[1] = 0;
  1686. qcaInfo.SendMmePacketSize = 0;
  1687. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0x01;//Fixed value (0x01) to indicate ��NMK��
  1688. memset(qcaInfo.SendMmePacket.MMENTRY+qcaInfo.SendMmePacketSize, 0, 4);//My Nonce, Fixed value(0x00000000), encrypted payload not used
  1689. qcaInfo.SendMmePacketSize += 4;
  1690. memset(qcaInfo.SendMmePacket.MMENTRY+qcaInfo.SendMmePacketSize, 0, 4);//Your Nonce, Fixed value(0x00000000), encrypted payload not used
  1691. qcaInfo.SendMmePacketSize += 4;
  1692. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0x04;//PID, Fixed value (0x04) to indicate ��HLE protocol��
  1693. memset(qcaInfo.SendMmePacket.MMENTRY+qcaInfo.SendMmePacketSize, 0, 2);//PRN, Fixed value(0x00), encrypted payload not used
  1694. qcaInfo.SendMmePacketSize += 2;
  1695. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0x00;//PMN, Fixed value(0x00) encrypted payload not used
  1696. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0x01;//CCo Capablility
  1697. srand(time(NULL));
  1698. for(int i = 10; i < 16; i++)
  1699. {
  1700. nRandValue = (rand()%62) + 1;
  1701. if((nRandValue>=0)&&(nRandValue<=9)) // 0 ~ 9
  1702. {
  1703. ConstString[i]= nRandValue + 0x30;
  1704. }
  1705. else if((nRandValue>=10)&&(nRandValue<=35)) // A ~ Z
  1706. {
  1707. ConstString[i]= nRandValue -10 + 0x41;
  1708. }
  1709. else if((nRandValue>=36)&&(nRandValue<=61)) // a ~ z
  1710. {
  1711. ConstString[i]= nRandValue -37 + 0x61;
  1712. }
  1713. else
  1714. {
  1715. ConstString[i]= 0x30;
  1716. }
  1717. }
  1718. memset(qcaInfo.NewNmkKey, 0, sizeof(qcaInfo.NewNmkKey));
  1719. memset(qcaInfo.Nid, 0, sizeof(qcaInfo.Nid));
  1720. HPAVKeyNMK(qcaInfo.NewNmkKey, (char*)ConstString);
  1721. HPAVKeyNID(qcaInfo.Nid, qcaInfo.NewNmkKey, DEFAULT_LEVEL);
  1722. memcpy(qcaInfo.SendMmePacket.MMENTRY+qcaInfo.SendMmePacketSize, qcaInfo.Nid, sizeof(qcaInfo.Nid)); //NID, 54 LSBs contain the NID 2 MSBs = 0b00
  1723. qcaInfo.SendMmePacketSize += sizeof(qcaInfo.Nid);
  1724. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0x01;//NewEKS,Fixed value (0x01)to indicate ��NMK��
  1725. memcpy(qcaInfo.SendMmePacket.MMENTRY+qcaInfo.SendMmePacketSize, qcaInfo.NewNmkKey,sizeof(qcaInfo.NewNmkKey));//NewKey
  1726. qcaInfo.SendMmePacketSize += sizeof(qcaInfo.NewNmkKey);
  1727. qcaInfo.SendMmePacketSize += 19; //the size before MMENTRY
  1728. DEBUG_INFO("QCA7000 [TX]CM_SET_KEY_REQ\n");
  1729. DEBUG_INFO("SendSetKey: send size: %d\n", sendto(socketFd.Raw, &qcaInfo.SendMmePacket, qcaInfo.SendMmePacketSize, 0, (struct sockaddr*)&qcaInfo.DestSocketAddress, sizeof(struct sockaddr_ll)));
  1730. Update_V2G_Status(CM_SET_KEY_REQ);
  1731. return 0;
  1732. }
  1733. /**
  1734. *
  1735. * @return
  1736. */
  1737. int GetQca7kMac()
  1738. {
  1739. struct QcaVendorMmeHeader SendPacket;
  1740. DEBUG_INFO("Req for QCA7K MacAddr\n");
  1741. memset(&SendPacket, 0, sizeof(struct QcaVendorMmeHeader));
  1742. memset(SendPacket.ODA, 0xFF, 6); //broadcast
  1743. memcpy(SendPacket.OSA, macAddr.eth1, 6);
  1744. SendPacket.MTYPE = htons(EtherType_HomePlug);
  1745. SendPacket.MMV = 0x00;
  1746. SendPacket.MMTYPE = MMTYPE_VENDOR_VS_NW_INFO;
  1747. SendPacket.OUI[0] = 0x00;
  1748. SendPacket.OUI[1] = 0xB0;
  1749. SendPacket.OUI[2] = 0x52;
  1750. DEBUG_INFO("GetQca7kMac: send size: %d\n", sendto(socketFd.Raw, &SendPacket, 20, 0, (struct sockaddr*)&qcaInfo.DestSocketAddress, sizeof(struct sockaddr_ll)));
  1751. return 0;
  1752. }
  1753. /**
  1754. *
  1755. * @param ptr
  1756. * @param size
  1757. * @return
  1758. */
  1759. int Array_Check_All_Zero(unsigned char *ptr, int size)
  1760. {
  1761. int result = TRUE;
  1762. int i = 0;
  1763. for (i = 0; i < size; i++)
  1764. {
  1765. if (ptr[i] != 0)
  1766. {
  1767. result = FALSE;
  1768. break;
  1769. }
  1770. }
  1771. return result;
  1772. }
  1773. /**
  1774. *
  1775. * @param ptrA
  1776. * @param ptrB
  1777. * @param size
  1778. * @return
  1779. */
  1780. int Array_Compare_Identity(unsigned char *ptrA, unsigned char *ptrB, int size)
  1781. {
  1782. int result = TRUE;
  1783. int i = 0;
  1784. for (i = 0; i < size; i++)
  1785. {
  1786. if (ptrA[i] != ptrB[i])
  1787. {
  1788. result = FALSE;
  1789. #if 0
  1790. DEBUG_INFO("[Array_Compare_Identity]%02X%02X%02X%02X%02X%02X,%02X%02X%02X%02X%02X%02X(%d)\n",
  1791. ptrA[0], ptrA[1], ptrA[2], ptrA[3], ptrA[4], ptrA[5],
  1792. ptrB[0], ptrB[1], ptrB[2], ptrB[3], ptrB[4], ptrB[5],
  1793. result);
  1794. #endif
  1795. break;
  1796. }
  1797. }
  1798. return result;
  1799. }
  1800. /**
  1801. *
  1802. * @param evcc
  1803. * @param EvMac_in
  1804. * @return
  1805. */
  1806. int SLAC_DB_Search_EvMac_idx(struct EVCC_SLAC_DATA_TYPE *evcc, unsigned char *EvMac_in)
  1807. {
  1808. int idx = -1;
  1809. int i = 0;
  1810. if (evcc->arrayLen == 0)
  1811. {
  1812. //printf("[SLAC_DB_Search_EvMac_idx]arrayLen is empty (%d)\n", evcc->arrayLen);
  1813. //no need to search
  1814. }
  1815. else if (evcc->arrayLen > EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE)
  1816. {
  1817. //error
  1818. DEBUG_ERROR("DB length(%d) > %d\n", evcc->arrayLen, EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE);
  1819. evcc->arrayLen = 0; //reset
  1820. }
  1821. else
  1822. {
  1823. //start searching
  1824. for (i = 0; i < evcc->arrayLen; i++)
  1825. {
  1826. //printf("[SLAC_DB_Search_EvMac_idx]checking DB[%d]...\n", i);
  1827. if (Array_Compare_Identity(evcc->array[i].EvMac, EvMac_in, SLAC_EVMAC_LENGTH) == TRUE)
  1828. {
  1829. //printf("[SLAC_DB_Search_EvMac_idx]identical at DB[%d]...\n", i);
  1830. idx = i;
  1831. break;
  1832. }
  1833. }
  1834. }
  1835. //printf("[SLAC_DB_Search_EvMac_idx]return = %d\n", idx);
  1836. return idx;
  1837. }
  1838. /**
  1839. *
  1840. * @param evcc
  1841. * @param EvMac_in
  1842. * @param RunID_in
  1843. * @return
  1844. */
  1845. int SLAC_DB_Check_EvMac_RunID_Matching(struct EVCC_SLAC_DATA_TYPE *evcc, unsigned char *EvMac_in, unsigned char *RunID_in)
  1846. {
  1847. int res = FALSE;
  1848. int idx = -1;
  1849. idx = SLAC_DB_Search_EvMac_idx(evcc, EvMac_in);
  1850. if (idx >= 0)
  1851. {
  1852. res = Array_Compare_Identity(evcc->array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH);
  1853. }
  1854. else
  1855. {
  1856. //not found the EvMac data in DB
  1857. res = FALSE;
  1858. }
  1859. return res;
  1860. }
  1861. /**
  1862. *
  1863. * @param EvMac_in
  1864. * @param RunID_in
  1865. * @return
  1866. */
  1867. int SLAC_DB_Add(unsigned char *EvMac_in, unsigned char *RunID_in)
  1868. {
  1869. int idx = -1;
  1870. //Search if this EvMac and RunID already exists
  1871. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  1872. if (idx < 0) //not exist, yet.
  1873. {
  1874. if (SLAC_INFO.arrayLen < EVCC_SLAC_DATA_ARRAY_TYPE_ARRAY_SIZE) //20
  1875. {
  1876. DEBUG_PRINTF_EVCOMM_DETAIL("data does not exist => added to %d-th\n", SLAC_INFO.arrayLen);
  1877. if (SLAC_INFO.arrayLen >= 0)
  1878. {
  1879. memset(&SLAC_INFO.array[SLAC_INFO.arrayLen], 0, sizeof(struct EVCC_SLAC_DATA_ARRAY_TYPE));
  1880. memcpy(SLAC_INFO.array[SLAC_INFO.arrayLen].EvMac, EvMac_in, SLAC_EVMAC_LENGTH);
  1881. memcpy(SLAC_INFO.array[SLAC_INFO.arrayLen].RunID, RunID_in, SLAC_RUNID_LENGTH);
  1882. idx = SLAC_INFO.arrayLen;
  1883. SLAC_INFO.arrayLen++;
  1884. }
  1885. else
  1886. {
  1887. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR]arrayLen: unexpected(%d)\n", SLAC_INFO.arrayLen);
  1888. SLAC_INFO.arrayLen = 0;
  1889. }
  1890. }
  1891. else
  1892. {
  1893. //DB is full
  1894. DEBUG_PRINTF_EVCOMM_DETAIL("DB is full(%d) => bypass\n", SLAC_INFO.arrayLen);
  1895. }
  1896. }
  1897. else
  1898. {
  1899. #if 0
  1900. DEBUG_INFO("EvMac: existed (%d)\n", idx);
  1901. #endif
  1902. //Check RunID
  1903. if (Array_Compare_Identity(SLAC_INFO.array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH) == TRUE)
  1904. {
  1905. //RunID is the same
  1906. //DEBUG_INFO("RunID: same\n");
  1907. }
  1908. else
  1909. {
  1910. DEBUG_INFO("RunID: diff\n");
  1911. }
  1912. //Reset all corresponding parameters
  1913. #if 0
  1914. DEBUG_INFO("EvMac: reset para(%d)\n", idx);
  1915. #endif
  1916. memset(&SLAC_INFO.array[idx], 0, sizeof(struct EVCC_SLAC_DATA_ARRAY_TYPE));
  1917. memcpy(SLAC_INFO.array[idx].EvMac, EvMac_in, SLAC_EVMAC_LENGTH);
  1918. memcpy(SLAC_INFO.array[idx].RunID, RunID_in, SLAC_RUNID_LENGTH);
  1919. }
  1920. return idx;
  1921. }
  1922. /**
  1923. *
  1924. * @return
  1925. */
  1926. int SLAC_DB_Reset()
  1927. {
  1928. memset(&SLAC_INFO, 0, sizeof(struct EVCC_SLAC_DATA_TYPE));
  1929. DEBUG_INFO("DONE\n");
  1930. return 0;
  1931. }
  1932. /**
  1933. *
  1934. * @param Buffer
  1935. * @param DataLength
  1936. * @return
  1937. */
  1938. int MmeProcess(unsigned char *Buffer, int DataLength)
  1939. {
  1940. //struct ethhdr *EthPacket;
  1941. struct MmeHeader *MmePacket;
  1942. static unsigned char counter;
  1943. unsigned char state = 0;
  1944. unsigned char tmpBuf[2048]={0};
  1945. int Rtn = 0;
  1946. int idx = 0;
  1947. unsigned char *EvMac_in;
  1948. unsigned char *RunID_in;
  1949. MmePacket = (struct MmeHeader *)Buffer;
  1950. state = Get_V2G_Status();
  1951. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  1952. DEBUG_PRINTF_EVCOMM_DETAIL("******* Received MME Packet *******\n");
  1953. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  1954. DEBUG_PRINTF_EVCOMM_DETAIL("DataLength=%d\n",DataLength);
  1955. DEBUG_PRINTF_EVCOMM_DETAIL("ODA: %02x:%02x:%02x:%02x:%02x:%02x\n", //Destination MAC Address
  1956. MmePacket->ODA[0], MmePacket->ODA[1],
  1957. MmePacket->ODA[2], MmePacket->ODA[3],
  1958. MmePacket->ODA[4], MmePacket->ODA[5]);
  1959. DEBUG_PRINTF_EVCOMM_DETAIL("OSA: %02x:%02x:%02x:%02x:%02x:%02x\n", //Source MAC Address (EV MAC)
  1960. MmePacket->OSA[0], MmePacket->OSA[1],
  1961. MmePacket->OSA[2], MmePacket->OSA[3],
  1962. MmePacket->OSA[4], MmePacket->OSA[5]);
  1963. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(MmePacket->MTYPE));
  1964. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", MmePacket->MMV);
  1965. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", MmePacket->MMTYPE);
  1966. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", MmePacket->FMI[0],MmePacket->FMI[1]);
  1967. #if (SLAC_FIRST_RESPONSE_METHOD == SET_NO_PWM_IF_NOT_GET_PERMISSION)
  1968. {
  1969. //Check CP as 5%
  1970. if (EVCOMM_SYS_INFO.CpState != 3 &&
  1971. EVCOMM_SYS_INFO.CpState != 4 &&
  1972. MmePacket->MMTYPE != MMTYPE_CM_SET_KEY_CNF &&
  1973. MmePacket->MMTYPE != MMTYPE_VENDOR_VS_HOST_ACTION &&
  1974. MmePacket->MMTYPE != MMTYPE_VENDOR_ATTEN_CHAR &&
  1975. MmePacket->MMTYPE != MMTYPE_VENDOR_VS_NW_INFO_CNF
  1976. )
  1977. {
  1978. //DEBUG_INFO("[SLAC]ignored(wrong CP state)\n");
  1979. return 0;
  1980. }
  1981. }
  1982. #endif
  1983. //[To-Do] Adding a pre-check filter mechanism for Source and Destination MAC Address
  1984. //check if the destination address is broadcast or EVSE itself, it can be ignore if not belong to itself.
  1985. switch(MmePacket->MMTYPE)
  1986. {
  1987. case MMTYPE_CM_SET_KEY_CNF:
  1988. {
  1989. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SET_KEY_CNF ---\n");
  1990. DEBUG_PRINTF_EVCOMM_DETAIL("Result: 0x%02x\n", MmePacket->MMENTRY[0]);
  1991. DEBUG_INFO("CM_SET_KEY_CNF (SetKey: DONE)\n");
  1992. Update_V2G_Status(CM_SET_KEY_CNF);
  1993. break;
  1994. }
  1995. case MMTYPE_CM_SLAC_PARM_REQ:
  1996. {
  1997. //Check QCA7000 status
  1998. if (EVCOMM_SYS_INFO.QCA7K_SetKeyDone == FALSE)
  1999. {
  2000. //DEBUG_WARN("[SLAC][RX]CM_SLAC_PARM_REQ: ignored(QCA7K init...)\n");
  2001. break;
  2002. }
  2003. //Check error state
  2004. state = Get_V2G_Status();
  2005. if (state == Performance_Timeout || //253
  2006. state == Sequence_Timeout || //254
  2007. state == Other_Fault) //255
  2008. {
  2009. DEBUG_WARN("CM_SLAC_PARM_REQ: ignored(in error state)\n");
  2010. break;
  2011. }
  2012. //Printing EV MAC Address
  2013. DEBUG_INFO("CM_SLAC_PARM_REQ\n");
  2014. //Avoid Coupled SLAC_PARM_REQ
  2015. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2016. {
  2017. #if 0
  2018. DEBUG_WARN("[SLAC][RX]CM_SLAC_PARM_REQ: ignored(coupled SLAC,%d)\n", CheckConnectorPlugIn());
  2019. #endif
  2020. break;
  2021. }
  2022. //[TC_SECC_VTB_CmSlacParm_003] SECURITY_TYPE needs to be 0x00 (no security)
  2023. if (MmePacket->MMENTRY[1] != 0)
  2024. {
  2025. DEBUG_INFO("CM_SLAC_PARM_REQ: ignored(invalid SECURITY_TYPE,%d)\n", //Source MAC Address (EV MAC)
  2026. MmePacket->MMENTRY[1]);
  2027. break;
  2028. }
  2029. //=================== Legal CM_SLAC_PARM_REQ Zone =================
  2030. Update_V2G_Status(CM_SLAC_PARM_REQ);
  2031. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_RX_CM_SLAC_PARM_REQ)
  2032. {
  2033. //Once receiving CM_SLAC_PARM_REQ, set PWM as 5%
  2034. SwitchCpStateE(DISABLE);
  2035. OutputCpPwmDuty(5);
  2036. }
  2037. #endif
  2038. //Checking if this SLAC Req comes before 5% PWM (for 15118)
  2039. if(ShmInternalComm->ChargingPermission == FALSE)
  2040. {
  2041. //Sniffer_Tcpdump(ENABLE);
  2042. DEBUG_INFO("Check Permission: %d (SLAC first => START)\n", ShmInternalComm->ChargingPermission);
  2043. }
  2044. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SLAC_PARM_REQ ---\n");
  2045. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2046. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2047. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2048. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3],
  2049. MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2050. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7],
  2051. MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2052. DEBUG_PRINTF_EVCOMM_DETAIL("CipherSuiteSetSize: 0x%x\n", MmePacket->MMENTRY[10]);
  2053. DEBUG_PRINTF_EVCOMM_DETAIL("CipherSuite [1]: 0x%x,0x%x\n", MmePacket->MMENTRY[11], MmePacket->MMENTRY[12]);
  2054. //Saving each CM_SLAC_PARM_REQ data into EVCC database
  2055. EvMac_in = &MmePacket->OSA[0];
  2056. RunID_in = &MmePacket->MMENTRY[2];
  2057. idx = SLAC_DB_Add(EvMac_in, RunID_in); //(EvMac, RunID)
  2058. if (idx < 0)
  2059. {
  2060. DEBUG_WARN("DB is full or errors occour(%d) => ignore\n", idx);
  2061. break;
  2062. }
  2063. //Select the 1st EV MAC address
  2064. if (SLAC_INFO.arrayLen == 1) //1st Req
  2065. {
  2066. DEBUG_INFO("CM_SLAC_PARM_REQ[%d](%02X:%02X:%02X:%02X:%02X:%02X, %02X%02X%02X%02X%02X%02X%02X%02X):selected\n",
  2067. (idx + 1),
  2068. MmePacket->OSA[0], MmePacket->OSA[1], MmePacket->OSA[2], MmePacket->OSA[3], MmePacket->OSA[4], MmePacket->OSA[5],
  2069. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3], MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2070. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7], MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2071. }
  2072. else //2nd Req
  2073. {
  2074. DEBUG_INFO("CM_SLAC_PARM_REQ[%d](%02X:%02X:%02X:%02X:%02X:%02X, %02X%02X%02X%02X%02X%02X%02X%02X):not selected\n",
  2075. (idx + 1),
  2076. MmePacket->OSA[0], MmePacket->OSA[1], MmePacket->OSA[2], MmePacket->OSA[3], MmePacket->OSA[4], MmePacket->OSA[5],
  2077. MmePacket->MMENTRY[2], MmePacket->MMENTRY[3], MmePacket->MMENTRY[4], MmePacket->MMENTRY[5],
  2078. MmePacket->MMENTRY[6], MmePacket->MMENTRY[7], MmePacket->MMENTRY[8], MmePacket->MMENTRY[9]);
  2079. }
  2080. memcpy(macAddr.evcc, MmePacket->OSA, sizeof(macAddr.evcc));
  2081. memcpy(qcaInfo.DestSocketAddress.sll_addr, MmePacket->OSA, SLAC_EVMAC_LENGTH);
  2082. memcpy(qcaInfo.SlacRunId, MmePacket->MMENTRY + 2, SLAC_RUNID_LENGTH);
  2083. memset(&qcaInfo.SendMmePacket, 0, sizeof(struct MmeHeader));
  2084. memcpy(qcaInfo.SendMmePacket.ODA, MmePacket->OSA, 6);
  2085. memcpy(qcaInfo.SendMmePacket.OSA, macAddr.eth1, 6);
  2086. qcaInfo.SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  2087. qcaInfo.SendMmePacket.MMV = MmePacket->MMV;
  2088. qcaInfo.SendMmePacket.MMTYPE = MMTYPE_CM_SLAC_PARM_CNF;
  2089. qcaInfo.SendMmePacket.FMI[0] = qcaInfo.SendMmePacket.FMI[1] = 0;
  2090. qcaInfo.SendMmePacketSize = 0;
  2091. memset(qcaInfo.SendMmePacket.MMENTRY, 0xFF, 6); //M-SOUND_TARGET(6-byte:0xFFFFFFFFFFFF): Fixed value indicating that M-Sounds to be sent as Ethernet broadcast
  2092. qcaInfo.SendMmePacketSize += 6;
  2093. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = C_EV_match_MNBC; //NUM_SOUNDS(0x0A): Number of M-Sounds to be transmitted by the EV GP Station during the SLAC process
  2094. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 6; //Time_Out(0x06): unit = 100ms
  2095. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.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
  2096. memcpy(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, SLAC_INFO.array[idx].EvMac, 6); //FORWARDING_STA(MAC Address of the EV HLE)
  2097. qcaInfo.SendMmePacketSize += 6;
  2098. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00): 0x00(PEV-EVSE Association), 0x01-0xFF(Reserved)
  2099. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00): 0x00(No Security), 0x01(Public Key Signature), 0x02-0xFF(Reserved)
  2100. memcpy(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, qcaInfo.SlacRunId, SLAC_RUNID_LENGTH); //RunID (8-byte)
  2101. qcaInfo.SendMmePacketSize += SLAC_RUNID_LENGTH;
  2102. qcaInfo.SendMmePacketSize += 19; //the size before MMENTRY
  2103. DEBUG_PRINTF_EVCOMM_DETAIL("***** Response MME Packet *****\n");
  2104. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2105. qcaInfo.SendMmePacket.ODA[0],qcaInfo.SendMmePacket.ODA[1],qcaInfo.SendMmePacket.ODA[2],qcaInfo.SendMmePacket.ODA[3],qcaInfo.SendMmePacket.ODA[4],qcaInfo.SendMmePacket.ODA[5]);
  2106. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2107. qcaInfo.SendMmePacket.OSA[0],qcaInfo.SendMmePacket.OSA[1],qcaInfo.SendMmePacket.OSA[2],qcaInfo.SendMmePacket.OSA[3],qcaInfo.SendMmePacket.OSA[4],qcaInfo.SendMmePacket.OSA[5]);
  2108. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(qcaInfo.SendMmePacket.MTYPE));
  2109. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", qcaInfo.SendMmePacket.MMV);
  2110. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", qcaInfo.SendMmePacket.MMTYPE);
  2111. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", qcaInfo.SendMmePacket.FMI[0],qcaInfo.SendMmePacket.FMI[1]);
  2112. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_PARM_CNF ---\n");
  2113. DEBUG_PRINTF_EVCOMM_DETAIL("M-SOUND_TARGET: %02x:%02x:%02x:%02x:%02x:%02x\n",
  2114. qcaInfo.SendMmePacket.MMENTRY[0],qcaInfo.SendMmePacket.MMENTRY[1],qcaInfo.SendMmePacket.MMENTRY[2],qcaInfo.SendMmePacket.MMENTRY[3],qcaInfo.SendMmePacket.MMENTRY[4],qcaInfo.SendMmePacket.MMENTRY[5]);
  2115. DEBUG_PRINTF_EVCOMM_DETAIL("NUM_SOUNDS: 0x%x\n", qcaInfo.SendMmePacket.MMENTRY[6]);
  2116. DEBUG_PRINTF_EVCOMM_DETAIL("Time_Out: 0x%x\n", qcaInfo.SendMmePacket.MMENTRY[7]);
  2117. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_TYPE: 0x%x\n", qcaInfo.SendMmePacket.MMENTRY[8]);
  2118. DEBUG_PRINTF_EVCOMM_DETAIL("M-FORWARDING_STA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2119. qcaInfo.SendMmePacket.MMENTRY[9],qcaInfo.SendMmePacket.MMENTRY[10],qcaInfo.SendMmePacket.MMENTRY[11],qcaInfo.SendMmePacket.MMENTRY[12],qcaInfo.SendMmePacket.MMENTRY[13],qcaInfo.SendMmePacket.MMENTRY[14]);
  2120. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", qcaInfo.SendMmePacket.MMENTRY[15]);
  2121. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", qcaInfo.SendMmePacket.MMENTRY[16]);
  2122. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2123. qcaInfo.SendMmePacket.MMENTRY[17],qcaInfo.SendMmePacket.MMENTRY[18],qcaInfo.SendMmePacket.MMENTRY[19],qcaInfo.SendMmePacket.MMENTRY[20],qcaInfo.SendMmePacket.MMENTRY[21],qcaInfo.SendMmePacket.MMENTRY[22],qcaInfo.SendMmePacket.MMENTRY[23],qcaInfo.SendMmePacket.MMENTRY[24]);
  2124. Update_V2G_Status(CM_SLAC_PARM_CONF);
  2125. Rtn = sendto(socketFd.Raw, &qcaInfo.SendMmePacket, qcaInfo.SendMmePacketSize, 0, (struct sockaddr*)&qcaInfo.DestSocketAddress, sizeof(struct sockaddr_ll));
  2126. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d, Rtn=%d\n", qcaInfo.SendMmePacketSize,Rtn);
  2127. ftime(&timerStart.SeqStart);
  2128. counter = 0;
  2129. break;
  2130. }
  2131. case MMTYPE_CM_START_ATTEN_CHAR_IND:
  2132. {
  2133. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2134. {
  2135. DEBUG_INFO("CM_START_ATTEN_CHAR_IND: ignored(time up)\n");
  2136. break;
  2137. }
  2138. //Avoid Coupled CM_START_ATTEN_CHAR_IND
  2139. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2140. {
  2141. DEBUG_INFO("CM_START_ATTEN_CHAR_IND: ignored(coupled SLAC,%d)\n", CheckConnectorPlugIn());
  2142. break;
  2143. }
  2144. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_START_ATTEN_CHAR_IND (counter : %d/3 ) ---\n", counter);
  2145. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2146. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2147. DEBUG_PRINTF_EVCOMM_DETAIL("NUM_SOUNDS: 0x%x\n", MmePacket->MMENTRY[2]);
  2148. DEBUG_PRINTF_EVCOMM_DETAIL("Time_Out 0x%x\n", MmePacket->MMENTRY[3]);
  2149. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_TYPE 0x%x\n", MmePacket->MMENTRY[4]); //Fixed value (0x01) indicating ��other Green PHY station��
  2150. DEBUG_PRINTF_EVCOMM_DETAIL("FORWARDING_STA: %02x:%02x:%02x:%02x:%02x:%02x\n", MmePacket->MMENTRY[5],MmePacket->MMENTRY[6],MmePacket->MMENTRY[7],MmePacket->MMENTRY[8], MmePacket->MMENTRY[9],MmePacket->MMENTRY[10]);
  2151. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n", MmePacket->MMENTRY[11],MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],MmePacket->MMENTRY[14], MmePacket->MMENTRY[15],MmePacket->MMENTRY[16],MmePacket->MMENTRY[17],MmePacket->MMENTRY[18]);
  2152. EvMac_in = &MmePacket->OSA[0];
  2153. RunID_in = &MmePacket->MMENTRY[11];
  2154. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2155. if (idx >= 0)
  2156. {
  2157. Update_V2G_Status(CM_START_ATTEN_CHAR_IND);
  2158. counter++;
  2159. //[TC_SECC_VTB_AttenuationCharacterization_013]
  2160. if (MmePacket->MMENTRY[0] != 0) //APPLICATION_TYPE must be 0x00(EV-EVSE Matching)
  2161. {
  2162. DEBUG_INFO("[CM_START_ATTEN_CHAR_IND]APPLICATION_TYPE(%d): invalid => ignore Req\n", MmePacket->MMENTRY[0]);
  2163. break;
  2164. }
  2165. //[TC_SECC_VTB_AttenuationCharacterization_014]
  2166. if (MmePacket->MMENTRY[1] != 0) //SECURITY_TYPE must be 0x00(No Security)
  2167. {
  2168. DEBUG_INFO("[CM_START_ATTEN_CHAR_IND]SECURITY_TYPE(%d): invalid => ignore Req\n", MmePacket->MMENTRY[1]);
  2169. break;
  2170. }
  2171. //[TC_SECC_VTB_AttenuationCharacterization_017]
  2172. if (MmePacket->MMENTRY[4] != 0x01) //RESP_TYPE must be 0x01(Send to another GP STA(EV))
  2173. {
  2174. DEBUG_INFO("[CM_START_ATTEN_CHAR_IND]RESP_TYPE(%d): invalid => ignore Req\n", MmePacket->MMENTRY[4]);
  2175. break;
  2176. }
  2177. //[TC_SECC_VTB_AttenuationCharacterization_021]
  2178. //FORWARDING_STA: MAC Address of the EV HLE (station which the measurement results shall be sent to)
  2179. if (Array_Compare_Identity(EvMac_in, &MmePacket->MMENTRY[5], SLAC_EVMAC_LENGTH) == FALSE)
  2180. {
  2181. DEBUG_INFO("[CM_START_ATTEN_CHAR_IND]FORWARDING_STA(%02X:%02X:%02X:%02X:%02X:%02X): invalid => ignore Req\n",
  2182. MmePacket->MMENTRY[5], MmePacket->MMENTRY[6],
  2183. MmePacket->MMENTRY[7], MmePacket->MMENTRY[8],
  2184. MmePacket->MMENTRY[9], MmePacket->MMENTRY[10]);
  2185. SLAC_INFO.array[idx].StartAttenCharErr = TRUE;
  2186. break;
  2187. }
  2188. //Check RunID
  2189. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE)
  2190. {
  2191. SLAC_INFO.array[idx].MnbcSoundNum = MmePacket->MMENTRY[2];
  2192. SLAC_INFO.array[idx].StartAttenCharCnt++;
  2193. if (SLAC_INFO.array[idx].StartAttenCharCnt == 1)
  2194. {
  2195. memset(SLAC_INFO.array[idx].AAG, 0, sizeof(SLAC_INFO.array[idx].AAG));
  2196. SLAC_INFO.array[idx].AttenProfileCnt = 0;
  2197. ftime(&timerStart.SeqStart); //start TT_EVSE_match_MNBC
  2198. }
  2199. else if (SLAC_INFO.array[idx].StartAttenCharCnt >= 3)
  2200. {
  2201. DEBUG_INFO("[CM_START_ATTEN_CHAR_IND]counter(%d): unexpected \n", SLAC_INFO.array[idx].StartAttenCharCnt);
  2202. }
  2203. else
  2204. {
  2205. //null
  2206. }
  2207. }
  2208. else
  2209. {
  2210. //This RunID is not matched with this EvMac,
  2211. //or this RunID is not found in DB.
  2212. DEBUG_INFO("[CM_START_ATTEN_CHAR_IND]EvMac-RunID: unmatched => ignore Req\n");
  2213. //Response: ignore
  2214. }
  2215. }
  2216. else
  2217. {
  2218. //this msg source is not in database
  2219. //ignore
  2220. }
  2221. break;
  2222. }
  2223. case MMTYPE_CM_MNBC_SOUND_IND:
  2224. {
  2225. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2226. {
  2227. DEBUG_INFO("CM_MNBC_SOUND_IND: ignored(timeup)\n");
  2228. break;
  2229. }
  2230. //Avoid Coupled CM_MNBC_SOUND_IND
  2231. if (CheckConnectorPlugIn() == FALSE) //12V(State 1)
  2232. {
  2233. DEBUG_INFO("CM_MNBC_SOUND_IND: ignored(coupled SLAC,%d)\n", CheckConnectorPlugIn());
  2234. break;
  2235. }
  2236. if(V2gFlowStatus == CM_START_ATTEN_CHAR_IND)
  2237. {
  2238. counter = 0;
  2239. }
  2240. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_MNBC_SOUND_IND (counter : %d/%d) ---\n", counter, SLAC_INFO.array[idx].MnbcSoundNum);
  2241. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2242. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2243. DEBUG_PRINTF_EVCOMM_DETAIL("SenderID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2244. MmePacket->MMENTRY[2],MmePacket->MMENTRY[3],MmePacket->MMENTRY[4],MmePacket->MMENTRY[5],
  2245. MmePacket->MMENTRY[6],MmePacket->MMENTRY[7],MmePacket->MMENTRY[8],MmePacket->MMENTRY[9],
  2246. MmePacket->MMENTRY[10],MmePacket->MMENTRY[11],MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],
  2247. MmePacket->MMENTRY[14],MmePacket->MMENTRY[15],MmePacket->MMENTRY[16],MmePacket->MMENTRY[17],
  2248. MmePacket->MMENTRY[18]);
  2249. DEBUG_PRINTF_EVCOMM_DETAIL("Cnt: 0x%x\n", MmePacket->MMENTRY[19]);
  2250. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2251. MmePacket->MMENTRY[20],MmePacket->MMENTRY[21],MmePacket->MMENTRY[22],MmePacket->MMENTRY[23],
  2252. MmePacket->MMENTRY[24],MmePacket->MMENTRY[25],MmePacket->MMENTRY[26],MmePacket->MMENTRY[27]);
  2253. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2254. MmePacket->MMENTRY[28],MmePacket->MMENTRY[29],MmePacket->MMENTRY[30],MmePacket->MMENTRY[31],
  2255. MmePacket->MMENTRY[32],MmePacket->MMENTRY[33],MmePacket->MMENTRY[34],MmePacket->MMENTRY[35]);
  2256. DEBUG_PRINTF_EVCOMM_DETAIL("Rnd: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
  2257. MmePacket->MMENTRY[36],MmePacket->MMENTRY[37],MmePacket->MMENTRY[38],MmePacket->MMENTRY[39],
  2258. MmePacket->MMENTRY[40],MmePacket->MMENTRY[41],MmePacket->MMENTRY[42],MmePacket->MMENTRY[43],
  2259. MmePacket->MMENTRY[44],MmePacket->MMENTRY[45],MmePacket->MMENTRY[46],MmePacket->MMENTRY[47],
  2260. MmePacket->MMENTRY[48],MmePacket->MMENTRY[49],MmePacket->MMENTRY[50],MmePacket->MMENTRY[51]);
  2261. EvMac_in = &MmePacket->OSA[0];
  2262. RunID_in = &MmePacket->MMENTRY[20];
  2263. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2264. if (idx >= 0)
  2265. {
  2266. Update_V2G_Status(CM_MNBC_SOUND_IND);
  2267. counter++;
  2268. //Check for RunID
  2269. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE)
  2270. {
  2271. SLAC_INFO.array[idx].MnbcSoundCnt++;
  2272. }
  2273. else
  2274. {
  2275. //RunID is not matched or does not exist.
  2276. DEBUG_INFO("CM_MNBC_SOUND_IND]EvMac-RunID: unmatched\n");
  2277. }
  2278. }
  2279. else
  2280. {
  2281. //ignore
  2282. DEBUG_INFO("CM_MNBC_SOUND_IND]EvMac does not exist\n");
  2283. }
  2284. break;
  2285. }
  2286. case MMTYPE_CM_ATTEN_PROFILE_IND:
  2287. {
  2288. if(V2gFlowStatus >= CM_ATTEN_CHAR_IND)
  2289. {
  2290. DEBUG_INFO("CM_ATTEN_PROFILE_IND: ignore(timeup)\n");
  2291. break;
  2292. }
  2293. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_ATTEN_PROFILE_IND (counter : %d/%d) ---\n", counter, SLAC_INFO.array[idx].MnbcSoundNum);
  2294. DEBUG_PRINTF_EVCOMM_DETAIL("EV MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", MmePacket->MMENTRY[0], MmePacket->MMENTRY[1], MmePacket->MMENTRY[2], MmePacket->MMENTRY[3], MmePacket->MMENTRY[4], MmePacket->MMENTRY[5]);
  2295. DEBUG_PRINTF_EVCOMM_DETAIL("NumGroups: 0x%x\n", MmePacket->MMENTRY[6]); //NumGroups, Number of AAG Groups (ex: 0x3A = 58)
  2296. //NumGroups: Number of OFDM carrier groups used for the SLAC signal characterization.
  2297. //AAG[i]: Average Attenuation of Group i (i = 1 ~ 58)
  2298. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: 0x%x\n", MmePacket->MMENTRY[7]);
  2299. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  2300. for(Rtn = 0; Rtn < MmePacket->MMENTRY[6]; Rtn++)
  2301. {
  2302. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, MmePacket->MMENTRY[8 + Rtn]);
  2303. }
  2304. DEBUG_PRINTF_EVCOMM_DETAIL("AAG: %s\n", tmpBuf);
  2305. EvMac_in = &MmePacket->MMENTRY[0]; //[CAUTION] The EvMac address is not &MmePacket->OSA[0]
  2306. //[CAUTION] There is no RunID information of CM_ATTEN_PROFILE_IND
  2307. // packet, which means SECC cannot use the RunID to
  2308. // distinguish those SLAC request with identical EvMac
  2309. // but with different RunID.
  2310. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2311. if (idx >= 0)
  2312. {
  2313. SLAC_INFO.array[idx].AttenProfileCnt++;
  2314. SLAC_INFO.array[idx].AagGroupsNum = MmePacket->MMENTRY[6];
  2315. ////NumGroups, Number of AAG Groups (ex: 0x3A = 58)
  2316. for(Rtn = 0; Rtn < SLAC_INFO.array[idx].AagGroupsNum; Rtn++) //ex:58
  2317. {
  2318. SLAC_INFO.array[idx].AAG[Rtn] += MmePacket->MMENTRY[8 + Rtn];
  2319. }
  2320. Update_V2G_Status(CM_MNBC_SOUND_IND);
  2321. break;
  2322. }
  2323. else
  2324. {
  2325. //The EvMac is not in the database
  2326. //ignore
  2327. DEBUG_INFO("[CM_ATTEN_PROFILE_IND]EvMac(%02X%02X%02X%02X%02X%02X): not exist => ignore\n", EvMac_in[0], EvMac_in[1], EvMac_in[2], EvMac_in[3], EvMac_in[4], EvMac_in[5]);
  2328. break;
  2329. }
  2330. }
  2331. case MMTYPE_CM_ATTEN_CHAR_RSP:
  2332. {
  2333. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_ATTEN_CHAR_RSP ---\n");
  2334. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2335. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2336. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n", MmePacket->MMENTRY[2],MmePacket->MMENTRY[3],MmePacket->MMENTRY[4],MmePacket->MMENTRY[5], MmePacket->MMENTRY[6],MmePacket->MMENTRY[7]);
  2337. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n", MmePacket->MMENTRY[8],MmePacket->MMENTRY[9],MmePacket->MMENTRY[10],MmePacket->MMENTRY[11], MmePacket->MMENTRY[12],MmePacket->MMENTRY[13],MmePacket->MMENTRY[14],MmePacket->MMENTRY[15]);
  2338. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  2339. for(Rtn = 0; Rtn < 17; Rtn++)
  2340. {
  2341. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, MmePacket->MMENTRY[16 + Rtn]);
  2342. }
  2343. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ID: %s\n", tmpBuf);
  2344. ;
  2345. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  2346. for(Rtn = 0; Rtn < 17; Rtn++)
  2347. {
  2348. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, MmePacket->MMENTRY[33 + Rtn]);
  2349. }
  2350. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_ID: %s\n", tmpBuf);
  2351. DEBUG_PRINTF_EVCOMM_DETAIL("Result: 0x%x\n", MmePacket->MMENTRY[50]); //Fixed value of 0x00 indicates a successful SLAC process
  2352. //Check ODA (Destination Address)
  2353. if (Array_Compare_Identity(macAddr.eth1, MmePacket->ODA, SLAC_EVSE_MAC_LENGTH) == FALSE)
  2354. {
  2355. DEBUG_INFO("[CM_ATTEN_CHAR_RSP]wrong ODA: ignore\n");
  2356. break;
  2357. }
  2358. EvMac_in = &MmePacket->OSA[0];
  2359. RunID_in = &MmePacket->MMENTRY[8];
  2360. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2361. //Check Parameters
  2362. if ((MmePacket->MMENTRY[0] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_005] applicationType must be 0x00
  2363. (MmePacket->MMENTRY[1] != 0) || //[TC_SECC_VTB_AttenuationCharacterization_006] securityType must be 0x00
  2364. (SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in) < 0) || //[TC_SECC_VTB_AttenuationCharacterization_007] sourceAddress cannot not be all zero
  2365. (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_008] check for invalid runID
  2366. (Array_Check_All_Zero(&MmePacket->MMENTRY[16], 17) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_009] SourceID(17 bytes) must be all zero.
  2367. (Array_Check_All_Zero(&MmePacket->MMENTRY[33], 17) == FALSE) || //[TC_SECC_VTB_AttenuationCharacterization_009] RESP_ID (17 bytes) must be all zero.
  2368. (MmePacket->MMENTRY[50] != 0) //Result: must be 0x00(Success)
  2369. )
  2370. {
  2371. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry++;
  2372. DEBUG_WARN("[SLAC][RX]CM_ATTEN_CHAR_RSP:invalid para(%d,%d,%d,%d,%d) => %d-th retry\n",
  2373. MmePacket->MMENTRY[0], //applicationType must be 0x00
  2374. MmePacket->MMENTRY[1], //securityType must be 0x00
  2375. SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in),
  2376. SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in),
  2377. MmePacket->MMENTRY[50],
  2378. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry
  2379. );
  2380. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry <= 2) //limited to 2 retries
  2381. {
  2382. Update_V2G_Status(CM_MNBC_SOUND_IND);
  2383. ftime(&timerStart.SeqStart);
  2384. break;
  2385. }
  2386. else
  2387. {
  2388. DEBUG_ERROR("[SLAC][RX]CM_ATTEN_CHAR_RSP:invalid para => 2-retry fail => End_Process\n");
  2389. Update_V2G_Status(Other_Fault);
  2390. break;
  2391. }
  2392. }
  2393. else
  2394. {
  2395. //The CM_ATTEN_CHAR_IND is legal
  2396. SLAC_INFO.array[idx].AttenCharRspCnt++;
  2397. DEBUG_INFO("[SLAC][RX]CM_ATTEN_CHAR_RSP[%d]:%d-th\n", (idx + 1), SLAC_INFO.array[idx].AttenCharRspCnt);
  2398. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry = 0;
  2399. Update_V2G_Status(CM_ATTEN_CHAR_RSP);
  2400. ftime(&timerStart.SeqStart);
  2401. break;
  2402. }
  2403. }
  2404. case MMTYPE_CM_VALIDATE_REQ: //BCB Toggle
  2405. {
  2406. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_VALIDATE_REQ ---\n");
  2407. DEBUG_PRINTF_EVCOMM_DETAIL("Signal Type: 0x%x\n", MmePacket->MMENTRY[0]); //Fixed value (0x00) to indicate ��PEV S2 toggles on control pilot line��
  2408. 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.
  2409. 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��.
  2410. counter = 0;
  2411. EvMac_in = &MmePacket->OSA[0];
  2412. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2413. if (idx >= 0)
  2414. {
  2415. Update_V2G_Status(CM_VALIDATE_CNF);
  2416. SLAC_INFO.array[idx].ValidateReqCnt++;
  2417. DEBUG_INFO("[SLAC][RX]CM_VALIDATE_REQ[%d]:%d-th\n", (idx + 1), SLAC_INFO.array[idx].ValidateReqCnt);
  2418. //TODO:
  2419. // 1. Protection
  2420. memset(&qcaInfo.SendMmePacket, 0, sizeof(struct MmeHeader));
  2421. memcpy(qcaInfo.SendMmePacket.ODA, EvMac_in, SLAC_EVMAC_LENGTH);
  2422. memcpy(qcaInfo.SendMmePacket.OSA, macAddr.eth1, 6);
  2423. qcaInfo.SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  2424. qcaInfo.SendMmePacket.MMV = 0x01;
  2425. qcaInfo.SendMmePacket.MMTYPE = MMTYPE_CM_VALIDATE_CNF;
  2426. qcaInfo.SendMmePacket.FMI[0] = qcaInfo.SendMmePacket.FMI[1] = 0;
  2427. qcaInfo.SendMmePacketSize = 0;
  2428. if(counter == 0)
  2429. {
  2430. //The First MMTYPE_CM_VALIDATE_REQ because Unicast
  2431. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0; //SignalType(0x00): Fixed value to indicate "EV S2 toggles on control pilot line"
  2432. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0; //ToggleNum(0x00): Fixed value. In the first VALIDATE Request-Response exchange, the ToggleNum field shall be set to zero.
  2433. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  2434. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 1; //0x01 = Ready
  2435. #else
  2436. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 4; //0x04 = Not Required
  2437. #endif
  2438. }
  2439. else
  2440. {
  2441. //The Second MMTYPE_CM_VALIDATE_REQ because Broadcast
  2442. unsigned char PreStatus = 3;
  2443. unsigned char ToggleNum = 0;
  2444. ftime(&timerStart.SeqStart);
  2445. while(1)
  2446. {
  2447. if((EVCOMM_SYS_INFO.CpState == 4) && (PreStatus == 3))
  2448. {
  2449. ToggleNum++;
  2450. PreStatus = 4;
  2451. }
  2452. else
  2453. {
  2454. PreStatus = 3;
  2455. }
  2456. ftime(&timerStart.SeqEnd);
  2457. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) >= (qcaInfo.SendMmePacket.MMENTRY[1]*100 + 100))
  2458. {
  2459. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0; //Fixed value to indicate "PEV S2 toggles on control pilot line"
  2460. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = ToggleNum;
  2461. #if (SUPPORT_BCB_TOGGLE_FUNCTION == ENABLE)
  2462. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 2; //0x02 = Success
  2463. #else
  2464. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 4; //0x04 = Not Required
  2465. #endif
  2466. break;
  2467. }
  2468. usleep(1000);
  2469. }
  2470. }
  2471. qcaInfo.SendMmePacketSize += 19; //the size before MMENTRY
  2472. Rtn = sendto(socketFd.Raw, &qcaInfo.SendMmePacket, qcaInfo.SendMmePacketSize, 0, (struct sockaddr*)&qcaInfo.DestSocketAddress, sizeof(struct sockaddr_ll));
  2473. ftime(&timerStart.SeqStart);
  2474. }
  2475. else
  2476. {
  2477. //EvMac does not exist.
  2478. //ignore
  2479. }
  2480. break;
  2481. }
  2482. case MMTYPE_CM_SLAC_MATCH_REQ:
  2483. {
  2484. char buf[512];
  2485. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_CM_SLAC_MATCH_REQ ---\n");
  2486. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", MmePacket->MMENTRY[0]);
  2487. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", MmePacket->MMENTRY[1]);
  2488. DEBUG_PRINTF_EVCOMM_DETAIL("MVFLength: 0x%x, 0x%x\n", MmePacket->MMENTRY[2],MmePacket->MMENTRY[3]); //Fixed value (0x3E) for matching
  2489. memset(buf, 0x00, ARRAY_SIZE(buf));
  2490. for(Rtn=0; Rtn<17; Rtn++)
  2491. {
  2492. sprintf(buf, "%s%02x ", buf, MmePacket->MMENTRY[4+Rtn]);
  2493. }
  2494. DEBUG_PRINTF_EVCOMM_DETAIL("PEV ID: %s\n", buf);
  2495. memset(buf, 0x00, ARRAY_SIZE(buf));
  2496. for(Rtn=0; Rtn<6; Rtn++)
  2497. {
  2498. sprintf(buf, "%s%02x ", buf, MmePacket->MMENTRY[21+Rtn]);
  2499. }
  2500. DEBUG_PRINTF_EVCOMM_DETAIL("PEV MAC: %s\n", buf);
  2501. memset(buf, 0x00, ARRAY_SIZE(buf));
  2502. for(Rtn=0; Rtn<17; Rtn++)
  2503. {
  2504. sprintf(buf, "%s%02x ", buf, MmePacket->MMENTRY[27+Rtn]);
  2505. }
  2506. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE ID: %d\n", buf);
  2507. memset(buf, 0x00, ARRAY_SIZE(buf));
  2508. for(Rtn=0; Rtn<6; Rtn++)
  2509. {
  2510. sprintf(buf, "%s%02x ", buf, MmePacket->MMENTRY[44+Rtn]);
  2511. }
  2512. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE MAC: %s\n", buf);
  2513. memset(buf, 0x00, ARRAY_SIZE(buf));
  2514. for(Rtn=0; Rtn<8; Rtn++)
  2515. {
  2516. sprintf(buf, "%s%02x ", buf, MmePacket->MMENTRY[50+Rtn]);
  2517. }
  2518. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %s\n", buf);
  2519. memset(buf, 0x00, ARRAY_SIZE(buf));
  2520. for(Rtn=0; Rtn<8; Rtn++)
  2521. {
  2522. sprintf(buf, "%s%02x ", buf, MmePacket->MMENTRY[58+Rtn]);
  2523. }
  2524. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: %s\n", buf);
  2525. //Check ODA (Destination Address)
  2526. if (Array_Compare_Identity(macAddr.eth1, MmePacket->ODA, SLAC_EVSE_MAC_LENGTH) == FALSE)
  2527. {
  2528. DEBUG_WARN("[CM_SLAC_MATCH_REQ]wrong ODA: ignore");
  2529. //break; //patch for DEKRA I2SE EVCC (another EVSE MAC Address protection comes as below.)
  2530. }
  2531. EvMac_in = &MmePacket->OSA[0];
  2532. RunID_in = &MmePacket->MMENTRY[50];
  2533. idx = SLAC_DB_Search_EvMac_idx(&SLAC_INFO, EvMac_in);
  2534. if (idx >= 0)
  2535. {
  2536. Update_V2G_Status(CM_SLAC_MATCH_REQ);
  2537. SLAC_INFO.array[idx].MatchReqNum++;
  2538. DEBUG_INFO("CM_SLAC_MATCH_REQ[%d]:%d-th\n", (idx + 1), SLAC_INFO.array[idx].MatchReqNum);
  2539. //[TC_SECC_VTB_CmSlacMatch_007] APPLICATION_TYPE must be 0x00(EV-EVSE Matching)
  2540. //[TC_SECC_VTB_CmSlacMatch_008]
  2541. if (MmePacket->MMENTRY[0] != 0)
  2542. {
  2543. DEBUG_WARN("CM_SLAC_MATCH_REQ[%d]:wrong APPLICATION_TYPE(%d)\n", (idx + 1), MmePacket->MMENTRY[0]);
  2544. break;
  2545. }
  2546. //[TC_SECC_VTB_CmSlacMatch_009] SECURITY_TYPE must be 0x00(No Security)
  2547. //[TC_SECC_VTB_CmSlacMatch_010]
  2548. if (MmePacket->MMENTRY[1] != 0)
  2549. {
  2550. DEBUG_WARN("CM_SLAC_MATCH_REQ[%d]:wrong SECURITY_TYPE(%d)\n", (idx + 1), MmePacket->MMENTRY[1]);
  2551. break;
  2552. }
  2553. //[TC_SECC_VTB_CmSlacMatch_011] MVFLength must be 0x3E(MatchVarField Length)
  2554. //[TC_SECC_VTB_CmSlacMatch_012]
  2555. if (MmePacket->MMENTRY[2] != 0x3E || MmePacket->MMENTRY[3] != 0x00)
  2556. {
  2557. DEBUG_WARN("CM_SLAC_MATCH_REQ[%d]:wrong MVFLength(%d,%d)\n", (idx + 1), MmePacket->MMENTRY[2], MmePacket->MMENTRY[3]);
  2558. break;
  2559. }
  2560. //[TC_SECC_VTB_CmSlacMatch_013] EV ID (muxt be all zero)
  2561. //[TC_SECC_VTB_CmSlacMatch_014]
  2562. if (Array_Check_All_Zero(&MmePacket->MMENTRY[4], 17) == FALSE)
  2563. {
  2564. DEBUG_WARN("CM_SLAC_MATCH_REQ[%d]: wrong EV ID\n", (idx + 1));
  2565. break;
  2566. }
  2567. //[TC_SECC_VTB_CmSlacMatch_015] EV MAC
  2568. //[TC_SECC_VTB_CmSlacMatch_016]
  2569. if (Array_Compare_Identity(EvMac_in, &MmePacket->MMENTRY[21], SLAC_EVMAC_LENGTH) == FALSE)
  2570. {
  2571. DEBUG_WARN("CM_SLAC_MATCH_REQ[%d]: wrong EV MAC(%02X:%02X:%02X:%02X:%02X:%02X)\n",
  2572. (idx + 1),
  2573. MmePacket->MMENTRY[21], MmePacket->MMENTRY[22], MmePacket->MMENTRY[23],
  2574. MmePacket->MMENTRY[24], MmePacket->MMENTRY[25], MmePacket->MMENTRY[26]);
  2575. break;
  2576. }
  2577. //[TC_SECC_VTB_CmSlacMatch_017] EVSE ID should be all zero
  2578. //[TC_SECC_VTB_CmSlacMatch_018]
  2579. if (Array_Check_All_Zero(&MmePacket->MMENTRY[27], 17) == FALSE)
  2580. {
  2581. DEBUG_WARN("CM_SLAC_MATCH_REQ[%d]: wrong EVSE ID\n", (idx + 1));
  2582. break;
  2583. }
  2584. //[TC_SECC_VTB_CmSlacMatch_019] EVSE MAC
  2585. //[TC_SECC_VTB_CmSlacMatch_020]
  2586. if (Array_Compare_Identity(macAddr.eth1, &MmePacket->MMENTRY[44], SLAC_EVSE_MAC_LENGTH) == FALSE)
  2587. {
  2588. DEBUG_WARN("CM_SLAC_MATCH_REQ[%d]: wrong EVSE MAC(%02X:%02X:%02X:%02X:%02X:%02X)\n",
  2589. (idx + 1),
  2590. MmePacket->MMENTRY[44], MmePacket->MMENTRY[45], MmePacket->MMENTRY[46],
  2591. MmePacket->MMENTRY[47], MmePacket->MMENTRY[48], MmePacket->MMENTRY[49]);
  2592. break;
  2593. }
  2594. //[TC_SECC_VTB_CmSlacMatch_021]runID: Check RunID
  2595. //[TC_SECC_VTB_CmSlacMatch_022]
  2596. if (SLAC_DB_Check_EvMac_RunID_Matching(&SLAC_INFO, EvMac_in, RunID_in) == TRUE &&
  2597. (idx == 0)) //only setup a successful link establishment with the first SLAC Req instance
  2598. {
  2599. memset(&qcaInfo.SendMmePacket, 0, sizeof(struct MmeHeader));
  2600. memcpy(qcaInfo.SendMmePacket.ODA, MmePacket->OSA, 6);
  2601. memcpy(qcaInfo.SendMmePacket.OSA, macAddr.eth1, 6);
  2602. qcaInfo.SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  2603. qcaInfo.SendMmePacket.MMV = MmePacket->MMV;
  2604. qcaInfo.SendMmePacket.MMTYPE = MMTYPE_CM_SLAC_MATCH_CNF;
  2605. qcaInfo.SendMmePacket.FMI[0] = qcaInfo.SendMmePacket.FMI[1] = 0;
  2606. qcaInfo.SendMmePacketSize = 0;
  2607. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0x00; //APPLICATION_TYPE: Fixed value (0x00: EV-EVSE matching)
  2608. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0x00; //SECURITY_TYPE: Fixed value (0x00: No Security)
  2609. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0x56; //MVFLength: MatchVarField Length (Fixed value: 0x0056)
  2610. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0x00; //MVFLength: MatchVarField Length (Fixed value: 0x0056)
  2611. memset(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, 0, 17); //EV ID
  2612. qcaInfo.SendMmePacketSize += 17;
  2613. memcpy(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, EvMac_in, SLAC_EVMAC_LENGTH); //EV MAC
  2614. qcaInfo.SendMmePacketSize += 6;
  2615. memset(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, 0, 17); //EVSE ID
  2616. qcaInfo.SendMmePacketSize += 17;
  2617. memcpy(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, macAddr.eth1, 6); //EVSE MAC
  2618. qcaInfo.SendMmePacketSize += 6;
  2619. memcpy(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, RunID_in, SLAC_RUNID_LENGTH);
  2620. qcaInfo.SendMmePacketSize += SLAC_RUNID_LENGTH;
  2621. memset(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, 0, 8); //RSVD
  2622. qcaInfo.SendMmePacketSize += 8;
  2623. memcpy(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, qcaInfo.Nid, sizeof(qcaInfo.Nid)); //NID: Network ID given by the CCo(EVSE)
  2624. qcaInfo.SendMmePacketSize += sizeof(qcaInfo.Nid); //NID caculated from the random NMK that will be set.
  2625. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0x00; //RSVD
  2626. memcpy(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, qcaInfo.NewNmkKey, sizeof(qcaInfo.NewNmkKey)); //NMK: Random value
  2627. qcaInfo.SendMmePacketSize += sizeof(qcaInfo.NewNmkKey); //NMK: Private NMK of the EVSE (random value)
  2628. qcaInfo.SendMmePacketSize += 19; //the size before MMENTRY
  2629. DEBUG_PRINTF_EVCOMM_DETAIL("***** Response MME Packet *****\n");
  2630. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2631. qcaInfo.SendMmePacket.ODA[0],qcaInfo.SendMmePacket.ODA[1],qcaInfo.SendMmePacket.ODA[2],qcaInfo.SendMmePacket.ODA[3],qcaInfo.SendMmePacket.ODA[4],qcaInfo.SendMmePacket.ODA[5]);
  2632. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n",
  2633. qcaInfo.SendMmePacket.OSA[0],qcaInfo.SendMmePacket.OSA[1],qcaInfo.SendMmePacket.OSA[2],qcaInfo.SendMmePacket.OSA[3],qcaInfo.SendMmePacket.OSA[4],qcaInfo.SendMmePacket.OSA[5]);
  2634. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(qcaInfo.SendMmePacket.MTYPE));
  2635. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", qcaInfo.SendMmePacket.MMV);
  2636. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", qcaInfo.SendMmePacket.MMTYPE);
  2637. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", qcaInfo.SendMmePacket.FMI[0],qcaInfo.SendMmePacket.FMI[1]);
  2638. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_SLAC_MATCH_CNF ---\n");
  2639. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", qcaInfo.SendMmePacket.MMENTRY[0]);
  2640. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", qcaInfo.SendMmePacket.MMENTRY[1]);
  2641. DEBUG_PRINTF_EVCOMM_DETAIL("MVFLength: 0x%x, 0x%x\n", qcaInfo.SendMmePacket.MMENTRY[2],qcaInfo.SendMmePacket.MMENTRY[3]);
  2642. memset(buf, 0x00, ARRAY_SIZE(buf));
  2643. for(Rtn=0; Rtn<17; Rtn++)
  2644. {
  2645. sprintf(buf, "%s%02x ", buf, qcaInfo.SendMmePacket.MMENTRY[4+Rtn]);
  2646. }
  2647. DEBUG_PRINTF_EVCOMM_DETAIL("PEV ID: %d\n", buf);
  2648. memset(buf, 0x00, ARRAY_SIZE(buf));
  2649. for(Rtn=0; Rtn<6; Rtn++)
  2650. {
  2651. sprintf(buf, "%s%02x ", buf, qcaInfo.SendMmePacket.MMENTRY[21+Rtn]);
  2652. }
  2653. DEBUG_PRINTF_EVCOMM_DETAIL("PEV MAC: %s\n", buf);
  2654. memset(buf, 0x00, ARRAY_SIZE(buf));
  2655. for(Rtn=0; Rtn<17; Rtn++)
  2656. {
  2657. sprintf(buf, "%s%02x ", buf, qcaInfo.SendMmePacket.MMENTRY[27+Rtn]);
  2658. }
  2659. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE ID: %s\n", buf);
  2660. memset(buf, 0x00, ARRAY_SIZE(buf));
  2661. for(Rtn=0; Rtn<6; Rtn++)
  2662. {
  2663. sprintf(buf, "%s%02x ", buf, qcaInfo.SendMmePacket.MMENTRY[44+Rtn]);
  2664. }
  2665. DEBUG_PRINTF_EVCOMM_DETAIL("EVSE MAC: %s\n", buf);
  2666. memset(buf, 0x00, ARRAY_SIZE(buf));
  2667. for(Rtn=0; Rtn<8; Rtn++)
  2668. {
  2669. sprintf(buf, "%s%02x ", buf, qcaInfo.SendMmePacket.MMENTRY[50+Rtn]);
  2670. }
  2671. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %s\n", buf);
  2672. memset(buf, 0x00, ARRAY_SIZE(buf));
  2673. for(Rtn=0; Rtn<8; Rtn++)
  2674. {
  2675. sprintf(buf, "%s%02x ", buf, qcaInfo.SendMmePacket.MMENTRY[58+Rtn]);
  2676. }
  2677. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: %s\n", buf);
  2678. memset(buf, 0x00, ARRAY_SIZE(buf));
  2679. for(Rtn=0; Rtn<7; Rtn++)
  2680. {
  2681. sprintf(buf, "%s%02x ", buf, qcaInfo.SendMmePacket.MMENTRY[66+Rtn]);
  2682. }
  2683. DEBUG_PRINTF_EVCOMM_DETAIL("NID: %s\n", buf);
  2684. DEBUG_PRINTF_EVCOMM_DETAIL("RSVD: 0x%02x\n", qcaInfo.SendMmePacket.MMENTRY[73]);
  2685. memset(buf, 0x00, ARRAY_SIZE(buf));
  2686. for(Rtn=0; Rtn<16; Rtn++)
  2687. {
  2688. sprintf(buf, "%s%02x ", buf, qcaInfo.SendMmePacket.MMENTRY[74+Rtn]);
  2689. }
  2690. DEBUG_PRINTF_EVCOMM_DETAIL("NMK: \n");
  2691. Update_V2G_Status(CM_SLAC_MATCH_CNF);
  2692. Rtn = sendto(socketFd.Raw, &qcaInfo.SendMmePacket, qcaInfo.SendMmePacketSize, 0, (struct sockaddr*)&qcaInfo.DestSocketAddress, sizeof(struct sockaddr_ll));
  2693. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n", qcaInfo.SendMmePacketSize,Rtn);
  2694. DEBUG_INFO( "CM_SLAC_MATCH_CNF[%d]\n", (idx + 1));
  2695. ftime(&timerStart.SeqStart);
  2696. }
  2697. else
  2698. {
  2699. //RunID does not match and it's not the first SLAC request
  2700. //Reset the SLAC database to embrace SLAC retry
  2701. DEBUG_INFO("[CM_SLAC_MATCH_REQ]No Real MATCH_REQ\n");
  2702. SLAC_DB_Reset();
  2703. }
  2704. }
  2705. else
  2706. {
  2707. //OSA(EvMac) does not exist
  2708. }
  2709. break;
  2710. }
  2711. case MMTYPE_VENDOR_VS_HOST_ACTION:
  2712. {
  2713. struct QcaVendorMmeHeader *RecvPacket;
  2714. RecvPacket = (struct QcaVendorMmeHeader *)Buffer;
  2715. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_VS_HOST_ACTION ---\n");
  2716. switch (RecvPacket->MBODY[0])
  2717. {
  2718. case 0x00:
  2719. //Loader (Device Softloader or Bootloader) ready
  2720. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Loader Ready\n");
  2721. break;
  2722. case 0x01:
  2723. //Firmware Upgrade Ready
  2724. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Firmware Upgrade Ready\n");
  2725. break;
  2726. case 0x02:
  2727. //PIB Update Ready
  2728. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: PIB Update Ready\n");
  2729. break;
  2730. case 0x03:
  2731. //Firmware Upgrade and PIB Update ready
  2732. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Firmware Upgrade and PIB Update ready\n");
  2733. break;
  2734. case 0x04:
  2735. //Loader (Bootloader) ready to receive SDRAM configuration.
  2736. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Loader ready to receive SDRAM configuration\n");
  2737. break;
  2738. case 0x05:
  2739. //Reset to Factory Defaults.
  2740. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Reset to Factory Defaults\n");
  2741. break;
  2742. default:
  2743. //Reserved
  2744. DEBUG_PRINTF_EVCOMM_DETAIL("QCA7K: Reserved\n");
  2745. break;
  2746. }
  2747. break;
  2748. }
  2749. case MMTYPE_VENDOR_ATTEN_CHAR:
  2750. {
  2751. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_ATTEN_CHAR ---\n");
  2752. break;
  2753. }
  2754. case MMTYPE_VENDOR_VS_NW_INFO_CNF:
  2755. {
  2756. // Only Set key for local QCA7000
  2757. if((MmePacket->OSA[0] == MmePacket->MMENTRY[12]) &&
  2758. (MmePacket->OSA[1] == MmePacket->MMENTRY[13]) &&
  2759. (MmePacket->OSA[2] == MmePacket->MMENTRY[14]) &&
  2760. (MmePacket->OSA[3] == MmePacket->MMENTRY[15]) &&
  2761. (MmePacket->OSA[4] == MmePacket->MMENTRY[16]) &&
  2762. (MmePacket->OSA[5] == MmePacket->MMENTRY[17]))
  2763. {
  2764. memcpy(macAddr.qca, MmePacket->OSA, 6);
  2765. DEBUG_PRINTF_EVCOMM_DETAIL("--- MMTYPE_VENDOR_VS_NW_INFO_CNF ---\n");
  2766. DEBUG_INFO("EVSE MAC address(QCA): %02X:%02X:%02X:%02X:%02X:%02X(comm:OK)\n",
  2767. macAddr.qca[0], macAddr.qca[1], macAddr.qca[2], macAddr.qca[3], macAddr.qca[4], macAddr.qca[5]);
  2768. Update_V2G_Status(CM_SET_KEY_REQ);
  2769. ftime(&timerStart.SeqStart);
  2770. }
  2771. break;
  2772. }
  2773. case MMTYPE_VENDOR_VS_PL_LNK_STATUS_CNF:
  2774. {
  2775. struct QcaVendorMmeHeader *RecvPacket;
  2776. RecvPacket = (struct QcaVendorMmeHeader *)Buffer;
  2777. if(RecvPacket->MBODY[1]==0)
  2778. {
  2779. //PLC disconnected
  2780. DEBUG_INFO("[MMTYPE_VENDOR_VS_PL_LNK_STATUS_CNF]Got PLC Link Status:%d\n", RecvPacket->MBODY[1]);
  2781. Update_V2G_Status(Other_Fault);
  2782. }
  2783. else
  2784. Update_V2G_Status(CM_SET_KEY_REQ);
  2785. ftime(&timerStart.SeqStart);
  2786. break;
  2787. }
  2788. default:
  2789. {
  2790. break;
  2791. }
  2792. }
  2793. return 0;
  2794. }
  2795. /**
  2796. *
  2797. * @return
  2798. */
  2799. int SlacComm()
  2800. {
  2801. static unsigned char qca7k_comm_retry = 0;
  2802. double t_diff = 0;
  2803. int packet_size = 0;
  2804. int count = 0;
  2805. unsigned char tmpBuf[2048]={0};
  2806. unsigned char *EvMac_in;
  2807. unsigned char *RunID_in;
  2808. int i = 0;
  2809. if(socketFd.Raw >= 0)
  2810. {
  2811. memset(v2gBuffer.rx, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  2812. packet_size = recvfrom(socketFd.Raw, v2gBuffer.rx, V2GTP_MSG_RX_BUFFER_SIZE, 0, NULL, NULL);
  2813. if(packet_size > 0)
  2814. {
  2815. MmeProcess(v2gBuffer.rx, packet_size);
  2816. }
  2817. }
  2818. switch(Get_V2G_Status())
  2819. {
  2820. case IDLE:
  2821. {
  2822. if(socketFd.Raw < 0)
  2823. {
  2824. socketFd.Raw = socket(PF_PACKET, SOCK_RAW, htons(EtherType_HomePlug));
  2825. DEBUG_INFO("[RawSock]opened(%d)\n", socketFd.Raw);
  2826. if(socketFd.Raw == -1)
  2827. {
  2828. DEBUG_ERROR("SlacComm:Failed to create socket\n");
  2829. Update_V2G_Status(Other_Fault);
  2830. return -1;
  2831. }
  2832. if (setsockopt(socketFd.Raw, SOL_SOCKET, SO_BINDTODEVICE, QcaInterface, 4) < 0)
  2833. {
  2834. DEBUG_ERROR("SlacComm:Set SO_BINDTODEVICE NG\n");
  2835. Update_V2G_Status(Other_Fault);
  2836. return -1;
  2837. }
  2838. struct timeval tv;
  2839. tv.tv_sec = 0;
  2840. tv.tv_usec = 100000; //100ms (Rx timeout)
  2841. if (setsockopt(socketFd.Raw, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  2842. {
  2843. DEBUG_ERROR("SlacComm:Set SO_RCVTIMEO NG\n");
  2844. Update_V2G_Status(Other_Fault);
  2845. return -1;
  2846. }
  2847. tv.tv_usec = 100000; //100ms (Tx timeout)
  2848. if (setsockopt(socketFd.Raw, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  2849. {
  2850. DEBUG_ERROR("SlacComm:Set SO_SNDTIMEO NG\n");
  2851. Update_V2G_Status(Other_Fault);
  2852. return -1;
  2853. }
  2854. memset(&qcaInfo.Req, 0, sizeof(struct ifreq));
  2855. strcpy( (char*)qcaInfo.Req.ifr_name, QcaInterface);
  2856. if (ioctl(socketFd.Raw, SIOCGIFINDEX, &qcaInfo.Req) < 0)
  2857. {
  2858. DEBUG_ERROR("SlacComm: ioctl NG\n");
  2859. Update_V2G_Status(Other_Fault);
  2860. return -1;
  2861. }
  2862. memset(&qcaInfo.DestSocketAddress, 0, sizeof(struct sockaddr_ll));
  2863. qcaInfo.DestSocketAddress.sll_ifindex = qcaInfo.Req.ifr_ifindex;
  2864. qcaInfo.DestSocketAddress.sll_halen = ETH_ALEN;
  2865. timerStart.PwmStart = 0;
  2866. EVCOMM_SYS_INFO.QCA7K_SetKeyDone = FALSE;
  2867. DEBUG_INFO("QCA7000 connecting...\n");
  2868. //Get QCA7K MAC address
  2869. GetQca7kMac();
  2870. ftime(&timerStart.SeqStart);
  2871. break;
  2872. }
  2873. else //RawSock: opened
  2874. {
  2875. if(EVCOMM_SYS_INFO.QCA7K_SetKeyDone == FALSE)
  2876. {
  2877. ftime(&timerStart.SeqEnd);
  2878. t_diff = DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd);
  2879. if (t_diff > V2G_SECC_QCA7000_GET_MAC_ADDR_REQ_RETRY_PERIOD) //3 secs
  2880. {
  2881. qca7k_comm_retry++;
  2882. DEBUG_INFO("Re-try connecting...(%.02lf/%dms)\n", t_diff, V2G_SECC_QCA7000_GET_MAC_ADDR_REQ_RETRY_PERIOD);
  2883. GetQca7kMac(); //re-send req
  2884. ftime(&timerStart.SeqStart);
  2885. break;
  2886. }
  2887. else
  2888. {}
  2889. //Retry by 3 times
  2890. if (qca7k_comm_retry >= 3)
  2891. {
  2892. DEBUG_ERROR("Comm: fail (retry by %d times)\n", qca7k_comm_retry);
  2893. //Update_ShmStatusCode(); //[To-Do] to be implemented
  2894. //CCS_SECCC_TIMEOUT_QCA7000_COMM (023892): The firmware code of QCA7000 may not be installed, yet
  2895. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  2896. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  2897. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  2898. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  2899. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  2900. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  2901. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  2902. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  2903. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  2904. Update_V2G_Status(Sequence_Timeout);
  2905. qca7k_comm_retry = 0;
  2906. break;
  2907. }
  2908. }
  2909. else //RawSock: opened; Set Key: DONE
  2910. {
  2911. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  2912. if((CheckConnectorPlugIn() == TRUE) && (ShmInternalComm->ChargingPermission >= 1))
  2913. {
  2914. if(timerStart.PwmStart <= 0)
  2915. {
  2916. //Sniffer_Tcpdump(ENABLE);
  2917. //#if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  2918. //sleep(1); //wait for tcpdump to be ready.
  2919. //#endif
  2920. SwitchCpStateE(DISABLE);
  2921. OutputCpPwmDuty(5);
  2922. timerStart.PwmStart = time(NULL);
  2923. #ifdef TEST_WITH_ETH0
  2924. Update_V2G_Status(SLACC_SDP_UDP_Connection);
  2925. #endif
  2926. }
  2927. else
  2928. {
  2929. if((time(NULL) - timerStart.PwmStart) > TT_EVSE_SLAC_init)
  2930. {
  2931. DEBUG_ERROR("SlacComm: Wait CM_SLAC_PARM_REQ Timeout - TT_EVSE_SLAC_init, NowTime(%ld)-timerStart.PwmStart(%d)>%d (sec)\n", time(NULL), timerStart.PwmStart, TT_EVSE_SLAC_init);
  2932. //Update_ShmStatusCode(); //[To-Do] to be implemented
  2933. //CCS_SECC_TIMEOUT_SLAC_TT_EVSE_SLAC_init (023809)
  2934. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  2935. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  2936. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  2937. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  2938. ShmStatusCodeData->PresentStatusCode[0][4] = 0;
  2939. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  2940. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  2941. Update_V2G_Status(Sequence_Timeout);
  2942. timerStart.PwmStart = 0;
  2943. return -1;
  2944. }
  2945. else
  2946. {
  2947. //waiting for CM_SLAC_PARM_REQ
  2948. }
  2949. }
  2950. }
  2951. else
  2952. {
  2953. timerStart.PwmStart = 0;
  2954. }
  2955. }
  2956. }
  2957. break;
  2958. }
  2959. case CM_SET_KEY_REQ: //13
  2960. {
  2961. //CM_SET_KEY_REQ
  2962. //DEBUG_INFO("QCA7000 [RX]CM_SET_KEY_REQ\n");
  2963. ftime(&timerStart.SeqEnd);
  2964. t_diff = DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd);
  2965. if (t_diff > V2G_SECC_QCA7000_COMM_TIMEOUT) //10 seconds
  2966. {
  2967. DEBUG_ERROR("QCA7000 Failed on SetKey => End_Process (%.02lf/%dms)\n", t_diff, V2G_SECC_QCA7000_COMM_TIMEOUT);
  2968. //Update_ShmStatusCode(); //[To-Do] to be implemented
  2969. //CCS_SECCC_TIMEOUT_QCA7000_COMM (023892): The firmware code of QCA7000 may not be installed, yet
  2970. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  2971. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  2972. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  2973. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  2974. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  2975. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  2976. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  2977. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  2978. Update_V2G_Status(Sequence_Timeout);
  2979. }
  2980. else if (t_diff > V2G_SECC_QCA7000_SEND_SET_KEY_PERIOD) //2 seconds
  2981. {
  2982. DEBUG_INFO("QCA7000 SetKey: proceed (%.02lf/%dms)\n", t_diff, V2G_SECC_QCA7000_SEND_SET_KEY_PERIOD);
  2983. SendSetKey();
  2984. ftime(&timerStart.SeqStart);
  2985. }
  2986. else
  2987. {
  2988. //null
  2989. }
  2990. break;
  2991. }
  2992. case CM_SET_KEY_CNF: //14
  2993. {
  2994. DEBUG_INFO("Wait: plugin(%d), matached(%d), permission(%d)...\n", CheckConnectorPlugIn(), CSUCOMMDC_TASK_FLAG.matched, ShmInternalComm->ChargingPermission);
  2995. EVCOMM_SYS_INFO.QCA7K_SetKeyDone = TRUE;
  2996. timerStart.PwmStart = 0;
  2997. Update_V2G_Status(IDLE);
  2998. break;
  2999. }
  3000. case CM_SLAC_PARM_CONF:
  3001. {
  3002. ftime(&timerStart.SeqEnd);
  3003. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > TT_match_sequence)
  3004. {
  3005. DEBUG_ERROR("SlacComm: Wait CM_START_ATTEN_CHAR_IND Timeout - TT_match_sequence (%.02lf of %dms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), TT_match_sequence);
  3006. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3007. //CCS_SECC_TIMEOUT_CM_START_ATTEN_CHAR_IND (023811):
  3008. //[Possible Reason] CCS Board image might not be updated properly. The 2nd reboot process is not finished.
  3009. //[Verification Method] By tying reboot command to the terminal and see if it could be executed immediately.
  3010. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3011. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3012. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3013. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3014. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  3015. ShmStatusCodeData->PresentStatusCode[0][5] = 1;
  3016. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3017. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3018. Update_V2G_Status(Sequence_Timeout);
  3019. return -1;
  3020. }
  3021. break;
  3022. }
  3023. case CM_START_ATTEN_CHAR_IND:
  3024. {
  3025. ftime(&timerStart.SeqEnd);
  3026. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > (TP_EV_batch_msg_interval)) //one more time interval for tolerance
  3027. {
  3028. DEBUG_ERROR("SlacComm: Wait CM_MNBC_SOUND_IND Timeout - TP_EV_batch_msg_interval (%.02lf of %dms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), TP_EV_batch_msg_interval);
  3029. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3030. //CCS_SECC_TIMEOUT_SLAC_CM_MNBC_SOUND_IND (023818):
  3031. //[Possible Reason] CCS Board image might not be updated properly. The 2nd reboot process is not finished.
  3032. //[Verification Method] By tying reboot command to the terminal and see if it could be executed immediately.
  3033. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3034. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3035. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3036. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3037. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  3038. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  3039. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3040. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3041. Update_V2G_Status(Sequence_Timeout);
  3042. return -1;
  3043. }
  3044. break;
  3045. }
  3046. case CM_MNBC_SOUND_IND:
  3047. {
  3048. ftime(&timerStart.SeqEnd);
  3049. t_diff = DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd);
  3050. //printf("time:%.02lf, profilNum:%d\n",DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd),AttenProfileCnt); //added by Vern
  3051. if(t_diff > TT_EVSE_match_MNBC ||
  3052. (SLAC_INFO.array[0].AttenProfileCnt >= SLAC_INFO.array[0].MnbcSoundNum) ||
  3053. (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0)) //TC_SECC_VTB_AttenuationCharacterization_003
  3054. {
  3055. //Wait for other SLAC Req sets
  3056. if ((SLAC_INFO.arrayLen >= 2) &&
  3057. (t_diff < TT_EVSE_match_MNBC) &&
  3058. (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry == 0)) //not a retry
  3059. {
  3060. break;
  3061. }
  3062. //Check if it is a timeup response
  3063. if (t_diff > TT_EVSE_match_MNBC)
  3064. {
  3065. DEBUG_WARN("[SLAC][TX]CM_ATTEN_CHAR_IND[%d]: timeup(%.2f/%dms) => send\n", (i + 1), t_diff, TT_EVSE_match_MNBC);
  3066. }
  3067. //Sending all CM_ATTEN_CHAR_IND according to all corresponding SLAC Req sets
  3068. for (i = 0; i < SLAC_INFO.arrayLen; i++)
  3069. {
  3070. if ((SLAC_INFO.array[i].AttenProfileCnt == 0) ||
  3071. (SLAC_INFO.array[i].AagGroupsNum == 0) ||
  3072. (SLAC_INFO.array[i].StartAttenCharCnt == 0) ||
  3073. (SLAC_INFO.array[i].MnbcSoundNum != 10) || //received in CM_START_ATTEN_CHAR_IND
  3074. (SLAC_INFO.array[i].StartAttenCharErr == TRUE)
  3075. )
  3076. {
  3077. //Ignoring those SLAC request sets without sending CM_MNBC_SOUND_IND(CM_ATTEN_PROFILE_IND)
  3078. DEBUG_WARN("[SLAC][TX]CM_ATTEN_CHAR_IND[%d]: para err(%d,%d,%d,%d,%d) => canceled\n",
  3079. (i + 1), SLAC_INFO.array[i].AttenProfileCnt, SLAC_INFO.array[i].AagGroupsNum, SLAC_INFO.array[i].StartAttenCharCnt, SLAC_INFO.array[i].MnbcSoundNum, SLAC_INFO.array[i].StartAttenCharErr);
  3080. continue;
  3081. }
  3082. //In CM_ATTEN_CHAR_IND retry process, here only re-send this message according to the 1st coming SLAC request
  3083. if ((EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0) && (i != 0))
  3084. {
  3085. DEBUG_INFO("[SLAC][TX]CM_ATTEN_CHAR_IND[%d]: canceled\n", (i + 1));
  3086. break;
  3087. }
  3088. EvMac_in = SLAC_INFO.array[i].EvMac;
  3089. RunID_in = SLAC_INFO.array[i].RunID;
  3090. memset(&qcaInfo.SendMmePacket, 0, sizeof(struct MmeHeader));
  3091. memcpy(qcaInfo.SendMmePacket.ODA, EvMac_in, SLAC_EVMAC_LENGTH);
  3092. memcpy(qcaInfo.SendMmePacket.OSA, macAddr.eth1, 6);
  3093. qcaInfo.SendMmePacket.MTYPE = htons(EtherType_HomePlug);
  3094. qcaInfo.SendMmePacket.MMV = 0x01;
  3095. qcaInfo.SendMmePacket.MMTYPE = MMTYPE_CM_ATTEN_CHAR_IND;
  3096. qcaInfo.SendMmePacket.FMI[0] = qcaInfo.SendMmePacket.FMI[1] = 0;
  3097. qcaInfo.SendMmePacketSize = 0;
  3098. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0; //APPLICATION_TYPE(0x00: EV-EVSE Matching)
  3099. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = 0; //SECURITY_TYPE(0x00: No Security)
  3100. memcpy(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, EvMac_in, SLAC_EVMAC_LENGTH); //SOURCE_ADDRESS, MAC address of the EV Host
  3101. qcaInfo.SendMmePacketSize += SLAC_EVMAC_LENGTH;
  3102. memcpy(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, RunID_in, SLAC_RUNID_LENGTH);
  3103. qcaInfo.SendMmePacketSize += SLAC_RUNID_LENGTH;
  3104. memset(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, 0, 17); //SOURCE_ID(0x00)
  3105. qcaInfo.SendMmePacketSize += 17;
  3106. memset(qcaInfo.SendMmePacket.MMENTRY + qcaInfo.SendMmePacketSize, 0, 17); //RESP_ID(0x00)
  3107. qcaInfo.SendMmePacketSize += 17;
  3108. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = SLAC_INFO.array[i].AttenProfileCnt; //NumSounds: Number of M-Sounds used for generation of the ATTEN_PROFILE
  3109. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = SLAC_INFO.array[i].AagGroupsNum; //NumGroups
  3110. for(count = 0; count < SLAC_INFO.array[i].AagGroupsNum; count++)
  3111. {
  3112. unsigned char TmpAag;
  3113. TmpAag = ((SLAC_INFO.array[i].AAG[count] / SLAC_INFO.array[i].AttenProfileCnt) & 0xFF);
  3114. SLAC_INFO.array[i].AAG_quality_ori += (float) TmpAag; //original signal quality
  3115. #if (SUDO_PSD_PARAMETER_MECHANISM == ENABLE) //default: ENABLE
  3116. #if 1
  3117. //TC_SECC_VTB_AttenuationCharacterization_019
  3118. TmpAag = TmpAag >> 1; //new method proposed by Joseph (divided by 2)
  3119. #else
  3120. if(TmpAag >= 39) //original method proposed by Vern
  3121. {
  3122. TmpAag = 37;
  3123. }
  3124. #endif
  3125. #endif
  3126. qcaInfo.SendMmePacket.MMENTRY[qcaInfo.SendMmePacketSize++] = TmpAag;
  3127. SLAC_INFO.array[i].AAG_quality_refined += (float) TmpAag; //refined signal quality
  3128. }
  3129. qcaInfo.SendMmePacketSize += 19; //the size before MMENTRY
  3130. DEBUG_PRINTF_EVCOMM_DETAIL("***** Send MME Packet *****\n");
  3131. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.ODA: %02x:%02x:%02x:%02x:%02x:%02x:\n", qcaInfo.SendMmePacket.ODA[0],qcaInfo.SendMmePacket.ODA[1],qcaInfo.SendMmePacket.ODA[2],qcaInfo.SendMmePacket.ODA[3],qcaInfo.SendMmePacket.ODA[4],qcaInfo.SendMmePacket.ODA[5]);
  3132. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacket.OSA: %02x:%02x:%02x:%02x:%02x:%02x:\n", qcaInfo.SendMmePacket.OSA[0],qcaInfo.SendMmePacket.OSA[1],qcaInfo.SendMmePacket.OSA[2],qcaInfo.SendMmePacket.OSA[3],qcaInfo.SendMmePacket.OSA[4],qcaInfo.SendMmePacket.OSA[5]);
  3133. DEBUG_PRINTF_EVCOMM_DETAIL("MTYPE: 0x%x\n", htons(qcaInfo.SendMmePacket.MTYPE));
  3134. DEBUG_PRINTF_EVCOMM_DETAIL("MMV: 0x%x\n", qcaInfo.SendMmePacket.MMV);
  3135. DEBUG_PRINTF_EVCOMM_DETAIL("MMTYPE: 0x%x\n", qcaInfo.SendMmePacket.MMTYPE);
  3136. DEBUG_PRINTF_EVCOMM_DETAIL("FMI 0x%x, 0x%x\n", qcaInfo.SendMmePacket.FMI[0],qcaInfo.SendMmePacket.FMI[1]);
  3137. DEBUG_PRINTF_EVCOMM_DETAIL("--- CM_ATTEN_CHAR_IND ---\n");
  3138. DEBUG_PRINTF_EVCOMM_DETAIL("APPLICATION_TYPE: 0x%x\n", qcaInfo.SendMmePacket.MMENTRY[0]);
  3139. DEBUG_PRINTF_EVCOMM_DETAIL("SECURITY_TYPE: 0x%x\n", qcaInfo.SendMmePacket.MMENTRY[1]);
  3140. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n", qcaInfo.SendMmePacket.MMENTRY[2],qcaInfo.SendMmePacket.MMENTRY[3],qcaInfo.SendMmePacket.MMENTRY[4],qcaInfo.SendMmePacket.MMENTRY[5],qcaInfo.SendMmePacket.MMENTRY[6],qcaInfo.SendMmePacket.MMENTRY[7]);
  3141. DEBUG_PRINTF_EVCOMM_DETAIL("RunID: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n", qcaInfo.SendMmePacket.MMENTRY[8],qcaInfo.SendMmePacket.MMENTRY[9],qcaInfo.SendMmePacket.MMENTRY[10],qcaInfo.SendMmePacket.MMENTRY[11],qcaInfo.SendMmePacket.MMENTRY[12],qcaInfo.SendMmePacket.MMENTRY[13],qcaInfo.SendMmePacket.MMENTRY[14],qcaInfo.SendMmePacket.MMENTRY[15]);
  3142. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  3143. for(count=0; count<17; count++)
  3144. {
  3145. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, qcaInfo.SendMmePacket.MMENTRY[16+count]);
  3146. }
  3147. DEBUG_PRINTF_EVCOMM_DETAIL("SOURCE_ID: %s\n", tmpBuf);
  3148. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  3149. for(count=0; count<17; count++)
  3150. {
  3151. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, qcaInfo.SendMmePacket.MMENTRY[33+count]);
  3152. }
  3153. DEBUG_PRINTF_EVCOMM_DETAIL("RESP_ID: %s\n", tmpBuf);
  3154. DEBUG_PRINTF_EVCOMM_DETAIL("NumSounds: 0x%x\n", qcaInfo.SendMmePacket.MMENTRY[50]);
  3155. memset(tmpBuf, 0x00, ARRAY_SIZE(tmpBuf));
  3156. for(count=0; count<qcaInfo.AagGroupsNum; count++)
  3157. {
  3158. sprintf((char*)tmpBuf, "%s%02x,", tmpBuf, qcaInfo.SendMmePacket.MMENTRY[51+count]);
  3159. }
  3160. DEBUG_PRINTF_EVCOMM_DETAIL("ATTEN_PROFILE: %s\n", tmpBuf);
  3161. count = sendto(socketFd.Raw, &qcaInfo.SendMmePacket, qcaInfo.SendMmePacketSize, 0, (struct sockaddr*)&qcaInfo.DestSocketAddress, sizeof(struct sockaddr_ll));
  3162. SLAC_INFO.array[i].AAG_quality_ori /= SLAC_INFO.array[i].AagGroupsNum;
  3163. SLAC_INFO.array[i].AAG_quality_refined /= SLAC_INFO.array[i].AagGroupsNum;
  3164. //Print log if this CM_ATTEN_CHAR_IND belongs to RETRY Tx message.
  3165. if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry > 0)
  3166. {
  3167. DEBUG_INFO("[SLAC][TX]CM_ATTEN_CHAR_IND[%d]: %d-th resend (Q=%.2f/%.2f)(%d/%d)\n",
  3168. (i + 1),
  3169. EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry,
  3170. SLAC_INFO.array[i].AAG_quality_refined,
  3171. SLAC_INFO.array[i].AAG_quality_ori,
  3172. SLAC_INFO.array[i].AttenProfileCnt,
  3173. SLAC_INFO.array[i].MnbcSoundNum);
  3174. }
  3175. else if (EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry == 0)
  3176. {
  3177. DEBUG_INFO("[SLAC][TX]CM_ATTEN_CHAR_IND[%d]: Q=%.2f/%.2f(%d/%d)\n",
  3178. (i + 1),
  3179. SLAC_INFO.array[i].AAG_quality_refined,
  3180. SLAC_INFO.array[i].AAG_quality_ori,
  3181. SLAC_INFO.array[i].AttenProfileCnt,
  3182. SLAC_INFO.array[i].MnbcSoundNum);
  3183. }
  3184. else
  3185. {
  3186. DEBUG_INFO("[SLAC][TX]CM_ATTEN_CHAR_IND[%d]: unexpected CM_ATTEN_CHAR_IND_retry(%d))\n", i, EVCOMM_SYS_INFO.CM_ATTEN_CHAR_IND_retry);
  3187. }
  3188. DEBUG_PRINTF_EVCOMM_DETAIL("SendMmePacketSize=%d,Rtn=%d\n", qcaInfo.SendMmePacketSize, count);
  3189. } //end of for loop
  3190. Update_V2G_Status(CM_ATTEN_CHAR_IND);
  3191. ftime(&timerStart.SeqStart);
  3192. }
  3193. break;
  3194. }
  3195. case CM_ATTEN_CHAR_IND:
  3196. {
  3197. ftime(&timerStart.SeqEnd);
  3198. //if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd)>TT_match_response)
  3199. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > TT_match_response) //extended to 400ms due to the response of CM_ATTEN_CHAR.RSP of some EVCC is slower than 200ms.
  3200. {
  3201. DEBUG_ERROR("SlacComm: Wait CM_ATTEN_CHAR_RSP Timeout - TT_match_response (%.02lf of %dms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), TT_match_response);
  3202. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3203. //CCS_SECC_TIMEOUT_SLAC_CM_ATTEN_CHAR_RSP (023814):
  3204. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  3205. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3206. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3207. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3208. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3209. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  3210. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  3211. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3212. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3213. Update_V2G_Status(Sequence_Timeout);
  3214. return -1;
  3215. }
  3216. break;
  3217. }
  3218. case CM_ATTEN_CHAR_RSP:
  3219. {
  3220. ftime(&timerStart.SeqEnd);
  3221. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > TT_EVSE_match_session)
  3222. {
  3223. DEBUG_ERROR("SlacComm: Wait CM_VALIDATE_REQ_1ST or CM_SLAC_MATCH_REQ Timeout - TT_EVSE_match_session (%.02lf of %dms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), TT_EVSE_match_session);
  3224. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3225. //CCS_SECC_TIMEOUT_SLAC_CM_VALIDATE_REQ_1ST__CM_SLAC_MATCH_REQ (023815):
  3226. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  3227. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3228. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3229. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3230. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3231. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  3232. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  3233. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3234. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3235. Update_V2G_Status(Sequence_Timeout);
  3236. return -1;
  3237. }
  3238. break;
  3239. }
  3240. case CM_VALIDATE_CNF:
  3241. {
  3242. ftime(&timerStart.SeqEnd);
  3243. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > TT_match_sequence)
  3244. {
  3245. DEBUG_ERROR("SlacComm: Wait CM_VALIDATE_REQ_2ND or CM_SLAC_MATCH_REQ Timeout - TT_match_sequence (%.02lf of %dms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), TT_match_sequence);
  3246. //Update_ShmStatusCode(); //[To-Do] to be implemented
  3247. //CCS_SECC_TIMEOUT_SLAC_CM_VALIDATE_REQ_2ND__CM_SLAC_MATCH_REQ (023819):
  3248. //[Possible Reason] Frequent on BMW i3, need to drive the EV a bit.
  3249. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  3250. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  3251. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  3252. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  3253. ShmStatusCodeData->PresentStatusCode[0][4] = 1;
  3254. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  3255. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  3256. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  3257. Update_V2G_Status(Sequence_Timeout);
  3258. return -1;
  3259. }
  3260. break;
  3261. }
  3262. case CM_SLAC_MATCH_CNF:
  3263. {
  3264. if(socketFd.Udp > 0)
  3265. {
  3266. close(socketFd.Udp);
  3267. socketFd.Udp = -1;
  3268. }
  3269. if(socketFd.Tcp > 0)
  3270. {
  3271. close(socketFd.Tcp);
  3272. socketFd.Tcp = -1;
  3273. }
  3274. ftime(&timerStart.SeqStart);
  3275. V2gTcpConnected();
  3276. Update_V2G_Status(SLACC_SDP_UDP_Connection);
  3277. DEBUG_INFO("SLAAC, SDP, UDP: connecting...\n");
  3278. break;
  3279. }
  3280. default:
  3281. {
  3282. break;
  3283. }
  3284. }
  3285. return 0;
  3286. }
  3287. /**
  3288. * 1. Decode the V2GTP messages inside "msg" and save the decoded
  3289. messages in v2gObject.DIN, v2gObject.ISO1, and v2gObject.ISO2,
  3290. respectively.
  3291. 2. After decoding, V2gMsg_Process() could then use
  3292. v2gObject.DIN, v2gObject.ISO1, or v2gObject.ISO2
  3293. to deal with the corresponding Response messages, respectively.
  3294. * @param msg
  3295. * @param msg_length
  3296. * @param v2g_state
  3297. * @return
  3298. */
  3299. int V2gMsgDecoder(unsigned char *msg, unsigned int msg_length, unsigned int v2g_state)
  3300. {
  3301. int errn = 0;
  3302. //Checking the minimum Header size requirement
  3303. if(msg_length < V2GTP_MSG_HEADER_LENGTH) //The minimum requirement should be 8-byte header.
  3304. {
  3305. errn = -1;
  3306. return errn;
  3307. }
  3308. //Decode the 1st V2GMSG: AppProtocol
  3309. if(v2g_state == SupportedAppProtocolRequest) //17
  3310. {
  3311. if ((errn = API_V2GMSG_EXI_Decoder_AppProtocol(msg, msg_length, &v2gObject.appHandshake)) < 0)
  3312. {
  3313. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR = %d (DEC)]V2gMsgDecoder: SupportedAppProtocolRequest()\n", errn);
  3314. }
  3315. else //decoded successfully.
  3316. {
  3317. //PRINT_XML_DOC_supportedAppProtocolReq(&v2gObject.appHandshake);
  3318. }
  3319. }
  3320. //Decode the subsequent V2GMSG (DIN 70121, ISO 15118-2, ISO 15118-20)
  3321. else if(v2g_state > SupportedAppProtocolRequest && v2g_state <= SessionStopResponse)
  3322. {
  3323. //Decoding according to its own protocol
  3324. switch (ShmCcsData->CommProtocol)
  3325. {
  3326. case V2GT_MSG_PROTOCOL_DIN70121: //0
  3327. {
  3328. //DIN
  3329. if((errn = API_V2GMSG_EXI_Decoder_DIN(msg, msg_length, &v2gObject.DIN)) < 0)
  3330. {
  3331. DEBUG_INFO("[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_DIN()\n", errn);
  3332. }
  3333. break;
  3334. }
  3335. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  3336. {
  3337. //ISO1
  3338. if((errn = API_V2GMSG_EXI_Decoder_ISO1(msg, msg_length, &v2gObject.ISO1)) < 0)
  3339. {
  3340. DEBUG_INFO("[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_ISO1()\n", errn);
  3341. }
  3342. break;
  3343. }
  3344. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  3345. {
  3346. //ISO2
  3347. if((errn = API_V2GMSG_EXI_Decoder_ISO2(msg, msg_length, &v2gObject.ISO2)) < 0)
  3348. {
  3349. DEBUG_INFO("[ERROR = %d (DEC)]V2gMsgDecoder: API_V2GMSG_EXI_Decoder_ISO2()\n", errn);
  3350. }
  3351. break;
  3352. }
  3353. default:
  3354. break;
  3355. }
  3356. }
  3357. else
  3358. {
  3359. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR = %d (DEC)]V2gMsgDecoder: Unexpected v2g_state\n", errn);
  3360. errn = -1;
  3361. }
  3362. return errn;
  3363. }
  3364. /**
  3365. *
  3366. * @param v2g_tx_stream
  3367. * @param v2gObject
  3368. * @return
  3369. */
  3370. int encode_din_V2GTP_stream(bitstream_t *v2g_tx_stream, struct dinEXIDocument *v2gObject)
  3371. {
  3372. int errn = 0;
  3373. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  3374. errn = encode_dinExiDocument(v2g_tx_stream, v2gObject);
  3375. if (errn == 0)
  3376. {
  3377. //successfully encoded
  3378. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  3379. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  3380. if (errn != 0)
  3381. {
  3382. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm]write_v2gtpHeader(): %d (DEC)\n", errn);
  3383. }
  3384. }
  3385. else
  3386. {
  3387. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm]encode_dinExiDocument(): %d (DEC)\n", errn);
  3388. }
  3389. return errn;
  3390. }
  3391. /**
  3392. *
  3393. * @param v2g_tx_stream
  3394. * @param v2gObject
  3395. * @return
  3396. */
  3397. int encode_iso1_V2GTP_stream(bitstream_t *v2g_tx_stream, struct iso1EXIDocument *v2gObject)
  3398. {
  3399. int errn = 0;
  3400. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  3401. errn = encode_iso1ExiDocument(v2g_tx_stream, v2gObject);
  3402. if (errn == 0)
  3403. {
  3404. //successfully encoded
  3405. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  3406. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  3407. if (errn != 0)
  3408. {
  3409. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm]write_v2gtpHeader(): %d (DEC)\n", errn);
  3410. }
  3411. }
  3412. else
  3413. {
  3414. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm]encode_iso1ExiDocument(): %d (DEC)\n", errn);
  3415. }
  3416. return errn;
  3417. }
  3418. /**
  3419. *
  3420. * @param v2g_tx_stream
  3421. * @param v2gObject
  3422. * @return
  3423. */
  3424. int encode_iso2_V2GTP_stream(bitstream_t *v2g_tx_stream, struct iso2EXIDocument *v2gObject)
  3425. {
  3426. int errn = 0;
  3427. *v2g_tx_stream->pos = V2GTP_HEADER_LENGTH;
  3428. errn = encode_iso2ExiDocument(v2g_tx_stream, v2gObject);
  3429. if (errn == 0)
  3430. {
  3431. //successfully encoded
  3432. errn = write_v2gtpHeader(v2g_tx_stream->data, (*v2g_tx_stream->pos) - V2GTP_HEADER_LENGTH, V2GTP_EXI_TYPE);
  3433. v2g_tx_stream->size = *v2g_tx_stream->pos; //total length of the encoded V2GMSG.
  3434. if (errn != 0)
  3435. {
  3436. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm]write_v2gtpHeader(): %d (DEC)\n", errn);
  3437. }
  3438. }
  3439. else
  3440. {
  3441. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR][SeccComm]encode_iso2ExiDocument(): %d (DEC)\n", errn);
  3442. }
  3443. return errn;
  3444. }
  3445. /**
  3446. *
  3447. * @param AcceptFd
  3448. * @param v2g_tx_stream
  3449. * @param v2gObject
  3450. * @return
  3451. */
  3452. int send_encoded_din_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct dinEXIDocument *v2gObject)
  3453. {
  3454. int errn = 0;
  3455. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  3456. errn = encode_din_V2GTP_stream(v2g_tx_stream, v2gObject);
  3457. // STEP 2: =========== Send Response Packet ===========
  3458. int rtn = 0;
  3459. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  3460. if (rtn == v2g_tx_stream->size)
  3461. {
  3462. /*
  3463. DEBUG_PRINTF_EVCOMM_DETAIL("Response Message is sent ( %d / %d ). (Bytes, DEC): OK\n",
  3464. rtn, v2g_tx_stream->size);
  3465. */
  3466. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  3467. }
  3468. else if (rtn >= 0)
  3469. {
  3470. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR]Incomplete Tx ( %d / %d ). (Bytes, DEC): FAIL\n", rtn, v2g_tx_stream->size);
  3471. }
  3472. else
  3473. {
  3474. errn = rtn;
  3475. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR]send(): %d (DEC)\n", errn);
  3476. }
  3477. //STEP 3: ========= Reset V2G MSG Flags ==========
  3478. init_dinBodyType(&v2gObject->V2G_Message.Body);
  3479. return errn;
  3480. }
  3481. /**
  3482. *
  3483. * @param AcceptFd
  3484. * @param v2g_tx_stream
  3485. * @param v2gObject
  3486. * @return
  3487. */
  3488. int send_encoded_iso1_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct iso1EXIDocument *v2gObject)
  3489. {
  3490. int errn = 0;
  3491. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  3492. errn = encode_iso1_V2GTP_stream(v2g_tx_stream, v2gObject);
  3493. // STEP 2: =========== Send Response Packet ===========
  3494. int rtn = 0;
  3495. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  3496. if (rtn == v2g_tx_stream->size)
  3497. {
  3498. //DEBUG_PRINTF_EVCOMM_DETAIL("Response message sent (%d/%d)(Bytes, DEC): OK\n", rtn, v2g_tx_stream->size);
  3499. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  3500. }
  3501. else if (rtn >= 0)
  3502. {
  3503. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR]Incomplete Tx (%d/%d)(Bytes, DEC): FAIL\n", rtn, v2g_tx_stream->size);
  3504. }
  3505. else
  3506. {
  3507. errn = rtn;
  3508. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR]send(): %d(DEC)\n", errn);
  3509. }
  3510. //STEP 3: ========= Reset V2G MSG Flags ==========
  3511. init_iso1BodyType(&v2gObject->V2G_Message.Body);
  3512. return errn;
  3513. }
  3514. /**
  3515. *
  3516. * @param AcceptFd
  3517. * @param v2g_tx_stream
  3518. * @param v2gObject
  3519. * @return
  3520. */
  3521. int send_encoded_iso2_V2GTP_Stream(int AcceptFd, bitstream_t *v2g_tx_stream, struct iso2EXIDocument *v2gObject)
  3522. {
  3523. int errn = 0;
  3524. // STEP 1: =========== Encoding into EXI and Composing into V2GTP Stream ==========
  3525. errn = encode_iso2_V2GTP_stream(v2g_tx_stream, v2gObject);
  3526. // STEP 2: =========== Send Response Packet ===========
  3527. int rtn = 0;
  3528. rtn = send(AcceptFd, v2g_tx_stream->data, v2g_tx_stream->size, 0);
  3529. if (rtn == v2g_tx_stream->size)
  3530. {
  3531. /*
  3532. DEBUG_PRINTF_EVCOMM_DETAIL("Response Message is sent ( %d / %d ). (Bytes, DEC): OK\n",
  3533. rtn, v2g_tx_stream->size);
  3534. */
  3535. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  3536. }
  3537. else if (rtn >= 0)
  3538. {
  3539. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR]Incomplete Tx ( %d / %d ). (Bytes, DEC): FAIL\n",
  3540. rtn, v2g_tx_stream->size);
  3541. }
  3542. else
  3543. {
  3544. errn = rtn;
  3545. DEBUG_PRINTF_EVCOMM_DETAIL("[ERROR]send(): %d (DEC)\n", errn);
  3546. }
  3547. //STEP 3: ========= Reset V2G MSG Flags ==========
  3548. init_iso2BodyType(&v2gObject->V2G_Message.Body);
  3549. return errn;
  3550. }
  3551. /**
  3552. *
  3553. * @param exi_doc_DIN
  3554. * @return
  3555. */
  3556. int Check_din_V2G_Rx_MSG_SessionID(struct dinEXIDocument *exi_doc_DIN)
  3557. {
  3558. int i = 0;
  3559. int leng = 0;
  3560. int errn = 0;
  3561. leng = exi_doc_DIN->V2G_Message.Header.SessionID.bytesLen;
  3562. //Step 1: Check SessionID Length
  3563. if (leng != 8) //8-byte
  3564. {
  3565. DEBUG_ERROR("SessionID: incorrect length(%d)\n", leng);
  3566. errn = -1;
  3567. }
  3568. else
  3569. {
  3570. //Step 2-1: Check SessionID content
  3571. for (i = 0; i < leng; i++)
  3572. {
  3573. if (exi_doc_DIN->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  3574. {
  3575. errn = -2;
  3576. break;
  3577. }
  3578. }
  3579. }
  3580. //Step 2-2: Print Incorrect ID
  3581. if (errn == -2) //incorrect ID
  3582. {
  3583. DEBUG_ERROR("SessionID: incorrect ID(RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)\n",
  3584. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[0],
  3585. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[1],
  3586. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[2],
  3587. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[3],
  3588. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[4],
  3589. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[5],
  3590. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[6],
  3591. exi_doc_DIN->V2G_Message.Header.SessionID.bytes[7],
  3592. EVCOMM_SYS_INFO.SessionID[0],
  3593. EVCOMM_SYS_INFO.SessionID[1],
  3594. EVCOMM_SYS_INFO.SessionID[2],
  3595. EVCOMM_SYS_INFO.SessionID[3],
  3596. EVCOMM_SYS_INFO.SessionID[4],
  3597. EVCOMM_SYS_INFO.SessionID[5],
  3598. EVCOMM_SYS_INFO.SessionID[6],
  3599. EVCOMM_SYS_INFO.SessionID[7]);
  3600. }
  3601. //Step 3: Correct SessionID for Res Message
  3602. if (errn != 0)
  3603. {
  3604. exi_doc_DIN->V2G_Message.Header.SessionID.bytesLen = 8;
  3605. memset(exi_doc_DIN->V2G_Message.Header.SessionID.bytes, 0, 8);
  3606. memcpy(exi_doc_DIN->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  3607. }
  3608. return errn;
  3609. }
  3610. /**
  3611. *
  3612. * @param exi_doc_ISO1
  3613. * @return
  3614. */
  3615. int Check_iso1_V2G_Rx_MSG_SessionID(struct iso1EXIDocument *exi_doc_ISO1)
  3616. {
  3617. int i = 0;
  3618. int leng = 0;
  3619. int errn = 0;
  3620. leng = exi_doc_ISO1->V2G_Message.Header.SessionID.bytesLen;
  3621. //Step 1: Check SessionID Length
  3622. if (leng != 8) //8-byte
  3623. {
  3624. DEBUG_ERROR("SessionID: incorrect length(%d)\n", leng);
  3625. errn = -1;
  3626. }
  3627. else
  3628. {
  3629. //Step 2-1: Check SessionID content
  3630. for (i = 0; i < leng; i++)
  3631. {
  3632. if (exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  3633. {
  3634. errn = -2;
  3635. break;
  3636. }
  3637. }
  3638. }
  3639. //Step 2-2: Print Incorrect ID
  3640. if (errn == -2) //incorrect ID
  3641. {
  3642. DEBUG_ERROR("SessionID: incorrect ID (RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)\n",
  3643. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[0],
  3644. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[1],
  3645. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[2],
  3646. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[3],
  3647. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[4],
  3648. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[5],
  3649. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[6],
  3650. exi_doc_ISO1->V2G_Message.Header.SessionID.bytes[7],
  3651. EVCOMM_SYS_INFO.SessionID[0],
  3652. EVCOMM_SYS_INFO.SessionID[1],
  3653. EVCOMM_SYS_INFO.SessionID[2],
  3654. EVCOMM_SYS_INFO.SessionID[3],
  3655. EVCOMM_SYS_INFO.SessionID[4],
  3656. EVCOMM_SYS_INFO.SessionID[5],
  3657. EVCOMM_SYS_INFO.SessionID[6],
  3658. EVCOMM_SYS_INFO.SessionID[7]);
  3659. }
  3660. //Step 3: Correct SessionID for Res Message
  3661. if (errn != 0)
  3662. {
  3663. exi_doc_ISO1->V2G_Message.Header.SessionID.bytesLen = 8;
  3664. memset(exi_doc_ISO1->V2G_Message.Header.SessionID.bytes, 0, 8);
  3665. memcpy(exi_doc_ISO1->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  3666. }
  3667. return errn;
  3668. }
  3669. /**
  3670. *
  3671. * @param exi_doc_ISO2
  3672. * @return
  3673. */
  3674. int Check_iso2_V2G_Rx_MSG_SessionID(struct iso2EXIDocument *exi_doc_ISO2)
  3675. {
  3676. int i = 0;
  3677. int leng = 0;
  3678. int errn = 0;
  3679. leng = exi_doc_ISO2->V2G_Message.Header.SessionID.bytesLen;
  3680. //Step 1: Check SessionID Length
  3681. if (leng != 8) //8-byte
  3682. {
  3683. DEBUG_ERROR("SessionID: incorrect length(%d)\n", leng);
  3684. errn = -1;
  3685. }
  3686. else
  3687. {
  3688. //Step 2-1: Check SessionID content
  3689. for (i = 0; i < leng; i++)
  3690. {
  3691. if (exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[i] != EVCOMM_SYS_INFO.SessionID[i])
  3692. {
  3693. errn = -2;
  3694. break;
  3695. }
  3696. }
  3697. }
  3698. //Step 2-2: Print Incorrect ID
  3699. if (errn == -2) //incorrect ID
  3700. {
  3701. DEBUG_ERROR("SessionID: incorrect ID(RX:%02X%02X%02X%02X%02X%02X%02X%02X, ORI:%02X%02X%02X%02X%02X%02X%02X%02X)\n",
  3702. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[0],
  3703. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[1],
  3704. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[2],
  3705. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[3],
  3706. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[4],
  3707. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[5],
  3708. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[6],
  3709. exi_doc_ISO2->V2G_Message.Header.SessionID.bytes[7],
  3710. EVCOMM_SYS_INFO.SessionID[0],
  3711. EVCOMM_SYS_INFO.SessionID[1],
  3712. EVCOMM_SYS_INFO.SessionID[2],
  3713. EVCOMM_SYS_INFO.SessionID[3],
  3714. EVCOMM_SYS_INFO.SessionID[4],
  3715. EVCOMM_SYS_INFO.SessionID[5],
  3716. EVCOMM_SYS_INFO.SessionID[6],
  3717. EVCOMM_SYS_INFO.SessionID[7]);
  3718. }
  3719. //Step 3: Correct SessionID for Res Message
  3720. if (errn != 0)
  3721. {
  3722. exi_doc_ISO2->V2G_Message.Header.SessionID.bytesLen = 8;
  3723. memset(exi_doc_ISO2->V2G_Message.Header.SessionID.bytes, 0, 8);
  3724. memcpy(exi_doc_ISO2->V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  3725. }
  3726. return errn;
  3727. }
  3728. /**
  3729. * 1. Get the SchemaID accroding to the input target
  3730. *V2GT_MSG_PROTOCOL_DIN70121: choose DIN 70121
  3731. *V2GT_MSG_PROTOCOL_ISO15118_2014: choose ISO 15118-2 (ed1)
  3732. *V2GT_MSG_PROTOCOL_ISO15118_2018: choose ISO 15118-20 (ed2)
  3733. *V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY: choose the one with the highest priority
  3734. 2. [To-do] Checking Major and Minor version
  3735. 3. The parsing method will not support those private protocols,
  3736. such as "usr:tesla...," since "tesla" is 5 bytes but "din" and "iso" are 3 bytes.
  3737. 4. [To-do] If the target is selected as DIN," but there is no DIN.
  3738. However, if EV and EVSE all support ISO, how to use ISO instead?
  3739. * @param target
  3740. * @return
  3741. */
  3742. int GetSchemaID_of_Protocol(unsigned char target)
  3743. {
  3744. int i = 0;
  3745. int ii = 0;
  3746. int id = 0;
  3747. unsigned char pri = 20; //priority = 1(highest)~20(lowerest)
  3748. char num[10];
  3749. //Choose the 1st protocol as default.
  3750. id = -1;
  3751. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  3752. for(i = 0; i < CCS_HANDSHAKE_PROTOCOLS.arrayLen; i++)
  3753. {
  3754. //Checking for <ProtocolNamespace>urn:din:70121:2012:MsgDef</ProtocolNamespace>
  3755. //[To-Do] Ignoring the priority from EV and force DIN 70121 as our only option. (for temp)
  3756. //[CAUTION] The parsing method will not support those private protocols, such as "usr:tesla..."
  3757. num[0] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[8];
  3758. num[1] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[9];
  3759. num[2] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[10];
  3760. num[3] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[11];
  3761. num[4] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[12];
  3762. num[5] = '\0';
  3763. if (atoi(num) == 70121)
  3764. {
  3765. DEBUG_INFO("support(%d/%d): DIN 70121(%d:v%d.%d;id=%d,pri=%d)\n",
  3766. (i+1),
  3767. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  3768. atoi(num),
  3769. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  3770. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  3771. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  3772. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  3773. if (target == V2GT_MSG_PROTOCOL_DIN70121)
  3774. {
  3775. DEBUG_INFO("select(%d/%d): DIN 70121(%d:v%d.%d;id=%d,pri=%d)\n",
  3776. (i+1),
  3777. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  3778. atoi(num),
  3779. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  3780. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  3781. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  3782. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  3783. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == DIN_SPEC_70121_2012_VersionNumberMajor)
  3784. {
  3785. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == DIN_SPEC_70121_2012_VersionNumberMinor)
  3786. {
  3787. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  3788. }
  3789. else
  3790. { //[TC_SECC_VTB_SupportedAppProtocol_005]
  3791. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  3792. }
  3793. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  3794. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  3795. return id;
  3796. }
  3797. else
  3798. {
  3799. //keep looking for the suitable protocol
  3800. }
  3801. }
  3802. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  3803. {
  3804. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  3805. {
  3806. ii = i;
  3807. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  3808. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  3809. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_DIN70121;
  3810. }
  3811. else
  3812. {
  3813. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  3814. }
  3815. }
  3816. else
  3817. {
  3818. //null
  3819. }
  3820. }
  3821. else if (atoi(num) == 15118)
  3822. {
  3823. //urn:din:70121:2012:MsgDef
  3824. //urn:iso:15118:2:2013:MsgDef
  3825. memset(num, 0, sizeof(num));
  3826. num[0] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[16];
  3827. num[1] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[17];
  3828. num[2] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[18];
  3829. num[3] = CCS_HANDSHAKE_PROTOCOLS.array[i].ProtocolNamespace.characters[19];
  3830. num[4] = '\0';
  3831. if (atoi(num) < 2018 && atoi(num) >= 2010)
  3832. {
  3833. DEBUG_INFO("support(%d/%d): ISO 15118-2(ed1,%d:v%d.%d;id=%d,pri=%d)\n",
  3834. (i+1),
  3835. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  3836. atoi(num),
  3837. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  3838. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  3839. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  3840. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  3841. if (target == V2GT_MSG_PROTOCOL_ISO15118_2014)
  3842. {
  3843. DEBUG_INFO("select(%d/%d): ISO 15118-2,ed1(%d:v%d.%d;id=%d,pri=%d)\n",
  3844. (i+1),
  3845. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  3846. atoi(num),
  3847. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  3848. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  3849. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  3850. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  3851. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == ISO1_15118_2013_VersionNumberMajor)
  3852. {
  3853. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == ISO1_15118_2013_VersionNumberMinor)
  3854. {
  3855. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  3856. }
  3857. else
  3858. {
  3859. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  3860. }
  3861. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  3862. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2014;
  3863. return id;
  3864. }
  3865. else
  3866. {
  3867. //keep looking for the suitable protocol
  3868. }
  3869. }
  3870. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  3871. {
  3872. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  3873. {
  3874. ii = i;
  3875. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  3876. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  3877. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2014;
  3878. }
  3879. else
  3880. {
  3881. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  3882. }
  3883. }
  3884. else
  3885. {
  3886. //null
  3887. }
  3888. }
  3889. else if (atoi(num) >= 2018 && atoi(num) <= 2100) // >= 2018
  3890. {
  3891. DEBUG_INFO("support(%d/%d): ISO 15118-20(ed2,%d:v%d.%d;id=%d,pri=%d)\n",
  3892. (i+1),
  3893. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  3894. atoi(num),
  3895. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  3896. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  3897. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  3898. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  3899. if (target == V2GT_MSG_PROTOCOL_ISO15118_2018)
  3900. {
  3901. DEBUG_INFO("select(%d/%d): ISO 15118-20,ed2(%d:v%d.%d;id=%d,pri=%d)\n",
  3902. (i+1),
  3903. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  3904. atoi(num),
  3905. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  3906. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  3907. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  3908. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  3909. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor == ISO2_15118_2018_VersionNumberMajor)
  3910. {
  3911. if (CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor == ISO2_15118_2018_VersionNumberMinor)
  3912. {
  3913. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiation;
  3914. }
  3915. else
  3916. {
  3917. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_OK_SuccessfulNegotiationWithMinorDeviation;
  3918. }
  3919. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  3920. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2018;
  3921. return id;
  3922. }
  3923. else
  3924. {
  3925. //keep looking for the suitable protocol
  3926. }
  3927. }
  3928. else if (target == V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY)
  3929. {
  3930. if (pri > CCS_HANDSHAKE_PROTOCOLS.array[i].Priority)
  3931. {
  3932. ii = i;
  3933. id = CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID;
  3934. pri = CCS_HANDSHAKE_PROTOCOLS.array[i].Priority;
  3935. ShmCcsData->CommProtocol = V2GT_MSG_PROTOCOL_ISO15118_2018;
  3936. }
  3937. else
  3938. {
  3939. //not using this SchemaID, and keep looking for that SchemaID with higer priority
  3940. }
  3941. }
  3942. else
  3943. {
  3944. //null
  3945. }
  3946. }
  3947. else
  3948. {
  3949. //Unexpected Year
  3950. DEBUG_INFO("unsupport(%d/%d): ISO 15118-X(unexpected year:%d,v%d.%d),id=%d,pri=%d\n",
  3951. (i+1),
  3952. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  3953. atoi(num),
  3954. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  3955. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  3956. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  3957. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  3958. }
  3959. }
  3960. else
  3961. {
  3962. DEBUG_INFO("unsupport protocol(%d/%d)(%d:v%d.%d;id=%d,pri=%d)\n",
  3963. (i+1),
  3964. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  3965. atoi(num),
  3966. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMajor,
  3967. CCS_HANDSHAKE_PROTOCOLS.array[i].VersionNumberMinor,
  3968. CCS_HANDSHAKE_PROTOCOLS.array[i].SchemaID,
  3969. CCS_HANDSHAKE_PROTOCOLS.array[i].Priority);
  3970. }
  3971. }
  3972. //The final result of highest priority protocol
  3973. DEBUG_INFO("select(%d/%d): pro=%d(0:DIN,1:ISO1,2:ISO2);id=%d,pri=%d\n",
  3974. (ii+1),
  3975. CCS_HANDSHAKE_PROTOCOLS.arrayLen,
  3976. ShmCcsData->CommProtocol,
  3977. id,
  3978. pri);
  3979. if (id < 0)
  3980. {
  3981. EVCOMM_SYS_INFO.SupportedAppProtocol_result = appHandresponseCodeType_Failed_NoNegotiation;
  3982. }
  3983. return id;
  3984. }
  3985. /**
  3986. *
  3987. * @param AcceptFd
  3988. * @return
  3989. */
  3990. int Proc_supportedAppProtocolRes(int AcceptFd)
  3991. {
  3992. int errn = 0;
  3993. bitstream_t v2g_tx_stream;
  3994. static struct ChargingInfoData *sys;
  3995. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  3996. //STEP 1: =========== Setting the Response Message ===========
  3997. init_appHandEXIDocument(&v2gObject.appHandshake);
  3998. v2gObject.appHandshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_OK_SuccessfulNegotiation;
  3999. v2gObject.appHandshake.supportedAppProtocolRes_isUsed = 1u;
  4000. //select the 1st one as the default
  4001. v2gObject.appHandshake.supportedAppProtocolRes.SchemaID = CCS_HANDSHAKE_PROTOCOLS.array[0].SchemaID;
  4002. v2gObject.appHandshake.supportedAppProtocolRes.SchemaID_isUsed = 1u;
  4003. int id = 0;
  4004. /*+++ 20200808, vern, support both DIN and ISO +++*/
  4005. //id = GetSchemaID_of_Protocol(V2GT_MSG_PROTOCOL_PREFERENCE); //output: EVCOMM_SYS_INFO.SupportedAppProtocol_result
  4006. id = GetSchemaID_of_Protocol(V2GT_MSG_PROTOCOL_HIGHEST_PRIORITY); //output: EVCOMM_SYS_INFO.SupportedAppProtocol_result
  4007. /*--- 20200808, vern, support both DIN and ISO ---*/
  4008. v2gObject.appHandshake.supportedAppProtocolRes.ResponseCode = EVCOMM_SYS_INFO.SupportedAppProtocol_result; //updating the response code
  4009. if (id < 0)
  4010. {
  4011. DEBUG_ERROR("No available CCS protocol (id = %d, preference = %d)\n", id, V2GT_MSG_PROTOCOL_PREFERENCE);
  4012. }
  4013. else
  4014. {
  4015. //selected SchemaID
  4016. v2gObject.appHandshake.supportedAppProtocolRes.SchemaID = (unsigned char) id;
  4017. }
  4018. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  4019. {
  4020. DEBUG_INFO("EVSE_Shutdown => End_Process\n");
  4021. v2gObject.appHandshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  4022. errn = -1;
  4023. }
  4024. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  4025. {
  4026. DEBUG_INFO("EVSE_EmergencyShutdown => End_Process\n");
  4027. v2gObject.appHandshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  4028. errn = -1;
  4029. }
  4030. else
  4031. {
  4032. //null
  4033. }
  4034. //Check for Permission Changing from TRUE to FALSE
  4035. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  4036. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  4037. ShmInternalComm->ChargingPermission == FALSE)
  4038. {
  4039. DEBUG_ERROR("[DIN][supportedAppProtocolRes]Permission OFF\n");
  4040. v2gObject.appHandshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  4041. //errn = -1;
  4042. }
  4043. #if (CP_PROTECTION_MECHANISM == ENABLE)
  4044. {
  4045. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  4046. {
  4047. //Detect for CP State should be 9V with 5% PWM or 100%PWM (State B1, B2)
  4048. if (sys->CpState != 2 && sys->CpState != 3) //State B1, B2
  4049. {
  4050. v2gObject.appHandshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  4051. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4052. Update_V2G_Status(Other_Fault);
  4053. DEBUG_INFO("Emergency Stop by CP Error (%d, %.02f V)\n",
  4054. sys->CpState,
  4055. sys->CpVoltage);
  4056. }
  4057. }
  4058. #else
  4059. {
  4060. //Detect for CP State should be 9V (State B)
  4061. if (sys->CpState != 3) //B2
  4062. {
  4063. v2gObject.appHandshake.supportedAppProtocolRes.ResponseCode = appHandresponseCodeType_Failed_NoNegotiation;
  4064. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4065. Update_V2G_Status(Other_Fault);
  4066. DEBUG_INFO("Emergency Stop by CP Error (%d, %.02f V)\n",
  4067. sys->CpState,
  4068. sys->CpVoltage);
  4069. }
  4070. }
  4071. #endif
  4072. }
  4073. #endif
  4074. //STEP 2: =========== Encode into EXI ===========
  4075. if ((errn = API_V2GMSG_EXI_Encoder_AppProtocol(&v2gObject.appHandshake, &v2g_tx_stream)) !=0)
  4076. {
  4077. DEBUG_PRINTF_EVCOMM_DETAIL("[Error]API_V2GMSG_EXI_Encoder_AppProtocol\n");
  4078. return errn;
  4079. }
  4080. //STEP 3: =========== Send Response Packet ===========
  4081. int Rtn = 0;
  4082. Rtn = send(AcceptFd, v2g_tx_stream.data, v2g_tx_stream.size, 0);
  4083. //Rtn = send(6, v2g_tx_stream.data, v2g_tx_stream.size, 0);
  4084. DEBUG_PRINTF_EVCOMM_DETAIL("Send V2GTP Packet Size = %d, Rtn = %d (Bytes, DEC)\n", v2g_tx_stream.size, Rtn);
  4085. if (Rtn < 0)
  4086. {
  4087. return Rtn;
  4088. }
  4089. //STEP 4: =========== Save into Share Memory =========
  4090. //[ To-Do] Adding a mechanism to choose DIN 70121 as our 1st priority. (for multiple protocols)
  4091. //STEP 5: =========== Updating the Flow State Flag =========
  4092. if (id < 0)
  4093. {
  4094. errn = -1;
  4095. }
  4096. //STEP 6: =========== Reset Flags ============
  4097. //Reset all EXI doc Req/Res _isUsed Flags after each Res Tx
  4098. init_appHandEXIDocument(&v2gObject.appHandshake);
  4099. return errn;
  4100. }
  4101. /**
  4102. *
  4103. * @param AcceptFd
  4104. * @return
  4105. */
  4106. int Proc_supportedAppProtocolReq(int AcceptFd)
  4107. {
  4108. //[ To-Do] analysis on Req message and choose the prefered protocol
  4109. //Default: DIN 70121 (find SchemaID)
  4110. int errn = 0;
  4111. DEBUG_INFO("Request in.\n");
  4112. SHM_Save_din_supportedAppProtocolReq(ShmCcsData, &v2gObject.appHandshake, ShmSysConfigAndInfo);
  4113. errn = Proc_supportedAppProtocolRes(AcceptFd);
  4114. if (errn == 0)
  4115. {
  4116. DEBUG_INFO("Response out.\n");
  4117. }
  4118. else
  4119. {
  4120. DEBUG_ERROR("Proc_supportedAppProtocolRes(): %d (DEC)\n", errn);
  4121. }
  4122. return errn;
  4123. }
  4124. /**
  4125. *
  4126. * @param AcceptFd
  4127. * @return
  4128. */
  4129. int Proc_din_SessionSetupRes(int AcceptFd)
  4130. {
  4131. //int i = 0;
  4132. int errn = 0;
  4133. bitstream_t v2g_tx_stream;
  4134. static struct ChargingInfoData *sys;
  4135. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  4136. size_t pos = 0;
  4137. v2g_tx_stream.pos = &pos;
  4138. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  4139. v2g_tx_stream.data = v2gBuffer.tx;
  4140. init_dinBodyType(&v2gObject.DIN.V2G_Message.Body);
  4141. init_dinSessionSetupResType(&v2gObject.DIN.V2G_Message.Body.SessionSetupRes);
  4142. // ====== [BODY (1/2) ResponseCode ======
  4143. v2gObject.DIN.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  4144. v2gObject.DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_OK_NewSessionEstablished;
  4145. //[HEADER] Assign Res SessionID
  4146. v2gObject.DIN.V2G_Message.Header.SessionID.bytesLen = 8;
  4147. memset(v2gObject.DIN.V2G_Message.Header.SessionID.bytes, 0, 8);
  4148. memcpy(v2gObject.DIN.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4149. //Check for SequenceError
  4150. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  4151. {
  4152. v2gObject.DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError;
  4153. DEBUG_ERROR("SequenceError => End_Process\n");
  4154. errn = -1;
  4155. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4156. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  4157. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4158. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4159. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4160. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  4161. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  4162. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  4163. }
  4164. //#if PARAMETER_NORMAL_MODE == ENABLE
  4165. //SHM_Read_din_V2GMSG_Header(&v2gObject.DIN, ShmCcsData);
  4166. //#endif
  4167. //Detect for CP State should be 9V (State B)
  4168. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  4169. {
  4170. #if CP_PROTECTION_MECHANISM == ENABLE
  4171. v2gObject.DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  4172. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4173. Update_V2G_Status(Other_Fault);
  4174. errn = -1;
  4175. DEBUG_INFO("Emergency Stop by CP Error (%d)\n", sys->CpState);
  4176. #else
  4177. DEBUG_INFO("Emergency Stop by CP Error (%d): ignored\n", sys->CpState);
  4178. #endif
  4179. }
  4180. //Check for shutdown commands from EVSE(DC Main Board)
  4181. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  4182. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  4183. {
  4184. v2gObject.DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  4185. DEBUG_INFO("Stop by EVSE(%d:normal, %d:emergency): (%d)\n", EVSE_Shutdown, EVSE_EmergencyShutdown, sys->DC_EVSEStatus);
  4186. errn = -1;
  4187. }
  4188. //Check for Permission Changing from TRUE to FALSE
  4189. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  4190. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  4191. ShmInternalComm->ChargingPermission == FALSE)
  4192. {
  4193. DEBUG_ERROR("[DIN]Permission OFF");
  4194. v2gObject.DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = dinresponseCodeType_FAILED;
  4195. errn = -1;
  4196. }
  4197. // ====== [BODY (2/3) EVSEID ======
  4198. //EVSEID = all zero
  4199. memset(v2gObject.DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytes, 0, sizeof(v2gObject.DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytes));
  4200. //vern, should be encode by SN
  4201. v2gObject.DIN.V2G_Message.Body.SessionSetupRes.EVSEID.bytesLen = 1; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  4202. // ====== [BODY (3/3) DateTimeNow ======
  4203. v2gObject.DIN.V2G_Message.Body.SessionSetupRes.DateTimeNow_isUsed = 1u;
  4204. v2gObject.DIN.V2G_Message.Body.SessionSetupRes.DateTimeNow = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  4205. #if PARAMETER_NORMAL_MODE == ENABLE
  4206. ///////////SHM_Read_din_SessionSetupRes(&v2gObject.DIN, ShmCcsData);
  4207. #endif
  4208. // ============ Encode and Send Response Message ===========
  4209. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  4210. {
  4211. DEBUG_ERROR("Tx encoded msg error\n");
  4212. errn = -1;
  4213. }
  4214. return errn;
  4215. }
  4216. /**
  4217. *
  4218. * @param AcceptFd
  4219. * @return
  4220. */
  4221. int Proc_iso1_SessionSetupRes(int AcceptFd)
  4222. {
  4223. //int i = 0;
  4224. int errn = 0;
  4225. bitstream_t v2g_tx_stream;
  4226. static struct ChargingInfoData *sys;
  4227. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  4228. size_t pos = 0;
  4229. v2g_tx_stream.pos = &pos;
  4230. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  4231. v2g_tx_stream.data = v2gBuffer.tx;
  4232. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  4233. init_iso1SessionSetupResType(&v2gObject.ISO1.V2G_Message.Body.SessionSetupRes);
  4234. // ====== [BODY (1/2) ResponseCode ======
  4235. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  4236. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_OK_NewSessionEstablished;
  4237. //[HEADER] Assign Res SessionID
  4238. v2gObject.ISO1.V2G_Message.Header.SessionID.bytesLen = 8;
  4239. memset(v2gObject.ISO1.V2G_Message.Header.SessionID.bytes, 0, 8);
  4240. memcpy(v2gObject.ISO1.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4241. //Check for SequenceError
  4242. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  4243. {
  4244. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  4245. DEBUG_ERROR("SequenceError => End_Process\n");
  4246. errn = -1;
  4247. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4248. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  4249. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4250. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4251. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4252. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  4253. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  4254. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  4255. }
  4256. //#if PARAMETER_NORMAL_MODE == ENABLE
  4257. //SHM_Read_iso1_V2GMSG_Header(&v2gObject.ISO1, ShmCcsData);
  4258. //#endif
  4259. //Detect for CP State should be 9V (State B)
  4260. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  4261. {
  4262. #if CP_PROTECTION_MECHANISM == ENABLE
  4263. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  4264. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4265. Update_V2G_Status(Other_Fault);
  4266. errn = -1;
  4267. DEBUG_INFO("Emergency Stop by CP Error (%d)\n", sys->CpState);
  4268. #else
  4269. DEBUG_INFO("Emergency Stop by CP Error (%d): ignored\n", sys->CpState);
  4270. #endif
  4271. }
  4272. //Check for shutdown commands from EVSE(DC Main Board)
  4273. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  4274. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  4275. {
  4276. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  4277. DEBUG_INFO("Stop by EVSE(%d:normal, %d:emergency): (%d)\n",
  4278. EVSE_Shutdown,
  4279. EVSE_EmergencyShutdown,
  4280. sys->DC_EVSEStatus);
  4281. errn = -1;
  4282. }
  4283. //Check for Permission Changing from TRUE to FALSE
  4284. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  4285. ShmInternalComm->ChargingPermission == FALSE)
  4286. {
  4287. DEBUG_ERROR("Permission OFF\n");
  4288. v2gObject.DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED;
  4289. errn = -1;
  4290. }
  4291. // ====== [BODY (2/3) EVSEID ======
  4292. //EVSEID = all zero
  4293. memset(v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters, 0, sizeof(v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters));
  4294. /*+++ 20200808, vern, set default EVSEID +++*/
  4295. //vern, should be encoded by SN
  4296. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[0]='Z';
  4297. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[1]='Z';
  4298. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[2]='0';
  4299. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[3]='0';
  4300. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[4]='0';
  4301. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[5]='0';
  4302. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.characters[6]='0';
  4303. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 7; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  4304. /*--- 20200808, vern, set default EVSEID ---*/
  4305. // ====== [BODY (3/3) DateTimeNow ======
  4306. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSETimeStamp_isUsed = 1u;
  4307. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.EVSETimeStamp = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  4308. #if PARAMETER_NORMAL_MODE == ENABLE
  4309. ///////////SHM_Read_iso1_SessionSetupRes(&v2gObject.ISO1, ShmCcsData);
  4310. #endif
  4311. // ============ Encode and Send Response Message ===========
  4312. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  4313. {
  4314. DEBUG_ERROR("Tx encoded msg error\n");
  4315. errn = -1;
  4316. }
  4317. return errn;
  4318. }
  4319. /**
  4320. *
  4321. * @param AcceptFd
  4322. * @return
  4323. */
  4324. int Proc_iso2_SessionSetupRes(int AcceptFd)
  4325. {
  4326. //int i = 0;
  4327. int errn = 0;
  4328. bitstream_t v2g_tx_stream;
  4329. static struct ChargingInfoData *sys;
  4330. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  4331. size_t pos = 0;
  4332. v2g_tx_stream.pos = &pos;
  4333. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  4334. v2g_tx_stream.data = v2gBuffer.tx;
  4335. init_iso2BodyType(&v2gObject.ISO2.V2G_Message.Body);
  4336. init_iso2SessionSetupResType(&v2gObject.ISO2.V2G_Message.Body.SessionSetupRes);
  4337. // ====== [BODY (1/2) ResponseCode ======
  4338. v2gObject.ISO2.V2G_Message.Body.SessionSetupRes_isUsed = 1u;
  4339. v2gObject.ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_OK_NewSessionEstablished;
  4340. //[HEADER] Assign Res SessionID
  4341. v2gObject.ISO2.V2G_Message.Header.SessionID.bytesLen = 8;
  4342. memset(v2gObject.ISO2.V2G_Message.Header.SessionID.bytes, 0, 8);
  4343. memcpy(v2gObject.ISO2.V2G_Message.Header.SessionID.bytes, EVCOMM_SYS_INFO.SessionID, 8);
  4344. //Check for SequenceError
  4345. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  4346. {
  4347. v2gObject.ISO1.V2G_Message.Body.SessionSetupRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  4348. DEBUG_ERROR("SequenceError => End_Process\n");
  4349. errn = -1;
  4350. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4351. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  4352. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4353. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4354. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4355. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  4356. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  4357. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  4358. }
  4359. //#if PARAMETER_NORMAL_MODE == ENABLE
  4360. //SHM_Read_iso2_V2GMSG_Header(&v2gObject.ISO2, ShmCcsData);
  4361. //#endif
  4362. //Detect for CP State should be 9V (State B)
  4363. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  4364. {
  4365. #if CP_PROTECTION_MECHANISM == ENABLE
  4366. v2gObject.ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  4367. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4368. Update_V2G_Status(Other_Fault);
  4369. errn = -1;
  4370. DEBUG_INFO("Emergency Stop by CP Error (%d)\n", sys->CpState);
  4371. #else
  4372. DEBUG_INFO("Emergency Stop by CP Error (%d): ignored\n", sys->CpState);
  4373. #endif
  4374. }
  4375. //Check for shutdown commands from EVSE(DC Main Board)
  4376. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  4377. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  4378. {
  4379. v2gObject.ISO2.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  4380. DEBUG_INFO("Stop by EVSE(%d:normal, %d:emergency): (%d)\n",
  4381. EVSE_Shutdown,
  4382. EVSE_EmergencyShutdown,
  4383. sys->DC_EVSEStatus);
  4384. errn = -1;
  4385. }
  4386. //Check for Permission Changing from TRUE to FALSE
  4387. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  4388. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  4389. ShmInternalComm->ChargingPermission == FALSE)
  4390. {
  4391. DEBUG_ERROR("Permission OFF\n");
  4392. v2gObject.DIN.V2G_Message.Body.SessionSetupRes.ResponseCode = iso2responseCodeType_FAILED;
  4393. errn = -1;
  4394. }
  4395. // ====== [BODY (2/3) EVSEID ======
  4396. //EVSEID = all zero
  4397. memset(v2gObject.ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.characters, 0, sizeof(v2gObject.ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.characters));
  4398. v2gObject.ISO2.V2G_Message.Body.SessionSetupRes.EVSEID.charactersLen = 15; //max: DIN = 32, ISO1/ISO2 = 37 bytes
  4399. // ====== [BODY (3/3) DateTimeNow ======
  4400. v2gObject.ISO2.V2G_Message.Body.SessionSetupRes.EVSETimeStamp_isUsed = 1u;
  4401. v2gObject.ISO2.V2G_Message.Body.SessionSetupRes.EVSETimeStamp = (int64_t)time(NULL); //[Joseph] Format: Unix Time Stamp
  4402. #if PARAMETER_NORMAL_MODE == ENABLE
  4403. ///////////SHM_Read_iso2_SessionSetupRes(&v2gObject.ISO2, ShmCcsData);
  4404. #endif
  4405. // ============ Encode and Send Response Message ===========
  4406. if (send_encoded_iso2_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO2) != 0)
  4407. {
  4408. DEBUG_ERROR("Tx encoded msg error\n");
  4409. errn = -1;
  4410. }
  4411. return errn;
  4412. }
  4413. /**
  4414. *
  4415. * @param AcceptFd
  4416. * @return
  4417. */
  4418. int Proc_din_SessionSetupReq(int AcceptFd)
  4419. {
  4420. int errn = 0;
  4421. DEBUG_INFO("Request in.\n");
  4422. //Print the decoded XML Document
  4423. PRINT_XML_DOC_DIN_SessionSetupReq(&v2gObject.DIN);
  4424. //Save into Share Memory
  4425. SHM_Save_din_SessionSetupReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  4426. errn = Proc_din_SessionSetupRes(AcceptFd);
  4427. if (errn == 0)
  4428. {
  4429. //successfully send response.
  4430. DEBUG_INFO("Response out.\n");
  4431. }
  4432. else
  4433. {
  4434. DEBUG_ERROR("Proc_din_SessionSetupRes(): %d (DEC)\n", errn);
  4435. }
  4436. return errn;
  4437. }
  4438. /**
  4439. *
  4440. * @param AcceptFd
  4441. * @return
  4442. */
  4443. int Proc_iso1_SessionSetupReq(int AcceptFd)
  4444. {
  4445. int errn = 0;
  4446. DEBUG_INFO("Request in.\n");
  4447. //Print the decoded XML Document
  4448. PRINT_XML_DOC_ISO1_SessionSetupReq(&v2gObject.ISO1);
  4449. //Save into Share Memory
  4450. SHM_Save_iso1_SessionSetupReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  4451. errn = Proc_iso1_SessionSetupRes(AcceptFd);
  4452. if (errn == 0)
  4453. {
  4454. //successfully send response.
  4455. DEBUG_INFO("Response out.\n");
  4456. }
  4457. else
  4458. {
  4459. DEBUG_ERROR("Proc_iso1_SessionSetupRes(): %d (DEC)\n", errn);
  4460. }
  4461. return errn;
  4462. }
  4463. /**
  4464. *
  4465. * @param AcceptFd
  4466. * @return
  4467. */
  4468. int Proc_iso2_SessionSetupReq(int AcceptFd)
  4469. {
  4470. int errn = 0;
  4471. DEBUG_INFO("Request in.\n");
  4472. //Print the decoded XML Document
  4473. PRINT_XML_DOC_ISO2_SessionSetupReq(&v2gObject.ISO2);
  4474. //Save into Share Memory
  4475. SHM_Save_iso2_SessionSetupReq(ShmCcsData, &v2gObject.ISO2, ShmSysConfigAndInfo);
  4476. errn = Proc_iso2_SessionSetupRes(AcceptFd);
  4477. if (errn == 0)
  4478. {
  4479. //successfully send response.
  4480. DEBUG_INFO("Response out.\n");
  4481. }
  4482. else
  4483. {
  4484. DEBUG_ERROR("Proc_iso2_SessionSetupRes(): %d (DEC)\n", errn);
  4485. }
  4486. return errn;
  4487. }
  4488. /**
  4489. *
  4490. * @param AcceptFd
  4491. * @return
  4492. */
  4493. int Proc_din_ServiceDiscoveryRes(int AcceptFd)
  4494. {
  4495. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  4496. //if it is not the same, the packet should be ignored.
  4497. //int i = 0;
  4498. int errn = 0;
  4499. bitstream_t v2g_tx_stream;
  4500. struct ChargingInfoData *sys;
  4501. //struct ServiceDiscoveryRequest_DIN70121 *req;
  4502. struct ServiceDiscoveryResponse_DIN70121 *res;
  4503. size_t pos = 0;
  4504. v2g_tx_stream.pos = &pos;
  4505. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  4506. v2g_tx_stream.data = v2gBuffer.tx;
  4507. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  4508. //req = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryRequest;
  4509. res = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryResponse;
  4510. init_dinBodyType(&v2gObject.DIN.V2G_Message.Body);
  4511. init_dinServiceDiscoveryResType(&v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes);
  4512. //[1/4] Response Code
  4513. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes_isUsed = 1u;
  4514. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_OK;
  4515. //[HEADER] Check Req SessionID
  4516. if (Check_din_V2G_Rx_MSG_SessionID(&v2gObject.DIN) < 0)
  4517. {
  4518. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  4519. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  4520. errn = -1;
  4521. }
  4522. //Check for SequenceError
  4523. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  4524. {
  4525. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  4526. DEBUG_ERROR("SequenceError => End_Process\n");
  4527. errn = -1;
  4528. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4529. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  4530. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4531. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4532. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4533. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  4534. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  4535. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  4536. }
  4537. //Detect for CP State should be 9V (State B)
  4538. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  4539. {
  4540. #if CP_PROTECTION_MECHANISM == ENABLE
  4541. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  4542. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4543. Update_V2G_Status(Other_Fault);
  4544. errn = -1;
  4545. DEBUG_INFO("Emergency Stop by CP Error (%d)\n", sys->CpState);
  4546. #else
  4547. DEBUG_INFO("Emergency Stop by CP Error (%d): ignored\n", sys->CpState);
  4548. #endif
  4549. }
  4550. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  4551. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  4552. {
  4553. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  4554. DEBUG_INFO("shutdown by EVSE\n");
  4555. errn = -1;
  4556. }
  4557. //Check for Permission Changing from TRUE to FALSE
  4558. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  4559. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  4560. ShmInternalComm->ChargingPermission == FALSE)
  4561. {
  4562. DEBUG_ERROR("[DIN]Permission OFF\n");
  4563. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = dinresponseCodeType_FAILED;
  4564. errn = -1;
  4565. }
  4566. //[2/4] PaymentOptions
  4567. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptions.PaymentOption.arrayLen = 1u;
  4568. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptions.PaymentOption.array[0] = dinpaymentOptionType_ExternalPayment; //1
  4569. //[3/4] Charge Service
  4570. res->ChargeService_DIN70121.Services.ServiceTag.ServiceID = 1;
  4571. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceTag.ServiceID = (unsigned short) res->ChargeService_DIN70121.Services.ServiceTag.ServiceID;
  4572. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceTag.ServiceCategory = dinserviceCategoryType_EVCharging;
  4573. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.FreeService = 1; //[Joseph] for test
  4574. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.EnergyTransferType = dinEVSESupportedEnergyTransferType_DC_extended;
  4575. //[4/4] Service List (null, not be uesed for now.)
  4576. //#if PARAMETER_NORMAL_MODE == ENABLE
  4577. ///////////////SHM_Read_din_ServiceDiscoveryRes(&v2gObject.DIN, ShmCcsData);
  4578. //#endif
  4579. // ============ Encode and Send Response Message ===========
  4580. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  4581. {
  4582. DEBUG_ERROR("Tx encoded msg error\n");
  4583. errn = -1;
  4584. }
  4585. return errn;
  4586. }
  4587. /**
  4588. *
  4589. * @param AcceptFd
  4590. * @return
  4591. */
  4592. int Proc_iso1_ServiceDiscoveryRes(int AcceptFd)
  4593. {
  4594. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  4595. //if it is not the same, the packet should be ignored.
  4596. //int i = 0;
  4597. int errn = 0;
  4598. bitstream_t v2g_tx_stream;
  4599. struct ChargingInfoData *sys;
  4600. //struct ServiceDiscoveryRequest_ISO15118_2014 *req;
  4601. struct ServiceDiscoveryResponse_ISO15118_2014 *res;
  4602. size_t pos = 0;
  4603. v2g_tx_stream.pos = &pos;
  4604. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  4605. v2g_tx_stream.data = v2gBuffer.tx;
  4606. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  4607. //req = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryRequest;
  4608. res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryResponse;
  4609. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  4610. init_iso1ServiceDiscoveryResType(&v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes);
  4611. //[1/4] Response Code
  4612. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes_isUsed = 1u;
  4613. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_OK;
  4614. //[HEADER] Check Req SessionID
  4615. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  4616. {
  4617. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  4618. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  4619. errn = -1;
  4620. }
  4621. //Check for SequenceError
  4622. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  4623. {
  4624. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  4625. DEBUG_ERROR("SequenceError => End_Process\n");
  4626. errn = -1;
  4627. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4628. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  4629. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4630. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4631. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4632. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  4633. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  4634. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  4635. }
  4636. //Detect for CP State should be 9V (State B)
  4637. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  4638. {
  4639. #if CP_PROTECTION_MECHANISM == ENABLE
  4640. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  4641. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4642. Update_V2G_Status(Other_Fault);
  4643. errn = -1;
  4644. DEBUG_INFO("Emergency Stop by CP Error (%d)\n", sys->CpState);
  4645. #else
  4646. DEBUG_INFO("Emergency Stop by CP Error (%d): ignored", sys->CpState);
  4647. #endif
  4648. }
  4649. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  4650. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  4651. {
  4652. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  4653. DEBUG_INFO("shutdown by EVSE\n");
  4654. errn = -1;
  4655. }
  4656. //Check for Permission Changing from TRUE to FALSE
  4657. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  4658. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  4659. ShmInternalComm->ChargingPermission == FALSE)
  4660. {
  4661. DEBUG_ERROR("Permission OFF\n");
  4662. v2gObject.DIN.V2G_Message.Body.ServiceDiscoveryRes.ResponseCode = iso1responseCodeType_FAILED;
  4663. errn = -1;
  4664. }
  4665. //[2/4] PaymentOptionList
  4666. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptionList.PaymentOption.arrayLen = 1u;
  4667. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.PaymentOptionList.PaymentOption.array[0] = iso1paymentOptionType_ExternalPayment; //1
  4668. //[3/4] Charge Service
  4669. res->ChargeService.Services.ServiceID = 1;
  4670. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceID = (unsigned short) res->ChargeService.Services.ServiceID;
  4671. //v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName_isUsed = 1; //new in ISO1, not be used, yet.
  4672. //v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName.charactersLen = xxx;
  4673. //v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceName.characters[i] = xxx;
  4674. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.ServiceCategory = iso1serviceCategoryType_EVCharging;
  4675. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.FreeService = 1; //[Joseph] for test
  4676. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.arrayLen = 1u; // max = 6
  4677. switch (ShmCcsData->EnergyTransferMode)
  4678. {
  4679. case DC_extended:
  4680. {
  4681. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_DC_extended;
  4682. break;
  4683. }
  4684. case AC_single_phase_core:
  4685. {
  4686. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_AC_single_phase_core;
  4687. break;
  4688. }
  4689. case AC_three_phase_core:
  4690. {
  4691. v2gObject.ISO1.V2G_Message.Body.ServiceDiscoveryRes.ChargeService.SupportedEnergyTransferMode.EnergyTransferMode.array[0] = iso1EnergyTransferModeType_AC_three_phase_core;
  4692. break;
  4693. }
  4694. default:
  4695. {
  4696. DEBUG_WARN("unexpected EnergyTransferMode(%d)\n", ShmCcsData->EnergyTransferMode);
  4697. break;
  4698. }
  4699. }
  4700. //[4/4] Service List (null, not be uesed for now.)
  4701. //#if PARAMETER_NORMAL_MODE == ENABLE
  4702. ///////////////SHM_Read_iso1_ServiceDiscoveryRes(&v2gObject.ISO1, ShmCcsData);
  4703. //#endif
  4704. // ============ Encode and Send Response Message ===========
  4705. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  4706. {
  4707. DEBUG_ERROR("Tx encoded msg error\n");
  4708. errn = -1;
  4709. }
  4710. return errn;
  4711. }
  4712. /**
  4713. *
  4714. * @param AcceptFd
  4715. * @return
  4716. */
  4717. int Proc_din_ServiceDiscoveryReq(int AcceptFd)
  4718. {
  4719. int errn = 0;
  4720. DEBUG_INFO("Request in.\n");
  4721. //Print the decoded XML Document
  4722. PRINT_XML_DOC_DIN_ServiceDiscoveryReq(&v2gObject.DIN);
  4723. //Save into Share Memory
  4724. SHM_Save_din_ServiceDiscoveryReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  4725. errn = Proc_din_ServiceDiscoveryRes(AcceptFd);
  4726. if (errn == 0)
  4727. {
  4728. //send response successfully.
  4729. DEBUG_INFO("Response out.\n");
  4730. }
  4731. else
  4732. {
  4733. DEBUG_ERROR("Proc_din_ServiceDiscoveryRes(): %d (DEC)\n", errn);
  4734. }
  4735. return errn;
  4736. }
  4737. /**
  4738. *
  4739. * @param AcceptFd
  4740. * @return
  4741. */
  4742. int Proc_iso1_ServiceDiscoveryReq(int AcceptFd)
  4743. {
  4744. int errn = 0;
  4745. DEBUG_INFO("Request in.\n");
  4746. //Print the decoded XML Document
  4747. PRINT_XML_DOC_ISO1_ServiceDiscoveryReq(&v2gObject.ISO1);
  4748. //Save into Share Memory
  4749. SHM_Save_iso1_ServiceDiscoveryReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  4750. errn = Proc_iso1_ServiceDiscoveryRes(AcceptFd);
  4751. if (errn == 0)
  4752. {
  4753. //send response successfully.
  4754. DEBUG_INFO("Response out.\n");
  4755. }
  4756. else
  4757. {
  4758. DEBUG_ERROR("Proc_iso1_ServiceDiscoveryRes(): %d (DEC)\n", errn);
  4759. }
  4760. return errn;
  4761. }
  4762. /**
  4763. *
  4764. * @param AcceptFd
  4765. * @return
  4766. */
  4767. int Proc_din_ServiceAndPaymentSelectionRes(int AcceptFd)
  4768. {
  4769. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  4770. //if it is not the same, the packet should be ignored.
  4771. //int i = 0;
  4772. int errn = 0;
  4773. bitstream_t v2g_tx_stream;
  4774. struct ServiceAndPaymentSelectionRequest_DIN70121 *req;
  4775. //struct ServiceAndPaymentSelectionResponse_DIN70121 *res;
  4776. struct ServiceDiscoveryResponse_DIN70121 *sd_res;
  4777. struct ChargingInfoData *sys;
  4778. size_t pos = 0;
  4779. v2g_tx_stream.pos = &pos;
  4780. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  4781. v2g_tx_stream.data = v2gBuffer.tx;
  4782. req = &ShmCcsData->V2GMessage_DIN70121.ServiceAndPaymentSelectionRequest;
  4783. //res = &ShmCcsData->V2GMessage_DIN70121.ServiceAndPaymentSelectionResponse;
  4784. sd_res = &ShmCcsData->V2GMessage_DIN70121.ServiceDiscoveryResponse;
  4785. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  4786. init_dinBodyType(&v2gObject.DIN.V2G_Message.Body);
  4787. init_dinServicePaymentSelectionResType(&v2gObject.DIN.V2G_Message.Body.ServicePaymentSelectionRes);
  4788. //[1/1] Response Code
  4789. v2gObject.DIN.V2G_Message.Body.ServicePaymentSelectionRes_isUsed = 1u;
  4790. v2gObject.DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_OK;
  4791. //[HEADER] Check Req SessionID
  4792. if (Check_din_V2G_Rx_MSG_SessionID(&v2gObject.DIN) < 0)
  4793. {
  4794. v2gObject.DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  4795. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  4796. errn = -1;
  4797. }
  4798. //Check for SequenceError
  4799. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  4800. {
  4801. v2gObject.DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  4802. DEBUG_ERROR("SequenceError => End_Process\n");
  4803. errn = -1;
  4804. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4805. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  4806. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4807. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4808. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4809. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  4810. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  4811. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  4812. }
  4813. //Check for SelectedPaymentOption (TC_SECC_VTB_ServicePaymentSelection_007)
  4814. if (req->SelectedPaymentOption != ExternalPayment) //1
  4815. {
  4816. v2gObject.DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_PaymentSelectionInvalid;
  4817. DEBUG_ERROR("unexpected SelectedPaymentOption(%d) => End_Process (EIM only, no PnC, yet.)\n", req->SelectedPaymentOption);
  4818. errn = -1;
  4819. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4820. //CCS_SECC_ResponseCode_FAILED_Payment SelectionInvalid (023762)
  4821. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4822. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4823. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4824. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  4825. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  4826. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  4827. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4828. }
  4829. //Check for ServiceID (TC_SECC_VTB_ServicePaymentSelection_004)
  4830. if (req->SelectedServiceList.SelectedService[0].ServiceID != sd_res->ChargeService_DIN70121.Services.ServiceTag.ServiceID)
  4831. {
  4832. v2gObject.DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED_ServiceSelectionInvalid; //8
  4833. DEBUG_ERROR("Wrong selected ServiceID(%d) => End_Process\n", req->SelectedServiceList.SelectedService[0].ServiceID);
  4834. errn = -1;
  4835. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4836. //CCS_SECC_ResponseCode_FAILED_ServiceSelectionInvalid (023764)
  4837. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4838. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4839. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4840. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  4841. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  4842. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  4843. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4844. }
  4845. //Detect for CP State should be 9V (State B)
  4846. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  4847. {
  4848. #if CP_PROTECTION_MECHANISM == ENABLE
  4849. v2gObject.DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  4850. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4851. Update_V2G_Status(Other_Fault);
  4852. errn = -1;
  4853. DEBUG_INFO("Emergency Stop by CP Error (%d)\n", sys->CpState);
  4854. #else
  4855. DEBUG_INFO("Emergency Stop by CP Error (%d): ignored due to function is disabled\n", sys->CpState);
  4856. #endif
  4857. }
  4858. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  4859. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  4860. {
  4861. DEBUG_INFO("shutdown by EVSE\n");
  4862. v2gObject.DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  4863. errn = -1;
  4864. }
  4865. //Check for Permission Changing from TRUE to FALSE
  4866. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  4867. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  4868. ShmInternalComm->ChargingPermission == FALSE)
  4869. {
  4870. DEBUG_ERROR("Permission OFF\n");
  4871. v2gObject.DIN.V2G_Message.Body.ServicePaymentSelectionRes.ResponseCode = dinresponseCodeType_FAILED;
  4872. errn = -1;
  4873. }
  4874. // ============ Encode and Send Response Message ===========
  4875. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  4876. {
  4877. DEBUG_ERROR("Tx encoded msg error\n");
  4878. errn = -1;
  4879. }
  4880. return errn;
  4881. }
  4882. /**
  4883. *
  4884. * @param AcceptFd
  4885. * @return
  4886. */
  4887. int Proc_iso1_ServiceAndPaymentSelectionRes(int AcceptFd)
  4888. {
  4889. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  4890. //if it is not the same, the packet should be ignored.
  4891. //int i = 0;
  4892. int errn = 0;
  4893. bitstream_t v2g_tx_stream;
  4894. struct ServiceAndPaymentSelectionRequest_ISO15118_2014 *req;
  4895. //struct ServiceAndPaymentSelectionResponse_ISO15118_2014 *res;
  4896. struct ServiceDiscoveryResponse_ISO15118_2014 *sd_res;
  4897. static struct ChargingInfoData *sys;
  4898. size_t pos = 0;
  4899. v2g_tx_stream.pos = &pos;
  4900. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  4901. v2g_tx_stream.data = v2gBuffer.tx;
  4902. req = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceAndPaymentSelectionRequest;
  4903. //res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceAndPaymentSelectionResponse;
  4904. sd_res = &ShmCcsData->V2GMessage_ISO15118_2014.ServiceDiscoveryResponse;
  4905. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  4906. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  4907. init_iso1PaymentServiceSelectionResType(&v2gObject.ISO1.V2G_Message.Body.PaymentServiceSelectionRes);
  4908. //[1/1] Response Code
  4909. v2gObject.ISO1.V2G_Message.Body.PaymentServiceSelectionRes_isUsed = 1u;
  4910. v2gObject.ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_OK;
  4911. //[HEADER] Check Req SessionID
  4912. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  4913. {
  4914. v2gObject.ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  4915. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  4916. errn = -1;
  4917. }
  4918. //Check for SequenceError
  4919. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  4920. {
  4921. v2gObject.ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  4922. DEBUG_ERROR("SequenceError => End_Process\n");
  4923. errn = -1;
  4924. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4925. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  4926. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4927. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4928. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4929. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  4930. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  4931. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  4932. }
  4933. //Check for SelectedPaymentOption (TC_SECC_VTB_ServicePaymentSelection_007)
  4934. if (req->SelectedPaymentOption != ExternalPayment) //1
  4935. {
  4936. v2gObject.ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_PaymentSelectionInvalid;
  4937. DEBUG_ERROR("unexpected SelectedPaymentOption(%d) => End_Process (EIM only, no PnC, yet.)\n", req->SelectedPaymentOption);
  4938. errn = -1;
  4939. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4940. //CCS_SECC_ResponseCode_FAILED_Payment SelectionInvalid (023762)
  4941. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4942. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4943. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4944. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  4945. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  4946. ShmStatusCodeData->PresentStatusCode[0][5] = 2;
  4947. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4948. }
  4949. //Check for ServiceID (TC_SECC_VTB_ServicePaymentSelection_004)
  4950. if (req->SelectedServiceList.SelectedService[0].ServiceID != sd_res->ChargeService.Services.ServiceID)
  4951. {
  4952. v2gObject.ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED_ServiceSelectionInvalid; //8
  4953. DEBUG_ERROR("Wrong selected ServiceID(%d) => End_Process\n", req->SelectedServiceList.SelectedService[0].ServiceID);
  4954. errn = -1;
  4955. //Update_ShmStatusCode(); //[To-Do] to be implemented
  4956. //CCS_SECC_ResponseCode_FAILED_ServiceSelectionInvalid (023764)
  4957. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  4958. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  4959. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  4960. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  4961. ShmStatusCodeData->PresentStatusCode[0][4] = 6;
  4962. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  4963. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4964. }
  4965. //Detect for CP State should be 9V (State B)
  4966. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  4967. {
  4968. #if CP_PROTECTION_MECHANISM == ENABLE
  4969. v2gObject.ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  4970. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  4971. Update_V2G_Status(Other_Fault);
  4972. errn = -1;
  4973. DEBUG_INFO("Emergency Stop by CP Error (%d)\n", sys->CpState);
  4974. #else
  4975. DEBUG_INFO("Emergency Stop by CP Error (%d): ignored due to function is disabled\n", sys->CpState);
  4976. #endif
  4977. }
  4978. if (sys->DC_EVSEStatus == EVSE_Shutdown ||
  4979. sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  4980. {
  4981. DEBUG_INFO("shutdown by EVSE\n");
  4982. v2gObject.ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  4983. errn = -1;
  4984. }
  4985. //Check for Permission Changing from TRUE to FALSE
  4986. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  4987. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  4988. ShmInternalComm->ChargingPermission == FALSE)
  4989. {
  4990. DEBUG_ERROR("Permission OFF\n");
  4991. v2gObject.ISO1.V2G_Message.Body.PaymentServiceSelectionRes.ResponseCode = iso1responseCodeType_FAILED;
  4992. errn = -1;
  4993. }
  4994. // ============ Encode and Send Response Message ===========
  4995. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  4996. {
  4997. DEBUG_ERROR("Tx encoded msg error\n");
  4998. errn = -1;
  4999. }
  5000. return errn;
  5001. }
  5002. /**
  5003. *
  5004. * @param AcceptFd
  5005. * @return
  5006. */
  5007. int Proc_din_ServiceAndPaymentSelectionReq(int AcceptFd)
  5008. {
  5009. int errn = 0;
  5010. DEBUG_INFO("Request in.\n");
  5011. //Print the decoded XML Document
  5012. PRINT_XML_DOC_DIN_ServiceAndPaymentSelectionReq(&v2gObject.DIN);
  5013. //Save into Share Memory
  5014. SHM_Save_din_ServiceAndPaymentSelectionReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  5015. errn = Proc_din_ServiceAndPaymentSelectionRes(AcceptFd);
  5016. if (errn == 0)
  5017. {
  5018. //send response successfully.
  5019. DEBUG_INFO("Response out.\n");
  5020. }
  5021. else
  5022. {
  5023. DEBUG_ERROR("Proc_din_ServiceAndPaymentSelectionRes(): %d (DEC)\n", errn);
  5024. }
  5025. return errn;
  5026. }
  5027. /**
  5028. *
  5029. * @param AcceptFd
  5030. * @return
  5031. */
  5032. int Proc_iso1_ServiceAndPaymentSelectionReq(int AcceptFd)
  5033. {
  5034. int errn = 0;
  5035. DEBUG_INFO("Request in.\n");
  5036. //Print the decoded XML Document
  5037. PRINT_XML_DOC_ISO1_ServiceAndPaymentSelectionReq(&v2gObject.ISO1);
  5038. //Save into Share Memory
  5039. SHM_Save_iso1_ServiceAndPaymentSelectionReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  5040. errn = Proc_iso1_ServiceAndPaymentSelectionRes(AcceptFd);
  5041. if (errn == 0)
  5042. {
  5043. //send response successfully.
  5044. DEBUG_INFO("Response out.\n");
  5045. }
  5046. else
  5047. {
  5048. DEBUG_ERROR("Proc_iso1_ServiceAndPaymentSelectionRes(): %d (DEC)\n", errn);
  5049. }
  5050. return errn;
  5051. }
  5052. /**
  5053. *
  5054. * @param AcceptFd
  5055. * @return
  5056. */
  5057. int Proc_din_ContractAuthenticationRes(int AcceptFd)
  5058. {
  5059. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  5060. //if it is not the same, the packet should be ignored.
  5061. //int i = 0;
  5062. int errn = 0;
  5063. bitstream_t v2g_tx_stream;
  5064. static struct ChargingInfoData *sys;
  5065. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5066. size_t pos = 0;
  5067. v2g_tx_stream.pos = &pos;
  5068. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5069. v2g_tx_stream.data = v2gBuffer.tx;
  5070. init_dinBodyType(&v2gObject.DIN.V2G_Message.Body);
  5071. init_dinContractAuthenticationResType(&v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes);
  5072. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes_isUsed = 1u;
  5073. //[BODY (1/2)] ResponseCode
  5074. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_OK;
  5075. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Ongoing; //0
  5076. //[HEADER] Check Req SessionID
  5077. if (Check_din_V2G_Rx_MSG_SessionID(&v2gObject.DIN) < 0)
  5078. {
  5079. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  5080. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  5081. errn = -1;
  5082. }
  5083. //Check for SequenceError
  5084. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5085. {
  5086. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  5087. DEBUG_ERROR("SequenceError => End_Process\n");
  5088. errn = -1;
  5089. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5090. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5091. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5092. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5093. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5094. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5095. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5096. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5097. }
  5098. //Detect for CP State should be 9V (State B)
  5099. #if CP_PROTECTION_MECHANISM == ENABLE
  5100. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5101. {
  5102. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  5103. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  5104. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5105. Update_V2G_Status(Other_Fault);
  5106. errn = -1;
  5107. DEBUG_INFO("Emergency Stop by CP Error (%d)\n", sys->CpState);
  5108. }
  5109. #endif
  5110. //Check for CSU command of "Stop by EVSE"
  5111. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  5112. {
  5113. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  5114. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  5115. errn = -1;
  5116. }
  5117. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5118. {
  5119. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  5120. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  5121. errn = -1;
  5122. }
  5123. //[BODY (2/2)] EVSEProcessing
  5124. if(ShmInternalComm->ChargingPermission == TRUE)
  5125. {
  5126. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished; //0
  5127. DEBUG_INFO("CSU Permission: OK\n");
  5128. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  5129. {
  5130. //Set PWM as 5% (for SLAC first case)
  5131. DEBUG_INFO("Set PWM as 5%%\n");
  5132. SwitchCpStateE(DISABLE);
  5133. OutputCpPwmDuty(5);
  5134. }
  5135. #endif
  5136. }
  5137. //Check for Permission Changing from TRUE to FALSE
  5138. if (ShmInternalComm->ChargingPermission_pre == TRUE &&
  5139. ShmInternalComm->ChargingPermission == FALSE)
  5140. {
  5141. DEBUG_ERROR("Permission OFF\n");
  5142. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.ResponseCode = dinresponseCodeType_FAILED;
  5143. v2gObject.DIN.V2G_Message.Body.ContractAuthenticationRes.EVSEProcessing = dinEVSEProcessingType_Finished;
  5144. errn = -1;
  5145. }
  5146. #if PARAMETER_NORMAL_MODE == ENABLE
  5147. ////////////SHM_Read_din_ContractAuthenticationRes(&v2gObject.DIN, ShmCcsData);
  5148. #endif
  5149. // ============ Encode and Send Response Message ===========
  5150. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  5151. {
  5152. DEBUG_ERROR("Tx encoded msg error\n");
  5153. errn = -1;
  5154. }
  5155. return errn;
  5156. }
  5157. /**
  5158. *
  5159. * @param AcceptFd
  5160. * @return
  5161. */
  5162. int Proc_iso1_AuthorizationRes(int AcceptFd)
  5163. {
  5164. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  5165. //if it is not the same, the packet should be ignored.
  5166. //int i = 0;
  5167. int errn = 0;
  5168. bitstream_t v2g_tx_stream;
  5169. static struct ChargingInfoData *sys;
  5170. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5171. size_t pos = 0;
  5172. v2g_tx_stream.pos = &pos;
  5173. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5174. v2g_tx_stream.data = v2gBuffer.tx;
  5175. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  5176. init_iso1AuthorizationResType(&v2gObject.ISO1.V2G_Message.Body.AuthorizationRes);
  5177. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes_isUsed = 1u;
  5178. //[BODY (1/2)] ResponseCode
  5179. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_OK;
  5180. /*+++ 20200808, vern, EVSEProcessing should be waiting for Customer during authrization +++*/
  5181. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Ongoing_WaitingForCustomerInteraction; //0
  5182. /*--- 20200808, vern, should be waiting for Customer during authrization ---*/
  5183. //[HEADER] Check Req SessionID
  5184. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  5185. {
  5186. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  5187. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  5188. errn = -1;
  5189. }
  5190. //Check for SequenceError
  5191. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5192. {
  5193. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  5194. DEBUG_ERROR("SequenceError => End_Process\n");
  5195. errn = -1;
  5196. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5197. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5198. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5199. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5200. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5201. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5202. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5203. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5204. }
  5205. //Detect for CP State should be 9V (State B)
  5206. #if CP_PROTECTION_MECHANISM == ENABLE
  5207. if ((sys->CpState != 2) && (sys->CpState != 3)) //State B1, B2
  5208. {
  5209. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = iso1responseCodeType_FAILED;
  5210. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  5211. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5212. Update_V2G_Status(Other_Fault);
  5213. errn = -1;
  5214. DEBUG_INFO("Emergency Stop by CP Error (%d)\n", sys->CpState);
  5215. }
  5216. #endif
  5217. //Check for CSU command of "Stop by EVSE"
  5218. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  5219. {
  5220. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  5221. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  5222. errn = -1;
  5223. }
  5224. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5225. {
  5226. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  5227. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  5228. errn = -1;
  5229. }
  5230. //[BODY (2/2)] EVSEProcessing
  5231. //Check for Permission from CSU
  5232. if(ShmInternalComm->ChargingPermission == TRUE)
  5233. {
  5234. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  5235. DEBUG_INFO("CSU Permission: OK\n");
  5236. #if (SLAC_FIRST_RESPONSE_METHOD == SET_5_PWM_ONCE_GET_PERMISSION_IN_AUTHORIZATIONRES)
  5237. {
  5238. //Set PWM as 5% (for SLAC first case)
  5239. DEBUG_INFO("Set PWM as 5%%\n");
  5240. SwitchCpStateE(DISABLE);
  5241. OutputCpPwmDuty(5);
  5242. }
  5243. #endif
  5244. }
  5245. //Check for Permission Changing from TRUE to FALSE
  5246. /*+++ 20200808, vern, sync with Tesla CHAdeMO adaptor, 2 for start communication(not yet auth), 1 for star charging (authorized) ---*/
  5247. if (ShmInternalComm->ChargingPermission_pre >= 1 &&
  5248. ShmInternalComm->ChargingPermission == FALSE)
  5249. {
  5250. DEBUG_ERROR("Permission OFF\n");
  5251. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.ResponseCode = FAILED_ISO15118_2014;
  5252. v2gObject.ISO1.V2G_Message.Body.AuthorizationRes.EVSEProcessing = iso1EVSEProcessingType_Finished;
  5253. errn = -1;
  5254. }
  5255. #if PARAMETER_NORMAL_MODE == ENABLE
  5256. ////////////SHM_Read_iso1_AuthorizationRes(&v2gObject.ISO1, ShmCcsData);
  5257. #endif
  5258. // ============ Encode and Send Response Message ===========
  5259. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  5260. {
  5261. DEBUG_ERROR("Tx encoded msg error\n");
  5262. errn = -1;
  5263. }
  5264. return errn;
  5265. }
  5266. /**
  5267. *
  5268. * @param AcceptFd
  5269. * @return
  5270. */
  5271. int Proc_din_ContractAuthenticationReq(int AcceptFd)
  5272. {
  5273. int errn = 0;
  5274. DEBUG_INFO("Request in.\n");
  5275. //Print the decoded XML Document
  5276. PRINT_XML_DOC_DIN_ContractAuthenticationReq(&v2gObject.DIN);
  5277. //Save into Share Memory
  5278. SHM_Save_din_ContractAuthenticationReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  5279. errn = Proc_din_ContractAuthenticationRes(AcceptFd);
  5280. if (errn == 0)
  5281. {
  5282. //send response successfully.
  5283. DEBUG_INFO("Resonse out.\n");
  5284. }
  5285. else
  5286. {
  5287. DEBUG_ERROR("Proc_din_ContractAuthenticationRes(): %d (DEC)\n", errn);
  5288. }
  5289. return errn;
  5290. }
  5291. /**
  5292. *
  5293. * @param AcceptFd
  5294. * @return
  5295. */
  5296. int Proc_iso1_AuthenticationReq(int AcceptFd)
  5297. {
  5298. int errn = 0;
  5299. DEBUG_INFO("Request in.\n");
  5300. //Print the decoded XML Document
  5301. PRINT_XML_DOC_ISO1_AuthorizationReq(&v2gObject.ISO1);
  5302. //Save into Share Memory
  5303. SHM_Save_iso1_AuthorizationReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  5304. errn = Proc_iso1_AuthorizationRes(AcceptFd);
  5305. if (errn == 0)
  5306. {
  5307. //send response successfully.
  5308. DEBUG_INFO("Response out.\n");
  5309. }
  5310. else
  5311. {
  5312. DEBUG_ERROR("Proc_iso1_AuthorizationRes(): %d (DEC)\n", errn);
  5313. }
  5314. return errn;
  5315. }
  5316. /**
  5317. *
  5318. * @param in
  5319. */
  5320. void SHM_Init_dinDC_EVSEStatusType(struct DC_EVSEStatusType_DIN70121 *in)
  5321. {
  5322. in->EVSEIsolationStatus = dinisolationLevelType_Valid;
  5323. // dinisolationLevelType_Invalid = 0,
  5324. // dinisolationLevelType_Valid = 1, (default)
  5325. // dinisolationLevelType_Warning = 2,
  5326. // dinisolationLevelType_Fault = 3
  5327. in->EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  5328. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  5329. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1, (default)
  5330. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  5331. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  5332. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  5333. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  5334. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  5335. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  5336. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  5337. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  5338. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  5339. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  5340. in->EVSENotification = dinEVSENotificationType_None;
  5341. // dinEVSENotificationType_None = 0, (default)
  5342. // dinEVSENotificationType_StopCharging = 1,
  5343. // dinEVSENotificationType_ReNegotiation = 2
  5344. }
  5345. /**
  5346. *
  5347. * @param obj
  5348. * @param value
  5349. * @param multiplier
  5350. * @param unit
  5351. */
  5352. void SHM_Save_dinPhysicalValueType(struct PhysicalValueType_DIN70121 *obj, short value, int multiplier, unsigned char unit)
  5353. {
  5354. obj->Value = value;
  5355. obj->Multiplier = multiplier;
  5356. obj->Unit =unit;
  5357. }
  5358. /**
  5359. *
  5360. * @param shm_ccs
  5361. */
  5362. void SHM_Init_din_ChargeParameterDiscoveryRes(struct CcsData *shm_ccs)
  5363. {
  5364. struct ChargeParameterDiscoveryResponse_DIN70121 *in;
  5365. in = &shm_ccs->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse;
  5366. //----- [BODY (1/5)] ResponseCode -----
  5367. in->ResponseCode = dinresponseCodeType_OK;
  5368. //----- [BODY (2/5)] EVSEProcessing -----
  5369. in->EVSEProcessing = dinEVSEProcessingType_Ongoing; //0
  5370. // dinEVSEProcessingType_Finished = 0,
  5371. // dinEVSEProcessingType_Ongoing = 1
  5372. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  5373. //be fixed in another function.
  5374. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  5375. //ignore, since DIN doesn't support AC
  5376. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  5377. struct DC_EVSEChargeParameterType_DIN70121 *in_para;
  5378. in_para = &in->DC_EVSEChargeParameter;
  5379. //DC_EVSEStatus
  5380. SHM_Init_dinDC_EVSEStatusType(&in_para->DC_EVSEStatus);
  5381. in_para->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  5382. short value = 0; int multiplier = 0; unsigned char unit = 0;
  5383. //EVSEMaximumCurrentLimit
  5384. value = 600; multiplier = -1; unit = A_DIN70121; //60A
  5385. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumCurrentLimit, value, multiplier, unit);
  5386. //EVSEMaximumPowerLimit
  5387. value = 3000; multiplier = 1; unit = W_DIN70121; //30KW
  5388. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumPowerLimit, value, multiplier, unit);
  5389. //EVSEMaximumVoltageLimit
  5390. value = 7500; multiplier = -1; unit = V_DIN70121; //750V
  5391. SHM_Save_dinPhysicalValueType(&in_para->EVSEMaximumVoltageLimit, value, multiplier, unit);
  5392. //EVSEMinimumVoltageLimit
  5393. value = 1500; multiplier = -1; unit = V_DIN70121; //150V
  5394. SHM_Save_dinPhysicalValueType(&in_para->EVSEMinimumVoltageLimit, value, multiplier, unit);
  5395. //EVSEMinimumCurrentLimit
  5396. value = 10; multiplier = -1; unit = A_DIN70121; //1A
  5397. SHM_Save_dinPhysicalValueType(&in_para->EVSEMinimumCurrentLimit, value, multiplier, unit);
  5398. //EVSECurrentRegulationTolerance
  5399. value = 10; multiplier = -1; unit = A_DIN70121; //1A
  5400. SHM_Save_dinPhysicalValueType(&in_para->EVSECurrentRegulationTolerance, value, multiplier, unit);
  5401. //EVSEPeakCurrentRipple
  5402. value = 2; multiplier = -1; unit = A_DIN70121; //0.2A
  5403. SHM_Save_dinPhysicalValueType(&in_para->EVSEPeakCurrentRipple, value, multiplier, unit);
  5404. //EVSEEnergyToBeDelivered (optional)
  5405. //SHM_Save_dinPhysicalValueType(&out_para->EVSEEnergyToBeDelivered, &in_para->EVSEEnergyToBeDelivered);
  5406. }
  5407. /**
  5408. *
  5409. */
  5410. void Sudo_Parameter_din_ChargeParameterDiscoveryRes()
  5411. {
  5412. init_dinBodyType(&v2gObject.DIN.V2G_Message.Body);
  5413. init_dinChargeParameterDiscoveryResType(&v2gObject.DIN.V2G_Message.Body.ChargeParameterDiscoveryRes);
  5414. v2gObject.DIN.V2G_Message.Body.ChargeParameterDiscoveryRes_isUsed = 1u;
  5415. //----- [BODY (1/5)] ResponseCode -----
  5416. struct dinChargeParameterDiscoveryResType *res;
  5417. res = &v2gObject.DIN.V2G_Message.Body.ChargeParameterDiscoveryRes;
  5418. res->ResponseCode = dinresponseCodeType_OK;
  5419. //----- [BODY (2/5)] EVSEProcessing -----
  5420. res->EVSEProcessing = dinEVSEProcessingType_Finished; //0
  5421. // dinEVSEProcessingType_Finished = 0,
  5422. // dinEVSEProcessingType_Ongoing = 1
  5423. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  5424. res->SAScheduleList_isUsed = 1u;
  5425. struct dinSAScheduleListType *list;
  5426. list = &res->SAScheduleList;
  5427. //
  5428. list->SAScheduleTuple.arrayLen = 1;
  5429. list->SAScheduleTuple.array[0].SAScheduleTupleID = 0;
  5430. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleID = 0;
  5431. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.arrayLen = 1;
  5432. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval_isUsed = 1u;
  5433. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.start = 0;
  5434. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration = xxx
  5435. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration_isUsed = 1u;
  5436. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval_isUsed = 0u;
  5437. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval.xxx
  5438. //list->SAScheduleTuple.array[0].SalesTariff.xxx
  5439. //list->SAScheduleTuple.array[0].SalesTariff_isUsed = 0u;
  5440. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  5441. //ignore, since DIN doesn't support AC
  5442. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  5443. res->DC_EVSEChargeParameter_isUsed = 1u;
  5444. struct dinDC_EVSEChargeParameterType *para;
  5445. para = &res->DC_EVSEChargeParameter;
  5446. //DC_EVSEStatus
  5447. para->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  5448. para->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  5449. // dinisolationLevelType_Invalid = 0,
  5450. // dinisolationLevelType_Valid = 1,
  5451. // dinisolationLevelType_Warning = 2,
  5452. // dinisolationLevelType_Fault = 3
  5453. para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  5454. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  5455. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  5456. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  5457. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  5458. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  5459. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  5460. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  5461. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  5462. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  5463. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  5464. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  5465. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  5466. para->DC_EVSEStatus.NotificationMaxDelay = 0u;
  5467. para->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  5468. // dinEVSENotificationType_None = 0,
  5469. // dinEVSENotificationType_StopCharging = 1,
  5470. // dinEVSENotificationType_ReNegotiation = 2
  5471. //EVSEMaximumCurrentLimit
  5472. para->EVSEMaximumCurrentLimit.Value = 2400;
  5473. para->EVSEMaximumCurrentLimit.Multiplier = -1;
  5474. para->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  5475. para->EVSEMaximumCurrentLimit.Unit = dinunitSymbolType_A;
  5476. // dinunitSymbolType_h = 0,
  5477. // dinunitSymbolType_m = 1,
  5478. // dinunitSymbolType_s = 2,
  5479. // dinunitSymbolType_A = 3,
  5480. // dinunitSymbolType_Ah = 4,
  5481. // dinunitSymbolType_V = 5,
  5482. // dinunitSymbolType_VA = 6,
  5483. // dinunitSymbolType_W = 7,
  5484. // dinunitSymbolType_W_s = 8,
  5485. // dinunitSymbolType_Wh = 9
  5486. //EVSEMaximumPowerLimit
  5487. para->EVSEMaximumPowerLimit.Value = 6000;
  5488. para->EVSEMaximumPowerLimit.Multiplier = 1;
  5489. para->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  5490. para->EVSEMaximumPowerLimit.Unit = dinunitSymbolType_W;
  5491. //EVSEMaximumVoltageLimit
  5492. para->EVSEMaximumVoltageLimit.Value = 7500;
  5493. para->EVSEMaximumVoltageLimit.Multiplier = -1;
  5494. para->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  5495. para->EVSEMaximumVoltageLimit.Unit = dinunitSymbolType_V;
  5496. //EVSEMinimumVoltageLimit
  5497. para->EVSEMinimumVoltageLimit.Value = 1500;
  5498. para->EVSEMinimumVoltageLimit.Multiplier = -1;
  5499. para->EVSEMinimumVoltageLimit.Unit_isUsed = 1u;
  5500. para->EVSEMinimumVoltageLimit.Unit = dinunitSymbolType_V;
  5501. //EVSEMinimumCurrentLimit
  5502. para->EVSEMinimumCurrentLimit.Value = 20;
  5503. para->EVSEMinimumCurrentLimit.Multiplier = -1;
  5504. para->EVSEMinimumCurrentLimit.Unit_isUsed = 1u;
  5505. para->EVSEMinimumCurrentLimit.Unit = dinunitSymbolType_A;
  5506. //EVSECurrentRegulationTolerance_isUsed
  5507. para->EVSECurrentRegulationTolerance_isUsed = 1u;
  5508. //EVSECurrentRegulationTolerance
  5509. para->EVSECurrentRegulationTolerance.Value = 10;
  5510. para->EVSECurrentRegulationTolerance.Multiplier = -1;
  5511. para->EVSECurrentRegulationTolerance.Unit_isUsed = 1u;
  5512. para->EVSECurrentRegulationTolerance.Unit = dinunitSymbolType_A;
  5513. //EVSEEnergyToBeDelivered_isUsed
  5514. para->EVSEEnergyToBeDelivered_isUsed = 1u;
  5515. //EVSEPeakCurrentRipple
  5516. para->EVSEPeakCurrentRipple.Value = 2;
  5517. para->EVSEPeakCurrentRipple.Multiplier = -1;
  5518. para->EVSEPeakCurrentRipple.Unit_isUsed = 1u;
  5519. para->EVSEPeakCurrentRipple.Unit = dinunitSymbolType_A;
  5520. //EVSEEnergyToBeDelivered (optional)
  5521. /*
  5522. para->EVSEEnergyToBeDelivered.Value = 360;
  5523. para->EVSEEnergyToBeDelivered.Multiplier = 3;
  5524. para->EVSEEnergyToBeDelivered.Unit_isUsed = 1u;
  5525. para->EVSEEnergyToBeDelivered.Unit = dinunitSymbolType_Wh;
  5526. */
  5527. }
  5528. /**
  5529. *
  5530. */
  5531. void Sudo_Parameter_iso1_ChargeParameterDiscoveryRes()
  5532. {
  5533. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  5534. init_iso1ChargeParameterDiscoveryResType(&v2gObject.ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes);
  5535. v2gObject.ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes_isUsed = 1u;
  5536. //----- [BODY (1/5)] ResponseCode -----
  5537. struct iso1ChargeParameterDiscoveryResType *res;
  5538. res = &v2gObject.ISO1.V2G_Message.Body.ChargeParameterDiscoveryRes;
  5539. res->ResponseCode = iso1responseCodeType_OK;
  5540. //----- [BODY (2/5)] EVSEProcessing -----
  5541. res->EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  5542. // iso1EVSEProcessingType_Finished = 0,
  5543. // iso1EVSEProcessingType_Ongoing = 1
  5544. //----- [BODY (3/5)] SAScheduleList of SASchedules -----
  5545. res->SAScheduleList_isUsed = 1u;
  5546. struct iso1SAScheduleListType *list;
  5547. list = &res->SAScheduleList;
  5548. //
  5549. list->SAScheduleTuple.arrayLen = 1;
  5550. list->SAScheduleTuple.array[0].SAScheduleTupleID = 0;
  5551. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.arrayLen = 1;
  5552. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval_isUsed = 1u;
  5553. list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.start = 0;
  5554. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration = xxx
  5555. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].RelativeTimeInterval.duration_isUsed = 1u;
  5556. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval_isUsed = 0u;
  5557. //list->SAScheduleTuple.array[0].PMaxSchedule.PMaxScheduleEntry.array[0].TimeInterval.xxx
  5558. //list->SAScheduleTuple.array[0].SalesTariff.xxx
  5559. //list->SAScheduleTuple.array[0].SalesTariff_isUsed = 0u;
  5560. //----- [BODY (4/5)] AC_EVSEChargeParameter of EVSEChargeParameter -----
  5561. //ignore, since our ISO1 doesn't support AC, yet
  5562. //----- [BODY (5/5)] DC_EVSEChargeParameter of EVSEChargeParameter -----
  5563. res->DC_EVSEChargeParameter_isUsed = 1u;
  5564. struct iso1DC_EVSEChargeParameterType *para;
  5565. para = &res->DC_EVSEChargeParameter;
  5566. //DC_EVSEStatus
  5567. para->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  5568. para->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  5569. // iso1isolationLevelType_Invalid = 0,
  5570. // iso1isolationLevelType_Valid = 1,
  5571. // iso1isolationLevelType_Warning = 2,
  5572. // iso1isolationLevelType_Fault = 3
  5573. para->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  5574. // iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  5575. // iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  5576. // iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  5577. // iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  5578. // iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  5579. // iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  5580. // iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  5581. // iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  5582. // iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  5583. // iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  5584. // iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  5585. // iso1DC_EVSEStatusCodeType_Reserved_C = 11
  5586. para->DC_EVSEStatus.NotificationMaxDelay = 0u;
  5587. para->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  5588. // iso1EVSENotificationType_None = 0,
  5589. // iso1EVSENotificationType_StopCharging = 1,
  5590. // iso1EVSENotificationType_ReNegotiation = 2
  5591. //EVSEMaximumCurrentLimit
  5592. para->EVSEMaximumCurrentLimit.Value = 2400;
  5593. para->EVSEMaximumCurrentLimit.Multiplier = -1;
  5594. //para->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  5595. para->EVSEMaximumCurrentLimit.Unit = iso1unitSymbolType_A;
  5596. //iso1unitSymbolType_h = 0,
  5597. //iso1unitSymbolType_m = 1,
  5598. //iso1unitSymbolType_s = 2,
  5599. //iso1unitSymbolType_A = 3,
  5600. //iso1unitSymbolType_V = 4,
  5601. //iso1unitSymbolType_W = 5,
  5602. //iso1unitSymbolType_Wh = 6
  5603. //EVSEMaximumPowerLimit
  5604. para->EVSEMaximumPowerLimit.Value = 6000;
  5605. para->EVSEMaximumPowerLimit.Multiplier = 1;
  5606. //para->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  5607. para->EVSEMaximumPowerLimit.Unit = iso1unitSymbolType_W;
  5608. //EVSEMaximumVoltageLimit
  5609. para->EVSEMaximumVoltageLimit.Value = 7500;
  5610. para->EVSEMaximumVoltageLimit.Multiplier = -1;
  5611. //para->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  5612. para->EVSEMaximumVoltageLimit.Unit = iso1unitSymbolType_V;
  5613. //EVSEMinimumVoltageLimit
  5614. para->EVSEMinimumVoltageLimit.Value = 1500;
  5615. para->EVSEMinimumVoltageLimit.Multiplier = -1;
  5616. //para->EVSEMinimumVoltageLimit.Unit_isUsed = 1u;
  5617. para->EVSEMinimumVoltageLimit.Unit = iso1unitSymbolType_V;
  5618. //EVSEMinimumCurrentLimit
  5619. para->EVSEMinimumCurrentLimit.Value = 20;
  5620. para->EVSEMinimumCurrentLimit.Multiplier = -1;
  5621. //para->EVSEMinimumCurrentLimit.Unit_isUsed = 1u;
  5622. para->EVSEMinimumCurrentLimit.Unit = iso1unitSymbolType_A;
  5623. //EVSECurrentRegulationTolerance_isUsed
  5624. para->EVSECurrentRegulationTolerance_isUsed = 1u;
  5625. //EVSECurrentRegulationTolerance
  5626. para->EVSECurrentRegulationTolerance.Value = 10;
  5627. para->EVSECurrentRegulationTolerance.Multiplier = -1;
  5628. //para->EVSECurrentRegulationTolerance.Unit_isUsed = 1u;
  5629. para->EVSECurrentRegulationTolerance.Unit = iso1unitSymbolType_A;
  5630. //EVSEEnergyToBeDelivered_isUsed
  5631. para->EVSEEnergyToBeDelivered_isUsed = 1u;
  5632. //EVSEPeakCurrentRipple
  5633. para->EVSEPeakCurrentRipple.Value = 2;
  5634. para->EVSEPeakCurrentRipple.Multiplier = -1;
  5635. //para->EVSEPeakCurrentRipple.Unit_isUsed = 1u;
  5636. para->EVSEPeakCurrentRipple.Unit = iso1unitSymbolType_A;
  5637. //EVSEEnergyToBeDelivered (optional)
  5638. /*
  5639. para->EVSEEnergyToBeDelivered.Value = 360;
  5640. para->EVSEEnergyToBeDelivered.Multiplier = 3;
  5641. para->EVSEEnergyToBeDelivered.Unit_isUsed = 1u;
  5642. para->EVSEEnergyToBeDelivered.Unit = iso1unitSymbolType_Wh;
  5643. */
  5644. }
  5645. /**
  5646. *
  5647. * @param AcceptFd
  5648. * @return
  5649. */
  5650. int Proc_din_ChargeParameterDiscoveryRes(int AcceptFd)
  5651. {
  5652. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  5653. //if it is not the same, the packet should be ignored.
  5654. //STEP 1: ============ Initialize ============
  5655. int errn = 0;
  5656. bitstream_t v2g_tx_stream;
  5657. struct ChargeParameterDiscoveryResponse_DIN70121 *res;
  5658. struct ChargeParameterDiscoveryRequest_DIN70121 *req;
  5659. struct DC_EVSEChargeParameterType_DIN70121 *dc_para;
  5660. struct ChargingInfoData *sys;
  5661. size_t pos = 0;
  5662. v2g_tx_stream.pos = &pos;
  5663. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5664. v2g_tx_stream.data = v2gBuffer.tx;
  5665. res = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse;
  5666. req = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest;
  5667. dc_para = &ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryResponse.DC_EVSEChargeParameter;
  5668. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5669. res->ResponseCode = OK_DIN70121;
  5670. res->EVSEProcessing = Ongoing_DIN70121;
  5671. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready; //1
  5672. //[HEADER] Check Req SessionID
  5673. if (Check_din_V2G_Rx_MSG_SessionID(&v2gObject.DIN) < 0)
  5674. {
  5675. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  5676. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  5677. errn = -1;
  5678. }
  5679. //Check for SequenceError
  5680. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5681. {
  5682. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  5683. DEBUG_ERROR("SequenceError => End_Process\n");
  5684. errn = -1;
  5685. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5686. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5687. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5688. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5689. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5690. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5691. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5692. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5693. }
  5694. //[TC_SECC_VTB_ChargeParameterDiscovery_005]
  5695. if (sys->EvBatteryMaxCurrent < 0)
  5696. {
  5697. DEBUG_ERROR("EvBatteryMaxCurrent is negative(%.02f) => End_Process\n", sys->EvBatteryMaxCurrent);
  5698. res->ResponseCode = dinresponseCodeType_FAILED_WrongChargeParameter; //16
  5699. res->EVSEProcessing = Finished_DIN70121;
  5700. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  5701. errn = -1;
  5702. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5703. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  5704. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5705. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5706. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5707. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5708. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  5709. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  5710. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5711. }
  5712. //STEP 2: ============ Modify Parameters of ShmCcsData ============
  5713. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_DIN70121);
  5714. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_DIN70121);
  5715. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_DIN70121);
  5716. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMinimumVoltageLimit, 1500, V_DIN70121); //150V
  5717. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMinimumCurrentLimit, 10, A_DIN70121); //1A
  5718. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSECurrentRegulationTolerance, 10, A_DIN70121); //1A
  5719. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEPeakCurrentRipple, 2, A_DIN70121); //0.2A
  5720. //SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEEnergyToBeDelivered, 0, WH_DIN70121); //optional
  5721. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  5722. if (sys->EvBatteryMaxVoltage <= 500)
  5723. {
  5724. SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumVoltageLimit, (int) (500 * 10), V_DIN70121); //500V
  5725. }
  5726. #endif
  5727. //for test with Tesla Model 3, 10A
  5728. //SAVE_PhysicalValueType_DIN70121(&dc_para->EVSEMaximumCurrentLimit, (int) (10 * 10), A_DIN70121); //10A, for testing with Tesla Model 3
  5729. //Check for EnergyTransferMode [TC_SECC_VTB_ChargeParameterDiscovery_004]
  5730. if (req->EVRequestedEnergyTransferType != DC_extended)
  5731. {
  5732. DEBUG_ERROR("Wrong EVRequestedEnergyTransferType(%d,%d)\n", req->EVRequestedEnergyTransferType, dinEVSESupportedEnergyTransferType_DC_extended);
  5733. res->ResponseCode = FAILED_WrongEnergyTransferType_DIN70121;
  5734. res->EVSEProcessing = Finished_DIN70121;
  5735. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  5736. errn = -1;
  5737. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5738. Update_V2G_Status(Other_Fault);
  5739. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5740. //CCS_SECC_ResponseCode_FAILED_WrongEnergyTransferMode (023774)
  5741. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5742. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5743. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5744. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5745. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  5746. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  5747. }
  5748. //Check for CSU command of "Stop by EVSE"
  5749. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  5750. {
  5751. DEBUG_INFO("EVSE_Shutdown\n");
  5752. res->ResponseCode = FAILED_DIN70121;
  5753. res->EVSEProcessing = Finished_DIN70121;
  5754. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  5755. errn = -1;
  5756. }
  5757. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5758. {
  5759. DEBUG_INFO("EVSE_EmergencyShutdown\n");
  5760. res->ResponseCode = FAILED_DIN70121;
  5761. res->EVSEProcessing = Finished_DIN70121;
  5762. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  5763. errn = -1;
  5764. }
  5765. //Detect for CP State should be 9V (State B)
  5766. #if CP_PROTECTION_MECHANISM == ENABLE
  5767. if (sys->CpState != 3) //State B1, B2
  5768. {
  5769. res->ResponseCode = FAILED_DIN70121;
  5770. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  5771. res->EVSEProcessing = Finished_DIN70121;
  5772. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5773. Update_V2G_Status(Other_Fault);
  5774. errn = -1;
  5775. DEBUG_INFO("Emergency Stop by CP Error (%d, %.02f V)\n", sys->CpState, sys->CpVoltage);
  5776. }
  5777. #endif
  5778. //Check for Permission from CSU
  5779. if (ShmInternalComm->ChargingPermission == TRUE)
  5780. {
  5781. res->EVSEProcessing = Finished_DIN70121;
  5782. }
  5783. //Check for Permission Off
  5784. if (ShmInternalComm->ChargingPermission == FALSE)
  5785. {
  5786. DEBUG_ERROR("Permission OFF\n");
  5787. res->ResponseCode = FAILED_DIN70121;
  5788. res->EVSEProcessing = Finished_DIN70121;
  5789. dc_para->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  5790. errn = -1;
  5791. }
  5792. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  5793. #if PARAMETER_NORMAL_MODE == ENABLE
  5794. SHM_Read_din_ChargeParameterDiscoveryRes(&v2gObject.DIN, ShmCcsData);
  5795. #else
  5796. Sudo_Parameter_din_ChargeParameterDiscoveryRes();
  5797. #endif
  5798. //STEP 4: ============ Encode and Send Response Message ===========
  5799. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  5800. {
  5801. DEBUG_ERROR("Tx encoded msg error\n");
  5802. errn = -1;
  5803. }
  5804. //STPE 5: ============ Update Flags ============
  5805. return errn;
  5806. }
  5807. /**
  5808. *
  5809. * @param AcceptFd
  5810. * @return
  5811. */
  5812. int Proc_iso1_ChargeParameterDiscoveryRes(int AcceptFd)
  5813. {
  5814. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  5815. //if it is not the same, the packet should be ignored.
  5816. //STEP 1: ============ Initialize ============
  5817. int errn = 0;
  5818. bitstream_t v2g_tx_stream;
  5819. struct ChargeParameterDiscoveryResponse_ISO15118_2014 *res;
  5820. struct ChargeParameterDiscoveryRequest_ISO15118_2014 *req;
  5821. struct DC_EVSEChargeParameterType_ISO15118_2014 *dc_para;
  5822. struct AC_EVSEChargeParameterType_ISO15118_2014 *ac_para;
  5823. struct ChargingInfoData *sys;
  5824. size_t pos = 0;
  5825. v2g_tx_stream.pos = &pos;
  5826. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  5827. v2g_tx_stream.data = v2gBuffer.tx;
  5828. res = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse;
  5829. req = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest;
  5830. dc_para = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse.DC_EVSEChargeParameter;
  5831. ac_para = &ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryResponse.AC_EVSEChargeParameter;
  5832. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  5833. res->ResponseCode = OK_ISO15118_2014;
  5834. /*+++ 20200808, vern, EVSEProcessing should be on-going during ChargeParameterDiscovery +++*/
  5835. res->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  5836. /*--- 20200808, vern, EVSEProcessing should be on-going during ChargeParameterDiscovery ---*/
  5837. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready; //1
  5838. ac_para->AC_EVSEStatus.RCD = ShmInternalComm->AC_RcdStatus; //0:no error, 1:error
  5839. ac_para->AC_EVSEStatus.NotificationMaxDelay = 10; //unit: 1s
  5840. ac_para->AC_EVSEStatus.EVSENotification = ShmInternalComm->AC_EVSENotification; //0:none, 1:StopCharging, 2:RenNgotiation
  5841. //[HEADER] Check Req SessionID
  5842. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  5843. {
  5844. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  5845. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  5846. errn = -1;
  5847. }
  5848. //Check for SequenceError
  5849. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  5850. {
  5851. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  5852. DEBUG_ERROR("SequenceError => End_Process\n");
  5853. errn = -1;
  5854. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5855. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  5856. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5857. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5858. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5859. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5860. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  5861. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  5862. }
  5863. //[TC_SECC_VTB_ChargeParameterDiscovery_005]
  5864. /*+++ 20200808, vern, should check the voltage and current to see if the range of battery parameter is accepted by charger ---*/
  5865. if((ShmCcsData->EnergyTransferMode == MODE_DC_EXTENDED) && ((sys->EvBatteryMaxCurrent < 0) || (sys->EvBatteryMaxVoltage<150)))
  5866. {
  5867. DEBUG_ERROR("EvBatteryMaxCurrent is negative(%.02f) or EvBatteryMaxVoltage is under 150(%.02f)=> End_Process\n", sys->EvBatteryMaxCurrent, sys->EvBatteryMaxVoltage);
  5868. res->ResponseCode = iso1responseCodeType_FAILED_WrongChargeParameter; //16
  5869. res->EVSEProcessing = Finished_ISO15118_2014;
  5870. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  5871. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  5872. errn = -1;
  5873. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5874. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  5875. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5876. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5877. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5878. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5879. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  5880. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  5881. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5882. }
  5883. //STEP 2: ============ Modify Parameters of ShmCcsData ============
  5884. if(ShmCcsData->EnergyTransferMode == MODE_DC_EXTENDED)
  5885. {
  5886. //DC
  5887. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_ISO15118_2014);
  5888. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  5889. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_ISO15118_2014);
  5890. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMinimumVoltageLimit, 1500, V_ISO15118_2014); //150V
  5891. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMinimumCurrentLimit, 10, A_ISO15118_2014); //1A
  5892. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSECurrentRegulationTolerance, 10, A_ISO15118_2014); //1A
  5893. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEPeakCurrentRipple, 2, A_ISO15118_2014); //0.2A
  5894. //SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEEnergyToBeDelivered, 0, Wh_ISO15118_2014); //optional
  5895. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  5896. if (sys->EvBatteryMaxVoltage <= 500)
  5897. {
  5898. SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumVoltageLimit, (int) (500 * 10), V_ISO15118_2014); //500V
  5899. }
  5900. #endif
  5901. //for test with Tesla Model 3, 10A
  5902. //SAVE_PhysicalValueType_ISO15118_2014(&dc_para->EVSEMaximumCurrentLimit, (int) (10 * 10), A_ISO15118_2014); //10A, for testing with Tesla Model 3
  5903. }
  5904. else
  5905. {
  5906. //AC
  5907. SAVE_PhysicalValueType_ISO15118_2014(&ac_para->EVSENominalVoltage, (int) (220 * 10), V_ISO15118_2014);
  5908. SAVE_PhysicalValueType_ISO15118_2014(&ac_para->EVSEMaxCurrent, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  5909. }
  5910. //Check for EnergyTransferMode
  5911. if(req->RequestedEnergyTransferMode != ShmCcsData->EnergyTransferMode) //[CAUTION] Their table should be kept as the same.
  5912. {
  5913. DEBUG_ERROR("Unmatched RequestedEnergyTransferMode(%d,%d)\n", req->RequestedEnergyTransferMode, ShmCcsData->EnergyTransferMode);
  5914. res->ResponseCode = FAILED_WrongEnergyTransferMode_ISO15118_2014;
  5915. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  5916. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  5917. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  5918. errn = -1;
  5919. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5920. Update_V2G_Status(Other_Fault);
  5921. //Update_ShmStatusCode(); //[To-Do] to be implemented
  5922. //CCS_SECC_ResponseCode_FAILED_WrongEnergyTransferMode (023774)
  5923. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  5924. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  5925. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  5926. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  5927. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  5928. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  5929. }
  5930. //Check for CSU command of "Stop by EVSE"
  5931. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  5932. {
  5933. DEBUG_INFO("EVSE_Shutdown\n");
  5934. res->ResponseCode = FAILED_ISO15118_2014;
  5935. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  5936. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  5937. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  5938. errn = -1;
  5939. }
  5940. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  5941. {
  5942. DEBUG_INFO("EVSE_EmergencyShutdown\n");
  5943. res->ResponseCode = FAILED_ISO15118_2014;
  5944. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  5945. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  5946. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  5947. errn = -1;
  5948. }
  5949. //Detect for CP State should be 9V (State B)
  5950. #if CP_PROTECTION_MECHANISM == ENABLE
  5951. if ((sys->CpState != CCS_CP_STATE_B1) && (sys->CpState != CCS_CP_STATE_B2) && (sys->CpState != CCS_CP_STATE_C)) //State B1, B2
  5952. {
  5953. res->ResponseCode = FAILED_ISO15118_2014;
  5954. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  5955. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  5956. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  5957. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  5958. Update_V2G_Status(Other_Fault);
  5959. errn = -1;
  5960. DEBUG_INFO("Emergency Stop by CP Error (%d, %.02f V)\n", sys->CpState, sys->CpVoltage);
  5961. }
  5962. #endif
  5963. //Check for Permission from CSU
  5964. if(ShmInternalComm->ChargingPermission == TRUE)
  5965. {
  5966. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  5967. }
  5968. //Check for Permission Off
  5969. if (ShmInternalComm->ChargingPermission == FALSE)
  5970. {
  5971. DEBUG_ERROR("Permission OFF\n");
  5972. res->ResponseCode = FAILED_ISO15118_2014;
  5973. res->EVSEProcessing = iso1EVSEProcessingType_Finished;
  5974. dc_para->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  5975. ac_para->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  5976. errn = -1;
  5977. }
  5978. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  5979. #if PARAMETER_NORMAL_MODE == ENABLE
  5980. SHM_Read_iso1_ChargeParameterDiscoveryRes(&v2gObject.ISO1, ShmCcsData);
  5981. #else
  5982. Sudo_Parameter_iso1_ChargeParameterDiscoveryRes();
  5983. #endif
  5984. //STEP 4: ============ Encode and Send Response Message ===========
  5985. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  5986. {
  5987. DEBUG_ERROR("Tx encoded msg error\n");
  5988. errn = -1;
  5989. }
  5990. //STPE 5: ============ Update Flags ============
  5991. return errn;
  5992. }
  5993. /**
  5994. *
  5995. * @param AcceptFd
  5996. * @return
  5997. */
  5998. int Proc_din_ChargeParameterDiscoveryReq(int AcceptFd)
  5999. {
  6000. int errn = 0;
  6001. DEBUG_INFO("Request in.\n");
  6002. //ftime(&timerStart.SeqStart);
  6003. //Print the decoded XML Document
  6004. PRINT_XML_DOC_DIN_ChargeParameterDiscoveryReq(&v2gObject.DIN);
  6005. //Save into Share Memory
  6006. SHM_Save_din_ChargeParameterDiscoveryReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  6007. //Check for EV Error Code
  6008. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.DC_EVStatus.EVErrorCode);
  6009. errn = Proc_din_ChargeParameterDiscoveryRes(AcceptFd);
  6010. //ftime(&timerStart.SeqEnd);
  6011. //DEBUG_PRINTF_EVCOMM_DETAIL("\ndelta = %.02lf ms\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd));
  6012. if ( errn == 0)
  6013. {
  6014. //send response successfully.
  6015. DEBUG_INFO("Response out.\n");
  6016. }
  6017. else
  6018. {
  6019. DEBUG_ERROR("Proc_din_ChargeParameterDiscoveryRes() fail: %d (DEC)\n",errn);
  6020. }
  6021. return errn;
  6022. }
  6023. /**
  6024. *
  6025. * @param AcceptFd
  6026. * @return
  6027. */
  6028. int Proc_iso1_ChargeParameterDiscoveryReq(int AcceptFd)
  6029. {
  6030. int errn = 0;
  6031. DEBUG_INFO("Request in.\n");
  6032. //ftime(&timerStart.SeqStart);
  6033. //Print the decoded XML Document
  6034. PRINT_XML_DOC_ISO1_ChargeParameterDiscoveryReq(&v2gObject.ISO1);
  6035. //Save into Share Memory
  6036. SHM_Save_iso1_ChargeParameterDiscoveryReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  6037. //Check for EV Error Code
  6038. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.DC_EVStatus.EVErrorCode);
  6039. errn = Proc_iso1_ChargeParameterDiscoveryRes(AcceptFd);
  6040. //ftime(&timerStart.SeqEnd);
  6041. //DEBUG_PRINTF_EVCOMM_DETAIL("\ndelta = %.02lf ms\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd));
  6042. if (errn == 0)
  6043. {
  6044. //send response successfully.
  6045. DEBUG_INFO("Response out.\n");
  6046. }
  6047. else
  6048. {
  6049. DEBUG_ERROR("Proc_iso1_ChargeParameterDiscoveryRes() fail: %d (DEC)\n", errn);
  6050. }
  6051. return errn;
  6052. }
  6053. /**
  6054. *
  6055. * @param shm_ccs
  6056. */
  6057. void SHM_Init_din_CableCheckRes(struct CcsData *shm_ccs)
  6058. {
  6059. struct CableCheckResponse_DIN70121 *in;
  6060. in = &shm_ccs->V2GMessage_DIN70121.CableCheckResponse;
  6061. //----- [BODY (1/3)] ResponseCode -----
  6062. in->ResponseCode = dinresponseCodeType_OK;
  6063. //----- [BODY (2/3)] EVSEProcessing -----
  6064. //in->EVSEProcessing = dinEVSEProcessingType_Finished; //for test
  6065. in->EVSEProcessing = dinEVSEProcessingType_Ongoing; //default
  6066. // dinEVSEProcessingType_Finished = 0,
  6067. // dinEVSEProcessingType_Ongoing = 1
  6068. //----- [BODY (3/3)] DC_EVSEStatus -----
  6069. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  6070. in->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0 (default)
  6071. // dinisolationLevelType_Invalid = 0,
  6072. // dinisolationLevelType_Valid = 1, (default)
  6073. // dinisolationLevelType_Warning = 2,
  6074. // dinisolationLevelType_Fault = 3
  6075. }
  6076. /**
  6077. *
  6078. */
  6079. void Sudo_Parameter_din_CableCheckRes()
  6080. {
  6081. init_dinBodyType(&v2gObject.DIN.V2G_Message.Body);
  6082. init_dinCableCheckResType(&v2gObject.DIN.V2G_Message.Body.CableCheckRes);
  6083. v2gObject.DIN.V2G_Message.Body.CableCheckRes_isUsed = 1u;
  6084. //----- [BODY (1/3)] ResponseCode -----
  6085. struct dinCableCheckResType *res;
  6086. res = &v2gObject.DIN.V2G_Message.Body.CableCheckRes;
  6087. res->ResponseCode = dinresponseCodeType_OK;
  6088. //----- [BODY (2/3)] EVSEProcessing -----
  6089. res->EVSEProcessing = dinEVSEProcessingType_Finished; //0
  6090. // dinEVSEProcessingType_Finished = 0,
  6091. // dinEVSEProcessingType_Ongoing = 1
  6092. //----- [BODY (3/3)] DC_EVSEStatus -----
  6093. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  6094. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  6095. // dinisolationLevelType_Invalid = 0,
  6096. // dinisolationLevelType_Valid = 1,
  6097. // dinisolationLevelType_Warning = 2,
  6098. // dinisolationLevelType_Fault = 3
  6099. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  6100. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6101. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  6102. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6103. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6104. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6105. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6106. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6107. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  6108. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  6109. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  6110. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  6111. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  6112. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  6113. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  6114. // dinEVSENotificationType_None = 0,
  6115. // dinEVSENotificationType_StopCharging = 1,
  6116. // dinEVSENotificationType_ReNegotiation = 2
  6117. }
  6118. /**
  6119. *
  6120. */
  6121. void Sudo_Parameter_iso1_CableCheckRes()
  6122. {
  6123. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  6124. init_iso1CableCheckResType(&v2gObject.ISO1.V2G_Message.Body.CableCheckRes);
  6125. v2gObject.ISO1.V2G_Message.Body.CableCheckRes_isUsed = 1u;
  6126. //----- [BODY (1/3)] ResponseCode -----
  6127. struct iso1CableCheckResType *res;
  6128. res = &v2gObject.ISO1.V2G_Message.Body.CableCheckRes;
  6129. res->ResponseCode = iso1responseCodeType_OK;
  6130. //----- [BODY (2/3)] EVSEProcessing -----
  6131. res->EVSEProcessing = iso1EVSEProcessingType_Finished; //0
  6132. //iso1EVSEProcessingType_Finished = 0,
  6133. //iso1EVSEProcessingType_Ongoing = 1,
  6134. //iso1EVSEProcessingType_Ongoing_WaitingForCustomerInteraction = 2
  6135. //----- [BODY (3/3)] DC_EVSEStatus -----
  6136. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  6137. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  6138. //iso1isolationLevelType_Invalid = 0,
  6139. //iso1isolationLevelType_Valid = 1,
  6140. //iso1isolationLevelType_Warning = 2,
  6141. //iso1isolationLevelType_Fault = 3,
  6142. //iso1isolationLevelType_No_IMD = 4
  6143. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  6144. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6145. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  6146. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6147. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6148. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6149. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6150. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6151. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  6152. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  6153. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  6154. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  6155. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  6156. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  6157. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  6158. //iso1EVSENotificationType_None = 0,
  6159. //iso1EVSENotificationType_StopCharging = 1,
  6160. //iso1EVSENotificationType_ReNegotiation = 2
  6161. }
  6162. /**
  6163. *
  6164. * @param AcceptFd
  6165. * @return
  6166. */
  6167. int Proc_din_CableCheckRes(int AcceptFd)
  6168. {
  6169. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  6170. //if it is not the same, the packet should be ignored.
  6171. //STEP 1: ============ Initialize ============
  6172. //int i = 0;
  6173. int errn = 0;
  6174. bitstream_t v2g_tx_stream;
  6175. static struct CableCheckResponse_DIN70121 *cab;
  6176. static struct ChargingInfoData *sys;
  6177. size_t pos = 0;
  6178. v2g_tx_stream.pos = &pos;
  6179. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6180. v2g_tx_stream.data = v2gBuffer.tx;
  6181. cab = &ShmCcsData->V2GMessage_DIN70121.CableCheckResponse;
  6182. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6183. cab->ResponseCode = OK_DIN70121;
  6184. cab->cnt++;
  6185. DEBUG_PRINTF_EVCOMM_DETAIL("CP_State = %d (%.02f V), V_now = %.02f, Isolation Status = %d, EVSEProcessing = %d\n",
  6186. sys->CpState,
  6187. sys->CpVoltage,
  6188. sys->PresentChargingVoltage,
  6189. sys->IsolationStatus,
  6190. cab->EVSEProcessing);
  6191. //[HEADER] Check Req SessionID
  6192. if (Check_din_V2G_Rx_MSG_SessionID(&v2gObject.DIN) < 0)
  6193. {
  6194. cab->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  6195. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  6196. errn = -1;
  6197. }
  6198. //Check for SequenceError
  6199. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6200. {
  6201. cab->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  6202. DEBUG_ERROR("SequenceError => End_Process\n");
  6203. errn = -1;
  6204. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6205. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6206. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6207. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6208. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6209. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6210. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6211. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6212. }
  6213. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  6214. if (sys->IsolationStatus == GFD_Invalid) //0: on going
  6215. {
  6216. //For PSU
  6217. sys->EvBatterytargetVoltage = CABLECHECK_TARGET_VOLTAGE; //500V //[To-Do] this should be modified to auto decision.
  6218. sys->EvBatterytargetCurrent = CABLECHECK_TARGET_CURRENT; //2A(default), unit: 1A
  6219. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  6220. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive; //4
  6221. cab->EVSEProcessing = dinEVSEProcessingType_Ongoing;
  6222. }
  6223. else if ((sys->IsolationStatus == GFD_Valid) || (sys->IsolationStatus == GFD_Warning)) //1: valid, 2: warning
  6224. {
  6225. //For PSU
  6226. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  6227. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  6228. if (sys->PresentChargingVoltage < 60) // < 60V
  6229. {
  6230. DEBUG_INFO("[V2G][CableCheck]Pass (V_now = %.02f, Isolated = %d)\n", sys->PresentChargingVoltage, sys->IsolationStatus);
  6231. //IsolationStatus
  6232. if (sys->IsolationStatus == GFD_Valid)
  6233. {
  6234. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  6235. }
  6236. else if (sys->IsolationStatus == GFD_Warning)
  6237. {
  6238. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning;
  6239. }
  6240. else
  6241. {
  6242. DEBUG_WARN("unexpected IsolationStatus(%d)\n", sys->IsolationStatus);
  6243. }
  6244. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  6245. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  6246. }
  6247. else
  6248. {
  6249. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  6250. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  6251. cab->EVSEProcessing = dinEVSEProcessingType_Ongoing;
  6252. }
  6253. }
  6254. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  6255. {
  6256. //For PSU
  6257. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  6258. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  6259. cab->ResponseCode = FAILED_DIN70121;
  6260. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  6261. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_NotReady;
  6262. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  6263. DEBUG_INFO("IsolationStatus = 3 (fault)\n");
  6264. errn = -1;
  6265. }
  6266. else
  6267. {
  6268. //For PSU
  6269. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  6270. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  6271. cab->ResponseCode = FAILED_DIN70121;
  6272. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  6273. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_NotReady;
  6274. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  6275. DEBUG_INFO("Undefined Isolation Status(%d)\n", sys->IsolationStatus);
  6276. }
  6277. //Check for CSU command of "Stop by EVSE"
  6278. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6279. {
  6280. cab->ResponseCode = FAILED_DIN70121;
  6281. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  6282. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  6283. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  6284. errn = -1;
  6285. DEBUG_INFO("EVSE_Shutdown\n");
  6286. }
  6287. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6288. {
  6289. cab->ResponseCode = FAILED_DIN70121;
  6290. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6291. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  6292. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  6293. errn = -1;
  6294. DEBUG_INFO("EVSE_EmergencyShutdown\n");
  6295. }
  6296. else if (ShmInternalComm->ChargingPermission == FALSE)
  6297. {
  6298. cab->ResponseCode = FAILED_DIN70121;
  6299. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  6300. cab->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  6301. cab->EVSEProcessing = dinEVSEProcessingType_Finished;
  6302. errn = -1;
  6303. DEBUG_ERROR("ChargingPermission = FALSE\n");
  6304. }
  6305. else
  6306. {
  6307. //null
  6308. }
  6309. //Response to CP Error
  6310. #if CP_PROTECTION_MECHANISM == ENABLE
  6311. //#if 1
  6312. if (sys->CableCheckPreCountDownDone == TRUE) //[To-Do] Here should be modified to 2 seconds
  6313. {
  6314. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  6315. {
  6316. cab->ResponseCode = FAILED_DIN70121;
  6317. cab->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6318. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6319. Update_V2G_Status(Other_Fault);
  6320. errn = -1;
  6321. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6322. //CCS_SECC_CP_State_Error (023889)
  6323. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6324. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6325. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6326. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  6327. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  6328. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  6329. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  6330. DEBUG_INFO("Emergency Stop by CP Error (%d, %.02f V, %d)\n", sys->CpState, sys->CpVoltage, cab->cnt );
  6331. //cab->cnt = 0;
  6332. }
  6333. }
  6334. #endif
  6335. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  6336. #if PARAMETER_NORMAL_MODE == ENABLE
  6337. SHM_Read_din_CableCheckRes(&v2gObject.DIN, ShmCcsData);
  6338. #else
  6339. Sudo_Parameter_din_CableCheckRes();
  6340. #endif
  6341. //STEP 4: ============ Encode and Send Response Message ===========
  6342. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  6343. {
  6344. DEBUG_ERROR("Tx encoded msg error\n");
  6345. errn = -1;
  6346. }
  6347. //STEP 5: ============ Update Flags ===========
  6348. sys->ConnectorLocked = TRUE; //Inicating EVSE that the CCS Connector is Locked.
  6349. return errn;
  6350. }
  6351. /**
  6352. *
  6353. * @param AcceptFd
  6354. * @return
  6355. */
  6356. int Proc_iso1_CableCheckRes(int AcceptFd)
  6357. {
  6358. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  6359. //if it is not the same, the packet should be ignored.
  6360. //STEP 1: ============ Initialize ============
  6361. //int i = 0;
  6362. int errn = 0;
  6363. bitstream_t v2g_tx_stream;
  6364. static struct CableCheckResponse_ISO15118_2014 *cab;
  6365. static struct ChargingInfoData *sys;
  6366. size_t pos = 0;
  6367. v2g_tx_stream.pos = &pos;
  6368. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6369. v2g_tx_stream.data = v2gBuffer.tx;
  6370. cab = &ShmCcsData->V2GMessage_ISO15118_2014.CableCheckResponse;
  6371. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6372. cab->ResponseCode = OK_ISO15118_2014;
  6373. cab->cnt++;
  6374. DEBUG_PRINTF_EVCOMM_DETAIL("CP_State = %d (%.02f V), V_now = %.02f, Isolation Status = %d, EVSEProcessing = %d\n",
  6375. sys->CpState,
  6376. sys->CpVoltage,
  6377. sys->PresentChargingVoltage,
  6378. sys->IsolationStatus,
  6379. cab->EVSEProcessing);
  6380. //[HEADER] Check Req SessionID
  6381. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  6382. {
  6383. cab->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  6384. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  6385. errn = -1;
  6386. }
  6387. //Check for SequenceError
  6388. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6389. {
  6390. cab->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  6391. DEBUG_ERROR("SequenceError => End_Process\n");
  6392. errn = -1;
  6393. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6394. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6395. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6396. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6397. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6398. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6399. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6400. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6401. }
  6402. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  6403. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  6404. {
  6405. //For PSU
  6406. sys->EvBatterytargetVoltage = CABLECHECK_TARGET_VOLTAGE; //500V //[To-Do] this should be modified to auto decision.
  6407. sys->EvBatterytargetCurrent = CABLECHECK_TARGET_CURRENT; //2A(default), unit: 1A
  6408. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  6409. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  6410. cab->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  6411. }
  6412. else if ((sys->IsolationStatus == GFD_Valid) || (sys->IsolationStatus == GFD_Warning)) //1: valid, 2:waring
  6413. {
  6414. //For PSU
  6415. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  6416. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  6417. if (sys->PresentChargingVoltage < 60) // < 60V
  6418. {
  6419. DEBUG_INFO( "[V2G][CableCheck]Pass (V_now = %.02f, Isolated = %d)\n", sys->PresentChargingVoltage, sys->IsolationStatus);
  6420. //IsolationStatus
  6421. if (sys->IsolationStatus == GFD_Valid)
  6422. {
  6423. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  6424. }
  6425. else if (sys->IsolationStatus == GFD_Warning)
  6426. {
  6427. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Warning;
  6428. }
  6429. else
  6430. {
  6431. DEBUG_WARN("unexpected IsolationStatus(%d)\n", sys->IsolationStatus);
  6432. }
  6433. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  6434. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  6435. }
  6436. else
  6437. {
  6438. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  6439. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive;
  6440. cab->EVSEProcessing = iso1EVSEProcessingType_Ongoing;
  6441. }
  6442. }
  6443. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  6444. {
  6445. //For PSU
  6446. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  6447. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  6448. cab->ResponseCode = FAILED_ISO15118_2014;
  6449. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  6450. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_NotReady;
  6451. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  6452. DEBUG_INFO("IsolationStatus = 3 (fault)\n");
  6453. errn = -1;
  6454. }
  6455. //else if (sys->IsolationStatus == GFD_No_IMD){} //only for ISO15118
  6456. else
  6457. {
  6458. //For PSU
  6459. sys->EvBatterytargetVoltage = 0; //0V, asking PSU to discharge to 0V
  6460. sys->EvBatterytargetCurrent = 0; //0A, unit: 1A
  6461. cab->ResponseCode = FAILED_ISO15118_2014;
  6462. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  6463. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_NotReady;
  6464. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  6465. DEBUG_INFO("Undefined Isolation Status.\n");
  6466. }
  6467. //Check for CSU command of "Stop by EVSE"
  6468. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6469. {
  6470. cab->ResponseCode = FAILED_ISO15118_2014;
  6471. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  6472. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  6473. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  6474. errn = -1;
  6475. DEBUG_INFO("EVSE_Shutdown\n");
  6476. }
  6477. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6478. {
  6479. cab->ResponseCode = FAILED_ISO15118_2014;
  6480. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6481. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  6482. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  6483. errn = -1;
  6484. DEBUG_INFO("EVSE_EmergencyShutdown\n");
  6485. }
  6486. else if (ShmInternalComm->ChargingPermission == FALSE)
  6487. {
  6488. cab->ResponseCode = FAILED_ISO15118_2014;
  6489. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  6490. cab->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  6491. cab->EVSEProcessing = iso1EVSEProcessingType_Finished;
  6492. errn = -1;
  6493. DEBUG_ERROR("ChargingPermission = FALSE\n");
  6494. }
  6495. else
  6496. {
  6497. //null
  6498. }
  6499. //Response to CP Error
  6500. #if CP_PROTECTION_MECHANISM == ENABLE
  6501. if (sys->CableCheckPreCountDownDone == TRUE) //[To-Do] Here should be modified to 2 seconds
  6502. {
  6503. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  6504. {
  6505. cab->ResponseCode = FAILED_ISO15118_2014;
  6506. cab->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6507. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6508. Update_V2G_Status(Other_Fault);
  6509. errn = -1;
  6510. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6511. //CCS_SECC_CP_State_Error (023889)
  6512. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6513. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6514. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6515. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  6516. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  6517. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  6518. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  6519. DEBUG_INFO("Emergency Stop by CP Error (%d, %.02f V, %d)\n", sys->CpState, sys->CpVoltage, cab->cnt);
  6520. //cab->cnt = 0;
  6521. }
  6522. }
  6523. #endif
  6524. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  6525. #if PARAMETER_NORMAL_MODE == ENABLE
  6526. SHM_Read_iso1_CableCheckRes(&v2gObject.ISO1, ShmCcsData);
  6527. #else
  6528. Sudo_Parameter_iso1_CableCheckRes();
  6529. #endif
  6530. //STEP 4: ============ Encode and Send Response Message ===========
  6531. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  6532. {
  6533. DEBUG_ERROR("Tx encoded msg error\n");
  6534. errn = -1;
  6535. }
  6536. //STEP 5: ============ Update Flags ===========
  6537. sys->ConnectorLocked = TRUE; //Inicating EVSE that the CCS Connector is Locked.
  6538. return errn;
  6539. }
  6540. /**
  6541. *
  6542. * @param AcceptFd
  6543. * @return
  6544. */
  6545. int Proc_din_CableCheckReq(int AcceptFd)
  6546. {
  6547. int errn = 0;
  6548. DEBUG_INFO("Request in.\n");
  6549. //Print the decoded XML Document
  6550. PRINT_XML_DOC_DIN_CableCheckReq(&v2gObject.DIN);
  6551. //Save into Share Memory
  6552. SHM_Save_din_CableCheckReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  6553. //Check for EV Error Code
  6554. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.CableCheckRequest.DC_EVStatus.EVErrorCode);
  6555. errn = Proc_din_CableCheckRes(AcceptFd);
  6556. if (errn == 0)
  6557. {
  6558. //send response successfully.
  6559. DEBUG_INFO("Response out.\n");
  6560. }
  6561. else
  6562. {
  6563. DEBUG_ERROR("Proc_din_CableCheckRes() fail: %d (DEC)", errn);
  6564. }
  6565. return errn;
  6566. }
  6567. /**
  6568. *
  6569. * @param AcceptFd
  6570. * @return
  6571. */
  6572. int Proc_iso1_CableCheckReq(int AcceptFd)
  6573. {
  6574. int errn = 0;
  6575. DEBUG_INFO("Request in.\n");
  6576. //Print the decoded XML Document
  6577. PRINT_XML_DOC_ISO1_CableCheckReq(&v2gObject.ISO1);
  6578. //Save into Share Memory
  6579. SHM_Save_iso1_CableCheckReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  6580. //Check for EV Error Code
  6581. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.CableCheckRequest.DC_EVStatus.EVErrorCode);
  6582. errn = Proc_iso1_CableCheckRes(AcceptFd);
  6583. if (errn == 0)
  6584. {
  6585. //send response successfully.
  6586. DEBUG_INFO("Response out.\n");
  6587. }
  6588. else
  6589. {
  6590. DEBUG_ERROR("Proc_iso1_CableCheckRes() fail: %d (DEC)\n", errn);
  6591. }
  6592. return errn;
  6593. }
  6594. /**
  6595. *
  6596. * @param shm_ccs
  6597. */
  6598. void SHM_Init_din_PreChargeRes(struct CcsData *shm_ccs)
  6599. {
  6600. struct PreChargeResponse_DIN70121 *in;
  6601. in = &shm_ccs->V2GMessage_DIN70121.PreChargeResponse;
  6602. //----- [BODY (1/3)] ResponseCode -----
  6603. in->ResponseCode = dinresponseCodeType_OK;
  6604. //----- [BODY (2/3)] EVSEPresentVoltage -----
  6605. short value = 0; int multiplier = 0; unsigned char unit = 0;
  6606. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for Test
  6607. value = 0; multiplier = 0; unit = V_DIN70121; //waiting for CsuComm to update V to fit EV Target
  6608. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  6609. //----- [BODY (3/3)] DC_EVSEStatus -----
  6610. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  6611. }
  6612. /**
  6613. *
  6614. */
  6615. void Sudo_Parameter_din_PreChargeRes()
  6616. {
  6617. init_dinBodyType(&v2gObject.DIN.V2G_Message.Body);
  6618. init_dinPreChargeResType(&v2gObject.DIN.V2G_Message.Body.PreChargeRes);
  6619. v2gObject.DIN.V2G_Message.Body.PreChargeRes_isUsed = 1u;
  6620. //----- [BODY (1/3)] ResponseCode -----
  6621. struct dinPreChargeResType *res;
  6622. res = &v2gObject.DIN.V2G_Message.Body.PreChargeRes;
  6623. res->ResponseCode = dinresponseCodeType_OK;
  6624. //----- [BODY (2/3)] EVSEPresentVoltage -----
  6625. res->EVSEPresentVoltage.Value = 3820;
  6626. res->EVSEPresentVoltage.Multiplier = -1;
  6627. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  6628. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  6629. // dinunitSymbolType_h = 0,
  6630. // dinunitSymbolType_m = 1,
  6631. // dinunitSymbolType_s = 2,
  6632. // dinunitSymbolType_A = 3,
  6633. // dinunitSymbolType_Ah = 4,
  6634. // dinunitSymbolType_V = 5,
  6635. // dinunitSymbolType_VA = 6,
  6636. // dinunitSymbolType_W = 7,
  6637. // dinunitSymbolType_W_s = 8,
  6638. // dinunitSymbolType_Wh = 9
  6639. //----- [BODY (3/3)] DC_EVSEStatus -----
  6640. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  6641. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  6642. // dinisolationLevelType_Invalid = 0,
  6643. // dinisolationLevelType_Valid = 1,
  6644. // dinisolationLevelType_Warning = 2,
  6645. // dinisolationLevelType_Fault = 3
  6646. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  6647. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6648. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  6649. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6650. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6651. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6652. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6653. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6654. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  6655. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  6656. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  6657. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  6658. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  6659. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  6660. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  6661. // dinEVSENotificationType_None = 0,
  6662. // dinEVSENotificationType_StopCharging = 1,
  6663. // dinEVSENotificationType_ReNegotiation = 2
  6664. }
  6665. /**
  6666. *
  6667. */
  6668. void Sudo_Parameter_iso1_PreChargeRes()
  6669. {
  6670. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  6671. init_iso1PreChargeResType(&v2gObject.ISO1.V2G_Message.Body.PreChargeRes);
  6672. v2gObject.ISO1.V2G_Message.Body.PreChargeRes_isUsed = 1u;
  6673. //----- [BODY (1/3)] ResponseCode -----
  6674. struct iso1PreChargeResType *res;
  6675. res = &v2gObject.ISO1.V2G_Message.Body.PreChargeRes;
  6676. res->ResponseCode = iso1responseCodeType_OK;
  6677. //----- [BODY (2/3)] EVSEPresentVoltage -----
  6678. res->EVSEPresentVoltage.Value = 3820;
  6679. res->EVSEPresentVoltage.Multiplier = -1;
  6680. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  6681. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  6682. //iso1unitSymbolType_h = 0,
  6683. //iso1unitSymbolType_m = 1,
  6684. //iso1unitSymbolType_s = 2,
  6685. //iso1unitSymbolType_A = 3,
  6686. //iso1unitSymbolType_V = 4,
  6687. //iso1unitSymbolType_W = 5,
  6688. //iso1unitSymbolType_Wh = 6
  6689. //----- [BODY (3/3)] DC_EVSEStatus -----
  6690. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  6691. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  6692. //iso1isolationLevelType_Invalid = 0,
  6693. //iso1isolationLevelType_Valid = 1,
  6694. //iso1isolationLevelType_Warning = 2,
  6695. //iso1isolationLevelType_Fault = 3,
  6696. //iso1isolationLevelType_No_IMD = 4
  6697. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  6698. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  6699. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  6700. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  6701. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  6702. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  6703. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  6704. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  6705. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  6706. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  6707. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  6708. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  6709. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  6710. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  6711. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  6712. //iso1EVSENotificationType_None = 0,
  6713. //iso1EVSENotificationType_StopCharging = 1,
  6714. //iso1EVSENotificationType_ReNegotiation = 2
  6715. }
  6716. /**
  6717. *
  6718. * @param AcceptFd
  6719. * @return
  6720. */
  6721. int Proc_din_PreChargeRes(int AcceptFd)
  6722. {
  6723. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  6724. //if it is not the same, the packet should be ignored.
  6725. //STEP 1: ============ Initialize ============
  6726. //int i = 0;
  6727. int errn = 0;
  6728. bitstream_t v2g_tx_stream;
  6729. static struct PreChargeResponse_DIN70121 *pre;
  6730. static struct ChargingInfoData *sys;
  6731. size_t pos = 0;
  6732. v2g_tx_stream.pos = &pos;
  6733. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6734. v2g_tx_stream.data = v2gBuffer.tx;
  6735. pre = &ShmCcsData->V2GMessage_DIN70121.PreChargeResponse;
  6736. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6737. pre->ResponseCode = OK_DIN70121;
  6738. //EVSE Status Code
  6739. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  6740. //[HEADER] Check Req SessionID
  6741. if (Check_din_V2G_Rx_MSG_SessionID(&v2gObject.DIN) < 0)
  6742. {
  6743. pre->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  6744. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  6745. errn = -1;
  6746. }
  6747. //Check for SequenceError
  6748. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6749. {
  6750. pre->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  6751. DEBUG_ERROR("SequenceError => End_Process\n");
  6752. errn = -1;
  6753. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6754. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6755. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6756. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6757. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6758. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6759. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6760. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6761. }
  6762. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  6763. SAVE_PhysicalValueType_DIN70121(&pre->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  6764. //Isolation Status
  6765. if (sys->IsolationStatus == GFD_Invalid) //0: invalid
  6766. {
  6767. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  6768. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6769. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6770. Update_V2G_Status(Other_Fault);
  6771. errn = -1;
  6772. DEBUG_ERROR("IsolationStatus = %d\n", sys->IsolationStatus);
  6773. }
  6774. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  6775. {
  6776. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  6777. }
  6778. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  6779. {
  6780. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  6781. }
  6782. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  6783. {
  6784. DEBUG_ERROR("GFD_Fault => Emergency Shutdown\n");
  6785. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  6786. pre->ResponseCode = FAILED_DIN70121;
  6787. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6788. errn = -1;
  6789. }
  6790. else //GFD_No_IMD or other unexpected status
  6791. {
  6792. pre->ResponseCode = FAILED_DIN70121;
  6793. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  6794. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6795. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6796. Update_V2G_Status(Other_Fault);
  6797. errn = -1;
  6798. DEBUG_ERROR("IsolationStatus = %d (undefined)\n", sys->IsolationStatus);
  6799. }
  6800. //Check for CSU command of "Stop by EVSE"
  6801. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6802. {
  6803. //Check for Alarm Code: CCS GFD trip (012235)
  6804. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  6805. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  6806. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  6807. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  6808. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  6809. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  6810. {
  6811. DEBUG_ERROR("CCS GFD trip => EVSE_Shutdown\n");
  6812. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  6813. }
  6814. else
  6815. {
  6816. DEBUG_ERROR("EVSE_Shutdown\n");
  6817. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  6818. }
  6819. pre->ResponseCode = FAILED_DIN70121;
  6820. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  6821. errn = -1;
  6822. }
  6823. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  6824. {
  6825. //Check for Alarm Code: CCS GFD trip (012235)
  6826. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  6827. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  6828. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  6829. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  6830. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  6831. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  6832. {
  6833. DEBUG_ERROR("CCS GFD trip => EVSE_EmergencyShutdown\n");
  6834. pre->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  6835. }
  6836. else
  6837. {
  6838. DEBUG_ERROR("EVSE_EmergencyShutdown\n");
  6839. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  6840. }
  6841. pre->ResponseCode = FAILED_DIN70121;
  6842. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6843. errn = -1;
  6844. }
  6845. else if (ShmInternalComm->ChargingPermission == FALSE)
  6846. {
  6847. DEBUG_ERROR("ChargingPermission = FALSE\n");
  6848. pre->ResponseCode = FAILED_DIN70121;
  6849. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  6850. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  6851. errn = -1;
  6852. }
  6853. else
  6854. {
  6855. //null
  6856. }
  6857. //Response to CP Error
  6858. #if CP_PROTECTION_MECHANISM == ENABLE
  6859. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  6860. {
  6861. pre->ResponseCode = FAILED_DIN70121;
  6862. pre->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6863. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6864. Update_V2G_Status(Other_Fault);
  6865. errn = -1;
  6866. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6867. //CCS_SECC_CP_State_Error (023889)
  6868. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6869. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6870. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6871. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  6872. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  6873. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  6874. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  6875. DEBUG_ERROR("Emergency Stop by CP Error (%d, %.02f V)\n", sys->CpState, sys->CpVoltage);
  6876. }
  6877. #endif
  6878. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  6879. #if PARAMETER_NORMAL_MODE == ENABLE
  6880. SHM_Read_din_PreChargeRes(&v2gObject.DIN, ShmCcsData);
  6881. #else
  6882. Sudo_Parameter_din_PreChargeRes();
  6883. #endif
  6884. //STEP 4: ============ Encode and Send Response Message ===========
  6885. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  6886. {
  6887. DEBUG_ERROR("Tx encoded msg error\n");
  6888. errn = -1;
  6889. }
  6890. //STEP 5: ============ Update Flags ===========
  6891. return errn;
  6892. }
  6893. /**
  6894. *
  6895. * @param AcceptFd
  6896. * @return
  6897. */
  6898. int Proc_iso1_PreChargeRes(int AcceptFd)
  6899. {
  6900. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  6901. //if it is not the same, the packet should be ignored.
  6902. //STEP 1: ============ Initialize ============
  6903. //int i = 0;
  6904. int errn = 0;
  6905. bitstream_t v2g_tx_stream;
  6906. static struct PreChargeResponse_ISO15118_2014 *pre;
  6907. static struct ChargingInfoData *sys;
  6908. size_t pos = 0;
  6909. v2g_tx_stream.pos = &pos;
  6910. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  6911. v2g_tx_stream.data = v2gBuffer.tx;
  6912. pre = &ShmCcsData->V2GMessage_ISO15118_2014.PreChargeResponse;
  6913. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  6914. pre->ResponseCode = OK_ISO15118_2014;
  6915. //EVSE Status Code
  6916. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  6917. //[HEADER] Check Req SessionID
  6918. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  6919. {
  6920. pre->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  6921. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  6922. errn = -1;
  6923. }
  6924. //Check for SequenceError
  6925. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  6926. {
  6927. pre->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  6928. DEBUG_ERROR("SequenceError => End_Process\n");
  6929. errn = -1;
  6930. //Update_ShmStatusCode(); //[To-Do] to be implemented
  6931. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  6932. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  6933. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  6934. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  6935. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  6936. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  6937. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  6938. }
  6939. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  6940. SAVE_PhysicalValueType_ISO15118_2014(&pre->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  6941. //Isolation Status
  6942. if (sys->IsolationStatus == GFD_Invalid) //0: invalid(on going)
  6943. {
  6944. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  6945. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6946. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6947. Update_V2G_Status(Other_Fault);
  6948. errn = -1;
  6949. DEBUG_ERROR("IsolationStatus = %d\n", sys->IsolationStatus);
  6950. }
  6951. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  6952. {
  6953. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  6954. }
  6955. else if (sys->IsolationStatus == GFD_Warning) //2: waring
  6956. {
  6957. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Warning; //1
  6958. }
  6959. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  6960. {
  6961. DEBUG_ERROR("GFD_Fault => Emergency Shutdown\n");
  6962. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  6963. pre->ResponseCode = FAILED_ISO15118_2014;
  6964. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6965. errn = -1;
  6966. }
  6967. else
  6968. {
  6969. pre->ResponseCode = FAILED_ISO15118_2014;
  6970. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  6971. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  6972. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  6973. Update_V2G_Status(Other_Fault);
  6974. errn = -1;
  6975. DEBUG_ERROR("IsolationStatus = %d (undefined)\n", sys->IsolationStatus);
  6976. }
  6977. //Check for CSU command of "Stop by EVSE"
  6978. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  6979. {
  6980. //Check for Alarm Code: CCS GFD trip (012235)
  6981. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  6982. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  6983. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  6984. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  6985. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  6986. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  6987. {
  6988. DEBUG_ERROR("CCS GFD trip => EVSE_Shutdown\n");
  6989. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  6990. }
  6991. else
  6992. {
  6993. DEBUG_ERROR("EVSE_Shutdown\n");
  6994. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  6995. }
  6996. pre->ResponseCode = FAILED_ISO15118_2014;
  6997. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  6998. errn = -1;
  6999. }
  7000. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7001. {
  7002. //Check for Alarm Code: CCS GFD trip (012235)
  7003. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  7004. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  7005. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  7006. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  7007. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  7008. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  7009. {
  7010. DEBUG_ERROR("CCS GFD trip => EVSE_EmergencyShutdown\n");
  7011. pre->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  7012. }
  7013. else
  7014. {
  7015. DEBUG_ERROR("EVSE_EmergencyShutdown\n");
  7016. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  7017. }
  7018. pre->ResponseCode = FAILED_ISO15118_2014;
  7019. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7020. errn = -1;
  7021. }
  7022. else if (ShmInternalComm->ChargingPermission == FALSE)
  7023. {
  7024. DEBUG_ERROR("ChargingPermission = FALSE\n");
  7025. pre->ResponseCode = FAILED_ISO15118_2014;
  7026. pre->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  7027. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7028. errn = -1;
  7029. }
  7030. else
  7031. {
  7032. //null
  7033. }
  7034. //Response to CP Error
  7035. #if CP_PROTECTION_MECHANISM == ENABLE
  7036. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  7037. {
  7038. pre->ResponseCode = FAILED_ISO15118_2014;
  7039. pre->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7040. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7041. Update_V2G_Status(Other_Fault);
  7042. errn = -1;
  7043. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7044. //CCS_SECC_CP_State_Error (023889)
  7045. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7046. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7047. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7048. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  7049. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  7050. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  7051. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  7052. DEBUG_ERROR("Emergency Stop by CP Error (%d, %.02f V)\n", sys->CpState, sys->CpVoltage);
  7053. }
  7054. #endif
  7055. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7056. #if PARAMETER_NORMAL_MODE == ENABLE
  7057. SHM_Read_iso1_PreChargeRes(&v2gObject.ISO1, ShmCcsData);
  7058. #else
  7059. Sudo_Parameter_iso1_PreChargeRes();
  7060. #endif
  7061. //STEP 4: ============ Encode and Send Response Message ===========
  7062. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  7063. {
  7064. DEBUG_ERROR("Tx encoded msg error\n");
  7065. errn = -1;
  7066. }
  7067. //STEP 5: ============ Update Flags ===========
  7068. return errn;
  7069. }
  7070. /**
  7071. *
  7072. * @param AcceptFd
  7073. * @return
  7074. */
  7075. int Proc_din_PreChargeReq(int AcceptFd)
  7076. {
  7077. int errn = 0;
  7078. DEBUG_INFO("Request in.\n");
  7079. //Print the decoded XML Document
  7080. PRINT_XML_DOC_DIN_PreChargeReq(&v2gObject.DIN);
  7081. //Save into Share Memory
  7082. SHM_Save_din_PreChargeReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  7083. //Check for EV Error Code
  7084. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PreChargeRequest.DC_EVStatus.EVErrorCode);
  7085. errn = Proc_din_PreChargeRes(AcceptFd);
  7086. if (errn == 0)
  7087. {
  7088. //send response successfully.
  7089. DEBUG_INFO("Response out.\n");
  7090. }
  7091. else
  7092. {
  7093. DEBUG_ERROR("Proc_din_PreChargeRes() fail: %d (DEC)\n", errn);
  7094. }
  7095. return errn;
  7096. }
  7097. /**
  7098. *
  7099. * @param AcceptFd
  7100. * @return
  7101. */
  7102. int Proc_iso1_PreChargeReq(int AcceptFd)
  7103. {
  7104. int errn = 0;
  7105. DEBUG_INFO("Request in.\n");
  7106. //Print the decoded XML Document
  7107. PRINT_XML_DOC_ISO1_PreChargeReq(&v2gObject.ISO1);
  7108. //Save into Share Memory
  7109. SHM_Save_iso1_PreChargeReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  7110. //Check for EV Error Code
  7111. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PreChargeRequest.DC_EVStatus.EVErrorCode);
  7112. errn = Proc_iso1_PreChargeRes(AcceptFd);
  7113. if (errn == 0)
  7114. {
  7115. //send response successfully.
  7116. DEBUG_INFO("Response out.\n");
  7117. }
  7118. else
  7119. {
  7120. DEBUG_ERROR("Proc_iso1_PreChargeRes() fail: %d (DEC)\n", errn);
  7121. }
  7122. return errn;
  7123. }
  7124. /**
  7125. *
  7126. * @param shm_ccs
  7127. */
  7128. void SHM_Init_din_PowerDeliveryRes(struct CcsData *shm_ccs)
  7129. {
  7130. struct PowerDeliveryResponse_DIN70121 *in;
  7131. in = &shm_ccs->V2GMessage_DIN70121.PowerDeliveryResponse;
  7132. //----- [BODY (1/3)] ResponseCode -----
  7133. in->ResponseCode = dinresponseCodeType_OK;
  7134. //----- [BODY (2/3)] AC_EVSEStatus -----
  7135. //ignore, since DIN 70121 doesn't support AC, yet.
  7136. //----- [BODY (2/3)] DC_EVSEStatus -----
  7137. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  7138. }
  7139. /**
  7140. *
  7141. */
  7142. void Sudo_Parameter_din_PowerDeliveryRes()
  7143. {
  7144. init_dinBodyType(&v2gObject.DIN.V2G_Message.Body);
  7145. init_dinPowerDeliveryResType(&v2gObject.DIN.V2G_Message.Body.PowerDeliveryRes);
  7146. v2gObject.DIN.V2G_Message.Body.PowerDeliveryRes_isUsed = 1u;
  7147. //----- [BODY (1/3)] ResponseCode -----
  7148. struct dinPowerDeliveryResType *res;
  7149. res = &v2gObject.DIN.V2G_Message.Body.PowerDeliveryRes;
  7150. res->ResponseCode = dinresponseCodeType_OK;
  7151. //----- [BODY (2/3)] AC_EVSEStatus -----
  7152. //ignore, since DIN 70121 doesn't support AC, yet.
  7153. //----- [BODY (2/3)] DC_EVSEStatus -----
  7154. res->DC_EVSEStatus_isUsed = 1u;
  7155. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7156. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  7157. // dinisolationLevelType_Invalid = 0,
  7158. // dinisolationLevelType_Valid = 1,
  7159. // dinisolationLevelType_Warning = 2,
  7160. // dinisolationLevelType_Fault = 3
  7161. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7162. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7163. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  7164. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7165. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7166. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7167. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7168. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7169. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  7170. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  7171. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  7172. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  7173. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  7174. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7175. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  7176. // dinEVSENotificationType_None = 0,
  7177. // dinEVSENotificationType_StopCharging = 1,
  7178. // dinEVSENotificationType_ReNegotiation = 2
  7179. }
  7180. /**
  7181. *
  7182. */
  7183. void Sudo_Parameter_iso1_PowerDeliveryRes()
  7184. {
  7185. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  7186. init_iso1PowerDeliveryResType(&v2gObject.ISO1.V2G_Message.Body.PowerDeliveryRes);
  7187. v2gObject.ISO1.V2G_Message.Body.PowerDeliveryRes_isUsed = 1u;
  7188. //----- [BODY (1/3)] ResponseCode -----
  7189. struct iso1PowerDeliveryResType *res;
  7190. res = &v2gObject.ISO1.V2G_Message.Body.PowerDeliveryRes;
  7191. res->ResponseCode = iso1responseCodeType_OK;
  7192. //----- [BODY (2/3)] AC_EVSEStatus -----
  7193. //ignore, since our ISO1 70121 doesn't support AC, yet.
  7194. //----- [BODY (2/3)] DC_EVSEStatus -----
  7195. res->DC_EVSEStatus_isUsed = 1u;
  7196. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7197. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  7198. //iso1isolationLevelType_Invalid = 0,
  7199. //iso1isolationLevelType_Valid = 1,
  7200. //iso1isolationLevelType_Warning = 2,
  7201. //iso1isolationLevelType_Fault = 3,
  7202. //iso1isolationLevelType_No_IMD = 4
  7203. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  7204. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7205. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  7206. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7207. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7208. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7209. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7210. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7211. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  7212. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  7213. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  7214. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  7215. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  7216. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7217. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  7218. //iso1EVSENotificationType_None = 0,
  7219. //iso1EVSENotificationType_StopCharging = 1,
  7220. //iso1EVSENotificationType_ReNegotiation = 2
  7221. }
  7222. /**
  7223. *
  7224. * @param AcceptFd
  7225. * @return
  7226. */
  7227. int Proc_din_PowerDeliveryStartRes(int AcceptFd)
  7228. {
  7229. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  7230. //if it is not the same, the packet should be ignored.
  7231. //STEP 1: ============ Initialize ============
  7232. //int i = 0;
  7233. int errn = 0;
  7234. bitstream_t v2g_tx_stream;
  7235. struct dinPowerDeliveryResType *res;
  7236. struct ChargingInfoData *sys;
  7237. size_t pos = 0;
  7238. v2g_tx_stream.pos = &pos;
  7239. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7240. v2g_tx_stream.data = v2gBuffer.tx;
  7241. res = &v2gObject.DIN.V2G_Message.Body.PowerDeliveryRes;
  7242. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7243. res->ResponseCode = OK_DIN70121;
  7244. //[HEADER] Check Req SessionID
  7245. if (Check_din_V2G_Rx_MSG_SessionID(&v2gObject.DIN) < 0)
  7246. {
  7247. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  7248. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  7249. errn = -1;
  7250. }
  7251. //Check for SequenceError
  7252. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7253. {
  7254. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  7255. DEBUG_ERROR("SequenceError => End_Process\n");
  7256. errn = -1;
  7257. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7258. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7259. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7260. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7261. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7262. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7263. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7264. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7265. }
  7266. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  7267. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7268. #if PARAMETER_NORMAL_MODE == ENABLE
  7269. SHM_Read_din_PowerDeliveryRes(&v2gObject.DIN, ShmCcsData);
  7270. #else
  7271. Sudo_Parameter_din_PowerDeliveryRes();
  7272. #endif
  7273. //EVSE Status Code
  7274. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  7275. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7276. //Check for CSU command of "Stop by EVSE"
  7277. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7278. {
  7279. //res->ResponseCode = FAILED_DIN70121;
  7280. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7281. }
  7282. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7283. {
  7284. //res->ResponseCode = FAILED_DIN70121;
  7285. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7286. }
  7287. else if (ShmInternalComm->ChargingPermission == FALSE)
  7288. {
  7289. //res->ResponseCode = FAILED_DIN70121;
  7290. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7291. }
  7292. //STEP 4: ============ Encode and Send Response Message ===========
  7293. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  7294. {
  7295. DEBUG_ERROR("Tx encoded msg error\n");
  7296. errn = -1;
  7297. }
  7298. //STEP 5: ============ Update Flags ===========
  7299. return errn;
  7300. }
  7301. /**
  7302. *
  7303. * @param AcceptFd
  7304. * @return
  7305. */
  7306. int Proc_iso1_PowerDeliveryStartRes(int AcceptFd)
  7307. {
  7308. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  7309. //if it is not the same, the packet should be ignored.
  7310. //STEP 1: ============ Initialize ============
  7311. //int i = 0;
  7312. int errn = 0;
  7313. bitstream_t v2g_tx_stream;
  7314. struct iso1PowerDeliveryResType *res;
  7315. struct ChargingInfoData *sys;
  7316. size_t pos = 0;
  7317. v2g_tx_stream.pos = &pos;
  7318. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7319. v2g_tx_stream.data = v2gBuffer.tx;
  7320. res = &v2gObject.ISO1.V2G_Message.Body.PowerDeliveryRes;
  7321. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7322. res->ResponseCode = OK_ISO15118_2014;
  7323. //[HEADER] Check Req SessionID
  7324. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  7325. {
  7326. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  7327. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  7328. errn = -1;
  7329. }
  7330. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  7331. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7332. #if PARAMETER_NORMAL_MODE == ENABLE
  7333. SHM_Read_iso1_PowerDeliveryRes(&v2gObject.ISO1, ShmCcsData);
  7334. #else
  7335. Sudo_Parameter_iso1_PowerDeliveryRes();
  7336. #endif
  7337. //EVSE Status Code
  7338. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  7339. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  7340. //Check for CSU command of "Stop by EVSE"
  7341. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7342. {
  7343. //res->ResponseCode = FAILED_ISO15118_2014;
  7344. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7345. }
  7346. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7347. {
  7348. //res->ResponseCode = FAILED_ISO15118_2014;
  7349. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7350. }
  7351. else if (ShmInternalComm->ChargingPermission == FALSE)
  7352. {
  7353. //res->ResponseCode = FAILED_ISO15118_2014;
  7354. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7355. }
  7356. //STEP 4: ============ Encode and Send Response Message ===========
  7357. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  7358. {
  7359. DEBUG_ERROR("Tx encoded msg error\n");
  7360. errn = -1;
  7361. }
  7362. //STEP 5: ============ Update Flags ===========
  7363. return errn;
  7364. }
  7365. /**
  7366. *
  7367. * @param AcceptFd
  7368. * @return
  7369. */
  7370. int Proc_din_PowerDeliveryStartReq(int AcceptFd)
  7371. {
  7372. int errn = 0;
  7373. DEBUG_INFO("Request in.\n");
  7374. //Print the decoded XML Document
  7375. PRINT_XML_DOC_DIN_PowerDeliveryReq(&v2gObject.DIN);
  7376. //Save into Share Memory
  7377. SHM_Save_din_PowerDeliveryReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  7378. //Check for EV Error Code
  7379. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  7380. errn = Proc_din_PowerDeliveryStartRes(AcceptFd);
  7381. if (errn == 0)
  7382. {
  7383. //send response successfully.
  7384. DEBUG_INFO("Response out.\n");
  7385. }
  7386. else
  7387. {
  7388. DEBUG_ERROR("Proc_din_PowerDeliveryStartRes() fail: %d (DEC)\n", errn);
  7389. }
  7390. return errn;
  7391. }
  7392. /**
  7393. *
  7394. * @param AcceptFd
  7395. * @return
  7396. */
  7397. int Proc_iso1_PowerDeliveryStartReq(int AcceptFd)
  7398. {
  7399. int errn = 0;
  7400. DEBUG_INFO("Request in.\n");
  7401. //Print the decoded XML Document
  7402. PRINT_XML_DOC_ISO1_PowerDeliveryReq(&v2gObject.ISO1);
  7403. //Save into Share Memory
  7404. SHM_Save_iso1_PowerDeliveryReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  7405. //Check for EV Error Code
  7406. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  7407. errn = Proc_iso1_PowerDeliveryStartRes(AcceptFd);
  7408. if (errn == 0)
  7409. {
  7410. //send response successfully.
  7411. DEBUG_INFO("Response out.\n");
  7412. }
  7413. else
  7414. {
  7415. DEBUG_ERROR("Proc_iso1_PowerDeliveryStartRes() fail: %d (DEC)\n", errn);
  7416. }
  7417. return errn;
  7418. }
  7419. /**
  7420. *
  7421. * @param shm_ccs
  7422. */
  7423. void SHM_Init_din_CurrentDemandRes(struct CcsData *shm_ccs)
  7424. {
  7425. struct CurrentDemandResponse_DIN70121 *in;
  7426. in = &shm_ccs->V2GMessage_DIN70121.CurrentDemandResponse;
  7427. //----- [BODY (1/10)] ResponseCode -----
  7428. in->ResponseCode = dinresponseCodeType_OK;
  7429. //----- [BODY (2/10)] DC_EVSEStatus -----
  7430. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  7431. //----- [BODY (3/10)] EVSEPresentVoltage -----
  7432. short value = 0; int multiplier = 0; unsigned char unit = 0;
  7433. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for test only.
  7434. value = 0; multiplier = 0; unit = V_DIN70121;
  7435. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  7436. //----- [BODY (4/10)] EVSEPresentCurrent -----
  7437. //value = 600; multiplier = -1; unit = A_DIN70121; //60A, for test only.
  7438. value = 0; multiplier = 0; unit = A_DIN70121;
  7439. SHM_Save_dinPhysicalValueType(&in->EVSEPresentCurrent, value, multiplier, unit);
  7440. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  7441. in->EVSECurrentLimitAchieved = FALSE;
  7442. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  7443. in->EVSEVoltageLimitAchieved = FALSE;
  7444. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  7445. in->EVSEPowerLimitAchieved = FALSE;
  7446. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  7447. //value = 7500; multiplier = -1; unit = V_DIN70121; //750V
  7448. value = 5000; multiplier = -1; unit = V_DIN70121; //500V
  7449. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumVoltageLimit, value, multiplier, unit);
  7450. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  7451. value = 600; multiplier = -1; unit = A_DIN70121; //60A
  7452. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumCurrentLimit, value, multiplier, unit);
  7453. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  7454. value = 3000; multiplier = 1; unit = W_DIN70121; //30KW
  7455. SHM_Save_dinPhysicalValueType(&in->EVSEMaximumPowerLimit, value, multiplier, unit);
  7456. }
  7457. /**
  7458. *
  7459. */
  7460. void Sudo_Parameter_din_CurrentDemandRes()
  7461. {
  7462. init_dinBodyType(&v2gObject.DIN.V2G_Message.Body);
  7463. init_dinCurrentDemandResType(&v2gObject.DIN.V2G_Message.Body.CurrentDemandRes);
  7464. v2gObject.DIN.V2G_Message.Body.CurrentDemandRes_isUsed = 1u;
  7465. //----- [BODY (1/10)] ResponseCode -----
  7466. struct dinCurrentDemandResType *res;
  7467. res = &v2gObject.DIN.V2G_Message.Body.CurrentDemandRes;
  7468. res->ResponseCode = dinresponseCodeType_OK;
  7469. //----- [BODY (2/10)] DC_EVSEStatus -----
  7470. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7471. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid;
  7472. // dinisolationLevelType_Invalid = 0,
  7473. // dinisolationLevelType_Valid = 1,
  7474. // dinisolationLevelType_Warning = 2,
  7475. // dinisolationLevelType_Fault = 3
  7476. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7477. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7478. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  7479. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7480. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7481. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7482. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7483. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7484. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  7485. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  7486. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  7487. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  7488. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  7489. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7490. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  7491. // dinEVSENotificationType_None = 0,
  7492. // dinEVSENotificationType_StopCharging = 1,
  7493. // dinEVSENotificationType_ReNegotiation = 2
  7494. //----- [BODY (3/10)] EVSEPresentVoltage -----
  7495. res->EVSEPresentVoltage.Value = 3820;
  7496. res->EVSEPresentVoltage.Multiplier = -1;
  7497. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  7498. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  7499. //----- [BODY (4/10)] EVSEPresentCurrent -----
  7500. res->EVSEPresentCurrent.Value = 1200;
  7501. res->EVSEPresentCurrent.Multiplier = -1;
  7502. res->EVSEPresentCurrent.Unit_isUsed = 1u;
  7503. res->EVSEPresentCurrent.Unit = dinunitSymbolType_A;
  7504. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  7505. res->EVSECurrentLimitAchieved = 0;
  7506. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  7507. res->EVSEVoltageLimitAchieved = 0;
  7508. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  7509. res->EVSEPowerLimitAchieved = 0;
  7510. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  7511. res->EVSEMaximumVoltageLimit_isUsed = 1u;
  7512. res->EVSEMaximumVoltageLimit.Value = 7500;
  7513. res->EVSEMaximumVoltageLimit.Multiplier = -1;
  7514. res->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  7515. res->EVSEMaximumVoltageLimit.Unit = dinunitSymbolType_V;
  7516. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  7517. res->EVSEMaximumCurrentLimit_isUsed = 1u;
  7518. res->EVSEMaximumCurrentLimit.Value = 1200;
  7519. res->EVSEMaximumCurrentLimit.Multiplier = -1;
  7520. res->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  7521. res->EVSEMaximumCurrentLimit.Unit = dinunitSymbolType_A;
  7522. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  7523. res->EVSEMaximumPowerLimit_isUsed = 1u;
  7524. res->EVSEMaximumPowerLimit.Value = 6000;
  7525. res->EVSEMaximumPowerLimit.Multiplier = 1;
  7526. res->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  7527. res->EVSEMaximumPowerLimit.Unit = dinunitSymbolType_W;
  7528. }
  7529. /**
  7530. *
  7531. */
  7532. void Sudo_Parameter_iso1_CurrentDemandRes()
  7533. {
  7534. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  7535. init_iso1CurrentDemandResType(&v2gObject.ISO1.V2G_Message.Body.CurrentDemandRes);
  7536. v2gObject.ISO1.V2G_Message.Body.CurrentDemandRes_isUsed = 1u;
  7537. //----- [BODY (1/10)] ResponseCode -----
  7538. struct iso1CurrentDemandResType *res;
  7539. res = &v2gObject.ISO1.V2G_Message.Body.CurrentDemandRes;
  7540. res->ResponseCode = iso1responseCodeType_OK;
  7541. //----- [BODY (2/10)] DC_EVSEStatus -----
  7542. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  7543. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid;
  7544. //iso1isolationLevelType_Invalid = 0,
  7545. //iso1isolationLevelType_Valid = 1,
  7546. //iso1isolationLevelType_Warning = 2,
  7547. //iso1isolationLevelType_Fault = 3,
  7548. //iso1isolationLevelType_No_IMD = 4
  7549. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  7550. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  7551. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  7552. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  7553. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  7554. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  7555. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  7556. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  7557. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  7558. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  7559. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  7560. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  7561. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  7562. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  7563. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  7564. //iso1EVSENotificationType_None = 0,
  7565. //iso1EVSENotificationType_StopCharging = 1,
  7566. //iso1EVSENotificationType_ReNegotiation = 2
  7567. //----- [BODY (3/10)] EVSEPresentVoltage -----
  7568. res->EVSEPresentVoltage.Value = 3820;
  7569. res->EVSEPresentVoltage.Multiplier = -1;
  7570. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  7571. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  7572. //----- [BODY (4/10)] EVSEPresentCurrent -----
  7573. res->EVSEPresentCurrent.Value = 1200;
  7574. res->EVSEPresentCurrent.Multiplier = -1;
  7575. //res->EVSEPresentCurrent.Unit_isUsed = 1u;
  7576. res->EVSEPresentCurrent.Unit = iso1unitSymbolType_A;
  7577. //----- [BODY (5/10)] EVSECurrentLimitAchieved -----
  7578. res->EVSECurrentLimitAchieved = 0;
  7579. //----- [BODY (6/10)] EVSEVoltageLimitAchieved -----
  7580. res->EVSEVoltageLimitAchieved = 0;
  7581. //----- [BODY (7/10)] EVSEPowerLimitAchieved -----
  7582. res->EVSEPowerLimitAchieved = 0;
  7583. //----- [BODY (8/10)] EVSEMaximumVoltageLimit -----
  7584. res->EVSEMaximumVoltageLimit_isUsed = 1u;
  7585. res->EVSEMaximumVoltageLimit.Value = 7500;
  7586. res->EVSEMaximumVoltageLimit.Multiplier = -1;
  7587. //res->EVSEMaximumVoltageLimit.Unit_isUsed = 1u;
  7588. res->EVSEMaximumVoltageLimit.Unit = iso1unitSymbolType_V;
  7589. //----- [BODY (9/10)] EVSEMaximumCurrentLimit -----
  7590. res->EVSEMaximumCurrentLimit_isUsed = 1u;
  7591. res->EVSEMaximumCurrentLimit.Value = 1200;
  7592. res->EVSEMaximumCurrentLimit.Multiplier = -1;
  7593. //res->EVSEMaximumCurrentLimit.Unit_isUsed = 1u;
  7594. res->EVSEMaximumCurrentLimit.Unit = iso1unitSymbolType_A;
  7595. //----- [BODY (10/10)] EVSEMaximumPowerLimit -----
  7596. res->EVSEMaximumPowerLimit_isUsed = 1u;
  7597. res->EVSEMaximumPowerLimit.Value = 6000;
  7598. res->EVSEMaximumPowerLimit.Multiplier = 1;
  7599. //res->EVSEMaximumPowerLimit.Unit_isUsed = 1u;
  7600. res->EVSEMaximumPowerLimit.Unit = iso1unitSymbolType_W;
  7601. }
  7602. /**
  7603. *
  7604. */
  7605. void Sudo_Parameter_iso1_ChargingStatusRes()
  7606. {
  7607. //int i = 0;
  7608. struct iso1ChargingStatusResType *res;
  7609. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  7610. init_iso1ChargingStatusResType(&v2gObject.ISO1.V2G_Message.Body.ChargingStatusRes);
  7611. res = &v2gObject.ISO1.V2G_Message.Body.ChargingStatusRes;
  7612. v2gObject.ISO1.V2G_Message.Body.ChargingStatusRes_isUsed = 1u;
  7613. //----- [BODY (1/10)] ResponseCode -----
  7614. res->ResponseCode = OK_ISO15118_2014;
  7615. //----- [BODY (2/10)] AC_EVSEStatus -----
  7616. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  7617. res->AC_EVSEStatus.NotificationMaxDelay = 0;
  7618. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  7619. // iso1EVSENotificationType_None = 0,
  7620. // iso1EVSENotificationType_StopCharging = 1,
  7621. // iso1EVSENotificationType_ReNegotiation = 2
  7622. //----- [BODY (3/10)] EVSEMaxCurrent -----
  7623. res->EVSEMaxCurrent_isUsed = 1u;
  7624. res->EVSEMaxCurrent.Value = 32;
  7625. res->EVSEMaxCurrent.Multiplier = 0;
  7626. res->EVSEMaxCurrent.Unit = iso1unitSymbolType_A;
  7627. //----- [BODY (4/10)] SAScheduleTupleID -----
  7628. res->SAScheduleTupleID = 0;
  7629. //----- [BODY (5/10)] EVSEID -----
  7630. res->EVSEID.charactersLen = 37;
  7631. memset(res->EVSEID.characters, 0, sizeof(res->EVSEID.characters));
  7632. //sprintf((char*)res->EVSEID.characters, CCS_AC_EVSEID);
  7633. //----- [BODY (6/10)] MeterInfo -----
  7634. res->MeterInfo_isUsed = 1u;
  7635. memset(res->MeterInfo.MeterID.characters, 0, sizeof(res->MeterInfo.MeterID.characters));
  7636. memset(res->MeterInfo.SigMeterReading.bytes, 0, sizeof(res->MeterInfo.SigMeterReading.bytes));
  7637. //[MeterInfo][1/5] MeterID
  7638. //sprintf((char*)res->MeterInfo.MeterID , CCS_AC_METER_ID);
  7639. //[MeterInfo][2/5] SigMeterReading (optional)
  7640. //sprintf((char*)res->MeterInfo.SigMeterReading , CCS_AC_SIG_METER_READING);
  7641. //[MeterInfo][3/5] MeterStatus (optional)
  7642. res->MeterInfo.MeterStatus = 0;
  7643. //[MeterInfo][4/5] MeterReading (optional)
  7644. res->MeterInfo.MeterReading = 12345;
  7645. //[MeterInfo][5/5] TMeter (optional)
  7646. res->MeterInfo.TMeter = 1586243587; //Unix Time Stamp format
  7647. //----- [BODY (7/10)] ReceiptRequired -----
  7648. res->ReceiptRequired_isUsed = 1u;
  7649. res->ReceiptRequired = FALSE; //optional
  7650. }
  7651. /**
  7652. *
  7653. * @param code
  7654. */
  7655. void Check_EVErrorCode(int code)
  7656. {
  7657. if (code != NO_ERROR) //NO_ERROR = 0
  7658. {
  7659. //Asking CSU to Stop
  7660. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7661. //Update_V2G_Status(Other_Fault);
  7662. DEBUG_INFO("Stop by EV (EVErrorCode = %d (DEC))\n", code);
  7663. }
  7664. }
  7665. /**
  7666. *
  7667. * @param AcceptFd
  7668. * @return
  7669. */
  7670. int Proc_din_CurrentDemandRes(int AcceptFd)
  7671. {
  7672. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  7673. //if it is not the same, the packet should be ignored.
  7674. //STEP 1: ============ Initialize ============
  7675. int errn = 0;
  7676. bitstream_t v2g_tx_stream;
  7677. static struct CurrentDemandResponse_DIN70121 *cur;
  7678. static struct ChargingInfoData *sys;
  7679. // int i = 0;
  7680. // static int EVSE_max_current;
  7681. // int tmp = 0;
  7682. size_t pos = 0;
  7683. v2g_tx_stream.pos = &pos;
  7684. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7685. v2g_tx_stream.data = v2gBuffer.tx;
  7686. cur = &ShmCcsData->V2GMessage_DIN70121.CurrentDemandResponse;
  7687. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7688. cur->ResponseCode = OK_DIN70121;
  7689. //EVSE Status Code
  7690. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  7691. //[HEADER] Check Req SessionID
  7692. if (Check_din_V2G_Rx_MSG_SessionID(&v2gObject.DIN) < 0)
  7693. {
  7694. cur->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  7695. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  7696. errn = -1;
  7697. }
  7698. //Check for SequenceError
  7699. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7700. {
  7701. cur->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  7702. DEBUG_ERROR("SequenceError => End_Process\n");
  7703. errn = -1;
  7704. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7705. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7706. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7707. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7708. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7709. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7710. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7711. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7712. }
  7713. //Check for negative EvBatteryMaxCurrent
  7714. if (sys->EvBatteryMaxCurrent < 0)
  7715. {
  7716. DEBUG_ERROR("EvBatteryMaxCurrent is negative(%.02f) => End_Process\n", sys->EvBatteryMaxCurrent);
  7717. cur->DC_EVSEStatus.EVSEStatusCode = dinresponseCodeType_FAILED_WrongChargeParameter; //16
  7718. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7719. errn = -1;
  7720. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7721. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  7722. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7723. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7724. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7725. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7726. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  7727. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  7728. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7729. }
  7730. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  7731. SAVE_PhysicalValueType_DIN70121(&cur->EVSEPresentCurrent, (int) (sys->PresentChargingCurrent * 10), A_DIN70121);
  7732. SAVE_PhysicalValueType_DIN70121(&cur->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  7733. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == DISABLE
  7734. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_DIN70121);
  7735. #endif
  7736. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_DIN70121);
  7737. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_DIN70121);
  7738. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  7739. if (sys->EvBatteryMaxVoltage <= 500)
  7740. {
  7741. SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumVoltageLimit, (int) (500 * 10), V_DIN70121); //500V
  7742. }
  7743. #endif
  7744. //Limit the EVTargetCurrent should be under EVSEMaximumCurrentLimit
  7745. if (sys->EvBatterytargetCurrent > sys->AvailableChargingCurrent)
  7746. {
  7747. sys->EvBatterytargetCurrent = sys->AvailableChargingCurrent;
  7748. //[To-Do] Limit is achieved flag
  7749. }
  7750. //Isolation Status
  7751. if (sys->IsolationStatus == GFD_Invalid) //0:invalid (on going)
  7752. {
  7753. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  7754. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7755. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7756. DEBUG_ERROR("IsolationStatus = %d\n", sys->IsolationStatus);
  7757. Update_V2G_Status(Other_Fault);
  7758. errn = -1;
  7759. }
  7760. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  7761. {
  7762. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  7763. }
  7764. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  7765. {
  7766. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  7767. }
  7768. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  7769. {
  7770. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  7771. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7772. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7773. DEBUG_ERROR("GFD_Fault => Emergency Shutdown\n");
  7774. Update_V2G_Status(Other_Fault);
  7775. errn = -1;
  7776. }
  7777. else //GFD_No_IMD or other unexpected status
  7778. {
  7779. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  7780. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7781. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7782. DEBUG_ERROR("IsolationStatus = %d(undefined)\n", sys->IsolationStatus);
  7783. Update_V2G_Status(Other_Fault);
  7784. errn = -1;
  7785. }
  7786. //For testing with Tesla Model 3
  7787. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == ENABLE
  7788. //[CAUTION][To-Do] Decresement of Current is not implemented.
  7789. //SAVE_PhysicalValueType_DIN70121(&cur->EVSEMaximumCurrentLimit, 10, A_DIN70121);
  7790. if (sys->EvBatterytargetCurrent <= 0)
  7791. {
  7792. EVSE_max_current = 50; //10A
  7793. cur->EVSEMaximumCurrentLimit.Value = 50; //10A
  7794. /*
  7795. DEBUG_PRINTF_EVCOMM_DETAIL("1PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  7796. sys->PresentChargingCurrent,
  7797. sys->EvBatterytargetCurrent,
  7798. EVSE_max_current,
  7799. cur->EVSEMaximumCurrentLimit.Value
  7800. );
  7801. */
  7802. }
  7803. else //1A
  7804. {
  7805. /*
  7806. DEBUG_PRINTF_EVCOMM_DETAIL("2PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  7807. sys->PresentChargingCurrent,
  7808. sys->EvBatterytargetCurrent,
  7809. EVSE_max_current,
  7810. cur->EVSEMaximumCurrentLimit.Value
  7811. );
  7812. */
  7813. if ((abs((int)sys->PresentChargingCurrent - (int)sys->EvBatterytargetCurrent) < 3)&&(abs((int)(EVSE_max_current/10) - (int)sys->EvBatterytargetCurrent) < 3))
  7814. {
  7815. tmp = EVSE_max_current + 50; //10A
  7816. if (tmp <= (int)(sys->AvailableChargingCurrent * 10))
  7817. {
  7818. cur->EVSEMaximumCurrentLimit.Value = (int)tmp;
  7819. EVSE_max_current = tmp;
  7820. }
  7821. else
  7822. {
  7823. cur->EVSEMaximumCurrentLimit.Value = (int) (sys->AvailableChargingCurrent * 10); //max is set to 40A
  7824. EVSE_max_current = (int) (sys->AvailableChargingCurrent * 10);
  7825. }
  7826. }
  7827. }
  7828. #endif
  7829. //Check for CSU command of "Stop by EVSE"
  7830. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  7831. {
  7832. //Check for Alarm Code: CCS GFD trip (012235)
  7833. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  7834. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  7835. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  7836. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  7837. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  7838. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  7839. {
  7840. DEBUG_ERROR("[DIN]CCS GFD trip => EVSE_Shutdown\n");
  7841. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7842. }
  7843. else
  7844. {
  7845. DEBUG_ERROR("[DIN]]EVSE_Shutdown\n");
  7846. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  7847. }
  7848. //cur->ResponseCode = FAILED_DIN70121;
  7849. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7850. //errn = -1;
  7851. }
  7852. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  7853. {
  7854. //Check for Alarm Code: CCS GFD trip (012235)
  7855. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  7856. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  7857. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  7858. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  7859. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  7860. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  7861. {
  7862. DEBUG_INFO("[DIN]]CCS GFD trip => EVSE_EmergencyShutdown\n");
  7863. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault;
  7864. }
  7865. else
  7866. {
  7867. DEBUG_ERROR("[DIN]EVSE_EmergencyShutdown\n");
  7868. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  7869. }
  7870. //cur->ResponseCode = FAILED_DIN70121;
  7871. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7872. //errn = -1;
  7873. }
  7874. else if (ShmInternalComm->ChargingPermission == FALSE)
  7875. {
  7876. DEBUG_ERROR("[DIN]Permission OFF\n");
  7877. //cur->ResponseCode = FAILED_DIN70121;
  7878. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  7879. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  7880. //errn = -1;
  7881. }
  7882. else
  7883. {
  7884. //null
  7885. }
  7886. //Response to CP Error
  7887. #if CP_PROTECTION_MECHANISM == ENABLE
  7888. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  7889. {
  7890. cur->ResponseCode = FAILED_DIN70121;
  7891. cur->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  7892. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7893. Update_V2G_Status(Other_Fault);
  7894. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7895. //CCS_SECC_CP_State_Error (023889)
  7896. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7897. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7898. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7899. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  7900. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  7901. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  7902. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  7903. DEBUG_INFO("Emergency Stop by CP Error (%d, %.02f V)\n", sys->CpState, sys->CpVoltage);
  7904. }
  7905. #endif
  7906. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  7907. #if PARAMETER_NORMAL_MODE == ENABLE
  7908. SHM_Read_din_CurrentDemandRes(&v2gObject.DIN, ShmCcsData);
  7909. #else
  7910. Sudo_Parameter_din_CurrentDemandRes();
  7911. #endif
  7912. //STEP 4: ============ Encode and Send Response Message ===========
  7913. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  7914. {
  7915. DEBUG_ERROR("Tx encoded msg error\n");
  7916. errn = -1;
  7917. }
  7918. //STEP 5: ============ Update Flags ===========
  7919. return errn;
  7920. }
  7921. /**
  7922. *
  7923. * @param AcceptFd
  7924. * @return
  7925. */
  7926. int Proc_iso1_CurrentDemandRes(int AcceptFd)
  7927. {
  7928. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  7929. //if it is not the same, the packet should be ignored.
  7930. //STEP 1: ============ Initialize ============
  7931. int errn = 0;
  7932. bitstream_t v2g_tx_stream;
  7933. static struct CurrentDemandResponse_ISO15118_2014 *cur;
  7934. static struct ChargingInfoData *sys;
  7935. // int i = 0;
  7936. // static int EVSE_max_current;
  7937. // int tmp = 0;
  7938. size_t pos = 0;
  7939. v2g_tx_stream.pos = &pos;
  7940. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  7941. v2g_tx_stream.data = v2gBuffer.tx;
  7942. cur = &ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandResponse;
  7943. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  7944. cur->ResponseCode = OK_ISO15118_2014;
  7945. //EVSE Status Code
  7946. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  7947. //[HEADER] Check Req SessionID
  7948. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  7949. {
  7950. cur->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  7951. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  7952. errn = -1;
  7953. }
  7954. //Check for SequenceError
  7955. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  7956. {
  7957. cur->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  7958. DEBUG_ERROR("SequenceError => End_Process\n");
  7959. errn = -1;
  7960. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7961. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  7962. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7963. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7964. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7965. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7966. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  7967. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  7968. }
  7969. //Check for negative EvBatteryMaxCurrent
  7970. if (sys->EvBatteryMaxCurrent < 0)
  7971. {
  7972. DEBUG_ERROR("EvBatteryMaxCurrent is negative(%.02f) => End_Process\n", sys->EvBatteryMaxCurrent);
  7973. cur->ResponseCode = iso1responseCodeType_FAILED_WrongChargeParameter; //16
  7974. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  7975. errn = -1;
  7976. //Update_ShmStatusCode(); //[To-Do] to be implemented
  7977. //CCS_SECC_ResponseCode_FAILED_WrongChargeParameter (023775)
  7978. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  7979. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  7980. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  7981. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  7982. ShmStatusCodeData->PresentStatusCode[0][4] = 7;
  7983. ShmStatusCodeData->PresentStatusCode[0][5] = 5;
  7984. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  7985. }
  7986. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  7987. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEPresentCurrent, (int) (sys->PresentChargingCurrent * 10), A_ISO15118_2014);
  7988. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  7989. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == DISABLE
  7990. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumCurrentLimit, (int) (sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  7991. #endif
  7992. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumPowerLimit, (int) (sys->AvailableChargingPower * 10), W_ISO15118_2014);
  7993. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumVoltageLimit, (int) (sys->MaximumChargingVoltage * 10), V_ISO15118_2014);
  7994. #if PATCH_FOR_BMW_I3_BUG_EVSEMAXIMUMVOLTAGELIMIT_599V == ENABLE
  7995. if (sys->EvBatteryMaxVoltage <= 500)
  7996. {
  7997. SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumVoltageLimit, (int) (500 * 10), V_ISO15118_2014); //500V
  7998. }
  7999. #endif
  8000. //Limit the EVTargetCurrent should be under EVSEMaximumCurrentLimit
  8001. if (sys->EvBatterytargetCurrent > sys->AvailableChargingCurrent)
  8002. {
  8003. sys->EvBatterytargetCurrent = sys->AvailableChargingCurrent;
  8004. //[To-Do] Limit is achieved flag
  8005. }
  8006. //Isolation Status
  8007. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  8008. {
  8009. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  8010. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8011. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8012. DEBUG_ERROR("IsolationStatus = %d\n", sys->IsolationStatus);
  8013. Update_V2G_Status(Other_Fault);
  8014. errn = -1;
  8015. }
  8016. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  8017. {
  8018. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  8019. }
  8020. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  8021. {
  8022. cur->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  8023. }
  8024. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  8025. {
  8026. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  8027. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8028. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8029. DEBUG_ERROR("GFD_Fault => EmergencyShutdown\n");
  8030. Update_V2G_Status(Other_Fault);
  8031. errn = -1;
  8032. }
  8033. else
  8034. {
  8035. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  8036. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8037. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8038. DEBUG_ERROR("IsolationStatus = %d(undefined)\n", sys->IsolationStatus);
  8039. Update_V2G_Status(Other_Fault);
  8040. errn = -1;
  8041. }
  8042. //For testing with Tesla Model 3
  8043. #if TESLA_SLOW_INCREASE_CURRENT_FUNCTION == ENABLE
  8044. //[CAUTION][To-Do] Decresement of Current is not implemented.
  8045. //SAVE_PhysicalValueType_ISO15118_2014(&cur->EVSEMaximumCurrentLimit, 10, A_ISO15118_2014);
  8046. if (sys->EvBatterytargetCurrent <= 0)
  8047. {
  8048. EVSE_max_current = 50; //10A
  8049. cur->EVSEMaximumCurrentLimit.Value = 50; //10A
  8050. /*
  8051. DEBUG_PRINTF_EVCOMM_DETAIL("1PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  8052. sys->PresentChargingCurrent,
  8053. sys->EvBatterytargetCurrent,
  8054. EVSE_max_current,
  8055. cur->EVSEMaximumCurrentLimit.Value
  8056. );
  8057. */
  8058. }
  8059. else //1A
  8060. {
  8061. /*
  8062. DEBUG_PRINTF_EVCOMM_DETAIL("2PresentChargingCurrent = %.02f, EvBatterytargetCurrent = %.02f, EVSE_max_current = %d, EVSEMaximumCurrentLimit = %d\n",
  8063. sys->PresentChargingCurrent,
  8064. sys->EvBatterytargetCurrent,
  8065. EVSE_max_current,
  8066. cur->EVSEMaximumCurrentLimit.Value
  8067. );
  8068. */
  8069. if ((abs((int)sys->PresentChargingCurrent - (int)sys->EvBatterytargetCurrent) < 3)&&(abs((int)(EVSE_max_current/10) - (int)sys->EvBatterytargetCurrent) < 3))
  8070. {
  8071. tmp = EVSE_max_current + 50; //10A
  8072. if (tmp <= (int)(sys->AvailableChargingCurrent * 10))
  8073. {
  8074. cur->EVSEMaximumCurrentLimit.Value = (int)tmp;
  8075. EVSE_max_current = tmp;
  8076. }
  8077. else
  8078. {
  8079. cur->EVSEMaximumCurrentLimit.Value = (int) (sys->AvailableChargingCurrent * 10); //max is set to 40A
  8080. EVSE_max_current = (int) (sys->AvailableChargingCurrent * 10);
  8081. }
  8082. }
  8083. }
  8084. #endif
  8085. //Check for CSU command of "Stop by EVSE"
  8086. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8087. {
  8088. //Check for Alarm Code: CCS GFD trip (012235)
  8089. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8090. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8091. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8092. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8093. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8094. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8095. {
  8096. DEBUG_ERROR("CCS GFD trip => EVSE_Shutdown\n");
  8097. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  8098. }
  8099. else
  8100. {
  8101. DEBUG_ERROR("EVSE_Shutdown\n");
  8102. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8103. }
  8104. //cur->ResponseCode = FAILED_ISO15118_2014;
  8105. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8106. //errn = -1;
  8107. }
  8108. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8109. {
  8110. //Check for Alarm Code: CCS GFD trip (012235)
  8111. if (ShmInternalComm->EVSEStopChargingReq.alarmcode[0] == 0 &&
  8112. ShmInternalComm->EVSEStopChargingReq.alarmcode[1] == 1 &&
  8113. ShmInternalComm->EVSEStopChargingReq.alarmcode[2] == 2 &&
  8114. ShmInternalComm->EVSEStopChargingReq.alarmcode[3] == 2 &&
  8115. ShmInternalComm->EVSEStopChargingReq.alarmcode[4] == 3 &&
  8116. ShmInternalComm->EVSEStopChargingReq.alarmcode[5] == 5)
  8117. {
  8118. DEBUG_ERROR("CCS GFD trip => EVSE_EmergencyShutdown\n");
  8119. cur->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault;
  8120. }
  8121. else
  8122. {
  8123. DEBUG_ERROR("EVSE_EmergencyShutdown\n");
  8124. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8125. }
  8126. //cur->ResponseCode = FAILED_ISO15118_2014;
  8127. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8128. //errn = -1;
  8129. }
  8130. else if (ShmInternalComm->ChargingPermission == FALSE)
  8131. {
  8132. DEBUG_ERROR("Permission OFF\n");
  8133. //cur->ResponseCode = FAILED_ISO15118_2014;
  8134. cur->DC_EVSEStatus.EVSEIsolationStatus = (unsigned char)sys->IsolationStatus;
  8135. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8136. //errn = -1;
  8137. }
  8138. else
  8139. {
  8140. //null
  8141. }
  8142. //Response to CP Error
  8143. #if CP_PROTECTION_MECHANISM == ENABLE
  8144. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  8145. {
  8146. cur->ResponseCode = FAILED_ISO15118_2014;
  8147. cur->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8148. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8149. Update_V2G_Status(Other_Fault);
  8150. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8151. //CCS_SECC_CP_State_Error (023889)
  8152. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8153. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8154. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8155. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  8156. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  8157. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  8158. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  8159. DEBUG_INFO("[CurrentDemand]Emergency Stop by CP Error (%d, %.02f V)\n", sys->CpState, sys->CpVoltage);
  8160. }
  8161. #endif
  8162. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8163. #if PARAMETER_NORMAL_MODE == ENABLE
  8164. SHM_Read_iso1_CurrentDemandRes(&v2gObject.ISO1, ShmCcsData);
  8165. #else
  8166. Sudo_Parameter_iso1_CurrentDemandRes();
  8167. #endif
  8168. //STEP 4: ============ Encode and Send Response Message ===========
  8169. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  8170. {
  8171. DEBUG_ERROR("Tx encoded msg error\n");
  8172. errn = -1;
  8173. }
  8174. //STEP 5: ============ Update Flags ===========
  8175. return errn;
  8176. }
  8177. /**
  8178. *
  8179. * @param AcceptFd
  8180. * @return
  8181. */
  8182. int Proc_iso1_ChargingStatusRes(int AcceptFd)
  8183. {
  8184. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  8185. //if it is not the same, the packet should be ignored.
  8186. //STEP 1: ============ Initialize ============
  8187. //int i = 0;
  8188. int errn = 0;
  8189. bitstream_t v2g_tx_stream;
  8190. static struct ChargingStatusResponse_ISO15118_2014 *res;
  8191. static struct ChargingInfoData *sys;
  8192. size_t pos = 0;
  8193. v2g_tx_stream.pos = &pos;
  8194. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8195. v2g_tx_stream.data = v2gBuffer.tx;
  8196. res = &ShmCcsData->V2GMessage_ISO15118_2014.ChargingStatusResponse;
  8197. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8198. //Init
  8199. res->ResponseCode = OK_ISO15118_2014;
  8200. res->ReceiptRequired = FALSE; //optional
  8201. res->SAScheduleTupleID = 0;
  8202. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  8203. res->AC_EVSEStatus.NotificationMaxDelay = 0;
  8204. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8205. // iso1EVSENotificationType_None = 0,
  8206. // iso1EVSENotificationType_StopCharging = 1,
  8207. // iso1EVSENotificationType_ReNegotiation = 2
  8208. //[HEADER] Check Req SessionID
  8209. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  8210. {
  8211. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  8212. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  8213. errn = -1;
  8214. }
  8215. //Check for SequenceError
  8216. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8217. {
  8218. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  8219. DEBUG_ERROR("SequenceError => End_Process\n");
  8220. errn = -1;
  8221. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8222. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8223. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8224. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8225. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8226. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8227. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8228. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8229. }
  8230. //EVSE ID
  8231. memset(res->EVSEID, 0, sizeof(res->EVSEID));
  8232. sprintf((char*)res->EVSEID, CCS_AC_EVSEID);
  8233. //[MeterInfo][0/5] init
  8234. //memset(res->MeterInfo.MeterID, 0, sizeof(res->MeterInfo.MeterID));
  8235. //memset(res->MeterInfo.SigMeterReading, 0, sizeof(res->MeterInfo.SigMeterReading));
  8236. //[MeterInfo][1/5] MeterID
  8237. //sprintf((char*)res->MeterInfo.MeterID, CCS_AC_METER_ID);
  8238. //[MeterInfo][2/5] SigMeterReading (optional)
  8239. //sprintf((char*)res->MeterInfo.SigMeterReading , CCS_AC_SIG_METER_READING);
  8240. //[MeterInfo][3/5] MeterStatus (optional)
  8241. res->MeterInfo.MeterStatus = 0;
  8242. //[MeterInfo][4/5] MeterReading (optional)
  8243. res->MeterInfo.MeterReading = 12345;
  8244. //[MeterInfo][5/5] TMeter (optional)
  8245. res->MeterInfo.TMeter = 1586243587; //Unix Time Stamp format
  8246. //STEP 2: ============ Modify Parameter of ShmCcsData ============
  8247. if(ShmCcsData->EnergyTransferMode == MODE_DC_EXTENDED)
  8248. {
  8249. SAVE_PhysicalValueType_ISO15118_2014(&res->EVSEMaxCurrent, (int)(sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  8250. //Isolation Status (RCD)
  8251. if (sys->IsolationStatus == 0) //Isolation is invalid
  8252. {
  8253. res->AC_EVSEStatus.RCD = TRUE; //FALSE(no error), TRUE(error is detected)
  8254. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  8255. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8256. DEBUG_ERROR("IsolationStatus: %d\n", sys->IsolationStatus);
  8257. Update_V2G_Status(Other_Fault);
  8258. errn = -1;
  8259. }
  8260. else if (sys->IsolationStatus == 1) //Isolation is valid
  8261. {
  8262. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  8263. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8264. }
  8265. else
  8266. {
  8267. res->AC_EVSEStatus.RCD = TRUE; //FALSE(no error), TRUE(error is detected)
  8268. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  8269. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8270. DEBUG_ERROR("IsolationStatus = %d(undefined)\n", sys->IsolationStatus);
  8271. Update_V2G_Status(Other_Fault);
  8272. errn = -1;
  8273. }
  8274. //Check for CSU command of "Stop by EVSE"
  8275. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8276. {
  8277. //res->ResponseCode = FAILED_ISO15118_2014;
  8278. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  8279. //errn = -1;
  8280. }
  8281. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8282. {
  8283. //res->ResponseCode = FAILED_ISO15118_2014;
  8284. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  8285. //errn = -1;
  8286. }
  8287. else if (ShmInternalComm->ChargingPermission == FALSE)
  8288. {
  8289. //res->ResponseCode = FAILED_ISO15118_2014;
  8290. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  8291. //errn = -1;
  8292. }
  8293. //Response to CP Error
  8294. #if CP_PROTECTION_MECHANISM == ENABLE
  8295. if ((sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  8296. {
  8297. res->ResponseCode = FAILED_ISO15118_2014;
  8298. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  8299. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8300. Update_V2G_Status(Other_Fault);
  8301. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8302. //CCS_SECC_CP_State_Error (023889)
  8303. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8304. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8305. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8306. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  8307. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  8308. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  8309. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  8310. DEBUG_INFO("Emergency Stop by CP Error (%d, %.02f V)\n", sys->CpState, sys->CpVoltage);
  8311. }
  8312. #endif
  8313. }
  8314. else
  8315. {
  8316. SAVE_PhysicalValueType_ISO15118_2014(&res->EVSEMaxCurrent, (int)(sys->AvailableChargingCurrent * 10), A_ISO15118_2014);
  8317. //Isolation Status (RCD)
  8318. res->AC_EVSEStatus.RCD = FALSE; //FALSE(no error), TRUE(error is detected)
  8319. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8320. //Check for CSU command of "Stop by EVSE"
  8321. if (ShmInternalComm->ChargingPermission == FALSE)
  8322. {
  8323. //res->ResponseCode = FAILED_ISO15118_2014;
  8324. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  8325. //errn = -1;
  8326. }
  8327. //Response to CP Error
  8328. #if CP_PROTECTION_MECHANISM == ENABLE
  8329. if((sys->CpState != 3) && (sys->CpState != 4) && (sys->CpState != 5)) //State C (6V), D (3V)
  8330. {
  8331. res->ResponseCode = FAILED_ISO15118_2014;
  8332. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_StopCharging;
  8333. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8334. Update_V2G_Status(Other_Fault);
  8335. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8336. //CCS_SECC_CP_State_Error (023889)
  8337. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8338. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8339. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8340. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  8341. ShmStatusCodeData->PresentStatusCode[0][4] = 8;
  8342. ShmStatusCodeData->PresentStatusCode[0][5] = 9;
  8343. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  8344. DEBUG_ERROR("Emergency Stop by CP Error (%d, %.02f V)\n", sys->CpState, sys->CpVoltage);
  8345. }
  8346. #endif
  8347. }
  8348. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8349. #if PARAMETER_NORMAL_MODE == ENABLE
  8350. SHM_Read_iso1_ChargingStatusRes(&v2gObject.ISO1, ShmCcsData);
  8351. #else
  8352. Sudo_Parameter_iso1_ChargingStatusRes();
  8353. #endif
  8354. //STEP 4: ============ Encode and Send Response Message ===========
  8355. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  8356. {
  8357. DEBUG_ERROR("Tx encoded msg error\n");
  8358. errn = -1;
  8359. }
  8360. //STEP 5: ============ Update Flags ===========
  8361. return errn;
  8362. }
  8363. /**
  8364. *
  8365. * @param AcceptFd
  8366. * @return
  8367. */
  8368. int Proc_din_CurrentDemandReq(int AcceptFd)
  8369. {
  8370. int errn = 0;
  8371. DEBUG_INFO("[V2G[RX]]CurrentDemandReq\n");
  8372. //Print the decoded XML Document
  8373. PRINT_XML_DOC_DIN_CurrentDemandReq(&v2gObject.DIN);
  8374. //Save into Share Memory
  8375. SHM_Save_din_CurrentDemandReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  8376. //Check for EV Error Code
  8377. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.CurrentDemandRequest.DC_EVStatus.EVErrorCode);
  8378. errn = Proc_din_CurrentDemandRes(AcceptFd);
  8379. if (errn == 0)
  8380. {
  8381. //Response is sent successfully.
  8382. DEBUG_INFO("[V2G][TX]CurrentDemandRes\n");
  8383. }
  8384. else
  8385. {
  8386. DEBUG_ERROR("Proc_din_CurrentDemandRes fail: (%d,DEC)\n", errn);
  8387. }
  8388. return errn;
  8389. }
  8390. /**
  8391. *
  8392. * @param AcceptFd
  8393. * @return
  8394. */
  8395. int Proc_iso1_CurrentDemandReq(int AcceptFd)
  8396. {
  8397. int errn = 0;
  8398. DEBUG_INFO("Request in.\n");
  8399. //Print the decoded XML Document
  8400. PRINT_XML_DOC_ISO1_CurrentDemandReq(&v2gObject.ISO1);
  8401. //Save into Share Memory
  8402. SHM_Save_iso1_CurrentDemandReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  8403. //Check for EV Error Code
  8404. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.CurrentDemandRequest.DC_EVStatus.EVErrorCode);
  8405. errn = Proc_iso1_CurrentDemandRes(AcceptFd);
  8406. if (errn == 0)
  8407. {
  8408. //Response is sent successfully.
  8409. DEBUG_INFO("Response out.\n");
  8410. }
  8411. else
  8412. {
  8413. DEBUG_ERROR("Proc_iso1_CurrentDemandRes fail: (%d,DEC)\n", errn);
  8414. }
  8415. return errn;
  8416. }
  8417. /**
  8418. *
  8419. * @param AcceptFd
  8420. * @return
  8421. */
  8422. int Proc_iso1_ChargingStatusReq(int AcceptFd)
  8423. {
  8424. int errn = 0;
  8425. DEBUG_INFO("Request in.\n");
  8426. //Print the decoded XML Document
  8427. PRINT_XML_DOC_ISO1_ChargingStatusReq(&v2gObject.ISO1);
  8428. //Save into Share Memory
  8429. SHM_Save_iso1_ChargingStatusReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  8430. //Check for EV Error Code
  8431. //no content in ISO1
  8432. errn = Proc_iso1_ChargingStatusRes(AcceptFd);
  8433. if (errn == 0)
  8434. {
  8435. //Response is sent successfully.
  8436. DEBUG_INFO("Response out.\n");
  8437. }
  8438. else
  8439. {
  8440. DEBUG_ERROR("Proc_iso1_ChargingStatusRes() fail: (%d,DEC)\n", errn);
  8441. }
  8442. return errn;
  8443. }
  8444. /**
  8445. *
  8446. * @param AcceptFd
  8447. * @return
  8448. */
  8449. int Proc_din_PowerDeliveryStopRes(int AcceptFd)
  8450. {
  8451. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  8452. //if it is not the same, the packet should be ignored.
  8453. //STEP 1: ============ Initialize ============
  8454. //int i = 0;
  8455. int errn = 0;
  8456. bitstream_t v2g_tx_stream;
  8457. struct dinPowerDeliveryResType *res;
  8458. struct ChargingInfoData *sys;
  8459. size_t pos = 0;
  8460. v2g_tx_stream.pos = &pos;
  8461. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8462. v2g_tx_stream.data = v2gBuffer.tx;
  8463. res = &v2gObject.DIN.V2G_Message.Body.PowerDeliveryRes;
  8464. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8465. res->ResponseCode = OK_DIN70121;
  8466. //[HEADER] Check Req SessionID
  8467. if (Check_din_V2G_Rx_MSG_SessionID(&v2gObject.DIN) < 0)
  8468. {
  8469. res->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  8470. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  8471. errn = -1;
  8472. }
  8473. //Check for SequenceError
  8474. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8475. {
  8476. res->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  8477. DEBUG_ERROR("SequenceError => End_Process\n");
  8478. errn = -1;
  8479. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8480. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8481. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8482. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8483. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8484. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8485. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8486. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8487. }
  8488. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8489. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8490. #if PARAMETER_NORMAL_MODE == ENABLE
  8491. ShmCcsData->V2GMessage_DIN70121.PowerDeliveryResponse.DC_EVSEStatus.EVSEStatusCode = EVSE_NotReady;
  8492. SHM_Read_din_PowerDeliveryRes(&v2gObject.DIN, ShmCcsData);
  8493. #else
  8494. Sudo_Parameter_din_PreChargeRes();
  8495. #endif
  8496. //EVSE Status Code
  8497. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8498. //Check for CSU command of "Stop by EVSE"
  8499. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8500. {
  8501. //res->ResponseCode = FAILED_DIN70121;
  8502. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8503. }
  8504. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8505. {
  8506. //res->ResponseCode = FAILED_DIN70121;
  8507. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8508. }
  8509. else if (ShmInternalComm->ChargingPermission == FALSE)
  8510. {
  8511. //res->ResponseCode = FAILED_DIN70121;
  8512. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8513. }
  8514. //STEP 4: ============ Encode and Send Response Message ===========
  8515. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  8516. {
  8517. DEBUG_ERROR("Tx encoded msg error\n");
  8518. errn = -1;
  8519. }
  8520. //STEP 5: ============ Update Flags ===========
  8521. return errn;
  8522. }
  8523. /**
  8524. *
  8525. * @param AcceptFd
  8526. * @return
  8527. */
  8528. int Proc_iso1_PowerDeliveryStopRes(int AcceptFd)
  8529. {
  8530. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  8531. //if it is not the same, the packet should be ignored.
  8532. //STEP 1: ============ Initialize ============
  8533. //int i = 0;
  8534. int errn = 0;
  8535. bitstream_t v2g_tx_stream;
  8536. struct iso1PowerDeliveryResType *res;
  8537. struct ChargingInfoData *sys;
  8538. size_t pos = 0;
  8539. v2g_tx_stream.pos = &pos;
  8540. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8541. v2g_tx_stream.data = v2gBuffer.tx;
  8542. res = &v2gObject.ISO1.V2G_Message.Body.PowerDeliveryRes;
  8543. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8544. res->ResponseCode = OK_ISO15118_2014;
  8545. //[HEADER] Check Req SessionID
  8546. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  8547. {
  8548. res->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  8549. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  8550. errn = -1;
  8551. }
  8552. //Check for SequenceError
  8553. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8554. {
  8555. res->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  8556. DEBUG_ERROR("SequenceError => End_Process\n");
  8557. errn = -1;
  8558. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8559. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8560. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8561. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8562. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8563. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8564. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8565. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8566. }
  8567. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8568. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8569. if(ShmCcsData->EnergyTransferMode == MODE_DC_EXTENDED)
  8570. {
  8571. #if PARAMETER_NORMAL_MODE == ENABLE
  8572. ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryResponse.DC_EVSEStatus.DC_EVSEStatusCode = EVSE_NotReady;
  8573. SHM_Read_iso1_PowerDeliveryRes(&v2gObject.ISO1, ShmCcsData);
  8574. #else
  8575. Sudo_Parameter_iso1_PreChargeRes();
  8576. #endif
  8577. //EVSE Status Code
  8578. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8579. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1 /*+++ 20200808, vern, Isolation Status should be valid during 2nd PowerDelivert ---*/
  8580. //Check for CSU command of "Stop by EVSE"
  8581. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8582. {
  8583. //res->ResponseCode = FAILED_ISO15118_2014;
  8584. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready; /*+++ 20200808, vern, Isolation Status should be valid during 2nd PowerDelivert ---*/
  8585. }
  8586. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8587. {
  8588. //res->ResponseCode = FAILED_ISO15118_2014;
  8589. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8590. }
  8591. else if (ShmInternalComm->ChargingPermission == FALSE)
  8592. {
  8593. //res->ResponseCode = FAILED_ISO15118_2014;
  8594. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready; /*+++ 20200808, vern, Isolation Status should be valid during 2nd PowerDelivert ---*/
  8595. }
  8596. }
  8597. else
  8598. {
  8599. #if PARAMETER_NORMAL_MODE == ENABLE
  8600. SHM_Read_iso1_PowerDeliveryRes(&v2gObject.ISO1, ShmCcsData);
  8601. #endif
  8602. //EVSE Status Code
  8603. res->AC_EVSEStatus.RCD = 0;
  8604. res->AC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8605. res->AC_EVSEStatus.NotificationMaxDelay = 10;
  8606. }
  8607. //STEP 4: ============ Encode and Send Response Message ===========
  8608. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  8609. {
  8610. DEBUG_ERROR("Tx encoded msg error\n");
  8611. errn = -1;
  8612. }
  8613. //STEP 5: ============ Update Flags ===========
  8614. return errn;
  8615. }
  8616. /**
  8617. *
  8618. * @param AcceptFd
  8619. * @return
  8620. */
  8621. int Proc_din_PowerDeliveryStopReq(int AcceptFd)
  8622. {
  8623. int errn = 0;
  8624. //Request CSU to STOP
  8625. //This should be reponsed as soon as possible once this message is received.
  8626. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8627. EVCOMM_SYS_INFO.EvBatterytargetVoltage = 0;
  8628. EVCOMM_SYS_INFO.EvBatterytargetCurrent = 0;
  8629. DEBUG_INFO("Request in.\n");
  8630. //Print the decoded XML Document
  8631. PRINT_XML_DOC_DIN_PowerDeliveryReq(&v2gObject.DIN);
  8632. //Save into Share Memory
  8633. SHM_Save_din_PowerDeliveryReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  8634. //Check for EV Error Code
  8635. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  8636. //[To-Do] Sending response after the EVSE output voltage decreases to under 60V
  8637. usleep(1500000); //1.5 seconds
  8638. //sleep(1); //1 second
  8639. errn = Proc_din_PowerDeliveryStopRes(AcceptFd);
  8640. if (errn == 0)
  8641. {
  8642. //send response successfully.
  8643. DEBUG_INFO("Response out.\n");
  8644. }
  8645. else
  8646. {
  8647. DEBUG_ERROR("Proc_din_PowerDeliveryStopRes() fail: %d (DEC)\n", errn);
  8648. }
  8649. return errn;
  8650. }
  8651. /**
  8652. *
  8653. * @param AcceptFd
  8654. * @return
  8655. */
  8656. int Proc_iso1_PowerDeliveryStopReq(int AcceptFd)
  8657. {
  8658. int errn = 0;
  8659. //Request CSU to STOP
  8660. //This should be reponsed as soon as possible once this message is received.
  8661. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8662. EVCOMM_SYS_INFO.EvBatterytargetVoltage = 0;
  8663. EVCOMM_SYS_INFO.EvBatterytargetCurrent = 0;
  8664. DEBUG_INFO("Request in.\n");
  8665. //Print the decoded XML Document
  8666. PRINT_XML_DOC_ISO1_PowerDeliveryReq(&v2gObject.ISO1);
  8667. //Save into Share Memory
  8668. SHM_Save_iso1_PowerDeliveryReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  8669. //Check for EV Error Code
  8670. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.PowerDeliveryRequest.DC_EVPowerDeliveryParameter.DC_EVStatus.EVErrorCode);
  8671. //[To-Do] Sending response after the EVSE output voltage decreases to under 60V
  8672. usleep(1500000); //1.5 seconds
  8673. //sleep(1); //1 second
  8674. errn = Proc_iso1_PowerDeliveryStopRes(AcceptFd);
  8675. if (errn == 0)
  8676. {
  8677. //send response successfully.
  8678. DEBUG_INFO("Response out.\n");
  8679. }
  8680. else
  8681. {
  8682. DEBUG_ERROR("Proc_iso1_PowerDeliveryStopRes fail(): %d (DEC)\n", errn);
  8683. }
  8684. return errn;
  8685. }
  8686. /**
  8687. *
  8688. * @param shm_ccs
  8689. */
  8690. void SHM_Init_din_WeldingDetectionRes(struct CcsData *shm_ccs)
  8691. {
  8692. struct WeldingDetectionResponse_DIN70121 *in;
  8693. in = &shm_ccs->V2GMessage_DIN70121.WeldingDetectionResponse;
  8694. //----- [BODY (1/3)] ResponseCode -----
  8695. in->ResponseCode = dinresponseCodeType_OK;
  8696. //----- [BODY (2/3)] EVSEPresentVoltage -----
  8697. short value = 0; int multiplier = 0; unsigned char unit = 0;
  8698. //value = 3820; multiplier = -1; unit = V_DIN70121; //382V, for test only.
  8699. value = 0; multiplier = 0; unit = V_DIN70121;
  8700. SHM_Save_dinPhysicalValueType(&in->EVSEPresentVoltage, value, multiplier, unit);
  8701. //----- [BODY (3/3)] DC_EVSEStatus -----
  8702. SHM_Init_dinDC_EVSEStatusType(&in->DC_EVSEStatus);
  8703. }
  8704. /**
  8705. *
  8706. */
  8707. void Sudo_Parameter_din_WeldingDetectionRes()
  8708. {
  8709. init_dinBodyType(&v2gObject.DIN.V2G_Message.Body);
  8710. init_dinWeldingDetectionResType(&v2gObject.DIN.V2G_Message.Body.WeldingDetectionRes);
  8711. v2gObject.DIN.V2G_Message.Body.WeldingDetectionRes_isUsed = 1u;
  8712. //----- [BODY (1/3)] ResponseCode -----
  8713. struct dinWeldingDetectionResType *res;
  8714. res = &v2gObject.DIN.V2G_Message.Body.WeldingDetectionRes;
  8715. res->ResponseCode = dinresponseCodeType_OK;
  8716. //----- [BODY (2/3)] EVSEPresentVoltage -----
  8717. res->EVSEPresentVoltage.Value = 3820;
  8718. res->EVSEPresentVoltage.Multiplier = -1;
  8719. res->EVSEPresentVoltage.Unit_isUsed = 1u;
  8720. res->EVSEPresentVoltage.Unit = dinunitSymbolType_V;
  8721. // dinunitSymbolType_h = 0,
  8722. // dinunitSymbolType_m = 1,
  8723. // dinunitSymbolType_s = 2,
  8724. // dinunitSymbolType_A = 3,
  8725. // dinunitSymbolType_Ah = 4,
  8726. // dinunitSymbolType_V = 5,
  8727. // dinunitSymbolType_VA = 6,
  8728. // dinunitSymbolType_W = 7,
  8729. // dinunitSymbolType_W_s = 8,
  8730. // dinunitSymbolType_Wh = 9
  8731. //----- [BODY (3/3)] DC_EVSEStatus -----
  8732. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8733. res->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid;
  8734. // dinisolationLevelType_Invalid = 0,
  8735. // dinisolationLevelType_Valid = 1,
  8736. // dinisolationLevelType_Warning = 2,
  8737. // dinisolationLevelType_Fault = 3
  8738. res->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8739. // dinDC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8740. // dinDC_EVSEStatusCodeType_EVSE_Ready = 1,
  8741. // dinDC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8742. // dinDC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8743. // dinDC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8744. // dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8745. // dinDC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8746. // dinDC_EVSEStatusCodeType_Reserved_8 = 7,
  8747. // dinDC_EVSEStatusCodeType_Reserved_9 = 8,
  8748. // dinDC_EVSEStatusCodeType_Reserved_A = 9,
  8749. // dinDC_EVSEStatusCodeType_Reserved_B = 10,
  8750. // dinDC_EVSEStatusCodeType_Reserved_C = 11
  8751. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8752. res->DC_EVSEStatus.EVSENotification = dinEVSENotificationType_None;
  8753. // dinEVSENotificationType_None = 0,
  8754. // dinEVSENotificationType_StopCharging = 1,
  8755. // dinEVSENotificationType_ReNegotiation = 2
  8756. }
  8757. /**
  8758. *
  8759. */
  8760. void Sudo_Parameter_iso1_WeldingDetectionRes()
  8761. {
  8762. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  8763. init_iso1WeldingDetectionResType(&v2gObject.ISO1.V2G_Message.Body.WeldingDetectionRes);
  8764. v2gObject.ISO1.V2G_Message.Body.WeldingDetectionRes_isUsed = 1u;
  8765. //----- [BODY (1/3)] ResponseCode -----
  8766. struct iso1WeldingDetectionResType *res;
  8767. res = &v2gObject.ISO1.V2G_Message.Body.WeldingDetectionRes;
  8768. res->ResponseCode = iso1responseCodeType_OK;
  8769. //----- [BODY (2/3)] EVSEPresentVoltage -----
  8770. res->EVSEPresentVoltage.Value = 3820;
  8771. res->EVSEPresentVoltage.Multiplier = -1;
  8772. //res->EVSEPresentVoltage.Unit_isUsed = 1u;
  8773. res->EVSEPresentVoltage.Unit = iso1unitSymbolType_V;
  8774. //iso1unitSymbolType_h = 0,
  8775. //iso1unitSymbolType_m = 1,
  8776. //iso1unitSymbolType_s = 2,
  8777. //iso1unitSymbolType_A = 3,
  8778. //iso1unitSymbolType_V = 4,
  8779. //iso1unitSymbolType_W = 5,
  8780. //iso1unitSymbolType_Wh = 6
  8781. //----- [BODY (3/3)] DC_EVSEStatus -----
  8782. res->DC_EVSEStatus.EVSEIsolationStatus_isUsed = 1u;
  8783. res->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid;
  8784. //iso1isolationLevelType_Invalid = 0,
  8785. //iso1isolationLevelType_Valid = 1,
  8786. //iso1isolationLevelType_Warning = 2,
  8787. //iso1isolationLevelType_Fault = 3,
  8788. //iso1isolationLevelType_No_IMD = 4
  8789. res->DC_EVSEStatus.EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8790. //iso1DC_EVSEStatusCodeType_EVSE_NotReady = 0,
  8791. //iso1DC_EVSEStatusCodeType_EVSE_Ready = 1,
  8792. //iso1DC_EVSEStatusCodeType_EVSE_Shutdown = 2,
  8793. //iso1DC_EVSEStatusCodeType_EVSE_UtilityInterruptEvent = 3,
  8794. //iso1DC_EVSEStatusCodeType_EVSE_IsolationMonitoringActive = 4,
  8795. //iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 5,
  8796. //iso1DC_EVSEStatusCodeType_EVSE_Malfunction = 6,
  8797. //iso1DC_EVSEStatusCodeType_Reserved_8 = 7,
  8798. //iso1DC_EVSEStatusCodeType_Reserved_9 = 8,
  8799. //iso1DC_EVSEStatusCodeType_Reserved_A = 9,
  8800. //iso1DC_EVSEStatusCodeType_Reserved_B = 10,
  8801. //iso1DC_EVSEStatusCodeType_Reserved_C = 11
  8802. res->DC_EVSEStatus.NotificationMaxDelay = 0u;
  8803. res->DC_EVSEStatus.EVSENotification = iso1EVSENotificationType_None;
  8804. //iso1EVSENotificationType_None = 0,
  8805. //iso1EVSENotificationType_StopCharging = 1,
  8806. //iso1EVSENotificationType_ReNegotiation = 2
  8807. }
  8808. /**
  8809. *
  8810. * @param AcceptFd
  8811. * @return
  8812. */
  8813. int Proc_din_WeldingDetectionRes(int AcceptFd)
  8814. {
  8815. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  8816. //if it is not the same, the packet should be ignored.
  8817. //STEP 1: ============ Initialize ============
  8818. //int i = 0;
  8819. int errn = 0;
  8820. bitstream_t v2g_tx_stream;
  8821. static struct WeldingDetectionResponse_DIN70121 *wel;
  8822. static struct ChargingInfoData *sys;
  8823. size_t pos = 0;
  8824. v2g_tx_stream.pos = &pos;
  8825. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8826. v2g_tx_stream.data = v2gBuffer.tx;
  8827. wel = &ShmCcsData->V2GMessage_DIN70121.WeldingDetectionResponse;
  8828. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8829. wel->ResponseCode = OK_DIN70121;
  8830. //[HEADER] Check Req SessionID
  8831. if (Check_din_V2G_Rx_MSG_SessionID(&v2gObject.DIN) < 0)
  8832. {
  8833. wel->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  8834. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  8835. errn = -1;
  8836. }
  8837. //Check for SequenceError
  8838. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8839. {
  8840. wel->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  8841. DEBUG_ERROR("SequenceError => End_Process\n");
  8842. errn = -1;
  8843. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8844. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8845. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8846. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8847. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8848. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8849. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8850. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8851. }
  8852. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8853. SAVE_PhysicalValueType_DIN70121(&wel->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_DIN70121);
  8854. //EVSE Status Code
  8855. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Ready;
  8856. //Check for CSU command of "Stop by EVSE"
  8857. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8858. {
  8859. //wel->ResponseCode = FAILED_DIN70121;
  8860. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8861. }
  8862. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8863. {
  8864. //wel->ResponseCode = FAILED_DIN70121;
  8865. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8866. }
  8867. else if (ShmInternalComm->ChargingPermission == FALSE)
  8868. {
  8869. //wel->ResponseCode = FAILED_DIN70121;
  8870. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_Shutdown;
  8871. }
  8872. //Isolation Status
  8873. if (sys->IsolationStatus == GFD_Invalid) //0:invalid (on going)
  8874. {
  8875. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0
  8876. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8877. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8878. DEBUG_ERROR("IsolationStatus = %d\n", sys->IsolationStatus);
  8879. Update_V2G_Status(Other_Fault);
  8880. errn = -1;
  8881. }
  8882. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  8883. {
  8884. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Valid; //1
  8885. }
  8886. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  8887. {
  8888. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  8889. }
  8890. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  8891. {
  8892. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  8893. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8894. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8895. DEBUG_ERROR("GFD_Fault => Emergency Shutdown\n");
  8896. Update_V2G_Status(Other_Fault);
  8897. errn = -1;
  8898. }
  8899. else //GFD_No_IMD or other unexpected status
  8900. {
  8901. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Fault; //3
  8902. wel->DC_EVSEStatus.EVSEStatusCode = dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8903. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8904. DEBUG_ERROR("IsolationStatus = %d(undefined)\n", sys->IsolationStatus);
  8905. Update_V2G_Status(Other_Fault);
  8906. errn = -1;
  8907. }
  8908. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  8909. #if PARAMETER_NORMAL_MODE == ENABLE
  8910. SHM_Read_din_WeldingDetectionRes(&v2gObject.DIN, ShmCcsData);
  8911. #else
  8912. Sudo_Parameter_din_WeldingDetectionRes();
  8913. #endif
  8914. //STEP 4: ============ Encode and Send Response Message ===========
  8915. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  8916. {
  8917. DEBUG_ERROR("Tx encoded msg error\n");
  8918. errn = -1;
  8919. }
  8920. //STEP 5: ============ Update Flags ===========
  8921. return errn;
  8922. }
  8923. /**
  8924. *
  8925. * @param AcceptFd
  8926. * @return
  8927. */
  8928. int Proc_iso1_WeldingDetectionRes(int AcceptFd)
  8929. {
  8930. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  8931. //if it is not the same, the packet should be ignored.
  8932. //STEP 1: ============ Initialize ============
  8933. //int i = 0;
  8934. int errn = 0;
  8935. bitstream_t v2g_tx_stream;
  8936. static struct WeldingDetectionResponse_ISO15118_2014 *wel;
  8937. static struct ChargingInfoData *sys;
  8938. size_t pos = 0;
  8939. v2g_tx_stream.pos = &pos;
  8940. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  8941. v2g_tx_stream.data = v2gBuffer.tx;
  8942. wel = &ShmCcsData->V2GMessage_ISO15118_2014.WeldingDetectionResponse;
  8943. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  8944. wel->ResponseCode = OK_ISO15118_2014;
  8945. //[HEADER] Check Req SessionID
  8946. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  8947. {
  8948. wel->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  8949. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  8950. errn = -1;
  8951. }
  8952. //Check for SequenceError
  8953. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  8954. {
  8955. wel->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  8956. DEBUG_ERROR("SequenceError => End_Process\n");
  8957. errn = -1;
  8958. //Update_ShmStatusCode(); //[To-Do] to be implemented
  8959. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  8960. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  8961. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  8962. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  8963. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  8964. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  8965. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  8966. }
  8967. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  8968. SAVE_PhysicalValueType_ISO15118_2014(&wel->EVSEPresentVoltage, (int) (sys->PresentChargingVoltage * 10), V_ISO15118_2014);
  8969. //EVSE Status Code
  8970. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Ready;
  8971. //Check for CSU command of "Stop by EVSE"
  8972. if (sys->DC_EVSEStatus == EVSE_Shutdown)
  8973. {
  8974. //wel->ResponseCode = FAILED_ISO15118_2014;
  8975. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8976. }
  8977. else if (sys->DC_EVSEStatus == EVSE_EmergencyShutdown)
  8978. {
  8979. //wel->ResponseCode = FAILED_ISO15118_2014;
  8980. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8981. }
  8982. else if (ShmInternalComm->ChargingPermission == FALSE)
  8983. {
  8984. //wel->ResponseCode = FAILED_ISO15118_2014;
  8985. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_Shutdown;
  8986. }
  8987. //Isolation Status
  8988. if (sys->IsolationStatus == GFD_Invalid) //0: invalid (on going)
  8989. {
  8990. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Invalid; //0
  8991. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  8992. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  8993. DEBUG_ERROR("IsolationStatus = %d\n", sys->IsolationStatus);
  8994. Update_V2G_Status(Other_Fault);
  8995. errn = -1;
  8996. }
  8997. else if (sys->IsolationStatus == GFD_Valid) //1: valid
  8998. {
  8999. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Valid; //1
  9000. }
  9001. else if (sys->IsolationStatus == GFD_Warning) //2: warning
  9002. {
  9003. wel->DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Warning; //2
  9004. }
  9005. else if (sys->IsolationStatus == GFD_Fault) //3: fault
  9006. {
  9007. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  9008. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9009. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9010. DEBUG_ERROR("GFD_Fault => EmergencyShutdown\n");
  9011. Update_V2G_Status(Other_Fault);
  9012. errn = -1;
  9013. }
  9014. else
  9015. {
  9016. wel->DC_EVSEStatus.EVSEIsolationStatus = iso1isolationLevelType_Fault; //3
  9017. wel->DC_EVSEStatus.DC_EVSEStatusCode = iso1DC_EVSEStatusCodeType_EVSE_EmergencyShutdown;
  9018. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  9019. DEBUG_ERROR("IsolationStatus = %d(undefined)\n", sys->IsolationStatus);
  9020. Update_V2G_Status(Other_Fault);
  9021. errn = -1;
  9022. }
  9023. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9024. #if PARAMETER_NORMAL_MODE == ENABLE
  9025. SHM_Read_iso1_WeldingDetectionRes(&v2gObject.ISO1, ShmCcsData);
  9026. #else
  9027. Sudo_Parameter_iso1_WeldingDetectionRes();
  9028. #endif
  9029. //STEP 4: ============ Encode and Send Response Message ===========
  9030. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  9031. {
  9032. DEBUG_ERROR("Tx encoded msg error\n");
  9033. errn = -1;
  9034. }
  9035. //STEP 5: ============ Update Flags ===========
  9036. return errn;
  9037. }
  9038. /**
  9039. *
  9040. * @param AcceptFd
  9041. * @return
  9042. */
  9043. int Proc_din_WeldingDetectionReq(int AcceptFd)
  9044. {
  9045. int errn = 0;
  9046. DEBUG_INFO("Request in.\n");
  9047. //Print the decoded XML Document
  9048. PRINT_XML_DOC_DIN_WeldingDetectionReq(&v2gObject.DIN);
  9049. //Save into Share Memory
  9050. SHM_Save_din_WeldingDetectionReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  9051. //Check for EV Error Code
  9052. Check_EVErrorCode(ShmCcsData->V2GMessage_DIN70121.WeldingDetectionRequest.DC_EVStatus.EVErrorCode);
  9053. errn = Proc_din_WeldingDetectionRes(AcceptFd);
  9054. if (errn == 0)
  9055. {
  9056. //send response successfully.
  9057. DEBUG_INFO("Response out.\n");
  9058. }
  9059. else
  9060. {
  9061. DEBUG_ERROR("Proc_din_WeldingDetectionRes() fail: %d (DEC)\n", errn);
  9062. }
  9063. return errn;
  9064. }
  9065. /**
  9066. *
  9067. * @param AcceptFd
  9068. * @return
  9069. */
  9070. int Proc_iso1_WeldingDetectionReq(int AcceptFd)
  9071. {
  9072. int errn = 0;
  9073. DEBUG_INFO("Request in.\n");
  9074. //Print the decoded XML Document
  9075. PRINT_XML_DOC_ISO1_WeldingDetectionReq(&v2gObject.ISO1);
  9076. //Save into Share Memory
  9077. SHM_Save_iso1_WeldingDetectionReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  9078. //Check for EV Error Code
  9079. Check_EVErrorCode(ShmCcsData->V2GMessage_ISO15118_2014.WeldingDetectionRequest.DC_EVStatus.EVErrorCode);
  9080. errn = Proc_iso1_WeldingDetectionRes(AcceptFd);
  9081. if (errn == 0)
  9082. {
  9083. //send response successfully.
  9084. DEBUG_INFO("Response out.\n");
  9085. }
  9086. else
  9087. {
  9088. DEBUG_ERROR("Proc_iso1_WeldingDetectionRes() fail: %d (DEC)", errn);
  9089. }
  9090. return errn;
  9091. }
  9092. /**
  9093. *
  9094. * @param shm_ccs
  9095. */
  9096. void SHM_Init_din_SessionStopRes(struct CcsData *shm_ccs)
  9097. {
  9098. struct SessionStopResponse_DIN70121 *in;
  9099. in = &shm_ccs->V2GMessage_DIN70121.SessionStopResponse;
  9100. //----- [BODY (1/3)] ResponseCode -----
  9101. in->ResponseCode = dinresponseCodeType_OK;
  9102. }
  9103. /**
  9104. *
  9105. */
  9106. void Sudo_Parameter_din_SessionStopRes()
  9107. {
  9108. init_dinBodyType(&v2gObject.DIN.V2G_Message.Body);
  9109. init_dinSessionStopResType(&v2gObject.DIN.V2G_Message.Body.SessionStopRes);
  9110. v2gObject.DIN.V2G_Message.Body.SessionStopRes_isUsed = 1u;
  9111. //----- [BODY (1/3)] ResponseCode -----
  9112. struct dinSessionStopResType *res;
  9113. res = &v2gObject.DIN.V2G_Message.Body.SessionStopRes;
  9114. res->ResponseCode = dinresponseCodeType_OK;
  9115. }
  9116. /**
  9117. *
  9118. */
  9119. void Sudo_Parameter_iso1_SessionStopRes()
  9120. {
  9121. init_iso1BodyType(&v2gObject.ISO1.V2G_Message.Body);
  9122. init_iso1SessionStopResType(&v2gObject.ISO1.V2G_Message.Body.SessionStopRes);
  9123. v2gObject.ISO1.V2G_Message.Body.SessionStopRes_isUsed = 1u;
  9124. //----- [BODY (1/3)] ResponseCode -----
  9125. struct iso1SessionStopResType *res;
  9126. res = &v2gObject.ISO1.V2G_Message.Body.SessionStopRes;
  9127. res->ResponseCode = iso1responseCodeType_OK;
  9128. }
  9129. /**
  9130. *
  9131. * @param AcceptFd
  9132. * @return
  9133. */
  9134. int Proc_din_SessionStopRes(int AcceptFd)
  9135. {
  9136. //[ To-Do] Check SessionID of ServiceDiscoveryReq,
  9137. //if it is not the same, the packet should be ignored.
  9138. //STEP 1: ============ Initialize ============
  9139. //int i = 0;
  9140. int errn = 0;
  9141. bitstream_t v2g_tx_stream;
  9142. struct SessionStopResponse_DIN70121 *stp;
  9143. size_t pos = 0;
  9144. v2g_tx_stream.pos = &pos;
  9145. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9146. v2g_tx_stream.data = v2gBuffer.tx;
  9147. stp = &ShmCcsData->V2GMessage_DIN70121.SessionStopResponse;
  9148. stp->ResponseCode = OK_DIN70121;
  9149. //[HEADER] Check Req SessionID
  9150. if (Check_din_V2G_Rx_MSG_SessionID(&v2gObject.DIN) < 0)
  9151. {
  9152. stp->ResponseCode = dinresponseCodeType_FAILED_UnknownSession; //6
  9153. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  9154. errn = -1;
  9155. }
  9156. //Check for SequenceError
  9157. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9158. {
  9159. stp->ResponseCode = dinresponseCodeType_FAILED_SequenceError; //5
  9160. DEBUG_ERROR("SequenceError => End_Process\n");
  9161. errn = -1;
  9162. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9163. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9164. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9165. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9166. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9167. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9168. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9169. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9170. }
  9171. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9172. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9173. #if PARAMETER_NORMAL_MODE == ENABLE
  9174. SHM_Read_din_SessionStopRes(&v2gObject.DIN, ShmCcsData);
  9175. #else
  9176. Sudo_Parameter_din_SessionStopRes();
  9177. #endif
  9178. //STEP 4: ============ Encode and Send Response Message ===========
  9179. if (send_encoded_din_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.DIN) != 0)
  9180. {
  9181. DEBUG_ERROR("Tx encoded msg error\n");
  9182. errn = -1;
  9183. }
  9184. //STEP 5: ============ Update Flags ===========
  9185. EVCOMM_SYS_INFO.ConnectorLocked = FALSE; //Indicating CSU that the Connector is unlocked.
  9186. //Once this is set, the CSU should
  9187. // =========== Annouce to CSU [To-Be Implemented]=============
  9188. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE; //This should be also added to EV Error events.
  9189. // =========== Re-initialized [To-Be Implemented]=============
  9190. //Keep 5% PWM for 2 seconds
  9191. for(int idx=3;idx>0;idx++)
  9192. {
  9193. DEBUG_INFO("PWM 5\%% countdown: %d\n", idx);
  9194. sleep(1);
  9195. }
  9196. OutputCpPwmDuty(100);
  9197. //[To-Do] Reset All Share memory
  9198. //[To-Do] CCS Module enter "idle" mode and CSU should check for this state.
  9199. //system("reboot -f");
  9200. //sleep(5);
  9201. //system("reboot -f");
  9202. return errn;
  9203. }
  9204. /**
  9205. *
  9206. * @param AcceptFd
  9207. * @return
  9208. */
  9209. int Proc_iso1_SessionStopRes(int AcceptFd)
  9210. {
  9211. //[To-Do] Check SessionID of ServiceDiscoveryReq,
  9212. //if it is not the same, the packet should be ignored.
  9213. //STEP 1: ============ Initialize ============
  9214. //int i = 0;
  9215. int errn = 0;
  9216. bitstream_t v2g_tx_stream;
  9217. struct SessionStopResponse_ISO15118_2014 *stp;
  9218. //struct ChargingInfoData *sys;
  9219. size_t pos = 0;
  9220. v2g_tx_stream.pos = &pos;
  9221. v2g_tx_stream.size = V2GTP_MSG_TX_BUFFER_SIZE; //(64*1024) //65,536 = 65.5KB
  9222. v2g_tx_stream.data = v2gBuffer.tx;
  9223. stp = &ShmCcsData->V2GMessage_ISO15118_2014.SessionStopResponse;
  9224. //sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  9225. stp->ResponseCode = OK_ISO15118_2014;
  9226. //[HEADER] Check Req SessionID
  9227. if (Check_iso1_V2G_Rx_MSG_SessionID(&v2gObject.ISO1) < 0)
  9228. {
  9229. stp->ResponseCode = iso1responseCodeType_FAILED_UnknownSession; //6
  9230. DEBUG_ERROR("unmatched SessionID => End_Process\n");
  9231. errn = -1;
  9232. }
  9233. //Check for SequenceError
  9234. if (EVCOMM_SYS_INFO.SequenceError == TRUE)
  9235. {
  9236. stp->ResponseCode = iso1responseCodeType_FAILED_SequenceError; //5
  9237. DEBUG_ERROR("SequenceError => End_Process\n");
  9238. errn = -1;
  9239. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9240. //CCS_SECC_ResponseCode_FAILED_SequenceError (023758)
  9241. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9242. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9243. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9244. ShmStatusCodeData->PresentStatusCode[0][3] = 7;
  9245. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9246. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  9247. }
  9248. //STEP 2: ============ Modifiy Parameter of ShmCcsData ============
  9249. //STEP 3: ============ Save Parameters from ShmCcsData to EXI Engine Buffer ============
  9250. #if PARAMETER_NORMAL_MODE == ENABLE
  9251. SHM_Read_iso1_SessionStopRes(&v2gObject.ISO1, ShmCcsData);
  9252. #else
  9253. Sudo_Parameter_iso1_SessionStopRes();
  9254. #endif
  9255. //STEP 4: ============ Encode and Send Response Message ===========
  9256. if (send_encoded_iso1_V2GTP_Stream(AcceptFd, &v2g_tx_stream, &v2gObject.ISO1) != 0)
  9257. {
  9258. DEBUG_ERROR("Tx encoded msg error\n");
  9259. errn = -1;
  9260. }
  9261. //STEP 5: ============ Update Flags ===========
  9262. EVCOMM_SYS_INFO.ConnectorLocked = FALSE; //Indicating CSU that the Connector is unlocked.
  9263. //Once this is set, the CSU should
  9264. // =========== Annouce to CSU [To-Be Implemented]=============
  9265. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE; //This should be also added to EV Error events.
  9266. // =========== Re-initialized [To-Be Implemented]=============
  9267. //Keep 5% PWM for 2 seconds
  9268. for(int idx=2;idx>0;idx--)
  9269. {
  9270. DEBUG_INFO("PWM 5\%% count down: %d\n", idx);
  9271. sleep(1);
  9272. }
  9273. OutputCpPwmDuty(100);
  9274. //[To-Do] Reset All Share memory
  9275. //[To-Do] CCS Module enter "idle" mode and CSU should check for this state.
  9276. //system("reboot -f");
  9277. //sleep(5);
  9278. //system("reboot -f");
  9279. return errn;
  9280. }
  9281. /**
  9282. *
  9283. * @param AcceptFd
  9284. * @return
  9285. */
  9286. int Proc_din_SessionStopReq(int AcceptFd)
  9287. {
  9288. int errn = 0;
  9289. DEBUG_INFO("Request in.\n");
  9290. //Print the decoded XML Document
  9291. PRINT_XML_DOC_DIN_SessionStopReq(&v2gObject.DIN);
  9292. //Save into Share Memory
  9293. SHM_Save_din_SessionStopReq(ShmCcsData, &v2gObject.DIN, ShmSysConfigAndInfo);
  9294. errn = Proc_din_SessionStopRes(AcceptFd);
  9295. if (errn == 0)
  9296. {
  9297. //send response successfully.
  9298. DEBUG_INFO("Response out.\n");
  9299. }
  9300. else
  9301. {
  9302. DEBUG_ERROR("Proc_din_SessionStopRes() fail: %d (DEC)", errn);
  9303. }
  9304. return errn;
  9305. }
  9306. /**
  9307. *
  9308. * @param AcceptFd
  9309. * @return
  9310. */
  9311. int Proc_iso1_SessionStopReq(int AcceptFd)
  9312. {
  9313. int errn = 0;
  9314. DEBUG_INFO("Request in.\n");
  9315. //Print the decoded XML Document
  9316. PRINT_XML_DOC_ISO1_SessionStopReq(&v2gObject.ISO1);
  9317. //Save into Share Memory
  9318. SHM_Save_iso1_SessionStopReq(ShmCcsData, &v2gObject.ISO1, ShmSysConfigAndInfo);
  9319. errn = Proc_iso1_SessionStopRes(AcceptFd);
  9320. if (errn == 0)
  9321. {
  9322. //send response successfully.
  9323. DEBUG_INFO("Response out.\n");
  9324. }
  9325. else
  9326. {
  9327. DEBUG_ERROR("Proc_iso1_SessionStopRes() fail: %d (DEC)\n", errn);
  9328. }
  9329. return errn;
  9330. }
  9331. /**
  9332. *
  9333. * @param AcceptFd
  9334. * @return
  9335. */
  9336. int V2gMsg_Process_din(int AcceptFd)
  9337. {
  9338. unsigned char req_is_responsed = FALSE;
  9339. while (req_is_responsed == FALSE)
  9340. {
  9341. //Check if it is in End_Process
  9342. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  9343. {
  9344. break;
  9345. }
  9346. switch(V2gFlowStatus)
  9347. {
  9348. //-------------------------------------------
  9349. case SupportedAppProtocolRequest:
  9350. {
  9351. if (v2gObject.appHandshake.supportedAppProtocolReq_isUsed == 1u)
  9352. {
  9353. v2gObject.appHandshake.supportedAppProtocolReq_isUsed = 0;
  9354. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  9355. {
  9356. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9357. Update_V2G_Status(SupportedAppProtocolResponse);
  9358. }
  9359. else
  9360. {
  9361. DEBUG_ERROR("supportedAppProtocolResReq: fail\n");
  9362. Update_V2G_Status(Other_Fault);
  9363. }
  9364. }
  9365. req_is_responsed = TRUE;
  9366. break;
  9367. }
  9368. case SupportedAppProtocolResponse:
  9369. {
  9370. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  9371. {
  9372. Update_V2G_Status(SessionSetupRequest);
  9373. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9374. }
  9375. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  9376. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  9377. {
  9378. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9379. EVCOMM_SYS_INFO.SequenceError = TRUE;
  9380. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9381. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9382. }
  9383. else
  9384. {
  9385. }
  9386. break;
  9387. }
  9388. //-------------------------------------------
  9389. case SessionSetupRequest: //19
  9390. {
  9391. if (Proc_din_SessionSetupReq(AcceptFd) == 0)
  9392. {
  9393. Update_V2G_Status(SessionSetupResponse);
  9394. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9395. }
  9396. else
  9397. {
  9398. DEBUG_ERROR("SessionSetupReqRes: fail\n");
  9399. Update_V2G_Status(Other_Fault);
  9400. }
  9401. req_is_responsed = TRUE;
  9402. break;
  9403. }
  9404. case SessionSetupResponse: //20
  9405. {
  9406. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  9407. {
  9408. Update_V2G_Status(ServiceDiscoveryRequest);
  9409. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9410. }
  9411. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  9412. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  9413. {
  9414. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9415. EVCOMM_SYS_INFO.SequenceError = TRUE;
  9416. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9417. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9418. }
  9419. else
  9420. {
  9421. }
  9422. break;
  9423. }
  9424. //-------------------------------------------
  9425. case ServiceDiscoveryRequest: //21
  9426. {
  9427. if (Proc_din_ServiceDiscoveryReq(AcceptFd) == 0)
  9428. {
  9429. Update_V2G_Status(ServiceDiscoveryResponse);
  9430. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9431. }
  9432. else
  9433. {
  9434. DEBUG_ERROR("ServiceDiscoveryReqRes: fail\n");
  9435. Update_V2G_Status(Other_Fault);
  9436. }
  9437. req_is_responsed = TRUE;
  9438. break;
  9439. }
  9440. case ServiceDiscoveryResponse: //22
  9441. {
  9442. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  9443. {
  9444. Update_V2G_Status(ServiceAndPaymentSelectionRequest);
  9445. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9446. }
  9447. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  9448. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  9449. {
  9450. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9451. EVCOMM_SYS_INFO.SequenceError = TRUE;
  9452. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9453. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9454. }
  9455. else
  9456. {
  9457. }
  9458. break;
  9459. }
  9460. //-------------------------------------------
  9461. case ServiceAndPaymentSelectionRequest: //25
  9462. {
  9463. if (Proc_din_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  9464. {
  9465. Update_V2G_Status(ServiceAndPaymentSelectionResponse);
  9466. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9467. }
  9468. else
  9469. {
  9470. DEBUG_ERROR("ServiceAndPaymentSelectionReqRes: fail\n");
  9471. Update_V2G_Status(Other_Fault);
  9472. }
  9473. req_is_responsed = TRUE;
  9474. break;
  9475. }
  9476. case ServiceAndPaymentSelectionResponse: //26
  9477. {
  9478. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  9479. {
  9480. Update_V2G_Status(AuthorizationRequest);
  9481. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9482. }
  9483. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  9484. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  9485. {
  9486. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9487. EVCOMM_SYS_INFO.SequenceError = TRUE;
  9488. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9489. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9490. }
  9491. else
  9492. {
  9493. }
  9494. break;
  9495. }
  9496. //-------------------------------------------
  9497. //case ContractAuthenticationReq:
  9498. case AuthorizationRequest: //29
  9499. {
  9500. if (Proc_din_ContractAuthenticationReq(AcceptFd) == 0)
  9501. {
  9502. Update_V2G_Status(AuthorizationResponse);
  9503. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9504. }
  9505. else
  9506. {
  9507. DEBUG_ERROR("AuthorizationReqRes: fail\n");
  9508. Update_V2G_Status(Other_Fault);
  9509. }
  9510. req_is_responsed = TRUE;
  9511. break;
  9512. }
  9513. case AuthorizationResponse: //30
  9514. {
  9515. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  9516. {
  9517. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9518. if (Proc_din_ContractAuthenticationReq(AcceptFd) == 0)
  9519. {
  9520. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9521. }
  9522. else
  9523. {
  9524. DEBUG_ERROR("AuthorizationReqRes: fail\n");
  9525. Update_V2G_Status(Other_Fault);
  9526. }
  9527. req_is_responsed = TRUE;
  9528. }
  9529. //Check for ChargeParameterDiscoveryReq
  9530. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  9531. {
  9532. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9533. Update_V2G_Status(ChargeParameterDiscoveryRequest);
  9534. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9535. ftime(&timerStart.SeqStart);
  9536. #endif
  9537. }
  9538. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  9539. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  9540. {
  9541. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9542. EVCOMM_SYS_INFO.SequenceError = TRUE;
  9543. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9544. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9545. }
  9546. else
  9547. {
  9548. }
  9549. break;
  9550. }
  9551. //-------------------------------------------
  9552. case ChargeParameterDiscoveryRequest: //35
  9553. {
  9554. if (Proc_din_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  9555. {
  9556. Update_V2G_Status(ChargeParameterDiscoveryResponse);
  9557. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9558. }
  9559. else
  9560. {
  9561. DEBUG_ERROR("ChargeParameterDiscoveryReqRes: fail\n");
  9562. Update_V2G_Status(Other_Fault);
  9563. }
  9564. req_is_responsed = TRUE;
  9565. break;
  9566. }
  9567. case ChargeParameterDiscoveryResponse:
  9568. {
  9569. //STEP 1: Check for Process Timeout
  9570. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9571. ftime(&timerStart.SeqEnd);
  9572. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  9573. {
  9574. DEBUG_ERROR("ChargingParameter Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_ChargingParameter_Performance_Time);
  9575. Update_V2G_Status(Sequence_Timeout);
  9576. break;
  9577. }
  9578. #endif
  9579. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  9580. {
  9581. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9582. if (Proc_din_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  9583. {
  9584. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9585. }
  9586. else
  9587. {
  9588. DEBUG_ERROR("ChargeParameterDiscoveryReqRes: fail\n");
  9589. Update_V2G_Status(Other_Fault);
  9590. }
  9591. req_is_responsed = TRUE;
  9592. break;
  9593. }
  9594. //STEP 2: Check for CableCheckReq message
  9595. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  9596. {
  9597. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9598. DEBUG_INFO("[V2G][RX]CableCheckReqReq: isolated?\n");
  9599. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  9600. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  9601. Update_V2G_Status(CableCheckRequest);
  9602. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9603. ftime(&timerStart.SeqStart);
  9604. #endif
  9605. }
  9606. //STEP 3: Wait for PowerDeliveryReq Message
  9607. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  9608. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  9609. {
  9610. Update_V2G_Status(PowerDeliveryRequestStop);
  9611. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9612. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  9613. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9614. ftime(&timerStart.SeqStart);
  9615. #endif
  9616. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  9617. {
  9618. Update_V2G_Status(PowerDeliveryResponseStop);
  9619. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9620. }
  9621. else
  9622. {
  9623. DEBUG_ERROR("PowerDeliveryStopReqRes: fail\n");
  9624. Update_V2G_Status(Other_Fault);
  9625. }
  9626. }
  9627. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  9628. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  9629. {
  9630. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9631. EVCOMM_SYS_INFO.SequenceError = TRUE;
  9632. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9633. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9634. }
  9635. else
  9636. {
  9637. }
  9638. break;
  9639. }
  9640. //-------------------------------------------
  9641. case CableCheckRequest: //37
  9642. {
  9643. //STEP 3: Execute Cable Check Process
  9644. if (Proc_din_CableCheckReq(AcceptFd) == 0)
  9645. {
  9646. Update_V2G_Status(CableCheckResponse);
  9647. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9648. }
  9649. else
  9650. {
  9651. DEBUG_ERROR("CableCheckReqRes: fail\n");
  9652. Update_V2G_Status(Other_Fault);
  9653. }
  9654. req_is_responsed = TRUE;
  9655. break;
  9656. }
  9657. case CableCheckResponse: //38
  9658. {
  9659. //STEP 1: Check for Process Timeout
  9660. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9661. ftime(&timerStart.SeqEnd);
  9662. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  9663. {
  9664. //DEBUG_INFO("[CableCheck] end counting...\n");
  9665. DEBUG_ERROR("CableCheck Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_CableCheck_Performance_Time);
  9666. Update_V2G_Status(Sequence_Timeout);
  9667. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9668. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  9669. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9670. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9671. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9672. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  9673. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  9674. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  9675. break;
  9676. }
  9677. #endif
  9678. //STEP 2: Check for CableCheckReq message
  9679. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  9680. {
  9681. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9682. //STEP 3: Execute Cable Check Process
  9683. if (Proc_din_CableCheckReq(AcceptFd) == 0)
  9684. {
  9685. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9686. }
  9687. else
  9688. {
  9689. DEBUG_ERROR("CableCheckReqRes: fail\n");
  9690. Update_V2G_Status(Other_Fault);
  9691. }
  9692. req_is_responsed = TRUE;
  9693. }
  9694. //STEP 3: Check for PreChargeReq message
  9695. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  9696. {
  9697. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9698. Update_V2G_Status(PreChargeRequest);
  9699. DEBUG_INFO("[V2G][RX]PreChargeReqReq: waiting for pre-charge voltage...\n");
  9700. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  9701. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  9702. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9703. ftime(&timerStart.SeqStart);
  9704. #endif
  9705. }
  9706. //STEP 3: Wait for PowerDeliveryReq Message
  9707. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  9708. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  9709. {
  9710. Update_V2G_Status(PowerDeliveryRequestStop);
  9711. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9712. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  9713. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9714. ftime(&timerStart.SeqStart);
  9715. #endif
  9716. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  9717. {
  9718. Update_V2G_Status(PowerDeliveryResponseStop);
  9719. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9720. }
  9721. else
  9722. {
  9723. DEBUG_ERROR("PowerDeliveryStopReqRes: fail\n");
  9724. Update_V2G_Status(Other_Fault);
  9725. }
  9726. }
  9727. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  9728. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  9729. {
  9730. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9731. EVCOMM_SYS_INFO.SequenceError = TRUE;
  9732. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9733. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9734. }
  9735. else
  9736. {
  9737. }
  9738. break;
  9739. }
  9740. //-------------------------------------------
  9741. case PreChargeRequest: //39
  9742. {
  9743. if (Proc_din_PreChargeReq(AcceptFd) == 0)
  9744. {
  9745. Update_V2G_Status(PreChargeResponse);
  9746. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9747. }
  9748. else
  9749. {
  9750. DEBUG_ERROR("PreChargeReqRes: fail\n");
  9751. Update_V2G_Status(Other_Fault);
  9752. }
  9753. req_is_responsed = TRUE;
  9754. break;
  9755. }
  9756. case PreChargeResponse: //40
  9757. {
  9758. //STEP 1: Check for Process Timeout
  9759. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9760. ftime(&timerStart.SeqEnd);
  9761. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  9762. {
  9763. DEBUG_ERROR("Precharge Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_PreCharge_Performance_Time);
  9764. Update_V2G_Status(Sequence_Timeout);
  9765. //Update_ShmStatusCode(); //[To-Do] to be implemented
  9766. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  9767. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  9768. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  9769. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  9770. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  9771. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  9772. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  9773. break;
  9774. }
  9775. #endif
  9776. //STEP 2: Check for PreChargeReq message
  9777. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  9778. {
  9779. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9780. if (Proc_din_PreChargeReq(AcceptFd) == 0)
  9781. {
  9782. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9783. }
  9784. else
  9785. {
  9786. DEBUG_ERROR("PreChargeReqRes: fail\n");
  9787. Update_V2G_Status(Other_Fault);
  9788. }
  9789. req_is_responsed = TRUE;
  9790. }
  9791. //STEP 3: Check for PowerDeliveryReq message
  9792. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  9793. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  9794. {
  9795. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9796. Update_V2G_Status(PowerDeliveryRequestStart);
  9797. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  9798. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9799. ftime(&timerStart.SeqStart);
  9800. #endif
  9801. }
  9802. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  9803. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  9804. {
  9805. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9806. EVCOMM_SYS_INFO.SequenceError = TRUE;
  9807. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9808. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9809. }
  9810. else
  9811. {
  9812. }
  9813. break;
  9814. }
  9815. //-------------------------------------------
  9816. case PowerDeliveryRequestStart: //41
  9817. {
  9818. if (Proc_din_PowerDeliveryStartReq(AcceptFd) == 0)
  9819. {
  9820. Update_V2G_Status(PowerDeliveryResponsetStart);
  9821. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9822. }
  9823. else
  9824. {
  9825. DEBUG_ERROR("PowerDeliveryStartReqRes: fail\n");
  9826. Update_V2G_Status(Other_Fault);
  9827. }
  9828. req_is_responsed = TRUE;
  9829. break;
  9830. }
  9831. case PowerDeliveryResponsetStart: //42
  9832. {
  9833. //STEP 1: Check for Process Timeout
  9834. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9835. if (v2gObject.DIN.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  9836. {
  9837. ftime(&timerStart.SeqEnd);
  9838. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > 2000) //5 seconds
  9839. {
  9840. DEBUG_ERROR("Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), 2000);
  9841. Update_V2G_Status(Sequence_Timeout);
  9842. }
  9843. break;
  9844. }
  9845. #endif
  9846. //STEP 2: Wait for CurrentDemandReq Message
  9847. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  9848. {
  9849. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9850. Update_V2G_Status(CurrentDemandRequest);
  9851. DEBUG_INFO("[V2G][RX]CurrentDemandReqReq: energy transfering...\n");
  9852. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  9853. }
  9854. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  9855. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  9856. {
  9857. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9858. EVCOMM_SYS_INFO.SequenceError = TRUE;
  9859. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9860. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9861. }
  9862. else
  9863. {
  9864. }
  9865. break;
  9866. }
  9867. //-------------------------------------------
  9868. case CurrentDemandRequest: //45,
  9869. {
  9870. if (Proc_din_CurrentDemandReq(AcceptFd) == 0)
  9871. {
  9872. Update_V2G_Status(CurrentDemandResponse);
  9873. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9874. }
  9875. else
  9876. {
  9877. DEBUG_ERROR("CurrentDemandReqRes: fail\n");
  9878. Update_V2G_Status(Other_Fault);
  9879. }
  9880. req_is_responsed = TRUE;
  9881. break;
  9882. }
  9883. case CurrentDemandResponse: //46,
  9884. {
  9885. //STEP 1: Wait for CurrentDemandReq Message
  9886. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  9887. {
  9888. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9889. if (Proc_din_CurrentDemandReq(AcceptFd) == 0)
  9890. {
  9891. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9892. }
  9893. else
  9894. {
  9895. DEBUG_ERROR("CurrentDemandReqRes: fail\n");
  9896. Update_V2G_Status(Other_Fault);
  9897. }
  9898. req_is_responsed = TRUE;
  9899. break;
  9900. }
  9901. //STEP 2: Wait for PowerDeliveryReq Message
  9902. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  9903. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  9904. {
  9905. Update_V2G_Status(PowerDeliveryRequestStop);
  9906. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9907. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  9908. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9909. ftime(&timerStart.SeqStart);
  9910. #endif
  9911. }
  9912. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  9913. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  9914. {
  9915. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9916. EVCOMM_SYS_INFO.SequenceError = TRUE;
  9917. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9918. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9919. }
  9920. else
  9921. {
  9922. }
  9923. break;
  9924. }
  9925. //-------------------------------------------
  9926. case PowerDeliveryRequestStop: //49,
  9927. {
  9928. if (Proc_din_PowerDeliveryStopReq(AcceptFd) == 0)
  9929. {
  9930. Update_V2G_Status(PowerDeliveryResponseStop);
  9931. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9932. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9933. ftime(&timerStart.SeqStart);
  9934. #endif
  9935. }
  9936. else
  9937. {
  9938. DEBUG_ERROR("PowerDeliveryStopReqRes: fail\n");
  9939. Update_V2G_Status(Other_Fault);
  9940. }
  9941. req_is_responsed = TRUE;
  9942. break;
  9943. }
  9944. case PowerDeliveryResponseStop: //50,
  9945. {
  9946. //STEP 1: Check for Process Timeout
  9947. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9948. ftime(&timerStart.SeqEnd);
  9949. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > 2000) //2 seconds
  9950. {
  9951. DEBUG_ERROR("Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), 2000);
  9952. Update_V2G_Status(Sequence_Timeout);
  9953. break;
  9954. }
  9955. #endif
  9956. //STEP 2: Check for WeldingDetectionReq Message
  9957. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  9958. {
  9959. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9960. Update_V2G_Status(WeldingDetectionRequest);
  9961. DEBUG_INFO("[V2G][RX]WeldingDetectionReq: ongoing...\n");
  9962. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  9963. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  9964. ftime(&timerStart.SeqStart);
  9965. #endif
  9966. }
  9967. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  9968. {
  9969. Update_V2G_Status(SessionStopRequest);
  9970. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9971. if (Proc_din_SessionStopReq(AcceptFd) == 0)
  9972. {
  9973. Update_V2G_Status(SessionStopResponse);
  9974. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  9975. }
  9976. else
  9977. {
  9978. DEBUG_ERROR("SessionStopReqRes: fail\n");
  9979. Update_V2G_Status(Other_Fault);
  9980. }
  9981. break;
  9982. }
  9983. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  9984. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  9985. {
  9986. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9987. EVCOMM_SYS_INFO.SequenceError = TRUE;
  9988. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  9989. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  9990. }
  9991. else
  9992. {
  9993. }
  9994. break;
  9995. }
  9996. //-------------------------------------------
  9997. case WeldingDetectionRequest: //51,
  9998. {
  9999. if (Proc_din_WeldingDetectionReq(AcceptFd) == 0)
  10000. {
  10001. Update_V2G_Status(WeldingDetectionResponse);
  10002. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10003. }
  10004. else
  10005. {
  10006. DEBUG_ERROR("WeldingDetectionReqRes: fail\n");
  10007. Update_V2G_Status(Other_Fault);
  10008. }
  10009. req_is_responsed = TRUE;
  10010. break;
  10011. }
  10012. case WeldingDetectionResponse: //52,
  10013. {
  10014. //STEP 1: Check for Process Timeout
  10015. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10016. ftime(&timerStart.SeqEnd);
  10017. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  10018. {
  10019. DEBUG_ERROR("Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_WeldingDetection_Performance_Time);
  10020. Update_V2G_Status(Sequence_Timeout);
  10021. break;
  10022. }
  10023. #endif
  10024. //STEP 2: Check for WeldingDetectionReq Message
  10025. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  10026. {
  10027. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10028. if (Proc_din_WeldingDetectionReq(AcceptFd) == 0)
  10029. {
  10030. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10031. }
  10032. else
  10033. {
  10034. DEBUG_ERROR("WeldingDetectionReqRes: fail\n");
  10035. Update_V2G_Status(Other_Fault);
  10036. }
  10037. req_is_responsed = TRUE;
  10038. }
  10039. //STEP 3: Check for SessionStopReq Message
  10040. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  10041. {
  10042. Update_V2G_Status(SessionStopRequest);
  10043. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10044. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  10045. }
  10046. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10047. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10048. {
  10049. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10050. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10051. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10052. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10053. }
  10054. else
  10055. {
  10056. }
  10057. break;
  10058. }
  10059. //-------------------------------------------
  10060. case SessionStopRequest: //53,
  10061. {
  10062. if (Proc_din_SessionStopReq(AcceptFd) == 0)
  10063. {
  10064. Update_V2G_Status(SessionStopResponse);
  10065. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10066. }
  10067. else
  10068. {
  10069. DEBUG_ERROR("SessionStopReqRes: fail\n");
  10070. Update_V2G_Status(Other_Fault);
  10071. }
  10072. req_is_responsed = TRUE;
  10073. break;
  10074. }
  10075. case SessionStopResponse: //54,
  10076. {
  10077. break;
  10078. }
  10079. //-------------------------------------------
  10080. default:
  10081. {
  10082. break;
  10083. }
  10084. }
  10085. usleep(1000);
  10086. }
  10087. return 0;
  10088. }
  10089. /**
  10090. *
  10091. * @param AcceptFd
  10092. * @return
  10093. */
  10094. int V2gMsg_Process_iso1_DC(int AcceptFd)
  10095. {
  10096. unsigned char req_is_responsed = FALSE;
  10097. while (req_is_responsed == FALSE)
  10098. {
  10099. //Check if it is in End_Process
  10100. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  10101. {
  10102. break;
  10103. }
  10104. switch(V2gFlowStatus)
  10105. {
  10106. //-------------------------------------------
  10107. case SupportedAppProtocolRequest:
  10108. {
  10109. if (v2gObject.appHandshake.supportedAppProtocolReq_isUsed == 1u)
  10110. {
  10111. v2gObject.appHandshake.supportedAppProtocolReq_isUsed = 0;
  10112. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  10113. {
  10114. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10115. Update_V2G_Status(SupportedAppProtocolResponse);
  10116. }
  10117. else
  10118. {
  10119. DEBUG_ERROR("supportedAppProtocolResReq: fail\n");
  10120. Update_V2G_Status(Other_Fault);
  10121. }
  10122. }
  10123. req_is_responsed = TRUE;
  10124. break;
  10125. }
  10126. case SupportedAppProtocolResponse:
  10127. {
  10128. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  10129. {
  10130. Update_V2G_Status(SessionSetupRequest);
  10131. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10132. }
  10133. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10134. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10135. {
  10136. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10137. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10138. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10139. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10140. }
  10141. else
  10142. {
  10143. }
  10144. break;
  10145. }
  10146. //-------------------------------------------
  10147. case SessionSetupRequest: //19
  10148. {
  10149. if (Proc_iso1_SessionSetupReq(AcceptFd) == 0)
  10150. {
  10151. Update_V2G_Status(SessionSetupResponse);
  10152. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10153. }
  10154. else
  10155. {
  10156. DEBUG_ERROR("SessionSetupReqRes: fail\n");
  10157. Update_V2G_Status(Other_Fault);
  10158. }
  10159. req_is_responsed = TRUE;
  10160. break;
  10161. }
  10162. case SessionSetupResponse: //20
  10163. {
  10164. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  10165. {
  10166. Update_V2G_Status(ServiceDiscoveryRequest);
  10167. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10168. }
  10169. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10170. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10171. {
  10172. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10173. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10174. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10175. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10176. }
  10177. else
  10178. {
  10179. }
  10180. break;
  10181. }
  10182. //-------------------------------------------
  10183. case ServiceDiscoveryRequest: //21
  10184. {
  10185. if (Proc_iso1_ServiceDiscoveryReq(AcceptFd) == 0)
  10186. {
  10187. Update_V2G_Status(ServiceDiscoveryResponse);
  10188. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10189. }
  10190. else
  10191. {
  10192. DEBUG_ERROR("ServiceDiscoveryReqRes: fail\n");
  10193. Update_V2G_Status(Other_Fault);
  10194. }
  10195. req_is_responsed = TRUE;
  10196. break;
  10197. }
  10198. case ServiceDiscoveryResponse: //22
  10199. {
  10200. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  10201. {
  10202. Update_V2G_Status(ServiceAndPaymentSelectionRequest);
  10203. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10204. }
  10205. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10206. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10207. {
  10208. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10209. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10210. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10211. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10212. }
  10213. else
  10214. {
  10215. }
  10216. break;
  10217. }
  10218. //-------------------------------------------
  10219. case ServiceAndPaymentSelectionRequest: //25
  10220. {
  10221. if (Proc_iso1_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  10222. {
  10223. Update_V2G_Status(ServiceAndPaymentSelectionResponse);
  10224. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10225. }
  10226. else
  10227. {
  10228. DEBUG_ERROR("ServiceAndPaymentSelectionReqRes: fail\n");
  10229. Update_V2G_Status(Other_Fault);
  10230. }
  10231. req_is_responsed = TRUE;
  10232. break;
  10233. }
  10234. case ServiceAndPaymentSelectionResponse: //26
  10235. {
  10236. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  10237. {
  10238. Update_V2G_Status(AuthorizationRequest);
  10239. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10240. }
  10241. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10242. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10243. {
  10244. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10245. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10246. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10247. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10248. }
  10249. else
  10250. {
  10251. }
  10252. break;
  10253. }
  10254. //-------------------------------------------
  10255. //case ContractAuthenticationReq:
  10256. case AuthorizationRequest: //29
  10257. {
  10258. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  10259. {
  10260. Update_V2G_Status(AuthorizationResponse);
  10261. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10262. }
  10263. else
  10264. {
  10265. DEBUG_ERROR("AuthorizationReqRes: fail\n");
  10266. Update_V2G_Status(Other_Fault);
  10267. }
  10268. req_is_responsed = TRUE;
  10269. break;
  10270. }
  10271. case AuthorizationResponse: //30
  10272. {
  10273. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  10274. {
  10275. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10276. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  10277. {
  10278. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10279. }
  10280. else
  10281. {
  10282. DEBUG_ERROR("AuthorizationReqRes: fail\n");
  10283. Update_V2G_Status(Other_Fault);
  10284. }
  10285. req_is_responsed = TRUE;
  10286. }
  10287. //Check for ChargeParameterDiscoveryReq
  10288. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  10289. {
  10290. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10291. Update_V2G_Status(ChargeParameterDiscoveryRequest);
  10292. DEBUG_INFO("[V2G][RX]ChargeParameterDiscoveryReq: CSU Permission?\n");
  10293. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10294. ftime(&timerStart.SeqStart);
  10295. #endif
  10296. }
  10297. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10298. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10299. {
  10300. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10301. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10302. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10303. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10304. }
  10305. else
  10306. {
  10307. }
  10308. break;
  10309. }
  10310. //-------------------------------------------
  10311. case ChargeParameterDiscoveryRequest: //35
  10312. {
  10313. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  10314. {
  10315. Update_V2G_Status(ChargeParameterDiscoveryResponse);
  10316. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10317. }
  10318. else
  10319. {
  10320. DEBUG_ERROR("ChargeParameterDiscoveryReqRes: fail\n");
  10321. Update_V2G_Status(Other_Fault);
  10322. }
  10323. req_is_responsed = TRUE;
  10324. break;
  10325. }
  10326. case ChargeParameterDiscoveryResponse:
  10327. {
  10328. //STEP 1: Check for Process Timeout
  10329. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10330. ftime(&timerStart.SeqEnd);
  10331. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  10332. {
  10333. DEBUG_ERROR("ChargingParameter Timeout - (%.02lf of %d ms)\n",
  10334. DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd),
  10335. V2G_SECC_ChargingParameter_Performance_Time);
  10336. Update_V2G_Status(Sequence_Timeout);
  10337. break;
  10338. }
  10339. #endif
  10340. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  10341. {
  10342. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10343. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  10344. {
  10345. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10346. }
  10347. else
  10348. {
  10349. DEBUG_ERROR("ChargeParameterDiscoveryReqRes: fail\n");
  10350. Update_V2G_Status(Other_Fault);
  10351. }
  10352. req_is_responsed = TRUE;
  10353. }
  10354. //STEP 2: Check for CableCheckReq message
  10355. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  10356. {
  10357. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10358. DEBUG_INFO("[V2G][RX]CableCheckReqReq: isolated?\n");
  10359. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  10360. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  10361. Update_V2G_Status(CableCheckRequest);
  10362. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10363. ftime(&timerStart.SeqStart);
  10364. #endif
  10365. }
  10366. //STEP 3: Wait for PowerDeliveryReq Message
  10367. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  10368. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  10369. {
  10370. Update_V2G_Status(PowerDeliveryRequestStop);
  10371. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10372. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  10373. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10374. ftime(&timerStart.SeqStart);
  10375. #endif
  10376. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  10377. {
  10378. Update_V2G_Status(PowerDeliveryResponseStop);
  10379. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10380. }
  10381. else
  10382. {
  10383. DEBUG_ERROR("PowerDeliveryStopReqRes: fail\n");
  10384. Update_V2G_Status(Other_Fault);
  10385. }
  10386. break;
  10387. }
  10388. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10389. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10390. {
  10391. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10392. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10393. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10394. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10395. }
  10396. else
  10397. {
  10398. }
  10399. break;
  10400. }
  10401. //-------------------------------------------
  10402. case CableCheckRequest: //37
  10403. {
  10404. //STEP 3: Execute Cable Check Process
  10405. if (Proc_iso1_CableCheckReq(AcceptFd) == 0)
  10406. {
  10407. Update_V2G_Status(CableCheckResponse);
  10408. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10409. }
  10410. else
  10411. {
  10412. DEBUG_ERROR("CableCheckReqRes: fail\n");
  10413. Update_V2G_Status(Other_Fault);
  10414. }
  10415. req_is_responsed = TRUE;
  10416. break;
  10417. }
  10418. case CableCheckResponse: //38
  10419. {
  10420. //STEP 1: Check for Process Timeout
  10421. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10422. ftime(&timerStart.SeqEnd);
  10423. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  10424. {
  10425. //DEBUG_INFO("[CableCheck] end counting...\n");
  10426. DEBUG_ERROR("CableCheck Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_CableCheck_Performance_Time);
  10427. Update_V2G_Status(Sequence_Timeout);
  10428. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10429. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  10430. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10431. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10432. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10433. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  10434. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  10435. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  10436. break;
  10437. }
  10438. #endif
  10439. //STEP 2: Check for CableCheckReq message
  10440. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  10441. {
  10442. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10443. //STEP 3: Execute Cable Check Process
  10444. if (Proc_iso1_CableCheckReq(AcceptFd) == 0)
  10445. {
  10446. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10447. }
  10448. else
  10449. {
  10450. DEBUG_ERROR("CableCheckReqRes: fail\n");
  10451. Update_V2G_Status(Other_Fault);
  10452. }
  10453. req_is_responsed = TRUE;
  10454. }
  10455. //STEP 3: Check for PreChargeReq message
  10456. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  10457. {
  10458. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10459. Update_V2G_Status(PreChargeRequest);
  10460. DEBUG_INFO("[V2G][RX]PreChargeReqReq: waiting for pre-charge voltage...\n");
  10461. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  10462. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  10463. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10464. ftime(&timerStart.SeqStart);
  10465. #endif
  10466. }
  10467. //STEP 3: Wait for PowerDeliveryReq Message
  10468. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  10469. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  10470. {
  10471. Update_V2G_Status(PowerDeliveryRequestStop);
  10472. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10473. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  10474. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10475. ftime(&timerStart.SeqStart);
  10476. #endif
  10477. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  10478. {
  10479. Update_V2G_Status(PowerDeliveryResponseStop);
  10480. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10481. }
  10482. else
  10483. {
  10484. DEBUG_ERROR("PowerDeliveryStopReqRes: fail\n");
  10485. Update_V2G_Status(Other_Fault);
  10486. }
  10487. }
  10488. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10489. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10490. {
  10491. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10492. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10493. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10494. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10495. }
  10496. else
  10497. {
  10498. }
  10499. break;
  10500. }
  10501. //-------------------------------------------
  10502. case PreChargeRequest: //39
  10503. {
  10504. if (Proc_iso1_PreChargeReq(AcceptFd) == 0)
  10505. {
  10506. Update_V2G_Status(PreChargeResponse);
  10507. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10508. }
  10509. else
  10510. {
  10511. DEBUG_ERROR("PreChargeReqRes: fail\n");
  10512. Update_V2G_Status(Other_Fault);
  10513. }
  10514. req_is_responsed = TRUE;
  10515. break;
  10516. }
  10517. case PreChargeResponse: //40
  10518. {
  10519. //STEP 1: Check for Process Timeout
  10520. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10521. ftime(&timerStart.SeqEnd);
  10522. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  10523. {
  10524. DEBUG_ERROR("Precharge Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_PreCharge_Performance_Time);
  10525. Update_V2G_Status(Sequence_Timeout);
  10526. //Update_ShmStatusCode(); //[To-Do] to be implemented
  10527. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  10528. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  10529. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  10530. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  10531. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  10532. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  10533. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  10534. break;
  10535. }
  10536. #endif
  10537. //STEP 2: Check for PreChargeReq message
  10538. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  10539. {
  10540. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10541. if (Proc_iso1_PreChargeReq(AcceptFd) == 0)
  10542. {
  10543. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10544. }
  10545. else
  10546. {
  10547. DEBUG_ERROR("PreChargeReqRes: fail\n");
  10548. Update_V2G_Status(Other_Fault);
  10549. }
  10550. req_is_responsed = TRUE;
  10551. }
  10552. //STEP 3: Check for PowerDeliveryReq message
  10553. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  10554. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  10555. {
  10556. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10557. Update_V2G_Status(PowerDeliveryRequestStart);
  10558. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  10559. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10560. ftime(&timerStart.SeqStart);
  10561. #endif
  10562. }
  10563. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10564. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10565. {
  10566. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10567. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10568. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10569. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10570. }
  10571. else
  10572. {
  10573. }
  10574. break;
  10575. }
  10576. //-------------------------------------------
  10577. case PowerDeliveryRequestStart: //41
  10578. {
  10579. if (Proc_iso1_PowerDeliveryStartReq(AcceptFd) == 0)
  10580. {
  10581. Update_V2G_Status(PowerDeliveryResponsetStart);
  10582. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10583. }
  10584. else
  10585. {
  10586. DEBUG_ERROR("PowerDeliveryStartReqRes: fail\n");
  10587. Update_V2G_Status(Other_Fault);
  10588. }
  10589. req_is_responsed = TRUE;
  10590. break;
  10591. }
  10592. case PowerDeliveryResponsetStart: //42
  10593. {
  10594. //STEP 1: Check for Process Timeout
  10595. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10596. if (v2gObject.ISO1.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  10597. {
  10598. ftime(&timerStart.SeqEnd);
  10599. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > 2000) //5 seconds
  10600. {
  10601. DEBUG_ERROR("Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), 2000);
  10602. Update_V2G_Status(Sequence_Timeout);
  10603. }
  10604. break;
  10605. }
  10606. #endif
  10607. //STEP 2: Wait for CurrentDemandReq Message
  10608. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  10609. {
  10610. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10611. Update_V2G_Status(CurrentDemandRequest);
  10612. DEBUG_INFO("[V2G][RX]CurrentDemandReqReq: energy transfering...\n");
  10613. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  10614. }
  10615. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10616. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10617. {
  10618. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10619. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10620. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10621. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10622. }
  10623. else
  10624. {}
  10625. break;
  10626. }
  10627. //-------------------------------------------
  10628. case CurrentDemandRequest: //45,
  10629. {
  10630. if (Proc_iso1_CurrentDemandReq(AcceptFd) == 0)
  10631. {
  10632. Update_V2G_Status(CurrentDemandResponse);
  10633. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10634. }
  10635. else
  10636. {
  10637. DEBUG_ERROR("CurrentDemandReqRes: fail\n");
  10638. Update_V2G_Status(Other_Fault);
  10639. }
  10640. req_is_responsed = TRUE;
  10641. break;
  10642. }
  10643. case CurrentDemandResponse: //46,
  10644. {
  10645. //STEP 1: Wait for CurrentDemandReq Message
  10646. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  10647. {
  10648. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10649. if (Proc_iso1_CurrentDemandReq(AcceptFd) == 0)
  10650. {
  10651. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10652. }
  10653. else
  10654. {
  10655. DEBUG_ERROR("CurrentDemandReqRes: fail\n");
  10656. Update_V2G_Status(Other_Fault);
  10657. }
  10658. req_is_responsed = TRUE;
  10659. }
  10660. //STEP 2: Wait for PowerDeliveryReq Message
  10661. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  10662. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  10663. {
  10664. Update_V2G_Status(PowerDeliveryRequestStop);
  10665. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10666. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  10667. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10668. ftime(&timerStart.SeqStart);
  10669. #endif
  10670. }
  10671. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10672. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10673. {
  10674. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10675. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10676. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10677. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10678. }
  10679. else
  10680. {
  10681. }
  10682. break;
  10683. }
  10684. //-------------------------------------------
  10685. case PowerDeliveryRequestStop: //49,
  10686. {
  10687. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  10688. {
  10689. Update_V2G_Status(PowerDeliveryResponseStop);
  10690. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10691. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10692. ftime(&timerStart.SeqStart);
  10693. #endif
  10694. }
  10695. else
  10696. {
  10697. DEBUG_ERROR("PowerDeliveryStopReqRes: fail\n");
  10698. Update_V2G_Status(Other_Fault);
  10699. }
  10700. req_is_responsed = TRUE;
  10701. break;
  10702. }
  10703. case PowerDeliveryResponseStop: //50,
  10704. {
  10705. //STEP 1: Check for Process Timeout
  10706. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10707. ftime(&timerStart.SeqEnd);
  10708. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > 2000) //2 seconds
  10709. {
  10710. DEBUG_ERROR("Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), 2000);
  10711. Update_V2G_Status(Sequence_Timeout);
  10712. break;
  10713. }
  10714. #endif
  10715. //STEP 2: Check for WeldingDetectionReq Message
  10716. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  10717. {
  10718. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10719. Update_V2G_Status(WeldingDetectionRequest);
  10720. DEBUG_INFO("[V2G][RX]WeldingDetectionReq: ongoing...\n");
  10721. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  10722. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10723. ftime(&timerStart.SeqStart);
  10724. #endif
  10725. }
  10726. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  10727. {
  10728. Update_V2G_Status(SessionStopRequest);
  10729. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10730. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  10731. {
  10732. Update_V2G_Status(SessionStopResponse);
  10733. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10734. }
  10735. else
  10736. {
  10737. DEBUG_ERROR("SessionStopReqRes: fail\n");
  10738. Update_V2G_Status(Other_Fault);
  10739. }
  10740. }
  10741. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10742. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10743. {
  10744. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10745. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10746. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10747. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10748. }
  10749. else
  10750. {
  10751. }
  10752. break;
  10753. }
  10754. //-------------------------------------------
  10755. case WeldingDetectionRequest: //51,
  10756. {
  10757. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  10758. {
  10759. Update_V2G_Status(WeldingDetectionResponse);
  10760. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10761. }
  10762. else
  10763. {
  10764. DEBUG_ERROR("WeldingDetectionReqRes: fail\n");
  10765. Update_V2G_Status(Other_Fault);
  10766. }
  10767. req_is_responsed = TRUE;
  10768. break;
  10769. }
  10770. case WeldingDetectionResponse: //52,
  10771. {
  10772. //STEP 1: Check for Process Timeout
  10773. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  10774. ftime(&timerStart.SeqEnd);
  10775. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  10776. {
  10777. DEBUG_ERROR("Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_WeldingDetection_Performance_Time);
  10778. Update_V2G_Status(Sequence_Timeout);
  10779. break;
  10780. }
  10781. #endif
  10782. //STEP 2: Check for WeldingDetectionReq Message
  10783. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  10784. {
  10785. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10786. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  10787. {
  10788. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10789. }
  10790. else
  10791. {
  10792. DEBUG_ERROR("WeldingDetectionReqRes: fail\n");
  10793. Update_V2G_Status(Other_Fault);
  10794. }
  10795. req_is_responsed = TRUE;
  10796. }
  10797. //STEP 3: Check for SessionStopReq Message
  10798. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  10799. {
  10800. Update_V2G_Status(SessionStopRequest);
  10801. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10802. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  10803. }
  10804. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10805. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10806. {
  10807. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10808. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10809. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10810. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10811. }
  10812. else
  10813. {
  10814. }
  10815. break;
  10816. }
  10817. //-------------------------------------------
  10818. case SessionStopRequest: //53,
  10819. {
  10820. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  10821. {
  10822. Update_V2G_Status(SessionStopResponse);
  10823. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10824. }
  10825. else
  10826. {
  10827. DEBUG_ERROR("SessionStopReqRes: fail\n");
  10828. Update_V2G_Status(Other_Fault);
  10829. }
  10830. req_is_responsed = TRUE;
  10831. break;
  10832. }
  10833. case SessionStopResponse: //54,
  10834. {
  10835. break;
  10836. }
  10837. //-------------------------------------------
  10838. default:
  10839. {
  10840. break;
  10841. }
  10842. }
  10843. usleep(1000);
  10844. }
  10845. return 0;
  10846. }
  10847. /**
  10848. *
  10849. * @param AcceptFd
  10850. * @return
  10851. */
  10852. int V2gMsg_Process_iso1_AC(int AcceptFd)
  10853. {
  10854. unsigned char req_is_responsed = FALSE;
  10855. while (req_is_responsed == FALSE)
  10856. {
  10857. //Check if it is in End_Process
  10858. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  10859. {
  10860. break;
  10861. }
  10862. switch(V2gFlowStatus)
  10863. {
  10864. //-------------------------------------------
  10865. case SupportedAppProtocolRequest: //17
  10866. {
  10867. if (v2gObject.appHandshake.supportedAppProtocolReq_isUsed == 1u)
  10868. {
  10869. v2gObject.appHandshake.supportedAppProtocolReq_isUsed = 0;
  10870. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  10871. {
  10872. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10873. Update_V2G_Status(SupportedAppProtocolResponse);
  10874. }
  10875. else
  10876. {
  10877. DEBUG_ERROR("supportedAppProtocolResReq: fail\n");
  10878. Update_V2G_Status(Other_Fault);
  10879. }
  10880. }
  10881. req_is_responsed = TRUE;
  10882. break;
  10883. }
  10884. case SupportedAppProtocolResponse: //18
  10885. {
  10886. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  10887. {
  10888. Update_V2G_Status(SessionSetupRequest);
  10889. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10890. }
  10891. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10892. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10893. {
  10894. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n",EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10895. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10896. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10897. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10898. }
  10899. else
  10900. {}
  10901. break;
  10902. }
  10903. //-------------------------------------------
  10904. case SessionSetupRequest: //19
  10905. {
  10906. if (Proc_iso1_SessionSetupReq(AcceptFd) == 0)
  10907. {
  10908. Update_V2G_Status(SessionSetupResponse);
  10909. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10910. }
  10911. else
  10912. {
  10913. DEBUG_ERROR("SessionSetupReqRes: fail\n");
  10914. Update_V2G_Status(Other_Fault);
  10915. }
  10916. req_is_responsed = TRUE;
  10917. break;
  10918. }
  10919. case SessionSetupResponse: //20
  10920. {
  10921. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  10922. {
  10923. Update_V2G_Status(ServiceDiscoveryRequest);
  10924. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10925. }
  10926. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10927. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10928. {
  10929. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10930. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10931. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10932. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10933. }
  10934. else
  10935. {}
  10936. break;
  10937. }
  10938. //-------------------------------------------
  10939. case ServiceDiscoveryRequest: //21
  10940. {
  10941. if (Proc_iso1_ServiceDiscoveryReq(AcceptFd) == 0)
  10942. {
  10943. Update_V2G_Status(ServiceDiscoveryResponse);
  10944. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10945. }
  10946. else
  10947. {
  10948. DEBUG_ERROR("ServiceDiscoveryReqRes: fail\n");
  10949. Update_V2G_Status(Other_Fault);
  10950. }
  10951. req_is_responsed = TRUE;
  10952. break;
  10953. }
  10954. case ServiceDiscoveryResponse: //22
  10955. {
  10956. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  10957. {
  10958. Update_V2G_Status(ServiceAndPaymentSelectionRequest);
  10959. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10960. }
  10961. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10962. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10963. {
  10964. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10965. EVCOMM_SYS_INFO.SequenceError = TRUE;
  10966. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  10967. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10968. }
  10969. else
  10970. {}
  10971. break;
  10972. }
  10973. //-------------------------------------------
  10974. case ServiceAndPaymentSelectionRequest: //25
  10975. {
  10976. if (Proc_iso1_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  10977. {
  10978. Update_V2G_Status(ServiceAndPaymentSelectionResponse);
  10979. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  10980. }
  10981. else
  10982. {
  10983. DEBUG_ERROR("ServiceAndPaymentSelectionReqRes: fail\n");
  10984. Update_V2G_Status(Other_Fault);
  10985. }
  10986. req_is_responsed = TRUE;
  10987. break;
  10988. }
  10989. case ServiceAndPaymentSelectionResponse://26
  10990. {
  10991. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  10992. {
  10993. Update_V2G_Status(AuthorizationRequest);
  10994. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  10995. }
  10996. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  10997. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  10998. {
  10999. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11000. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11001. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11002. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11003. }
  11004. else
  11005. {}
  11006. break;
  11007. }
  11008. //-------------------------------------------
  11009. //case ContractAuthenticationReq:
  11010. case AuthorizationRequest: //29
  11011. {
  11012. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  11013. {
  11014. Update_V2G_Status(AuthorizationResponse);
  11015. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11016. }
  11017. else
  11018. {
  11019. DEBUG_ERROR("AuthorizationReqRes: fail\n");
  11020. Update_V2G_Status(Other_Fault);
  11021. }
  11022. req_is_responsed = TRUE;
  11023. break;
  11024. }
  11025. case AuthorizationResponse: //30
  11026. {
  11027. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11028. {
  11029. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11030. if (Proc_iso1_AuthenticationReq(AcceptFd) == 0)
  11031. {
  11032. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11033. }
  11034. else
  11035. {
  11036. DEBUG_ERROR("AuthorizationReqRes: fail\n");
  11037. Update_V2G_Status(Other_Fault);
  11038. }
  11039. req_is_responsed = TRUE;
  11040. }
  11041. //Check for ChargeParameterDiscoveryReq
  11042. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11043. {
  11044. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11045. Update_V2G_Status(ChargeParameterDiscoveryRequest);
  11046. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11047. ftime(&timerStart.SeqStart);
  11048. #endif
  11049. }
  11050. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11051. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11052. {
  11053. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11054. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11055. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11056. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11057. }
  11058. else
  11059. {}
  11060. break;
  11061. }
  11062. //-------------------------------------------
  11063. case ChargeParameterDiscoveryRequest: //35
  11064. {
  11065. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11066. {
  11067. Update_V2G_Status(ChargeParameterDiscoveryResponse);
  11068. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11069. }
  11070. else
  11071. {
  11072. DEBUG_ERROR("ChargeParameterDiscoveryReqRes: fail\n");
  11073. Update_V2G_Status(Other_Fault);
  11074. }
  11075. req_is_responsed = TRUE;
  11076. break;
  11077. }
  11078. case ChargeParameterDiscoveryResponse: //36
  11079. {
  11080. //STEP 1: Check for Process Timeout
  11081. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11082. ftime(&timerStart.SeqEnd);
  11083. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  11084. {
  11085. DEBUG_ERROR("ChargingParameter Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_ChargingParameter_Performance_Time);
  11086. Update_V2G_Status(Sequence_Timeout);
  11087. break;
  11088. }
  11089. #endif
  11090. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11091. {
  11092. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11093. if (Proc_iso1_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11094. {
  11095. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11096. }
  11097. else
  11098. {
  11099. DEBUG_ERROR("ChargeParameterDiscoveryReqRes: fail\n");
  11100. Update_V2G_Status(Other_Fault);
  11101. }
  11102. req_is_responsed = TRUE;
  11103. }
  11104. //STEP 3: Wait for PowerDeliveryReq Message
  11105. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11106. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11107. {
  11108. Update_V2G_Status(PowerDeliveryRequestStart);
  11109. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11110. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  11111. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11112. ftime(&timerStart.SeqStart);
  11113. #endif
  11114. /*
  11115. if(Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  11116. {
  11117. Update_V2G_Status(PowerDeliveryResponseStop);
  11118. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11119. }
  11120. else
  11121. {
  11122. DEBUG_ERROR("PowerDeliveryStopReqRes: fail\n");
  11123. Update_V2G_Status(Other_Fault);
  11124. }*/
  11125. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11126. ftime(&timerStart.SeqStart);
  11127. #endif
  11128. }
  11129. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11130. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11131. {
  11132. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11133. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11134. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11135. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11136. }
  11137. else
  11138. {
  11139. }
  11140. break;
  11141. }
  11142. //-------------------------------------------
  11143. case PowerDeliveryRequestStart: //41
  11144. {
  11145. if (Proc_iso1_PowerDeliveryStartReq(AcceptFd) == 0)
  11146. {
  11147. Update_V2G_Status(PowerDeliveryResponsetStart);
  11148. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11149. }
  11150. else
  11151. {
  11152. DEBUG_ERROR("PowerDeliveryStartReqRes: fail\n");
  11153. Update_V2G_Status(Other_Fault);
  11154. }
  11155. req_is_responsed = TRUE;
  11156. break;
  11157. }
  11158. case PowerDeliveryResponsetStart: //42
  11159. {
  11160. //STEP 1: Check for Process Timeout
  11161. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11162. if (v2gObject.ISO1.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  11163. {
  11164. ftime(&timerStart.SeqEnd);
  11165. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > 2000) //5 seconds
  11166. {
  11167. DEBUG_ERROR("Wait for ChargingStatusReq Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), 2000);
  11168. Update_V2G_Status(Sequence_Timeout);
  11169. }
  11170. break;
  11171. }
  11172. #endif
  11173. //STEP 2: Wait for ChargingStatusReq Message
  11174. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargingStatusRequest)
  11175. {
  11176. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11177. Update_V2G_Status(ChargingStatusRequest);
  11178. DEBUG_INFO("[V2G][RX]ChargingStatusReq: energy transfering...\n");
  11179. ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargingStatus_Timer_Start);
  11180. }
  11181. else
  11182. {}
  11183. break;
  11184. }
  11185. //-------------------------------------------
  11186. case ChargingStatusRequest: //43
  11187. {
  11188. if (Proc_iso1_ChargingStatusReq(AcceptFd) == 0)
  11189. {
  11190. Update_V2G_Status(ChargingStatusResponse);
  11191. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11192. }
  11193. else
  11194. {
  11195. DEBUG_ERROR("ChargingStatusRes: fail\n");
  11196. Update_V2G_Status(Other_Fault);
  11197. }
  11198. req_is_responsed = TRUE;
  11199. break;
  11200. }
  11201. case ChargingStatusResponse: //44
  11202. {
  11203. //STEP 1: Wait for ChargingStatusReq Message
  11204. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargingStatusRequest)
  11205. {
  11206. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11207. if (Proc_iso1_ChargingStatusReq(AcceptFd) == 0)
  11208. {
  11209. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11210. }
  11211. else
  11212. {
  11213. DEBUG_ERROR("ChargingStatusRes: fail\n");
  11214. Update_V2G_Status(Other_Fault);
  11215. }
  11216. req_is_responsed = TRUE;
  11217. }
  11218. //STEP 2: Wait for PowerDeliveryReq Message
  11219. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11220. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11221. {
  11222. Update_V2G_Status(PowerDeliveryRequestStop);
  11223. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11224. ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargingStatus_Timer_End);
  11225. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11226. ftime(&timerStart.SeqStart);
  11227. #endif
  11228. }
  11229. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11230. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11231. {
  11232. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11233. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11234. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11235. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11236. }
  11237. else
  11238. {}
  11239. break;
  11240. }
  11241. //-------------------------------------------
  11242. case PowerDeliveryRequestStop: //49
  11243. {
  11244. if (Proc_iso1_PowerDeliveryStopReq(AcceptFd) == 0)
  11245. {
  11246. Update_V2G_Status(PowerDeliveryResponseStop);
  11247. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11248. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11249. ftime(&timerStart.SeqStart);
  11250. #endif
  11251. }
  11252. else
  11253. {
  11254. DEBUG_ERROR("PowerDeliveryStopReqRes: fail\n");
  11255. Update_V2G_Status(Other_Fault);
  11256. }
  11257. req_is_responsed = TRUE;
  11258. break;
  11259. }
  11260. case PowerDeliveryResponseStop: //50
  11261. {
  11262. //STEP 1: Check for Process Timeout
  11263. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11264. ftime(&timerStart.SeqEnd);
  11265. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > 2000) //2 seconds
  11266. {
  11267. DEBUG_ERROR("Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), 2000);
  11268. Update_V2G_Status(Sequence_Timeout);
  11269. break;
  11270. }
  11271. #endif
  11272. //STEP 3: Check for SessionStopReq Message
  11273. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  11274. {
  11275. Update_V2G_Status(SessionStopRequest);
  11276. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11277. }
  11278. else if ((EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest) &&
  11279. (EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest))
  11280. {
  11281. if((EVCOMM_SYS_INFO.V2G_Rx_Msg != ChargeParameterDiscoveryRequest))
  11282. {
  11283. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11284. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11285. }
  11286. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11287. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11288. }
  11289. else
  11290. {}
  11291. break;
  11292. }
  11293. case SessionStopRequest: //53
  11294. {
  11295. if (Proc_iso1_SessionStopReq(AcceptFd) == 0)
  11296. {
  11297. Update_V2G_Status(SessionStopResponse);
  11298. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11299. }
  11300. else
  11301. {
  11302. DEBUG_ERROR("SessionStopReqRes: fail\n");
  11303. Update_V2G_Status(Other_Fault);
  11304. }
  11305. req_is_responsed = TRUE;
  11306. break;
  11307. }
  11308. case SessionStopResponse: //54
  11309. {
  11310. break;
  11311. }
  11312. default:
  11313. {
  11314. break;
  11315. }
  11316. }
  11317. usleep(1000);
  11318. }
  11319. return 0;
  11320. }
  11321. /**
  11322. *
  11323. * @param AcceptFd
  11324. * @param EnergyTransferMode
  11325. * @return
  11326. */
  11327. int V2gMsg_Process_iso1(int AcceptFd, unsigned char EnergyTransferMode)
  11328. {
  11329. switch (EnergyTransferMode)
  11330. {
  11331. case DC_extended:
  11332. {
  11333. //DEBUG_INFO("EnergyTransferMode: DC_extended\n");
  11334. V2gMsg_Process_iso1_DC(AcceptFd);
  11335. break;
  11336. }
  11337. case AC_single_phase_core:
  11338. case AC_three_phase_core:
  11339. {
  11340. //DEBUG_INFO("EnergyTransferMode: AC_single_phase_core or AC_three_phase_core\n");
  11341. V2gMsg_Process_iso1_AC(AcceptFd);
  11342. break;
  11343. }
  11344. default:
  11345. {
  11346. DEBUG_WARN("Unexpected EnergyTransferMode: (%d)\n", EnergyTransferMode);
  11347. break;
  11348. }
  11349. }
  11350. return 0;
  11351. }
  11352. /**
  11353. *
  11354. * @param AcceptFd
  11355. * @return
  11356. */
  11357. int V2gMsg_Process_iso2_DC(int AcceptFd)
  11358. {
  11359. unsigned char req_is_responsed = FALSE;
  11360. while (req_is_responsed == FALSE)
  11361. {
  11362. //Check if it is in End_Process
  11363. if (EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  11364. {
  11365. break;
  11366. }
  11367. switch(V2gFlowStatus)
  11368. {
  11369. //-------------------------------------------
  11370. case SupportedAppProtocolRequest:
  11371. {
  11372. if (v2gObject.appHandshake.supportedAppProtocolReq_isUsed == 1u)
  11373. {
  11374. v2gObject.appHandshake.supportedAppProtocolReq_isUsed = 0;
  11375. if (Proc_supportedAppProtocolReq(AcceptFd) == 0) //0: no error
  11376. {
  11377. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11378. Update_V2G_Status(SupportedAppProtocolResponse);
  11379. }
  11380. else
  11381. {
  11382. DEBUG_ERROR("supportedAppProtocolResReq: fail\n");
  11383. Update_V2G_Status(Other_Fault);
  11384. }
  11385. }
  11386. req_is_responsed = TRUE;
  11387. break;
  11388. }
  11389. case SupportedAppProtocolResponse:
  11390. {
  11391. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionSetupRequest)
  11392. {
  11393. Update_V2G_Status(SessionSetupRequest);
  11394. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11395. }
  11396. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11397. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11398. {
  11399. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11400. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11401. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11402. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11403. }
  11404. else
  11405. {}
  11406. break;
  11407. }
  11408. //-------------------------------------------
  11409. case SessionSetupRequest: //19
  11410. {
  11411. if (Proc_iso2_SessionSetupReq(AcceptFd) == 0)
  11412. {
  11413. Update_V2G_Status(SessionSetupResponse);
  11414. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11415. }
  11416. else
  11417. {
  11418. DEBUG_ERROR("SessionSetupReqRes: fail\n");
  11419. Update_V2G_Status(Other_Fault);
  11420. }
  11421. req_is_responsed = TRUE;
  11422. break;
  11423. }
  11424. case SessionSetupResponse: //20
  11425. {
  11426. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceDiscoveryRequest)
  11427. {
  11428. Update_V2G_Status(ServiceDiscoveryRequest);
  11429. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11430. }
  11431. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11432. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11433. {
  11434. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11435. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11436. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11437. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11438. }
  11439. else
  11440. {}
  11441. break;
  11442. }
  11443. #if 0
  11444. //-------------------------------------------
  11445. case ServiceDiscoveryRequest: //21
  11446. {
  11447. if (Proc_iso2_ServiceDiscoveryReq(AcceptFd) == 0)
  11448. {
  11449. Update_V2G_Status(ServiceDiscoveryResponse);
  11450. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11451. }
  11452. else
  11453. {
  11454. DEBUG_ERROR("ServiceDiscoveryReqRes: fail\n");
  11455. Update_V2G_Status(Other_Fault);
  11456. }
  11457. req_is_responsed = TRUE;
  11458. break;
  11459. }
  11460. case ServiceDiscoveryResponse: //22
  11461. {
  11462. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ServiceAndPaymentSelectionRequest)
  11463. {
  11464. Update_V2G_Status(ServiceAndPaymentSelectionRequest);
  11465. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11466. }
  11467. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11468. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11469. {
  11470. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11471. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11472. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11473. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11474. }
  11475. else
  11476. {
  11477. break;
  11478. }
  11479. }
  11480. //-------------------------------------------
  11481. case ServiceAndPaymentSelectionRequest: //25
  11482. {
  11483. if (Proc_iso2_ServiceAndPaymentSelectionReq(AcceptFd) == 0)
  11484. {
  11485. Update_V2G_Status(ServiceAndPaymentSelectionResponse);
  11486. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11487. }
  11488. else
  11489. {
  11490. DEBUG_ERROR("ServiceAndPaymentSelectionReqRes: fail\n");
  11491. Update_V2G_Status(Other_Fault);
  11492. }
  11493. req_is_responsed = TRUE;
  11494. break;
  11495. }
  11496. case ServiceAndPaymentSelectionResponse: //26
  11497. {
  11498. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11499. {
  11500. Update_V2G_Status(AuthorizationRequest);
  11501. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11502. }
  11503. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11504. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11505. {
  11506. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11507. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11508. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11509. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11510. }
  11511. else
  11512. {
  11513. break;
  11514. }
  11515. }
  11516. //-------------------------------------------
  11517. //case ContractAuthenticationReq:
  11518. case AuthorizationRequest: //29
  11519. {
  11520. if (Proc_iso2_AuthenticationReq(AcceptFd) == 0)
  11521. {
  11522. Update_V2G_Status(AuthorizationResponse);
  11523. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11524. }
  11525. else
  11526. {
  11527. DEBUG_ERROR("AuthorizationReqRes: fail\n");
  11528. Update_V2G_Status(Other_Fault);
  11529. }
  11530. req_is_responsed = TRUE;
  11531. break;
  11532. }
  11533. case AuthorizationResponse: //30
  11534. {
  11535. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == AuthorizationRequest)
  11536. {
  11537. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11538. if (Proc_iso2_AuthenticationReq(AcceptFd) == 0)
  11539. {
  11540. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11541. }
  11542. else
  11543. {
  11544. DEBUG_ERROR("AuthorizationReqRes: fail\n");
  11545. Update_V2G_Status(Other_Fault);
  11546. }
  11547. req_is_responsed = TRUE;
  11548. break;
  11549. }
  11550. //Check for ChargeParameterDiscoveryReq
  11551. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11552. {
  11553. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11554. Update_V2G_Status(ChargeParameterDiscoveryRequest);
  11555. DEBUG_INFO("[V2G][RX]ChargeParameterDiscoveryReq: CSU Permission?\n");
  11556. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11557. ftime(&timerStart.SeqStart);
  11558. #endif
  11559. }
  11560. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11561. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11562. {
  11563. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11564. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11565. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11566. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11567. }
  11568. else
  11569. {
  11570. break;
  11571. }
  11572. }
  11573. //-------------------------------------------
  11574. case ChargeParameterDiscoveryRequest: //35
  11575. {
  11576. if (Proc_iso2_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11577. {
  11578. Update_V2G_Status(ChargeParameterDiscoveryResponse);
  11579. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11580. }
  11581. else
  11582. {
  11583. DEBUG_ERROR("ChargeParameterDiscoveryReqRes: fail\n");
  11584. Update_V2G_Status(Other_Fault);
  11585. }
  11586. req_is_responsed = TRUE;
  11587. break;
  11588. }
  11589. case ChargeParameterDiscoveryResponse:
  11590. {
  11591. //STEP 1: Check for Process Timeout
  11592. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11593. ftime(&timerStart.SeqEnd);
  11594. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_ChargingParameter_Performance_Time) //60 seconds
  11595. {
  11596. DEBUG_ERROR("ChargingParameter Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_ChargingParameter_Performance_Time);
  11597. Update_V2G_Status(Sequence_Timeout);
  11598. break;
  11599. }
  11600. #endif
  11601. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == ChargeParameterDiscoveryRequest)
  11602. {
  11603. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11604. if (Proc_iso2_ChargeParameterDiscoveryReq(AcceptFd) == 0)
  11605. {
  11606. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11607. }
  11608. else
  11609. {
  11610. DEBUG_ERROR("ChargeParameterDiscoveryReqRes: fail\n");
  11611. Update_V2G_Status(Other_Fault);
  11612. }
  11613. req_is_responsed = TRUE;
  11614. break;
  11615. }
  11616. //STEP 2: Check for CableCheckReq message
  11617. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  11618. {
  11619. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11620. Update_V2G_Status(CableCheckRequest);
  11621. DEBUG_INFO("[V2G][RX]CableCheckReqReq: isolated?\n");
  11622. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_Start);
  11623. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  11624. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11625. ftime(&timerStart.SeqStart);
  11626. #endif
  11627. }
  11628. //STEP 3: Wait for PowerDeliveryReq Message
  11629. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11630. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11631. {
  11632. Update_V2G_Status(PowerDeliveryRequestStop);
  11633. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11634. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  11635. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11636. ftime(&timerStart.SeqStart);
  11637. #endif
  11638. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  11639. {
  11640. Update_V2G_Status(PowerDeliveryResponseStop);
  11641. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11642. }
  11643. else
  11644. {
  11645. DEBUG_ERROR("PowerDeliveryStopReqRes: fail\n");
  11646. Update_V2G_Status(Other_Fault);
  11647. }
  11648. break;
  11649. }
  11650. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11651. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11652. {
  11653. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11654. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11655. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11656. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11657. }
  11658. else
  11659. {
  11660. break;
  11661. }
  11662. }
  11663. //-------------------------------------------
  11664. case CableCheckRequest: //37
  11665. {
  11666. //STEP 3: Execute Cable Check Process
  11667. if (Proc_iso2_CableCheckReq(AcceptFd) == 0)
  11668. {
  11669. Update_V2G_Status(CableCheckResponse);
  11670. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11671. }
  11672. else
  11673. {
  11674. DEBUG_ERROR("CableCheckReqRes: fail\n");
  11675. Update_V2G_Status(Other_Fault);
  11676. }
  11677. req_is_responsed = TRUE;
  11678. break;
  11679. }
  11680. case CableCheckResponse: //38
  11681. {
  11682. //STEP 1: Check for Process Timeout
  11683. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11684. ftime(&timerStart.SeqEnd);
  11685. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_CableCheck_Performance_Time) //38 seconds
  11686. {
  11687. //DEBUG_INFO("[CableCheck] end counting...\n");
  11688. DEBUG_ERROR("CableCheck Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_CableCheck_Performance_Time);
  11689. Update_V2G_Status(Sequence_Timeout);
  11690. //Update_ShmStatusCode(); //[To-Do] to be implemented
  11691. //CCS_SECC_TIMEOUT_V2G_CableCheck_Performance_Time (023847)
  11692. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  11693. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  11694. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  11695. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  11696. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  11697. ShmStatusCodeData->PresentStatusCode[0][5] = 7;
  11698. break;
  11699. }
  11700. #endif
  11701. //STEP 2: Check for CableCheckReq message
  11702. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CableCheckRequest)
  11703. {
  11704. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11705. //STEP 3: Execute Cable Check Process
  11706. if (Proc_iso2_CableCheckReq(AcceptFd) == 0)
  11707. {
  11708. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11709. }
  11710. else
  11711. {
  11712. DEBUG_ERROR("CableCheckReqRes: fail\n");
  11713. Update_V2G_Status(Other_Fault);
  11714. }
  11715. req_is_responsed = TRUE;
  11716. break;
  11717. }
  11718. //STEP 3: Check for PreChargeReq message
  11719. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  11720. {
  11721. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11722. Update_V2G_Status(PreChargeRequest);
  11723. DEBUG_INFO("[V2G][RX]PreChargeReqReq: waiting for pre-charge voltage...\n");
  11724. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CableCheck_Timer_End);
  11725. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_Start);
  11726. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11727. ftime(&timerStart.SeqStart);
  11728. #endif
  11729. }
  11730. //STEP 3: Wait for PowerDeliveryReq Message
  11731. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11732. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11733. {
  11734. Update_V2G_Status(PowerDeliveryRequestStop);
  11735. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11736. //ftime(&EVCOMM_SYS_INFO.V2G_SECC_ChargeParameterDiscovery_Timer_End);
  11737. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11738. ftime(&timerStart.SeqStart);
  11739. #endif
  11740. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  11741. {
  11742. Update_V2G_Status(PowerDeliveryResponseStop);
  11743. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11744. }
  11745. else
  11746. {
  11747. DEBUG_ERROR("PowerDeliveryStopReqRes: fail\n");
  11748. Update_V2G_Status(Other_Fault);
  11749. }
  11750. break;
  11751. }
  11752. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11753. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11754. {
  11755. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11756. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11757. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11758. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11759. }
  11760. else
  11761. {
  11762. break;
  11763. }
  11764. }
  11765. //-------------------------------------------
  11766. case PreChargeRequest: //39
  11767. {
  11768. if (Proc_iso2_PreChargeReq(AcceptFd) == 0)
  11769. {
  11770. Update_V2G_Status(PreChargeResponse);
  11771. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11772. }
  11773. else
  11774. {
  11775. DEBUG_ERROR("PreChargeReqRes: fail\n");
  11776. Update_V2G_Status(Other_Fault);
  11777. }
  11778. req_is_responsed = TRUE;
  11779. break;
  11780. }
  11781. case PreChargeResponse: //40
  11782. {
  11783. //STEP 1: Check for Process Timeout
  11784. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11785. ftime(&timerStart.SeqEnd);
  11786. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_PreCharge_Performance_Time) //5 seconds
  11787. {
  11788. DEBUG_ERROR("Pre-charge Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_PreCharge_Performance_Time);
  11789. Update_V2G_Status(Sequence_Timeout);
  11790. //Update_ShmStatusCode(); //[To-Do] to be implemented
  11791. //CCS_SECC_TIMEOUT_V2G_PreCharge_Performace_Time (023850)
  11792. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  11793. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  11794. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  11795. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  11796. ShmStatusCodeData->PresentStatusCode[0][4] = 5;
  11797. ShmStatusCodeData->PresentStatusCode[0][5] = 0;
  11798. break;
  11799. }
  11800. #endif
  11801. //STEP 2: Check for PreChargeReq message
  11802. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == PreChargeRequest)
  11803. {
  11804. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11805. if (Proc_iso2_PreChargeReq(AcceptFd) == 0)
  11806. {
  11807. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11808. }
  11809. else
  11810. {
  11811. DEBUG_ERROR("PreChargeReqRes: fail\n");
  11812. Update_V2G_Status(Other_Fault);
  11813. }
  11814. req_is_responsed = TRUE;
  11815. break;
  11816. }
  11817. //STEP 3: Check for PowerDeliveryReq message
  11818. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11819. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11820. {
  11821. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11822. Update_V2G_Status(PowerDeliveryRequestStart);
  11823. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Precharge_Timer_End);
  11824. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11825. ftime(&timerStart.SeqStart);
  11826. #endif
  11827. }
  11828. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11829. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11830. {
  11831. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11832. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11833. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11834. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11835. }
  11836. else
  11837. {
  11838. break;
  11839. }
  11840. }
  11841. //-------------------------------------------
  11842. case PowerDeliveryRequestStart: //41
  11843. {
  11844. if (Proc_iso2_PowerDeliveryStartReq(AcceptFd) == 0)
  11845. {
  11846. Update_V2G_Status(PowerDeliveryResponsetStart);
  11847. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11848. }
  11849. else
  11850. {
  11851. DEBUG_ERROR("PowerDeliveryStartReqRes: fail\n");
  11852. Update_V2G_Status(Other_Fault);
  11853. }
  11854. req_is_responsed = TRUE;
  11855. break;
  11856. }
  11857. case PowerDeliveryResponsetStart: //42
  11858. {
  11859. //STEP 1: Check for Process Timeout
  11860. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11861. if (v2gObject.ISO2.V2G_Message.Body.PowerDeliveryReq_isUsed == 1u)
  11862. {
  11863. ftime(&timerStart.SeqEnd);
  11864. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > 2000) //5 seconds
  11865. {
  11866. DEBUG_ERROR("Wait for CurrentDemandReq Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), 2000);
  11867. Update_V2G_Status(Sequence_Timeout);
  11868. }
  11869. break;
  11870. }
  11871. #endif
  11872. //STEP 2: Wait for CurrentDemandReq Message
  11873. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  11874. {
  11875. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11876. Update_V2G_Status(CurrentDemandRequest);
  11877. DEBUG_INFO("[V2G][RX]CurrentDemandReqReq: energy transfering...\n");
  11878. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_Start);
  11879. }
  11880. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11881. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11882. {
  11883. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11884. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11885. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11886. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11887. }
  11888. else
  11889. {
  11890. break;
  11891. }
  11892. }
  11893. //-------------------------------------------
  11894. case CurrentDemandRequest: //45,
  11895. {
  11896. if (Proc_iso2_CurrentDemandReq(AcceptFd) == 0)
  11897. {
  11898. Update_V2G_Status(CurrentDemandResponse);
  11899. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11900. }
  11901. else
  11902. {
  11903. DEBUG_ERROR("CurrentDemandReqRes: fail\n");
  11904. Update_V2G_Status(Other_Fault);
  11905. }
  11906. req_is_responsed = TRUE;
  11907. break;
  11908. }
  11909. case CurrentDemandResponse: //46,
  11910. {
  11911. //STEP 1: Wait for CurrentDemandReq Message
  11912. if (EVCOMM_SYS_INFO.V2G_Rx_Msg == CurrentDemandRequest)
  11913. {
  11914. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11915. if (Proc_iso2_CurrentDemandReq(AcceptFd) == 0)
  11916. {
  11917. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11918. }
  11919. else
  11920. {
  11921. DEBUG_ERROR("CurrentDemandReqRes: fail\n");
  11922. Update_V2G_Status(Other_Fault);
  11923. }
  11924. req_is_responsed = TRUE;
  11925. break;
  11926. }
  11927. //STEP 2: Wait for PowerDeliveryReq Message
  11928. else if((EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStop) ||
  11929. (EVCOMM_SYS_INFO.V2G_Rx_Msg == PowerDeliveryRequestStart))
  11930. {
  11931. Update_V2G_Status(PowerDeliveryRequestStop);
  11932. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11933. ftime(&EVCOMM_SYS_INFO.V2G_SECC_CurrentDemand_Timer_End);
  11934. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11935. ftime(&timerStart.SeqStart);
  11936. #endif
  11937. }
  11938. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  11939. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  11940. {
  11941. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11942. EVCOMM_SYS_INFO.SequenceError = TRUE;
  11943. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  11944. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11945. }
  11946. else
  11947. {
  11948. break;
  11949. }
  11950. }
  11951. //-------------------------------------------
  11952. case PowerDeliveryRequestStop: //49,
  11953. {
  11954. if (Proc_iso2_PowerDeliveryStopReq(AcceptFd) == 0)
  11955. {
  11956. Update_V2G_Status(PowerDeliveryResponseStop);
  11957. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  11958. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11959. ftime(&timerStart.SeqStart);
  11960. #endif
  11961. }
  11962. else
  11963. {
  11964. DEBUG_ERROR("PowerDeliveryStopReqRes: fail\n");
  11965. Update_V2G_Status(Other_Fault);
  11966. }
  11967. req_is_responsed = TRUE;
  11968. break;
  11969. }
  11970. case PowerDeliveryResponseStop: //50,
  11971. {
  11972. //STEP 1: Check for Process Timeout
  11973. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11974. ftime(&timerStart.SeqEnd);
  11975. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > 2000) //2 seconds
  11976. {
  11977. DEBUG_ERROR("Wait for WeldingDetectionReq or SessionStopReq Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), 2000);
  11978. Update_V2G_Status(Sequence_Timeout);
  11979. break;
  11980. }
  11981. #endif
  11982. //STEP 2: Check for WeldingDetectionReq Message
  11983. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  11984. {
  11985. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11986. Update_V2G_Status(WeldingDetectionRequest);
  11987. DEBUG_INFO("[V2G][RX]WeldingDetectionReq: ongoing...\n");
  11988. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_Start);
  11989. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  11990. ftime(&timerStart.SeqStart);
  11991. #endif
  11992. }
  11993. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  11994. {
  11995. Update_V2G_Status(SessionStopRequest);
  11996. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  11997. if (Proc_iso2_SessionStopReq(AcceptFd) == 0)
  11998. {
  11999. Update_V2G_Status(SessionStopResponse);
  12000. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12001. }
  12002. else
  12003. {
  12004. DEBUG_ERROR("SessionStopReqRes: fail\n");
  12005. Update_V2G_Status(Other_Fault);
  12006. }
  12007. break;
  12008. }
  12009. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12010. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12011. {
  12012. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12013. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12014. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12015. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12016. }
  12017. else
  12018. {
  12019. break;
  12020. }
  12021. }
  12022. //-------------------------------------------
  12023. case WeldingDetectionRequest: //51,
  12024. {
  12025. if (Proc_iso2_WeldingDetectionReq(AcceptFd) == 0)
  12026. {
  12027. Update_V2G_Status(WeldingDetectionResponse);
  12028. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12029. }
  12030. else
  12031. {
  12032. DEBUG_ERROR("WeldingDetectionReqRes: fail\n");
  12033. Update_V2G_Status(Other_Fault);
  12034. }
  12035. req_is_responsed = TRUE;
  12036. break;
  12037. }
  12038. case WeldingDetectionResponse: //52,
  12039. {
  12040. //STEP 1: Check for Process Timeout
  12041. #if V2G_SECC_TIMEOUT_PROTECTION == ENABLE
  12042. ftime(&timerStart.SeqEnd);
  12043. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > V2G_SECC_WeldingDetection_Performance_Time) //20 seconds
  12044. {
  12045. DEBUG_ERROR("Wait for SessionStopReq Timeout - (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), V2G_SECC_WeldingDetection_Performance_Time);
  12046. Update_V2G_Status(Sequence_Timeout);
  12047. break;
  12048. }
  12049. #endif
  12050. //STEP 2: Check for WeldingDetectionReq Message
  12051. if(EVCOMM_SYS_INFO.V2G_Rx_Msg == WeldingDetectionRequest)
  12052. {
  12053. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12054. if (Proc_iso1_WeldingDetectionReq(AcceptFd) == 0)
  12055. {
  12056. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12057. }
  12058. else
  12059. {
  12060. DEBUG_ERROR("WeldingDetectionReqRes: fail\n");
  12061. Update_V2G_Status(Other_Fault);
  12062. }
  12063. req_is_responsed = TRUE;
  12064. break;
  12065. }
  12066. //STEP 3: Check for SessionStopReq Message
  12067. else if(EVCOMM_SYS_INFO.V2G_Rx_Msg == SessionStopRequest)
  12068. {
  12069. Update_V2G_Status(SessionStopRequest);
  12070. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12071. ftime(&EVCOMM_SYS_INFO.V2G_SECC_WeldingDetection_Timer_End);
  12072. }
  12073. else if (EVCOMM_SYS_INFO.V2G_Rx_Msg >= SupportedAppProtocolRequest &&
  12074. EVCOMM_SYS_INFO.V2G_Rx_Msg <= SessionStopRequest)
  12075. {
  12076. DEBUG_ERROR("SequenceError(%d) => Tx Res MSG\n", EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12077. EVCOMM_SYS_INFO.SequenceError = TRUE;
  12078. Update_V2G_Status(EVCOMM_SYS_INFO.V2G_Rx_Msg);
  12079. EVCOMM_SYS_INFO.V2G_Rx_Msg = 0;
  12080. }
  12081. else
  12082. {
  12083. break;
  12084. }
  12085. }
  12086. //-------------------------------------------
  12087. case SessionStopRequest: //53,
  12088. {
  12089. if (Proc_iso2_SessionStopReq(AcceptFd) == 0)
  12090. {
  12091. Update_V2G_Status(SessionStopResponse);
  12092. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12093. }
  12094. else
  12095. {
  12096. DEBUG_ERROR("SessionStopReqRes: fail\n");
  12097. Update_V2G_Status(Other_Fault);
  12098. }
  12099. req_is_responsed = TRUE;
  12100. //break;
  12101. }
  12102. case SessionStopResponse: //54,
  12103. {
  12104. break;
  12105. }
  12106. #endif
  12107. //-------------------------------------------
  12108. default:
  12109. {
  12110. break;
  12111. }
  12112. }
  12113. usleep(1000);
  12114. }
  12115. return 0;
  12116. }
  12117. /**
  12118. *
  12119. * @param AcceptFd
  12120. * @return
  12121. */
  12122. int V2gMsg_Process_iso2_AC(int AcceptFd)
  12123. {
  12124. return 0;
  12125. }
  12126. /**
  12127. *
  12128. * @param AcceptFd
  12129. * @param EnergyTransferMode
  12130. * @return
  12131. */
  12132. int V2gMsg_Process_iso2(int AcceptFd, unsigned char EnergyTransferMode)
  12133. {
  12134. switch (EnergyTransferMode)
  12135. {
  12136. case DC_extended:
  12137. {
  12138. V2gMsg_Process_iso2_DC(AcceptFd);
  12139. break;
  12140. }
  12141. case AC_single_phase_core:
  12142. case AC_three_phase_core:
  12143. {
  12144. V2gMsg_Process_iso2_AC(AcceptFd);
  12145. break;
  12146. }
  12147. default:
  12148. {
  12149. DEBUG_WARN("Unexpected EnergyTransferMode: (%d)\n", EnergyTransferMode);
  12150. break;
  12151. }
  12152. }
  12153. return 0;
  12154. }
  12155. /**
  12156. *
  12157. * @param AcceptFd
  12158. * @return
  12159. */
  12160. int V2gMsg_Process(int AcceptFd)
  12161. {
  12162. int errn = 0;
  12163. switch (ShmCcsData->CommProtocol)
  12164. {
  12165. case V2GT_MSG_PROTOCOL_DIN70121: //0
  12166. {
  12167. V2gMsg_Process_din(AcceptFd);
  12168. break;
  12169. }
  12170. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  12171. {
  12172. V2gMsg_Process_iso1(AcceptFd, ShmCcsData->EnergyTransferMode);
  12173. break;
  12174. }
  12175. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  12176. {
  12177. V2gMsg_Process_iso2(AcceptFd, ShmCcsData->EnergyTransferMode);
  12178. break;
  12179. }
  12180. default:
  12181. {
  12182. DEBUG_WARN("Unexpected Communication Protocol: (%d)\n", ShmCcsData->CommProtocol);
  12183. break;
  12184. }
  12185. }
  12186. return errn;
  12187. }
  12188. /**
  12189. *
  12190. * @param AcceptFd
  12191. * @return
  12192. */
  12193. int V2gMsg_Rx(int AcceptFd)
  12194. {
  12195. int errn = 0;
  12196. unsigned int packet_size = 0;
  12197. memset(v2gBuffer.rx, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  12198. packet_size = recv(AcceptFd, v2gBuffer.rx, V2GTP_MSG_RX_BUFFER_SIZE, 0);
  12199. //[CAUTION] Everytime recv() is called, it will take a default time to wait for TCP packets.
  12200. //The more time you call recv(), the more time you will waste here.
  12201. //Here it is suggested that response immediatedly once you receive any packets.
  12202. //For configuring the Rx waiting time, please use setsockopt().
  12203. if(packet_size > 0) //[Joseph] If there is no data, ignore. (TBD)
  12204. {
  12205. //DEBUG_PRINTF_EVCOMM_DETAIL("\n\n Got TCP V2GTP Message: size = %d Bytes\n", packet_size);
  12206. errn = V2gMsgDecoder(v2gBuffer.rx, packet_size, V2gFlowStatus);
  12207. if (errn < 0)
  12208. {
  12209. DEBUG_ERROR("V2gMsgDecoder fail: (%d)\n", errn);
  12210. }
  12211. }
  12212. return errn;
  12213. }
  12214. /**
  12215. *
  12216. * @param AcceptFd
  12217. * @return
  12218. */
  12219. int V2gComm(int AcceptFd)
  12220. {
  12221. int errn = 0;
  12222. if (V2gMsg_Rx(AcceptFd) < 0)
  12223. {
  12224. Update_V2G_Status(Other_Fault);
  12225. errn = -1;
  12226. }
  12227. //following are the response message handling according to status flag
  12228. if (V2gMsg_Process(AcceptFd) < 0)
  12229. {
  12230. errn = -1;
  12231. }
  12232. //Error Check
  12233. //V2G_Error_Monitor();
  12234. return errn;
  12235. }
  12236. /**
  12237. *
  12238. * @return
  12239. */
  12240. int SdpUdpConnected()
  12241. {
  12242. int packet_size,Rtn;
  12243. struct sockaddr_in6 ServerAddr,ClientAddr;
  12244. struct V2gtpHeader *header;
  12245. unsigned char *payload;
  12246. // UDP server initial
  12247. if(socketFd.Udp <= 0)
  12248. {
  12249. if ((socketFd.Udp = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
  12250. {
  12251. DEBUG_ERROR("Fail to open UdpSock\n");
  12252. return 0;
  12253. }
  12254. memset(&ServerAddr,0, sizeof(struct sockaddr_in));
  12255. ServerAddr.sin6_family = AF_INET6;
  12256. ServerAddr.sin6_addr = in6addr_any;
  12257. ServerAddr.sin6_port = htons(SdpUdpServerPort);
  12258. if(bind(socketFd.Udp, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) < 0)
  12259. {
  12260. DEBUG_ERROR("Fail to bind UdpSock(%d)\n", socketFd.Udp);
  12261. close(socketFd.Udp);
  12262. socketFd.Udp = -1;
  12263. return 0;
  12264. }
  12265. DEBUG_INFO("[UDP] opened(%d), port: %d\n", socketFd.Udp, SdpUdpServerPort);
  12266. DEBUG_INFO("[UDP] Wait connect request from EVCC\n");
  12267. }
  12268. // Receive packet from udp server
  12269. memset(v2gBuffer.rx, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  12270. memset(&ClientAddr, 0, sizeof(struct sockaddr_in));
  12271. Rtn = sizeof(struct sockaddr_in6);
  12272. packet_size = recvfrom(socketFd.Udp, v2gBuffer.rx, V2GTP_MSG_RX_BUFFER_SIZE, MSG_DONTWAIT, (struct sockaddr *)&ClientAddr, (socklen_t *)&Rtn);
  12273. if(packet_size > 0)
  12274. {
  12275. char buf[128];
  12276. header = (struct V2gtpHeader *) v2gBuffer.rx;
  12277. payload = v2gBuffer.rx+sizeof(struct V2gtpHeader);
  12278. DEBUG_PRINTF_EVCOMM_DETAIL("Got UDP packet_size: %d (Bytes)\n", packet_size);
  12279. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  12280. DEBUG_PRINTF_EVCOMM_DETAIL("***** Received SDP Packet *****\n");
  12281. DEBUG_PRINTF_EVCOMM_DETAIL("***********************************\n");
  12282. memset(buf, 0x00, ARRAY_SIZE(buf));
  12283. for(Rtn = 0; Rtn < 16; Rtn += 2)
  12284. {
  12285. sprintf(buf, "%s%02x%02x:", buf, ClientAddr.sin6_addr.s6_addr[Rtn],ClientAddr.sin6_addr.s6_addr[Rtn+1]);
  12286. }
  12287. DEBUG_PRINTF_EVCOMM_DETAIL("ClientAddress: %s\n", buf);
  12288. DEBUG_PRINTF_EVCOMM_DETAIL("ClientPort: %d\n",ClientAddr.sin6_port);
  12289. DEBUG_PRINTF_EVCOMM_DETAIL("ProtocolVersion: %d\n",header->ProtocolVersion);
  12290. DEBUG_PRINTF_EVCOMM_DETAIL("InverseProtocolVersion: 0x%x\n",header->InverseProtocolVersion);
  12291. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadType: 0x%x\n",htons(header->PayloadType));
  12292. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadLength: 0x%x\n",htonl(header->PayloadLength));
  12293. DEBUG_PRINTF_EVCOMM_DETAIL("htons(header->PayloadType): 0x%x\n",htons(header->PayloadType));
  12294. if( (header->ProtocolVersion == 0x01) &&
  12295. (header->InverseProtocolVersion == 0xFE) &&
  12296. (htons(header->PayloadType) == V2GTP_PAYLOAD_TYPE_SDP_REQUEST))
  12297. {
  12298. DEBUG_INFO("[UDP] Get connect request from EVCC: (%d)\n", socketFd.Udp);
  12299. DEBUG_PRINTF_EVCOMM_DETAIL("Security: 0x%02X\n", *(payload + 0));
  12300. DEBUG_PRINTF_EVCOMM_DETAIL("TransportProtocol: 0x%02X\n", *(payload + 1));
  12301. header->PayloadType = htons(V2GTP_PAYLOAD_TYPE_SDP_RESPONSE);
  12302. header->PayloadLength = htonl(20); //Fixed Length=20
  12303. memset(payload, 0, 20);
  12304. // MAC address[0:2] + FFFE + MAC address[3:5]
  12305. payload[0] = (IPV6_LINK_LOCAL_PREFIX>>8) & 0xFF;
  12306. payload[1] = IPV6_LINK_LOCAL_PREFIX & 0xFF;
  12307. #ifdef TEST_WITH_ETH0
  12308. payload[8] = macAddr.eth0[0];
  12309. payload[8] ^= 0x02;// bit 1 should complemented.
  12310. payload[9] = macAddr.eth0[1];
  12311. payload[10] = macAddr.eth0[2];
  12312. payload[11] = 0xFF;
  12313. payload[12] = 0xFE;
  12314. payload[13] = macAddr.eth0[3];
  12315. payload[14] = macAddr.eth0[4];
  12316. payload[15] = macAddr.eth0[5];
  12317. #else
  12318. payload[8] = macAddr.eth1[0];
  12319. payload[8] ^= 0x02;// bit 1 should complemented.
  12320. payload[9] = macAddr.eth1[1];
  12321. payload[10] = macAddr.eth1[2];
  12322. payload[11] = 0xFF;
  12323. payload[12] = 0xFE;
  12324. payload[13] = macAddr.eth1[3];
  12325. payload[14] = macAddr.eth1[4];
  12326. payload[15] = macAddr.eth1[5];
  12327. #endif
  12328. // Report TCP port
  12329. payload[16] = (EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active>>8) & 0xFF;
  12330. payload[17] = EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active & 0xFF;
  12331. payload[18] = SDP_PAYLOAD_SECURITY_NONE; //Security
  12332. payload[19] = SDP_PAYLOAD_TRANS_PROTOCOL_TCP; //Transport protocol
  12333. // TODO:
  12334. // Setup the TCP in advance to receive the coming TCP handshake messages after SDP response.
  12335. Rtn = sendto(socketFd.Udp, v2gBuffer.rx, sizeof(struct V2gtpHeader) + htonl(header->PayloadLength), 0, (struct sockaddr *)&ClientAddr, sizeof(struct sockaddr_in6));
  12336. DEBUG_INFO("[UDP] Response to EVCC\n");
  12337. DEBUG_PRINTF_EVCOMM_DETAIL("***** Response SDP Packet *****\n");
  12338. DEBUG_PRINTF_EVCOMM_DETAIL("Send size: %d\n", Rtn);
  12339. memset(buf, 0x00, ARRAY_SIZE(buf));
  12340. for(Rtn = 0; Rtn < 16; Rtn++)
  12341. {
  12342. sprintf(buf, "%s%02x ", buf, ClientAddr.sin6_addr.s6_addr[Rtn]);
  12343. }
  12344. DEBUG_PRINTF_EVCOMM_DETAIL("Destination Address: %s\n", buf);
  12345. DEBUG_PRINTF_EVCOMM_DETAIL("Destination Port: %d\n", ClientAddr.sin6_port);
  12346. DEBUG_PRINTF_EVCOMM_DETAIL("ProtocolVersion: %d\n", header->ProtocolVersion);
  12347. DEBUG_PRINTF_EVCOMM_DETAIL("InverseProtocolVersion: 0x%x\n", header->InverseProtocolVersion);
  12348. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadType: 0x%x\n", htons(header->PayloadType));
  12349. DEBUG_PRINTF_EVCOMM_DETAIL("PayloadLength: 0x%x\n", htonl(header->PayloadLength));
  12350. memset(buf, 0x00, ARRAY_SIZE(buf));
  12351. for(Rtn = 0; Rtn < 16; Rtn++)
  12352. {
  12353. sprintf(buf, "%s%02x:", buf, payload[Rtn]);
  12354. }
  12355. DEBUG_PRINTF_EVCOMM_DETAIL("SECC Ipv6 Address: %s\n", buf);
  12356. DEBUG_PRINTF_EVCOMM_DETAIL("SECC Port: %d\n", (payload[16]<<8 | payload[17]));
  12357. DEBUG_PRINTF_EVCOMM_DETAIL("Security: 0x%x\n", payload[19]);
  12358. DEBUG_PRINTF_EVCOMM_DETAIL("TransportProtocol: 0x%x\n", payload[20]);
  12359. if(Rtn > 0)
  12360. {
  12361. return 1;
  12362. }
  12363. }
  12364. }
  12365. return 0;
  12366. }
  12367. /**
  12368. *
  12369. * @return
  12370. */
  12371. int V2gTcpConnected()
  12372. {
  12373. int Rtn,AcceptFd;
  12374. char buf[128];
  12375. struct sockaddr_in6 ServerAddr,ClientAddr;
  12376. // Initial TCP server
  12377. if(socketFd.Tcp <= 0)
  12378. {
  12379. if ((socketFd.Tcp = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
  12380. {
  12381. DEBUG_ERROR("Fail to open TcpSock (%s)\n", strerror(errno));
  12382. usleep(100000);
  12383. return 0;
  12384. }
  12385. fcntl(socketFd.Tcp, F_SETFL, O_NONBLOCK); //set to O_NONBLOCK
  12386. DEBUG_INFO("[TCP]Socket set: DONE.\n");
  12387. memset(&ServerAddr,0, sizeof(struct sockaddr_in));
  12388. ServerAddr.sin6_family = PF_INET6;
  12389. ServerAddr.sin6_addr = in6addr_any;
  12390. ServerAddr.sin6_port = htons(EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  12391. if(bind(socketFd.Tcp, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) < 0)
  12392. {
  12393. DEBUG_ERROR("Fail to bind TcpSock(%s), SdpTcpServerPort: %d\n", strerror(errno), EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  12394. if(bind(socketFd.Tcp, (struct sockaddr *)&ServerAddr, sizeof(struct sockaddr_in6)) < 0)
  12395. {
  12396. DEBUG_INFO("[Error Fail to bind TcpSock(%s), SdpTcpServerPort: %d\n", strerror(errno), EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  12397. usleep(100000);
  12398. close(socketFd.Tcp);
  12399. socketFd.Tcp = -1;
  12400. return 0;
  12401. }
  12402. DEBUG_INFO("[TCP]bind: DONE\n");
  12403. }
  12404. DEBUG_INFO("[TCP]listen: START\n");
  12405. if(listen(socketFd.Tcp, 1) == -1) //only accept one connection
  12406. {
  12407. DEBUG_ERROR("Fail to listen TcpSock(%s)", strerror(errno));
  12408. usleep(100000);
  12409. close(socketFd.Tcp);
  12410. socketFd.Tcp = -1;
  12411. return 0;
  12412. }
  12413. DEBUG_INFO("[TCP]listen: DONE\n");
  12414. DEBUG_INFO("[TCP]TcpSock: opened(%d), port=%d\n", socketFd.Tcp, htons(ServerAddr.sin6_port));
  12415. DEBUG_INFO("[TCP]accept: START\n");
  12416. }
  12417. // Wait connection in
  12418. Rtn = sizeof(struct sockaddr_in6);
  12419. if((AcceptFd = accept(socketFd.Tcp, (struct sockaddr *)&ClientAddr, (socklen_t *)&Rtn)) == -1)
  12420. {
  12421. static BOOL tmp = 0;
  12422. if (tmp == 0)
  12423. {
  12424. DEBUG_INFO("[TCP]Wait TCP connection...\n");
  12425. tmp = 1;
  12426. }
  12427. else
  12428. {
  12429. //DEBUG_PRINTF_EVCOMM_DETAIL(".");
  12430. }
  12431. return 0;
  12432. }
  12433. DEBUG_INFO("[TCP]Accept: DONE\n");
  12434. DEBUG_PRINTF_EVCOMM_DETAIL("Accept one TCP connection:\n");
  12435. DEBUG_PRINTF_EVCOMM_DETAIL("AcceptFd: %d\n",AcceptFd);
  12436. memset(buf, 0x00, ARRAY_SIZE(buf));
  12437. for(Rtn = 0; Rtn < 16; Rtn += 2)
  12438. {
  12439. sprintf(buf, "%s%02x%02x:", buf, ClientAddr.sin6_addr.s6_addr[Rtn], ClientAddr.sin6_addr.s6_addr[Rtn+1]);
  12440. }
  12441. DEBUG_PRINTF_EVCOMM_DETAIL("ClientAddress: %s\n", buf);
  12442. DEBUG_PRINTF_EVCOMM_DETAIL("ClientPort: %d\n", ClientAddr.sin6_port);
  12443. return AcceptFd;
  12444. }
  12445. /**
  12446. *
  12447. * @return
  12448. */
  12449. int End_Process()
  12450. {
  12451. if(EVCOMM_SYS_INFO.End_Process_inused == TRUE)
  12452. {
  12453. DEBUG_INFO("End_Process has been triggered by another event.\n");
  12454. return -1;
  12455. }
  12456. DEBUG_INFO("Entering...\n");
  12457. //STEP 1: Ask CSU to Stop
  12458. EVCOMM_SYS_INFO.End_Process_inused = TRUE;
  12459. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  12460. EVCOMM_SYS_INFO.ConnectorLocked = FALSE;
  12461. ShmInternalComm->ChargingPermission = FALSE;
  12462. //Step 2: Close sockets
  12463. DEBUG_INFO("Close RAW, UDP, TCP sockets...\n");
  12464. if(socketFd.Raw > 0)
  12465. {
  12466. close(socketFd.Raw);
  12467. }
  12468. if(socketFd.Udp > 0)
  12469. {
  12470. close(socketFd.Udp);
  12471. }
  12472. if(socketFd.Tcp > 0)
  12473. {
  12474. close(socketFd.Tcp);
  12475. close(socketFd.TcpAccept);
  12476. }
  12477. socketFd.Raw = socketFd.Udp = socketFd.Tcp = socketFd.TcpAccept = -1;
  12478. //STEP 3: Switch to State E
  12479. //SwitchCpStateE(ENABLE);
  12480. //STEP 4: Close tcpdump
  12481. Sniffer_Tcpdump(DISABLE);
  12482. //STEP 5: Keep 100% PWM for 5 seconds
  12483. OutputCpPwmDuty(100);
  12484. for(int idx=5;idx>0;idx--)
  12485. {
  12486. DEBUG_INFO("Wait PWM switch to 100%% count down: %d\n", idx);
  12487. sleep(1);
  12488. }
  12489. qcaInfo.AttenProfileCnt = 0;
  12490. init_appHandEXIDocument(&v2gObject.appHandshake);
  12491. // Qca7kPowerReset(); //reset QCA7000 /* +++ 20200808, vern, should disconnected PLC connection after session stop ---*/
  12492. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
  12493. {
  12494. system("sync");
  12495. }
  12496. #endif
  12497. //STEP 4: Switch to State E
  12498. //Keep State E for 5 seconds
  12499. #if 0
  12500. SwitchCpStateE(ENABLE); //Set PWM Duty as 0 and set State as E (0V)
  12501. for(uint8_t idx=5;idx>0;idx--)
  12502. {
  12503. DEBUG_INFO("Wait CP change to State E count down: %d\n", idx);
  12504. sleep(1);
  12505. }
  12506. #endif
  12507. //Reset Memory
  12508. unsigned char SlaveAddress_backup;
  12509. unsigned int matched_backup;
  12510. //unsigned char state_backup;
  12511. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
  12512. {
  12513. system("sync");
  12514. }
  12515. #endif
  12516. sleep(1);
  12517. //Backup CsuComm flags
  12518. SlaveAddress_backup = ShmInternalComm->SlaveAddress;
  12519. matched_backup = CSUCOMMDC_TASK_FLAG.matched;
  12520. //state_backup = Get_V2G_Status();
  12521. memset(v2gBuffer.rx, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  12522. memset(v2gBuffer.tx, 0, V2GTP_MSG_TX_BUFFER_SIZE);
  12523. memset(&v2gObject.appHandshake, 0, sizeof(struct appHandEXIDocument));
  12524. memset(&v2gObject.DIN, 0, sizeof(struct dinEXIDocument));
  12525. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  12526. memset(ShmInternalComm, 0, sizeof(struct InternalComm));
  12527. memset(ShmCcsData, 0, sizeof(struct CcsData));
  12528. //Resume CsuComm flags
  12529. ShmInternalComm->SlaveAddress = SlaveAddress_backup;
  12530. CSUCOMMDC_TASK_FLAG.matched = matched_backup;
  12531. Sniffer_Candump(DISABLE);
  12532. Sniffer_Candump(ENABLE);
  12533. DEBUG_INFO("---------------------------------------------\n");
  12534. DEBUG_INFO("-- EVCOMM: END --\n");
  12535. DEBUG_INFO("---------------------------------------------\n");
  12536. system("pkill Module_CCS");
  12537. while(1)
  12538. {
  12539. //wait for CSU confirm
  12540. usleep(1000);
  12541. }
  12542. }
  12543. /**
  12544. *
  12545. * @return
  12546. */
  12547. int Parameters_Init()
  12548. {
  12549. //Step 0: Generate random number
  12550. unsigned int value_random;
  12551. struct timeb time_seed;
  12552. ftime(&time_seed);
  12553. srand(time_seed.millitm);
  12554. //Step 1: Init SDP TCP Port
  12555. srand(time(NULL));
  12556. value_random = rand();
  12557. EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active = (unsigned short)(SdpTcpServerPort + (value_random % 10000) + 1);
  12558. DEBUG_INFO("TCP Port assign: %d\n", EVCOMM_SYS_INFO.SDP_TCP_Server_Port_active);
  12559. //Step 2: Init SessionID
  12560. srand(time(NULL));
  12561. value_random = rand();
  12562. memcpy(&EVCOMM_SYS_INFO.SessionID[0], &value_random, 4);
  12563. srand(time(NULL));
  12564. value_random = rand();
  12565. memcpy(&EVCOMM_SYS_INFO.SessionID[4], &value_random, 4);
  12566. DEBUG_INFO("SessionID: (%02X%02X%02X%02X%02X%02X%02X%02X)\n",
  12567. EVCOMM_SYS_INFO.SessionID[0],
  12568. EVCOMM_SYS_INFO.SessionID[1],
  12569. EVCOMM_SYS_INFO.SessionID[2],
  12570. EVCOMM_SYS_INFO.SessionID[3],
  12571. EVCOMM_SYS_INFO.SessionID[4],
  12572. EVCOMM_SYS_INFO.SessionID[5],
  12573. EVCOMM_SYS_INFO.SessionID[6],
  12574. EVCOMM_SYS_INFO.SessionID[7]);
  12575. return 0;
  12576. }
  12577. /**
  12578. *
  12579. * @return
  12580. */
  12581. int SyncAcShreaMemory()
  12582. {
  12583. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  12584. pid_t pid;
  12585. pid = fork();
  12586. if(pid == 0)
  12587. {
  12588. for(;;)
  12589. {
  12590. if(!EVCOMM_SYS_INFO.End_Process_inused)
  12591. {
  12592. //========================================
  12593. // CSU -> CCS setting
  12594. //========================================
  12595. // Heart beat
  12596. ShmDispenser->gun_info.acCcsInfo.CcsHeartBeat++;
  12597. // Permission
  12598. if(ShmInternalComm->ChargingPermission != ShmDispenser->gun_info.acCcsInfo.ChargingPermission)
  12599. {
  12600. DEBUG_INFO("AC permission: %d -> %d\n", ShmInternalComm->ChargingPermission, ShmDispenser->gun_info.acCcsInfo.ChargingPermission);
  12601. ShmInternalComm->ChargingPermission_new = ShmDispenser->gun_info.acCcsInfo.ChargingPermission;
  12602. ShmInternalComm->ChargingPermission_pre = ShmInternalComm->ChargingPermission;
  12603. ShmInternalComm->ChargingPermission = ShmInternalComm->ChargingPermission_new;
  12604. }
  12605. // CP
  12606. ShmInternalComm->AC_CpPositiveVoltage = ShmDispenser->gun_info.acCcsInfo.CpPositiveVoltage;
  12607. if(ShmInternalComm->AC_CpPresentState != ShmDispenser->gun_info.acCcsInfo.CpPresentState)
  12608. {
  12609. DEBUG_INFO("CP state: %d -> %d\n", ShmInternalComm->AC_CpPresentState, ShmDispenser->gun_info.acCcsInfo.CpPresentState);
  12610. ShmInternalComm->AC_CpPresentState = ShmDispenser->gun_info.acCcsInfo.CpPresentState;
  12611. }
  12612. // Charging parameter
  12613. ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].AvailableChargingCurrent = ShmInternalComm->AvailableChargingCurrent = ShmDispenser->gun_info.targetCurrent;
  12614. // RCD status sync
  12615. ShmInternalComm->AC_RcdStatus = 0;
  12616. // EVSENotification
  12617. ShmInternalComm->AC_EVSENotification = 0;//ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification;
  12618. //========================================
  12619. // CCS -> CSU check
  12620. //========================================
  12621. // V2G message flow status
  12622. if(ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus != Get_V2G_Status())
  12623. {
  12624. //DEBUG_INFO("PresentMsgFlowStatus: %d -> %d\n", ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus, Get_V2G_Status());
  12625. ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus = Get_V2G_Status();
  12626. switch(ShmDispenser->gun_info.acCcsInfo.PresentMsgFlowStatus)
  12627. {
  12628. case PowerDeliveryRequestStart ... SessionStopRequest:
  12629. ShmDispenser->gun_info.acCcsInfo.EVChargeProgress = HLC_START_MODE;
  12630. break;
  12631. case CM_SLAC_PARM_REQ ... PreChargeResponse:
  12632. ShmDispenser->gun_info.acCcsInfo.EVChargeProgress = HLC_STANDBY_MODE;
  12633. break;
  12634. default:
  12635. ShmDispenser->gun_info.acCcsInfo.EVChargeProgress = HLC_STOP_MODE;
  12636. break;
  12637. }
  12638. }
  12639. }
  12640. usleep(1000);
  12641. }
  12642. }
  12643. #endif
  12644. return 0;
  12645. }
  12646. /**
  12647. *
  12648. * @param argc
  12649. * @param argv
  12650. * @return
  12651. */
  12652. int main(int argc, char *argv[])
  12653. {
  12654. DEBUG_INFO("---------------------------------------------\n");
  12655. DEBUG_INFO("-- EVCOMM: START --\n");
  12656. DEBUG_INFO("---------------------------------------------\n");
  12657. // QCA7000 chip reset
  12658. Qca7kPowerReset();
  12659. // Share memory Initialization
  12660. ShareMemory_Init();
  12661. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  12662. // Release socket handler
  12663. if(socketFd.Raw > 0)
  12664. {
  12665. close(socketFd.Raw);
  12666. }
  12667. if(socketFd.Udp > 0)
  12668. {
  12669. close(socketFd.Udp);
  12670. }
  12671. if(socketFd.Tcp > 0)
  12672. {
  12673. close(socketFd.Tcp);
  12674. }
  12675. socketFd.Raw = socketFd.Udp = socketFd.Tcp = -1;
  12676. Update_V2G_Status(IDLE);
  12677. //Init V2G TCP/IPv6 packets buffer
  12678. memset(v2gBuffer.rx, 0, V2GTP_MSG_RX_BUFFER_SIZE);
  12679. memset(v2gBuffer.tx, 0, V2GTP_MSG_TX_BUFFER_SIZE);
  12680. //Release State E Control
  12681. SwitchCpStateE(DISABLE);
  12682. OutputCpPwmDuty(100);
  12683. //start to detect CP pilot state
  12684. pid.CP_Detection = 0;
  12685. CP_Detection(); //fork1
  12686. DEBUG_INFO("[fork1]CP Detection: ON\n");
  12687. //start to detect errors
  12688. Error_Monitor(); //fork2
  12689. DEBUG_INFO("[fork2]Error Monitor: ON\n");
  12690. //start to detect PP
  12691. pid.PP_Detection = 0;
  12692. #if (PP_PROTECTION_MECHANISM == ENABLE)
  12693. PP_Detection();
  12694. DEBUG_INFO("[fork3]PP Detection: ON\n");
  12695. #else
  12696. DEBUG_INFO("[fork3]PP Detection: OFF\n");
  12697. #endif
  12698. // Get network interface MAC address
  12699. GetEthMac((uint8_t*)"eth0", macAddr.eth0);
  12700. GetEthMac((uint8_t*)QcaInterface, macAddr.eth1);
  12701. // XML parser initialization
  12702. qcaInfo.AttenProfileCnt = 0;
  12703. init_appHandEXIDocument(&v2gObject.appHandshake);
  12704. // Init Energy transfer mode
  12705. // TODO:
  12706. // 1. Parsing Model Name
  12707. ShmCcsData->EnergyTransferMode = CCS_ENERGY_TRANSFER_MODE;
  12708. Parameters_Init();
  12709. Sniffer_Tcpdump(TCPDUMP_PACKETS_SNIFFER_SWITCH);
  12710. #if (TCPDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  12711. sleep(1); //wait for TCP dump getting ready
  12712. #endif
  12713. // AC share memory exchange
  12714. SyncAcShreaMemory();
  12715. DEBUG_INFO("[fork4]AC share memory exchange: ON\n");
  12716. DEBUG_INFO("Module_CCS initialize OK.\n");
  12717. for(;;)
  12718. {
  12719. if(!EVCOMM_SYS_INFO.End_Process_inused)
  12720. {
  12721. if(V2gFlowStatus < SLACC_SDP_UDP_Connection)
  12722. {
  12723. SlacComm();
  12724. }
  12725. else if(V2gFlowStatus == SLACC_SDP_UDP_Connection)
  12726. {
  12727. if(SdpUdpConnected() == 1)
  12728. {
  12729. Update_V2G_Status(SLACC_SDP_TCP_Connection);
  12730. continue;
  12731. }
  12732. SlacComm(); //TC_SECC_VTB_CmSlacMatch_004
  12733. ftime(&timerStart.SeqEnd);
  12734. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > TT_match_join)
  12735. {
  12736. DEBUG_ERROR("Wait SLACC_SDP_UDP_Connection Timeout - TT_match_join (%.02lf of %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), TT_match_join);
  12737. Update_V2G_Status(Sequence_Timeout);
  12738. //Update_ShmStatusCode(); //[To-Do] to be implemented
  12739. //CCS_SECC_TIMEOUT_SLACC_SDP_UDP_TT_match_join (023823)
  12740. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  12741. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  12742. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  12743. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  12744. ShmStatusCodeData->PresentStatusCode[0][4] = 2;
  12745. ShmStatusCodeData->PresentStatusCode[0][5] = 3;
  12746. //CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  12747. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  12748. }
  12749. }
  12750. else if(V2gFlowStatus == SLACC_SDP_TCP_Connection)
  12751. {
  12752. if((socketFd.TcpAccept = V2gTcpConnected()) > 0)
  12753. {
  12754. Update_V2G_Status(SupportedAppProtocolRequest);
  12755. ftime(&EVCOMM_SYS_INFO.V2G_SECC_Sequence_Timer_Start);
  12756. continue;
  12757. }
  12758. SlacComm();
  12759. ftime(&timerStart.SeqEnd);
  12760. if(DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd) > TT_match_join)
  12761. {
  12762. DEBUG_ERROR("Wait SLACC_SDP_TCP_Connection Timeout - TT_match_join (%.02lf / %d ms)\n", DiffTimeb(timerStart.SeqStart, timerStart.SeqEnd), TT_match_join);
  12763. Update_V2G_Status(Sequence_Timeout);
  12764. //Update_ShmStatusCode(); //[To-Do] to be implemented
  12765. //CCS_SECC_TIMEOUT_SLACC_SDP_TCP_TT_match_join (023824)
  12766. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  12767. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  12768. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  12769. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  12770. ShmStatusCodeData->PresentStatusCode[0][4] = 2;
  12771. ShmStatusCodeData->PresentStatusCode[0][5] = 4;
  12772. //CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  12773. Proc_EVStopRes(ShmInternalComm->FD_CAN_Socket);
  12774. }
  12775. }
  12776. else if(V2gFlowStatus <= SessionStopResponse)
  12777. {
  12778. if (V2gComm(socketFd.TcpAccept) < 0)
  12779. {
  12780. //error occurs
  12781. }
  12782. }
  12783. else if (V2gFlowStatus >= Performance_Timeout)
  12784. {
  12785. //End_Process
  12786. }
  12787. else
  12788. {}
  12789. }
  12790. else
  12791. {
  12792. // TODO:
  12793. // 1. After complete or error occur task do not need to end
  12794. //Update_V2G_Status(IDLE);
  12795. //SwitchCpStateE(DISABLE);
  12796. //OutputCpPwmDuty(100);
  12797. }
  12798. usleep(1000);
  12799. } //while
  12800. }//main while