main.c 256 KB

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