ReMain.c 271 KB

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