ReMain.c 278 KB

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