main.c 288 KB

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