main.c 259 KB

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