mainNew.c 293 KB

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