main.c 255 KB

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