main.c 255 KB

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