main.c 301 KB

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