ReMain.c 268 KB

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