ReMain.c 270 KB

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