ReMain.c 271 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514
  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 log_info(format, args...) StoreLogMsg_1("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  118. #define log_warn(format, args...) StoreLogMsg_1("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  119. #define log_error(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.06.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. log_info("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. log_info("destroy left gun, cur page = %d\r\n", getCurLcmPage());
  206. if (getCurLcmPage() == ShmSysConfigAndInfo->SysInfo.SystemPage) {
  207. log_info("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. log_info("destroy right gun, cur page = %d\r\n", getCurLcmPage());
  223. if (getCurLcmPage() == ShmSysConfigAndInfo->SysInfo.SystemPage) {
  224. log_info("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. log_error("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. log_error("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. log_error("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. log_error("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. log_error("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. log_info("%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. log_info("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. log_error("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. log_error("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. log_error("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. log_error("Primary SysConfigData checksum NG, read backup\r\n");
  792. fd = open("/dev/mtdblock11", O_RDWR);
  793. if (fd < 0) {
  794. free(buf);
  795. log_error("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. log_error("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. log_error("backup SysConfigData checksum NG, read Factory default\r\n");
  828. fd = open("/dev/mtdblock12", O_RDWR);
  829. if (fd < 0) {
  830. free(buf);
  831. log_error("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. log_error("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. log_error("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. //log_info("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. log_info("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. log_info("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. log_info("IEC model");
  1178. } else if (strcmp(_buf, "WU") == EQUAL) {
  1179. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1180. log_info("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. log_info("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. log_info("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. log_info("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. log_info("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. log_info("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. //log_info("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. log_info("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. log_info("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. log_info("CCS[%d] FW Rev = %s",
  1511. chargingInfo[index]->type_index,
  1512. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1513. ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].SelfTest_Comp = YES;
  1514. } else {
  1515. //log_info("CCS[%d] ccs fw lose...... %s \n",
  1516. // chargingInfo[index]->type_index,
  1517. // ShmCcsData->V2GMessage_DIN70121[chargingInfo[index]->type_index].version);
  1518. evInitFlag = NO;
  1519. }
  1520. }
  1521. }
  1522. }
  1523. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; index++) {
  1524. if (ac_chargingInfo[index]->Type == _Type_AC) {
  1525. if ((strlen((char *)ac_chargingInfo[index]->version) != 0 || ac_chargingInfo[index]->version[0] != '\0')
  1526. && (ac_chargingInfo[index]->SelfTest_Comp != YES)
  1527. ) {
  1528. log_info("AC connector[%d] FW Rev = %s", index, ac_chargingInfo[index]->version);
  1529. ac_chargingInfo[index]->SelfTest_Comp = YES;
  1530. } else {
  1531. evInitFlag = NO;
  1532. }
  1533. }
  1534. }
  1535. }
  1536. if ( ShmFanModuleData->SelfTest_Comp &&
  1537. ShmRelayModuleData->SelfTest_Comp &&
  1538. ShmPrimaryMcuData->SelfTest_Comp &&
  1539. //ShmLedModuleData->SelfTest_Comp &&
  1540. evInitFlag
  1541. ) {
  1542. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_AC_CONTACTOR;
  1543. }
  1544. }
  1545. break;
  1546. case _STEST_AC_CONTACTOR: {
  1547. //ShmPsuData->Work_Step = _TEST_COMPLETE;
  1548. // 因為 30KW 以下沒有 Relay feedback 功能,所以暫時先直接跳過
  1549. #if !defined DD360 && !defined DD360Audi
  1550. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES) {
  1551. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1552. log_info("AC contactor self test OK");
  1553. }
  1554. #else
  1555. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_DETECT;
  1556. log_info("Waiting for DO communication");
  1557. #endif //!defined DD360 && !defined DD360Audi
  1558. }
  1559. break;
  1560. case _STEST_PSU_DETECT: {
  1561. #if !defined DD360 && !defined DD360Audi
  1562. if (ShmPsuData->Work_Step >= GET_SYS_CAP) {
  1563. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1564. }
  1565. #else
  1566. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_PSU_CAP;
  1567. #endif //defined DD360 && !defined DD360Audi
  1568. }
  1569. break;
  1570. case _STEST_PSU_CAP: {
  1571. #if !defined DD360 && !defined DD360Audi
  1572. // 此測試是要確認當前總輸出能力
  1573. // 如果沒有 PSU 模組請 bypass
  1574. if (ShmPsuData->Work_Step == BOOTING_COMPLETE) {
  1575. sleep(1);
  1576. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1577. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1578. }
  1579. #else
  1580. //check the power limit from DO
  1581. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_COMPLETE;
  1582. ShmSysConfigAndInfo->SysInfo.BootingStatus = BOOT_COMPLETE;
  1583. log_info("Successful Self Test");
  1584. #endif //defined DD360 && !defined DD360Audi
  1585. }
  1586. break;
  1587. }
  1588. } else {
  1589. break;
  1590. }
  1591. usleep(100000);
  1592. }
  1593. }
  1594. int SpawnTask()
  1595. {
  1596. sleep(2);
  1597. system("/root/Module_EventLogging &");
  1598. system("/root/Module_PrimaryComm &");
  1599. system("/root/Module_EvComm &");
  1600. system("/root/Module_LcmControl &");
  1601. system("/root/Module_InternalComm &");
  1602. system("/root/Module_ProduceUtils &");
  1603. #if defined DD360 || defined DD360Audi
  1604. system("/root/DoComm &");
  1605. #else
  1606. system("/root/Module_PsuComm &");
  1607. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  1608. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL) {
  1609. system("/root/OcppBackend &");
  1610. }
  1611. if (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') {
  1612. system("/root/Module_4g &");
  1613. }
  1614. if (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') {
  1615. system("/root/Module_Wifi &");
  1616. }
  1617. if (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D') {
  1618. system("/root/Module_4g &");
  1619. system("/root/Module_Wifi &");
  1620. }
  1621. #endif //defined DD360 || defined DD360Audi
  1622. return PASS;
  1623. }
  1624. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1625. {
  1626. int result = PASS;
  1627. int fd, wrd;
  1628. unsigned int i, Chk;
  1629. unsigned char *ptr, *BufTmp;
  1630. Chk = 0;
  1631. ptr = (unsigned char *)UsrData;
  1632. if ((BufTmp = malloc(MtdBlockSize)) != NULL) {
  1633. memset(BufTmp, 0, MtdBlockSize);
  1634. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  1635. for (i = 0; i < MtdBlockSize - 4; i++) {
  1636. Chk += *(ptr + i);
  1637. }
  1638. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  1639. fd = open("/dev/mtdblock10", O_RDWR);
  1640. if (fd > 0) {
  1641. wrd = write(fd, BufTmp, MtdBlockSize);
  1642. close(fd);
  1643. if (wrd >= MtdBlockSize) {
  1644. fd = open("/dev/mtdblock11", O_RDWR);
  1645. if (fd > 0) {
  1646. wrd = write(fd, BufTmp, MtdBlockSize);
  1647. close(fd);
  1648. if (wrd < MtdBlockSize) {
  1649. log_error("write /dev/mtdblock11(backup) NG\r\n");
  1650. result = FAIL;
  1651. }
  1652. } else {
  1653. log_error("open /dev/mtdblock11(backup) NG\r\n");
  1654. result = FAIL;
  1655. }
  1656. } else {
  1657. log_error("write /dev/mtdblock10 NG\r\n");
  1658. result = FAIL;
  1659. }
  1660. } else {
  1661. log_error("open /dev/mtdblock10 NG\r\n");
  1662. result = FAIL;
  1663. }
  1664. } else {
  1665. log_error("alloc BlockSize NG\r\n");
  1666. result = FAIL;
  1667. }
  1668. if (BufTmp != NULL) {
  1669. free(BufTmp);
  1670. }
  1671. return result;
  1672. }
  1673. //===============================================
  1674. // Common Detect Chk - Stop Charging ?
  1675. //===============================================
  1676. int isEvBoardStopChargeFlag(byte gunIndex)
  1677. {
  1678. //printf("StopChargeFlag = %d\r\n", chargingInfo[gunIndex]->StopChargeFlag);
  1679. return chargingInfo[gunIndex]->StopChargeFlag;
  1680. }
  1681. bool isEvBoardNormalStopChargeFlag(byte gunIndex)
  1682. {
  1683. return chargingInfo[gunIndex]->UnKnowStopChargeFlag;
  1684. }
  1685. //===============================================
  1686. // 掃描插槍狀況
  1687. //===============================================
  1688. void ClearDetectPluginFlag()
  1689. {
  1690. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  1691. //DS60-120 add
  1692. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  1693. if (chargingInfo[gun_index]->RemoteStartFlag == YES) {
  1694. chargingInfo[gun_index]->RemoteStartFlag = NO;
  1695. }
  1696. }
  1697. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE) {
  1698. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1699. }
  1700. }
  1701. void DetectPluginStart()
  1702. {
  1703. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = YES;
  1704. }
  1705. bool isDetectPlugin()
  1706. {
  1707. if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES) {
  1708. return YES;
  1709. }
  1710. return NO;
  1711. }
  1712. //===============================================
  1713. // Common Detect Chk - Chademo
  1714. //===============================================
  1715. bool isEvGunLocked_chademo(byte gunIndex)
  1716. {
  1717. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0) ? NO : YES;
  1718. }
  1719. bool isEvContactorWelding_chademo(byte gunIndex)
  1720. {
  1721. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 3);
  1722. }
  1723. bool isEvStopReq_chademo(byte gunIndex)
  1724. {
  1725. return DetectBitValue(ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].EvDetection, 4);
  1726. }
  1727. bool isEvStopCharging_chademo(byte gunIndex)
  1728. {
  1729. if (isEvGunLocked_chademo(gunIndex) == NO) {
  1730. // 無鎖槍 = 停止
  1731. PRINTF_FUNC("gun locked none (%d) \n", gunIndex);
  1732. return YES;
  1733. }
  1734. return NO;
  1735. }
  1736. byte isPrechargeStatus_chademo(byte gunIndex)
  1737. {
  1738. byte result = 0x00;
  1739. result = ShmCHAdeMOData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1740. return result;
  1741. }
  1742. //===============================================
  1743. // Common Detect Chk - GB
  1744. //===============================================
  1745. bool isEvGunLocked_gb(byte gunIndex)
  1746. {
  1747. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0) ? NO : YES;
  1748. }
  1749. bool isEvStopCharging_gb(byte gunIndex)
  1750. {
  1751. if (isEvGunLocked_gb(gunIndex) == NO) {
  1752. // 無鎖槍 = 停止
  1753. PRINTF_FUNC("gun locked none. \n");
  1754. return YES;
  1755. }
  1756. return NO;
  1757. }
  1758. byte isPrechargeStatus_gb(byte gunIndex)
  1759. {
  1760. byte result = 0x00;
  1761. result = ShmGBTData->ev[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1762. return result;
  1763. }
  1764. //===============================================
  1765. // Common Detect Chk - CCS
  1766. //===============================================
  1767. bool isEvGunLocked_ccs(byte gunIndex)
  1768. {
  1769. return (DetectBitValue(chargingInfo[gunIndex]->GunLocked , 0) == 0) ? NO : YES;
  1770. }
  1771. byte isPrechargeStatus_ccs(byte gunIndex)
  1772. {
  1773. byte result = 0x00;
  1774. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121) {
  1775. result = ShmCcsData->V2GMessage_DIN70121[chargingInfo[gunIndex]->type_index].PresentMsgFlowStatus;
  1776. }
  1777. return result;
  1778. }
  1779. bool isEvStopCharging_ccs(byte gunIndex)
  1780. {
  1781. if (isEvGunLocked_ccs(gunIndex) == NO) {
  1782. // 無鎖槍 = 停止
  1783. PRINTF_FUNC("gun locked none. \n");
  1784. return YES;
  1785. }
  1786. return NO;
  1787. }
  1788. //===============================================
  1789. // Callback
  1790. //===============================================
  1791. void DisplayChargingInfo()
  1792. {
  1793. PRINTF_FUNC("*********** DisplayChargingInfo *********** \n");
  1794. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  1795. if (chargingInfo[i]->SystemStatus != S_IDLE &&
  1796. chargingInfo[i]->SystemStatus != S_RESERVATION) {
  1797. ChangeGunSelectByIndex(i);
  1798. return;
  1799. }
  1800. }
  1801. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  1802. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  1803. ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE) {
  1804. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  1805. }
  1806. usleep(50000);
  1807. #if defined DD360Audi
  1808. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  1809. #else
  1810. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1811. #endif //defined DD360Audi
  1812. }
  1813. void _AutoReturnTimeout()
  1814. {
  1815. PRINTF_FUNC("*********** _AutoReturnTimeout %d*********** \n", ShmSysConfigAndInfo->SysInfo.PageIndex);
  1816. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG) {
  1817. ClearDetectPluginFlag();
  1818. } else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP) {
  1819. DetectPluginStart();
  1820. }
  1821. usleep(50000);
  1822. #if defined DD360Audi
  1823. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  1824. #else
  1825. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1826. #endif //defined DD360Audi
  1827. }
  1828. void _SelfTestTimeout()
  1829. {
  1830. if (ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE) {
  1831. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  1832. setChargerMode(gun_index, MODE_ALARM);
  1833. }
  1834. }
  1835. ShmPsuData->Work_Step = _NO_WORKING;
  1836. ShmSysConfigAndInfo->SysInfo.SelfTestSeq = _STEST_FAIL;
  1837. PRINTF_FUNC("Self test timeout. \n");
  1838. }
  1839. void _AuthorizedTimeout()
  1840. {
  1841. if (IsAuthorizingMode()) {
  1842. PRINTF_FUNC("*********** _AuthorizedTimeout *********** \n");
  1843. isCardScan = false;
  1844. //gAudiCustInfo->PricesInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected].Balance = 0.0; //Jerry add
  1845. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  1846. //ChangeLcmByIndex(_LCM_AUTHORIZ_FAIL);
  1847. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1848. ClearAuthorizedFlag();
  1849. }
  1850. }
  1851. void _DetectPlugInTimeout()
  1852. {
  1853. PRINTF_FUNC("*********** _DetectPlugInTimeout *********** \n");
  1854. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  1855. ClearDetectPluginFlag();
  1856. //usleep(50000);
  1857. sleep(1); //Jerry add
  1858. #if defined DD360Audi
  1859. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  1860. #else
  1861. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1862. #endif //defined DD360Audi
  1863. }
  1864. void _DetectEvChargingEnableTimeout(byte gunIndex)
  1865. {
  1866. if (chargingInfo[gunIndex]->Type == _Type_Chademo) {
  1867. if (!isEvGunLocked_chademo(gunIndex)) {
  1868. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (chademo) ***********\n");
  1869. }
  1870. } else if (chargingInfo[gunIndex]->Type == _Type_GB) {
  1871. if (!isEvGunLocked_ccs(gunIndex)) {
  1872. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (gb) ***********\n");
  1873. }
  1874. } else if (chargingInfo[gunIndex]->Type == _Type_CCS_2) {
  1875. if (!isEvGunLocked_ccs(gunIndex)) {
  1876. PRINTF_FUNC("*********** _DetectEvChargingEnableTimeout (ccs) ***********\n");
  1877. }
  1878. }
  1879. ChargingTerminalProcess(gunIndex);
  1880. _AutoReturnTimeout();
  1881. }
  1882. void _DetectEvseChargingEnableTimeout(byte gunIndex)
  1883. {
  1884. PRINTF_FUNC("*********** _DetectEvseChargingEnableTimeout (GFD timeout) ***********\n");
  1885. //if (chargingInfo[gunIndex]->GroundFaultStatus != GFD_PASS)
  1886. {
  1887. setChargerMode(gunIndex, MODE_IDLE);
  1888. _AutoReturnTimeout();
  1889. }
  1890. }
  1891. void _PrepareTimeout(byte gunIndex)
  1892. {
  1893. PRINTF_FUNC("*********** _PrepareTimeout ***********\n");
  1894. setChargerMode(gunIndex, MODE_IDLE);
  1895. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  1896. _AutoReturnTimeout();
  1897. }
  1898. void _CcsPrechargeTimeout(byte gunIndex)
  1899. {
  1900. PRINTF_FUNC("*********** _CcsPrechargeTimeout ***********\n");
  1901. setChargerMode(gunIndex, MODE_IDLE);
  1902. }
  1903. //===============================================
  1904. // 取得卡號與卡號驗證
  1905. //===============================================
  1906. bool canStartCharging()
  1907. {
  1908. char buf2[16] = "";
  1909. memset(buf2, 0, ARRAY_SIZE(buf2));
  1910. for (byte index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++) {
  1911. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  1912. }
  1913. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  1914. // 因為無法得知實際的長度,所以只能用搜尋的方式
  1915. if (strcmp(buf2, "Accepted") == EQUAL) {
  1916. return true;
  1917. } else {
  1918. }
  1919. return false;
  1920. }
  1921. void AuthorizingStart()
  1922. {
  1923. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = YES;
  1924. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = YES;
  1925. }
  1926. void ClearAuthorizedFlag()
  1927. {
  1928. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  1929. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  1930. }
  1931. bool isAuthorizedComplete()
  1932. {
  1933. if (ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == YES) {
  1934. return false;
  1935. }
  1936. return true;
  1937. }
  1938. bool IsAuthorizingMode()
  1939. {
  1940. if (ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == NO) {
  1941. return false;
  1942. }
  1943. return true;
  1944. }
  1945. //===============================================
  1946. // 紀錄 Alarm Code
  1947. //===============================================
  1948. void RecordAlarmCode(byte gunIndex, char *code)
  1949. {
  1950. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, code, 6);
  1951. if (strcmp(code, "012234") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = YES; }
  1952. if (strcmp(code, "012235") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = YES; }
  1953. if (strcmp(code, "012236") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = YES; }
  1954. if (strcmp(code, "012288") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = YES; }
  1955. if (strcmp(code, "012289") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = YES; }
  1956. if (strcmp(code, "012290") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = YES; }
  1957. }
  1958. void RecordWarningCode(byte gunIndex, char *code)
  1959. {
  1960. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, code, 6);
  1961. if (strcmp(code, "012296") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = YES; }
  1962. if (strcmp(code, "012297") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = YES; }
  1963. if (strcmp(code, "012298") == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = YES; }
  1964. }
  1965. void ReleaseAlarmCode(byte gunIndex)
  1966. {
  1967. bool isCleanCheck = false;
  1968. char code[7];
  1969. if (chargingInfo[gunIndex]->Type == _Type_Chademo) {
  1970. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012234", 6) == EQUAL &&
  1971. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES) {
  1972. memcpy(code, "012234", 6);
  1973. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1974. isCleanCheck = true;
  1975. } else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012289", 6) == EQUAL &&
  1976. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail == YES) {
  1977. memcpy(code, "012289", 6);
  1978. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1979. isCleanCheck = true;
  1980. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == YES) {
  1981. ReleaseWarningCodeByString(gunIndex, "012296");
  1982. }
  1983. } else if (chargingInfo[gunIndex]->Type == _Type_GB) {
  1984. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012236", 6) == EQUAL &&
  1985. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES) {
  1986. memcpy(code, "012236", 6);
  1987. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1988. isCleanCheck = true;
  1989. } else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012290", 6) == EQUAL &&
  1990. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail == YES) {
  1991. memcpy(code, "012290", 6);
  1992. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  1993. isCleanCheck = true;
  1994. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES) {
  1995. ReleaseWarningCodeByString(gunIndex, "012298");
  1996. }
  1997. } else if (chargingInfo[gunIndex]->Type == _Type_CCS_2) {
  1998. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012235", 6) == EQUAL &&
  1999. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES) {
  2000. memcpy(code, "012235", 6);
  2001. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2002. isCleanCheck = true;
  2003. } else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "012288", 6) == EQUAL &&
  2004. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail == YES) {
  2005. memcpy(code, "012288", 6);
  2006. memcpy(chargingInfo[gunIndex]->ConnectorAlarmCode, "", 6);
  2007. isCleanCheck = true;
  2008. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning == YES) {
  2009. ReleaseWarningCodeByString(gunIndex, "012297");
  2010. }
  2011. }
  2012. if (isCleanCheck) {
  2013. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  2014. if (index != gunIndex || ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  2015. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, code, 6) != EQUAL) {
  2016. if (strncmp(code, "012234", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip = NO; }
  2017. if (strncmp(code, "012289", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoOutputUVPFail = NO; }
  2018. if (strncmp(code, "012236", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip = NO; }
  2019. if (strncmp(code, "012290", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbtOutputUVPFail = NO; }
  2020. if (strncmp(code, "012235", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip = NO; }
  2021. if (strncmp(code, "012288", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsOutputUVPFail = NO; }
  2022. }
  2023. }
  2024. }
  2025. }
  2026. }
  2027. void ReleaseWarningCodeByString(byte gunIndex, char *code)
  2028. {
  2029. bool isCleanCheck = false;
  2030. if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  2031. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning == 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.CcsGroundfaultWarning == YES) {
  2036. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  2037. isCleanCheck = true;
  2038. } else if (strncmp((char *)chargingInfo[gunIndex]->ConnectorWarningCode, code, 6) == EQUAL &&
  2039. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning == YES) {
  2040. memcpy(chargingInfo[gunIndex]->ConnectorWarningCode, "", 6);
  2041. isCleanCheck = true;
  2042. }
  2043. if (isCleanCheck) {
  2044. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  2045. if (index != gunIndex || ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  2046. if (strncmp((char *)chargingInfo[index]->ConnectorWarningCode, code, 6) != EQUAL) {
  2047. if (strncmp(code, "012296", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGroundWarning = NO; }
  2048. if (strncmp(code, "012297", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGroundfaultWarning = NO; }
  2049. if (strncmp(code, "012298", 6) == EQUAL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGroundfaultWarning = NO; }
  2050. }
  2051. }
  2052. }
  2053. }
  2054. }
  2055. //===============================================
  2056. // EmergencyStop and Charging Stop
  2057. //===============================================
  2058. void ChargingTerminalProcess(byte gunIndex)
  2059. {
  2060. setChargerMode(gunIndex, MODE_TERMINATING);
  2061. }
  2062. void ChargingAlarmProcess(byte gunIndex)
  2063. {
  2064. setChargerMode(gunIndex, MODE_ALARM);
  2065. }
  2066. void AcChargingTerminalProcess()
  2067. {
  2068. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2069. }
  2070. void StopChargingProcessByString(byte level)
  2071. {
  2072. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level) {
  2073. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2074. }
  2075. }
  2076. void ReleaseChargingProcessByString(byte level)
  2077. {
  2078. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level) {
  2079. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2080. }
  2081. }
  2082. // 一般錯誤停止充電處理函式
  2083. void BoardErrOccurByString(byte index, char *code)
  2084. {
  2085. byte level = 1;
  2086. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2087. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  2088. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO) {
  2089. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2090. }
  2091. ChargingTerminalProcess(index);
  2092. }
  2093. StopChargingProcessByString(level);
  2094. }
  2095. // 急停狀況的停止充電處理函式
  2096. void EmcOccureByString(char *code)
  2097. {
  2098. byte level = 2;
  2099. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2100. // 其錯誤等級為 2
  2101. #if defined DD360 || defined DD360Audi
  2102. //DS60-120 remove
  2103. if (strncmp(code, "012251", 6) == EQUAL ||
  2104. strncmp(code, "012252", 6) == EQUAL ||
  2105. strncmp(code, "012238", 6) == EQUAL ||
  2106. strncmp(code, "042251", 6) == EQUAL ||
  2107. strncmp(code, "042252", 6) == EQUAL ||
  2108. strncmp(code, "012304", 6) == EQUAL ||
  2109. strncmp(code, "042200", 6) == EQUAL ||
  2110. strncmp(code, "042201", 6) == EQUAL ||
  2111. strncmp(code, "042202", 6) == EQUAL ||
  2112. strncmp(code, "042267", 6) == EQUAL)
  2113. #endif //defined DD360 || defined DD360Audi
  2114. {
  2115. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++) {
  2116. //strncpy((char *)ShmOCPP16Data->StatusNotification[gun].VendorErrorCode, code, 6);
  2117. if ((chargingInfo[gun]->SystemStatus > S_IDLE &&
  2118. chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2119. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  2120. chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  2121. //ChargingTerminalProcess(gun);
  2122. ChargingAlarmProcess(gun);
  2123. }
  2124. }
  2125. #if defined DD360 || defined DD360Audi
  2126. StopChargingProcessByString(level);
  2127. InformOcppErrOccur(4);
  2128. #endif //defined DD360 || defined DD360Audi
  2129. }
  2130. }
  2131. void ReleaseBoardErrOccurByString(byte index, char *code)
  2132. {
  2133. bool isTrigger = false;
  2134. byte level = 1;
  2135. if (strncmp(code, "023730", 6) == 0 && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == YES) {
  2136. isTrigger = true;
  2137. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = NO;
  2138. }
  2139. if (isTrigger) {
  2140. ReleaseChargingProcessByString(level);
  2141. }
  2142. }
  2143. void ReleaseEmsOccureByString(byte index, char *code)
  2144. {
  2145. bool isTrigger = false;
  2146. byte level = 2;
  2147. if (strncmp(code, "042251", 6) == 0 ) {
  2148. isTrigger = true;
  2149. } else if (strncmp(code, "012251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES) {
  2150. isTrigger = true;
  2151. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2152. } else if (strncmp(code, "012252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES) {
  2153. isTrigger = true;
  2154. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2155. } else if (strncmp(code, "012237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES) {
  2156. isTrigger = true;
  2157. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2158. } else if (strncmp(code, "012238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES) {
  2159. isTrigger = true;
  2160. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2161. }
  2162. if (isTrigger) {
  2163. ReleaseChargingProcessByString(level);
  2164. InformOcppErrOccur(6);
  2165. }
  2166. }
  2167. //===============================================
  2168. // 確認硬體 (按鈕) 狀態
  2169. //===============================================
  2170. bool leftBtnPush = false;
  2171. bool rightBtnPush = false;
  2172. void ChkPrimaryStatus()
  2173. {
  2174. unsigned char Rtn;
  2175. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 0) {
  2176. Rtn = 0;
  2177. for (unsigned char i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  2178. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042251", 6) == 0) {
  2179. EmcOccureByString("042251");
  2180. Rtn = 1;
  2181. } else if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042252", 6) == 0) {
  2182. EmcOccureByString("042252");
  2183. Rtn = 1;
  2184. } else if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042200", 6) == 0) {
  2185. EmcOccureByString("042200");
  2186. Rtn = 1;
  2187. } else if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042201", 6) == 0) {
  2188. EmcOccureByString("042201");
  2189. Rtn = 1;
  2190. } else if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042202", 6) == 0) {
  2191. EmcOccureByString("042202");
  2192. Rtn = 1;
  2193. } else if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "012304", 6) == 0) {
  2194. EmcOccureByString("012304");
  2195. Rtn = 1;
  2196. }
  2197. }
  2198. if (Rtn == 0) {
  2199. ReleaseEmsOccureByString(0, "042251");
  2200. ReleaseEmsOccureByString(0, "042252");
  2201. ReleaseEmsOccureByString(0, "042200");
  2202. ReleaseEmsOccureByString(0, "042201");
  2203. ReleaseEmsOccureByString(0, "042202");
  2204. ReleaseEmsOccureByString(0, "012304");
  2205. }
  2206. } else {
  2207. ReleaseEmsOccureByString(0, "042251");
  2208. ReleaseEmsOccureByString(0, "042252");
  2209. ReleaseEmsOccureByString(0, "042200");
  2210. ReleaseEmsOccureByString(0, "042201");
  2211. ReleaseEmsOccureByString(0, "042202");
  2212. ReleaseEmsOccureByString(0, "012304");
  2213. }
  2214. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL) {
  2215. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2216. EmcOccureByString("012251");
  2217. } else {
  2218. ReleaseEmsOccureByString(0, "012251");
  2219. }
  2220. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL) {
  2221. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2222. EmcOccureByString("012238");
  2223. } else {
  2224. ReleaseEmsOccureByString(0, "012238");
  2225. }
  2226. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL) {
  2227. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2228. } else {
  2229. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2230. }
  2231. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL) {
  2232. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2233. EmcOccureByString("012252");
  2234. } else {
  2235. ReleaseEmsOccureByString(0, "012252");
  2236. }
  2237. //DS60-120 add
  2238. //if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS &&
  2239. // ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS) {
  2240. // ShmSysConfigAndInfo->SysConfig.ShowInformation = YES;
  2241. //} else {
  2242. // ShmSysConfigAndInfo->SysConfig.ShowInformation = NO;
  2243. //}
  2244. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS &&
  2245. !leftBtnPush
  2246. #if defined DD360Audi
  2247. &&
  2248. getCurLcmPage() != _LCM_AUTHORIZING &&
  2249. getCurLcmPage() != _LCM_AUTHORIZ_COMP &&
  2250. getCurLcmPage() != _LCM_AUTHORIZ_FAIL
  2251. #endif //defined DD360Audi
  2252. ) {
  2253. if (!leftBtnPush) {
  2254. leftBtnPush = true;
  2255. PRINTF_FUNC("left btn down...............................%x\n",
  2256. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus);
  2257. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE) {
  2258. switch (ac_chargingInfo[0]->SystemStatus) {
  2259. case S_IDLE: {
  2260. if (isDetectPlugin()) {
  2261. _DetectPlugInTimeout();
  2262. StopSystemTimeoutDet();
  2263. }
  2264. }
  2265. break;
  2266. case S_REASSIGN_CHECK:
  2267. case S_REASSIGN:
  2268. case S_PREPARNING:
  2269. case S_PREPARING_FOR_EV:
  2270. case S_PREPARING_FOR_EVSE:
  2271. case S_CCS_PRECHARGE_ST0:
  2272. case S_CCS_PRECHARGE_ST1: {
  2273. // 取消充電
  2274. AcChargingTerminalProcess();
  2275. }
  2276. break;
  2277. case S_CHARGING: {
  2278. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) {
  2279. // 停止充電
  2280. AcChargingTerminalProcess();
  2281. }
  2282. }
  2283. break;
  2284. case S_COMPLETE:
  2285. {}
  2286. break;
  2287. }
  2288. }
  2289. switch (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus) {
  2290. case S_IDLE: {
  2291. if (isDetectPlugin()) {
  2292. _DetectPlugInTimeout();
  2293. StopSystemTimeoutDet();
  2294. #if defined DD360Audi
  2295. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2296. #endif //defined DD360Audi
  2297. }
  2298. #if defined DD360Audi
  2299. else {
  2300. if (getConfirmSelectedGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected) == PASS) {
  2301. //printf("destroy gun = %d\r\n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2302. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2303. } else {
  2304. confirmSelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2305. PRINTF_FUNC("confirm select gun ............................... %d \n",
  2306. ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2307. }
  2308. }
  2309. #endif //defined DD360Audi
  2310. }
  2311. break;
  2312. case S_REASSIGN_CHECK:
  2313. case S_REASSIGN:
  2314. case S_PREPARNING:
  2315. case S_PREPARING_FOR_EV:
  2316. case S_PREPARING_FOR_EVSE:
  2317. case S_CCS_PRECHARGE_ST0:
  2318. case S_CCS_PRECHARGE_ST1: {
  2319. // 取消充電
  2320. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE) {
  2321. AcChargingTerminalProcess();
  2322. } else {
  2323. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2324. }
  2325. }
  2326. break;
  2327. case S_CHARGING: {
  2328. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) {
  2329. // 停止充電
  2330. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2331. }
  2332. }
  2333. break;
  2334. case S_COMPLETE: {
  2335. // 回 IDLE
  2336. //PRINTF_FUNC("right btn down.................S_COMPLETE \n");
  2337. //chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_IDLE;
  2338. }
  2339. break;
  2340. }
  2341. }
  2342. } else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE) {
  2343. if (leftBtnPush) {
  2344. leftBtnPush = false;
  2345. //PRINTF_FUNC("left btn up............................... \n");
  2346. }
  2347. }
  2348. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS &&
  2349. !rightBtnPush
  2350. #if defined DD360Audi
  2351. &&
  2352. getCurLcmPage() != _LCM_IDLE &&
  2353. getCurLcmPage() != _LCM_AUTHORIZING &&
  2354. getCurLcmPage() != _LCM_AUTHORIZ_COMP &&
  2355. getCurLcmPage() != _LCM_AUTHORIZ_FAIL &&
  2356. getCurLcmPage() != _LCM_WAIT_FOR_PLUG
  2357. #endif //defined DD360Audi
  2358. ) {
  2359. if (!rightBtnPush) {
  2360. rightBtnPush = true;
  2361. //PRINTF_FUNC("right btn down............................... %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2362. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1 < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2363. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO) {
  2364. ShmSysConfigAndInfo->SysInfo.CurGunSelected++;
  2365. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2366. } else if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0 &&
  2367. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE) {
  2368. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2369. } else if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES) {
  2370. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  2371. if (chargingInfo[_index]->SystemStatus != S_BOOTING &&
  2372. chargingInfo[_index]->SystemStatus != S_IDLE &&
  2373. chargingInfo[_index]->SystemStatus != S_RESERVATION) {
  2374. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  2375. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2376. return;
  2377. }
  2378. }
  2379. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2380. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2381. } else {
  2382. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  2383. ChangeGunSelectByIndex(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2384. }
  2385. }
  2386. PRINTF_FUNC("current select gun ............................... %d \n",
  2387. ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2388. } else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE) {
  2389. if (rightBtnPush) {
  2390. rightBtnPush = false;
  2391. //PRINTF_FUNC("right btn up............................... \n");
  2392. }
  2393. }
  2394. }
  2395. //===============================================
  2396. // 確認各小板偵測的錯誤狀況
  2397. //===============================================
  2398. void CheckErrorOccurStatus(byte index)
  2399. {
  2400. // 小板
  2401. if (chargingInfo[index]->Type == _Type_Chademo) {
  2402. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES) {
  2403. BoardErrOccurByString(index, "011012");
  2404. }
  2405. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoGfdTrip == YES)
  2406. // BoardErrOccurByString(index, "012234");
  2407. } else if (chargingInfo[index]->Type == _Type_GB) {
  2408. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault == YES) {
  2409. BoardErrOccurByString(index, "011016");
  2410. }
  2411. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.GbGfdTrip == YES)
  2412. // BoardErrOccurByString(index, "012236");
  2413. } else if (chargingInfo[index]->Type == _Type_CCS_2) {
  2414. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES) {
  2415. BoardErrOccurByString(index, "011014");
  2416. }
  2417. // else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CcsGfdTrip == YES)
  2418. // BoardErrOccurByString(index, "012235");
  2419. }
  2420. // RB
  2421. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES) {
  2422. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2423. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2424. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES) {
  2425. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  2426. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2427. StopChargingProcessByString(2);
  2428. InformOcppErrOccur(13);
  2429. }
  2430. //DS60-120 add -----
  2431. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL) {
  2432. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES) {
  2433. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012203", 6);
  2434. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES) {
  2435. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012204", 6);
  2436. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES) {
  2437. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012205", 6);
  2438. }
  2439. }
  2440. //log_info("1 CheckErrorOccurStatus\r\n");
  2441. chargingInfo[index]->StopChargeFlag = YES;
  2442. //------------------------------------------------------------------
  2443. } else {
  2444. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  2445. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2446. ReleaseChargingProcessByString(2);
  2447. InformOcppErrOccur(6);
  2448. }
  2449. }
  2450. } else {
  2451. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP) {
  2452. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2453. ReleaseChargingProcessByString(2);
  2454. InformOcppErrOccur(6);
  2455. }
  2456. }
  2457. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2458. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2459. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES) {
  2460. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE) {
  2461. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2462. StopChargingProcessByString(2);
  2463. InformOcppErrOccur(14);
  2464. }
  2465. //DS60-120 -----
  2466. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL) {
  2467. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES) {
  2468. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012200", 6);
  2469. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES) {
  2470. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012201", 6);
  2471. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES) {
  2472. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012202", 6);
  2473. }
  2474. }
  2475. //log_info("2 CheckErrorOccurStatus\r\n");
  2476. chargingInfo[index]->StopChargeFlag = YES;
  2477. //----------------------------------------------------------------------
  2478. } else {
  2479. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP) {
  2480. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2481. ReleaseChargingProcessByString(2);
  2482. InformOcppErrOccur(6);
  2483. }
  2484. }
  2485. //DS60-120 ----
  2486. if (chargingInfo[index]->Type == _Type_Chademo) {
  2487. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding == YES) {
  2488. memcpy(chargingInfo[index]->ConnectorAlarmCode, "011011", 6);
  2489. } else if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES) {
  2490. memcpy(chargingInfo[index]->ConnectorAlarmCode, "011012", 6);
  2491. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP == YES) {
  2492. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012217", 6);
  2493. }
  2494. } else if (chargingInfo[index]->Type == _Type_GB) {
  2495. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding == YES) {
  2496. memcpy(chargingInfo[index]->ConnectorAlarmCode, "011015", 6);
  2497. } else if (ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault == YES) {
  2498. memcpy(chargingInfo[index]->ConnectorAlarmCode, "011016", 6);
  2499. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP == YES) {
  2500. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012221", 6);
  2501. }
  2502. } else if (chargingInfo[index]->Type == _Type_CCS_2) {
  2503. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding == YES) {
  2504. memcpy(chargingInfo[index]->ConnectorAlarmCode, "011013", 6);
  2505. } else if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES) {
  2506. memcpy(chargingInfo[index]->ConnectorAlarmCode, "011014", 6);
  2507. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP == YES) {
  2508. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012219", 6);
  2509. }
  2510. }
  2511. //--------------------------------------------------------------------------
  2512. //Primary
  2513. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES) {
  2514. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012251", 6);
  2515. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES) {
  2516. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012252", 6);
  2517. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES) {
  2518. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012238", 6);
  2519. } else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo == YES) {
  2520. memcpy(chargingInfo[index]->ConnectorAlarmCode, "012304", 6);
  2521. }
  2522. }
  2523. //===============================================
  2524. // 確認 GPIO 狀態
  2525. //===============================================
  2526. void gpio_set_value(unsigned int gpio, unsigned int value)
  2527. {
  2528. int fd;
  2529. char buf[MAX_BUF];
  2530. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2531. fd = open(buf, O_WRONLY);
  2532. if (fd < 0) {
  2533. perror("gpio/set-value");
  2534. return;
  2535. }
  2536. if (value) {
  2537. write(fd, "1", 2);
  2538. } else {
  2539. write(fd, "0", 2);
  2540. }
  2541. close(fd);
  2542. }
  2543. int gpio_get_value(unsigned int gpio, unsigned int *value)
  2544. {
  2545. int fd;
  2546. char buf[MAX_BUF];
  2547. char ch;
  2548. snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  2549. fd = open(buf, O_RDONLY);
  2550. if (fd < 0) {
  2551. perror("gpio/get-value");
  2552. return fd;
  2553. }
  2554. read(fd, &ch, 1);
  2555. if (ch != '0') {
  2556. *value = 1;
  2557. } else {
  2558. *value = 0;
  2559. }
  2560. close(fd);
  2561. return 0;
  2562. }
  2563. void CheckGunTypeFromHw()
  2564. {
  2565. uint8_t i = 0;
  2566. int pinIn[4] = {22, 23, 44, 45};
  2567. unsigned int gpioValue = 0;
  2568. unsigned char tmp[2] = {0};
  2569. log_info("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  2570. for (i = 0; i < ARRAY_SIZE(pinIn); i++) {
  2571. gpio_get_value(pinIn[i], &gpioValue);
  2572. switch (pinIn[i]) {
  2573. //right slot
  2574. case 22:
  2575. bd1_1_status = gpioValue;
  2576. break;
  2577. case 23:
  2578. bd1_2_status = gpioValue;
  2579. break;
  2580. //left slot
  2581. case 44:
  2582. bd0_1_status = gpioValue;
  2583. break;
  2584. case 45:
  2585. bd0_2_status = gpioValue;
  2586. break;
  2587. }
  2588. }
  2589. #if 1 //DS60-120 remove
  2590. //BD1(Left-CCS-CND1-SMR2-左槍), BD2(Right-CHADEMO-CND2-SMR1-右槍), CCS: 10 , CHAdeMO: 01 , GBT: 11
  2591. //CcsChargingData [0至1] 分別為 Right至Left
  2592. //model name 槍順序左至右分別為Right至Left
  2593. tmp[1] = (bd0_1_status << 4 | bd0_2_status);
  2594. tmp[0] = (bd1_1_status << 4 | bd1_2_status);
  2595. for (i = 0; i < 2; i++) {
  2596. switch (tmp[i]) {
  2597. case 0x01:
  2598. log_info("BD%d(%s) = %s ", i + 1, i == 1 ? "Left" : "Right", "CHAdeMO");
  2599. break;
  2600. case 0x10:
  2601. log_info("BD%d(%s) = %s ", i + 1, i == 1 ? "Left" : "Right", "CCS");
  2602. break;
  2603. case 0x11:
  2604. log_info("BD%d(%s) = %s ", i + 1, i == 1 ? "Left" : "Right", "GBT");
  2605. break;
  2606. case 0x00:
  2607. log_info("BD%d(%s) = %s ", i + 1, i == 1 ? "Left" : "Right", "None");
  2608. break;
  2609. }
  2610. }
  2611. #endif //0
  2612. }
  2613. void CheckGpioInStatus()
  2614. {
  2615. int pinIn[2] = { 27, 47 };//{IO BD1_2, IO BD2_2}
  2616. unsigned int gpioValue = 0;
  2617. for (int i = 0; i < ARRAY_SIZE(pinIn); i++) {
  2618. gpio_get_value(pinIn[i], &gpioValue);
  2619. if (gpioValue == 0x01) {
  2620. switch (pinIn[i]) {
  2621. // 小板緊急停止
  2622. case 47: {
  2623. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  2624. if (chargingInfo[i]->slotsIndex == 1) {
  2625. if (chargingInfo[i]->Type == _Type_Chademo) {
  2626. BoardErrOccurByString(i, "023730");
  2627. } else if (chargingInfo[i]->Type == _Type_CCS_2) {
  2628. BoardErrOccurByString(i, "013627");
  2629. }
  2630. break;
  2631. }
  2632. }
  2633. }
  2634. break;
  2635. case 27: {
  2636. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  2637. if (chargingInfo[i]->slotsIndex == 3) {
  2638. if (chargingInfo[i]->Type == _Type_Chademo) {
  2639. BoardErrOccurByString(i, "023730");
  2640. } else if (chargingInfo[i]->Type == _Type_CCS_2) {
  2641. BoardErrOccurByString(i, "013627");
  2642. }
  2643. break;
  2644. }
  2645. }
  2646. }
  2647. break;
  2648. }
  2649. } else {
  2650. switch (pinIn[i]) {
  2651. // 小板解除緊急停止
  2652. case 47: {
  2653. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  2654. if (chargingInfo[i]->slotsIndex == 1) {
  2655. if (chargingInfo[i]->Type == _Type_Chademo) {
  2656. ReleaseBoardErrOccurByString(i, "023730");
  2657. } else if (chargingInfo[i]->Type == _Type_CCS_2) {
  2658. ReleaseBoardErrOccurByString(i, "013627");
  2659. }
  2660. break;
  2661. }
  2662. }
  2663. }
  2664. break;
  2665. case 27: {
  2666. for (int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  2667. if (chargingInfo[i]->slotsIndex == 3) {
  2668. if (chargingInfo[i]->Type == _Type_Chademo) {
  2669. ReleaseBoardErrOccurByString(i, "023730");
  2670. } else if (chargingInfo[i]->Type == _Type_CCS_2) {
  2671. ReleaseBoardErrOccurByString(i, "013627");
  2672. }
  2673. break;
  2674. }
  2675. }
  2676. }
  2677. break;
  2678. }
  2679. }
  2680. }
  2681. }
  2682. //===============================================
  2683. // Main process
  2684. //===============================================
  2685. // 檢查 Byte 中某個 Bit 的值
  2686. // _byte : 欲改變的 byte
  2687. // _bit : 該 byte 的第幾個 bit
  2688. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  2689. {
  2690. return ( _byte & mask_table[_bit] ) != 0x00;
  2691. }
  2692. // 設定 Byte 中某個 Bit的值
  2693. // _byte : 欲改變的 byte
  2694. // _bit : 該 byte 的第幾個 bit
  2695. // value : 修改的值為 0 or 1
  2696. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  2697. {
  2698. if (value == 1) {
  2699. *_byte |= (1 << _bit);
  2700. } else if (value == 0) {
  2701. *_byte ^= (1 << _bit);
  2702. }
  2703. }
  2704. void UserScanFunction()
  2705. {
  2706. bool idleReq = false;
  2707. unsigned char stopReq = 255;
  2708. // 當前非驗證的狀態
  2709. if (!IsAuthorizingMode()) {
  2710. #if defined DD360Audi
  2711. //當前沒有選槍
  2712. if (getConfirmSelectedGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected) == FAIL) { //Jerry add
  2713. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2714. return;
  2715. }
  2716. #endif //defined DD360Audi
  2717. // 先判斷現在是否可以提供刷卡
  2718. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  2719. // 2. 停止充電
  2720. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX) {
  2721. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2722. return;
  2723. }
  2724. for (byte i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++) {
  2725. if (chargingInfo[i]->SystemStatus == S_CHARGING) {
  2726. stopReq = i;
  2727. }
  2728. if ((chargingInfo[i]->SystemStatus == S_IDLE && chargingInfo[i]->IsAvailable) == YES ||
  2729. (_acgunIndex > 0 && ac_chargingInfo[0]->SystemStatus == S_IDLE && ac_chargingInfo[0]->IsAvailable)) {
  2730. idleReq = true;
  2731. }
  2732. }
  2733. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  2734. ac_chargingInfo[0]->SystemStatus == S_CHARGING) {
  2735. stopReq = DEFAULT_AC_INDEX;
  2736. }
  2737. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) > 0) {
  2738. if (_acgunIndex > 0 && stopReq == DEFAULT_AC_INDEX && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  2739. char value[32];
  2740. PRINTF_FUNC("ac stop charging \n");
  2741. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc,
  2742. ac_chargingInfo[0]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2743. memcpy(value, (unsigned char *)ac_chargingInfo[0]->StartUserId,
  2744. ARRAY_SIZE(ac_chargingInfo[0]->StartUserId));
  2745. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL) {
  2746. AcChargingTerminalProcess();
  2747. }
  2748. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2749. } else if (stopReq < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount &&
  2750. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_CHARGING &&
  2751. (_acgunIndex <= 0 || (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE))) {
  2752. char value[32];
  2753. PRINTF_FUNC("stop charging \n");
  2754. PRINTF_FUNC("index = %d, card number = %s, UserId = %s \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected,
  2755. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId, ShmSysConfigAndInfo->SysConfig.UserId);
  2756. memcpy(value, (unsigned char *)chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId,
  2757. ARRAY_SIZE(chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->StartUserId));
  2758. // 同一張卡直接停掉
  2759. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.UserId, value) == EQUAL) {
  2760. ChargingTerminalProcess(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  2761. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2762. } else {
  2763. // 進驗證
  2764. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  2765. _authorizeIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc;
  2766. } else {
  2767. _authorizeIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  2768. }
  2769. #if !defined DD360 && !defined DD360Audi
  2770. StartSystemTimeoutDet(Timeout_AuthorizingForStop);
  2771. AuthorizingStart();
  2772. #else
  2773. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2774. #endif //!defined DD360 && !defined DD360Audi
  2775. }
  2776. } else if (idleReq) {
  2777. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 &&
  2778. stopReq != 255 &&
  2779. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES) {
  2780. idleReq = false;
  2781. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2782. } else if ((_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  2783. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_IDLE) {
  2784. PRINTF_FUNC("// LCM => Authorizing \n");
  2785. #if defined DD360Audi
  2786. setSelGunWaitToAuthor(ShmSysConfigAndInfo->SysInfo.CurGunSelected); //Jerry add
  2787. #endif //defined DD360Audi
  2788. // LCM => Authorizing
  2789. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZING;
  2790. // 進入確認卡號狀態
  2791. AuthorizingStart();
  2792. } else {
  2793. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2794. }
  2795. } else {
  2796. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2797. }
  2798. }
  2799. } else {
  2800. // 透過後臺停止充電的判斷
  2801. if (isAuthorizedComplete()
  2802. #if !defined DD360 && !defined DD360Audi
  2803. || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  2804. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2805. #endif //!defined DD360 && !defined DD360Audi
  2806. ) {
  2807. // 判斷後台回覆狀態
  2808. if (canStartCharging()
  2809. #if !defined DD360 && !defined DD360Audi
  2810. || (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO &&
  2811. ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING)
  2812. #endif //!defined DD360 && !defined DD360Audi
  2813. ) {
  2814. if (_authorizeIndex != NO_DEFINE) {
  2815. // 先找 AC
  2816. if (_authorizeIndex == DEFAULT_AC_INDEX) {
  2817. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  2818. strcmp((char *)chargingInfo[_authorizeIndex]->StartUserId, "") != EQUAL) {
  2819. AcChargingTerminalProcess();
  2820. }
  2821. } else {
  2822. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  2823. strcmp((char *)chargingInfo[_authorizeIndex]->StartUserId, "") != EQUAL) {
  2824. ChargingTerminalProcess(_authorizeIndex);
  2825. }
  2826. }
  2827. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2828. _authorizeIndex = NO_DEFINE;
  2829. }
  2830. } else {
  2831. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2832. }
  2833. ClearAuthorizedFlag();
  2834. } else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST) {
  2835. // 白名單驗證
  2836. for (int i = 0; i < 10; i++) {
  2837. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL) {
  2838. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL) {
  2839. ChargingTerminalProcess(_authorizeIndex);
  2840. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2841. ClearAuthorizedFlag();
  2842. break;
  2843. }
  2844. }
  2845. }
  2846. }
  2847. }
  2848. }
  2849. unsigned char isModeChange(unsigned char gun_index)
  2850. {
  2851. unsigned char result = NO;
  2852. if (chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus) {
  2853. result = YES;
  2854. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  2855. }
  2856. return result;
  2857. }
  2858. void ScannerCardProcess()
  2859. {
  2860. if (!isDetectPlugin() && !isCardScan && ShmSysConfigAndInfo->SysWarningInfo.Level != 2 &&
  2861. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) {
  2862. isCardScan = true;
  2863. // 處理刷卡及驗證卡號的動作
  2864. UserScanFunction();
  2865. }
  2866. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZING) {
  2867. StartSystemTimeoutDet(Timeout_Authorizing);
  2868. // 確認驗證卡號完成沒
  2869. if (isAuthorizedComplete()
  2870. #if !defined DD360 && !defined DD360Audi
  2871. || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING
  2872. #endif //!defined DD360 && !defined DD360Audi
  2873. ) {
  2874. StopSystemTimeoutDet();
  2875. // 判斷後台回覆狀態
  2876. if (canStartCharging()
  2877. #if !defined DD360 && !defined DD360Audi
  2878. || ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING
  2879. #endif //!defined DD360 && !defined DD360Audi
  2880. ) {
  2881. // LCM => Authorize complete
  2882. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2883. } else {
  2884. // LCM => Authorize fail
  2885. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_FAIL;
  2886. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2887. }
  2888. ClearAuthorizedFlag();
  2889. } else if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST) {
  2890. // 白名單驗證
  2891. for (int i = 0; i < 10; i++) {
  2892. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], "") != EQUAL) {
  2893. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[i], (char *)ShmSysConfigAndInfo->SysConfig.UserId) == EQUAL) {
  2894. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_AUTHORIZ_COMP;
  2895. ClearAuthorizedFlag();
  2896. break;
  2897. }
  2898. }
  2899. }
  2900. }
  2901. } else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_FAIL) {
  2902. StartSystemTimeoutDet(Timeout_VerifyFail);
  2903. isCardScan = false;
  2904. } else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_AUTHORIZ_COMP) {
  2905. StartSystemTimeoutDet(Timeout_VerifyComp);
  2906. } else if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_WAIT_FOR_PLUG) {
  2907. StartSystemTimeoutDet(Timeout_WaitPlug);
  2908. } else {
  2909. isCardScan = false;
  2910. }
  2911. }
  2912. bool AddGunInfoByConnector(byte typeValue, byte slots)
  2913. {
  2914. bool result = true;
  2915. switch (typeValue) {
  2916. case '0': // none
  2917. break;
  2918. case '1': // IEC 62196-2 Type 1/SAE J1772 Plug
  2919. break;
  2920. case '2': // IEC 62196-2 Type 1/SAE J1772 Socket
  2921. break;
  2922. case '3': // IEC 62196-2 Type 2 Plug
  2923. case '4': // IEC 62196-2 Type 2 Socket
  2924. if (AC_QUANTITY > _ac_Index) {
  2925. ac_chargingInfo[_acgunIndex] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[_ac_Index];
  2926. // AC 固定 index
  2927. ac_chargingInfo[_acgunIndex]->Index = 0;
  2928. ac_chargingInfo[_acgunIndex]->ReservationId = -1;
  2929. ac_chargingInfo[_acgunIndex]->SystemStatus = S_IDLE;
  2930. ac_chargingInfo[_acgunIndex]->Type = _Type_AC;
  2931. ac_chargingInfo[_acgunIndex]->IsAvailable = YES;
  2932. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO; //DS60-120 add
  2933. ac_chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO; //DS60-120 add
  2934. _ac_Index++;
  2935. _acgunIndex++;
  2936. } else {
  2937. result = false;
  2938. }
  2939. break;
  2940. case '5': // GB/T AC Plug
  2941. break;
  2942. case '6': // GB/T AC Socket
  2943. break;
  2944. case 'J': { // CHAdeMO
  2945. if (CHAdeMO_QUANTITY > _chademoIndex) {
  2946. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[_chademoIndex];
  2947. chargingInfo[_gunIndex]->Index = _gunIndex;
  2948. chargingInfo[_gunIndex]->ReservationId = -1;
  2949. chargingInfo[_gunIndex]->slotsIndex = slots;
  2950. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2951. chargingInfo[_gunIndex]->Type = _Type_Chademo;
  2952. chargingInfo[_gunIndex]->type_index = _chademoIndex;
  2953. chargingInfo[_gunIndex]->IsAvailable = YES;
  2954. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO; //DS60-120 add
  2955. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO; //DS60-120 add
  2956. _chademoIndex++;
  2957. _gunIndex++;
  2958. } else {
  2959. result = false;
  2960. }
  2961. }
  2962. break;
  2963. case 'U': // CCS1 combo
  2964. case 'E': // CCS2 combo
  2965. case 'V': // Liquid CCS1 combo
  2966. case 'F': { // Liquid CCS2 combo
  2967. if (CCS_QUANTITY > _ccsIndex) {
  2968. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[_ccsIndex];
  2969. chargingInfo[_gunIndex]->Index = _gunIndex;
  2970. chargingInfo[_gunIndex]->ReservationId = -1;
  2971. chargingInfo[_gunIndex]->slotsIndex = slots;
  2972. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2973. chargingInfo[_gunIndex]->Type = _Type_CCS_2;
  2974. chargingInfo[_gunIndex]->type_index = _ccsIndex;
  2975. chargingInfo[_gunIndex]->IsAvailable = YES;
  2976. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;//DS60-120 add
  2977. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO; //DS60-120 add
  2978. // 現階段預設為走 DIN70121
  2979. ShmCcsData->CommProtocol = _CCS_COMM_V2GMessage_DIN70121;
  2980. _ccsIndex++;
  2981. _gunIndex++;
  2982. } else {
  2983. result = false;
  2984. }
  2985. }
  2986. break;
  2987. case 'G': { // GBT DC
  2988. if (GB_QUANTITY > _gb_Index) {
  2989. chargingInfo[_gunIndex] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[_gb_Index];
  2990. chargingInfo[_gunIndex]->Index = _gunIndex;
  2991. chargingInfo[_gunIndex]->ReservationId = -1;
  2992. chargingInfo[_gunIndex]->slotsIndex = slots;
  2993. chargingInfo[_gunIndex]->SystemStatus = S_BOOTING;
  2994. chargingInfo[_gunIndex]->Type = _Type_GB;
  2995. chargingInfo[_gunIndex]->type_index = _gb_Index;
  2996. chargingInfo[_gunIndex]->IsAvailable = YES;
  2997. chargingInfo[_acgunIndex]->schedule.isTriggerStart = NO;//DS60-120 add
  2998. chargingInfo[_acgunIndex]->schedule.isTriggerStop = NO; //DS60-120 add
  2999. _gb_Index++;
  3000. _gunIndex++;
  3001. } else {
  3002. result = false;
  3003. }
  3004. }
  3005. break;
  3006. case 'D': // GBT DC x 2
  3007. break;
  3008. }
  3009. return result;
  3010. }
  3011. bool CheckConnectorTypeStatus()
  3012. {
  3013. bool result = true;
  3014. //PRINTF_FUNC("bd0_1_status = %d, bd0_2_status = %d, bd1_1_status = %d, bd1_2_status = %d \n",
  3015. // bd0_1_status,
  3016. // bd0_2_status,
  3017. // bd1_1_status,
  3018. // bd1_2_status);
  3019. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 9) {
  3020. //printf("1 CheckConnectorTypeStatus\r\n");
  3021. byte slots = 1;
  3022. for (byte typeIndex = 7; typeIndex <= 9; typeIndex++) {
  3023. if (!AddGunInfoByConnector(ShmSysConfigAndInfo->SysConfig.ModelName[typeIndex], slots)) {
  3024. return false;
  3025. }
  3026. slots++;
  3027. }
  3028. // AC index 接在 DC 後面
  3029. //if (AC_QUANTITY > 0) {
  3030. if (_ac_Index > 0) { //DS60-120 add
  3031. ac_chargingInfo[0]->Index += _gunIndex;
  3032. }
  3033. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  3034. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = _acgunIndex;
  3035. log_info("DC connector Quality = %d, AC connector Quality = %d",
  3036. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  3037. ShmSysConfigAndInfo->SysConfig.AcConnectorCount);
  3038. log_info("Type 0~3 = CHAdeMO, CCS, GB, AC");
  3039. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0 &&
  3040. ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 0) {
  3041. result = false;
  3042. }
  3043. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  3044. //DS60-120 add
  3045. bool isFind = false;
  3046. if (chargingInfo[0]->Type == _Type_Chademo) {
  3047. if ((bd0_1_status == 0 && bd0_2_status == 1) ||
  3048. (bd1_1_status == 0 && bd1_2_status == 1)) {
  3049. isFind = true;
  3050. }
  3051. } else if (chargingInfo[0]->Type == _Type_CCS_2) {
  3052. if ((bd0_1_status == 1 && bd0_2_status == 0) ||
  3053. (bd1_1_status == 1 && bd1_2_status == 0)) {
  3054. isFind = true;
  3055. }
  3056. } else if (chargingInfo[0]->Type == _Type_GB) {
  3057. if ((bd0_1_status == 1 && bd0_2_status == 1) ||
  3058. (bd1_1_status == 1 && bd1_2_status == 1)) {
  3059. isFind = true;
  3060. }
  3061. }
  3062. if (isFind) {
  3063. if (bd0_1_status == 1 || bd0_2_status == 1) {
  3064. chargingInfo[0]->Evboard_id = 0x01;
  3065. }
  3066. if (bd1_1_status == 1 || bd1_2_status == 1) {
  3067. chargingInfo[0]->Evboard_id = 0x02;
  3068. }
  3069. CheckHwSlotStatusLog(0);
  3070. } else {
  3071. result = false;
  3072. }
  3073. //chargingInfo[0]->Evboard_id = 0x01;
  3074. log_info("index = %d, Type = %d, Evboard_id = %d", 0, chargingInfo[0]->Type, chargingInfo[0]->Evboard_id);
  3075. } else {
  3076. char type = NO_DEFINE; //DS60-120 add
  3077. // 偵測槍屬於哪個 slot : 可知道插在板上的Slot 0 或 1 是 Chademo 還是 CCS
  3078. for (byte gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++) {
  3079. if (gunIndex == 0 && bd0_1_status == 0 && bd0_2_status == 1) {
  3080. // 與硬體相同 type : Chademo
  3081. if (chargingInfo[gunIndex]->Type == _Type_Chademo) {
  3082. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3083. }
  3084. } else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 0) {
  3085. // 與硬體相同 type : CCS
  3086. if (chargingInfo[gunIndex]->Type == _Type_CCS_2) {
  3087. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3088. }
  3089. } else if (gunIndex == 0 && bd0_1_status == 1 && bd0_2_status == 1) {
  3090. // 與硬體相同 type : GB
  3091. if (chargingInfo[gunIndex]->Type == _Type_GB) {
  3092. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3093. }
  3094. }
  3095. if (gunIndex == 1 && bd1_1_status == 0 && bd1_2_status == 1) {
  3096. // 與硬體相同 type : Chademo
  3097. if (chargingInfo[gunIndex]->Type == _Type_Chademo) {
  3098. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3099. }
  3100. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  3101. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3102. }
  3103. } else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 0) {
  3104. // 與硬體相同 type : CCS
  3105. if (chargingInfo[gunIndex]->Type == _Type_CCS_2) {
  3106. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3107. }
  3108. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  3109. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3110. }
  3111. } else if (gunIndex == 1 && bd1_1_status == 1 && bd1_2_status == 1) {
  3112. // 與硬體相同 type : GB
  3113. if (chargingInfo[gunIndex]->Type == _Type_GB) {
  3114. chargingInfo[gunIndex]->Evboard_id = 0x02;
  3115. }
  3116. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 1) {
  3117. chargingInfo[gunIndex]->Evboard_id = 0x01;
  3118. }
  3119. }
  3120. log_info("index = %d, Type = %d, Evboard_id = %d",
  3121. gunIndex,
  3122. chargingInfo[gunIndex]->Type,
  3123. chargingInfo[gunIndex]->Evboard_id);
  3124. CheckHwSlotStatusLog(gunIndex);
  3125. if (type == NO_DEFINE) {
  3126. type = chargingInfo[gunIndex]->Type;
  3127. }
  3128. #if defined METER_ENABLE
  3129. else {
  3130. // 雙槍都是同一個 type
  3131. if (type == chargingInfo[gunIndex]->Type) {
  3132. ShmDcCommonData->SysGunAreSameType = YES;
  3133. }
  3134. }
  3135. #endif //METER_ENABLE
  3136. if (chargingInfo[gunIndex]->Evboard_id == 0x00) {
  3137. result = false;
  3138. }
  3139. }
  3140. }
  3141. } else {
  3142. // Module Name 不正確 - 告警
  3143. result = false;
  3144. }
  3145. return result;
  3146. }
  3147. void KillTask()
  3148. {
  3149. ChangeLcmByIndex(_LCM_FIX);
  3150. system("killall Module_EventLogging");
  3151. system("killall Module_PrimaryComm");
  3152. system("killall Module_EvComm");
  3153. system("killall Module_LcmControl");
  3154. system("killall Module_InternalComm");
  3155. //system("killall DoComm");
  3156. #if !defined DD360 && !defined DD360Audi
  3157. system("killall Module_PsuComm");
  3158. system("killall OcppBackend &");
  3159. system("killall Module_4g &");
  3160. system("killall Module_Wifi &");
  3161. #endif //!defined DD360 && !defined DD360Audi
  3162. }
  3163. void KillTaskExceptPrimary()
  3164. {
  3165. ChangeLcmByIndex(_LCM_FIX);
  3166. system("killall Module_EvComm");
  3167. system("killall Module_InternalComm");
  3168. #if !defined DD360 && !defined DD360Audi
  3169. system("killall Module_PsuComm");
  3170. system("killall Module_4g &");
  3171. system("killall Module_Wifi &");
  3172. #endif //!defined DD360 && !defined DD360Audi
  3173. }
  3174. void KillAllTask()
  3175. {
  3176. ChangeLcmByIndex(_LCM_FIX);
  3177. system("killall Module_EventLogging");
  3178. system("killall Module_PrimaryComm");
  3179. system("killall Module_EvComm");
  3180. system("killall Module_LcmControl");
  3181. system("killall Module_InternalComm");
  3182. #if !defined DD360 && !defined DD360Audi
  3183. system("killall Module_PsuComm");
  3184. system("killall OcppBackend &");
  3185. system("killall Module_4g &");
  3186. system("killall Module_Wifi &");
  3187. #else
  3188. system("killall DoComm");
  3189. #endif //!defined DD360 && !defined DD360Audi
  3190. }
  3191. int CheckUpdateProcess(void)
  3192. {
  3193. //bool isPass = true;
  3194. uint8_t retSucc = 0;
  3195. uint8_t retFail = 0;
  3196. byte index = 0;
  3197. byte target = 0;
  3198. char *new_str = NULL;
  3199. unsigned char *ptr = NULL;
  3200. int fd = 0;
  3201. int CanFd = 0;
  3202. int uartFd = 0;
  3203. unsigned int Type = 0;
  3204. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  3205. DIR *d;
  3206. struct dirent *dir;
  3207. d = opendir("/mnt/");
  3208. if (d) {
  3209. while ((dir = readdir(d)) != NULL) {
  3210. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  3211. continue;
  3212. }
  3213. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  3214. //new_str[0] = '\0';
  3215. strcat(new_str, "/mnt/");
  3216. strcat(new_str, dir->d_name);
  3217. log_info("%s%s\r\n", "/mnt/", dir->d_name);
  3218. fd = open(new_str, O_RDONLY);
  3219. if (fd < 0) {
  3220. return FAIL;
  3221. }
  3222. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  3223. //memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  3224. //get the image length
  3225. ImageLen = read(fd, ptr, MaxLen);
  3226. for (byte i = 0; i < 16; i++) {
  3227. if (ShmSysConfigAndInfo->SysConfig.ModelName[i] != ptr[i]) {
  3228. return FAIL;
  3229. }
  3230. }
  3231. PRINTF_FUNC("model name check pass. \n");
  3232. if (ImageLen > 20) {
  3233. Type = (((unsigned int)ptr[16]) << 24 |
  3234. ((unsigned int)ptr[17]) << 16 |
  3235. ((unsigned int)ptr[18]) << 8 |
  3236. ((unsigned int)ptr[19]));
  3237. PRINTF_FUNC("Typed...%x \r\n", Type);
  3238. switch (Type) {
  3239. case 0x10000001:
  3240. case 0x10000002:
  3241. case 0x10000003:
  3242. case 0x10000004:
  3243. case 0x10000005:
  3244. if (Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS) {
  3245. //return PASS;
  3246. retSucc++;
  3247. } else {
  3248. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  3249. //return FAIL;
  3250. retFail++;
  3251. }
  3252. break;
  3253. case 0x10000007:
  3254. case 0x10000008:
  3255. case 0x10000009:
  3256. case 0x1000000A:
  3257. //isPass = true;
  3258. CanFd = InitCanBus();
  3259. if (CanFd > 0) {
  3260. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  3261. //if (!isPass) {
  3262. // break;
  3263. //}
  3264. if (chargingInfo[index]->Type == _Type_CCS_2) {
  3265. if (Upgrade_CCS(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == FAIL) {
  3266. //isPass = false;
  3267. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  3268. retFail++;
  3269. }
  3270. }
  3271. }
  3272. } else {
  3273. log_error("Upgrade CCS open CAN FD fail.\n");
  3274. //isPass = false;
  3275. return FAIL;
  3276. }
  3277. if (retFail != 0) {
  3278. break;
  3279. } else {
  3280. retSucc++;
  3281. }
  3282. //return isPass;
  3283. break;
  3284. case 0x10000006:
  3285. case 0x1000000D:
  3286. case 0x1000000E:
  3287. case 0x20000002:
  3288. case 0x10000014:
  3289. // CSU_PRIMARY_CONTROLLER : 0x10000006
  3290. target = 0x00;
  3291. if (Type == 0x10000006) {
  3292. target = UPGRADE_PRI;
  3293. } else if (Type == 0x1000000D) {
  3294. target = UPGRADE_RB;
  3295. } else if (Type == 0x1000000E) {
  3296. target = UPGRADE_FAN;
  3297. } else if (Type == 0x20000002) {
  3298. target = UPGRADE_AC;
  3299. } else if (Type == 0x10000014) {
  3300. target = UPGRADE_LED;
  3301. }
  3302. uartFd = InitComPort(target);
  3303. if (Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS) {
  3304. //return PASS;
  3305. retSucc++;
  3306. } else {
  3307. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  3308. //return FAIL;
  3309. return FAIL;
  3310. }
  3311. if (uartFd > 0) {
  3312. close(uartFd);
  3313. }
  3314. break;
  3315. case 0x1000000B:
  3316. case 0x1000000C:
  3317. // CHAdeMO_BOARD : 0x1000000B, GBT : 0x1000000C
  3318. //bool isPass = true;
  3319. CanFd = InitCanBus();
  3320. if (CanFd > 0) {
  3321. for (index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  3322. //if (!isPass) {
  3323. // break;
  3324. //}
  3325. if ((Type == 0x1000000B && chargingInfo[index]->Type == _Type_Chademo) ||
  3326. (Type == 0x1000000C && chargingInfo[index]->Type == _Type_GB)) {
  3327. if (Upgrade_CAN(CanFd, Type, chargingInfo[index]->Evboard_id, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == PASS) {
  3328. //isPass = PASS;
  3329. retSucc++;
  3330. } else {
  3331. PRINTF_FUNC("Upgrade %x Failed\r\n", Type);
  3332. //isPass = FAIL;
  3333. retFail++;
  3334. }
  3335. }
  3336. }
  3337. } else {
  3338. PRINTF_FUNC("Upgrad FD fail. \n");
  3339. //isPass = false;
  3340. return FAIL;
  3341. }
  3342. //return isPass;
  3343. break;
  3344. }
  3345. }
  3346. free(new_str);
  3347. free(ptr);
  3348. }
  3349. }
  3350. free(dir);
  3351. closedir(d);
  3352. if (retFail != 0) {
  3353. return FAIL;
  3354. }
  3355. return PASS;
  3356. }
  3357. void CreateRfidFork()
  3358. {
  3359. pid_t rfidRecPid;
  3360. rfidRecPid = fork();
  3361. log_info("CreateRfidFork = %d\r\n", rfidRecPid);
  3362. if (rfidRecPid == 0) {
  3363. while (true) {
  3364. // 刷卡判斷
  3365. RFID rfid;
  3366. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  3367. !ShmSysConfigAndInfo->SysConfig.isRFID)
  3368. {}
  3369. else if (getRequestCardSN(rfidFd, 0, &rfid)) {
  3370. //log_info("Get Card..-%s- \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3371. if (strlen((char *)ShmSysConfigAndInfo->SysConfig.UserId) == 0) {
  3372. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_LITTLE) {
  3373. switch (rfid.snType) {
  3374. case RFID_SN_TYPE_6BYTE:
  3375. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3376. "%02X%02X%02X%02X%02X%02X",
  3377. rfid.currentCard[0], rfid.currentCard[1],
  3378. rfid.currentCard[2], rfid.currentCard[3],
  3379. rfid.currentCard[4], rfid.currentCard[5]);
  3380. break;
  3381. case RFID_SN_TYPE_7BYTE:
  3382. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3383. "%02X%02X%02X%02X%02X%02X%02X",
  3384. rfid.currentCard[0], rfid.currentCard[1],
  3385. rfid.currentCard[2], rfid.currentCard[3],
  3386. rfid.currentCard[4], rfid.currentCard[5],
  3387. rfid.currentCard[6]);
  3388. break;
  3389. case RFID_SN_TYPE_10BYTE:
  3390. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3391. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3392. rfid.currentCard[0], rfid.currentCard[1],
  3393. rfid.currentCard[2], rfid.currentCard[3],
  3394. rfid.currentCard[4], rfid.currentCard[5],
  3395. rfid.currentCard[6], rfid.currentCard[7],
  3396. rfid.currentCard[8], rfid.currentCard[9]);
  3397. break;
  3398. case RFID_SN_TYPE_4BYTE:
  3399. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3400. "%02X%02X%02X%02X",
  3401. rfid.currentCard[0], rfid.currentCard[1],
  3402. rfid.currentCard[2], rfid.currentCard[3]);
  3403. break;
  3404. }
  3405. } else if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian == RFID_ENDIAN_BIG) {
  3406. switch (rfid.snType) {
  3407. case RFID_SN_TYPE_6BYTE:
  3408. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3409. "%02X%02X%02X%02X%02X%02X",
  3410. rfid.currentCard[5], rfid.currentCard[4],
  3411. rfid.currentCard[3], rfid.currentCard[2],
  3412. rfid.currentCard[1], rfid.currentCard[0]);
  3413. break;
  3414. case RFID_SN_TYPE_7BYTE:
  3415. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3416. "%02X%02X%02X%02X%02X%02X%02X",
  3417. rfid.currentCard[6], rfid.currentCard[5],
  3418. rfid.currentCard[4], rfid.currentCard[3],
  3419. rfid.currentCard[2], rfid.currentCard[1],
  3420. rfid.currentCard[0]);
  3421. break;
  3422. case RFID_SN_TYPE_10BYTE:
  3423. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3424. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  3425. rfid.currentCard[9], rfid.currentCard[8],
  3426. rfid.currentCard[7], rfid.currentCard[6],
  3427. rfid.currentCard[5], rfid.currentCard[4],
  3428. rfid.currentCard[3], rfid.currentCard[2],
  3429. rfid.currentCard[1], rfid.currentCard[0]);
  3430. break;
  3431. case RFID_SN_TYPE_4BYTE:
  3432. sprintf((char *) ShmSysConfigAndInfo->SysConfig.UserId,
  3433. "%02X%02X%02X%02X",
  3434. rfid.currentCard[3], rfid.currentCard[2],
  3435. rfid.currentCard[1], rfid.currentCard[0]);
  3436. break;
  3437. }
  3438. }
  3439. log_info("card number = %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3440. }
  3441. }
  3442. usleep(500000);
  3443. }
  3444. }
  3445. }
  3446. void StartSystemTimeoutDet(unsigned char flag)
  3447. {
  3448. if (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != flag) {
  3449. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3450. }
  3451. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = flag;
  3452. }
  3453. void StopSystemTimeoutDet()
  3454. {
  3455. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  3456. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  3457. }
  3458. void StartGunInfoTimeoutDet(unsigned char gunIndex, unsigned char flag)
  3459. {
  3460. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount) {
  3461. if (chargingInfo[gunIndex]->TimeoutFlag != flag) {
  3462. gettimeofday(&chargingInfo[gunIndex]->TimeoutTimer, NULL);
  3463. }
  3464. chargingInfo[gunIndex]->TimeoutFlag = flag;
  3465. }
  3466. }
  3467. void StopGunInfoTimeoutDet(unsigned char gunIndex)
  3468. {
  3469. if (gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount) {
  3470. chargingInfo[gunIndex]->TimeoutFlag = Timeout_None;
  3471. }
  3472. }
  3473. void CheckConnectionTimeout()
  3474. {
  3475. if (gAudiCustInfo->RemoteSetup.ConnectionTimeout != 0) { //Jerry add
  3476. _connectionTimeout = gAudiCustInfo->RemoteSetup.ConnectionTimeout;
  3477. } else {
  3478. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3479. }
  3480. #if 0
  3481. if (system("pidof -s OcppBackend > /dev/null") != 0) {
  3482. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3483. } else {
  3484. if (strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData, "") != 0) {
  3485. _connectionTimeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3486. if (_connectionTimeout <= 0) {
  3487. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3488. }
  3489. } else {
  3490. _connectionTimeout = CONN_PLUG_TIME_OUT;
  3491. }
  3492. }
  3493. #endif //0
  3494. }
  3495. void CreateTimeoutFork()
  3496. {
  3497. pid_t timeoutPid;
  3498. timeoutPid = fork();
  3499. log_info("CreateTimeoutFork = %d\r\n", timeoutPid);
  3500. if (timeoutPid == 0) {
  3501. gettimeofday(&_cmdSubPriority_time, NULL);
  3502. CheckConnectionTimeout();
  3503. while (1) {
  3504. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000) {
  3505. CheckConnectionTimeout();
  3506. gettimeofday(&_cmdSubPriority_time, NULL);
  3507. }
  3508. //printf("Timeout ***********SystemTimeoutFlag = %d, ********\n", ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag);
  3509. // 系統
  3510. switch (ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag) {
  3511. case Timeout_SelftestChk:
  3512. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= SELFTEST_TIMEOUT) {
  3513. _SelfTestTimeout();
  3514. StopSystemTimeoutDet();
  3515. #if defined DD360Audi
  3516. destroySelGun(DESTROY_ALL_SEL); //jerry add
  3517. #endif //defined DD360Audi
  3518. }
  3519. break;
  3520. case Timeout_Authorizing:
  3521. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_TIMEOUT) {
  3522. _AuthorizedTimeout();
  3523. StopSystemTimeoutDet();
  3524. #if defined DD360Audi
  3525. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3526. #endif //defined DD360Audi
  3527. }
  3528. break;
  3529. case Timeout_VerifyFail:
  3530. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_FAIL_TIMEOUT) {
  3531. _AutoReturnTimeout();
  3532. StopSystemTimeoutDet();
  3533. #if defined DD360Audi
  3534. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3535. #endif //defined DD360Audi
  3536. }
  3537. break;
  3538. case Timeout_VerifyComp:
  3539. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_COMP_TIMEOUT) {
  3540. _AutoReturnTimeout();
  3541. StopSystemTimeoutDet();
  3542. }
  3543. break;
  3544. case Timeout_WaitPlug:
  3545. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= _connectionTimeout) {
  3546. _DetectPlugInTimeout();
  3547. StopSystemTimeoutDet();
  3548. #if defined DD360Audi
  3549. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  3550. #endif //defined DD360Audi
  3551. }
  3552. break;
  3553. case Timeout_ReturnToChargingGunDet: {
  3554. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= RETURN_TO_CHARGING_PAGE) {
  3555. #if defined DD360Audi
  3556. if (getCurLcmPage() != _LCM_PRE_CHARGE &&
  3557. getCurLcmPage() != _LCM_CHARGING &&
  3558. getCurLcmPage() != _LCM_COMPLETE) {
  3559. destroySelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected); //jerry add
  3560. }
  3561. #endif //defined DD360Audi
  3562. DisplayChargingInfo();
  3563. StopSystemTimeoutDet();
  3564. }
  3565. }
  3566. break;
  3567. case Timeout_AuthorizingForStop: {
  3568. if (GetTimeoutValue(ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer) / uSEC_VAL >= AUTHORIZE_STOP_TIMEOUT) {
  3569. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  3570. ClearAuthorizedFlag();
  3571. StopSystemTimeoutDet();
  3572. }
  3573. }
  3574. break;
  3575. }
  3576. // 各槍
  3577. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  3578. //printf("Timeout ***********TimeoutFlag = %d, ********\n", chargingInfo[gun_index]->TimeoutFlag);
  3579. switch (chargingInfo[gun_index]->TimeoutFlag) {
  3580. case Timeout_Preparing: {
  3581. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PREPARE_TIMEOUT) {
  3582. _PrepareTimeout(gun_index);
  3583. StopGunInfoTimeoutDet(gun_index);
  3584. #if defined DD360Audi
  3585. destroySelGun(gun_index); //jerry add
  3586. #endif //defined DD360Audi
  3587. }
  3588. }
  3589. break;
  3590. case Timeout_EvChargingDet: {
  3591. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EV_WAIT_TIMEOUT) {
  3592. _DetectEvChargingEnableTimeout(gun_index);
  3593. StopGunInfoTimeoutDet(gun_index);
  3594. #if defined DD360Audi
  3595. destroySelGun(gun_index); //jerry add
  3596. #endif //defined DD360Audi
  3597. }
  3598. }
  3599. break;
  3600. case Timeout_EvseChargingDet: {
  3601. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_EVSE_WAIT_TIMEOUT) {
  3602. _DetectEvseChargingEnableTimeout(gun_index);
  3603. StopGunInfoTimeoutDet(gun_index);
  3604. #if defined DD360Audi
  3605. destroySelGun(gun_index); //jerry add
  3606. #endif //defined DD360Audi
  3607. }
  3608. }
  3609. break;
  3610. case Timeout_EvseCompleteDet: {
  3611. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_COMP_WAIT_TIMEOUT) {
  3612. StopGunInfoTimeoutDet(gun_index);
  3613. #if defined DD360Audi
  3614. destroySelGun(gun_index); //jerry add
  3615. #endif //defined DD360Audi
  3616. }
  3617. }
  3618. break;
  3619. case Timeout_ForCcsPrechargeDet: {
  3620. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= GUN_PRECHARGING_TIMEOUT) {
  3621. _CcsPrechargeTimeout(gun_index);
  3622. StopGunInfoTimeoutDet(gun_index);
  3623. #if defined DD360Audi
  3624. destroySelGun(gun_index); //jerry add
  3625. #endif //defined DD360Audi
  3626. }
  3627. }
  3628. break;
  3629. #if defined DD360Audi
  3630. case Timeout_SelectGun: //Jerry add
  3631. if (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) / uSEC_VAL >= SEL_GUN_TIMEOUT) {
  3632. log_info("Timeout_SelectGun\r\n");
  3633. StopGunInfoTimeoutDet(gun_index);
  3634. destroySelGun(gun_index); //jerry add
  3635. }
  3636. break;
  3637. #endif //defined DD360Audi
  3638. }
  3639. }
  3640. sleep(1);
  3641. }
  3642. }
  3643. }
  3644. void GetSystemTime()
  3645. {
  3646. struct timeb csuTime;
  3647. struct tm *tmCSU;
  3648. ftime(&csuTime);
  3649. tmCSU = localtime(&csuTime.time);
  3650. PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  3651. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  3652. tmCSU->tm_sec);
  3653. // byte date[14];
  3654. //
  3655. //
  3656. // //sprintf(&date, "%d", );
  3657. //
  3658. // date[0] = '0' + ((tmCSU->tm_year + 1900) / 1000 % 10);
  3659. // date[0] = (tmCSU->tm_year + 1900) / 1000 % 10;
  3660. // date[1] = (tmCSU->tm_year + 1900) / 100 % 10;
  3661. // date[2] = (tmCSU->tm_year + 1900) / 10 % 10;
  3662. // date[3] = (tmCSU->tm_year + 1900) / 1 % 10;
  3663. //
  3664. // date[4] = (tmCSU->tm_mon + 1) / 10 % 10;
  3665. // date[5] = (tmCSU->tm_mon + 1) / 1 % 10;
  3666. //
  3667. // date[6] = (tmCSU->tm_mday) / 10 % 10;
  3668. // date[7] = (tmCSU->tm_mday) / 1 % 10;
  3669. //
  3670. // date[8] = (tmCSU->tm_hour) / 10 % 10;
  3671. // date[9] = (tmCSU->tm_hour) / 1 % 10;
  3672. //
  3673. // date[10] = (tmCSU->tm_min) / 10 % 10;
  3674. // date[11] = (tmCSU->tm_min) / 1 % 10;
  3675. //
  3676. // date[12] = (tmCSU->tm_sec) / 10 % 10;
  3677. // date[13] = (tmCSU->tm_sec) / 1 % 10;
  3678. // 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],
  3679. // date[4], date[5], date[6], date[7],
  3680. // date[8], date[9], date[10], date[11],
  3681. // date[12], date[13]);
  3682. }
  3683. void CheckFactoryConfigFunction()
  3684. {
  3685. char Buf[256] = {0};
  3686. if (ShmSysConfigAndInfo->SysInfo.FactoryConfiguration) {
  3687. sprintf(Buf, "cd /root;./FactoryConfig -m %s %s",
  3688. ShmSysConfigAndInfo->SysConfig.ModelName,
  3689. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  3690. system(Buf);
  3691. system("rm -f /Storage/OCPP/OCPPConfiguration");
  3692. system("sync");
  3693. sleep(5);
  3694. system("reboot -f");
  3695. sleep(5);
  3696. system("reboot -f");
  3697. }
  3698. }
  3699. void CheckFwUpdateFunction()
  3700. {
  3701. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = %d \n", ShmSysConfigAndInfo->SysInfo.FirmwareUpdate);
  3702. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  3703. log_info("ftp : update start. \n");
  3704. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  3705. setChargerMode(gun_index, MODE_UPDATE);
  3706. }
  3707. byte updateResult = CheckUpdateProcess();
  3708. if (updateResult == PASS) {
  3709. log_info("ftp : update complete. \n");
  3710. } else if (updateResult == MODELNAME_FAIL) {
  3711. log_info("ftp : model name is none match. \n");
  3712. KillAllTask();
  3713. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  3714. sleep(5);
  3715. system("/usr/bin/run_evse_restart.sh");
  3716. return;
  3717. } else {
  3718. log_info("ftp : update fail. \n");
  3719. }
  3720. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  3721. sleep(5);
  3722. system("reboot -f");
  3723. } else if (ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES) {
  3724. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = NO;
  3725. if (strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == EQUAL) {
  3726. log_info("Backend : update start. \n");
  3727. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "");
  3728. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3729. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3730. KillTask();
  3731. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  3732. setChargerMode(_index, MODE_UPDATE);
  3733. }
  3734. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; _index++) {
  3735. ac_chargingInfo[_index]->SystemStatus = MODE_UPDATE;
  3736. }
  3737. byte updateResult = CheckUpdateProcess();
  3738. if (updateResult == PASS) {
  3739. log_info("Backend : update complete. \n");
  3740. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3741. } else if (updateResult == MODELNAME_FAIL) {
  3742. log_info("Backend : model name is none match. \n");
  3743. KillAllTask();
  3744. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  3745. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3746. sleep(5);
  3747. system("/usr/bin/run_evse_restart.sh");
  3748. return;
  3749. } else {
  3750. log_info("Backend : update fail. \n");
  3751. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  3752. }
  3753. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3754. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3755. sleep(5);
  3756. system("reboot -f");
  3757. }
  3758. }
  3759. }
  3760. //===============================================
  3761. // Check reservation date is expired
  3762. //===============================================
  3763. int isReservationExpired(unsigned char gun_index)
  3764. {
  3765. int result = NO;
  3766. struct tm expiredDate;
  3767. struct timeb expiredTime;
  3768. if (sscanf((char *) ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate,
  3769. "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year,
  3770. &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour,
  3771. &expiredDate.tm_min, &expiredDate.tm_sec) == 6) {
  3772. expiredDate.tm_year -= 1900;
  3773. expiredDate.tm_mon -= 1;
  3774. expiredTime.time = mktime(&expiredDate);
  3775. if (!CheckTimeOut(expiredTime)) {
  3776. result = YES;
  3777. }
  3778. }
  3779. return result;
  3780. }
  3781. //===============================================
  3782. // OCPP
  3783. //===============================================
  3784. void CheckOcppStatus()
  3785. {
  3786. if (ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES) {
  3787. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = NO;
  3788. }
  3789. if (ShmOCPP16Data->MsMsg.bits.ResetReq == YES) {
  3790. bool canReset = true;
  3791. bool canHardReset = true; //DS60-120 add
  3792. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2) {
  3793. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  3794. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  3795. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  3796. chargingInfo[_index]->SystemStatus != S_MAINTAIN) {
  3797. canReset = false;
  3798. if (chargingInfo[_index]->SystemStatus >= S_REASSIGN &&
  3799. chargingInfo[_index]->SystemStatus < S_TERMINATING) {
  3800. canHardReset = false;
  3801. ChargingTerminalProcess(_index);
  3802. restartFlag = 1;
  3803. }
  3804. }
  3805. }
  3806. }
  3807. if (canReset) {
  3808. ShmOCPP16Data->MsMsg.bits.ResetReq = NO;
  3809. sprintf((char *)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3810. if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Hard") == EQUAL) {
  3811. log_error("****** Hard Reboot ****** \n");
  3812. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  3813. sleep(3);
  3814. system("reboot -f");
  3815. } else if (strcmp((char *)ShmOCPP16Data->Reset.Type, "Soft") == EQUAL) {
  3816. log_error("****** Soft Reboot ****** \n");
  3817. ShmOCPP16Data->MsMsg.bits.ResetConf = YES;
  3818. sleep(3);
  3819. system("killall OcppBackend &");
  3820. KillAllTask();
  3821. system("/usr/bin/run_evse_restart.sh");
  3822. }
  3823. }
  3824. }
  3825. }
  3826. void OcppStartTransation(byte gunIndex)
  3827. {
  3828. byte _OcppGunIndex = gunIndex;
  3829. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  3830. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1) {
  3831. _OcppGunIndex = 2;
  3832. }
  3833. if (strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL) {
  3834. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  3835. } else {
  3836. strcpy((char *)ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  3837. }
  3838. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StartTransaction[_OcppGunIndex].IdTag);
  3839. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StartTransactionReq = YES;
  3840. }
  3841. void OcppStopTransation(byte gunIndex)
  3842. {
  3843. byte _OcppGunIndex = gunIndex;
  3844. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  3845. if (ShmSysConfigAndInfo->SysConfig.AcConnectorCount == 1 && gunIndex == 1) {
  3846. _OcppGunIndex = 2;
  3847. }
  3848. if (strcmp((char *)chargingInfo[gunIndex]->StartUserId, "") == EQUAL) {
  3849. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  3850. } else {
  3851. strcpy((char *)ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag, (char *)chargingInfo[gunIndex]->StartUserId);
  3852. }
  3853. PRINTF_FUNC("IdTag = %s \n", ShmOCPP16Data->StopTransaction[_OcppGunIndex].IdTag);
  3854. ShmOCPP16Data->CpMsg.bits[_OcppGunIndex].StopTransactionReq = YES;
  3855. }
  3856. bool OcppRemoteStop(byte gunIndex)
  3857. {
  3858. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  3859. // 有 AC 槍的話
  3860. if (acDirIndex > 0 && gunIndex > 0) {
  3861. gunIndex += acDirIndex;
  3862. }
  3863. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  3864. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES) {
  3865. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  3866. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  3867. }
  3868. return result;
  3869. }
  3870. bool WifiScheduleStop(byte gunIndex)
  3871. {
  3872. bool result = false;
  3873. result = chargingInfo[gunIndex]->schedule.isTriggerStop;
  3874. chargingInfo[gunIndex]->schedule.isTriggerStop = NO;
  3875. return result;
  3876. }
  3877. void OcppRemoteStartChk()
  3878. {
  3879. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING)
  3880. {}
  3881. else if (!isDetectPlugin()) {
  3882. // 如果有 AC 槍,則固定是第 2 把槍,所以索引固定為 1
  3883. byte acDirIndex = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  3884. for (byte ac_index = 0; ac_index < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; ac_index++) {
  3885. if (ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq == YES) {
  3886. if (ac_chargingInfo[ac_index]->SystemStatus == S_IDLE ||
  3887. ac_chargingInfo[ac_index]->SystemStatus == S_RESERVATION) {
  3888. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  3889. ac_chargingInfo[ac_index]->RemoteStartFlag = YES;
  3890. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3891. //ShmSysConfigAndInfo->SysInfo.OrderCharging = DEFAULT_AC_INDEX;
  3892. ShmOCPP16Data->CsMsg.bits[ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ac_index].RemoteStartTransactionReq = NO;
  3893. DetectPluginStart();
  3894. return;
  3895. }
  3896. ShmOCPP16Data->CsMsg.bits[acDirIndex].RemoteStartTransactionReq = NO;
  3897. }
  3898. }
  3899. byte threeGunIndex = 0;
  3900. byte dcIndex = 0;
  3901. bool isGunUsingStatus = false;
  3902. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  3903. // 如果有 AC 槍,且 DC 槍也有兩把
  3904. if (acDirIndex == 1 && _index == 1) {
  3905. threeGunIndex = 1;
  3906. }
  3907. if (ShmOCPP16Data->CsMsg.bits[_index + threeGunIndex].RemoteStartTransactionReq == YES) {
  3908. dcIndex = _index;
  3909. }
  3910. if (chargingInfo[_index]->SystemStatus != S_IDLE) {
  3911. isGunUsingStatus = true;
  3912. }
  3913. }
  3914. // 如果是雙槍單模,只認閒置狀態的槍,如果有預約~ 則預約也算被使用
  3915. if (isGunUsingStatus && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf) {
  3916. if (dcIndex == 0) {
  3917. threeGunIndex = 0;
  3918. }
  3919. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  3920. return;
  3921. }
  3922. if (dcIndex == 0) {
  3923. threeGunIndex = 0;
  3924. }
  3925. if (ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq == YES) {
  3926. if (chargingInfo[dcIndex]->SystemStatus == S_IDLE ||
  3927. chargingInfo[dcIndex]->SystemStatus == S_RESERVATION) {
  3928. chargingInfo[dcIndex]->RemoteStartFlag = YES;
  3929. ShmSysConfigAndInfo->SysInfo.OrderCharging = YES;
  3930. //ShmSysConfigAndInfo->SysInfo.OrderCharging = gun_index;
  3931. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  3932. DetectPluginStart();
  3933. #if defined DD360Audi
  3934. setSelGunWaitToAuthor(dcIndex); //Jerry add
  3935. #endif //defined DD360Audi
  3936. }
  3937. ShmOCPP16Data->CsMsg.bits[dcIndex + threeGunIndex].RemoteStartTransactionReq = NO;
  3938. }
  3939. }
  3940. }
  3941. void ChkOcppStatus(byte gunIndex)
  3942. {
  3943. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  3944. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq == YES) {
  3945. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowReq = NO;
  3946. if (isReservationExpired(gunIndex)) {
  3947. PRINTF_FUNC("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  3948. log_error("***************ChkOcppStatus : OcppReservedStatus******************** \n");
  3949. chargingInfo[gunIndex]->ReservationId = ShmOCPP16Data->ReserveNow[gunIndex].ReservationId;
  3950. chargingInfo[gunIndex]->SystemStatus = S_RESERVATION;
  3951. }
  3952. ShmOCPP16Data->CsMsg.bits[gunIndex].ReserveNowConf = YES;
  3953. }
  3954. if (chargingInfo[gunIndex]->SystemStatus == S_RESERVATION &&
  3955. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq == YES) {
  3956. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationReq = NO;
  3957. if (isReservationExpired(gunIndex)) {
  3958. PRINTF_FUNC("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  3959. log_error("***************ChkOcppStatus : Cancel OcppReservedStatus******************** \n");
  3960. chargingInfo[gunIndex]->ReservationId = 0;
  3961. chargingInfo[gunIndex]->SystemStatus = S_IDLE;
  3962. }
  3963. ShmOCPP16Data->CsMsg.bits[gunIndex].CancelReservationConf = YES;
  3964. }
  3965. if (ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq == YES) {
  3966. PRINTF_FUNC("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  3967. log_error("***************ChkOcppStatus : OcppChangeAvailability******************** \n");
  3968. ShmOCPP16Data->CsMsg.bits[gunIndex].ChangeAvailabilityReq = NO;
  3969. if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Operative") == EQUAL) {
  3970. if (isDb_ready) {
  3971. DB_Update_Operactive(localDb, gunIndex, true);
  3972. }
  3973. chargingInfo[gunIndex]->IsAvailable = YES;
  3974. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  3975. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  3976. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN) {
  3977. setChargerMode(gunIndex, MODE_IDLE);
  3978. }
  3979. } else if (strcmp((char *)ShmOCPP16Data->ChangeAvailability[gunIndex].Type, "Inoperative") == EQUAL) {
  3980. if (isDb_ready) {
  3981. DB_Update_Operactive(localDb, gunIndex, false);
  3982. }
  3983. chargingInfo[gunIndex]->IsAvailable = NO;
  3984. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  3985. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  3986. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN) {
  3987. setChargerMode(gunIndex, MODE_MAINTAIN);
  3988. }
  3989. }
  3990. }
  3991. if (ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq == YES) {
  3992. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorReq = NO;
  3993. if (chargingInfo[gunIndex]->SystemStatus >= S_REASSIGN_CHECK && // DS60-120 add ||
  3994. chargingInfo[gunIndex]->SystemStatus <= S_CHARGING) {
  3995. // 充電中,需停止充電
  3996. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "UnlockCommand");
  3997. ChargingTerminalProcess(gunIndex);
  3998. }
  3999. strcpy((char *)ShmOCPP16Data->UnlockConnector[gunIndex].ResponseStatus, "Unlocked");
  4000. ShmOCPP16Data->CsMsg.bits[gunIndex].UnlockConnectorConf = YES;
  4001. }
  4002. }
  4003. bool CheckBackendChargingTimeout(byte gunIndex)
  4004. {
  4005. bool result = false;
  4006. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) {
  4007. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) {
  4008. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60)) {
  4009. result = true;
  4010. }
  4011. }
  4012. } else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) {
  4013. // 隨插即充電的要看 offline
  4014. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) {
  4015. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60)) {
  4016. result = true;
  4017. }
  4018. }
  4019. }
  4020. return result;
  4021. }
  4022. bool CheckBackendChargingEnergy(byte gunIndex)
  4023. {
  4024. bool result = false;
  4025. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) {
  4026. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) {
  4027. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy) {
  4028. result = true;
  4029. }
  4030. }
  4031. } else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) {
  4032. // 隨插即充電的要看 offline
  4033. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) {
  4034. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) {
  4035. result = true;
  4036. }
  4037. }
  4038. }
  4039. return result;
  4040. }
  4041. void InformOcppErrOccur(byte codeType)
  4042. {
  4043. char _error[25];
  4044. switch (codeType) {
  4045. case 4: strcpy(_error, "InternalError"); break;
  4046. case 6: strcpy(_error, "NoError"); break;
  4047. case 7: strcpy(_error, "OtherError"); break;
  4048. case 13: strcpy(_error, "UnderVoltage"); break;
  4049. case 14: strcpy(_error, "OverVoltage"); break;
  4050. }
  4051. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  4052. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, _error);
  4053. }
  4054. }
  4055. //===============================================
  4056. // SQLite3 related routine
  4057. //===============================================
  4058. int DB_Open(sqlite3 *db)
  4059. {
  4060. int result = PASS;
  4061. char *errMsg = NULL;
  4062. char *createRecordSql = "CREATE TABLE IF NOT EXISTS charging_record("
  4063. "idx integer primary key AUTOINCREMENT, "
  4064. "reservationId text, "
  4065. "transactionId text, "
  4066. "startMethod text, "
  4067. "userId text, "
  4068. "dateTimeStart text, "
  4069. "dateTimeStop text,"
  4070. "socStart text, "
  4071. "socStop text, "
  4072. "chargeEnergy text, "
  4073. "stopReason text"
  4074. ");";
  4075. /*char *createCfgSql = "CREATE TABLE IF NOT EXISTS `config` ( "
  4076. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  4077. "`IsAvailable` TEXT NOT NULL, "
  4078. "`connector` INTEGER NOT NULL, "
  4079. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  4080. */
  4081. //DS60-120 add
  4082. char *createCfgSql = "CREATE TABLE IF NOT EXISTS `config` ( "
  4083. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  4084. "`item` TEXT NOT NULL, "
  4085. "`connector` INTEGER NOT NULL, "
  4086. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  4087. char *createrecordSql = "CREATE TABLE IF NOT EXISTS `event_record` ( "
  4088. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  4089. "`occurDatetime` TEXT NOT NULL, "
  4090. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  4091. char *createrebootSql = "CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  4092. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  4093. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  4094. if (sqlite3_open(DB_FILE, &db)) {
  4095. result = FAIL;
  4096. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4097. sqlite3_close(db);
  4098. } else {
  4099. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  4100. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK) {
  4101. result = FAIL;
  4102. PRINTF_FUNC( "Create local charging record table error message: %s\n", errMsg);
  4103. } else {
  4104. PRINTF_FUNC( "Opened local charging record table successfully\n");
  4105. }
  4106. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK) {
  4107. result = FAIL;
  4108. PRINTF_FUNC( "Create local config table error message: %s\n", errMsg);
  4109. } else {
  4110. PRINTF_FUNC( "Opened local config table successfully\n");
  4111. }
  4112. //DS60-120
  4113. if (sqlite3_exec(db, createrecordSql, 0, 0, &errMsg) != SQLITE_OK) {
  4114. result = FAIL;
  4115. PRINTF_FUNC( "Create local record table error message: %s\n", errMsg);
  4116. } else {
  4117. PRINTF_FUNC( "Opened local record table successfully\n");
  4118. }
  4119. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK) {
  4120. result = FAIL;
  4121. PRINTF_FUNC( "Create reboot record table error message: %s\n", errMsg);
  4122. } else {
  4123. PRINTF_FUNC( "Opened reboot record table successfully\n");
  4124. }
  4125. //-----
  4126. sqlite3_close(db);
  4127. }
  4128. return result;
  4129. }
  4130. int DB_Insert_Record(sqlite3 *db, int gun_index)
  4131. {
  4132. int result = PASS;
  4133. char *errMsg = NULL;
  4134. char insertSql[1024];
  4135. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  4136. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  4137. chargingInfo[gun_index]->ReservationId, //DS60-120 add
  4138. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  4139. chargingInfo[gun_index]->StartMethod,
  4140. chargingInfo[gun_index]->StartUserId,
  4141. chargingInfo[gun_index]->StartDateTime,
  4142. chargingInfo[gun_index]->StopDateTime,
  4143. chargingInfo[gun_index]->EvBatteryStartSoc,
  4144. chargingInfo[gun_index]->EvBatterySoc,
  4145. chargingInfo[gun_index]->PresentChargedEnergy,
  4146. //DS60-120 remove
  4147. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  4148. //ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  4149. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  4150. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  4151. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  4152. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  4153. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  4154. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  4155. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  4156. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4157. //if (sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db)) {
  4158. if (sqlite3_open(DB_FILE, &db)) { //DS60-120 add
  4159. result = FAIL;
  4160. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4161. sqlite3_close(db);
  4162. } else {
  4163. PRINTF_FUNC( "Local charging record database open successfully.\r\n");
  4164. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK) {
  4165. result = FAIL;
  4166. PRINTF_FUNC( "Insert local charging record error message: %s\n", errMsg);
  4167. } else {
  4168. PRINTF_FUNC( "Insert local charging record successfully\n");
  4169. }
  4170. //DS60-120 add
  4171. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  4172. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK) {
  4173. result = FAIL;
  4174. PRINTF_FUNC( "delete local charging error message: %s\n", errMsg);
  4175. } else {
  4176. PRINTF_FUNC( "delete local charging record successfully\n");
  4177. }
  4178. sqlite3_close(db);
  4179. }
  4180. return result;
  4181. }
  4182. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  4183. {
  4184. uint8_t result = false;
  4185. char *errMsg = NULL;
  4186. char sqlStr[1024];
  4187. srand(time(NULL));
  4188. if (sqlite3_open(DB_FILE, &db)) {
  4189. result = FAIL;
  4190. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4191. sqlite3_close(db);
  4192. } else {
  4193. PRINTF_FUNC( "Local charging record database open successfully (%d).\r\n", IsAvailable);
  4194. //sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  4195. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable); //DS60-120 add
  4196. PRINTF_FUNC("sqlStr= %s\r\n", sqlStr);
  4197. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK) {
  4198. result = FAIL;
  4199. PRINTF_FUNC( "update config error message: %s\n", errMsg);
  4200. } else {
  4201. PRINTF_FUNC("update connector-%d config item isOperactive to %d\r\n", gun_index, IsAvailable);
  4202. }
  4203. sqlite3_close(db);
  4204. }
  4205. return result;
  4206. }
  4207. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  4208. {
  4209. uint8_t result = true;
  4210. char *errMsg = NULL;
  4211. char sqlStr[1024];
  4212. char **rs;
  4213. int rows, cols;
  4214. //sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  4215. sprintf(sqlStr, "select * from config where item='IsAvailable' and connector=%d;", gun_index); //DS60-120 add
  4216. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  4217. if (sqlite3_open(DB_FILE, &db)) {
  4218. result = FAIL;
  4219. PRINTF_FUNC( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  4220. sqlite3_close(db);
  4221. } else {
  4222. PRINTF_FUNC( "Local config query database open successfully.\r\n");
  4223. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  4224. if (rows > 0) {
  4225. for (int idxRow = 1; idxRow <= rows; idxRow++) {
  4226. if (strcmp(rs[(idxRow * cols) + 3], "0") == 0) {
  4227. result = false;
  4228. }
  4229. PRINTF_FUNC("Query connector-%d isOperactive: %s\r\n", gun_index, rs[(idxRow * cols) + 3]);
  4230. }
  4231. } else {
  4232. PRINTF_FUNC("Query connector-%d fail, set default value to operactive.\r\n", gun_index);
  4233. }
  4234. sqlite3_free_table(rs);
  4235. sqlite3_close(db);
  4236. }
  4237. return result;
  4238. }
  4239. int DB_Reboot_Record(sqlite3 *db)
  4240. {
  4241. int result = PASS;
  4242. char *errMsg = NULL;
  4243. char insertSql[256];
  4244. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  4245. if (sqlite3_open(DB_FILE, &db)) {
  4246. result = FAIL;
  4247. PRINTF_FUNC( "Can't open database: %s\n", sqlite3_errmsg(db));
  4248. sqlite3_close(db);
  4249. } else {
  4250. PRINTF_FUNC( "Local charging record database open successfully.\n");
  4251. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK) {
  4252. result = FAIL;
  4253. PRINTF_FUNC( "Insert reboot record error message: %s\n", errMsg);
  4254. } else {
  4255. PRINTF_FUNC( "Insert reboot record successfully\n");
  4256. }
  4257. sprintf(insertSql, "delete from reboot_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  4258. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK) {
  4259. result = FAIL;
  4260. PRINTF_FUNC( "delete reboot record error message: %s\n", errMsg);
  4261. } else {
  4262. PRINTF_FUNC( "delete reboot record successfully\n");
  4263. }
  4264. sqlite3_close(db);
  4265. }
  4266. return result;
  4267. }
  4268. //===============================================
  4269. // Config process
  4270. //===============================================
  4271. void AddPlugInTimes(byte gunIndex)
  4272. {
  4273. if (chargingInfo[gunIndex]->Type == _Type_Chademo) {
  4274. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  4275. } else if (chargingInfo[gunIndex]->Type == _Type_CCS_2) {
  4276. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  4277. } else if (chargingInfo[gunIndex]->Type == _Type_GB) {
  4278. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  4279. }
  4280. }
  4281. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  4282. {
  4283. char cmdBuf[32];
  4284. struct timeb csuTime;
  4285. struct tm *tmCSU;
  4286. ftime(&csuTime);
  4287. tmCSU = localtime(&csuTime.time);
  4288. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  4289. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4290. tmCSU->tm_sec);
  4291. if (isStart) {
  4292. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  4293. } else {
  4294. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  4295. }
  4296. }
  4297. void zipLogFiles()
  4298. {
  4299. const char *logPath = "/Storage/SystemLog";
  4300. // 獲取目錄
  4301. DIR *pDir = opendir(logPath);
  4302. if (pDir != NULL) {
  4303. struct timeb csuTime;
  4304. struct tm *tmCSU;
  4305. ftime(&csuTime);
  4306. tmCSU = localtime(&csuTime.time);
  4307. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  4308. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  4309. // tmCSU->tm_sec);
  4310. // Read items inside the folder
  4311. struct dirent *pEntry = NULL;
  4312. while ((pEntry = readdir(pDir)) != NULL) {
  4313. if (strcmp(pEntry->d_name, ".") != 0 &&
  4314. strcmp(pEntry->d_name, "..") != 0 &&
  4315. strncmp(pEntry->d_name, "[", 1) == 0 &&
  4316. strstr(pEntry->d_name, "tar") < 0) {
  4317. char yearC[5];
  4318. unsigned short year = 0;
  4319. char monthC[3];
  4320. unsigned short month = 0;
  4321. yearC[4] = '\0';
  4322. strncpy(yearC, pEntry->d_name + 1, 4);
  4323. monthC[2] = '\0';
  4324. strncpy(monthC, pEntry->d_name + 6, 2);
  4325. year = atoi(yearC);
  4326. month = atoi(monthC);
  4327. if (year != 0) {
  4328. if (year < tmCSU->tm_year + 1900 ||
  4329. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1)) {
  4330. log_info("tar file name : %s \n", pEntry->d_name);
  4331. char file[256];
  4332. memset(file, 0x00, sizeof(file));
  4333. strcat(file, "tar zcvf ");
  4334. strcat(file, logPath);
  4335. strncat(file, "/", 1);
  4336. strcat(file, pEntry->d_name);
  4337. strcat(file, ".tar");
  4338. strncat(file, " ", 1);
  4339. strcat(file, logPath);
  4340. strncat(file, "/", 1);
  4341. strcat(file, pEntry->d_name);
  4342. PRINTF_FUNC("zip = %s \n", file);
  4343. system(file);
  4344. }
  4345. }
  4346. }
  4347. }
  4348. }
  4349. // Close folder
  4350. closedir(pDir);
  4351. }
  4352. void ChangeGunSelectByIndex(byte sel)
  4353. {
  4354. ShmSysConfigAndInfo->SysInfo.CurGunSelected = sel;
  4355. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  4356. }
  4357. void CheckIsAlternatvieByModelName()
  4358. {
  4359. // 黑白機 ?
  4360. /*if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWWU301J0UT1PH") == EQUAL ||
  4361. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYE301J0ET1PH") == EQUAL ||
  4362. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DSYE301J3EW2PH") == EQUAL ||
  4363. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ModelName, "DWYC301J0UW1PH") == EQUAL) {
  4364. */
  4365. if (ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'W') { //DS60-120
  4366. // 壁掛
  4367. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = YES;
  4368. } else {
  4369. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf = NO;
  4370. }
  4371. }
  4372. void StopProcessingLoop()
  4373. {
  4374. for (;;) {
  4375. CheckFactoryConfigFunction();
  4376. CheckFwUpdateFunction();
  4377. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2) {
  4378. ChkPrimaryStatus();
  4379. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0) {
  4380. PRINTF_FUNC("Soft reboot for retry self-tets (Primary). \n");
  4381. KillAllTask();
  4382. sleep(3);
  4383. system("/usr/bin/run_evse_restart.sh");
  4384. return;
  4385. }
  4386. }
  4387. sleep(1);
  4388. }
  4389. }
  4390. void CreateWatchdog()
  4391. {
  4392. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO) {
  4393. wtdFd = InitWatchDog();
  4394. if (wtdFd < 0) {
  4395. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  4396. }
  4397. }
  4398. }
  4399. bool IsConnectorWholeIdle()
  4400. {
  4401. bool result = true;
  4402. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++) {
  4403. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  4404. chargingInfo[count]->SystemStatus != S_RESERVATION) {
  4405. result = false;
  4406. break;
  4407. }
  4408. }
  4409. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.AcConnectorCount; count++) {
  4410. if (ac_chargingInfo[count]->SystemStatus != S_IDLE &&
  4411. ac_chargingInfo[count]->IsErrorOccur == NO) {
  4412. result = false;
  4413. break;
  4414. }
  4415. }
  4416. return result;
  4417. }
  4418. void ClearAlarmCodeWhenAcOff()
  4419. {
  4420. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus) {
  4421. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  4422. }
  4423. }
  4424. //==========================================
  4425. // Check task processing
  4426. //==========================================
  4427. void CheckTask()
  4428. {
  4429. /*+++ 20200908, vern, disable it for DD360 +++*/
  4430. #if 0
  4431. if (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') {
  4432. if (system("pidof -s Module_4g > /dev/null") != 0) {
  4433. log_error("Module_4g not running, restart it.\r\n");
  4434. system("/root/Module_4g &");
  4435. }
  4436. } else if (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') {
  4437. if (system("pidof -s Module_Wifi > /dev/null") != 0) {
  4438. log_error("Module_Wifi not running, restart it.\r\n");
  4439. system("/root/Module_Wifi &");
  4440. }
  4441. }
  4442. if (strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  4443. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL) {
  4444. if (system("pidof -s OcppBackend > /dev/null") != 0) {
  4445. log_error("OcppBackend not running, restart it.\r\n");
  4446. system("/root/OcppBackend &");
  4447. }
  4448. }
  4449. #endif
  4450. /*--- 20200908, vern, disable it for DD360 ---*/
  4451. if (system("pidof -s Module_ProduceUtils > /dev/null") != 0) {
  4452. log_error("Module_ProduceUtils not running, restart it.\r\n");
  4453. system ("/root/Module_ProduceUtils &");
  4454. }
  4455. }
  4456. void InitialDHCP()
  4457. {
  4458. char tmpbuf[256] = {0};
  4459. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  4460. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  4461. ShmSysConfigAndInfo->SysConfig.SystemId);
  4462. system(tmpbuf);
  4463. }
  4464. //==========================================
  4465. // Check Smart Charging Profile
  4466. //==========================================
  4467. int GetStartScheduleTime(unsigned char *time)
  4468. {
  4469. int result = -1;
  4470. struct tm tmScheduleStart;
  4471. struct timeb tbScheduleStart;
  4472. 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)) {
  4473. tmScheduleStart.tm_year -= 1900;
  4474. tmScheduleStart.tm_mon -= 1;
  4475. tbScheduleStart.time = mktime(&tmScheduleStart);
  4476. tbScheduleStart.millitm = 0;
  4477. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  4478. }
  4479. return result;
  4480. }
  4481. void CheckSmartChargeProfile(byte _index)
  4482. {
  4483. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == YES) {
  4484. // Get Charging Profile
  4485. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf = NO;
  4486. if (strcmp((char *)ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileKind, "Absolute") == EQUAL &&
  4487. ShmOCPP16Data->SmartChargingProfile[_index].ChargingProfileId == YES //DS60-120 add
  4488. ) {
  4489. int _time = GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  4490. byte _startCount = NO_DEFINE;
  4491. byte _maxCount = ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod);
  4492. for (byte _count = 0; _count < _maxCount; _count++) {
  4493. // 預設最小輸出電流 (MIN_OUTPUT_CUR) A
  4494. if (_time >= ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].StartPeriod) {
  4495. if ((_count == 0 && ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit >= MIN_OUTPUT_CUR) ||
  4496. ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_count].Limit > MIN_OUTPUT_CUR) {
  4497. _startCount = _count;
  4498. }
  4499. }
  4500. }
  4501. PRINTF_FUNC("_startCount = %d \n", _startCount);
  4502. if (_startCount < _maxCount) {
  4503. //DS60-120 add
  4504. PRINTF_FUNC("Profile Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  4505. chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  4506. if (chargingInfo[_index]->EvBatterytargetVoltage > 0 && chargingInfo[_index]->PresentChargingVoltage > 0) {
  4507. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  4508. } else {
  4509. chargingInfo[_index]->ChargingProfileCurrent = 0;
  4510. }
  4511. //DS60-120 remove
  4512. //PRINTF_FUNC("*********Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit);
  4513. //chargingInfo[_index]->ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * 10;
  4514. //chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * chargingInfo[_index]->EvBatterytargetVoltage / 100;
  4515. //chargingInfo[_index]->ChargingProfilePower = ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[_startCount].Limit * AC_OUTPUT_VOL;
  4516. // if ((chargingInfo[_index]->EvBatterytargetVoltage * 10) > 0)
  4517. // {
  4518. // chargingInfo[_index]->ChargingProfileCurrent = chargingInfo[_index]->ChargingProfilePower / (chargingInfo[_index]->EvBatterytargetVoltage * 10);
  4519. // }
  4520. } else {
  4521. chargingInfo[_index]->ChargingProfilePower = -1;
  4522. chargingInfo[_index]->ChargingProfileCurrent = -1;
  4523. }
  4524. } else {
  4525. chargingInfo[_index]->ChargingProfilePower = -1;
  4526. chargingInfo[_index]->ChargingProfileCurrent = -1;
  4527. }
  4528. PRINTF_FUNC("ChargingProfilePower = %f \n", chargingInfo[_index]->ChargingProfilePower);
  4529. PRINTF_FUNC("ChargingProfileCurrent = %f \n", chargingInfo[_index]->ChargingProfileCurrent);
  4530. }
  4531. //
  4532. // printf("-------------Schedule------------\n");
  4533. // printf("index = %d \n", _index);
  4534. // printf("StartSchedule = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.StartSchedule);
  4535. // printf("ChargingRateUnit = %s \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingRateUnit);
  4536. // printf("----------SchedulePeriod---------\n");
  4537. // for (int v = 0; v < 10; v++)
  4538. // {
  4539. // printf("StartPeriod = %d \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].StartPeriod);
  4540. // printf("Limit = %f \n", ShmOCPP16Data->SmartChargingProfile[_index].ChargingSchedule.ChargingSchedulePeriod[v].Limit);
  4541. // }
  4542. // printf("---------------------------------\n");
  4543. }
  4544. void ChargingProfileFlat(byte _index)
  4545. {
  4546. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileConf == NO) {
  4547. if (ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq == NO) {
  4548. ShmOCPP16Data->CSUMsg.bits[_index].ChargingProfileReq = YES;
  4549. }
  4550. }
  4551. }
  4552. void CheckReturnToChargingConn()
  4553. {
  4554. if ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount + ShmSysConfigAndInfo->SysConfig.AcConnectorCount) > 1 &&
  4555. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZING &&
  4556. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_FAIL &&
  4557. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_AUTHORIZ_COMP &&
  4558. ShmSysConfigAndInfo->SysInfo.PageIndex != _LCM_WAIT_FOR_PLUG) {
  4559. bool isReturnTimeout = false;
  4560. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++) {
  4561. // 如果選的 DC 槍在充電~ 則 DC 槍不改變
  4562. if (count == ShmSysConfigAndInfo->SysInfo.CurGunSelected) {
  4563. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  4564. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  4565. isReturnTimeout = false;
  4566. break;
  4567. }
  4568. } else if (count != ShmSysConfigAndInfo->SysInfo.CurGunSelected) {
  4569. if ((chargingInfo[count]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[count]->SystemStatus <= S_COMPLETE) ||
  4570. (chargingInfo[count]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[count]->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  4571. isReturnTimeout = true;
  4572. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  4573. }
  4574. }
  4575. }
  4576. // AC 槍
  4577. if (!isReturnTimeout && ShmSysConfigAndInfo->SysConfig.AcConnectorCount > 0) {
  4578. // 沒有選中 AC,且 AC 在充電中
  4579. if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == NO_DEFINE &&
  4580. (ac_chargingInfo[0]->SystemStatus >= S_PREPARNING && ac_chargingInfo[0]->SystemStatus <= S_COMPLETE)) {
  4581. // 當前 DC 充電槍在 idle 狀態
  4582. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_IDLE ||
  4583. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_RESERVATION) {
  4584. isReturnTimeout = true;
  4585. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  4586. }
  4587. } else if (ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  4588. ((chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= S_REASSIGN_CHECK && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= S_COMPLETE) ||
  4589. (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus <= S_CCS_PRECHARGE_ST1))) {
  4590. // 當前 DC 充電槍在 idle 狀態
  4591. if (ac_chargingInfo[0]->SystemStatus == S_IDLE ||
  4592. ac_chargingInfo[0]->SystemStatus == S_RESERVATION) {
  4593. isReturnTimeout = true;
  4594. StartSystemTimeoutDet(Timeout_ReturnToChargingGunDet);
  4595. }
  4596. }
  4597. }
  4598. if (!isReturnTimeout) {
  4599. StopSystemTimeoutDet();
  4600. }
  4601. }
  4602. }
  4603. bool GetStartChargingByAlterMode(byte _gun)
  4604. {
  4605. bool result = true;
  4606. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 2 &&
  4607. ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == YES) {
  4608. for (byte _select = 0; _select < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _select++) {
  4609. if (_select != _gun) {
  4610. if (chargingInfo[_select]->SystemStatus != S_IDLE &&
  4611. chargingInfo[_select]->SystemStatus != S_RESERVATION) {
  4612. result = false;
  4613. break;
  4614. }
  4615. }
  4616. }
  4617. }
  4618. return result;
  4619. }
  4620. void TheEndCharging(byte gun_index)
  4621. {
  4622. ftime(&endChargingTime[gun_index]);
  4623. if (chargingInfo[gun_index]->PresentChargedDuration != 0) {
  4624. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  4625. }
  4626. chargingInfo[gun_index]->isRemoteStart = NO;
  4627. StopGunInfoTimeoutDet(gun_index);
  4628. StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  4629. ChangeStartOrStopDateTime(NO, gun_index);
  4630. DB_Insert_Record(localDb, gun_index);
  4631. }
  4632. void UpdateErrorCodeToOcpp(byte index)
  4633. {
  4634. uint8_t i = 0;
  4635. //if ((strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") == EQUAL) &&
  4636. // (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") == EQUAL)
  4637. // ) {
  4638. // for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  4639. // if (strncmp((char *)&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "012251", 6) == EQUAL ||
  4640. // strncmp((char *)&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "012252", 6) == EQUAL ||
  4641. // strncmp((char *)&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "012238", 6) == EQUAL ||
  4642. // strncmp((char *)&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042251", 6) == EQUAL ||
  4643. // strncmp((char *)&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042252", 6) == EQUAL ||
  4644. // strncmp((char *)&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "012304", 6) == EQUAL ||
  4645. // strncmp((char *)&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042200", 6) == EQUAL ||
  4646. // strncmp((char *)&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042201", 6) == EQUAL ||
  4647. // strncmp((char *)&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042202", 6) == EQUAL ||
  4648. // strncmp((char *)&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042267", 6) == EQUAL) {
  4649. // strncpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], 6);
  4650. // //log_info("1 %d = VendorErrorCode = %s\r\n", index, (char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode);
  4651. // break;
  4652. // }
  4653. // }
  4654. //}
  4655. //log_info("%d = ConnectorAlarmCode = %s\r\n", index, (char *)chargingInfo[index]->ConnectorAlarmCode);
  4656. //log_info("%d = EvConnAlarmCode = %s\r\n", index, (char *)chargingInfo[index]->EvConnAlarmCode);
  4657. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL) {
  4658. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "InternalError");
  4659. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)chargingInfo[index]->ConnectorAlarmCode);
  4660. } else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL) {
  4661. strcpy((char *)ShmOCPP16Data->StatusNotification[index].ErrorCode, "OtherError");
  4662. strcpy((char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode, (char *)chargingInfo[index]->EvConnAlarmCode);
  4663. }
  4664. //log_info("2 %d = VendorErrorCode = %s\r\n", index, (char *)ShmOCPP16Data->StatusNotification[index].VendorErrorCode);
  4665. }
  4666. void AdjustChargerCurrent()
  4667. {
  4668. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmPsuData->SystemAvailableCurrent / 10;
  4669. // 設定的電流~ 如超過可輸出的電流,則 bypass
  4670. if (ShmSysConfigAndInfo->SysConfig.RatingCurrent < ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent ||
  4671. ShmSysConfigAndInfo->SysConfig.RatingCurrent == 0) {
  4672. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent = 0;
  4673. }
  4674. PRINTF_FUNC ("PSU : MaxChargingPower = %d, MaxChargingCurrent = %d",
  4675. ShmPsuData->SystemAvailablePower / 10,
  4676. ShmPsuData->SystemAvailableCurrent / 10
  4677. );
  4678. PRINTF_FUNC ("Config : ChargingPower = %d, ChargingCurrent = %d",
  4679. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  4680. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent
  4681. );
  4682. }
  4683. #if METER_ENABLE
  4684. void CheckStatusOfMeter()
  4685. {
  4686. // 橋接如果搭上~ 則兩個電表都是使用狀態
  4687. // 如橋接沒有搭上~ 則視該火線 relay 狀態為電表使用狀態
  4688. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES) {
  4689. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++) {
  4690. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  4691. }
  4692. } else {
  4693. for (byte mIndex = 0; mIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; mIndex++) {
  4694. if (chargingInfo[mIndex]->RelayKPK2Status == YES || chargingInfo[mIndex]->RelayK1K2Status) {
  4695. ShmCsuMeterData->_meter[mIndex].isCalculation = YES;
  4696. } else {
  4697. ShmCsuMeterData->_meter[mIndex].isCalculation = NO;
  4698. }
  4699. }
  4700. }
  4701. }
  4702. void CheckRelayWeldingOrDrivingFault(byte gun_index)
  4703. {
  4704. // relay welding fault then stop the charging process.
  4705. byte faultCode = RELAY_STATUS_ERROR_NONE;
  4706. if (gun_index == 0) {
  4707. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  4708. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  4709. faultCode = RELAY_STATUS_ERROR_WELDING;
  4710. } else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  4711. ShmDcCommonData->RelayCheckStatus[RELAY_SMR1_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  4712. faultCode = RELAY_STATUS_ERROR_DRIVING;
  4713. }
  4714. } else if (gun_index == 1) {
  4715. if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  4716. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  4717. faultCode = RELAY_STATUS_ERROR_WELDING;
  4718. } else if (ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  4719. ShmDcCommonData->RelayCheckStatus[RELAY_SMR2_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  4720. faultCode = RELAY_STATUS_ERROR_DRIVING;
  4721. }
  4722. }
  4723. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount >= 2 &&
  4724. !ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf) {
  4725. // 橋接
  4726. if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_WELDING ||
  4727. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_WELDING) {
  4728. faultCode = RELAY_STATUS_ERROR_WELDING;
  4729. } else if (ShmDcCommonData->RelayCheckStatus[RELAY_PARA_P_STATUS] == RELAY_STATUS_ERROR_DRIVING ||
  4730. ShmDcCommonData->RelayCheckStatus[RELAY_PARA_N_STATUS] == RELAY_STATUS_ERROR_DRIVING) {
  4731. faultCode = RELAY_STATUS_ERROR_DRIVING;
  4732. }
  4733. }
  4734. if (faultCode == RELAY_STATUS_ERROR_WELDING) {
  4735. // welding
  4736. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  4737. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = YES;
  4738. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  4739. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = YES;
  4740. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  4741. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = YES;
  4742. }
  4743. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = YES;
  4744. EmcOccureByString("");
  4745. } else if (faultCode == RELAY_STATUS_ERROR_DRIVING) {
  4746. // driving
  4747. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  4748. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  4749. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  4750. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  4751. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  4752. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  4753. }
  4754. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = YES;
  4755. EmcOccureByString("");
  4756. } else {
  4757. ShmDcCommonData->GunRelayWeldingOccur[gun_index] = NO;
  4758. ShmDcCommonData->GunRelayDrivingOccur[gun_index] = NO;
  4759. bool isClear = false;
  4760. if (!relayDrivingCheck) {
  4761. if (chargingInfo[gun_index]->Type == _Type_Chademo &&
  4762. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES) {
  4763. isClear = true;
  4764. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  4765. } else if (chargingInfo[gun_index]->Type == _Type_GB &&
  4766. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault == YES) {
  4767. isClear = true;
  4768. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  4769. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2 &&
  4770. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES) {
  4771. isClear = true;
  4772. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  4773. }
  4774. }
  4775. if (!relayWeldingCheck) {
  4776. if (chargingInfo[gun_index]->Type == _Type_Chademo &&
  4777. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding == YES) {
  4778. isClear = true;
  4779. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = NO;
  4780. } else if (chargingInfo[gun_index]->Type == _Type_GB &&
  4781. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding == YES) {
  4782. isClear = true;
  4783. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = NO;
  4784. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2 &&
  4785. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding == YES) {
  4786. isClear = true;
  4787. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = NO;
  4788. }
  4789. }
  4790. if (!relayDrivingCheck && !relayWeldingCheck && isClear) {
  4791. ReleaseChargingProcessByString(2);
  4792. }
  4793. }
  4794. }
  4795. bool PrecheckIsPass(byte gun_index)
  4796. {
  4797. bool result = true;
  4798. // relay welding or driving 是反向
  4799. if (result) {
  4800. result = !ShmDcCommonData->GunRelayWeldingOccur[gun_index];
  4801. }
  4802. return result;
  4803. }
  4804. #endif //METER_ENABLE
  4805. void ResetDetAlarmStatus(byte gun)
  4806. {
  4807. if (chargingInfo[gun]->Type == _Type_Chademo) {
  4808. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP == YES) {
  4809. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = NO;
  4810. }
  4811. } else if (chargingInfo[gun]->Type == _Type_GB) {
  4812. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP == YES) {
  4813. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = NO;
  4814. }
  4815. } else if (chargingInfo[gun]->Type == _Type_CCS_2) {
  4816. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP == YES) {
  4817. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = NO;
  4818. }
  4819. }
  4820. }
  4821. static void powerCabinetPsuAlarmStatus(void)
  4822. {
  4823. uint8_t i = 0;
  4824. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  4825. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], "042267", 6) == 0) {
  4826. EmcOccureByString("042267");
  4827. }
  4828. }
  4829. }
  4830. int main(void)
  4831. {
  4832. uint8_t evBoardStopState = 0;
  4833. if (CreateShareMemory() == 0) {
  4834. log_error("CreatShareMemory NG\r\n");
  4835. if (ShmStatusCodeData != NULL) {
  4836. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  4837. }
  4838. sleep(5);
  4839. system("reboot -f");
  4840. sleep(5);
  4841. system("reboot -f");
  4842. }
  4843. log_info(" **************** FileSystem Boot up ***************\n");
  4844. if (!InitialSystemDefaultConfig()) {
  4845. log_error("InitialSystemDefaultConfig NG \n");
  4846. //StopProcessingLoop();
  4847. sleep(5);
  4848. system("reboot -f");
  4849. }
  4850. CheckGunTypeFromHw();
  4851. CheckIsAlternatvieByModelName();
  4852. InitialShareMemoryInfo();
  4853. ChangeLcmByIndex(_LCM_INIT);
  4854. if (!CheckConnectorTypeStatus()) {
  4855. isModelNameMatch = false;
  4856. }
  4857. Initialization();
  4858. SpawnTask();
  4859. log_info("Spawned all Task");
  4860. if (!isModelNameMatch) {
  4861. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  4862. ChangeLcmByIndex(_LCM_FIX);
  4863. // Module Name 與硬體對應不正確
  4864. log_error("Module Name & HW info none match. \n");
  4865. sleep(3);
  4866. KillAllTask();
  4867. StopProcessingLoop();
  4868. }
  4869. CreateTimeoutFork();
  4870. log_info("Start self test... \n");
  4871. SelfTestRun();
  4872. StopSystemTimeoutDet();
  4873. log_info("Self test finished : SelfTestSeq = %d, Work_Step = %d ",
  4874. ShmSysConfigAndInfo->SysInfo.SelfTestSeq,
  4875. ShmPsuData->Work_Step);
  4876. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_FAIL ||
  4877. ShmPsuData->Work_Step == _NO_WORKING ||
  4878. ShmStatusCodeData->InfoCode.InfoEvents.bits.CcsSeccTimeoutQCA7000Comm == YES //DS60-120 add
  4879. ) {
  4880. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2) {
  4881. if (!DisplaySelfTestFailReason()) { //DS60-120 add
  4882. PRINTF_FUNC("Soft reboot for retry self-tets. \n");
  4883. sleep(3);
  4884. system("killall OcppBackend &");
  4885. KillAllTask();
  4886. system("/usr/bin/run_evse_restart.sh");
  4887. }
  4888. }
  4889. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  4890. setChargerMode(gun_index, MODE_ALARM);
  4891. }
  4892. //ChangeLcmByIndex(_LCM_FIX);
  4893. sleep(3);
  4894. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2) { //DS60-120 add
  4895. KillTaskExceptPrimary();
  4896. } else {
  4897. KillTask();
  4898. }
  4899. StopProcessingLoop();
  4900. } else {
  4901. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  4902. setChargerMode(gun_index, MODE_IDLE);
  4903. }
  4904. }
  4905. // Local DB
  4906. if (DB_Open(localDb) != PASS) {
  4907. PRINTF_FUNC("DB_Open fail. \n");
  4908. isDb_ready = false;
  4909. } else {
  4910. isDb_ready = true;
  4911. for (int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  4912. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  4913. }
  4914. DB_Reboot_Record(localDb);
  4915. }
  4916. PRINTF_FUNC("===== Create DB End ===== \n");
  4917. #if defined DD360Audi
  4918. ChangeLcmByIndex(_LCM_SELECT_GUN);
  4919. #else
  4920. ChangeLcmByIndex(_LCM_IDLE);
  4921. #endif //defined DD360Audi
  4922. sleep(1);
  4923. //***** 須新增的偵測 *****//
  4924. // 1. Thernal - 控制風扇轉速
  4925. // 2. ouput fuse - 控制風扇轉速
  4926. CreateRfidFork();
  4927. // Create Watchdog
  4928. //CreateWatchdog();
  4929. // Main loop
  4930. PRINTF_FUNC("===== Charger info ===== ");
  4931. PRINTF_FUNC("SW Version = %s", fwVersion);
  4932. PRINTF_FUNC("ModelName = %s", ShmSysConfigAndInfo->SysConfig.ModelName);
  4933. CheckFwSlotStatusLog();
  4934. AdjustChargerCurrent();
  4935. gettimeofday(&_cmdMainPriority_time, NULL);
  4936. for (;;) {
  4937. CheckOcppStatus();
  4938. ChkPrimaryStatus();
  4939. if ((IsConnectorWholeIdle() || ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_FIX) &&
  4940. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag != Timeout_ReturnToChargingGunDet) {
  4941. CheckFactoryConfigFunction();
  4942. CheckFwUpdateFunction();
  4943. }
  4944. // OCPP 邏輯
  4945. OcppRemoteStartChk();
  4946. // 讀卡邏輯
  4947. ScannerCardProcess();
  4948. // 當 AC 沒有搭上時,清除一些錯誤碼
  4949. ClearAlarmCodeWhenAcOff();
  4950. // 確認是否要回到充電中的槍畫面邏輯判斷
  4951. CheckReturnToChargingConn();
  4952. #if defined DD360 || defined DD360Audi
  4953. //確認Power cabinet PSU Status
  4954. powerCabinetPsuAlarmStatus();
  4955. #endif // defined DD360 || defined DD360Audi
  4956. if (_acgunIndex > 0 && isDetectPlugin() && !isCardScan) {
  4957. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  4958. }
  4959. if ((GetTimeoutValue(_cmdMainPriority_time) / 1000) > 5000) {
  4960. CheckTask();
  4961. #if defined METER_ENABLE
  4962. relayWeldingCheck = false;
  4963. relayDrivingCheck = false;
  4964. #endif //defined METER_ENABLE
  4965. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++) {
  4966. //if (chargingInfo[_index]->SystemStatus == S_CHARGING) {
  4967. // CheckSmartChargeProfile(_index);
  4968. //}
  4969. if ((chargingInfo[_index]->SystemStatus >= S_PREPARING_FOR_EVSE &&
  4970. chargingInfo[_index]->SystemStatus <= S_CHARGING) ||
  4971. (chargingInfo[_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  4972. chargingInfo[_index]->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  4973. if (chargingInfo[_index]->SystemStatus == S_CHARGING &&
  4974. _ocppProfileChkFlag == 12) {
  4975. ChargingProfileFlat(_index);
  4976. _ocppProfileChkFlag = 0;
  4977. } else if (chargingInfo[_index]->SystemStatus != S_CHARGING) {
  4978. ChargingProfileFlat(_index);
  4979. _ocppProfileChkFlag = 0;
  4980. } else {
  4981. _ocppProfileChkFlag++;
  4982. }
  4983. }
  4984. #if defined METER_ENABLE
  4985. if (ShmDcCommonData->SysGunAreSameType == YES) {
  4986. if (ShmDcCommonData->GunRelayWeldingOccur[_index] == YES) {
  4987. relayWeldingCheck = true;
  4988. }
  4989. if (ShmDcCommonData->GunRelayDrivingOccur[_index] == YES) {
  4990. relayDrivingCheck = true;
  4991. }
  4992. }
  4993. #endif //defined METER_ENABLE
  4994. }
  4995. gettimeofday(&_cmdMainPriority_time, NULL);
  4996. }
  4997. #if defined METER_ENABLE
  4998. if (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'M' ||
  4999. ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'L') {
  5000. CheckStatusOfMeter();
  5001. }
  5002. #endif //defined METER_ENABLE
  5003. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++) {
  5004. CheckGpioInStatus();
  5005. CheckErrorOccurStatus(gun_index);
  5006. ChkOcppStatus(gun_index);
  5007. #if defined METER_ENABLE
  5008. CheckRelayWeldingOrDrivingFault(gun_index);
  5009. #endif //defined METER_ENABLE
  5010. if ((chargingInfo[gun_index]->SystemStatus >= S_PREPARING_FOR_EVSE &&
  5011. chargingInfo[gun_index]->SystemStatus <= S_CHARGING) ||
  5012. (chargingInfo[gun_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  5013. chargingInfo[gun_index]->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  5014. CheckSmartChargeProfile(gun_index);
  5015. }
  5016. //PRINTF_FUNC("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  5017. switch (chargingInfo[gun_index]->SystemStatus) {
  5018. case S_IDLE:
  5019. if (isModeChange(gun_index)) {
  5020. log_info("S_IDLE================================== %x \n", gun_index);
  5021. chargingInfo[gun_index]->PresentChargedDuration = 0;
  5022. chargingInfo[gun_index]->RemainChargingDuration = 0;
  5023. chargingInfo[gun_index]->PresentChargingVoltage = 0;//DS60-120 add
  5024. chargingInfo[gun_index]->PresentChargingCurrent = 0;//DS60-120 add
  5025. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  5026. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  5027. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  5028. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  5029. //Jerry add
  5030. memset(&gAudiCustInfo->PricesInfo[gun_index], 0, sizeof(PricesInfo));
  5031. gAudiCustInfo->PricesInfo[gun_index].Balance = FAIL_BALANCE_PRICES;
  5032. #if defined DD360Audi
  5033. destroySelGun(gun_index);
  5034. #endif //defined DD360Audi
  5035. ResetDetAlarmStatus(gun_index); //recovery OVP status code
  5036. //strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, "");
  5037. }
  5038. case S_RESERVATION:
  5039. if (isModeChange(gun_index)) {
  5040. log_info("S_RESERVATION....................%x \n", gun_index);
  5041. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = YES;
  5042. }
  5043. if (chargingInfo[gun_index]->IsAvailable == NO) {
  5044. setChargerMode(gun_index, MODE_MAINTAIN);
  5045. }
  5046. case S_MAINTAIN:
  5047. case S_FAULT: {
  5048. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2) {
  5049. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected) {
  5050. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  5051. } else if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != S_IDLE &&
  5052. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != S_RESERVATION &&
  5053. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != S_MAINTAIN &&
  5054. chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus != S_FAULT) {
  5055. if (chargingInfo[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus == S_CHARGING) {
  5056. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_COMPLETE;
  5057. } else {
  5058. #if defined DD360Audi
  5059. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  5060. #else
  5061. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5062. #endif //defined DD360Audi
  5063. }
  5064. }
  5065. ClearDetectPluginFlag();
  5066. //#if !defined DD360 && !defined DD360Audi
  5067. UpdateErrorCodeToOcpp(gun_index);
  5068. //#endif //!defined DD360 && !defined DD360Audi
  5069. setChargerMode(gun_index, MODE_FAULT);
  5070. } else {
  5071. #if 1
  5072. if (chargingInfo[gun_index]->SystemStatus == S_FAULT) {
  5073. #if defined DD360Audi
  5074. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  5075. #else
  5076. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5077. #endif //defined DD360Audi
  5078. setChargerMode(gun_index, MODE_IDLE);
  5079. }
  5080. //if (ShmSysConfigAndInfo->SysInfo.SystemPage == _LCM_FIX) {
  5081. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;//_LCM_NONE; //Jerry add
  5082. // //for (byte g_index = 0; g_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; g_index++) {
  5083. // setChargerMode(gun_index, MODE_IDLE);
  5084. // //}
  5085. //}
  5086. #endif //0
  5087. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER) {
  5088. #if !defined DD360 && !defined DD360Audi
  5089. if (ShmSysConfigAndInfo->SysInfo.AcContactorStatus == YES)
  5090. #else
  5091. if (1)
  5092. #endif //!defined DD360 && !defined DD360Audi
  5093. {
  5094. // 均充 -> 最大充
  5095. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO) {
  5096. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE) {
  5097. log_info("=============Smart Charging============= Step 11 \n");
  5098. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_A_TO_M;
  5099. }
  5100. } else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  5101. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING) {
  5102. log_info("=============Smart Charging============= Step 14 \n");
  5103. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  5104. } else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP) {
  5105. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  5106. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5107. }
  5108. } else {
  5109. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  5110. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5111. }
  5112. } else {
  5113. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5114. }
  5115. #if defined METER_ENABLE
  5116. if (PrecheckIsPass(gun_index)) {
  5117. if (chargingInfo[gun_index]->SystemStatus == S_FAULT) {
  5118. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5119. setChargerMode(gun_index, MODE_IDLE);
  5120. }
  5121. #endif //defined METER_ENABLE
  5122. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING) {
  5123. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5124. #if defined DD360Audi
  5125. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_SELECT_GUN;
  5126. #else
  5127. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  5128. #endif //
  5129. }
  5130. } else {
  5131. // Idle 正常程序起點
  5132. // 判斷是否有啟用檢查插槍
  5133. if (isDetectPlugin()) {
  5134. // 卡號驗證成功後,等待充電槍插入充電車
  5135. if (chargingInfo[gun_index]->RemoteStartFlag == YES) {
  5136. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  5137. chargingInfo[gun_index]->IsAvailable) {
  5138. PRINTF_FUNC("-----------------1----------------- %d \n", gun_index);
  5139. chargingInfo[gun_index]->RemoteStartFlag = NO;
  5140. chargingInfo[gun_index]->isRemoteStart = YES; //DS60-120
  5141. ChangeGunSelectByIndex(gun_index);
  5142. AddPlugInTimes(gun_index);
  5143. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5144. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  5145. ClearDetectPluginFlag();
  5146. continue;
  5147. }
  5148. } else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE) {
  5149. if (chargingInfo[gun_index]->ConnectorPlugIn == YES &&
  5150. chargingInfo[gun_index]->IsAvailable &&
  5151. chargingInfo[gun_index]->SystemStatus == S_IDLE
  5152. #if defined DD360Audi
  5153. && (waitRightGunPlugIt(gun_index) == PASS ||
  5154. waitLeftGunPlugIt(gun_index) == PASS)
  5155. #endif //defined DD360Audi
  5156. ) {
  5157. PRINTF_FUNC("-----------------2----------------- \n");
  5158. ChangeGunSelectByIndex(gun_index);
  5159. AddPlugInTimes(gun_index);
  5160. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  5161. PRINTF_FUNC("index = %d, CardNumber = %s \n", gun_index, chargingInfo[gun_index]->StartUserId);
  5162. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  5163. // 當前操作的槍號,進入 Preparing
  5164. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5165. ClearDetectPluginFlag();
  5166. continue;
  5167. }
  5168. }
  5169. if (!isCardScan) {
  5170. // LCM => Waiting for plugging
  5171. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_WAIT_FOR_PLUG;
  5172. }
  5173. } else if (chargingInfo[gun_index]->SystemStatus == S_RESERVATION) {
  5174. if (!isReservationExpired(gun_index)) {
  5175. chargingInfo[gun_index]->SystemStatus = S_IDLE;
  5176. }
  5177. } else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE &&
  5178. (chargingInfo[gun_index]->ConnectorPlugIn == YES && chargingInfo[gun_index]->IsAvailable)) {
  5179. PRINTF_FUNC("-----------------3----------------- \n");
  5180. bool isCanStartChargingFlag = GetStartChargingByAlterMode(gun_index);
  5181. if (isCanStartChargingFlag) {
  5182. ChangeGunSelectByIndex(gun_index);
  5183. AddPlugInTimes(gun_index);
  5184. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  5185. ClearDetectPluginFlag();
  5186. continue;
  5187. }
  5188. } else {
  5189. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5190. #if defined DD360Audi
  5191. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_SELECT_GUN;
  5192. #else
  5193. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_IDLE;
  5194. #endif // defined DD360Audi
  5195. }
  5196. }
  5197. } // Idle 正常程序終點
  5198. #if defined METER_ENABLE
  5199. } else {
  5200. setChargerMode(gun_index, MODE_FAULT);
  5201. if (gun_index == ShmSysConfigAndInfo->SysInfo.CurGunSelected) {
  5202. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_FIX;
  5203. }
  5204. }
  5205. #endif //defined METER_ENABLE
  5206. ReleaseAlarmCode(gun_index);
  5207. }
  5208. }
  5209. break;
  5210. case S_REASSIGN_CHECK: {
  5211. if (isModeChange(gun_index)) {
  5212. log_info("S_REASSIGN_CHECK================================== %x \n", gun_index);
  5213. #if defined DD360Audi
  5214. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  5215. #else
  5216. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5217. #endif //defined DD360Audi
  5218. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE) {
  5219. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  5220. }
  5221. StopSystemTimeoutDet();
  5222. }
  5223. #if !defined DD360 && !defined DD360Audi
  5224. bool isRessign = false;
  5225. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 1 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO) {
  5226. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX) {
  5227. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  5228. // 有其他槍已經分配好 psu 模塊
  5229. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != index &&
  5230. chargingInfo[index]->SystemStatus >= S_PREPARNING &&
  5231. chargingInfo[index]->SystemStatus != S_MAINTAIN) {
  5232. log_info("=============Smart Charging============= Step 1 \n");
  5233. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_PREPARE_M_TO_A;
  5234. isRessign = true;
  5235. break;
  5236. }
  5237. }
  5238. } else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER &&
  5239. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_NONE) {
  5240. // 如果在切換最大充的過程中,需等待最大充切換完成後,在走均充流程
  5241. if (ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == YES) {
  5242. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_COMP &&
  5243. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag != _REASSIGNED_WAITING) {
  5244. log_info("=============Smart Charging============= Step 14 \n");
  5245. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_WAITING;
  5246. } else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_COMP) {
  5247. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  5248. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5249. continue;
  5250. }
  5251. }
  5252. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5253. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5254. }
  5255. continue;
  5256. }
  5257. }
  5258. #endif // !defined DD360 && !defined DD360Audi
  5259. if (0) { // if (isRessign)
  5260. #if defined DD360 || defined DD360Audi
  5261. setChargerMode(gun_index, MODE_PRECHARGE);
  5262. #else
  5263. setChargerMode(gun_index, MODE_REASSIGN);
  5264. #endif //defined DD360 || defined DD360Audi
  5265. } else {
  5266. setChargerMode(gun_index, MODE_PRECHARGE);
  5267. }
  5268. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5269. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5270. }
  5271. }
  5272. break;
  5273. case S_REASSIGN: {
  5274. if (isModeChange(gun_index)) {
  5275. PRINTF_FUNC("S_REASSIGN================================== %x \n", gun_index);
  5276. gettimeofday(&_toAverage_time, NULL);
  5277. }
  5278. // 重新分配,此階段主要是讓已經在充電或者準備進入充電前的緩衝
  5279. // 此狀態下~ 控制權在於 PSU 及 EV小板 Process
  5280. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_NONE ||
  5281. ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER) {
  5282. if (ShmSysConfigAndInfo->SysInfo.CanAverageCharging) {
  5283. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;//DS60-120 add
  5284. setChargerMode(gun_index, MODE_PRECHARGE);
  5285. } else {
  5286. setChargerMode(gun_index, MODE_IDLE);
  5287. }
  5288. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE; //DS60-120 add
  5289. } else if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag == _REASSIGNED_RELAY_M_TO_A &&
  5290. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus == NO) {
  5291. PRINTF_FUNC("=============Smart Charging : _REASSIGNED_COMP============= Step 6 \n");
  5292. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  5293. //ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE; //DS60-120 remove
  5294. ShmSysConfigAndInfo->SysInfo.CanAverageCharging = true; //DS60-120 add
  5295. }
  5296. //PRINTF_FUNC("CurGunSelected = %d, gun_index = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected, gun_index);
  5297. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5298. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5299. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5300. }
  5301. }
  5302. break;
  5303. case S_PREPARNING: {
  5304. if (isModeChange(gun_index)) {
  5305. PRINTF_FUNC("S_PREPARNING================================== %x \n", gun_index);
  5306. StopGunInfoTimeoutDet(gun_index);
  5307. StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  5308. }
  5309. if (ShmPsuData->SystemPresentPsuQuantity > 0 &&
  5310. ShmPsuData->SystemAvailablePower > 10 &&
  5311. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 5000000) {
  5312. //if ((chargingInfo[gun_index]->MaximumChargingVoltage > 0) && //DS60-120 remove
  5313. // (chargingInfo[gun_index]->AvailableChargingCurrent > 0) &&
  5314. // (chargingInfo[gun_index]->AvailableChargingPower > 0)) {
  5315. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  5316. //}
  5317. }
  5318. //DS60-120 add
  5319. if (ShmSysConfigAndInfo->SysConfig.TotalConnectorCount >= 2) {
  5320. bool oughtAver = true;
  5321. for (byte index = 0; index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; index++) {
  5322. // 共同進入充電邏輯
  5323. if (chargingInfo[index]->SystemStatus != S_PREPARNING) {
  5324. oughtAver = false;
  5325. break;
  5326. }
  5327. }
  5328. if (oughtAver) {
  5329. PRINTF_FUNC("********* Automatically change to aver mode ********** \n");
  5330. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_AVER;
  5331. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  5332. }
  5333. }
  5334. if ((evBoardStopState = isEvBoardStopChargeFlag(gun_index)) > 0) {
  5335. // 板端要求停止 (錯誤)
  5336. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  5337. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  5338. }
  5339. if (evBoardStopState == EV_BOARD_STOP_CHARGING) {
  5340. ChargingAlarmProcess(gun_index);
  5341. } else if (evBoardStopState == POWER_CABINET_STOP_CHARGING) {
  5342. ChargingTerminalProcess(gun_index);
  5343. }
  5344. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  5345. // 板端要求停止 (正常)
  5346. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  5347. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  5348. }
  5349. ChargingTerminalProcess(gun_index);
  5350. } else if (OcppRemoteStop(gun_index) == YES ||
  5351. WifiScheduleStop(gun_index)) {
  5352. // 後臺要求停止
  5353. ChargingTerminalProcess(gun_index);
  5354. }
  5355. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5356. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5357. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5358. }
  5359. }
  5360. break;
  5361. case S_PREPARING_FOR_EV: { // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  5362. if (isModeChange(gun_index)) {
  5363. log_info("S_PREPARING_FOR_EV================================== %x \n", gun_index);
  5364. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  5365. StopGunInfoTimeoutDet(gun_index);
  5366. StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  5367. }
  5368. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  5369. // 檢查車端的槍鎖是否為鎖上
  5370. if (isEvGunLocked_chademo(gun_index) == YES) {
  5371. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  5372. }
  5373. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  5374. // 檢查車端的 charging enable 是否為 1
  5375. if (isEvGunLocked_gb(gun_index) == YES) {
  5376. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  5377. }
  5378. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  5379. // 檢查車端的 charging enable 是否為 1
  5380. if (isEvGunLocked_ccs(gun_index) == YES) {
  5381. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  5382. }
  5383. }
  5384. if ((evBoardStopState = isEvBoardStopChargeFlag(gun_index)) > 0) {
  5385. // 板端要求停止 (錯誤)
  5386. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  5387. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  5388. }
  5389. if (evBoardStopState == EV_BOARD_STOP_CHARGING) {
  5390. ChargingAlarmProcess(gun_index);
  5391. } else if (evBoardStopState == POWER_CABINET_STOP_CHARGING) {
  5392. ChargingTerminalProcess(gun_index);
  5393. }
  5394. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  5395. // 板端要求停止 (正常)
  5396. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  5397. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  5398. }
  5399. ChargingTerminalProcess(gun_index);
  5400. } else if (OcppRemoteStop(gun_index) == YES ||
  5401. WifiScheduleStop(gun_index)) {
  5402. // 後臺要求停止
  5403. ChargingTerminalProcess(gun_index);
  5404. }
  5405. // LCM => Pre-charging
  5406. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5407. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5408. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5409. }
  5410. }
  5411. break;
  5412. case S_PREPARING_FOR_EVSE: { // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  5413. if (isModeChange(gun_index)) {
  5414. log_info("S_PREPARING_FOR_EVSE================================== %x \n", gun_index);
  5415. StopGunInfoTimeoutDet(gun_index);
  5416. StartGunInfoTimeoutDet(gun_index, Timeout_EvseChargingDet);
  5417. }
  5418. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  5419. // 檢查樁端的 GFD 結果
  5420. if (isPrechargeStatus_chademo(gun_index) > 5 && isPrechargeStatus_chademo(gun_index) < 8) {
  5421. // 當前操作的槍號,進入 Charging
  5422. setChargerMode(gun_index, MODE_CHARGING);
  5423. }
  5424. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5425. // GFD 錯誤停止
  5426. RecordAlarmCode(gun_index, "012234");
  5427. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  5428. // GFD 警告
  5429. RecordWarningCode(gun_index, "012296");
  5430. }
  5431. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  5432. // 檢查樁端的 GFD 結果
  5433. if (isPrechargeStatus_gb(gun_index) > 5 && isPrechargeStatus_gb(gun_index) < 9) {
  5434. setChargerMode(gun_index, MODE_CHARGING);
  5435. }
  5436. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5437. // GFD 錯誤停止
  5438. RecordAlarmCode(gun_index, "012236");
  5439. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  5440. // GFD 警告
  5441. RecordWarningCode(gun_index, "012298");
  5442. }
  5443. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  5444. // 檢查樁端的 GFD 結果
  5445. if ((chargingInfo[gun_index]->GroundFaultStatus == GFD_PASS ||
  5446. chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING)) {
  5447. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP0);
  5448. }
  5449. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5450. // GFD 錯誤停止
  5451. RecordAlarmCode(gun_index, "012235");
  5452. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  5453. // GFD 警告
  5454. RecordWarningCode(gun_index, "012297");
  5455. }
  5456. }
  5457. if ((evBoardStopState = isEvBoardStopChargeFlag(gun_index)) > 0) {
  5458. // 板端要求停止 (錯誤)
  5459. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  5460. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  5461. }
  5462. if (evBoardStopState == EV_BOARD_STOP_CHARGING) {
  5463. ChargingAlarmProcess(gun_index);
  5464. } else if (evBoardStopState == POWER_CABINET_STOP_CHARGING) {
  5465. ChargingTerminalProcess(gun_index);
  5466. }
  5467. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  5468. // 板端要求停止 (正常)
  5469. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  5470. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  5471. }
  5472. ChargingTerminalProcess(gun_index);
  5473. } else if (OcppRemoteStop(gun_index) == YES ||
  5474. WifiScheduleStop(gun_index)) {
  5475. // 後臺要求停止
  5476. ChargingTerminalProcess(gun_index);
  5477. }
  5478. // LCM => Pre-charging
  5479. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5480. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5481. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5482. }
  5483. }
  5484. break;
  5485. case S_CHARGING: { // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  5486. if (isModeChange(gun_index)) {
  5487. log_info("S_CHARGING================================== %x \n", gun_index);
  5488. StopGunInfoTimeoutDet(gun_index);
  5489. ftime(&startChargingTime[gun_index]);
  5490. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, ""); //DS60-120 add
  5491. ChangeStartOrStopDateTime(YES, gun_index);
  5492. #if !defined DD360 && !defined DD360Audi
  5493. OcppStartTransation(gun_index);
  5494. #endif //!defined DD360 && !defined DD360Audi
  5495. }
  5496. if (ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf) {
  5497. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = NO;
  5498. }
  5499. ftime(&endChargingTime[gun_index]);
  5500. chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  5501. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  5502. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5503. // GFD 錯誤停止
  5504. RecordAlarmCode(gun_index, "012234");
  5505. }
  5506. //else if (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  5507. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL)) {
  5508. // // UVP
  5509. // RecordAlarmCode(gun_index, "012289");
  5510. // ChargingTerminalProcess(gun_index);
  5511. //}
  5512. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  5513. // GFD 警告
  5514. RecordWarningCode(gun_index, "012296");
  5515. }
  5516. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  5517. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5518. // GFD 錯誤停止
  5519. RecordAlarmCode(gun_index, "012236");
  5520. }
  5521. //else if (isPrechargeStatus_gb(gun_index) == 10 &&
  5522. // (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  5523. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL))) {
  5524. // // UVP
  5525. // RecordAlarmCode(gun_index, "012290");
  5526. // ChargingTerminalProcess(gun_index);
  5527. //}
  5528. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  5529. // GFD 警告
  5530. RecordWarningCode(gun_index, "012298");
  5531. }
  5532. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  5533. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5534. // GFD 錯誤停止
  5535. RecordAlarmCode(gun_index, "012235");
  5536. }
  5537. //else if (((chargingInfo[gun_index]->EvBatterytargetVoltage * 10) > 0 && chargingInfo[gun_index]->EvBatterytargetVoltage < SYSTEM_MIN_VOL) ||
  5538. // (chargingInfo[gun_index]->PresentChargedDuration >= 10 && chargingInfo[gun_index]->PresentChargingVoltage < SYSTEM_MIN_VOL)) {
  5539. // // UVP
  5540. // RecordAlarmCode(gun_index, "012288");
  5541. // ChargingTerminalProcess(gun_index);
  5542. //}
  5543. else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  5544. // GFD 警告
  5545. RecordWarningCode(gun_index, "012297");
  5546. }
  5547. }
  5548. if ((evBoardStopState = isEvBoardStopChargeFlag(gun_index)) > 0) {
  5549. // 板端要求停止 (錯誤)
  5550. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  5551. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  5552. }
  5553. //printf("%d evBoardStopState = %d\r\n", gun_index, evBoardStopState);
  5554. if (evBoardStopState == EV_BOARD_STOP_CHARGING) {
  5555. ChargingAlarmProcess(gun_index);
  5556. } else if (evBoardStopState == POWER_CABINET_STOP_CHARGING) {
  5557. ChargingTerminalProcess(gun_index);
  5558. }
  5559. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  5560. // 板端要求停止 (正常)
  5561. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  5562. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  5563. }
  5564. ChargingTerminalProcess(gun_index);
  5565. } else if (OcppRemoteStop(gun_index) == YES ||
  5566. WifiScheduleStop(gun_index) ||
  5567. CheckBackendChargingTimeout(gun_index) ||
  5568. CheckBackendChargingEnergy(gun_index) ||
  5569. strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid") == EQUAL) {
  5570. // 後臺要求停止
  5571. ChargingTerminalProcess(gun_index);
  5572. }
  5573. // LCM => Charging
  5574. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5575. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_CHARGING;
  5576. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5577. }
  5578. }
  5579. break;
  5580. case S_ALARM:
  5581. case S_TERMINATING: {
  5582. if (isModeChange(gun_index)) {
  5583. if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  5584. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  5585. }
  5586. if (chargingInfo[gun_index]->SystemStatus == S_ALARM) {
  5587. PRINTF_FUNC("================== S_ALARM (%x) ================ \n", gun_index);
  5588. UpdateErrorCodeToOcpp(gun_index);
  5589. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL) {
  5590. OcppStopTransation(gun_index);
  5591. }
  5592. TheEndCharging(gun_index);
  5593. } else {
  5594. PRINTF_FUNC("================== S_TERMINATING (%x) ================ \n", gun_index);
  5595. }
  5596. //log_info ("terminating......................... %x \n", gun_index);
  5597. StopGunInfoTimeoutDet(gun_index);
  5598. }
  5599. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  5600. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  5601. //if (isEvStopCharging_chademo(gun_index) == YES) {
  5602. // /*+++ 20200908, vern, disable it for DD360 +++*/
  5603. // /*if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  5604. // strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");*/
  5605. // /*--- 20200908, vern, disable it for DD360 ---*/
  5606. //}
  5607. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5608. // GFD 錯誤停止
  5609. RecordAlarmCode(gun_index, "012234");
  5610. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  5611. // GFD 警告
  5612. RecordWarningCode(gun_index, "012296");
  5613. }
  5614. //if (isEvStopCharging_chademo(gun_index) == YES ||
  5615. // isPrechargeStatus_chademo(gun_index) <= 0) {
  5616. // setChargerMode(gun_index, MODE_COMPLETE);
  5617. //}
  5618. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  5619. log_info("************ GB lock Status = %d, status = %d \n", isEvStopCharging_gb(gun_index), isPrechargeStatus_gb(gun_index));
  5620. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  5621. //if (isEvStopCharging_chademo(gun_index) == YES) {
  5622. // /*+++ 20200908, vern, disable it for DD360 +++*/
  5623. // /*if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  5624. // strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");*/
  5625. // /*--- 20200908, vern, disable it for DD360 ---*/
  5626. //}
  5627. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5628. // GFD 錯誤停止
  5629. RecordAlarmCode(gun_index, "012236");
  5630. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  5631. // GFD 警告
  5632. RecordWarningCode(gun_index, "012298");
  5633. }
  5634. //if (isEvStopCharging_gb(gun_index) == YES ||
  5635. // isPrechargeStatus_gb(gun_index) <= 0) {
  5636. // setChargerMode(gun_index, MODE_COMPLETE);
  5637. //}
  5638. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  5639. // 非車端的停止 : 需等待小板送出停止指令,讓車端解除槍
  5640. //if (isEvStopCharging_chademo(gun_index) == YES) {
  5641. // /*+++ 20200908, vern, disable it for DD360 +++*/
  5642. // /*if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  5643. // strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");*/
  5644. // /*--- 20200908, vern, disable it for DD360 ---*/
  5645. //}
  5646. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5647. // GFD 錯誤停止
  5648. RecordAlarmCode(gun_index, "012235");
  5649. } else if (chargingInfo[gun_index]->GroundFaultStatus == GFD_WARNING) {
  5650. // GFD 警告
  5651. RecordWarningCode(gun_index, "012297");
  5652. }
  5653. //if (isEvStopCharging_ccs(gun_index) == YES &&
  5654. // (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  5655. // isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14)) {
  5656. // setChargerMode(gun_index, MODE_COMPLETE);
  5657. //}
  5658. }
  5659. if (chargingInfo[gun_index]->SystemStatus == S_ALARM) {
  5660. if (chargingInfo[gun_index]->ConnectorPlugIn == NO &&
  5661. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000) {
  5662. setChargerMode(gun_index, MODE_IDLE);
  5663. }
  5664. } else {
  5665. if (chargingInfo[gun_index]->Type == _Type_Chademo) {
  5666. if (isEvStopCharging_chademo(gun_index) == YES ||
  5667. isPrechargeStatus_chademo(gun_index) <= 0) {
  5668. setChargerMode(gun_index, MODE_COMPLETE);
  5669. }
  5670. } else if (chargingInfo[gun_index]->Type == _Type_GB) {
  5671. if (isEvStopCharging_gb(gun_index) == YES ||
  5672. isPrechargeStatus_gb(gun_index) <= 0) {
  5673. setChargerMode(gun_index, MODE_COMPLETE);
  5674. }
  5675. } else if (chargingInfo[gun_index]->Type == _Type_CCS_2) {
  5676. if (isEvStopCharging_ccs(gun_index) == YES &&
  5677. (isPrechargeStatus_ccs(gun_index) >= 53 || isPrechargeStatus_ccs(gun_index) == 0 ||
  5678. isPrechargeStatus_ccs(gun_index) == 13 || isPrechargeStatus_ccs(gun_index) == 14)) {
  5679. setChargerMode(gun_index, MODE_COMPLETE);
  5680. }
  5681. }
  5682. }
  5683. // 車端的停止
  5684. //if (strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL) {
  5685. // strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  5686. //}
  5687. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5688. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  5689. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5690. }
  5691. }
  5692. break;
  5693. case S_COMPLETE: {
  5694. if (isModeChange(gun_index)) {
  5695. PRINTF_FUNC ("complete......................... %x \n", gun_index);
  5696. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL) {
  5697. OcppStopTransation(gun_index);
  5698. }
  5699. TheEndCharging(gun_index);
  5700. //ftime(&endChargingTime[gun_index]);
  5701. //if (chargingInfo[gun_index]->PresentChargedDuration != 0) {
  5702. // chargingInfo[gun_index]->PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
  5703. //}
  5704. //StopGunInfoTimeoutDet(gun_index);
  5705. //StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  5706. //ChangeStartOrStopDateTime(NO, gun_index);
  5707. }
  5708. //if (((chargingInfo[gun_index]->ConnectorPlugIn == NO) ||
  5709. // (restartFlag == 1)) &&
  5710. // (GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000)) {
  5711. // setChargerMode(gun_index, MODE_IDLE);
  5712. // destroySelGun(gun_index); //Jerry add
  5713. //}
  5714. if (chargingInfo[gun_index]->ConnectorPlugIn == NO &&
  5715. GetTimeoutValue(chargingInfo[gun_index]->TimeoutTimer) >= 10000000) {
  5716. setChargerMode(gun_index, MODE_IDLE);
  5717. #if defined DD360Audi
  5718. destroySelGun(gun_index); //Jerry add
  5719. #endif //defined DD360Audi
  5720. }
  5721. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5722. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_COMPLETE;
  5723. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5724. }
  5725. }
  5726. break;
  5727. case S_CCS_PRECHARGE_ST0: {
  5728. if (isModeChange(gun_index)) {
  5729. PRINTF_FUNC("CCS Precharge Processing 1....................%x \n", gun_index);
  5730. StopGunInfoTimeoutDet(gun_index);
  5731. StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  5732. }
  5733. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5734. // GFD 錯誤停止
  5735. RecordAlarmCode(gun_index, "012235");
  5736. }
  5737. if ((evBoardStopState = isEvBoardStopChargeFlag(gun_index)) > 0) {
  5738. // 板端要求停止 (錯誤)
  5739. if (evBoardStopState == EV_BOARD_STOP_CHARGING) {
  5740. ChargingAlarmProcess(gun_index);
  5741. } else if (evBoardStopState == POWER_CABINET_STOP_CHARGING) {
  5742. ChargingTerminalProcess(gun_index);
  5743. }
  5744. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  5745. // 板端要求停止 (正常)
  5746. ChargingTerminalProcess(gun_index);
  5747. } else if (OcppRemoteStop(gun_index) == YES ||
  5748. WifiScheduleStop(gun_index)) {
  5749. // 後臺要求停止
  5750. ChargingTerminalProcess(gun_index);
  5751. }
  5752. // 等待 EV 小板 (CCS) 通知可以開始 Precharge
  5753. // 切換 D+ Relay to Precharge Relay
  5754. if (isPrechargeStatus_ccs(gun_index) == 39 || isPrechargeStatus_ccs(gun_index) == 40) {
  5755. if (chargingInfo[gun_index]->RelayKPK2Status == YES && chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_READY)
  5756. //if (chargingInfo[gun_index]->PrechargeStatus != PRECHARGE_PRERELAY_PASS)
  5757. {
  5758. //PRINTF_FUNC("Send precharge ready 1..........%x, status = %d \n", gun_index, isPrechargeStatus_ccs(gun_index));
  5759. if (isPrechargeStatus_ccs(gun_index) == 39) {
  5760. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PreChargeResponse (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  5761. } else if (isPrechargeStatus_ccs(gun_index) == 40) {
  5762. PRINTF_FUNC("Conn %x, Precharge ready, CCS status = PowerDeliveryRequest start (%d) \n", gun_index, isPrechargeStatus_ccs(gun_index));
  5763. }
  5764. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  5765. }
  5766. } else if (isPrechargeStatus_ccs(gun_index) == 45 || isPrechargeStatus_ccs(gun_index) == 46) {
  5767. setChargerMode(gun_index, MODE_CCS_PRECHARGE_STEP1);
  5768. }
  5769. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5770. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5771. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5772. }
  5773. break;
  5774. }
  5775. case S_CCS_PRECHARGE_ST1: {
  5776. if (isModeChange(gun_index)) {
  5777. log_info("CCS Precharge Processing 2....................%x \n", gun_index);
  5778. }
  5779. if (chargingInfo[gun_index]->GroundFaultStatus == GFD_FAIL) {
  5780. // GFD 錯誤停止
  5781. RecordAlarmCode(gun_index, "012235");
  5782. }
  5783. if ((evBoardStopState = isEvBoardStopChargeFlag(gun_index)) > 0) {
  5784. // 板端要求停止 (錯誤)
  5785. if (evBoardStopState == EV_BOARD_STOP_CHARGING) {
  5786. ChargingAlarmProcess(gun_index);
  5787. } else if (evBoardStopState == POWER_CABINET_STOP_CHARGING) {
  5788. ChargingTerminalProcess(gun_index);
  5789. }
  5790. } else if (isEvBoardNormalStopChargeFlag(gun_index) == YES) {
  5791. // 板端要求停止 (正常)
  5792. ChargingTerminalProcess(gun_index);
  5793. } else if (OcppRemoteStop(gun_index) == YES ||
  5794. WifiScheduleStop(gun_index)) {
  5795. // 後臺要求停止
  5796. ChargingTerminalProcess(gun_index);
  5797. }
  5798. // 等待小板通知進入充電
  5799. // 切換 D+ Relay to Precharge Relay
  5800. if (chargingInfo[gun_index]->RelayK1K2Status == YES) {
  5801. chargingInfo[gun_index]->PrechargeStatus = PRECHARGE_READY;
  5802. setChargerMode(gun_index, MODE_CHARGING);
  5803. }
  5804. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == gun_index) {
  5805. ShmSysConfigAndInfo->SysInfo.ConnectorPage = _LCM_PRE_CHARGE;
  5806. // ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  5807. }
  5808. break;
  5809. }
  5810. }
  5811. }
  5812. #if defined DD360Audi
  5813. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_SELECT_GUN) {
  5814. #else
  5815. if (ShmSysConfigAndInfo->SysInfo.SystemPage != _LCM_NONE) {
  5816. #endif //defined DD360Audi
  5817. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.SystemPage);
  5818. } else {
  5819. bool dcPageRun = false;
  5820. if (_acgunIndex > 0 && ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc != NO_DEFINE) {
  5821. if (ac_chargingInfo[0]->SystemStatus == S_IDLE) {
  5822. ChangeLcmByIndex(_LCM_IDLE);
  5823. } else if (ac_chargingInfo[0]->SystemStatus == S_PREPARNING) {
  5824. ChangeLcmByIndex(_LCM_PRE_CHARGE);
  5825. } else if (ac_chargingInfo[0]->SystemStatus == S_CHARGING) {
  5826. ChangeLcmByIndex(_LCM_CHARGING);
  5827. } else if (ac_chargingInfo[0]->SystemStatus == S_TERMINATING ||
  5828. ac_chargingInfo[0]->SystemStatus == S_COMPLETE) {
  5829. ChangeLcmByIndex(_LCM_COMPLETE);
  5830. } else {
  5831. dcPageRun = true;
  5832. }
  5833. } else {
  5834. dcPageRun = true;
  5835. }
  5836. if (dcPageRun) {
  5837. ChangeLcmByIndex(ShmSysConfigAndInfo->SysInfo.ConnectorPage);
  5838. }
  5839. }
  5840. write(wtdFd, "a", 1);
  5841. usleep(whileLoopTime);
  5842. }
  5843. return FAIL;
  5844. }