main.c 325 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445
  1. /*
  2. * main.c
  3. *
  4. * Created on: 2020年9月10日
  5. * Author: Wendell
  6. */
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/time.h>
  10. #include <sys/timeb.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/mman.h>
  17. #include <linux/wireless.h>
  18. #include <arpa/inet.h>
  19. #include <netinet/in.h>
  20. #include <unistd.h>
  21. #include <stdarg.h>
  22. #include <stdio.h> /*標準輸入輸出定義*/
  23. #include <stdlib.h> /*標準函數庫定義*/
  24. #include <unistd.h> /*Unix 標準函數定義*/
  25. #include <fcntl.h> /*檔控制定義*/
  26. #include <termios.h> /*PPSIX 終端控制定義*/
  27. #include <errno.h> /*錯誤號定義*/
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <math.h>
  35. #include "../../define.h"
  36. #include "Config.h"
  37. #include <stdbool.h>
  38. #include <dirent.h>
  39. #include "timeout.h"
  40. #include "Module_EvComm.h"
  41. #include "Module_CabinetParallel.h"
  42. #include "Common.h"
  43. #define BTN_RELEASE 0
  44. #define BTN_PRESS 1
  45. #define MAX_BUF 64
  46. #define MtdBlockSize 0x300000
  47. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  48. #define UPGRADE_FAN 0x02
  49. #define UPGRADE_RB 0x03
  50. #define UPGRADE_PRI 0x04
  51. #define UPGRADE_AC 0x05
  52. #define UPGRADE_LED 0x06
  53. #define UPGRADE_DORC1 0x07
  54. #define UPGRADE_DORC2 0x08
  55. #define UPGRADE_DDRB 0x09
  56. #define SYSTEM_MIN_VOL 150
  57. #define MIN_OUTPUT_CUR 0
  58. #define AC_OUTPUT_VOL 220
  59. #define NO_DEFINE 255
  60. #define DEFAULT_AC_INDEX 2
  61. #define PSU_MIN_CUR 100
  62. #define MAIN_PRIORITY_INTERVAL 5
  63. #define CABINET_ROLE_TIMEOUT 3 // unit: 1s
  64. #define GUN_PREPARE_TIMEOUT 30
  65. #define GUN_EV_WAIT_TIMEOUT (120 + 10)
  66. #define GUN_EVSE_WAIT_TIMEOUT (60 + 10)
  67. #define GUN_COMP_WAIT_TIMEOUT 10
  68. #define GUN_PRECHARGING_TIMEOUT 60
  69. #define REBOOT_TIMEOUT 10
  70. #define RESET_TIMEOUT 10
  71. #define OCPP_COST_REQ_INTERVAL 1
  72. #define DISPENSER_RECONNECTE_TIME 30
  73. #define DISPENSER_AUTH_COMP_TIME 3
  74. #define FORCE_BALANCE_TIME 5
  75. #define DISPENSER_UPGRADE_TIME 600
  76. #define AC_CONTACTOR_REOPEN_TIME 60
  77. #define PSU_FAILURE_RESUME_TIME 180
  78. #define MAX_AC_CONTACTOR_COUNT 3
  79. #define STANDBY_TIME 600
  80. #define FAST_STANDBY_TIME 30
  81. #define WIRING_INFO_DELAY 30
  82. #define WAIT_REASSIGN_TIME 30 // unit: second
  83. #define MAX_REQUEST_CHARGING_TIME 15 // unit: second
  84. #define MIN_WAIT_PREPARNING_TIME 1 // unit: second
  85. #define FORCE_CHARGING_WAIT_EVSE 10 // unit: second
  86. #define TRY_RESET_4G_WIFI_TIME 300 // unit: second
  87. #define FAST_RESET_4G_WIFI_TIME 60 // unit: second
  88. #define ENERAGY_INTERVAL 1 // unit: second
  89. #define FAULT_RELEASE_TIME 1 // unit: second
  90. #define MAINTAIN_RECOVERY_TIME 1 // unit: second
  91. #define PARALLEL_WAIT_TIME 3 // unit: second
  92. #define START_UPGRADE_TIME 60 // unit: second
  93. #define PCABINET_UPGRADE_TIME 300 // unit: second
  94. #define MCABINET_UPGRADE_TIME 60 // unit: second
  95. #define CONNECTION_TIME_OFFSET 5 // unit: second
  96. #define AUTO_START_CHARGING "AutoStartCharging"
  97. #define CONTACTOR_AUTO_ON_OFF 0
  98. #define CONTACTOR_AUTO_ON_TIME 60
  99. #define CONTACTOR_AUTO_OFF_TIME 300
  100. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  101. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  102. int whileLoopTime = 10000; // 10 ms
  103. int wtdFd = -1;
  104. byte _authorizeIndex = NO_DEFINE;
  105. BOOL _UpgradeNeedReboot = FALSE;
  106. const byte Group_to_Location_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  107. const byte Location_to_Group_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  108. const byte GroupParallelRelay_2_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  109. {
  110. {0, 1, 0, 0},
  111. {1, 0, 0, 0},
  112. {0, 0, 0, 0},
  113. {0, 0, 0, 0}
  114. };
  115. //Location: 1 2 3 4
  116. // Gun: 1 3 4 2
  117. //Parallel: 1 2 3
  118. //const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 1, 2};
  119. //const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 2, 3, 1};
  120. //const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  121. //{
  122. // {0, 3, 1, 2}, // group 0
  123. // {1, 0, 2, 3}, // group 1
  124. // {1, 3, 0, 2}, // group 2
  125. // {1, 3, 2, 0} // group 3
  126. //};
  127. //Location: 1 2 3 4
  128. // Gun: 1 4 3 2
  129. //Parallel: 1 2 3
  130. const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  131. const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  132. const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  133. {
  134. {0, 3, 2, 1}, // group 0
  135. {1, 0, 3, 2}, // group 1
  136. {1, 3, 0, 2}, // group 2
  137. {1, 3, 2, 0} // group 3
  138. };
  139. long long DiffTimebWithNow(struct timeb ST);
  140. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  141. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  142. void ChargingTerminalProcess(byte gunIndex);
  143. void ChkPrimaryStatus();
  144. void ChargingAlarmProcess(byte gunIndex);
  145. void InformOcppErrOccur(byte codeType);
  146. void SetOcppErrorCode(unsigned char _index, char *errCode);
  147. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode);
  148. void ReleaseAlarmCode(byte gunIndex);
  149. int DB_Open(sqlite3 *db);
  150. int DB_Insert_Record(sqlite3 *db, int gun_index);
  151. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  152. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  153. void DB_Update_PowerConsumption(sqlite3 *db, uint8_t gun_index, float value);
  154. float DB_Get_PowerConsumption(sqlite3 *db, uint8_t gun_index);
  155. int DB_Reboot_Record(sqlite3 *db);
  156. void InitialDHCP();
  157. void InitialDispenserDhcpServerConfig(void);
  158. void StartDispenserDhcpServer(void);
  159. int GetStartScheduleTime(unsigned char *time);
  160. void CheckConnectionTimeout(void);
  161. bool IsParallelCabinetIdle(void);
  162. bool IsStandbyEnable(void);
  163. bool IsConnectorWholeIdle(void);
  164. void SetAcContactor(unsigned char OnOff);
  165. void UpdateErrorCodeToOcpp(byte index);
  166. bool CheckConnectorTypeStatus(void);
  167. void CreateWatchdog(void);
  168. void TryCloseWatchdog(void);
  169. void TryFeedWatchdog(void);
  170. bool IsReadyParalleling(void);
  171. void SetParallelingStart(void);
  172. void SetParallelingDone(void);
  173. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff);
  174. void SetParallelCabinetAcContactor(unsigned char OnOff);
  175. unsigned short GetTotalParallelCabinetPower(void);
  176. void MCabinetParallelingProcess(void);
  177. void SCabinetControllProcess(void);
  178. bool Get_Ocpp_ConnectionStatus(void);
  179. void Run_Ocpp_Service(void);
  180. bool Is_Ocpp_Task_Alive(void);
  181. void Kill_Ocpp_Service(void);
  182. void Run_MaintainOcpp_Service(void);
  183. bool Is_MaintainOcpp_Task_Alive(void);
  184. void Kill_MaintainOcpp_Service(void);
  185. unsigned int Get_Ocpp_procDogTime(void);
  186. void Clean_Ocpp_procDogTime(void);
  187. bool Is_Ocpp_BootNotification(void);
  188. void Clean_Ocpp_BootNotification(void);
  189. bool Is_Ocpp_ResetReq(void);
  190. bool Chk_Ocpp_Reset_Type(char *type);
  191. void Clean_Ocpp_ResetReq(void);
  192. void Set_Ocpp_AuthorizeReq(void);
  193. bool Is_Ocpp_AuthorizeConf(void);
  194. bool Is_Ocpp_Authorize_Status(char *status);
  195. bool Is_Ocpp_UpdateFirmwareReq(void);
  196. void Clean_Ocpp_UpdateFirmwareReq(void);
  197. bool Is_Ocpp_UpdateFirmwareStatus(char *status);
  198. void Set_Ocpp_UpdateFirmwareStatus(char *status);
  199. void Clean_Ocpp_UpdateFirmwareStatus(char *status);
  200. void Set_Ocpp_FirmwareStatusNotificationReq(void);
  201. bool Is_Ocpp_StopOnInvalidId_Enable(void);
  202. bool Is_Ocpp_AuthorizeRemoteTxRequests(void);
  203. int Get_Ocpp_ConnectionTimeOut(void);
  204. void Get_Ocpp_DefaultPrice(char *defaultPrice);
  205. void Get_Ocpp_UserPrice(char * userId, char *userPrice);
  206. void Clean_Ocpp_UserPrice(void);
  207. void Get_Ocpp_RunningCost(int gun_index, char *cost);
  208. void Clean_Ocpp_RunningCost(int gun_index);
  209. void Get_Ocpp_FinalCost(int gun_index, char *cost);
  210. void Clean_Ocpp_FinalCost(int gun_index);
  211. void Get_Ocpp_TimeOffset(char *timeOffset);
  212. void Get_Ocpp_Refunding(char *Refunding);
  213. void Clean_Ocpp_Refunding(void);
  214. bool Is_Ocpp_LcdOverideReq(int *gun, int *page);
  215. void Clean_Ocpp_LcdOverideReq(void);
  216. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name);
  217. bool Is_Ocpp_ChargerStationReq(void);
  218. void Clean_Ocpp_ChargerStationReq(void);
  219. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature);
  220. bool Is_Ocpp_WeatherReq(void);
  221. void Clean_Ocpp_WeatherReq(void);
  222. void Set_Ocpp_DeductTransaction(int transaction);
  223. void Set_Ocpp_DeductCreditNo(char *userId);
  224. void Set_Ocpp_DeductVemData(char *vemData);
  225. void Set_Ocpp_DeductAmount(double value);
  226. void Set_Ocpp_DeductResult(unsigned char result);
  227. void Set_Ocpp_DonateInvoice(unsigned char donate);
  228. bool Is_Ocpp_ReportCreditDeductReq(void);
  229. void Set_Ocpp_ReportCreditDeductReq(void);
  230. void Clean_Ocpp_TcciSerialNo(int gun_index);
  231. void Set_Ocpp_TcciReaderStatus(int gun_index, int status, char *cardNo);
  232. void Set_Ocpp_TcciReaderStatusReq(int gun_index);
  233. void Set_Ocpp_StartTransactionId(int gun_index, char *id);
  234. void Set_Ocpp_StartTransactionReq(int gun_index);
  235. bool Is_Ocpp_StartTransactionConf(int gun_index);
  236. void Clean_Ocpp_StartTransactionConf(int gun_index);
  237. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status);
  238. bool Is_Ocpp_StartTransaction_Accepted(int gun_index);
  239. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status);
  240. void Get_Ocpp_TransactionId(int gun_index, char *transactionId);
  241. bool Is_Invalid_StartTransactionId(int gun_index);
  242. void Set_Ocpp_StopTransactionId(int gun_index, char *id);
  243. void Set_Ocpp_StopTransactionReq(int gun_index);
  244. void Chk_Ocpp_Backup_ErrorCode(int gun_index);
  245. bool Is_Ocpp_ReserveNowReq(int gun_index);
  246. void Clean_Ocpp_ReserveNowReq(int gun_index);
  247. int Get_Ocpp_ReserveNowId(int gun_index);
  248. void Set_Ocpp_ReserveNowConf(int gun_index);
  249. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date);
  250. bool Is_Ocpp_ReserveNowExpired(int gun_index);
  251. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag);
  252. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag);
  253. bool Is_Ocpp_CancelReservationReq(int gun_index);
  254. void Clean_Ocpp_CancelReservationReq(int gun_index);
  255. void Set_Ocpp_CancelReservationConf(int gun_index);
  256. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index);
  257. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index);
  258. bool Chk_Ocpp_Availability_Type(int gun_index, char *type);
  259. bool Is_Ocpp_UnlockConnectorReq(int gun_index);
  260. void Clean_Ocpp_UnlockConnectorReq(int gun_index);
  261. void Set_Ocpp_UnlockConnectorConf(int gun_index);
  262. bool Is_Ocpp_RemoteStartReq(int gun_index);
  263. void Clean_Ocpp_RemoteStartReq(int gun_index);
  264. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag);
  265. bool Is_Ocpp_RemoteStopReq(int gun_index);
  266. void Clean_Ocpp_RemoteStopReq(int gun_index);
  267. void Set_Ocpp_StopReason(int gun_index, char *reason);
  268. void Get_Ocpp_StopReason(int gun_index, char *reason);
  269. void Clean_Ocpp_StopReason(int gun_index);
  270. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  271. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  272. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  273. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  274. bool Is_Ocpp_ChargingProfileConf(int gun_index);
  275. void Clean_Ocpp_ChargingProfileConf(int gun_index);
  276. bool Is_Ocpp_ChargingProfileReq(int gun_index);
  277. void Set_Ocpp_ChargingProfileReq(int gun_index);
  278. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind);
  279. bool Is_Ocpp_ChargingProfileId(int gun_index);
  280. int Get_Ocpp_ChargingProfileStartTime(int gun_index);
  281. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index);
  282. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index);
  283. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index);
  284. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index);
  285. int Get_Ocpp_ChargingProfileStartIndex(int gun_index);
  286. bool Is_Ocpp_MaxChargingProfileKind(char *kind);
  287. bool Is_Ocpp_MaxChargingProfileId(void);
  288. int Get_Ocpp_MaxProfileStartTime(void);
  289. int Get_Ocpp_MaxProfileScheduleCount(void);
  290. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index);
  291. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index);
  292. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index);
  293. int Get_Ocpp_MaxProfileStartIndex(void);
  294. int Is_Ocpp_RemoteStartWaitReq(void);
  295. void Set_Deduct_UserId(int gunIndex, char *userId);
  296. void Set_Deduct_TransactionId(int gunIndex, int transaction);
  297. void Clean_DeductInfo(int gunIndex);
  298. bool Is_DeductResultReq(int gunIndex);
  299. void Set_UserTransactionId(int gunIndex, int transaction);
  300. void Set_UserPrice(int gunIndex, float price);
  301. void Set_UserTotalCost(int gunIndex, float cost);
  302. void Set_UserAccountBalance(int gunIndex, float balance);
  303. void Set_UserDiscount(int gunIndex, float discount);
  304. void Clean_UserTransaction(int gunIndex);
  305. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  306. struct StatusCodeData *ShmStatusCodeData;
  307. struct PsuData *ShmPsuData;
  308. struct CHAdeMOData *ShmCHAdeMOData;
  309. struct GBTData *ShmGBTData;
  310. struct CcsData *ShmCcsData;
  311. struct PrimaryMcuData *ShmPrimaryMcuData;
  312. struct FanModuleData *ShmFanModuleData;
  313. struct RelayModuleData *ShmRelayModuleData[2];
  314. struct LedModuleData *ShmLedModuleData;
  315. struct OCPP16Data *ShmOCPP16Data;
  316. struct OCPP20Data *ShmOCPP20Data;
  317. struct MeterInformation *ShmCsuMeterData;
  318. ChargerInfoData *ShmChargerInfo;
  319. PsuPositionInfoData *ShmPsuPosition;
  320. PsuGroupingInfoData *ShmPsuGrouping;
  321. struct ChargingInfoData *chargingInfo[CONNECTOR_QUANTITY];
  322. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  323. struct timespec _cmdMainPriority_time;
  324. struct timespec _cmdSubPriority_time;
  325. struct timespec _ConnectorAuthorizing_Time[CONNECTOR_QUANTITY];
  326. struct timespec _ConnectorAuthorizeFail_Time[CONNECTOR_QUANTITY];
  327. struct timespec _DispenserAuthorizing_Time[CONNECTOR_QUANTITY];
  328. struct timespec _DispenserUpgrade_time;
  329. struct timespec _Standby_time;
  330. struct timespec _SystemStatus_Time[CONNECTOR_QUANTITY];
  331. struct timespec _Parallel_Time;
  332. struct timespec _RemoteStartNoId_Time;
  333. float gunOutputVol[CONNECTOR_QUANTITY];
  334. unsigned char _ocppProfileChkFlag[CONNECTOR_QUANTITY];
  335. float _lastProfilePower[CONNECTOR_QUANTITY];
  336. float _lastProfileCurrent[CONNECTOR_QUANTITY];
  337. float _lastMaxProfilePower;
  338. struct StructStatusNotification _BackupStatusNotification[CONNECTOR_QUANTITY];
  339. DeductResultInfoData _BackupDeductResult[CONNECTOR_QUANTITY];
  340. bool _NeedReset4gWifi;
  341. struct timespec _4gWifiReset_time;
  342. // for initial index to check EV board type is correct
  343. byte _gunIndex = 0;
  344. byte _acgunIndex = 0;
  345. byte _chademoIndex = 0;
  346. byte _ccsIndex = 0;
  347. byte _gb_Index = 0;
  348. byte _ac_Index = 0;
  349. bool _isStandbyEnable = true;
  350. bool isModelNameMatch = true;
  351. //int rfidFd = -1;
  352. //char* rfidPortName = "/dev/ttyS2";
  353. char* fwVersion = "V1.10.00.0000.00";
  354. char* subVersion = "02";
  355. sqlite3 *localDb;
  356. bool isDb_ready;
  357. sqlite3 *networkDb;
  358. //================================================
  359. // initial can-bus
  360. //================================================
  361. int InitCanBus()
  362. {
  363. int s0,nbytes;
  364. struct timeval tv;
  365. struct ifreq ifr0;
  366. struct sockaddr_can addr0;
  367. system("/sbin/ip link set can0 down");
  368. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  369. system("/sbin/ip link set can0 up");
  370. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  371. tv.tv_sec = 0;
  372. tv.tv_usec = 10000;
  373. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  374. {
  375. #ifdef SystemLogMessage
  376. LOG_ERROR("Set SO_RCVTIMEO NG");
  377. #endif
  378. }
  379. nbytes=40960;
  380. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  381. {
  382. #ifdef SystemLogMessage
  383. LOG_ERROR("Set SO_RCVBUF NG");
  384. #endif
  385. }
  386. nbytes=40960;
  387. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  388. {
  389. #ifdef SystemLogMessage
  390. LOG_ERROR("Set SO_SNDBUF NG");
  391. #endif
  392. }
  393. strcpy(ifr0.ifr_name, "can0" );
  394. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  395. addr0.can_family = AF_CAN;
  396. addr0.can_ifindex = ifr0.ifr_ifindex;
  397. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  398. return s0;
  399. }
  400. //================================================
  401. // initial uart port
  402. //================================================
  403. char *_priPortName = "/dev/ttyS1";
  404. char *_485PortName = "/dev/ttyS5";
  405. int InitComPort(byte target)
  406. {
  407. int fd;
  408. struct termios tios;
  409. if(target == UPGRADE_PRI)
  410. fd = open(_priPortName, O_RDWR);
  411. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  412. fd = open(_485PortName, O_RDWR);
  413. if(fd<=0)
  414. {
  415. #ifdef SystemLogMessage
  416. LOG_ERROR("open 407 Communication port NG");
  417. #endif
  418. return -1;
  419. }
  420. ioctl (fd, TCGETS, &tios);
  421. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  422. tios.c_lflag = 0;
  423. tios.c_iflag = 0;
  424. tios.c_oflag = 0;
  425. tios.c_cc[VMIN]=0;
  426. tios.c_cc[VTIME]=(unsigned char)1;
  427. tios.c_lflag=0;
  428. tcflush(fd, TCIFLUSH);
  429. ioctl (fd, TCSETS, &tios);
  430. return fd;
  431. }
  432. //=================================
  433. // Common routine
  434. //=================================
  435. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  436. {
  437. strncpy(dest, src + start, cnt);
  438. dest[cnt] = 0;
  439. }
  440. int InitWatchDog()
  441. {
  442. int fd;
  443. int timeout = 180;
  444. system("/usr/bin/fuser -k /dev/watchdog");
  445. sleep(1);
  446. system("echo V > /dev/watchdog");
  447. sleep(1);
  448. fd=open("/dev/watchdog", O_RDWR);
  449. if(fd<=0)
  450. {
  451. LOG_ERROR("System watch dog initial fail.\r\n");
  452. }
  453. ioctl(fd, _IOWR('W', 6, int), &timeout);
  454. return fd;
  455. }
  456. void setChargerMode(byte gun_index, byte mode)
  457. {
  458. chargingInfo[gun_index]->SystemStatus = mode;
  459. }
  460. long long DiffTimebWithNow(struct timeb ST)
  461. {
  462. //return milli-second
  463. struct timeb ET;
  464. long long StartTime,StopTime;
  465. ftime(&ET);
  466. StartTime=(long long)ST.time;
  467. StopTime=(long long)ET.time;
  468. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  469. }
  470. //=================================
  471. // Create all share memory
  472. //=================================
  473. int CreateShareMemory()
  474. {
  475. int MeterSMId;
  476. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  477. {
  478. #ifdef SystemLogMessage
  479. LOG_ERROR("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG");
  480. #endif
  481. return 0;
  482. }
  483. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  484. {
  485. #ifdef SystemLogMessage
  486. LOG_ERROR("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG");
  487. #endif
  488. return 0;
  489. }
  490. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  491. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  492. {
  493. #ifdef SystemLogMessage
  494. LOG_ERROR("[main]CreatShareMemory:shmget ShmStatusCodeData NG");
  495. #endif
  496. return 0;
  497. }
  498. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  499. {
  500. #ifdef SystemLogMessage
  501. LOG_ERROR("[main]CreatShareMemory:shmat ShmStatusCodeData NG");
  502. #endif
  503. return 0;
  504. }
  505. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  506. //creat ShmPsuData
  507. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  508. {
  509. #ifdef SystemLogMessage
  510. LOG_ERROR("[main]CreatShareMemory:shmget ShmPsuData NG");
  511. #endif
  512. return 0;
  513. }
  514. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  515. {
  516. #ifdef SystemLogMessage
  517. LOG_ERROR("[main]CreatShareMemory:shmat ShmPsuData NG");
  518. #endif
  519. return 0;
  520. }
  521. memset(ShmPsuData, 0, sizeof(struct PsuData));
  522. /*
  523. if(CHAdeMO_QUANTITY > 0)
  524. {
  525. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  526. {
  527. #ifdef SystemLogMessage
  528. LOG_ERROR("[main]CreatShareMemory:shmget ShmCHAdeMOData NG");
  529. #endif
  530. return 0;
  531. }
  532. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  533. {
  534. #ifdef SystemLogMessage
  535. LOG_ERROR("[main]CreatShareMemory:shmat ShmCHAdeMOData NG");
  536. #endif
  537. return 0;
  538. }
  539. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  540. }
  541. if(GB_QUANTITY > 0)
  542. {
  543. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  544. {
  545. #ifdef SystemLogMessage
  546. LOG_ERROR("[main]CreatShareMemory:shmget ShmGBTData NG");
  547. #endif
  548. return 0;
  549. }
  550. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  551. {
  552. #ifdef SystemLogMessage
  553. LOG_ERROR("[main]CreatShareMemory:shmat ShmGBTData NG");
  554. #endif
  555. return 0;
  556. }
  557. memset(ShmGBTData, 0, sizeof(struct GBTData));
  558. }
  559. //creat ShmCcsData
  560. if(CCS_QUANTITY > 0)
  561. {
  562. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  563. {
  564. #ifdef SystemLogMessage
  565. LOG_ERROR("[main]CreatShareMemory:shmget ShmCcsData NG");
  566. #endif
  567. return 0;
  568. }
  569. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  570. {
  571. #ifdef SystemLogMessage
  572. LOG_ERROR("[main]CreatShareMemory:shmat ShmCcsData NG");
  573. #endif
  574. return 0;
  575. }
  576. memset(ShmCcsData, 0, sizeof(struct CcsData));
  577. }
  578. */
  579. //creat ShmPrimaryMcuData
  580. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  581. {
  582. #ifdef SystemLogMessage
  583. LOG_ERROR("[main]CreatShareMemory:shmget ShmPrimaryMcuData NGn");
  584. #endif
  585. return 0;
  586. }
  587. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  588. {
  589. #ifdef SystemLogMessage
  590. LOG_ERROR("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG");
  591. #endif
  592. return 0;
  593. }
  594. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  595. //creat ShmFanModuleData
  596. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  597. {
  598. #ifdef SystemLogMessage
  599. LOG_ERROR("[main]CreatShareMemory:shmget ShmFanModuleData NG");
  600. #endif
  601. return 0;
  602. }
  603. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  604. {
  605. #ifdef SystemLogMessage
  606. LOG_ERROR("[main]CreatShareMemory:shmat ShmFanModuleData NG");
  607. #endif
  608. return 0;
  609. }
  610. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  611. //creat ShmRelayModuleData
  612. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  613. {
  614. #ifdef SystemLogMessage
  615. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[0] NG");
  616. #endif
  617. return 0;
  618. }
  619. else if ((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  620. {
  621. #ifdef SystemLogMessage
  622. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[0] NG");
  623. #endif
  624. return 0;
  625. }
  626. memset(ShmRelayModuleData[0], 0, sizeof(struct RelayModuleData));
  627. // DO360 RC2
  628. if ((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  629. {
  630. #ifdef SystemLogMessage
  631. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[1] NG");
  632. #endif
  633. return 0;
  634. }
  635. else if ((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  636. {
  637. #ifdef SystemLogMessage
  638. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[1] NG");
  639. #endif
  640. return 0;
  641. }
  642. memset(ShmRelayModuleData[1], 0, sizeof(struct RelayModuleData));
  643. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  644. {
  645. #ifdef SystemLogMessage
  646. LOG_ERROR("[main]CreatShareMemory:shmget ShmLedModuleData NG");
  647. #endif
  648. return 0;
  649. }
  650. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  651. {
  652. #ifdef SystemLogMessage
  653. LOG_ERROR("[main]CreatShareMemory:shmat ShmLedModuleData NG");
  654. #endif
  655. return 0;
  656. }
  657. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  658. //creat ShmOCPP16Data
  659. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  660. {
  661. #ifdef SystemLogMessage
  662. LOG_ERROR("[main]CreatShareMemory:shmget ShmOCPP16Data NG");
  663. #endif
  664. return 0;
  665. }
  666. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  667. {
  668. #ifdef SystemLogMessage
  669. LOG_ERROR("[main]CreatShareMemory:shmat ShmOCPP16Data NG");
  670. #endif
  671. return 0;
  672. }
  673. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  674. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  675. {
  676. LOG_ERROR("[main]CreatShareMemory:shmget OCPP20Data NG\n");
  677. return 0;
  678. }
  679. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  680. {
  681. LOG_ERROR("[main]CreatShareMemory:shmat OCPP20Data NG\n");
  682. return 0;
  683. }
  684. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  685. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), IPC_CREAT | 0777)) < 0)
  686. {
  687. #ifdef SystemLogMessage
  688. LOG_ERROR("[main]CreatShareMemory:shmget ChargerInfoData NG");
  689. #endif
  690. return 0;
  691. }
  692. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  693. {
  694. #ifdef SystemLogMessage
  695. LOG_ERROR("[main]CreatShareMemory:shmat ChargerInfoData NG");
  696. #endif
  697. return 0;
  698. }
  699. memset(ShmChargerInfo, 0, sizeof(ChargerInfoData));
  700. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  701. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  702. return 1;
  703. }
  704. //======================================================
  705. // Peripheral initial
  706. //======================================================
  707. void InitGPIO()
  708. {
  709. /*****************0~3, 4 bank, bank x 32+ num*********************/
  710. /***************************************************************/
  711. /*************** GPIO 0 ***************************************/
  712. /***************************************************************/
  713. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/ // no use
  714. system("echo 22 > /sys/class/gpio/export");
  715. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  716. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/ // no use
  717. system("echo 23 > /sys/class/gpio/export");
  718. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  719. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/ // no use
  720. system("echo 26 > /sys/class/gpio/export");
  721. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  722. system("echo 1 > /sys/class/gpio/gpio26/value");
  723. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/ // no use
  724. system("echo 27 > /sys/class/gpio/export");
  725. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  726. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  727. system("echo 29 > /sys/class/gpio/export");
  728. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  729. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/ // no use
  730. system("echo 19 > /sys/class/gpio/export");
  731. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  732. system("echo 1 > /sys/class/gpio/gpio19/value");
  733. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/ // no use
  734. system("echo 20 > /sys/class/gpio/export");
  735. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  736. /***************************************************************/
  737. /*************** GPIO 1 ***************************************/
  738. /***************************************************************/
  739. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/ // no use
  740. system("echo 44 > /sys/class/gpio/export");
  741. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  742. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/ // no use
  743. system("echo 45 > /sys/class/gpio/export");
  744. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  745. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/ // no use
  746. system("echo 46 > /sys/class/gpio/export");
  747. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  748. system("echo 0 > /sys/class/gpio/gpio46/value");
  749. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/ // no use
  750. system("echo 47 > /sys/class/gpio/export");
  751. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  752. /***************************************************************/
  753. /*************** GPIO 2 ***************************************/
  754. /***************************************************************/
  755. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  756. system("echo 89 > /sys/class/gpio/export");
  757. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  758. system("echo 1 > /sys/class/gpio/gpio89/value");
  759. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  760. system("echo 87 > /sys/class/gpio/export");
  761. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  762. system("echo 0 > /sys/class/gpio/gpio87/value");
  763. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/ // no use
  764. system("echo 88 > /sys/class/gpio/export");
  765. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  766. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/ // no use
  767. system("echo 86 > /sys/class/gpio/export");
  768. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  769. /***************************************************************/
  770. /*************** GPIO 3 ***************************************/
  771. /***************************************************************/
  772. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  773. system("echo 111 > /sys/class/gpio/export");
  774. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  775. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  776. system("echo 114 > /sys/class/gpio/export");
  777. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  778. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  779. system("echo 113 > /sys/class/gpio/export");
  780. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  781. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  782. system("echo 110 > /sys/class/gpio/export");
  783. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  784. system("echo 0 > /sys/class/gpio/gpio110/value");
  785. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/ // no use
  786. system("echo 115 > /sys/class/gpio/export");
  787. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  788. system("echo 0 > /sys/class/gpio/gpio115/value");
  789. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  790. system("echo 112 > /sys/class/gpio/export");
  791. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  792. system("echo 1 > /sys/class/gpio/gpio112/value");
  793. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/ // no use
  794. system("echo 116 > /sys/class/gpio/export");
  795. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  796. system("echo 0 > /sys/class/gpio/gpio116/value");
  797. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  798. system("echo 103 > /sys/class/gpio/export");
  799. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  800. system("echo 0 > /sys/class/gpio/gpio103/value");
  801. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  802. system("echo 104 > /sys/class/gpio/export");
  803. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  804. system("echo 0 > /sys/class/gpio/gpio104/value");
  805. LOG_INFO("[main]InitGPIO: Initial GPIO OK");
  806. }
  807. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  808. {
  809. int fd, wrd;
  810. unsigned char *buf;
  811. unsigned int ChkSum, ChkSumOrg;
  812. if((buf = malloc(MtdBlockSize)) == NULL)
  813. {
  814. LOG_ERROR("malloc buffer NG, rebooting..\n");
  815. if(ShmStatusCodeData!=NULL)
  816. {
  817. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  818. }
  819. sleep(5);
  820. system("reboot -f");
  821. sleep(5);
  822. system("reboot -f");
  823. }
  824. memset(buf, 0, MtdBlockSize);
  825. //================================================
  826. // Load configuration from mtdblock10
  827. //================================================
  828. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  829. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  830. if(fd < 0)
  831. {
  832. free(buf);
  833. LOG_ERROR("open mtdblock10 NG, rebooting..\n");
  834. if(ShmStatusCodeData != NULL)
  835. {
  836. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  837. }
  838. sleep(5);
  839. system("reboot -f");
  840. sleep(5);
  841. system("reboot -f");
  842. }
  843. wrd = read(fd, buf, MtdBlockSize);
  844. close(fd);
  845. if(wrd < MtdBlockSize)
  846. {
  847. free(buf);
  848. LOG_ERROR("read SysConfigData data NG, rebooting..\n");
  849. if(ShmStatusCodeData != NULL)
  850. {
  851. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  852. }
  853. sleep(5);
  854. system("reboot -f");
  855. sleep(5);
  856. system("reboot -f");
  857. }
  858. ChkSum = 0;
  859. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  860. {
  861. ChkSum += buf[wrd];
  862. }
  863. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  864. memcpy(&ptr->ModelName, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), ARRAY_SIZE(ptr->ModelName));
  865. memcpy(&ptr->SerialNumber, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), ARRAY_SIZE(ptr->SerialNumber));
  866. //================================================
  867. // Load configuration from mtdblock11
  868. //================================================
  869. if(ChkSum != ChkSumOrg)
  870. {
  871. LOG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  872. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  873. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  874. if(fd < 0)
  875. {
  876. free(buf);
  877. LOG_ERROR("open mtdblock11 (backup) NG, rebooting..\n");
  878. if(ShmStatusCodeData != NULL)
  879. {
  880. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  881. }
  882. sleep(5);
  883. system("reboot -f");
  884. sleep(5);
  885. system("reboot -f");
  886. }
  887. memset(buf, 0, MtdBlockSize);
  888. wrd = read(fd, buf, MtdBlockSize);
  889. close(fd);
  890. if(wrd < MtdBlockSize)
  891. {
  892. free(buf);
  893. LOG_ERROR("read backup SysConfigData data NG, rebooting..\n");
  894. if(ShmStatusCodeData != NULL)
  895. {
  896. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  897. }
  898. sleep(5);
  899. system("reboot -f");
  900. sleep(5);
  901. system("reboot -f");
  902. }
  903. ChkSum = 0;
  904. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  905. {
  906. ChkSum += buf[wrd];
  907. }
  908. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  909. //================================================
  910. // Load configuration from mtdblock12 (Factory default)
  911. //================================================
  912. if(ChkSum != ChkSumOrg)
  913. {
  914. LOG_ERROR("backup SysConfigData checksum NG, read Factory default\n");
  915. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  916. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  917. if(fd < 0)
  918. {
  919. free(buf);
  920. LOG_ERROR("open mtdblock12 (Factory default) NG, rebooting..\n");
  921. if(ShmStatusCodeData != NULL)
  922. {
  923. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  924. }
  925. sleep(5);
  926. system("reboot -f");
  927. sleep(5);
  928. system("reboot -f");
  929. }
  930. memset(buf, 0, MtdBlockSize);
  931. wrd = read(fd, buf, MtdBlockSize);
  932. close(fd);
  933. if(wrd < MtdBlockSize)
  934. {
  935. free(buf);
  936. LOG_ERROR("read factory default SysConfigData data NG, rebooting..\n");
  937. if(ShmStatusCodeData != NULL)
  938. {
  939. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  940. }
  941. sleep(5);
  942. system("reboot -f");
  943. sleep(5);
  944. system("reboot -f");
  945. }
  946. ChkSum = 0;
  947. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  948. {
  949. ChkSum += buf[wrd];
  950. }
  951. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  952. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  953. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  954. if(ChkSum != ChkSumOrg)
  955. {
  956. LOG_ERROR("factory default SysConfigData checksum NG, restore factory default\n");
  957. free(buf);
  958. system("cd /root;./FactoryConfig -m");
  959. system("rm -f /Storage/OCPP/OCPPConfiguration");
  960. system("sync");
  961. sleep(5);
  962. system("reboot -f");
  963. sleep(5);
  964. system("reboot -f");
  965. return FAIL;
  966. }
  967. }
  968. }
  969. //load OK
  970. memcpy((struct SysConfigData *)ptr, buf, sizeof(struct SysConfigData));
  971. free(buf);
  972. system("rm -f /mnt/EvseConfig.bin");
  973. // SysConfig in flash is empty (0xffffffff)
  974. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  975. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  976. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  977. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  978. {
  979. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  980. {
  981. LOG_INFO("Model name over length.");
  982. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  983. }
  984. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  985. {
  986. LOG_INFO("Model serial number over length.");
  987. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  988. }
  989. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  990. {
  991. LOG_INFO("System id over length.");
  992. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  993. }
  994. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  995. {
  996. LOG_INFO("Ethernet dhcp config is null.");
  997. }
  998. system("cd /root;./Module_FactoryConfig -m");
  999. sleep(3);
  1000. system("/usr/bin/run_evse_restart.sh");
  1001. }
  1002. LOG_INFO("Load SysConfigData OK");
  1003. return PASS;
  1004. }
  1005. int isEthernetCablePlugIn(int ethernet)
  1006. {
  1007. int fd, PlugInStatus = 0;
  1008. char ch[2];
  1009. char buffer[64];
  1010. snprintf(buffer, sizeof(buffer), "/sys/class/net/eth%d/carrier", ethernet);
  1011. fd = open(buffer, O_RDONLY);
  1012. if(fd < 0)
  1013. {
  1014. return -1;
  1015. }
  1016. if(read(fd, ch, 1) < 0)
  1017. {
  1018. return -1;
  1019. }
  1020. PlugInStatus = atoi(ch);
  1021. close(fd);
  1022. return PlugInStatus;
  1023. }
  1024. int isRouteFail()
  1025. {
  1026. int result = YES;
  1027. FILE *fp;
  1028. char buf[512];
  1029. fp = popen("route -n", "r");
  1030. if(fp != NULL)
  1031. {
  1032. while(fgets(buf, sizeof(buf), fp) != NULL)
  1033. {
  1034. if(strstr(buf, "eth0") != NULL)
  1035. result = NO;
  1036. }
  1037. }
  1038. pclose(fp);
  1039. return result;
  1040. }
  1041. int isReachableInternet()
  1042. {
  1043. int result = FAIL;
  1044. FILE *fp;
  1045. char cmd[256];
  1046. char buf[512];
  1047. char tmp[512];
  1048. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1049. // {
  1050. // result = PASS;
  1051. // }
  1052. // else
  1053. {
  1054. strcpy(cmd, "ifconfig eth0");
  1055. fp = popen(cmd, "r");
  1056. if (fp != NULL)
  1057. {
  1058. while(fgets(buf, sizeof(buf), fp) != NULL)
  1059. {
  1060. if (strstr(buf, "inet addr:") > 0)
  1061. {
  1062. sscanf(buf, "%*s%s", tmp);
  1063. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  1064. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  1065. {
  1066. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  1067. }
  1068. }
  1069. }
  1070. }
  1071. pclose(fp);
  1072. memset(buf, 0x00, sizeof(buf));
  1073. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1074. {
  1075. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1076. fp = popen(cmd, "r");
  1077. if(fp != NULL)
  1078. {
  1079. while(fgets(buf, sizeof(buf), fp) != NULL)
  1080. {
  1081. if(strstr(buf, "transmitted") > 0)
  1082. {
  1083. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1084. if(strstr(buf,"100%") != NULL)
  1085. {
  1086. }
  1087. else
  1088. {
  1089. result = PASS;
  1090. }
  1091. //DEBUG_INFO("%s",buf);
  1092. //DEBUG_INFO("%s\n",tmp);
  1093. }
  1094. }
  1095. }
  1096. pclose(fp);
  1097. }
  1098. }
  1099. return result;
  1100. }
  1101. bool Is_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1102. {
  1103. return (ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue & misc_cmd) > 0 ? true : false;
  1104. }
  1105. void Set_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1106. {
  1107. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue |= misc_cmd;
  1108. }
  1109. void Clean_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1110. {
  1111. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue &= ~misc_cmd;
  1112. }
  1113. bool Is_Connector_MiscCommand(int connector, int misc_cmd)
  1114. {
  1115. return (ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue & misc_cmd) > 0 ? true : false;
  1116. }
  1117. void Set_Connector_MiscCommand(int connector, int misc_cmd)
  1118. {
  1119. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue |= misc_cmd;
  1120. }
  1121. void Clean_Connector_MiscCommand(int connector, int misc_cmd)
  1122. {
  1123. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue &= ~misc_cmd;
  1124. }
  1125. void AnnounceEthernetStatus(void)
  1126. {
  1127. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1128. {
  1129. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1130. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1131. {
  1132. Set_Dispenser_MiscCommand(index, MISC_DISP_ETHERNET_STATUS);
  1133. }
  1134. }
  1135. }
  1136. void AnnounceWiFiStatus(void)
  1137. {
  1138. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1139. {
  1140. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1141. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1142. {
  1143. Set_Dispenser_MiscCommand(index, MISC_DISP_WIFI_STATUS);
  1144. }
  1145. }
  1146. }
  1147. void AnnounceTelcomModemStatus(void)
  1148. {
  1149. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1150. {
  1151. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1152. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1153. {
  1154. Set_Dispenser_MiscCommand(index, MISC_DISP_TELCOM_MODEM_STATUS);
  1155. }
  1156. }
  1157. }
  1158. void AnnounceBackendStatus(void)
  1159. {
  1160. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1161. {
  1162. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1163. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1164. {
  1165. Set_Dispenser_MiscCommand(index, MISC_DISP_BACKEND_STATUS);
  1166. }
  1167. }
  1168. }
  1169. void AnnounceBillingStatus(void)
  1170. {
  1171. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1172. {
  1173. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1174. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1175. {
  1176. Set_Dispenser_MiscCommand(index, MISC_DISP_BILLING_STATUS);
  1177. }
  1178. }
  1179. }
  1180. void AnnounceStopChargingButtonStatus(void)
  1181. {
  1182. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1183. {
  1184. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1185. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1186. {
  1187. Set_Dispenser_MiscCommand(index, MISC_DISP_STOP_BUTTON_STATUS);
  1188. }
  1189. }
  1190. }
  1191. void AnnounceAuthModeConfig(void)
  1192. {
  1193. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1194. {
  1195. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1196. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1197. {
  1198. Set_Dispenser_MiscCommand(index, MISC_DISP_AUTH_MODE_CONFIG);
  1199. }
  1200. }
  1201. }
  1202. void AnnounceEVCCIDConfig(void)
  1203. {
  1204. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1205. {
  1206. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1207. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1208. {
  1209. Set_Dispenser_MiscCommand(index, MISC_DISP_EVCCID_CONFIG);
  1210. }
  1211. }
  1212. }
  1213. void AnnounceLEDIntensity(void)
  1214. {
  1215. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1216. {
  1217. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1218. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1219. {
  1220. Set_Dispenser_MiscCommand(index, MISC_DISP_LED_INTENSITY);
  1221. }
  1222. }
  1223. }
  1224. void AnnounceCurrency(void)
  1225. {
  1226. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1227. {
  1228. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1229. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1230. {
  1231. Set_Dispenser_MiscCommand(index, MISC_DISP_CURRENCY);
  1232. }
  1233. }
  1234. }
  1235. void AnnounceRemoteStartNoID(void)
  1236. {
  1237. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  1238. {
  1239. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1240. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1241. {
  1242. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  1243. {
  1244. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  1245. if(chargingInfo[gun]->SystemStatus == S_IDLE)
  1246. {
  1247. ShmChargerInfo->ConnectorPageReq[gun].bits.RemoteStartNoID = true;
  1248. break;
  1249. }
  1250. }
  1251. }
  1252. }
  1253. }
  1254. void AnnounceLineStatusCode(int gun, int status)
  1255. {
  1256. int dispenser = 0;
  1257. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].ParentDispensetIndex;
  1258. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_None &&
  1259. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  1260. {
  1261. ShmChargerInfo->LineApi.StatusCode[gun] = status;
  1262. Set_Connector_MiscCommand(gun, MISC_CONN_LINE_STATUS);
  1263. }
  1264. }
  1265. void AnnounceStationInfo(void)
  1266. {
  1267. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1268. {
  1269. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1270. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1271. {
  1272. Set_Dispenser_MiscCommand(index, MISC_DISP_STATION_INFO);
  1273. }
  1274. }
  1275. }
  1276. void UpdateConfigAndIcon(void)
  1277. {
  1278. unsigned char status = _Connnection_Disable;
  1279. // update ethernet connection status
  1280. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1281. if(ShmChargerInfo->CabinetMiscValue.EthernetStatus != status)
  1282. {
  1283. ShmChargerInfo->CabinetMiscValue.EthernetStatus = status;
  1284. AnnounceEthernetStatus();
  1285. }
  1286. // update wifi connection status
  1287. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1288. {
  1289. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1290. }
  1291. else
  1292. {
  1293. status = _Connnection_Disable;
  1294. }
  1295. if(ShmChargerInfo->CabinetMiscValue.WiFiStatus != status)
  1296. {
  1297. ShmChargerInfo->CabinetMiscValue.WiFiStatus = status;
  1298. AnnounceWiFiStatus();
  1299. }
  1300. // update 4g connection status
  1301. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1302. {
  1303. status = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1304. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? _Connnection_Connected : _Connnection_Disconnected;
  1305. }
  1306. else
  1307. {
  1308. status = _Connnection_Disable;
  1309. }
  1310. if(ShmChargerInfo->CabinetMiscValue.TelcomModemStatus != status)
  1311. {
  1312. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = status;
  1313. AnnounceTelcomModemStatus();
  1314. }
  1315. // update backend connection status
  1316. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1317. if(ShmChargerInfo->CabinetMiscValue.BackendStatus != status)
  1318. {
  1319. ShmChargerInfo->CabinetMiscValue.BackendStatus = status;
  1320. AnnounceBackendStatus();
  1321. }
  1322. // update billing status
  1323. status = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1324. if(ShmChargerInfo->CabinetMiscValue.BillingStatus != status)
  1325. {
  1326. ShmChargerInfo->CabinetMiscValue.BillingStatus = status;
  1327. AnnounceBillingStatus();
  1328. }
  1329. // update stop charging button
  1330. status = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1331. if(ShmChargerInfo->CabinetMiscValue.StopChargingButton != status)
  1332. {
  1333. ShmChargerInfo->CabinetMiscValue.StopChargingButton = status;
  1334. AnnounceStopChargingButtonStatus();
  1335. }
  1336. // update AuthMode config
  1337. status = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  1338. if(ShmChargerInfo->CabinetMiscValue.AuthModeConfig != status)
  1339. {
  1340. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = status;
  1341. AnnounceAuthModeConfig();
  1342. AnnounceEVCCIDConfig();
  1343. }
  1344. // update EVCCID config
  1345. status = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  1346. if(ShmChargerInfo->CabinetMiscValue.EVCCIDConfig != status)
  1347. {
  1348. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = status;
  1349. AnnounceEVCCIDConfig();
  1350. }
  1351. // update LED Intensity
  1352. status = ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity;
  1353. if(ShmChargerInfo->CabinetMiscValue.LEDIntensity != status)
  1354. {
  1355. ShmChargerInfo->CabinetMiscValue.LEDIntensity = status;
  1356. AnnounceLEDIntensity();
  1357. }
  1358. // update currency
  1359. status = ShmSysConfigAndInfo->SysConfig.BillingData.Currency;
  1360. if(ShmChargerInfo->CabinetMiscValue.Currency != status)
  1361. {
  1362. ShmChargerInfo->CabinetMiscValue.Currency = status;
  1363. AnnounceCurrency();
  1364. }
  1365. }
  1366. void Check4gWifiReset(void)
  1367. {
  1368. int timeDelay = 0;
  1369. if(ShmChargerInfo->Control.SysCtrl.bits.BackendEnable)
  1370. {
  1371. if((!ShmSysConfigAndInfo->SysInfo.InternetConn && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  1372. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE ||
  1373. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)) ||
  1374. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES)
  1375. {
  1376. if(!_NeedReset4gWifi)
  1377. {
  1378. GetClockTime(&_4gWifiReset_time);
  1379. LOG_INFO("Backend Enable But Internet Not Available!");
  1380. }
  1381. _NeedReset4gWifi = true;
  1382. }
  1383. else
  1384. {
  1385. _NeedReset4gWifi = false;
  1386. }
  1387. if(_NeedReset4gWifi)
  1388. {
  1389. timeDelay = ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES ?
  1390. FAST_RESET_4G_WIFI_TIME : TRY_RESET_4G_WIFI_TIME;
  1391. if(GetTimeoutValue(_4gWifiReset_time) / uSEC_VAL >= timeDelay)
  1392. {
  1393. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = true;
  1394. usleep(100000);
  1395. // before reset 4g io, kill Module_4g & Module_Wifi task
  1396. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1397. {
  1398. system("killall Module_4g &");
  1399. }
  1400. usleep(100000);
  1401. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1402. {
  1403. system("killall Module_Wifi &");
  1404. }
  1405. LOG_INFO("Reset 4g_Wifi Module");
  1406. system("echo 1 > /sys/class/gpio/gpio104/value");
  1407. sleep(1);
  1408. system("echo 0 > /sys/class/gpio/gpio104/value");
  1409. _NeedReset4gWifi = false;
  1410. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = false;
  1411. }
  1412. }
  1413. }
  1414. else
  1415. {
  1416. _NeedReset4gWifi = false;
  1417. }
  1418. }
  1419. void MasterCabinetDhcpClientConfig(void)
  1420. {
  1421. char tmpbuf[256];
  1422. //Run DHCP client if enabled
  1423. system("killall udhcpc");
  1424. system("rm -rf /etc/resolv.conf");
  1425. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1426. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  1427. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1428. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1429. {
  1430. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1431. system(tmpbuf);
  1432. }
  1433. //Upgrade system id to /etc/hostname
  1434. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1435. system(tmpbuf);
  1436. }
  1437. void InitMasterCabinetEthernet(void)
  1438. {
  1439. char tmpbuf[256];
  1440. //Init Eth0 for internet
  1441. memset(tmpbuf,0,256);
  1442. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1443. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1444. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1445. system(tmpbuf);
  1446. memset(tmpbuf,0,256);
  1447. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1448. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1449. system(tmpbuf);
  1450. //system("ifconfig lo up");
  1451. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1452. //Init Eth1 for administrator tool
  1453. memset(tmpbuf,0,256);
  1454. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", "192.168.100.1", "255.255.255.0");
  1455. system(tmpbuf);
  1456. }
  1457. void InitSlaveCabinetEthernet(void)
  1458. {
  1459. char tmpbuf[256];
  1460. system("/sbin/ifconfig eth0 down");
  1461. system("/sbin/ifconfig eth1 down");
  1462. system("killall udhcpc");
  1463. system("rm -rf /etc/resolv.conf");
  1464. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  1465. //Init Eth0 for internet
  1466. memset(tmpbuf,0,256);
  1467. sprintf(tmpbuf,"/sbin/ifconfig eth0 192.168.100.3%d netmask %s up",
  1468. (ShmChargerInfo->Control.CabinetSwitch - 1),
  1469. "255.255.255.0");
  1470. system(tmpbuf);
  1471. system("/sbin/route add default gw 192.168.100.1 eth0");
  1472. //Init Eth1 for internet
  1473. memset(tmpbuf,0,256);
  1474. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", SLAVE_ETH1_IP, SLAVE_ETH1_SUBMASK);
  1475. system(tmpbuf);
  1476. }
  1477. void InitEthernet(void)
  1478. {
  1479. system("ifconfig eth0 down");
  1480. system("ifconfig eth1 down");
  1481. sleep(2);
  1482. char tmpbuf[256];
  1483. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1484. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1485. sleep(2);
  1486. InitMasterCabinetEthernet();
  1487. MasterCabinetDhcpClientConfig();
  1488. // DO360 DHCP Server
  1489. InitialDispenserDhcpServerConfig();
  1490. StartDispenserDhcpServer();
  1491. pid_t pid = fork();
  1492. uint8_t cnt_pingDNS_Fail;
  1493. if(pid == 0)
  1494. {
  1495. ShmChargerInfo->CabinetMiscValue.BackendStatus = _Connnection_Disconnected;
  1496. ShmChargerInfo->CabinetMiscValue.EthernetStatus = _Connnection_Disconnected;
  1497. ShmChargerInfo->CabinetMiscValue.WiFiStatus = _Connnection_Disable;
  1498. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = _Connnection_Disable;
  1499. LOG_INFO("Internet Fork PID = %d", getpid());
  1500. for(;;)
  1501. {
  1502. if(isEthernetCablePlugIn(0) == YES)
  1503. {
  1504. if(isRouteFail())
  1505. {
  1506. LOG_INFO("eth0 not in route, restart eht0.");
  1507. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1508. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1509. {
  1510. InitialDHCP();
  1511. }
  1512. else
  1513. {
  1514. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1515. memset(tmpbuf,0,256);
  1516. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1517. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1518. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1519. system(tmpbuf);
  1520. memset(tmpbuf,0,256);
  1521. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1522. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1523. system(tmpbuf);
  1524. }
  1525. }
  1526. }
  1527. if(isReachableInternet() == PASS)
  1528. {
  1529. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != NO)
  1530. {
  1531. LOG_INFO("Internet is Reachable");
  1532. }
  1533. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = NO;
  1534. ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1535. cnt_pingDNS_Fail = 0;
  1536. }
  1537. else
  1538. {
  1539. if(++cnt_pingDNS_Fail > 3)
  1540. {
  1541. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != YES)
  1542. {
  1543. LOG_INFO("Internet is Disconnect Via Ethernet");
  1544. }
  1545. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1546. ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1547. }
  1548. }
  1549. bool ethResult = ShmSysConfigAndInfo->SysInfo.ethInternetConn;
  1550. if(ethResult == YES)
  1551. {
  1552. system("/sbin/ifmetric eth0 0");
  1553. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1554. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1555. {
  1556. system("/sbin/ifmetric mlan0 1");
  1557. }
  1558. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1559. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1560. {
  1561. system("/sbin/ifmetric ppp0 2");
  1562. }
  1563. }
  1564. if (!ethResult &&
  1565. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1566. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1567. {
  1568. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1569. if (ethResult)
  1570. {
  1571. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1572. {
  1573. system("/sbin/ifmetric eth0 1");
  1574. system("/sbin/ifmetric mlan0 0");
  1575. }
  1576. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1577. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1578. {
  1579. system("/sbin/ifmetric ppp0 2");
  1580. }
  1581. }
  1582. }
  1583. if (!ethResult &&
  1584. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1585. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1586. {
  1587. ethResult = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1588. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? YES : NO;
  1589. if (ethResult)
  1590. {
  1591. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1592. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1593. {
  1594. system("/sbin/ifmetric mlan0 2");
  1595. }
  1596. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1597. {
  1598. system("/sbin/ifmetric eth0 1");
  1599. system("/sbin/ifmetric ppp0 0");
  1600. }
  1601. }
  1602. }
  1603. if(!Is_Ocpp_Task_Alive())
  1604. {
  1605. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = false;
  1606. }
  1607. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1608. ShmStatusCodeData->InfoCode.InfoEvents.bits.WiFiDisable = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == false ? true : false;
  1609. ShmStatusCodeData->InfoCode.InfoEvents.bits.Telocom4GModuleDisable = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == false ? true : false;
  1610. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet = ShmSysConfigAndInfo->SysInfo.OcppConnStatus == false ? true : false;
  1611. UpdateConfigAndIcon();
  1612. Check4gWifiReset();
  1613. sleep(5);
  1614. }
  1615. }
  1616. #ifdef SystemLogMessage
  1617. LOG_INFO("[main]InitEthernet: Initial Ethernet OK.");
  1618. #endif
  1619. }
  1620. void GetMacAddress()
  1621. {
  1622. for (byte index = 0; index < 2; index++)
  1623. {
  1624. int fd;
  1625. struct ifreq ifr;
  1626. char tarEth[5];
  1627. char Mac[18];
  1628. sprintf(tarEth,"eth%d",index);
  1629. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1630. ifr.ifr_addr.sa_family = AF_INET;
  1631. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1632. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1633. close(fd);
  1634. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1635. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1636. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1637. if (index == 0)
  1638. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1639. else
  1640. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1641. }
  1642. }
  1643. void GetFirmwareVersion()
  1644. {
  1645. // Get CSU root file system version
  1646. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1647. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1648. for(uint8_t idx=0;idx<3;idx++)
  1649. {
  1650. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J')
  1651. {
  1652. chademo++;
  1653. count++;
  1654. }
  1655. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1656. {
  1657. gb++;
  1658. count++;
  1659. }
  1660. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  1661. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E')
  1662. {
  1663. ccs++;
  1664. count++;
  1665. }
  1666. }
  1667. if (count == 1)
  1668. {
  1669. if (chademo > 0)
  1670. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1671. else if (ccs > 0)
  1672. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1673. else if (gb > 0)
  1674. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1675. }
  1676. else
  1677. {
  1678. if (chademo > 0 && ccs > 0)
  1679. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1680. else if (chademo > 0 && gb > 0)
  1681. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1682. else if (ccs > 0 && gb > 0)
  1683. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1684. }
  1685. // Get network option from model name
  1686. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1687. {
  1688. case 'B':
  1689. case 'U':
  1690. //Blue tooth
  1691. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1692. break;
  1693. case 'W':
  1694. // WIFI
  1695. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1696. break;
  1697. case 'T':
  1698. // 3G/4G
  1699. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1700. break;
  1701. case 'D':
  1702. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1703. break;
  1704. default:
  1705. // LAN
  1706. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1707. break;
  1708. }
  1709. // Get rating power from model name
  1710. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1711. // Get IEC or UL
  1712. char _buf[3] = {0};
  1713. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  1714. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL)
  1715. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1716. else if (strcmp(_buf, "WU") == EQUAL)
  1717. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1718. }
  1719. void InitialShareMemoryInfo()
  1720. {
  1721. FILE *fp;
  1722. char cmd[512];
  1723. char buf[512];
  1724. _UpgradeNeedReboot = FALSE;
  1725. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1726. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1727. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1728. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1729. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1730. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1731. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1732. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1733. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1734. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1735. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1736. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1737. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1738. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1739. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1740. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1741. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1742. sprintf(cmd, "/bin/uname -r");
  1743. fp = popen(cmd, "r");
  1744. if(fp == NULL)
  1745. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1746. else
  1747. {
  1748. while(fgets(buf, sizeof(buf), fp) != NULL)
  1749. {
  1750. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1751. }
  1752. }
  1753. // 雙槍 CCS + Chademo
  1754. GetFirmwareVersion();
  1755. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1756. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1757. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1758. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1759. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1760. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1761. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1762. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1763. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  1764. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  1765. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  1766. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  1767. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  1768. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleHwRev, " "); //DO360 RC2
  1769. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, " "); //DO360 RC2
  1770. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  1771. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, " ");
  1772. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector1FwRev, " ");
  1773. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector2FwRev, " ");
  1774. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  1775. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  1776. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  1777. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  1778. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  1779. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  1780. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  1781. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  1782. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  1783. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  1784. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  1785. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  1786. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  1787. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  1788. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  1789. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  1790. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  1791. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  1792. memset(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev)); //DO360 RC2
  1793. ShmPrimaryMcuData->SelfTest_Comp = NO;
  1794. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  1795. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  1796. ShmFanModuleData->SelfTest_Comp = NO;
  1797. ShmLedModuleData->SelfTest_Comp = NO;
  1798. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  1799. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  1800. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  1801. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  1802. ShmFanModuleData->TestFanSpeed = 0;
  1803. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  1804. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  1805. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  1806. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  1807. LOG_INFO("DispenserSequence = %d, MaxConnectorQuantity = %d",
  1808. ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence,
  1809. ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity);
  1810. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  1811. {
  1812. LOG_INFO("Dispenser[%d] <=> %d", i, ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[i]);
  1813. }
  1814. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  1815. {
  1816. memset((char *)&_BackupStatusNotification[i], 0, sizeof(struct StructStatusNotification));
  1817. memset((char *)&_BackupDeductResult[i], 0, sizeof(DeductResultInfoData));
  1818. }
  1819. }
  1820. int Initialization(void)
  1821. {
  1822. // int pinOut[2] = { 116, 115 };
  1823. for (byte count = 0; count < CONNECTOR_QUANTITY; count++)
  1824. {
  1825. chargingInfo[count]->RemoteStartFlag = NO;
  1826. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStartConfirm = false;
  1827. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStopConfirm = false;
  1828. SetOcppErrorCode(count, "NoError");
  1829. }
  1830. LOG_INFO("Initialization OK");
  1831. return PASS;
  1832. }
  1833. void InitialPsuGroupingAndLocation(void)
  1834. {
  1835. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1836. {
  1837. memcpy(ShmPsuGrouping->Location, Group_to_Location_4_Gun, sizeof(ShmPsuGrouping->Location));
  1838. memcpy(ShmPsuGrouping->Layout, Location_to_Group_4_Gun, sizeof(ShmPsuGrouping->Layout));
  1839. }
  1840. else
  1841. {
  1842. memcpy(ShmPsuGrouping->Location, Group_to_Location_2_Gun, sizeof(ShmPsuGrouping->Location));
  1843. memcpy(ShmPsuGrouping->Layout, Location_to_Group_2_Gun, sizeof(ShmPsuGrouping->Layout));
  1844. }
  1845. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  1846. {
  1847. ShmPsuGrouping->GroupCollection[i].Index = i;
  1848. ShmPsuGrouping->GroupCollection[i].Location = ShmPsuGrouping->Location[i];
  1849. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  1850. {
  1851. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun[i][0], GENERAL_GUN_QUANTITY);
  1852. }
  1853. else
  1854. {
  1855. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_2_Gun[i][0], GENERAL_GUN_QUANTITY);
  1856. }
  1857. ShmChargerInfo->Control.GunAvailable[i] = NO;
  1858. }
  1859. }
  1860. void InitialCustomerCode(void)
  1861. {
  1862. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  1863. {
  1864. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Audi;
  1865. }
  1866. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'E' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == '4')
  1867. {
  1868. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_E4U;
  1869. }
  1870. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'U' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'U')
  1871. {
  1872. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_UCar;
  1873. }
  1874. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'T' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'C')
  1875. {
  1876. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_TCC;
  1877. }
  1878. else
  1879. {
  1880. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Phihong;
  1881. }
  1882. }
  1883. void InitialMaxDispenserConnectorQuantity(void)
  1884. {
  1885. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable == false)
  1886. {
  1887. ShmChargerInfo->Control.MaxDispenser = 2;
  1888. ShmChargerInfo->Control.MaxConnector = 2;
  1889. }
  1890. else
  1891. {
  1892. ShmChargerInfo->Control.MaxDispenser = GENERAL_GUN_QUANTITY;
  1893. ShmChargerInfo->Control.MaxConnector = GENERAL_GUN_QUANTITY;
  1894. }
  1895. }
  1896. void InitialAuthMode(void)
  1897. {
  1898. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  1899. {
  1900. ShmChargerInfo->AuthInfo.AuthMode.bits.AutoStartEnable = true;
  1901. }
  1902. }
  1903. void InitialQRCodeMode(void)
  1904. {
  1905. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi ||
  1906. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  1907. {
  1908. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_ChargeBoxId;
  1909. }
  1910. else
  1911. {
  1912. // customized
  1913. if(ShmSysConfigAndInfo->SysConfig.QRCodeMadeMode)
  1914. {
  1915. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Customized;
  1916. }
  1917. else
  1918. {
  1919. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Default;
  1920. }
  1921. }
  1922. }
  1923. void InitialPowerCabinetSetting(void)
  1924. {
  1925. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  1926. {
  1927. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = true;
  1928. }
  1929. ShmChargerInfo->CabinetMiscValue.BillingStatus = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1930. ShmChargerInfo->CabinetMiscValue.StopChargingButton = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1931. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  1932. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  1933. }
  1934. bool InitialChargerSetting(void)
  1935. {
  1936. bool result = true;
  1937. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'O')
  1938. {
  1939. ShmChargerInfo->Control.PrimaryCtrl.bits.AcContactorReverse = true;
  1940. ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse = true;
  1941. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  1942. ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable = true;
  1943. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  1944. }
  1945. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  1946. {
  1947. ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  1948. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  1949. ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  1950. }
  1951. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'B')
  1952. {
  1953. //ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  1954. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  1955. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  1956. //ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  1957. ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable = true;
  1958. ShmChargerInfo->Control.RelayCtrl.bits.DcInputEnable = true;
  1959. }
  1960. else
  1961. {
  1962. result = false;
  1963. }
  1964. InitialCustomerCode();
  1965. InitialMaxDispenserConnectorQuantity();
  1966. InitialAuthMode();
  1967. InitialQRCodeMode();
  1968. InitialPsuGroupingAndLocation();
  1969. InitialPowerCabinetSetting();
  1970. if(!CheckConnectorTypeStatus())
  1971. {
  1972. result = false;
  1973. }
  1974. return result;
  1975. }
  1976. bool InitialSystemDefaultConfig()
  1977. {
  1978. bool result = true;
  1979. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1980. InitGPIO();
  1981. InitEthernet();
  1982. GetMacAddress();
  1983. return result;
  1984. }
  1985. void SelfTestRun(void)
  1986. {
  1987. LOG_INFO("Self test.");
  1988. system("/root/Module_ChargerSelfTest &");
  1989. }
  1990. void SpawnNormalService(void)
  1991. {
  1992. system("/root/Module_EventLogging &");
  1993. system("/root/Module_PrimaryComm &");
  1994. system("/root/Module_EvComm &");
  1995. //system("/root/Module_LcmControl &");
  1996. system("/root/Module_InternalComm &");
  1997. system("/root/Module_PsuComm &");
  1998. system("/root/Module_ProduceUtils &");
  1999. system("/root/Module_CabinetParallel &");
  2000. system("/root/Module_LedIndication &");
  2001. system("/root/Module_Authorize &");
  2002. }
  2003. void SpawnOcppService(void)
  2004. {
  2005. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  2006. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  2007. {
  2008. Run_Ocpp_Service();
  2009. }
  2010. }
  2011. void SpawnMaintainOcppService(void)
  2012. {
  2013. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  2014. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  2015. {
  2016. Run_MaintainOcpp_Service();
  2017. }
  2018. }
  2019. void SpawnInternetService(void)
  2020. {
  2021. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  2022. {
  2023. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2024. {
  2025. system("/root/Module_4g &");
  2026. }
  2027. }
  2028. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2029. {
  2030. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2031. {
  2032. system("/root/Module_Wifi &");
  2033. }
  2034. }
  2035. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  2036. {
  2037. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2038. {
  2039. system("/root/Module_4g &");
  2040. }
  2041. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2042. {
  2043. system("/root/Module_Wifi &");
  2044. }
  2045. }
  2046. }
  2047. int SpawnTask(void)
  2048. {
  2049. sleep(2);
  2050. SpawnNormalService();
  2051. SpawnInternetService();
  2052. return PASS;
  2053. }
  2054. int runFlashShellCmd(const char *cmd)
  2055. {
  2056. int result = FAIL;
  2057. char buf[256];
  2058. FILE *fp;
  2059. fp = popen(cmd, "r");
  2060. if(fp != NULL)
  2061. {
  2062. while(fgets(buf, sizeof(buf), fp) != NULL)
  2063. {
  2064. LOG_INFO("%s\n", buf);
  2065. }
  2066. result = PASS;
  2067. }
  2068. pclose(fp);
  2069. return result;
  2070. }
  2071. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2072. {
  2073. int result = PASS;
  2074. int fd, wrd;
  2075. unsigned int i, Chk;
  2076. unsigned char *ptr, *BufTmp;
  2077. Chk = 0;
  2078. ptr = (unsigned char *)UsrData;
  2079. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2080. {
  2081. memset(BufTmp, 0, MtdBlockSize);
  2082. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2083. for(i = ARRAY_SIZE(UsrData->CsuBootLoadFwRev); i < MtdBlockSize - 4; i++)
  2084. {
  2085. Chk += *(BufTmp+i);
  2086. }
  2087. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  2088. // Output configuration to file.
  2089. fd = open("/mnt/EvseTempConfig.bin", O_RDWR | O_CREAT | O_TRUNC);
  2090. if(fd < 0)
  2091. {
  2092. LOG_ERROR("open /mnt/EvseTempConfig.bin NG");
  2093. free(BufTmp);
  2094. return 0;
  2095. }
  2096. wrd = write(fd, BufTmp, MtdBlockSize);
  2097. close(fd);
  2098. if(wrd < MtdBlockSize)
  2099. {
  2100. LOG_ERROR("write /mnt/EvseTempConfig.bin NG");
  2101. free(BufTmp);
  2102. return 0;
  2103. }
  2104. LOG_INFO("EvseTempConfig write to file in /mnt OK.");
  2105. LOG_INFO("Erase /dev/mtd10.");
  2106. runFlashShellCmd("flash_erase /dev/mtd10 0 0");
  2107. LOG_INFO("Write /dev/mtd10.");
  2108. runFlashShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseTempConfig.bin");
  2109. LOG_INFO("Erase /dev/mtd11.");
  2110. runFlashShellCmd("flash_erase /dev/mtd11 0 0");
  2111. LOG_INFO("Write /dev/mtd11.");
  2112. runFlashShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseTempConfig.bin");
  2113. system("rm -f /mnt/EvseTempConfig.bin");
  2114. LOG_INFO("EvseTempConfig write to flash OK");
  2115. }
  2116. else
  2117. {
  2118. LOG_ERROR("alloc BlockSize NG");
  2119. result = FAIL;
  2120. }
  2121. if(BufTmp != NULL)
  2122. {
  2123. free(BufTmp);
  2124. }
  2125. return result;
  2126. }
  2127. //===============================================
  2128. // Common Detect Chk - Stop Charging ?
  2129. //===============================================
  2130. bool isEvBoardStopChargeFlag(byte gunIndex)
  2131. {
  2132. return chargingInfo[gunIndex]->ChargingStopFlag.bits.AlarmStop;
  2133. }
  2134. bool isNormalStopChargeFlag(byte gunIndex)
  2135. {
  2136. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.NormalStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.ManualStop);
  2137. }
  2138. bool isResetStopChargeFlag(byte gunIndex)
  2139. {
  2140. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.HardResetStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.SoftResetStop);
  2141. }
  2142. bool isInvalidIdStopFlag(byte gunIndex)
  2143. {
  2144. return chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop;
  2145. }
  2146. void ReleaseAlarmCode(byte gunIndex)
  2147. {
  2148. if(!Is_Ocpp_StatusNotificationErrorCode(gunIndex, "NoError"))
  2149. {
  2150. SetOcppErrorCode(gunIndex, "NoError");
  2151. }
  2152. if(!Is_Ocpp_StatusNotificationVendorCode(gunIndex, ""))
  2153. {
  2154. SetOcppVendorErrorCode(gunIndex, "");
  2155. }
  2156. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  2157. {
  2158. strcpy((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "");
  2159. }
  2160. if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  2161. {
  2162. strcpy((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "");
  2163. }
  2164. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "") != EQUAL)
  2165. {
  2166. strcpy((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "");
  2167. }
  2168. }
  2169. //===============================================
  2170. // EmergencyStop and Charging Stop
  2171. //===============================================
  2172. void ChargingTerminalProcess(byte gunIndex)
  2173. {
  2174. setChargerMode(gunIndex, MODE_TERMINATING);
  2175. }
  2176. void ChargingAlarmProcess(byte gunIndex)
  2177. {
  2178. UpdateErrorCodeToOcpp(gunIndex);
  2179. setChargerMode(gunIndex, MODE_ALARM);
  2180. }
  2181. void AcChargingTerminalProcess()
  2182. {
  2183. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2184. }
  2185. void StopChargingProcessByString(byte level)
  2186. {
  2187. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2188. {
  2189. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2190. }
  2191. }
  2192. void ReleaseChargingProcessByString(byte level)
  2193. {
  2194. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2195. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2196. }
  2197. // 一般錯誤停止充電處理函式
  2198. void BoardErrOccurByString(byte index, char *code)
  2199. {
  2200. byte level = 1;
  2201. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2202. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2203. {
  2204. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2205. {
  2206. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2207. }
  2208. ChargingTerminalProcess(index);
  2209. }
  2210. StopChargingProcessByString(level);
  2211. }
  2212. // 急停狀況的停止充電處理函式
  2213. void EmcOccureByString(char *code)
  2214. {
  2215. byte level = 2;
  2216. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  2217. {
  2218. return;
  2219. }
  2220. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2221. // 其錯誤等級為 2
  2222. if (strncmp(code, "042251", 6) == EQUAL || strncmp(code, "042252", 6) == EQUAL ||
  2223. strncmp(code, "042238", 6) == EQUAL || strncmp(code, "042267", 6) == EQUAL ||
  2224. strncmp(code, "042328", 6) == EQUAL)
  2225. {
  2226. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2227. {
  2228. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2229. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2230. {
  2231. if (strncmp((char *)chargingInfo[gun]->ConnectorAlarmCode, "", 6) == EQUAL)
  2232. {
  2233. memcpy(chargingInfo[gun]->ConnectorAlarmCode, code, 6);
  2234. }
  2235. InformOcppErrOccur(4);
  2236. ChargingAlarmProcess(gun);
  2237. }
  2238. }
  2239. StopChargingProcessByString(level);
  2240. }
  2241. }
  2242. void ReleaseEmsOccureByString(byte index, char *code)
  2243. {
  2244. bool isTrigger = false;
  2245. byte level = 2;
  2246. if (strncmp(code, "042251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2247. {
  2248. isTrigger = true;
  2249. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2250. }
  2251. else if (strncmp(code, "042252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2252. {
  2253. isTrigger = true;
  2254. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2255. }
  2256. else if (strncmp(code, "042237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2257. {
  2258. isTrigger = true;
  2259. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2260. }
  2261. else if (strncmp(code, "042238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2262. {
  2263. isTrigger = true;
  2264. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2265. }
  2266. else if (strncmp(code, "042267", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm == YES)
  2267. {
  2268. isTrigger = true;
  2269. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  2270. }
  2271. if (isTrigger)
  2272. {
  2273. ReleaseChargingProcessByString(level);
  2274. }
  2275. }
  2276. //===============================================
  2277. // 確認硬體 (按鈕) 狀態
  2278. //===============================================
  2279. void ChkPrimaryStatus()
  2280. {
  2281. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  2282. {
  2283. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2284. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2285. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2286. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2287. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2288. return;
  2289. }
  2290. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2291. {
  2292. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2293. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = YES;
  2294. EmcOccureByString("042251");
  2295. }
  2296. else
  2297. {
  2298. ReleaseEmsOccureByString(0, "042251");
  2299. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2300. }
  2301. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2302. {
  2303. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2304. //EmcOccureByString("042238");
  2305. }
  2306. else
  2307. {
  2308. //ReleaseEmsOccureByString(0, "042238");
  2309. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2310. }
  2311. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2312. {
  2313. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2314. }
  2315. else
  2316. {
  2317. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2318. }
  2319. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2320. {
  2321. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2322. EmcOccureByString("042252");
  2323. }
  2324. else
  2325. {
  2326. ReleaseEmsOccureByString(0, "042252");
  2327. }
  2328. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2329. {
  2330. EmcOccureByString("042328");
  2331. SetAcContactor(OFF);
  2332. }
  2333. }
  2334. struct timespec _AcContactReOpen_Time;
  2335. void ChkPsuStatus(void)
  2336. {
  2337. if(!ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2338. {
  2339. for(int psuGroup = 0; psuGroup < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; psuGroup++)
  2340. {
  2341. if(ShmPsuData->PsuGroup[psuGroup].GroupErrorFlag.PsuGroupErrorValue != 0)
  2342. {
  2343. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2344. }
  2345. }
  2346. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2347. {
  2348. EmcOccureByString("042267");
  2349. }
  2350. }
  2351. else
  2352. {
  2353. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor &&
  2354. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume == NO)
  2355. {
  2356. bool allFaultStatus = true;
  2357. for(int gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2358. {
  2359. if(chargingInfo[gunIndex]->SystemStatus != S_FAULT && chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  2360. {
  2361. allFaultStatus = false;
  2362. }
  2363. }
  2364. if(allFaultStatus)
  2365. {
  2366. LOG_INFO("PSU Failure, Need Resume");
  2367. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = YES;
  2368. }
  2369. }
  2370. }
  2371. }
  2372. int AcContactorReOpenCount = 0;
  2373. void PsuFailureResume(void)
  2374. {
  2375. if(ShmChargerInfo->Control.PsuCtrl.bits.FailureResume)
  2376. {
  2377. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO)
  2378. {
  2379. LOG_INFO("Ac Contactor Off By Psu Failure");
  2380. // update _AcContactReOpen_Time
  2381. GetClockTime(&_AcContactReOpen_Time);
  2382. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = YES;
  2383. AcContactorReOpenCount++;
  2384. }
  2385. else
  2386. {
  2387. if(AcContactorReOpenCount < MAX_AC_CONTACTOR_COUNT)
  2388. {
  2389. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= AC_CONTACTOR_REOPEN_TIME)
  2390. {
  2391. ReleaseEmsOccureByString(0, "042267");
  2392. // update _AcContactReOpen_Time
  2393. GetClockTime(&_AcContactReOpen_Time);
  2394. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = NO;
  2395. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = NO;
  2396. LOG_INFO("Psu Failure Resume");
  2397. }
  2398. }
  2399. }
  2400. }
  2401. else
  2402. {
  2403. if(AcContactorReOpenCount > 0)
  2404. {
  2405. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= PSU_FAILURE_RESUME_TIME)
  2406. {
  2407. AcContactorReOpenCount = 0;
  2408. }
  2409. }
  2410. }
  2411. }
  2412. void SetAcContactor(unsigned char OnOff)
  2413. {
  2414. OnOff = OnOff ? ON : OFF;
  2415. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor != OnOff)
  2416. {
  2417. if(OnOff)
  2418. {
  2419. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2420. }
  2421. LOG_INFO("Set Ac Contactor %s", OnOff == ON ? "On" : "Off");
  2422. }
  2423. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = OnOff == ON ? ON : OFF;
  2424. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER && ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  2425. {
  2426. SetParallelCabinetAcContactor(OnOff);
  2427. }
  2428. }
  2429. int GetStandbyTime(void)
  2430. {
  2431. int time = STANDBY_TIME;
  2432. if(ShmChargerInfo->Control.TestCtrl.bits.FastStandbyTime)
  2433. {
  2434. time = FAST_STANDBY_TIME;
  2435. }
  2436. return time;
  2437. }
  2438. void StandbyCheck(void)
  2439. {
  2440. bool enable = IsStandbyEnable();
  2441. if(IsConnectorWholeIdle() && IsParallelCabinetIdle())
  2442. {
  2443. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2444. {
  2445. int time = GetStandbyTime();
  2446. #if CONTACTOR_AUTO_ON_OFF == 1
  2447. time = CONTACTOR_AUTO_ON_TIME;
  2448. #endif
  2449. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor)
  2450. {
  2451. if(GetSecTimeoutValue(_Standby_time) >= time && enable)
  2452. {
  2453. SetAcContactor(OFF);
  2454. }
  2455. }
  2456. #if CONTACTOR_AUTO_ON_OFF == 1
  2457. time = CONTACTOR_AUTO_ON_TIME + CONTACTOR_AUTO_OFF_TIME;
  2458. if((GetTimeoutValue(_Standby_time) / uSEC_VAL) >= time)
  2459. {
  2460. SetAcContactor(ON);
  2461. }
  2462. #endif
  2463. }
  2464. else
  2465. {
  2466. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = YES;
  2467. GetClockTime(&_Standby_time);
  2468. }
  2469. }
  2470. else
  2471. {
  2472. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2473. {
  2474. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2475. }
  2476. }
  2477. }
  2478. void CheckDisconnectionStatusRecovery(void)
  2479. {
  2480. bool recovery = true;
  2481. // check all connector
  2482. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2483. {
  2484. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection)
  2485. {
  2486. recovery = false;
  2487. }
  2488. }
  2489. if(recovery)
  2490. {
  2491. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = false;
  2492. }
  2493. }
  2494. //===============================================
  2495. // 確認各小板偵測的錯誤狀況
  2496. //===============================================
  2497. void CheckErrorOccurStatus(byte index)
  2498. {
  2499. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.NormalStopRequest)
  2500. {
  2501. chargingInfo[index]->ChargingStopFlag.bits.NormalStop = true;
  2502. }
  2503. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AlarmStopRequest)
  2504. {
  2505. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2506. {
  2507. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemotenAlarmCode, 6);
  2508. }
  2509. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2510. {
  2511. LOG_INFO("Connector %d Error Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2512. }
  2513. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2514. }
  2515. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest)
  2516. {
  2517. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest = false;
  2518. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.Disconnection = true;
  2519. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = true;
  2520. if(chargingInfo[index]->SystemStatus == S_AUTHORIZING ||
  2521. chargingInfo[index]->SystemStatus == S_PREPARNING ||
  2522. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EV ||
  2523. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EVSE ||
  2524. chargingInfo[index]->SystemStatus == S_CHARGING)
  2525. {
  2526. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2527. {
  2528. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042304", 6);
  2529. }
  2530. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2531. {
  2532. LOG_INFO("Connector %d Disconnect Alarm Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2533. }
  2534. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2535. }
  2536. else if(chargingInfo[index]->SystemStatus == S_REASSIGN_CHECK ||
  2537. chargingInfo[index]->SystemStatus == S_REASSIGN)
  2538. {
  2539. setChargerMode(index, MODE_IDLE);
  2540. }
  2541. }
  2542. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo)
  2543. {
  2544. CheckDisconnectionStatusRecovery();
  2545. }
  2546. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES ||
  2547. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2548. {
  2549. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2550. {
  2551. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_PRIMARY;
  2552. StopChargingProcessByString(2);
  2553. }
  2554. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2555. {
  2556. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2557. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042251", 6);
  2558. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2559. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042252", 6);
  2560. }
  2561. }
  2562. else
  2563. {
  2564. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_PRIMARY)
  2565. {
  2566. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2567. ReleaseChargingProcessByString(2);
  2568. }
  2569. }
  2570. // RB
  2571. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2572. {
  2573. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2574. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2575. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2576. {
  2577. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2578. {
  2579. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2580. StopChargingProcessByString(2);
  2581. }
  2582. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2583. {
  2584. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2585. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042203", 6);
  2586. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2587. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042204", 6);
  2588. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2589. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042205", 6);
  2590. }
  2591. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2592. {
  2593. LOG_INFO("Connector %d RC Board Input UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2594. }
  2595. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2596. }
  2597. else
  2598. {
  2599. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2600. {
  2601. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2602. ReleaseChargingProcessByString(2);
  2603. }
  2604. }
  2605. }
  2606. else
  2607. {
  2608. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2609. {
  2610. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2611. ReleaseChargingProcessByString(2);
  2612. }
  2613. }
  2614. if(ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable == NO)
  2615. {
  2616. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2617. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2618. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2619. {
  2620. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2621. {
  2622. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2623. StopChargingProcessByString(2);
  2624. }
  2625. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2626. {
  2627. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  2628. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042200", 6);
  2629. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  2630. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042201", 6);
  2631. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2632. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042202", 6);
  2633. }
  2634. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2635. {
  2636. LOG_INFO("Connector %d RC Board Input OVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2637. }
  2638. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2639. }
  2640. else
  2641. {
  2642. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2643. {
  2644. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2645. ReleaseChargingProcessByString(2);
  2646. }
  2647. }
  2648. }
  2649. else
  2650. {
  2651. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES ||
  2652. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2653. {
  2654. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2655. {
  2656. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2657. StopChargingProcessByString(2);
  2658. }
  2659. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2660. {
  2661. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES)
  2662. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042327", 6);
  2663. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2664. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042328", 6);
  2665. }
  2666. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2667. {
  2668. LOG_INFO("Gun %d DC Input OVP/UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2669. }
  2670. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2671. }
  2672. else
  2673. {
  2674. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2675. {
  2676. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2677. ReleaseChargingProcessByString(2);
  2678. }
  2679. }
  2680. }
  2681. }
  2682. int DiffTimebWithNowSec(struct timeb ST)
  2683. {
  2684. //return milli-second
  2685. struct timeb ET;
  2686. unsigned int StartTime,StopTime;
  2687. ftime(&ET);
  2688. StartTime=(unsigned int)ST.time;
  2689. StopTime=(unsigned int)ET.time;
  2690. return (StopTime-StartTime);
  2691. }
  2692. int CheckTimeOut(char *start)
  2693. {
  2694. int result = YES;
  2695. struct ParsingResult
  2696. {
  2697. int result;
  2698. int scanedElement;
  2699. int year;
  2700. int month;
  2701. int mday;
  2702. int hour;
  2703. int min;
  2704. int sec;
  2705. int tz_hour;
  2706. int tz_min;
  2707. float minSec;
  2708. }parsingResult;
  2709. struct tm tmStart;
  2710. struct timeb tbStart;
  2711. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  2712. if(strstr((char*)start, ".") != NULL)
  2713. {
  2714. // Original data with mini second
  2715. if(strstr((char*)start, "Z") != NULL)
  2716. {
  2717. // Original data with Z
  2718. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  2719. &parsingResult.year,
  2720. &parsingResult.month,
  2721. &parsingResult.mday,
  2722. &parsingResult.hour,
  2723. &parsingResult.min,
  2724. &parsingResult.sec,
  2725. &parsingResult.minSec);
  2726. }
  2727. else
  2728. {
  2729. // Original data without Z
  2730. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  2731. &parsingResult.year,
  2732. &parsingResult.month,
  2733. &parsingResult.mday,
  2734. &parsingResult.hour,
  2735. &parsingResult.min,
  2736. &parsingResult.sec,
  2737. &parsingResult.minSec,
  2738. &parsingResult.tz_hour,
  2739. &parsingResult.tz_min);
  2740. }
  2741. }
  2742. else
  2743. {
  2744. // Original data without mini second
  2745. if(strstr((char*)start, "Z") != NULL)
  2746. {
  2747. // Original data with Z
  2748. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  2749. &parsingResult.year,
  2750. &parsingResult.month,
  2751. &parsingResult.mday,
  2752. &parsingResult.hour,
  2753. &parsingResult.min,
  2754. &parsingResult.sec);
  2755. }
  2756. else
  2757. {
  2758. // Original data without Z
  2759. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  2760. &parsingResult.year,
  2761. &parsingResult.month,
  2762. &parsingResult.mday,
  2763. &parsingResult.hour,
  2764. &parsingResult.min,
  2765. &parsingResult.sec,
  2766. &parsingResult.tz_hour,
  2767. &parsingResult.tz_min);
  2768. }
  2769. }
  2770. if(parsingResult.scanedElement >= 6)
  2771. {
  2772. tmStart.tm_year = parsingResult.year - 1900;
  2773. tmStart.tm_mon = parsingResult.month - 1;
  2774. tmStart.tm_mday = parsingResult.mday;
  2775. tmStart.tm_hour = parsingResult.hour;
  2776. tmStart.tm_min = parsingResult.min;
  2777. tmStart.tm_sec = parsingResult.sec;
  2778. tmStart.tm_gmtoff = 0;
  2779. tbStart.time = mktime(&tmStart);
  2780. tbStart.timezone = 0;
  2781. tbStart.millitm = 0;
  2782. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  2783. if(DiffTimebWithNowSec(tbStart) <= 0)
  2784. result = NO;
  2785. else
  2786. result = YES;
  2787. }
  2788. else
  2789. {
  2790. LOG_INFO("Start date parsing error");
  2791. }
  2792. return result;
  2793. }
  2794. //===============================================
  2795. // Ocpp_Service
  2796. //===============================================
  2797. void Run_Ocpp_Service(void)
  2798. {
  2799. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2800. {
  2801. system("/root/OcppBackend &");
  2802. return;
  2803. }
  2804. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2805. {
  2806. system("/root/OcppBackend20 &");
  2807. return;
  2808. }
  2809. }
  2810. bool Is_Ocpp_Task_Alive(void)
  2811. {
  2812. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2813. {
  2814. if(system("pidof -s OcppBackend > /dev/null") != 0)
  2815. {
  2816. return false;
  2817. }
  2818. return true;
  2819. }
  2820. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2821. {
  2822. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  2823. {
  2824. return false;
  2825. }
  2826. return true;
  2827. }
  2828. return false;
  2829. }
  2830. void Kill_Ocpp_Service(void)
  2831. {
  2832. system("killall OcppBackend");
  2833. system("killall OcppBackend20");
  2834. }
  2835. //===============================================
  2836. // Maintain Ocpp_Service
  2837. //===============================================
  2838. void Run_MaintainOcpp_Service(void)
  2839. {
  2840. system("/root/OcppBackendPH &");
  2841. }
  2842. bool Is_MaintainOcpp_Task_Alive(void)
  2843. {
  2844. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  2845. {
  2846. return false;
  2847. }
  2848. return false;
  2849. }
  2850. void Kill_MaintainOcpp_Service(void)
  2851. {
  2852. system("killall OcppBackendPH");
  2853. }
  2854. //===============================================
  2855. // Ocpp OcppConnStatus
  2856. //===============================================
  2857. bool Get_Ocpp_ConnectionStatus(void)
  2858. {
  2859. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2860. {
  2861. return ShmOCPP16Data->OcppConnStatus > 0 ? true : false;
  2862. }
  2863. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2864. {
  2865. return ShmOCPP20Data->OcppConnStatus > 0 ? true : false;
  2866. }
  2867. return false;
  2868. }
  2869. //===============================================
  2870. // Ocpp procDogTime
  2871. //===============================================
  2872. unsigned int Get_Ocpp_procDogTime(void)
  2873. {
  2874. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2875. {
  2876. return ShmOCPP16Data->procDogTime;
  2877. }
  2878. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2879. {
  2880. return ShmOCPP20Data->procDogTime;
  2881. }
  2882. return 0;
  2883. }
  2884. void Clean_Ocpp_procDogTime(void)
  2885. {
  2886. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2887. {
  2888. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  2889. return;
  2890. }
  2891. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2892. {
  2893. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  2894. return;
  2895. }
  2896. }
  2897. //===============================================
  2898. // Ocpp Boot Notification
  2899. //===============================================
  2900. bool Is_Ocpp_BootNotification(void)
  2901. {
  2902. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2903. {
  2904. return ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  2905. }
  2906. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2907. {
  2908. return ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  2909. }
  2910. return false;
  2911. }
  2912. void Clean_Ocpp_BootNotification(void)
  2913. {
  2914. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2915. {
  2916. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = false;
  2917. return;
  2918. }
  2919. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2920. {
  2921. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = false;
  2922. return;
  2923. }
  2924. }
  2925. //===============================================
  2926. // Ocpp Rest
  2927. //===============================================
  2928. bool Is_Ocpp_ResetReq(void)
  2929. {
  2930. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2931. {
  2932. return ShmOCPP16Data->MsMsg.bits.ResetReq == YES ? true : false;
  2933. }
  2934. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2935. {
  2936. return ShmOCPP20Data->MsMsg.bits.ResetReq == YES ? true : false;
  2937. }
  2938. return false;
  2939. }
  2940. bool Chk_Ocpp_Reset_Type(char *type)
  2941. {
  2942. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2943. {
  2944. return strcmp((char *)ShmOCPP16Data->Reset.Type, type) == EQUAL ? true : false;
  2945. }
  2946. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2947. {
  2948. return strcmp((char *)ShmOCPP20Data->Reset.type, type) == EQUAL ? true : false;
  2949. }
  2950. return false;
  2951. }
  2952. void Clean_Ocpp_ResetReq(void)
  2953. {
  2954. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2955. {
  2956. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2957. ShmOCPP16Data->MsMsg.bits.ResetReq = false;
  2958. ShmOCPP16Data->MsMsg.bits.ResetConf = true;
  2959. return;
  2960. }
  2961. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2962. {
  2963. sprintf ( (char*) ShmOCPP20Data->Reset.Response_status, "Accepted" );
  2964. ShmOCPP20Data->MsMsg.bits.ResetReq = false;
  2965. ShmOCPP20Data->MsMsg.bits.ResetConf = true;
  2966. return;
  2967. }
  2968. }
  2969. //===============================================
  2970. // Ocpp AuthorizeReq
  2971. //===============================================
  2972. void Set_Ocpp_AuthorizeReq(void)
  2973. {
  2974. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2975. {
  2976. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  2977. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  2978. return;
  2979. }
  2980. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2981. {
  2982. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = false;
  2983. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = true;
  2984. return;
  2985. }
  2986. }
  2987. bool Is_Ocpp_AuthorizeConf(void)
  2988. {
  2989. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2990. {
  2991. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  2992. }
  2993. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2994. {
  2995. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  2996. }
  2997. return false;
  2998. }
  2999. bool Is_Ocpp_Authorize_Status(char *status)
  3000. {
  3001. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3002. {
  3003. return strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  3004. }
  3005. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3006. {
  3007. return strcmp((char *)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, status) == EQUAL ? true : false;
  3008. }
  3009. return false;
  3010. }
  3011. //===============================================
  3012. // Ocpp UpdateFirmwareReq
  3013. //===============================================
  3014. bool Is_Ocpp_UpdateFirmwareReq(void)
  3015. {
  3016. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3017. {
  3018. return ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3019. }
  3020. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3021. {
  3022. return ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3023. }
  3024. return false;
  3025. }
  3026. void Clean_Ocpp_UpdateFirmwareReq(void)
  3027. {
  3028. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3029. {
  3030. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = false;
  3031. return;
  3032. }
  3033. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3034. {
  3035. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = false;
  3036. return;
  3037. }
  3038. }
  3039. bool Is_Ocpp_UpdateFirmwareStatus(char *status)
  3040. {
  3041. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3042. {
  3043. return strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status) == EQUAL ? true : false;
  3044. }
  3045. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3046. {
  3047. return strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status) == EQUAL ? true : false;
  3048. }
  3049. return false;
  3050. }
  3051. void Set_Ocpp_UpdateFirmwareStatus(char *status)
  3052. {
  3053. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3054. {
  3055. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status);
  3056. return;
  3057. }
  3058. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3059. {
  3060. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status);
  3061. return;
  3062. }
  3063. }
  3064. void Clean_Ocpp_UpdateFirmwareStatus(char *status)
  3065. {
  3066. Set_Ocpp_UpdateFirmwareStatus("");
  3067. }
  3068. void Set_Ocpp_FirmwareStatusNotificationReq(void)
  3069. {
  3070. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3071. {
  3072. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3073. return;
  3074. }
  3075. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3076. {
  3077. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3078. return;
  3079. }
  3080. }
  3081. //===============================================
  3082. // Ocpp StopTransactionOnInvalidId
  3083. //===============================================
  3084. bool Is_Ocpp_StopOnInvalidId_Enable(void)
  3085. {
  3086. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3087. {
  3088. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE") > 0 ? true : false;
  3089. }
  3090. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3091. {
  3092. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3093. }
  3094. return false;
  3095. }
  3096. //===============================================
  3097. // Ocpp AuthorizeRemoteTxRequests
  3098. //===============================================
  3099. bool Is_Ocpp_AuthorizeRemoteTxRequests(void)
  3100. {
  3101. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3102. {
  3103. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE") > 0 ? true : false;
  3104. }
  3105. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3106. {
  3107. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3108. }
  3109. return false;
  3110. }
  3111. //===============================================
  3112. // Ocpp ConnectionTimeOut
  3113. //===============================================
  3114. int Get_Ocpp_ConnectionTimeOut(void)
  3115. {
  3116. int timeout = 0;
  3117. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3118. {
  3119. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData, "") != 0)
  3120. {
  3121. timeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3122. }
  3123. }
  3124. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3125. {
  3126. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value, "" ) != 0)
  3127. {
  3128. timeout = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  3129. }
  3130. }
  3131. return timeout > 0 ? timeout : 0;
  3132. }
  3133. //===============================================
  3134. // Ocpp DefaultPrice
  3135. //===============================================
  3136. void Get_Ocpp_DefaultPrice(char *defaultPrice)
  3137. {
  3138. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3139. {
  3140. strcpy(defaultPrice, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  3141. return;
  3142. }
  3143. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3144. {
  3145. strcpy(defaultPrice, "");
  3146. return;
  3147. }
  3148. strcpy(defaultPrice, "");
  3149. }
  3150. //===============================================
  3151. // Ocpp UserPrice
  3152. //===============================================
  3153. void Get_Ocpp_UserPrice(char *userId, char *userPrice)
  3154. {
  3155. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3156. {
  3157. strcpy(userId, (char *)ShmOCPP16Data->Cost.SetUserPrice.idToken);
  3158. strcpy(userPrice, (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  3159. return;
  3160. }
  3161. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3162. {
  3163. strcpy(userId, "");
  3164. strcpy(userPrice, "");
  3165. return;
  3166. }
  3167. strcpy(userId, "");
  3168. strcpy(userPrice, "");
  3169. }
  3170. void Clean_Ocpp_UserPrice(void)
  3171. {
  3172. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3173. {
  3174. memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  3175. return;
  3176. }
  3177. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3178. {
  3179. return;
  3180. }
  3181. }
  3182. //===============================================
  3183. // Ocpp RunningCost
  3184. //===============================================
  3185. void Get_Ocpp_RunningCost(int gun_index, char *cost)
  3186. {
  3187. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3188. {
  3189. strcpy(cost, (char *)ShmOCPP16Data->Cost.RunningCost[gun_index].description);
  3190. return;
  3191. }
  3192. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3193. {
  3194. strcpy(cost, (char *)"");
  3195. return;
  3196. }
  3197. }
  3198. void Clean_Ocpp_RunningCost(int gun_index)
  3199. {
  3200. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3201. {
  3202. memset(&ShmOCPP16Data->Cost.RunningCost[gun_index], 0x00, sizeof(struct StrcutRunningCost));
  3203. return;
  3204. }
  3205. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3206. {
  3207. return;
  3208. }
  3209. }
  3210. //===============================================
  3211. // Ocpp FinalCost
  3212. //===============================================
  3213. void Get_Ocpp_FinalCost(int gun_index, char *cost)
  3214. {
  3215. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3216. {
  3217. strcpy(cost, (char *)ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  3218. return;
  3219. }
  3220. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3221. {
  3222. strcpy(cost, (char *)"");
  3223. return;
  3224. }
  3225. }
  3226. void Clean_Ocpp_FinalCost(int gun_index)
  3227. {
  3228. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3229. {
  3230. memset(&ShmOCPP16Data->Cost.FinalCost[gun_index], 0x00, sizeof(struct StrcutFinalCost));
  3231. return;
  3232. }
  3233. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3234. {
  3235. return;
  3236. }
  3237. }
  3238. //===============================================
  3239. // Ocpp TimeOffset
  3240. //===============================================
  3241. void Get_Ocpp_TimeOffset(char *timeOffset)
  3242. {
  3243. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3244. {
  3245. strcpy(timeOffset, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[TimeOffset].ItemData);
  3246. return;
  3247. }
  3248. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3249. {
  3250. strcpy(timeOffset, "");
  3251. return;
  3252. }
  3253. strcpy(timeOffset, "");
  3254. }
  3255. //===============================================
  3256. // Ocpp Refunding
  3257. //===============================================
  3258. void Get_Ocpp_Refunding(char *Refunding)
  3259. {
  3260. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3261. {
  3262. return;
  3263. }
  3264. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3265. {
  3266. return;
  3267. }
  3268. }
  3269. void Clean_Ocpp_Refunding(void)
  3270. {
  3271. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3272. {
  3273. return;
  3274. }
  3275. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3276. {
  3277. return;
  3278. }
  3279. }
  3280. //===============================================
  3281. // Ocpp LcdOveride
  3282. //===============================================
  3283. bool Is_Ocpp_LcdOverideReq(int *gun, int *page)
  3284. {
  3285. *gun = ShmSysConfigAndInfo->SysInfo.LcdOveride.connectorId;
  3286. *page = ShmSysConfigAndInfo->SysInfo.LcdOveride.page_index;
  3287. return ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq ? true : false;
  3288. }
  3289. void Clean_Ocpp_LcdOverideReq(void)
  3290. {
  3291. ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq = false;
  3292. }
  3293. //===============================================
  3294. // Ocpp StationInfo
  3295. //===============================================
  3296. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name)
  3297. {
  3298. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3299. {
  3300. strcpy(name, (char *)ShmOCPP16Data->TcciCustomData.ChargerInfo.station_name);
  3301. }
  3302. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3303. {
  3304. }
  3305. }
  3306. bool Is_Ocpp_ChargerStationReq(void)
  3307. {
  3308. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3309. {
  3310. return ShmOCPP16Data->TcciCustomData.ChargerInfoReq ? true : false;
  3311. }
  3312. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3313. {
  3314. return false;
  3315. }
  3316. return false;
  3317. }
  3318. void Clean_Ocpp_ChargerStationReq(void)
  3319. {
  3320. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3321. {
  3322. ShmOCPP16Data->TcciCustomData.ChargerInfoReq = false;
  3323. }
  3324. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3325. {
  3326. }
  3327. }
  3328. //===============================================
  3329. // Ocpp WeatherInfo
  3330. //===============================================
  3331. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature)
  3332. {
  3333. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3334. {
  3335. *weatherID = ShmOCPP16Data->TcciCustomData.WeatherInfo.weatherId;
  3336. *temperature = ShmOCPP16Data->TcciCustomData.WeatherInfo.temperature;
  3337. }
  3338. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3339. {
  3340. }
  3341. }
  3342. bool Is_Ocpp_WeatherReq(void)
  3343. {
  3344. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3345. {
  3346. return ShmOCPP16Data->TcciCustomData.WeatherInfoReq ? true : false;
  3347. }
  3348. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3349. {
  3350. return false;
  3351. }
  3352. return false;
  3353. }
  3354. void Clean_Ocpp_WeatherReq(void)
  3355. {
  3356. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3357. {
  3358. ShmOCPP16Data->TcciCustomData.WeatherInfoReq = false;
  3359. }
  3360. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3361. {
  3362. }
  3363. }
  3364. //===============================================
  3365. // Ocpp DeductInfo
  3366. //===============================================
  3367. void Set_Ocpp_DeductTransaction(int transaction)
  3368. {
  3369. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3370. {
  3371. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = transaction;
  3372. }
  3373. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3374. {
  3375. }
  3376. }
  3377. void Set_Ocpp_DeductCreditNo(char *userId)
  3378. {
  3379. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3380. {
  3381. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, userId, strlen(userId));
  3382. }
  3383. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3384. {
  3385. }
  3386. }
  3387. void Set_Ocpp_DeductVemData(char *vemData)
  3388. {
  3389. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3390. {
  3391. strcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, vemData);
  3392. }
  3393. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3394. {
  3395. }
  3396. }
  3397. void Set_Ocpp_DeductAmount(double value)
  3398. {
  3399. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3400. {
  3401. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = value;
  3402. }
  3403. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3404. {
  3405. }
  3406. }
  3407. void Set_Ocpp_DeductResult(unsigned char result)
  3408. {
  3409. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3410. {
  3411. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = result;
  3412. }
  3413. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3414. {
  3415. }
  3416. }
  3417. void Set_Ocpp_DonateInvoice(unsigned char donate)
  3418. {
  3419. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3420. {
  3421. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = donate ? YES : NO;
  3422. }
  3423. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3424. {
  3425. }
  3426. }
  3427. void Set_Ocpp_DeductInfo(DeductResultInfoData *deductInfo)
  3428. {
  3429. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3430. {
  3431. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = deductInfo->TransactionId;
  3432. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, (char *)deductInfo->creditNo, strlen((char *)deductInfo->creditNo));
  3433. strcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, "");
  3434. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = ((double)deductInfo->DeductAmount / 100);
  3435. strcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.approvalNo, deductInfo->ApprovalNumber);
  3436. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = deductInfo->DeductResult;
  3437. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = deductInfo->IsDonateInvoice ? YES : NO;
  3438. }
  3439. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3440. {
  3441. }
  3442. }
  3443. bool Is_Ocpp_ReportCreditDeductReq(void)
  3444. {
  3445. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3446. {
  3447. return ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq ? true : false;
  3448. }
  3449. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3450. {
  3451. return false;
  3452. }
  3453. return false;
  3454. }
  3455. void Set_Ocpp_ReportCreditDeductReq(void)
  3456. {
  3457. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3458. {
  3459. ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq = YES;
  3460. }
  3461. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3462. {
  3463. }
  3464. }
  3465. //===============================================
  3466. // Ocpp Tcci SerialNo
  3467. //===============================================
  3468. void Clean_Ocpp_TcciSerialNo(int gun_index)
  3469. {
  3470. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3471. {
  3472. memset((char *)&ShmOCPP16Data->TcciCustomData.SerialNo[gun_index], 0x00, sizeof(37));
  3473. }
  3474. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3475. {
  3476. }
  3477. }
  3478. //===============================================
  3479. // Ocpp Tcci ReaderStatus
  3480. //===============================================
  3481. void Set_Ocpp_TcciReaderStatus(int gun_index, int status, char *cardNo)
  3482. {
  3483. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3484. {
  3485. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = status;
  3486. strcpy((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, cardNo);
  3487. }
  3488. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3489. {
  3490. }
  3491. }
  3492. void Set_Ocpp_TcciReaderStatusReq(int gun_index)
  3493. {
  3494. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3495. {
  3496. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq = true;
  3497. }
  3498. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3499. {
  3500. }
  3501. }
  3502. //===============================================
  3503. // Ocpp Start Transaction
  3504. //===============================================
  3505. void Set_Ocpp_StartTransactionId(int gun_index, char *id)
  3506. {
  3507. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3508. {
  3509. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag, id);
  3510. return;
  3511. }
  3512. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3513. {
  3514. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  3515. return;
  3516. }
  3517. }
  3518. void Set_Ocpp_StartTransactionReq(int gun_index)
  3519. {
  3520. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3521. {
  3522. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = true;
  3523. return;
  3524. }
  3525. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3526. {
  3527. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  3528. return;
  3529. }
  3530. }
  3531. bool Is_Ocpp_StartTransactionConf(int gun_index)
  3532. {
  3533. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3534. {
  3535. return ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == YES ? true : false;
  3536. }
  3537. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3538. {
  3539. return ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf == YES ? true : false;
  3540. }
  3541. return false;
  3542. }
  3543. void Clean_Ocpp_StartTransactionConf(int gun_index)
  3544. {
  3545. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3546. {
  3547. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = false;
  3548. return;
  3549. }
  3550. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3551. {
  3552. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  3553. return;
  3554. }
  3555. }
  3556. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status)
  3557. {
  3558. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3559. {
  3560. return strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  3561. }
  3562. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3563. {
  3564. return strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, status) == EQUAL ? true : false;
  3565. }
  3566. return false;
  3567. }
  3568. bool Is_Ocpp_StartTransaction_Accepted(int gun_index)
  3569. {
  3570. return Is_Ocpp_StartTransaction_Status(gun_index, "Accepted");
  3571. }
  3572. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status)
  3573. {
  3574. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3575. {
  3576. strcpy(status, (char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status);
  3577. }
  3578. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3579. {
  3580. strcpy(status, (char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status);
  3581. }
  3582. }
  3583. void Get_Ocpp_TransactionId(int gun_index, char *transactionId)
  3584. {
  3585. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3586. {
  3587. sprintf(transactionId, "%d", ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId);
  3588. return;
  3589. }
  3590. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3591. {
  3592. strcpy(transactionId, (char *)&ShmOCPP20Data->TransactionEvent [gun_index].transactionInfo.transactionId[0]);
  3593. return;
  3594. }
  3595. sprintf(transactionId, "%d", 0);
  3596. }
  3597. bool Is_Invalid_StartTransactionId(int gun_index)
  3598. {
  3599. if(Is_Ocpp_StartTransaction_Status(gun_index, "Blocked") ||
  3600. Is_Ocpp_StartTransaction_Status(gun_index, "Expired") ||
  3601. Is_Ocpp_StartTransaction_Status(gun_index, "Invalid") ||
  3602. Is_Ocpp_StartTransaction_Status(gun_index, "NoCredit") ||
  3603. Is_Ocpp_StartTransaction_Status(gun_index, "NotAllowedTypeEVSE") ||
  3604. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisLocation") ||
  3605. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisTime") ||
  3606. Is_Ocpp_StartTransaction_Status(gun_index, "Unknown"))
  3607. {
  3608. return true;
  3609. }
  3610. return false;
  3611. }
  3612. //===============================================
  3613. // Ocpp Stop Transaction
  3614. //===============================================
  3615. void Set_Ocpp_StopTransactionId(int gun_index, char *id)
  3616. {
  3617. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3618. {
  3619. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].IdTag, id);
  3620. return;
  3621. }
  3622. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3623. {
  3624. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  3625. return;
  3626. }
  3627. }
  3628. void Set_Ocpp_StopTransactionReq(int gun_index)
  3629. {
  3630. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3631. {
  3632. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = true;
  3633. return;
  3634. }
  3635. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3636. {
  3637. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  3638. return;
  3639. }
  3640. }
  3641. //===============================================
  3642. // Ocpp Check Backup Error Code
  3643. //===============================================
  3644. void Chk_Ocpp_Backup_ErrorCode(int gun_index)
  3645. {
  3646. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3647. {
  3648. if(strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, "") == EQUAL)
  3649. {
  3650. if(strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, "") != EQUAL)
  3651. {
  3652. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, (char *)_BackupStatusNotification[gun_index].ErrorCode);
  3653. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, (char *)_BackupStatusNotification[gun_index].VendorErrorCode);
  3654. }
  3655. }
  3656. }
  3657. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3658. {
  3659. }
  3660. }
  3661. //===============================================
  3662. // Ocpp Reserve Now
  3663. //===============================================
  3664. bool Is_Ocpp_ReserveNowReq(int gun_index)
  3665. {
  3666. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3667. {
  3668. return ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  3669. }
  3670. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3671. {
  3672. return ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  3673. }
  3674. return false;
  3675. }
  3676. void Clean_Ocpp_ReserveNowReq(int gun_index)
  3677. {
  3678. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3679. {
  3680. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  3681. return;
  3682. }
  3683. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3684. {
  3685. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  3686. return;
  3687. }
  3688. }
  3689. int Get_Ocpp_ReserveNowId(int gun_index)
  3690. {
  3691. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3692. {
  3693. return ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  3694. }
  3695. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3696. {
  3697. return ShmOCPP20Data->ReserveNow[gun_index].id;
  3698. }
  3699. return 0;
  3700. }
  3701. void Set_Ocpp_ReserveNowConf(int gun_index)
  3702. {
  3703. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3704. {
  3705. ShmOCPP16Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  3706. return;
  3707. }
  3708. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3709. {
  3710. ShmOCPP20Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  3711. return;
  3712. }
  3713. }
  3714. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date)
  3715. {
  3716. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3717. {
  3718. strcpy(date, (char *)ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate);
  3719. return;
  3720. }
  3721. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3722. {
  3723. strcpy(date, (char *)ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime);
  3724. return;
  3725. }
  3726. strcpy(date, "");
  3727. return;
  3728. }
  3729. bool Is_Ocpp_ReserveNowExpired(int gun_index)
  3730. {
  3731. bool _expired = true;
  3732. char _expiryDate[128];
  3733. Get_Ocpp_ReserveNowExpiryDate(gun_index, _expiryDate);
  3734. _expired = CheckTimeOut(_expiryDate);
  3735. return _expired;
  3736. }
  3737. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag)
  3738. {
  3739. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3740. {
  3741. strcpy(idTag, (char *)ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  3742. return;
  3743. }
  3744. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3745. {
  3746. strcpy(idTag, (char *)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken);
  3747. return;
  3748. }
  3749. strcpy(idTag, "");
  3750. return;
  3751. }
  3752. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag)
  3753. {
  3754. char ReserveIdTag[32];
  3755. Get_Ocpp_ReserveNowIdTag(gun_index, ReserveIdTag);
  3756. return strcmp(ReserveIdTag, authIdTag) == EQUAL ? true : false;
  3757. }
  3758. //===============================================
  3759. // Ocpp Cancel Reservation
  3760. //===============================================
  3761. bool Is_Ocpp_CancelReservationReq(int gun_index)
  3762. {
  3763. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3764. {
  3765. return ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  3766. }
  3767. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3768. {
  3769. return ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  3770. }
  3771. return false;
  3772. }
  3773. void Clean_Ocpp_CancelReservationReq(int gun_index)
  3774. {
  3775. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3776. {
  3777. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  3778. return;
  3779. }
  3780. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3781. {
  3782. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  3783. return;
  3784. }
  3785. }
  3786. void Set_Ocpp_CancelReservationConf(int gun_index)
  3787. {
  3788. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3789. {
  3790. ShmOCPP16Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  3791. return;
  3792. }
  3793. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3794. {
  3795. ShmOCPP20Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  3796. return;
  3797. }
  3798. }
  3799. //===============================================
  3800. // Ocpp Change Availability
  3801. //===============================================
  3802. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index)
  3803. {
  3804. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3805. {
  3806. return ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  3807. }
  3808. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3809. {
  3810. return ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  3811. }
  3812. return false;
  3813. }
  3814. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index)
  3815. {
  3816. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3817. {
  3818. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  3819. return;
  3820. }
  3821. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3822. {
  3823. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  3824. return;
  3825. }
  3826. }
  3827. bool Chk_Ocpp_Availability_Type(int gun_index, char *type)
  3828. {
  3829. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3830. {
  3831. return strcmp((char *)ShmOCPP16Data->ChangeAvailability[gun_index].Type, type) == EQUAL ? true : false;
  3832. }
  3833. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3834. {
  3835. return strcmp((char *)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, type) == EQUAL ? true : false;
  3836. }
  3837. return false;
  3838. }
  3839. //===============================================
  3840. // Ocpp Unlock Connector
  3841. //===============================================
  3842. bool Is_Ocpp_UnlockConnectorReq(int gun_index)
  3843. {
  3844. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3845. {
  3846. return ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  3847. }
  3848. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3849. {
  3850. return ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  3851. }
  3852. return false;
  3853. }
  3854. void Clean_Ocpp_UnlockConnectorReq(int gun_index)
  3855. {
  3856. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3857. {
  3858. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  3859. return;
  3860. }
  3861. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3862. {
  3863. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  3864. return;
  3865. }
  3866. }
  3867. void Set_Ocpp_UnlockConnectorConf(int gun_index)
  3868. {
  3869. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3870. {
  3871. strcpy((char *)ShmOCPP16Data->UnlockConnector[gun_index].ResponseStatus, "Unlocked");
  3872. ShmOCPP16Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  3873. return;
  3874. }
  3875. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3876. {
  3877. strcpy((char *)ShmOCPP20Data->UnlockConnector[gun_index].Response_status, "Unlocked");
  3878. ShmOCPP20Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  3879. return;
  3880. }
  3881. }
  3882. //===============================================
  3883. // Ocpp Remote Start
  3884. //===============================================
  3885. bool Is_Ocpp_RemoteStartReq(int gun_index)
  3886. {
  3887. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3888. {
  3889. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES ? true : false;
  3890. }
  3891. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3892. {
  3893. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq == YES ? true : false;
  3894. }
  3895. return false;
  3896. }
  3897. void Clean_Ocpp_RemoteStartReq(int gun_index)
  3898. {
  3899. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3900. {
  3901. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = false;
  3902. return;
  3903. }
  3904. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3905. {
  3906. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = false;
  3907. return;
  3908. }
  3909. }
  3910. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag)
  3911. {
  3912. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3913. {
  3914. strcpy(idTag, (char *)&ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag[0]);
  3915. return;
  3916. }
  3917. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3918. {
  3919. strcpy(idTag, (char *)&ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken[0]);
  3920. return;
  3921. }
  3922. strcpy(idTag, "");
  3923. }
  3924. //===============================================
  3925. // Ocpp Remote Stop
  3926. //===============================================
  3927. bool Is_Ocpp_RemoteStopReq(int gun_index)
  3928. {
  3929. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3930. {
  3931. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == YES ? true : false;
  3932. }
  3933. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3934. {
  3935. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq == YES ? true : false;
  3936. }
  3937. return false;
  3938. }
  3939. void Clean_Ocpp_RemoteStopReq(int gun_index)
  3940. {
  3941. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3942. {
  3943. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = false;
  3944. return;
  3945. }
  3946. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3947. {
  3948. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = false;
  3949. return;
  3950. }
  3951. }
  3952. //===============================================
  3953. // Ocpp StopReason
  3954. //===============================================
  3955. void Set_Ocpp_StopReason(int gun_index, char *reason)
  3956. {
  3957. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3958. {
  3959. if(strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  3960. {
  3961. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, reason);
  3962. return;
  3963. }
  3964. }
  3965. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3966. {
  3967. if(strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "" ) == EQUAL)
  3968. {
  3969. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, reason);
  3970. return;
  3971. }
  3972. }
  3973. }
  3974. void Get_Ocpp_StopReason(int gun_index, char *reason)
  3975. {
  3976. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3977. {
  3978. strcpy(reason, (char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3979. return;
  3980. }
  3981. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3982. {
  3983. strcpy(reason, (char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  3984. return;
  3985. }
  3986. strcpy(reason, "");
  3987. }
  3988. void Clean_Ocpp_StopReason(int gun_index)
  3989. {
  3990. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3991. {
  3992. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  3993. return;
  3994. }
  3995. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3996. {
  3997. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "");
  3998. return;
  3999. }
  4000. }
  4001. //===============================================
  4002. // Ocpp StatusNotification ErrorCode
  4003. //===============================================
  4004. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4005. {
  4006. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4007. {
  4008. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode);
  4009. return;
  4010. }
  4011. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4012. {
  4013. return;
  4014. }
  4015. }
  4016. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4017. {
  4018. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4019. {
  4020. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4021. }
  4022. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4023. {
  4024. return strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4025. }
  4026. return false;
  4027. }
  4028. //===============================================
  4029. // Ocpp StatusNotification VendorErrorCode
  4030. //===============================================
  4031. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4032. {
  4033. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4034. {
  4035. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode);
  4036. return;
  4037. }
  4038. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4039. {
  4040. return;
  4041. }
  4042. }
  4043. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4044. {
  4045. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4046. {
  4047. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4048. }
  4049. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4050. {
  4051. return strcmp((char *)_BackupStatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4052. }
  4053. return false;
  4054. }
  4055. //===============================================
  4056. // Ocpp ChargingProfileConf
  4057. //===============================================
  4058. bool Is_Ocpp_ChargingProfileConf(int gun_index)
  4059. {
  4060. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4061. {
  4062. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4063. }
  4064. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4065. {
  4066. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4067. }
  4068. return false;
  4069. }
  4070. void Clean_Ocpp_ChargingProfileConf(int gun_index)
  4071. {
  4072. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4073. {
  4074. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4075. return;
  4076. }
  4077. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4078. {
  4079. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4080. return;
  4081. }
  4082. }
  4083. //===============================================
  4084. // Ocpp ChargingProfileReq
  4085. //===============================================
  4086. bool Is_Ocpp_ChargingProfileReq(int gun_index)
  4087. {
  4088. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4089. {
  4090. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4091. }
  4092. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4093. {
  4094. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4095. }
  4096. return false;
  4097. }
  4098. void Set_Ocpp_ChargingProfileReq(int gun_index)
  4099. {
  4100. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4101. {
  4102. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4103. return;
  4104. }
  4105. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4106. {
  4107. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4108. return;
  4109. }
  4110. }
  4111. //===============================================
  4112. // Ocpp SmartChargingProfile ChargingProfileKind
  4113. //===============================================
  4114. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind)
  4115. {
  4116. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4117. {
  4118. return strcmp((char *)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, kind) == EQUAL ? true : false;
  4119. }
  4120. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4121. {
  4122. return strcmp((char *)ShmOCPP20Data->SmartChargingProfile [gun_index].chargingProfileKind, kind) == EQUAL ? true : false;
  4123. }
  4124. return false;
  4125. }
  4126. bool Is_Ocpp_ChargingProfileId(int gun_index)
  4127. {
  4128. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4129. {
  4130. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == YES ? true : false;
  4131. }
  4132. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4133. {
  4134. return ShmOCPP20Data->SmartChargingProfile[gun_index].id == YES ? true : false;
  4135. }
  4136. return false;
  4137. }
  4138. int Get_Ocpp_ChargingProfileStartTime(int gun_index)
  4139. {
  4140. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4141. {
  4142. return GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  4143. }
  4144. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4145. {
  4146. return GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  4147. }
  4148. return 0;
  4149. }
  4150. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index)
  4151. {
  4152. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4153. {
  4154. return ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);
  4155. }
  4156. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4157. {
  4158. return ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);
  4159. }
  4160. return 0;
  4161. }
  4162. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index)
  4163. {
  4164. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4165. {
  4166. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4167. }
  4168. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4169. {
  4170. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4171. }
  4172. return 0;
  4173. }
  4174. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index)
  4175. {
  4176. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4177. {
  4178. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4179. }
  4180. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4181. {
  4182. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4183. }
  4184. return 0;
  4185. }
  4186. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index)
  4187. {
  4188. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4189. {
  4190. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4191. }
  4192. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4193. {
  4194. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].numberPhases;
  4195. }
  4196. return 0;
  4197. }
  4198. int Get_Ocpp_ChargingProfileStartIndex(int gun_index)
  4199. {
  4200. int _time = 0, _scheduleTime = 0;
  4201. int _maxCount = 0;
  4202. float _limit = 0;
  4203. int _startCount = NO_DEFINE;
  4204. _time = Get_Ocpp_ChargingProfileStartTime(gun_index);
  4205. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(gun_index);
  4206. for(int i = 0; i < _maxCount; i++)
  4207. {
  4208. _scheduleTime = Get_Ocpp_ChargingProfileScheduleTime(gun_index, i);
  4209. if(_time >= _scheduleTime)
  4210. {
  4211. _limit = Get_Ocpp_ChargingProfileScheduleLimit(gun_index, i);
  4212. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4213. {
  4214. _startCount = i;
  4215. }
  4216. }
  4217. }
  4218. return _startCount;
  4219. }
  4220. //===============================================
  4221. // Ocpp MaxChargingProfile ChargingProfileKind
  4222. //===============================================
  4223. bool Is_Ocpp_MaxChargingProfileKind(char *kind)
  4224. {
  4225. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4226. {
  4227. return strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, kind) == EQUAL ? true : false;
  4228. }
  4229. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4230. {
  4231. return strcmp((char *)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, kind) == EQUAL ? true : false;
  4232. }
  4233. return false;
  4234. }
  4235. bool Is_Ocpp_MaxChargingProfileId(void)
  4236. {
  4237. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4238. {
  4239. return ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES ? true : false;
  4240. }
  4241. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4242. {
  4243. return ShmOCPP20Data->MaxChargingProfile.id == YES ? true : false;
  4244. }
  4245. return false;
  4246. }
  4247. int Get_Ocpp_MaxProfileStartTime(void)
  4248. {
  4249. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4250. {
  4251. return GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  4252. }
  4253. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4254. {
  4255. return GetStartScheduleTime(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule);
  4256. }
  4257. return 0;
  4258. }
  4259. int Get_Ocpp_MaxProfileScheduleCount(void)
  4260. {
  4261. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4262. {
  4263. return ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  4264. }
  4265. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4266. {
  4267. return ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);
  4268. }
  4269. return 0;
  4270. }
  4271. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index)
  4272. {
  4273. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4274. {
  4275. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4276. }
  4277. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4278. {
  4279. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4280. }
  4281. return 0;
  4282. }
  4283. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index)
  4284. {
  4285. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4286. {
  4287. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4288. }
  4289. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4290. {
  4291. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4292. }
  4293. return 0;
  4294. }
  4295. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index)
  4296. {
  4297. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4298. {
  4299. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4300. }
  4301. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4302. {
  4303. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4304. }
  4305. return 0;
  4306. }
  4307. int Get_Ocpp_MaxProfileStartIndex(void)
  4308. {
  4309. int _time = 0, _scheduleTime = 0;
  4310. int _maxCount = 0;
  4311. float _limit = 0;
  4312. int _startCount = NO_DEFINE;
  4313. _time = Get_Ocpp_MaxProfileStartTime();
  4314. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  4315. for(int i = 0; i < _maxCount; i++)
  4316. {
  4317. _scheduleTime = Get_Ocpp_MaxProfileScheduleTime(i);
  4318. if(_time >= _scheduleTime)
  4319. {
  4320. _limit = Get_Ocpp_MaxProfileScheduleLimit(i);
  4321. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4322. {
  4323. _startCount = i;
  4324. }
  4325. }
  4326. }
  4327. return _startCount;
  4328. }
  4329. //===============================================
  4330. // Ocpp isRemoteStartWaitReq
  4331. //===============================================
  4332. int Is_Ocpp_RemoteStartWaitReq(void)
  4333. {
  4334. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4335. {
  4336. return ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  4337. }
  4338. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4339. {
  4340. return ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  4341. }
  4342. return 0;
  4343. }
  4344. //===============================================
  4345. // DeductResult
  4346. //===============================================
  4347. void Set_Deduct_UserId(int gunIndex, char *userId)
  4348. {
  4349. memcpy((char *)&ShmChargerInfo->DeductResultReq[gunIndex].creditNo, userId, strlen(userId));
  4350. }
  4351. void Set_Deduct_TransactionId(int gunIndex, int transaction)
  4352. {
  4353. ShmChargerInfo->DeductResultReq[gunIndex].TransactionId = transaction;
  4354. }
  4355. void Clean_DeductInfo(int gunIndex)
  4356. {
  4357. memset(&ShmChargerInfo->DeductResultReq[gunIndex], 0x00, sizeof(DeductResultInfoData));
  4358. }
  4359. bool Is_DeductResultReq(int gunIndex)
  4360. {
  4361. return ShmChargerInfo->DeductResultReq[gunIndex].DeductReq ? true : false;
  4362. }
  4363. //===============================================
  4364. // TransactionInfoData
  4365. //===============================================
  4366. void Set_UserTransactionId(int gunIndex, int transaction)
  4367. {
  4368. ShmChargerInfo->UserTransaction[gunIndex].TransactionId = transaction;
  4369. }
  4370. void Set_UserPrice(int gunIndex, float price)
  4371. {
  4372. ShmChargerInfo->UserTransaction[gunIndex].UserPrice = price;
  4373. }
  4374. void Set_UserTotalCost(int gunIndex, float cost)
  4375. {
  4376. ShmChargerInfo->UserTransaction[gunIndex].TotalCost = cost;
  4377. }
  4378. void Set_UserAccountBalance(int gunIndex, float balance)
  4379. {
  4380. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = balance;
  4381. }
  4382. void Set_UserDiscount(int gunIndex, float discount)
  4383. {
  4384. ShmChargerInfo->UserTransaction[gunIndex].CostDiscount = discount;
  4385. }
  4386. void Clean_UserTransaction(int gunIndex)
  4387. {
  4388. memset(&ShmChargerInfo->UserTransaction[gunIndex], 0x00, sizeof(TransactionInfoData));
  4389. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = -65535;
  4390. }
  4391. //===============================================
  4392. // Main process
  4393. //===============================================
  4394. // 檢查 Byte 中某個 Bit 的值
  4395. // _byte : 欲改變的 byte
  4396. // _bit : 該 byte 的第幾個 bit
  4397. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  4398. {
  4399. return ( _byte & mask_table[_bit] ) != 0x00;
  4400. }
  4401. // 設定 Byte 中某個 Bit的值
  4402. // _byte : 欲改變的 byte
  4403. // _bit : 該 byte 的第幾個 bit
  4404. // value : 修改的值為 0 or 1
  4405. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  4406. {
  4407. if(value == 1)
  4408. *_byte |= (1 << _bit);
  4409. else if (value == 0)
  4410. *_byte ^= (1 << _bit);
  4411. }
  4412. void UserScanFunction()
  4413. {
  4414. }
  4415. unsigned char isModeChange(unsigned char gun_index)
  4416. {
  4417. unsigned char result = NO;
  4418. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  4419. {
  4420. result = YES;
  4421. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  4422. }
  4423. return result;
  4424. }
  4425. void AnnounceAccountBalance(unsigned char index, float balance)
  4426. {
  4427. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  4428. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = balance;
  4429. if(!ShmChargerInfo->Control.TestCtrl.bits.DisableBalance)
  4430. {
  4431. Set_Connector_MiscCommand(index, MISC_CONN_ACCOUNT_BALANCE);
  4432. }
  4433. LOG_INFO("%sAnnounce Account Balance %4.2f To Connector %d",
  4434. ShmChargerInfo->Control.TestCtrl.bits.DisableBalance ? "Do Not " : "", balance, index + 1);
  4435. }
  4436. void FouceAnnounceAccountBalance(unsigned char index)
  4437. {
  4438. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  4439. {
  4440. AnnounceAccountBalance(index, 0);
  4441. }
  4442. }
  4443. void CleanConnectorUserIdAndPrice(unsigned char index)
  4444. {
  4445. if(chargingInfo[index]->SystemStatus == S_IDLE)
  4446. {
  4447. LOG_INFO("*********** Connector %d Clean UserId & UserPrice ***********", index + 1);
  4448. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  4449. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  4450. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -65535;
  4451. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  4452. }
  4453. }
  4454. void DispenserAuthInitial(unsigned char dispenser)
  4455. {
  4456. LOG_INFO("*********** Dispenser %d Authorizing Initial ***********", dispenser + 1);
  4457. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  4458. }
  4459. void GunAuthInitial(unsigned char connector)
  4460. {
  4461. LOG_INFO("*********** Gun %d Authorizing Initial ***********", connector + 1);
  4462. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  4463. }
  4464. unsigned char _preGunAuthResult[GENERAL_GUN_QUANTITY];
  4465. unsigned char _preDispenserAuthResult[GENERAL_GUN_QUANTITY];
  4466. struct timespec _GunAuth_Time[GENERAL_GUN_QUANTITY];
  4467. struct timespec _DispenserAuth_Time[GENERAL_GUN_QUANTITY];
  4468. void AuthorizeTimeoutProcess(void)
  4469. {
  4470. int CompletedTime = 0;
  4471. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  4472. {
  4473. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult != _AuthResult_None)
  4474. {
  4475. if(_preGunAuthResult[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult)
  4476. {
  4477. GetClockTime(&_GunAuth_Time[index]);
  4478. }
  4479. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  4480. {
  4481. FouceAnnounceAccountBalance(index);
  4482. }
  4483. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  4484. {
  4485. if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= AUTHORIZE_COMPLETED_TIME)
  4486. {
  4487. LOG_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
  4488. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  4489. CleanConnectorUserIdAndPrice(index);
  4490. }
  4491. }
  4492. }
  4493. _preGunAuthResult[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult;
  4494. }
  4495. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  4496. {
  4497. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult != _AuthResult_None)
  4498. {
  4499. if(_preDispenserAuthResult[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult)
  4500. {
  4501. GetClockTime(&_DispenserAuth_Time[index]);
  4502. }
  4503. CompletedTime = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
  4504. ShmChargerInfo->CabinetMiscValue.ConnectionTimeout : AUTHORIZE_COMPLETED_TIME;
  4505. if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= CompletedTime)
  4506. {
  4507. LOG_INFO("*********** Dispenser %d Authorizing Initial ***********", index + 1);
  4508. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  4509. }
  4510. }
  4511. _preDispenserAuthResult[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult;
  4512. }
  4513. }
  4514. BOOL IsConnectorAuthorizeSuccess(unsigned char connector)
  4515. {
  4516. BOOL success = false;
  4517. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult == _AuthResult_Valid)
  4518. {
  4519. success = true;
  4520. }
  4521. return success;
  4522. }
  4523. BOOL IsAutoGunSelectionAuthorizeSuccess(unsigned char connector)
  4524. {
  4525. BOOL success = false;
  4526. unsigned char dispenser = 0;
  4527. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  4528. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo.AuthResult == _AuthResult_Valid)
  4529. {
  4530. success = true;
  4531. }
  4532. return success;
  4533. }
  4534. unsigned short ParsingRatingPower(char *modelname)
  4535. {
  4536. char strVal[3], strExp[2];
  4537. unsigned short value = 0, exponent = 0;
  4538. memset(strVal, 0x00, sizeof(strVal));
  4539. memset(strExp, 0x00, sizeof(strExp));
  4540. memcpy(strVal, &modelname[4], 2);
  4541. memcpy(strExp, &modelname[6], 1);
  4542. value = atoi(strVal);
  4543. exponent = atoi(strExp);
  4544. if(value < 0 || exponent < 0)
  4545. {
  4546. return 0;
  4547. }
  4548. for(int i = 0; i < exponent; i++)
  4549. {
  4550. value *= 10;
  4551. }
  4552. return value;
  4553. }
  4554. bool CheckConnectorTypeStatus(void)
  4555. {
  4556. bool result = true;
  4557. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 14)
  4558. {
  4559. // DO360 for Audi, two gun only
  4560. _gunIndex = ShmChargerInfo->Control.MaxConnector;
  4561. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = _gunIndex;
  4562. ShmChargerInfo->Control.ChargerRatingPower = ParsingRatingPower((char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  4563. LOG_INFO("DC Gun Count = %d, RatingPower = %d.%d kW",
  4564. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  4565. (ShmChargerInfo->Control.ChargerRatingPower / 10),
  4566. (ShmChargerInfo->Control.ChargerRatingPower % 10));
  4567. if(ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  4568. {
  4569. result = false;
  4570. }
  4571. //DO360 charging info default setting
  4572. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  4573. {
  4574. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  4575. chargingInfo[i]->Index = (i < ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity || i == 0) ? i : 0xFF;
  4576. chargingInfo[i]->Evboard_id = 0x00;
  4577. chargingInfo[i]->ReservationId = -1;
  4578. chargingInfo[i]->slotsIndex = 0;
  4579. chargingInfo[i]->SystemStatus = S_BOOTING;
  4580. chargingInfo[i]->Type = _Type_Unknown;
  4581. chargingInfo[i]->type_index = 0;
  4582. chargingInfo[i]->IsAvailable = YES;
  4583. }
  4584. }
  4585. else
  4586. {
  4587. // Module Name 不正確 - 告警
  4588. result = false;
  4589. }
  4590. return result;
  4591. }
  4592. void KillTaskExceptPrimary()
  4593. {
  4594. system("killall Module_Authorize");
  4595. system("killall Module_CabinetParallel");
  4596. system("killall Module_ChargerSelfTest");
  4597. system("killall Module_LedIndication");
  4598. system("killall Module_EvComm");
  4599. system("killall Module_InternalComm");
  4600. system("killall Module_PsuComm");
  4601. system("killall Module_4g &");
  4602. system("killall Module_Wifi &");
  4603. }
  4604. void KillAllTask()
  4605. {
  4606. system("killall Module_Authorize");
  4607. system("killall Module_CabinetParallel");
  4608. system("killall Module_ChargerSelfTest");
  4609. system("killall Module_LedIndication");
  4610. system("killall Module_EventLogging");
  4611. system("killall Module_PrimaryComm");
  4612. system("killall Module_EvComm");
  4613. system("killall Module_LcmControl");
  4614. system("killall Module_InternalComm");
  4615. system("killall Module_PsuComm");
  4616. Kill_Ocpp_Service();
  4617. Kill_MaintainOcpp_Service();
  4618. system("killall Module_4g &");
  4619. system("killall Module_Wifi &");
  4620. }
  4621. int TransferImageViaFTP(char *IpAddress, char *SourcePath)
  4622. {
  4623. int isPass = PASS;
  4624. unsigned char ftpcmdbuf[256];
  4625. LOG_INFO("Transfer image(%s) to ip: %s ", SourcePath, IpAddress);
  4626. //Using ftpput command to transfer upgrade image,
  4627. //User name : root
  4628. //User password : y42j/4cj84
  4629. //Destination : as same as source path
  4630. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s %s %s", IpAddress, SourcePath, SourcePath);
  4631. if(system((char*)ftpcmdbuf) != 0)
  4632. {
  4633. isPass = FAIL;
  4634. LOG_INFO("Transfer Image NG - FTP put upgrade image to fail");
  4635. }
  4636. else
  4637. {
  4638. LOG_INFO("FTP put Image OK");
  4639. }
  4640. return isPass;
  4641. }
  4642. void TriggerDispenserUpgrade(unsigned char index, char *FileName)
  4643. {
  4644. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, 0x00, 128);
  4645. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, FileName, strlen(FileName));
  4646. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.FirmwareUpdateRequest = true;
  4647. LOG_INFO("Trigger dispenser %d to update image [%s]", index + 1, FileName);
  4648. }
  4649. BOOL PutImageToAllDispenser(char *SourcePath, char *FileName)
  4650. {
  4651. int isPass = FAIL;
  4652. BOOL transferred = FALSE;
  4653. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  4654. {
  4655. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  4656. {
  4657. char DispenserIpAddress[16];
  4658. memset((char*)DispenserIpAddress, 0x00, 16);
  4659. sprintf((char*)DispenserIpAddress, "%d.%d.%d.%d",
  4660. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  4661. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  4662. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  4663. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  4664. isPass = TransferImageViaFTP(DispenserIpAddress, SourcePath);
  4665. if(isPass == PASS)
  4666. {
  4667. TriggerDispenserUpgrade(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex, FileName);
  4668. }
  4669. transferred = true;
  4670. }
  4671. }
  4672. return transferred;
  4673. }
  4674. BOOL WaitAllDispenserUpgradeCompleted(void)
  4675. {
  4676. BOOL completed = TRUE;
  4677. GetClockTime(&_DispenserUpgrade_time);
  4678. do
  4679. {
  4680. completed = TRUE;
  4681. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  4682. {
  4683. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest ||
  4684. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm)
  4685. {
  4686. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted)
  4687. {
  4688. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest = false;
  4689. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm = false;
  4690. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted = false;
  4691. LOG_INFO("Dispenser %d Upgrade Success", i + 1);
  4692. }
  4693. else
  4694. {
  4695. completed = FALSE;
  4696. }
  4697. }
  4698. }
  4699. if(GetTimeoutValue(_DispenserUpgrade_time) / uSEC_VAL >= DISPENSER_UPGRADE_TIME)
  4700. {
  4701. LOG_INFO("Wait Upgrade Timeout");
  4702. completed = FALSE;
  4703. break;
  4704. }
  4705. if(completed == FALSE)
  4706. {
  4707. sleep(1);
  4708. }
  4709. }while(!completed);
  4710. return completed;
  4711. }
  4712. void UpgradePrepare(void)
  4713. {
  4714. int count = 0;
  4715. SetAcContactor(OFF);
  4716. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  4717. {
  4718. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4719. {
  4720. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  4721. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  4722. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  4723. {
  4724. count++;
  4725. }
  4726. }
  4727. }
  4728. if(count > 0)
  4729. {
  4730. sleep(3);
  4731. }
  4732. else
  4733. {
  4734. sleep(1);
  4735. }
  4736. }
  4737. void TriggerPCabinetUpgrade(unsigned char index, char *FileName)
  4738. {
  4739. memset(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, 0x00, 128);
  4740. memcpy(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, FileName, strlen(FileName));
  4741. ShmChargerInfo->ParallelCabinet.PCabinet[index].UpdateRequest = YES;
  4742. LOG_INFO("Trigger PCabinet %d to update image [%s]", index + 1, FileName);
  4743. }
  4744. BOOL PutImageToAllParallelCabinet(char *SourcePath, char *FileName)
  4745. {
  4746. int isPass = FAIL;
  4747. BOOL transferred = FALSE;
  4748. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4749. {
  4750. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  4751. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  4752. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  4753. {
  4754. int channel = ShmChargerInfo->ParallelCabinet.PCabinet[i].SocketChannel;
  4755. if(ShmChargerInfo->CabinetConnInfo[channel].Status == _SCONN_MATCHED &&
  4756. ShmChargerInfo->ParallelCabinet.PCUpdateState[i] == Not_Update)
  4757. {
  4758. char PCabinetIpAddress[16];
  4759. memset((char*)PCabinetIpAddress, 0x00, 16);
  4760. sprintf((char*)PCabinetIpAddress, "%d.%d.%d.%d",
  4761. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 0) & 0xFF),
  4762. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 8) & 0xFF),
  4763. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 16) & 0xFF),
  4764. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 24) & 0xFF));
  4765. isPass = TransferImageViaFTP(PCabinetIpAddress, SourcePath);
  4766. if(isPass == PASS)
  4767. {
  4768. TriggerPCabinetUpgrade(i, FileName);
  4769. }
  4770. transferred = true;
  4771. }
  4772. }
  4773. }
  4774. return transferred;
  4775. }
  4776. BOOL WaitAllPCabinetStartUpgrade(void)
  4777. {
  4778. BOOL isStart = true;
  4779. int count = 0;
  4780. struct timespec _waitStart;
  4781. GetClockTime(&_waitStart);
  4782. do
  4783. {
  4784. isStart = true;
  4785. count = 0;
  4786. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4787. {
  4788. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  4789. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  4790. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  4791. {
  4792. count++;
  4793. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updating)
  4794. {
  4795. isStart = false;
  4796. }
  4797. }
  4798. }
  4799. if((GetTimeoutValue(_waitStart) / uSEC_VAL) >= START_UPGRADE_TIME)
  4800. {
  4801. isStart = false;
  4802. break;
  4803. }
  4804. usleep(10000);
  4805. }while(!isStart);
  4806. if(isStart && count > 0)
  4807. {
  4808. LOG_INFO("Total %d PCabinet upgrade start...", count);
  4809. }
  4810. else
  4811. {
  4812. if(count > 0)
  4813. {
  4814. LOG_INFO("Wait PCabinet upgrade timeout");
  4815. }
  4816. else
  4817. {
  4818. LOG_INFO("No PCabinet need to upgrade");
  4819. }
  4820. }
  4821. return isStart;
  4822. }
  4823. BOOL WaitAllPCabinetUpgradeCompleted(void)
  4824. {
  4825. BOOL completed = true;
  4826. int count = 0;
  4827. struct timespec _waitCompleted;
  4828. GetClockTime(&_waitCompleted);
  4829. do
  4830. {
  4831. completed = true;
  4832. count = 0;
  4833. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  4834. {
  4835. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  4836. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  4837. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  4838. {
  4839. count++;
  4840. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updated)
  4841. {
  4842. completed = false;
  4843. }
  4844. }
  4845. }
  4846. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= PCABINET_UPGRADE_TIME)
  4847. {
  4848. completed = false;
  4849. break;
  4850. }
  4851. usleep(10000);
  4852. }while(!completed);
  4853. if(completed && count > 0)
  4854. {
  4855. LOG_INFO("Total %d PCabinet upgrade completed!", count);
  4856. }
  4857. else
  4858. {
  4859. if(count > 0)
  4860. {
  4861. LOG_INFO("Wait PCabinet upgrade completed timeout");
  4862. }
  4863. else
  4864. {
  4865. LOG_INFO("Need not to wait PCabinet completed");
  4866. }
  4867. }
  4868. return completed;
  4869. }
  4870. BOOL WaitMCabinetUpgradeCompleted(void)
  4871. {
  4872. BOOL completed = true;
  4873. struct timespec _waitCompleted;
  4874. GetClockTime(&_waitCompleted);
  4875. do
  4876. {
  4877. completed = false;
  4878. if(ShmChargerInfo->SCabinetControl.NeedUpgrade == _R_NoUpgrade)
  4879. {
  4880. completed = true;
  4881. }
  4882. if(ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Idle &&
  4883. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Alarm &&
  4884. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Charging)
  4885. {
  4886. completed = false;
  4887. break;
  4888. }
  4889. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= MCABINET_UPGRADE_TIME)
  4890. {
  4891. completed = false;
  4892. break;
  4893. }
  4894. usleep(10000);
  4895. }while(!completed);
  4896. if(completed)
  4897. {
  4898. LOG_INFO("MCabinet upgrade completed!");
  4899. }
  4900. else
  4901. {
  4902. LOG_INFO("Wait MCabinet upgrade timeout or disconnected...");
  4903. }
  4904. return completed;
  4905. }
  4906. bool IsModelNameMatch(char *model)
  4907. {
  4908. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == model[0] &&
  4909. ShmSysConfigAndInfo->SysConfig.ModelName[1] == model[1] &&
  4910. ShmSysConfigAndInfo->SysConfig.ModelName[7] == model[7] &&
  4911. ShmSysConfigAndInfo->SysConfig.ModelName[8] == model[8] &&
  4912. ShmSysConfigAndInfo->SysConfig.ModelName[9] == model[9] &&
  4913. ShmSysConfigAndInfo->SysConfig.ModelName[11] == model[11] &&
  4914. ShmSysConfigAndInfo->SysConfig.ModelName[12] == model[12] &&
  4915. ShmSysConfigAndInfo->SysConfig.ModelName[13] == model[13])
  4916. {
  4917. return true;
  4918. }
  4919. return false;
  4920. }
  4921. char CheckUpdateProcess()
  4922. {
  4923. char model[32];
  4924. bool support = false;
  4925. bool OneImageOnly = false;
  4926. int isPass = true;
  4927. uint8_t retSucc = 0;
  4928. uint8_t retFail = 0;
  4929. byte target = 0;
  4930. char *new_str = NULL;
  4931. unsigned char *ptr = NULL;
  4932. int fd = 0;
  4933. int uartFd = 0;
  4934. unsigned int Type = 0;
  4935. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  4936. DIR *d;
  4937. struct dirent *dir;
  4938. char cmdBuf[128];
  4939. d = opendir("/mnt/");
  4940. if(d)
  4941. {
  4942. while((dir = readdir(d)) != NULL && !OneImageOnly)
  4943. {
  4944. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  4945. continue;
  4946. }
  4947. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  4948. strcat(new_str, "/mnt/");
  4949. strcat(new_str, dir->d_name);
  4950. fd = open(new_str, O_RDONLY);
  4951. if(fd < 0)
  4952. {
  4953. close(fd);
  4954. free(new_str);
  4955. continue;
  4956. }
  4957. LOG_INFO("%s\n", new_str);
  4958. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  4959. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  4960. //get the image length
  4961. ImageLen = read(fd, ptr, MaxLen);
  4962. if (ImageLen > 20)
  4963. {
  4964. support = false;
  4965. memcpy(model, ptr, 16);
  4966. LOG_INFO("Model: %s", model);
  4967. Type = (((unsigned int)ptr[16]) << 24 | ((unsigned int)ptr[17]) << 16 | ((unsigned int)ptr[18]) << 8 | ((unsigned int)ptr[19]));
  4968. LOG_INFO("Typed...%x", Type);
  4969. //if(strcmp(model, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == EQUAL)
  4970. if(IsModelNameMatch(model))
  4971. {
  4972. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  4973. {
  4974. PutImageToAllParallelCabinet(new_str, dir->d_name);
  4975. WaitAllPCabinetStartUpgrade();
  4976. }
  4977. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.NeedUpgrade)
  4978. {
  4979. ShmChargerInfo->SCabinetControl.UpgradeState = Updating;
  4980. sleep(1);
  4981. }
  4982. switch (Type)
  4983. {
  4984. case 0x10000001:
  4985. case 0x10000002:
  4986. case 0x10000003:
  4987. case 0x10000004:
  4988. case 0x10000005:
  4989. support = true;
  4990. _UpgradeNeedReboot = TRUE;
  4991. isPass = Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  4992. break;
  4993. case 0x10000006: // primary mcu
  4994. case 0x1000000D: // relay board
  4995. case 0x1000000E: // fan board
  4996. case 0x10000014: // led board
  4997. support = true;
  4998. target = 0x00;
  4999. if (Type == 0x10000006)
  5000. {
  5001. target = UPGRADE_PRI;
  5002. }
  5003. else if (Type == 0x1000000D)
  5004. {
  5005. target = UPGRADE_RB;
  5006. }
  5007. else if (Type == 0x1000000E)
  5008. {
  5009. target = UPGRADE_FAN;
  5010. }
  5011. else if (Type == 0x10000014)
  5012. {
  5013. target = UPGRADE_LED;
  5014. }
  5015. uartFd = InitComPort(target);
  5016. if(target == UPGRADE_RB)
  5017. {
  5018. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC1, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5019. if(isPass == PASS)
  5020. {
  5021. retSucc++;
  5022. }
  5023. else
  5024. {
  5025. retFail++;
  5026. }
  5027. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5028. sleep(3);
  5029. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC2, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5030. }
  5031. else
  5032. {
  5033. isPass = Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5034. }
  5035. close(uartFd);
  5036. break;
  5037. }
  5038. if(support)
  5039. {
  5040. if(isPass == PASS)
  5041. {
  5042. retSucc++;
  5043. }
  5044. else
  5045. {
  5046. retFail++;
  5047. }
  5048. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5049. }
  5050. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5051. {
  5052. LOG_INFO("MCabinet upgrade completed");
  5053. WaitAllPCabinetUpgradeCompleted();
  5054. }
  5055. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.UpgradeState == Updating)
  5056. {
  5057. ShmChargerInfo->SCabinetControl.UpgradeState = Updated;
  5058. LOG_INFO("SCabinet upgrade completed");
  5059. WaitMCabinetUpgradeCompleted();
  5060. }
  5061. }
  5062. else
  5063. {
  5064. if(model[0] == 'D' && (model[1] == 'D' || model[1] == 'X'))
  5065. {
  5066. OneImageOnly = true;
  5067. _UpgradeNeedReboot = TRUE;
  5068. LOG_INFO("Start Put Image To All Dispenser");
  5069. PutImageToAllDispenser(new_str, dir->d_name);
  5070. WaitAllDispenserUpgradeCompleted();
  5071. LOG_INFO("All Dispenser Upgrade Completed");
  5072. }
  5073. }
  5074. }
  5075. sprintf(cmdBuf, "rm -f %s", new_str);
  5076. system(cmdBuf);
  5077. close(fd);
  5078. free(new_str);
  5079. free(ptr);
  5080. }
  5081. }
  5082. free(dir);
  5083. closedir(d);
  5084. if(retFail != 0)
  5085. {
  5086. return FAIL;
  5087. }
  5088. return PASS;
  5089. }
  5090. void TriggerDispenserConnectionTimeoutSetting(void)
  5091. {
  5092. BOOL trigger = FALSE;
  5093. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5094. {
  5095. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0 &&
  5096. !Is_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT))
  5097. {
  5098. trigger = TRUE;
  5099. Set_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT);
  5100. }
  5101. }
  5102. if(trigger)
  5103. {
  5104. LOG_INFO("********** Trigger Dispenser Connector Timeout Value: %d **********", ShmChargerInfo->CabinetMiscValue.ConnectionTimeout);
  5105. }
  5106. }
  5107. void CheckConnectionTimeout(void)
  5108. {
  5109. if(!Is_Ocpp_Task_Alive())
  5110. {
  5111. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5112. }
  5113. else
  5114. {
  5115. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  5116. {
  5117. _connectionTimeout = Get_Ocpp_ConnectionTimeOut();
  5118. if(_connectionTimeout <= 0)
  5119. {
  5120. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5121. }
  5122. }
  5123. else
  5124. {
  5125. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5126. }
  5127. }
  5128. if(_connectionTimeout != ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  5129. {
  5130. ShmChargerInfo->CabinetMiscValue.ConnectionTimeout = _connectionTimeout;
  5131. TriggerDispenserConnectionTimeoutSetting();
  5132. }
  5133. }
  5134. void CreateTimeoutFork()
  5135. {
  5136. pid_t timeoutPid;
  5137. timeoutPid = fork();
  5138. if (timeoutPid == 0)
  5139. {
  5140. GetClockTime(&_cmdSubPriority_time);
  5141. CheckConnectionTimeout();
  5142. LOG_INFO("Timeout Fork PID = %d", getpid());
  5143. while(true)
  5144. {
  5145. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  5146. {
  5147. CheckConnectionTimeout();
  5148. GetClockTime(&_cmdSubPriority_time);
  5149. }
  5150. sleep(1);
  5151. }
  5152. }
  5153. }
  5154. void CheckFactoryConfigFunction(void)
  5155. {
  5156. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  5157. {
  5158. char Buf[256];
  5159. sprintf(Buf, "cd /root;./FactoryConfig -m");
  5160. system(Buf);
  5161. system("rm -f /Storage/OCPP/OCPPConfiguration");
  5162. system("sync");
  5163. sleep(5);
  5164. system("reboot -f");
  5165. sleep(5);
  5166. system("reboot -f");
  5167. }
  5168. }
  5169. void CheckFwUpdateFunction(void)
  5170. {
  5171. char UpdateResult = PASS;
  5172. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  5173. {
  5174. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  5175. UpgradePrepare();
  5176. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  5177. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  5178. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  5179. LOG_INFO("ftp : update start.");
  5180. TryCloseWatchdog();
  5181. //KillTask();
  5182. system("killall Module_PrimaryComm");
  5183. system("killall Module_InternalComm");
  5184. UpdateResult = CheckUpdateProcess();
  5185. if (UpdateResult == PASS)
  5186. {
  5187. LOG_INFO("ftp : update complete.");
  5188. }
  5189. else
  5190. {
  5191. LOG_INFO("ftp : update fail.");
  5192. }
  5193. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  5194. sleep(10);
  5195. if(_UpgradeNeedReboot)
  5196. {
  5197. system("reboot -f");
  5198. }
  5199. else
  5200. {
  5201. KillAllTask();
  5202. system("/usr/bin/run_evse_restart.sh");
  5203. }
  5204. }
  5205. else if(Is_Ocpp_UpdateFirmwareReq())
  5206. {
  5207. Clean_Ocpp_UpdateFirmwareReq();
  5208. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  5209. UpgradePrepare();
  5210. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  5211. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  5212. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  5213. if(Is_Ocpp_UpdateFirmwareStatus("Downloaded"))
  5214. {
  5215. LOG_INFO("Backend : update start.");
  5216. TryCloseWatchdog();
  5217. Set_Ocpp_UpdateFirmwareStatus("Installing");
  5218. Set_Ocpp_FirmwareStatusNotificationReq();
  5219. //KillTask();
  5220. system("killall Module_PrimaryComm");
  5221. system("killall Module_InternalComm");
  5222. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5223. {
  5224. setChargerMode(_index, MODE_UPDATE);
  5225. }
  5226. UpdateResult = CheckUpdateProcess();
  5227. if (UpdateResult == PASS)
  5228. {
  5229. LOG_INFO("Backend : update complete.");
  5230. Set_Ocpp_UpdateFirmwareStatus("Installed");
  5231. }
  5232. else
  5233. {
  5234. LOG_INFO("Backend : update fail.");
  5235. Set_Ocpp_UpdateFirmwareStatus("InstallationFailed");
  5236. }
  5237. Set_Ocpp_FirmwareStatusNotificationReq();
  5238. sleep(10);
  5239. if(_UpgradeNeedReboot)
  5240. {
  5241. system("reboot -f");
  5242. }
  5243. else
  5244. {
  5245. KillAllTask();
  5246. system("/usr/bin/run_evse_restart.sh");
  5247. }
  5248. }
  5249. }
  5250. }
  5251. void ExecuteWriteWiringInfo(void)
  5252. {
  5253. struct SysConfigData config;
  5254. memcpy(&config, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  5255. config.SwitchDebugFlag = NO;
  5256. strcpy((char *) config.UserId, "");
  5257. int result = StoreUsrConfigData(&config);
  5258. if(result != 1)
  5259. {
  5260. LOG_INFO("Store User Config NG");
  5261. }
  5262. else
  5263. {
  5264. LOG_INFO("Store User Config OK");
  5265. }
  5266. }
  5267. struct timespec _WiringInfo_time;
  5268. int CheckWiringInfoUpdate(void)
  5269. {
  5270. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.FlashConfigChanged)
  5271. {
  5272. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.FlashConfigChanged = false;
  5273. LOG_INFO("Flash Config Changed");
  5274. GetClockTime(&_WiringInfo_time);
  5275. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.EnableWriteFlash = true;
  5276. }
  5277. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.EnableWriteFlash)
  5278. {
  5279. // 30s for reboot request timeout
  5280. if((GetTimeoutValue(_WiringInfo_time) / uSEC_VAL) >= WIRING_INFO_DELAY ||
  5281. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.CleanWiringInfo)
  5282. {
  5283. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.EnableWriteFlash = false;
  5284. LOG_INFO("Start Write To Flash");
  5285. pid_t writePid = fork();
  5286. if(writePid == 0)
  5287. {
  5288. ExecuteWriteWiringInfo();
  5289. if(ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.CleanWiringInfo)
  5290. {
  5291. ShmSysConfigAndInfo->SysInfo.CabinetSetting.bits.CleanWiringInfo = false;
  5292. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  5293. LOG_INFO("Clean Wiring Info And Need Soft Reset");
  5294. }
  5295. return NO;
  5296. }
  5297. LOG_INFO("Clean Write Flahs Flag");
  5298. }
  5299. }
  5300. return YES;
  5301. }
  5302. void CheckSoftHardReset(void)
  5303. {
  5304. if(ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset)
  5305. {
  5306. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = false;
  5307. LOG_INFO("********** Need Soft Reset **********");
  5308. sleep(5);
  5309. Kill_Ocpp_Service();
  5310. KillAllTask();
  5311. TryCloseWatchdog();
  5312. system("/usr/bin/run_evse_restart.sh");
  5313. }
  5314. if(ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset)
  5315. {
  5316. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = false;
  5317. LOG_INFO("********** Need Hard Reset **********");
  5318. sleep(5);
  5319. system("reboot -f");
  5320. }
  5321. }
  5322. struct timespec _Reboot_time;
  5323. void TriggerDispenserHardwareReboot(void)
  5324. {
  5325. BOOL trigger = FALSE;
  5326. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5327. {
  5328. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  5329. {
  5330. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm == 0 &&
  5331. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  5332. {
  5333. trigger = TRUE;
  5334. Set_Dispenser_MiscCommand(i, MISC_DISP_HARDWARE_REBOOT);
  5335. }
  5336. }
  5337. }
  5338. if(trigger)
  5339. {
  5340. GetClockTime(&_Reboot_time);
  5341. LOG_INFO("********** Trigger Dispenser Hardware Reboot **********");
  5342. }
  5343. }
  5344. BOOL IsDispenserHardwareRebootResponsed(void)
  5345. {
  5346. BOOL response_ok = TRUE;
  5347. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5348. {
  5349. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  5350. {
  5351. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  5352. {
  5353. response_ok = FALSE;
  5354. // 10s for reboot request timeout
  5355. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  5356. {
  5357. response_ok = TRUE;
  5358. LOG_INFO("Reboot request timeout");
  5359. }
  5360. }
  5361. }
  5362. }
  5363. return response_ok;
  5364. }
  5365. struct timespec _Reset_time;
  5366. void TriggerDispenserSoftwareReset(void)
  5367. {
  5368. BOOL trigger = FALSE;
  5369. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5370. {
  5371. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  5372. {
  5373. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  5374. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  5375. {
  5376. trigger = TRUE;
  5377. Set_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET);
  5378. }
  5379. }
  5380. }
  5381. if(trigger)
  5382. {
  5383. GetClockTime(&_Reset_time);
  5384. LOG_INFO("********** Trigger Dispenser Software Reset **********");
  5385. }
  5386. }
  5387. BOOL IsDispenserSoftwareResetResponsed(void)
  5388. {
  5389. BOOL response_ok = TRUE;
  5390. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5391. {
  5392. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  5393. {
  5394. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  5395. {
  5396. response_ok = FALSE;
  5397. // 10s for reset request timeout
  5398. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  5399. {
  5400. response_ok = TRUE;
  5401. LOG_INFO("Reset request timeout");
  5402. }
  5403. }
  5404. }
  5405. }
  5406. return response_ok;
  5407. }
  5408. float TariffParsing(char *StringItem, char *TariffCode)
  5409. {
  5410. char *ptrSave, *ptrToken;
  5411. char strSource[128];
  5412. float fee = 0;
  5413. memcpy(strSource, StringItem, 128);
  5414. ptrToken = strtok_r(strSource, ";", &ptrSave);
  5415. while(ptrToken != NULL)
  5416. {
  5417. char *strTariff = strstr(ptrToken, TariffCode);
  5418. if(strTariff != NULL)
  5419. {
  5420. char *strFee = strstr(strTariff, "$");
  5421. if(strFee != NULL)
  5422. {
  5423. strFee++;
  5424. fee = atof(strFee);
  5425. break;
  5426. }
  5427. }
  5428. ptrToken = strtok_r(NULL, ";", &ptrSave);
  5429. }
  5430. return fee;
  5431. }
  5432. float PhTariffParsing(char *StringItem, char *TariffCode)
  5433. {
  5434. char *ptrSave, *ptrToken;
  5435. char strSource[128];
  5436. float fee = 0;
  5437. memcpy(strSource, StringItem, 128);
  5438. ptrToken = strtok_r(strSource, ",", &ptrSave);
  5439. while(ptrToken != NULL)
  5440. {
  5441. char *strTariff = strstr(ptrToken, TariffCode);
  5442. if(strTariff != NULL)
  5443. {
  5444. char *strFee = strstr(strTariff, ":");
  5445. if(strFee != NULL)
  5446. {
  5447. strFee++;
  5448. fee = atof(strFee);
  5449. break;
  5450. }
  5451. }
  5452. ptrToken = strtok_r(NULL, ",", &ptrSave);
  5453. }
  5454. return fee;
  5455. }
  5456. // the unit of return value: 0.01 dollar
  5457. float DefaultPriceParsing(char *StringItem)
  5458. {
  5459. char *ptrSave, *ptrToken;
  5460. char strSource[128];
  5461. float fee = 0;
  5462. memcpy(strSource, StringItem, 128);
  5463. ptrToken = strtok_r(strSource, ",", &ptrSave);
  5464. while(ptrToken != NULL)
  5465. {
  5466. char *strFee = strstr(ptrToken, "$");
  5467. if(strFee != NULL)
  5468. {
  5469. strFee++;
  5470. fee = atof(strFee);
  5471. break;
  5472. }
  5473. else
  5474. {
  5475. fee = atof(ptrToken);
  5476. break;
  5477. }
  5478. ptrToken = strtok_r(NULL, ",", &ptrSave);
  5479. }
  5480. return fee;
  5481. }
  5482. // return time offset
  5483. int TimeOffsetKeyParsing(char *StringItem)
  5484. {
  5485. int offset = 0;
  5486. int hour = 0, minute = 0, sign = 1;
  5487. char *ptrSave, *ptrToken;
  5488. char strSource[501];
  5489. memset(strSource, 0x00, sizeof(strSource));
  5490. strcpy(strSource, StringItem);
  5491. if(strSource[0] != '+' && strSource[0] != '-')
  5492. {
  5493. return offset;
  5494. }
  5495. sign = strSource[0] == '+' ? (1) : (-1);
  5496. ptrToken = strtok_r(&strSource[1], ":", &ptrSave);
  5497. if(ptrToken != NULL)
  5498. {
  5499. hour = atoi(ptrToken);
  5500. ptrToken = strtok_r(NULL, ":", &ptrSave);
  5501. if(ptrToken != NULL)
  5502. {
  5503. minute = atoi(ptrToken);
  5504. offset = ((hour * 60) + minute) * sign;
  5505. }
  5506. }
  5507. return offset;
  5508. }
  5509. char MyDefaultPriceString[501];
  5510. void DefaultPriceHandler(void)
  5511. {
  5512. char price[501];
  5513. Get_Ocpp_DefaultPrice((char *)price);
  5514. if(strcmp(MyDefaultPriceString, (char *)price) != EQUAL)
  5515. {
  5516. BOOL trigger = FALSE;
  5517. float ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  5518. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  5519. {
  5520. ConnectionFee = TariffParsing(price, "Connection Fee");
  5521. CurrentRate = TariffParsing(price, "Current Rate");
  5522. OccupancyFee = TariffParsing(price, "Occupancy Fee");
  5523. }
  5524. else
  5525. {
  5526. CurrentRate = DefaultPriceParsing(price);
  5527. }
  5528. LOG_INFO("********** Default Price: %s", price);
  5529. LOG_INFO("********** Connection Fee: %4.2f **********", ConnectionFee);
  5530. LOG_INFO("********** Current Rate: %4.2f **********", CurrentRate);
  5531. LOG_INFO("********** Occupancy Fee: %4.2f **********", OccupancyFee);
  5532. ShmChargerInfo->CabinetMiscValue.DefaultPrice = CurrentRate;
  5533. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5534. {
  5535. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  5536. {
  5537. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE))
  5538. {
  5539. trigger = TRUE;
  5540. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE);
  5541. }
  5542. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY))
  5543. {
  5544. trigger = TRUE;
  5545. Set_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY);
  5546. }
  5547. }
  5548. }
  5549. if(trigger)
  5550. {
  5551. LOG_INFO("********** Trigger Dispenser Default Price **********");
  5552. }
  5553. strcpy(MyDefaultPriceString, price);
  5554. }
  5555. }
  5556. void UserPriceHandler(void)
  5557. {
  5558. char UserId[32];
  5559. char preice[500];
  5560. Get_Ocpp_UserPrice(UserId, preice);
  5561. if(strlen((char *)UserId) > 0 && strlen((char *)preice) > 0)
  5562. {
  5563. float ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  5564. float AccountBalance = 0;
  5565. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  5566. {
  5567. ConnectionFee = TariffParsing((char *)preice, "Connection Fee");
  5568. CurrentRate = TariffParsing((char *)preice, "Current Rate");
  5569. OccupancyFee = TariffParsing((char *)preice, "Occupancy Fee");
  5570. AccountBalance = TariffParsing((char *)preice, "Account Balance");
  5571. }
  5572. else
  5573. {
  5574. CurrentRate = PhTariffParsing((char *)preice, "chargineFee");
  5575. OccupancyFee = PhTariffParsing((char *)preice, "parkingFee");
  5576. }
  5577. LOG_INFO("********** User Id: %s **********", UserId);
  5578. LOG_INFO("********** User Price: %s", preice);
  5579. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  5580. LOG_INFO("********** Current Rate: %7.2f **********", CurrentRate);
  5581. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  5582. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  5583. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5584. {
  5585. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  5586. {
  5587. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  5588. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  5589. {
  5590. LOG_INFO("********** Connector Id %d User Price %4.2f, Account Balance %7.2f **********",
  5591. (i + 1), CurrentRate, AccountBalance);
  5592. }
  5593. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  5594. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  5595. if(chargingInfo[i]->SystemStatus == S_AUTHORIZING)
  5596. {
  5597. AnnounceAccountBalance(i, AccountBalance);
  5598. }
  5599. }
  5600. }
  5601. Clean_Ocpp_UserPrice();
  5602. }
  5603. }
  5604. void RunningFinalCostHandler(void)
  5605. {
  5606. char strCost[256];
  5607. float ConnectionFee = 0, SessionFee = 0, OccupancyFee = 0, TotalCost = 0, CostDiscount = 0;
  5608. float AccountBalance = 0;
  5609. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5610. {
  5611. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  5612. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_ALARM)
  5613. {
  5614. Get_Ocpp_RunningCost(i, strCost);
  5615. if(strlen((char *)strCost) > 0)
  5616. {
  5617. ConnectionFee = 0;
  5618. SessionFee = 0;
  5619. OccupancyFee = 0;
  5620. TotalCost = 0;
  5621. AccountBalance = 0;
  5622. CostDiscount = 0;
  5623. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  5624. {
  5625. ConnectionFee = TariffParsing((char *)strCost, "Connection Fee");
  5626. SessionFee = TariffParsing((char *)strCost, "Session Fee");
  5627. OccupancyFee = TariffParsing((char *)strCost, "Occupancy Fee");
  5628. TotalCost = TariffParsing((char *)strCost, "Total Cost");
  5629. AccountBalance = TariffParsing((char *)strCost, "Account Balance");
  5630. }
  5631. else
  5632. {
  5633. TotalCost = PhTariffParsing((char *)strCost, "chargingFee");
  5634. OccupancyFee = PhTariffParsing((char *)strCost, "parkingFee");
  5635. }
  5636. #if 0
  5637. LOG_INFO("********** Connector Id: %d Running Cost **********", (i + 1));
  5638. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  5639. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  5640. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  5641. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  5642. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  5643. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost != TotalCost ||
  5644. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  5645. {
  5646. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance %7.2f **********",
  5647. (i + 1), TotalCost, AccountBalance);
  5648. }
  5649. #endif
  5650. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost = TotalCost;
  5651. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  5652. Clean_Ocpp_RunningCost(i);
  5653. }
  5654. Get_Ocpp_FinalCost(i, strCost);
  5655. if(strlen((char *)strCost) > 0)
  5656. {
  5657. ConnectionFee = 0;
  5658. SessionFee = 0;
  5659. OccupancyFee = 0;
  5660. TotalCost = 0;
  5661. AccountBalance = 0;
  5662. CostDiscount = 0;
  5663. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  5664. {
  5665. ConnectionFee = TariffParsing((char *)strCost, "Connection Fee");
  5666. SessionFee = TariffParsing((char *)strCost, "Session Fee");
  5667. OccupancyFee = TariffParsing((char *)strCost, "Occupancy Fee");
  5668. TotalCost = TariffParsing((char *)strCost, "Total Cost");
  5669. AccountBalance = TariffParsing((char *)strCost, "Account Balance");
  5670. }
  5671. else
  5672. {
  5673. TotalCost = PhTariffParsing((char *)strCost, "chargingFee");
  5674. OccupancyFee = PhTariffParsing((char *)strCost, "parkingFee");
  5675. AccountBalance = PhTariffParsing((char *)strCost, "accountBalance");
  5676. CostDiscount = PhTariffParsing((char *)strCost, "couponPoint");
  5677. }
  5678. LOG_INFO("********** Connector Id: %d Final Cost **********", (i + 1));
  5679. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  5680. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  5681. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  5682. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  5683. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  5684. #if 0
  5685. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost != TotalCost ||
  5686. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance ||
  5687. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CostDiscount != CostDiscount)
  5688. {
  5689. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance: %7.2f, Discount: %d.%02d **********",
  5690. (i + 1), TotalCost, AccountBalance, (CostDiscount / 100), (CostDiscount % 100));
  5691. }
  5692. #endif
  5693. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].TotalCost = TotalCost;
  5694. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  5695. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CostDiscount = CostDiscount;
  5696. Set_Connector_MiscCommand(i, MISC_CONN_FINAL_COST);
  5697. Clean_Ocpp_FinalCost(i);
  5698. }
  5699. }
  5700. }
  5701. }
  5702. char MyTimeOffsetString[501];
  5703. void TimeOffsetHandler(void)
  5704. {
  5705. char timeOffset[501];
  5706. Get_Ocpp_TimeOffset((char *)timeOffset);
  5707. if(strcmp(MyTimeOffsetString, (char *)timeOffset) != EQUAL)
  5708. {
  5709. BOOL trigger = FALSE;
  5710. int offset = 0;
  5711. offset = TimeOffsetKeyParsing(timeOffset);
  5712. LOG_INFO("********** Time Offset String [%s] -> Value [%d] -> Time [%s%02d:%02d]",
  5713. timeOffset, offset, offset >= 0 ? "+" : "-", (abs(offset) / 60), (abs(offset) % 60));
  5714. ShmChargerInfo->CabinetMiscValue.TimeOffset = offset;
  5715. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5716. {
  5717. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  5718. {
  5719. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET))
  5720. {
  5721. trigger = TRUE;
  5722. Set_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET);
  5723. }
  5724. }
  5725. }
  5726. if(trigger)
  5727. {
  5728. LOG_INFO("********** Trigger Dispenser Time Offset **********");
  5729. }
  5730. strcpy(MyTimeOffsetString, timeOffset);
  5731. }
  5732. }
  5733. struct timespec _Cost_Price_time;
  5734. void CheckOcppCostAndPrice(void)
  5735. {
  5736. // 5s for reboot request timeout
  5737. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  5738. {
  5739. GetClockTime(&_Cost_Price_time);
  5740. DefaultPriceHandler();
  5741. UserPriceHandler();
  5742. RunningFinalCostHandler();
  5743. TimeOffsetHandler();
  5744. }
  5745. }
  5746. void CheckRemoteStartNoIdRequest(void)
  5747. {
  5748. if(Is_Ocpp_RemoteStartWaitReq())
  5749. {
  5750. if(!ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  5751. {
  5752. LOG_INFO("Ocpp remote start without connector id is trigger");
  5753. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = true;
  5754. AnnounceRemoteStartNoID();
  5755. GetClockTime(&_RemoteStartNoId_Time);
  5756. }
  5757. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  5758. {
  5759. // to avoid keep RemoteStartNoId state when ocpp is disconnected
  5760. if((GetTimeoutValue(_RemoteStartNoId_Time) / uSEC_VAL) >= (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + CONNECTION_TIME_OFFSET))
  5761. {
  5762. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  5763. }
  5764. }
  5765. }
  5766. else
  5767. {
  5768. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  5769. {
  5770. LOG_INFO("Ocpp remote start without connector id is expired");
  5771. }
  5772. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  5773. }
  5774. }
  5775. void CheckOcppPageChangeRequest(void)
  5776. {
  5777. int gunId = 0, pageIndex = 0;
  5778. if(Is_Ocpp_LcdOverideReq(&gunId, &pageIndex))
  5779. {
  5780. Clean_Ocpp_LcdOverideReq();
  5781. switch(pageIndex)
  5782. {
  5783. case _OCPP_Page_LineStatus_1:
  5784. case _OCPP_Page_LineStatus_2:
  5785. case _OCPP_Page_LineStatus_3:
  5786. case _OCPP_Page_LineStatus_4:
  5787. LOG_INFO("Gun %d Ocpp Line Status [%d] is trigger", gunId, pageIndex);
  5788. AnnounceLineStatusCode(gunId - 1, pageIndex);
  5789. break;
  5790. default:
  5791. LOG_INFO("Gun %d Ocpp page change [Unknown][%d]", gunId, pageIndex);
  5792. break;
  5793. }
  5794. }
  5795. }
  5796. void CheckLcmPageChangeRequest(void)
  5797. {
  5798. bool isTrigger = false;
  5799. CheckRemoteStartNoIdRequest();
  5800. CheckOcppPageChangeRequest();
  5801. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  5802. {
  5803. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  5804. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  5805. {
  5806. isTrigger = false;
  5807. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  5808. {
  5809. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  5810. if(ShmChargerInfo->ConnectorPageReq[gun].CtrlValue != 0)
  5811. {
  5812. isTrigger = true;
  5813. break;
  5814. }
  5815. }
  5816. if(isTrigger)
  5817. {
  5818. Set_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  5819. }
  5820. else
  5821. {
  5822. Clean_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  5823. }
  5824. }
  5825. }
  5826. }
  5827. void CheckStationInfo(void)
  5828. {
  5829. int station_id = 0;
  5830. char station_name[64];
  5831. if(Is_Ocpp_ChargerStationReq())
  5832. {
  5833. memset(station_name, 0x00, sizeof(station_name));
  5834. Get_Ocpp_ChargerStationInfo(&station_id, station_name);
  5835. if(strcmp(station_name, ShmChargerInfo->StationInfo.StationName) != EQUAL ||
  5836. ShmChargerInfo->StationInfo.StationID != station_id)
  5837. {
  5838. strcpy(ShmChargerInfo->StationInfo.StationName, station_name);
  5839. ShmChargerInfo->StationInfo.StationID = station_id;
  5840. LOG_INFO("Station ID: %d, Name: %s", ShmChargerInfo->StationInfo.StationID, ShmChargerInfo->StationInfo.StationName);
  5841. AnnounceStationInfo();
  5842. }
  5843. Clean_Ocpp_ChargerStationReq();
  5844. }
  5845. }
  5846. void CheckWeatherInfo(void)
  5847. {
  5848. int weatherID = 0;
  5849. float temperature = 0;
  5850. char strWeather[16];
  5851. char *str_weather_type[] = {STR_WEATHER_NONE, STR_WEATHER_SUNNY, STR_WEATHER_CLOUDY, STR_WEATHER_RAIN, STR_WEATHER_THUNDERSTORM, STR_WEATHER_SNOW, STR_WEATHER_MIST};
  5852. if(Is_Ocpp_WeatherReq())
  5853. {
  5854. Get_Ocpp_WeatherInfo(&weatherID, &temperature);
  5855. if(weatherID != ShmChargerInfo->StationInfo.WeatherID ||
  5856. temperature != ShmChargerInfo->StationInfo.Temperature)
  5857. {
  5858. ShmChargerInfo->StationInfo.WeatherID = weatherID;
  5859. ShmChargerInfo->StationInfo.Temperature = temperature;
  5860. if(ShmChargerInfo->StationInfo.WeatherID <= _WEATHER_Mist)
  5861. {
  5862. strcpy(strWeather, str_weather_type[ShmChargerInfo->StationInfo.WeatherID]);
  5863. }
  5864. else
  5865. {
  5866. sprintf(strWeather, "%d", ShmChargerInfo->StationInfo.WeatherID);
  5867. }
  5868. LOG_INFO("Weather: [%s][%d], Temperature: %4.1f", strWeather, ShmChargerInfo->StationInfo.WeatherID, ShmChargerInfo->StationInfo.Temperature);
  5869. AnnounceStationInfo();
  5870. }
  5871. Clean_Ocpp_WeatherReq();
  5872. }
  5873. }
  5874. void CheckDeductResultRequest(void)
  5875. {
  5876. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5877. {
  5878. if(Is_DeductResultReq(i))
  5879. {
  5880. LOG_INFO("Gun %d Need to Report Deduct Info", i + 1);
  5881. memcpy((char *)&_BackupDeductResult[i], (char *)&ShmChargerInfo->DeductResultReq[i], sizeof(DeductResultInfoData));
  5882. Clean_DeductInfo(i);
  5883. }
  5884. if(_BackupDeductResult[i].DeductReq && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  5885. {
  5886. if(!Is_Ocpp_ReportCreditDeductReq())
  5887. {
  5888. //Set_Ocpp_DeductTransaction(_BackupDeductResult[i].TransactionId);
  5889. //Set_Ocpp_DeductCreditNo((char *)&_BackupDeductResult[i].creditNo);
  5890. //Set_Ocpp_DeductVemData("");
  5891. //Set_Ocpp_DeductAmount(((double)_BackupDeductResult[i].DeductAmount / 100));
  5892. //Set_Ocpp_DeductResult(_BackupDeductResult[i].DeductResult);
  5893. //Set_Ocpp_DonateInvoice(_BackupDeductResult[i].IsDonateInvoice);
  5894. Set_Ocpp_DeductInfo(&_BackupDeductResult[i]);
  5895. Set_Ocpp_ReportCreditDeductReq();
  5896. LOG_INFO("Gun %d Report Deduct Info: Transaction: %d, UserId: %s, Amount: %d.%02d, Result: %s, Donate: %s",
  5897. i + 1,
  5898. _BackupDeductResult[i].TransactionId,
  5899. (char *)&_BackupDeductResult[i].creditNo,
  5900. (_BackupDeductResult[i].DeductAmount / 100),
  5901. (_BackupDeductResult[i].DeductAmount % 100),
  5902. _BackupDeductResult[i].DeductResult ? "Pass" : "Fail",
  5903. _BackupDeductResult[i].IsDonateInvoice ? "Yes" : "No");
  5904. memset((char *)&_BackupDeductResult[i], 0, sizeof(DeductResultInfoData));
  5905. }
  5906. }
  5907. }
  5908. }
  5909. void CheckReDeductRequest(void)
  5910. {
  5911. if(ShmChargerInfo->ReDeductReq.DeductReq)
  5912. {
  5913. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  5914. {
  5915. Set_Ocpp_DeductInfo(&ShmChargerInfo->ReDeductReq);
  5916. Set_Ocpp_ReportCreditDeductReq();
  5917. LOG_INFO("Dispenser Report ReDeduct Info: Transaction: %d, UserId: %s, Amount: %d.%02d, Result: %s, Donate: %s",
  5918. ShmChargerInfo->ReDeductReq.TransactionId,
  5919. (char *)&ShmChargerInfo->ReDeductReq.creditNo,
  5920. (ShmChargerInfo->ReDeductReq.DeductAmount / 100),
  5921. (ShmChargerInfo->ReDeductReq.DeductAmount % 100),
  5922. ShmChargerInfo->ReDeductReq.DeductResult ? "Pass" : "Fail",
  5923. ShmChargerInfo->ReDeductReq.IsDonateInvoice ? "Yes" : "No");
  5924. memset(&ShmChargerInfo->ReDeductReq, 0x00, sizeof(DeductResultInfoData));
  5925. }
  5926. }
  5927. }
  5928. //===============================================
  5929. // OCPP
  5930. //===============================================
  5931. void CheckOcppStatus()
  5932. {
  5933. if (Is_Ocpp_BootNotification())
  5934. {
  5935. Clean_Ocpp_BootNotification();
  5936. LOG_INFO("*********************OCPP Boot Notification****************************");
  5937. }
  5938. if (Is_Ocpp_ResetReq())
  5939. {
  5940. bool canReset = true;
  5941. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  5942. {
  5943. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  5944. {
  5945. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  5946. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  5947. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  5948. {
  5949. canReset = false;
  5950. //if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  5951. //{
  5952. // ChargingTerminalProcess(_index);
  5953. //}
  5954. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  5955. {
  5956. chargingInfo[_index]->ChargingStopFlag.bits.HardResetStop = true;
  5957. }
  5958. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  5959. {
  5960. chargingInfo[_index]->ChargingStopFlag.bits.SoftResetStop = true;
  5961. }
  5962. }
  5963. }
  5964. }
  5965. if(canReset)
  5966. {
  5967. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  5968. {
  5969. //trigger dispenser to hardware reboot
  5970. TriggerDispenserHardwareReboot();
  5971. if(IsDispenserHardwareRebootResponsed())
  5972. {
  5973. Clean_Ocpp_ResetReq();
  5974. LOG_INFO("****** Hard Reboot ******");
  5975. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  5976. {
  5977. setChargerMode(i, MODE_MAINTAIN);
  5978. }
  5979. sleep(3);
  5980. system("reboot -f");
  5981. }
  5982. }
  5983. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  5984. {
  5985. //trigger dispenser to software reset
  5986. TriggerDispenserSoftwareReset();
  5987. if(IsDispenserSoftwareResetResponsed())
  5988. {
  5989. Clean_Ocpp_ResetReq();
  5990. LOG_INFO("****** Soft Reboot ******");
  5991. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  5992. {
  5993. setChargerMode(i, MODE_MAINTAIN);
  5994. }
  5995. sleep(3);
  5996. Kill_Ocpp_Service();
  5997. KillAllTask();
  5998. TryCloseWatchdog();
  5999. system("/usr/bin/run_evse_restart.sh");
  6000. }
  6001. }
  6002. }
  6003. }
  6004. CheckOcppCostAndPrice();
  6005. CheckLcmPageChangeRequest();
  6006. CheckStationInfo();
  6007. CheckWeatherInfo();
  6008. CheckDeductResultRequest();
  6009. CheckReDeductRequest();
  6010. }
  6011. void CheckoutStartChargingSoc(byte gunIndex, bool force)
  6012. {
  6013. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc)
  6014. {
  6015. if(chargingInfo[gunIndex]->EvBatterySoc > 0 || force)
  6016. {
  6017. chargingInfo[gunIndex]->EvBatteryStartSoc = chargingInfo[gunIndex]->EvBatterySoc;
  6018. LOG_INFO("Gun %d StartSoc = %d", chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->EvBatteryStartSoc);
  6019. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc = true;
  6020. }
  6021. }
  6022. }
  6023. void OcppStartTransaction(byte gunIndex)
  6024. {
  6025. // clean deduct info
  6026. Clean_DeductInfo(gunIndex);
  6027. // set deduct user id
  6028. Set_Deduct_UserId(gunIndex, (char *)&chargingInfo[gunIndex]->StartUserId);
  6029. Set_Ocpp_StartTransactionId(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  6030. LOG_INFO("Gun %d Start Transaction IdTag = %s", chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->StartUserId);
  6031. Clean_Ocpp_StartTransactionConf(gunIndex);
  6032. Set_Ocpp_StartTransactionReq(gunIndex);
  6033. }
  6034. void OcppStopTransaction(byte gunIndex)
  6035. {
  6036. Set_Ocpp_StopTransactionId(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  6037. LOG_INFO("Gun %d Stop Transaction IdTag = %s, StopSoc = %d",
  6038. chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->StartUserId, chargingInfo[gunIndex]->EvBatterySoc);
  6039. Set_Ocpp_StopTransactionReq(gunIndex);
  6040. }
  6041. void ChkOcppStatus(byte gunIndex)
  6042. {
  6043. Chk_Ocpp_Backup_ErrorCode(gunIndex);
  6044. if(Is_Ocpp_ReserveNowReq(gunIndex))
  6045. {
  6046. Clean_Ocpp_ReserveNowReq(gunIndex);
  6047. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE && !Is_Ocpp_ReserveNowExpired(gunIndex))
  6048. {
  6049. LOG_INFO("***************ChkOcppStatus: Gun %d OcppReservedStatus********************", gunIndex + 1);
  6050. chargingInfo[gunIndex]->ReservationId = Get_Ocpp_ReserveNowId(gunIndex);
  6051. setChargerMode(gunIndex, MODE_RESERVATION);
  6052. }
  6053. Set_Ocpp_ReserveNowConf(gunIndex);
  6054. }
  6055. if(Is_Ocpp_CancelReservationReq(gunIndex))
  6056. {
  6057. Clean_Ocpp_CancelReservationReq(gunIndex);
  6058. if(chargingInfo[gunIndex]->SystemStatus == S_RESERVATION)
  6059. {
  6060. LOG_INFO("***************ChkOcppStatus: Gun %d Cancel OcppReservedStatus********************", gunIndex + 1);
  6061. chargingInfo[gunIndex]->ReservationId = -1;
  6062. setChargerMode(gunIndex, MODE_IDLE);
  6063. }
  6064. Set_Ocpp_CancelReservationConf(gunIndex);
  6065. }
  6066. if (Is_Ocpp_ChangeAvailabilityReq(gunIndex) == YES)
  6067. {
  6068. LOG_INFO("***************ChkOcppStatus: Gun %d OcppChangeAvailability********************", gunIndex + 1);
  6069. Clean_Ocpp_ChangeAvailabilityReq(gunIndex);
  6070. if(Chk_Ocpp_Availability_Type(gunIndex, "Operative"))
  6071. {
  6072. if (isDb_ready)
  6073. DB_Update_Operactive(localDb, gunIndex, true);
  6074. chargingInfo[gunIndex]->IsAvailable = YES;
  6075. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  6076. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  6077. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  6078. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  6079. {
  6080. setChargerMode(gunIndex, MODE_IDLE);
  6081. }
  6082. }
  6083. else if(Chk_Ocpp_Availability_Type(gunIndex, "Inoperative"))
  6084. {
  6085. if (isDb_ready)
  6086. DB_Update_Operactive(localDb, gunIndex, false);
  6087. chargingInfo[gunIndex]->IsAvailable = NO;
  6088. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  6089. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  6090. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  6091. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  6092. {
  6093. setChargerMode(gunIndex, MODE_MAINTAIN);
  6094. }
  6095. }
  6096. }
  6097. if (Is_Ocpp_UnlockConnectorReq(gunIndex))
  6098. {
  6099. Clean_Ocpp_UnlockConnectorReq(gunIndex);
  6100. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  6101. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  6102. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  6103. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  6104. {
  6105. // 充電中,需停止充電
  6106. Set_Ocpp_StopReason(gunIndex, "UnlockCommand");
  6107. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_UNLOCK);
  6108. chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop = true;
  6109. LOG_INFO("********** OcppUnlockStop Gun %d **********", gunIndex + 1);
  6110. }
  6111. Set_Ocpp_UnlockConnectorConf(gunIndex);
  6112. }
  6113. if (Is_Ocpp_RemoteStopReq(gunIndex))
  6114. {
  6115. Clean_Ocpp_RemoteStopReq(gunIndex);
  6116. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  6117. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  6118. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  6119. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  6120. {
  6121. Set_Ocpp_StopReason(gunIndex, "Remote");
  6122. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_REMOTE_STOP);
  6123. chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop = true;
  6124. LOG_INFO("********** OcppRemoteStop Gun %d **********", gunIndex + 1);
  6125. }
  6126. }
  6127. if(Is_Ocpp_StartTransactionConf(gunIndex))
  6128. {
  6129. char acceptId[128] = {0};
  6130. char status[32] = {0};
  6131. Get_Ocpp_StartTransaction_Status(gunIndex, status);
  6132. Get_Ocpp_TransactionId(gunIndex, acceptId);
  6133. Set_Deduct_TransactionId(gunIndex, atoi(acceptId));
  6134. Set_UserTransactionId(gunIndex, atoi(acceptId));
  6135. LOG_INFO("Gun %d OCPP Start Transaction %s, TransactionId [%s]", gunIndex + 1, status, acceptId);
  6136. if(!Is_Ocpp_StartTransaction_Accepted(gunIndex))
  6137. {
  6138. if(Is_Ocpp_StopOnInvalidId_Enable())
  6139. {
  6140. if(Is_Invalid_StartTransactionId(gunIndex))
  6141. {
  6142. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop = true;
  6143. LOG_INFO("Gun %d OCPP StopOnInvalidId", gunIndex + 1);
  6144. }
  6145. }
  6146. LOG_INFO("Gun %d OCPP Start Transaction Accepted, TransactionId [%s]", gunIndex + 1, acceptId);
  6147. }
  6148. Clean_Ocpp_StartTransactionConf(gunIndex);
  6149. }
  6150. }
  6151. bool CheckBackendChargingTimeout(byte gunIndex)
  6152. {
  6153. bool result = false;
  6154. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  6155. {
  6156. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  6157. {
  6158. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  6159. result = true;
  6160. }
  6161. }
  6162. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  6163. {
  6164. // 隨插即充電的要看 offline
  6165. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  6166. {
  6167. if (chargingInfo[gunIndex]->PresentChargedDuration > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  6168. result = true;
  6169. }
  6170. }
  6171. return result;
  6172. }
  6173. bool CheckBackendChargingEnergy(byte gunIndex)
  6174. {
  6175. bool result = false;
  6176. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  6177. {
  6178. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  6179. {
  6180. if (chargingInfo[gunIndex]->PresentChargedEnergy > ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  6181. result = true;
  6182. }
  6183. }
  6184. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  6185. {
  6186. // 隨插即充電的要看 offline
  6187. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  6188. {
  6189. if (chargingInfo[gunIndex]->PresentChargedEnergy > (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  6190. result = true;
  6191. }
  6192. }
  6193. return result;
  6194. }
  6195. void InformOcppErrOccur(byte codeType)
  6196. {
  6197. char _error[25];
  6198. switch(codeType)
  6199. {
  6200. case 4: strcpy(_error, "InternalError"); break;
  6201. case 6: strcpy(_error, "NoError"); break;
  6202. case 7: strcpy(_error, "OtherError"); break;
  6203. case 13: strcpy(_error, "UnderVoltage"); break;
  6204. case 14: strcpy(_error, "OverVoltage"); break;
  6205. }
  6206. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  6207. {
  6208. SetOcppErrorCode(gun_index, _error);
  6209. }
  6210. }
  6211. void SetOcppErrorCode(unsigned char _index, char *errCode)
  6212. {
  6213. Set_Ocpp_StatusNotificationErrorCode(_index, errCode);
  6214. strcpy((char *)_BackupStatusNotification[_index].ErrorCode, errCode);
  6215. }
  6216. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode)
  6217. {
  6218. Set_Ocpp_StatusNotificationVendorCode(_index, vendorCode);
  6219. strcpy((char *)_BackupStatusNotification[_index].VendorErrorCode, vendorCode);
  6220. }
  6221. //===============================================
  6222. // SQLite3 related routine
  6223. //===============================================
  6224. int NetworkDB_Open(sqlite3 *db)
  6225. {
  6226. int result = PASS;
  6227. char* errMsg = NULL;
  6228. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  6229. "idx integer primary key AUTOINCREMENT, "
  6230. "occurDatetime text NOT NULL, "
  6231. "isInternet text NOT NULL, "
  6232. "isOcppConnected text NOT NULL, "
  6233. "isEth0Internet text NOT NULL, "
  6234. "isMlan0Internet text NOT NULL, "
  6235. "isPpp0Internet text NOT NULL, "
  6236. "rssiMlan0 text NOT NULL, "
  6237. "rssiPpp0 text NOT NULL"
  6238. ");";
  6239. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  6240. if(sqlite3_open(NETWORK_DB_FILE, &db))
  6241. {
  6242. result = FAIL;
  6243. LOG_ERROR("Can't open database (Net) : %s", sqlite3_errmsg(db));
  6244. sqlite3_close(db);
  6245. }
  6246. else
  6247. {
  6248. LOG_INFO("Local network status record database open successfully.");
  6249. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6250. {
  6251. result = FAIL;
  6252. LOG_ERROR("Create local network status record table error message: %s", errMsg);
  6253. }
  6254. else
  6255. {
  6256. LOG_INFO("Opened local network status record table successfully");
  6257. }
  6258. sqlite3_close(db);
  6259. }
  6260. return result;
  6261. }
  6262. int DB_Open(sqlite3 *db)
  6263. {
  6264. int result = PASS;
  6265. char* errMsg = NULL;
  6266. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  6267. "idx integer primary key AUTOINCREMENT, "
  6268. "reservationId text, "
  6269. "transactionId text, "
  6270. "startMethod text, "
  6271. "userId text, "
  6272. "dateTimeStart text, "
  6273. "dateTimeStop text,"
  6274. "socStart text, "
  6275. "socStop text, "
  6276. "chargeEnergy text, "
  6277. "stopReason text"
  6278. ");";
  6279. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  6280. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6281. "`IsAvailable` TEXT NOT NULL, "
  6282. "`connector` INTEGER NOT NULL, "
  6283. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  6284. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  6285. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6286. "`occurDatetime` TEXT NOT NULL, "
  6287. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  6288. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  6289. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6290. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  6291. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  6292. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  6293. "`connector` INTEGER NOT NULL, "
  6294. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  6295. if(sqlite3_open(DB_FILE, &db))
  6296. {
  6297. result = FAIL;
  6298. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  6299. sqlite3_close(db);
  6300. }
  6301. else
  6302. {
  6303. LOG_INFO( "Local charging record database open successfully.");
  6304. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6305. {
  6306. result = FAIL;
  6307. LOG_ERROR( "Create local charging record table error message: %s", errMsg);
  6308. }
  6309. else
  6310. {
  6311. LOG_INFO( "Opened local charging record table successfully");
  6312. }
  6313. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  6314. {
  6315. result = FAIL;
  6316. LOG_ERROR( "Create local config table error message: %s", errMsg);
  6317. }
  6318. else
  6319. {
  6320. LOG_INFO( "Opened local config table successfully");
  6321. }
  6322. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  6323. {
  6324. result = FAIL;
  6325. LOG_ERROR( "Create local record table error message: %s", errMsg);
  6326. }
  6327. else
  6328. {
  6329. LOG_INFO( "Opened local record table successfully");
  6330. }
  6331. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  6332. {
  6333. result = FAIL;
  6334. LOG_ERROR( "Create reboot record table error message: %s", errMsg);
  6335. }
  6336. else
  6337. {
  6338. LOG_INFO( "Opened reboot record table successfully");
  6339. }
  6340. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  6341. {
  6342. result = FAIL;
  6343. LOG_ERROR( "Create power consumption record table error message: %s", errMsg);
  6344. }
  6345. else
  6346. {
  6347. LOG_INFO( "Opened power consumption record table successfully");
  6348. }
  6349. sqlite3_close(db);
  6350. }
  6351. return result;
  6352. }
  6353. int DB_Insert_Record(sqlite3 *db, int gun_index)
  6354. {
  6355. int result = PASS;
  6356. char* errMsg = NULL;
  6357. char insertSql[1024];
  6358. char transactionId[128];
  6359. char reason[32];
  6360. Get_Ocpp_TransactionId(gun_index, transactionId);
  6361. Get_Ocpp_StopReason(gun_index, reason);
  6362. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  6363. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  6364. chargingInfo[gun_index]->ReservationId,
  6365. transactionId,
  6366. chargingInfo[gun_index]->StartMethod,
  6367. chargingInfo[gun_index]->StartUserId,
  6368. chargingInfo[gun_index]->StartDateTime,
  6369. chargingInfo[gun_index]->StopDateTime,
  6370. chargingInfo[gun_index]->EvBatteryStartSoc,
  6371. chargingInfo[gun_index]->EvBatterySoc,
  6372. chargingInfo[gun_index]->PresentChargedEnergy,
  6373. reason);
  6374. if(sqlite3_open(DB_FILE, &db))
  6375. {
  6376. result = FAIL;
  6377. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  6378. sqlite3_close(db);
  6379. }
  6380. else
  6381. {
  6382. //LOG_INFO( "Local charging record database open successfully.");
  6383. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6384. {
  6385. result = FAIL;
  6386. LOG_ERROR("Insert local charging record error message: %s", errMsg);
  6387. }
  6388. else
  6389. {
  6390. //LOG_INFO("Insert local charging record successfully");
  6391. }
  6392. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  6393. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6394. {
  6395. result = FAIL;
  6396. LOG_ERROR("delete local charging error message: %s", errMsg);
  6397. }
  6398. else
  6399. {
  6400. //LOG_INFO("delete local charging record successfully");
  6401. }
  6402. sqlite3_close(db);
  6403. }
  6404. return result;
  6405. }
  6406. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  6407. {
  6408. uint8_t result = false;
  6409. char* errMsg = NULL;
  6410. char sqlStr[1024];
  6411. srand(time(NULL));
  6412. if(sqlite3_open(DB_FILE, &db))
  6413. {
  6414. result = FAIL;
  6415. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  6416. sqlite3_close(db);
  6417. }
  6418. else
  6419. {
  6420. LOG_INFO( "Local charging record database open successfully (%d).", IsAvailable);
  6421. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  6422. LOG_INFO("sqlStr= %s", sqlStr);
  6423. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  6424. {
  6425. result = FAIL;
  6426. LOG_INFO( "update config error message: %s", errMsg);
  6427. }
  6428. else
  6429. {
  6430. LOG_INFO("update connector-%d config item isOperactive to %d", gun_index, IsAvailable);
  6431. }
  6432. sqlite3_close(db);
  6433. }
  6434. return result;
  6435. }
  6436. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  6437. {
  6438. uint8_t result = true;
  6439. char* errMsg = NULL;
  6440. char sqlStr[1024];
  6441. char **rs;
  6442. int rows, cols;
  6443. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  6444. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  6445. if(sqlite3_open(DB_FILE, &db))
  6446. {
  6447. result = FAIL;
  6448. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  6449. sqlite3_close(db);
  6450. }
  6451. else
  6452. {
  6453. LOG_INFO( "Local config query database open successfully.");
  6454. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  6455. if(rows>0)
  6456. {
  6457. for(int idxRow=1;idxRow<=rows;idxRow++)
  6458. {
  6459. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  6460. {
  6461. result = false;
  6462. }
  6463. LOG_INFO("Query connector-%d isOperactive: %s", gun_index, rs[(idxRow*cols)+3]);
  6464. }
  6465. }
  6466. else
  6467. {
  6468. LOG_INFO("Query connector-%d fail, set default value to operactive.", gun_index);
  6469. }
  6470. sqlite3_free_table(rs);
  6471. sqlite3_close(db);
  6472. }
  6473. return result;
  6474. }
  6475. void DB_Update_PowerConsumption(sqlite3 *db, uint8_t gun_index, float value)
  6476. {
  6477. char* errMsg = NULL;
  6478. char sqlStr[1024];
  6479. srand(time(NULL));
  6480. if(sqlite3_open(DB_FILE, &db))
  6481. {
  6482. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  6483. sqlite3_close(db);
  6484. }
  6485. else
  6486. {
  6487. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  6488. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  6489. {
  6490. LOG_ERROR("update power_consumption error message: %s", errMsg);
  6491. }
  6492. else
  6493. {
  6494. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  6495. }
  6496. sqlite3_close(db);
  6497. }
  6498. }
  6499. float DB_Get_PowerConsumption(sqlite3 *db, uint8_t gun_index)
  6500. {
  6501. float result = 0;
  6502. char* errMsg = NULL;
  6503. char sqlStr[1024];
  6504. char **rs;
  6505. int rows, cols;
  6506. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  6507. if (sqlite3_open(DB_FILE, &db))
  6508. {
  6509. result = FAIL;
  6510. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  6511. sqlite3_close(db);
  6512. }
  6513. else
  6514. {
  6515. //LOG_INFO("Local config query database open successfully.");
  6516. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  6517. if (rows > 0)
  6518. {
  6519. for (int idxRow = 1; idxRow <= rows; idxRow++)
  6520. {
  6521. result = atof(rs[(idxRow * cols) + 2]);
  6522. }
  6523. }
  6524. else
  6525. {
  6526. LOG_INFO("Query connector-%d fail, set default value to consumption.", gun_index);
  6527. }
  6528. sqlite3_free_table(rs);
  6529. sqlite3_close(db);
  6530. }
  6531. return result;
  6532. }
  6533. int DB_Reboot_Record(sqlite3 *db)
  6534. {
  6535. int result = PASS;
  6536. char* errMsg = NULL;
  6537. char insertSql[256];
  6538. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  6539. if(sqlite3_open(DB_FILE, &db))
  6540. {
  6541. result = FAIL;
  6542. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  6543. sqlite3_close(db);
  6544. }
  6545. else
  6546. {
  6547. LOG_INFO("Local charging record database open successfully.");
  6548. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6549. {
  6550. result = FAIL;
  6551. LOG_ERROR("Insert reboot record error message: %s", errMsg);
  6552. }
  6553. else
  6554. {
  6555. //LOG_INFO("Insert reboot record successfully");
  6556. }
  6557. sprintf(insertSql, "delete from reboot_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  6558. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  6559. {
  6560. result = FAIL;
  6561. LOG_ERROR("delete reboot record error message: %s", errMsg);
  6562. }
  6563. else
  6564. {
  6565. //LOG_INFO("delete reboot record successfully");
  6566. }
  6567. sqlite3_close(db);
  6568. }
  6569. return result;
  6570. }
  6571. //===============================================
  6572. // Config process
  6573. //===============================================
  6574. void AddPlugInTimes(byte gunIndex)
  6575. {
  6576. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  6577. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  6578. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  6579. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  6580. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  6581. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  6582. }
  6583. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  6584. {
  6585. char cmdBuf[32];
  6586. struct timeb csuTime;
  6587. struct tm *tmCSU;
  6588. ftime(&csuTime);
  6589. tmCSU = localtime(&csuTime.time);
  6590. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  6591. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  6592. tmCSU->tm_sec);
  6593. if (isStart)
  6594. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  6595. else
  6596. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  6597. }
  6598. void zipLogFiles()
  6599. {
  6600. const char* logPath = "/Storage/SystemLog";
  6601. // 獲取目錄
  6602. DIR* pDir = opendir(logPath);
  6603. if (pDir != NULL)
  6604. {
  6605. struct timeb csuTime;
  6606. struct tm *tmCSU;
  6607. ftime(&csuTime);
  6608. tmCSU = localtime(&csuTime.time);
  6609. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  6610. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  6611. // tmCSU->tm_sec);
  6612. // Read items inside the folder
  6613. struct dirent* pEntry = NULL;
  6614. while ((pEntry = readdir(pDir)) != NULL)
  6615. {
  6616. if (strcmp(pEntry->d_name, ".") != 0 &&
  6617. strcmp(pEntry->d_name, "..") != 0 &&
  6618. strncmp(pEntry->d_name, "[", 1) == 0 &&
  6619. strstr(pEntry->d_name, "tar") < 0)
  6620. {
  6621. char yearC[5];
  6622. unsigned short year = 0;
  6623. char monthC[3];
  6624. unsigned short month = 0;
  6625. yearC[4] = '\0';
  6626. strncpy(yearC, pEntry->d_name + 1, 4);
  6627. monthC[2] = '\0';
  6628. strncpy(monthC, pEntry->d_name + 6, 2);
  6629. year = atoi(yearC);
  6630. month = atoi(monthC);
  6631. if (year != 0)
  6632. {
  6633. if (year < tmCSU->tm_year + 1900 ||
  6634. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  6635. {
  6636. LOG_INFO("tar file name : %s", pEntry->d_name);
  6637. char file[256];
  6638. memset(file, 0x00, sizeof(file));
  6639. strcat(file, "tar zcvf ");
  6640. strcat(file, logPath);
  6641. strncat(file, "/", 1);
  6642. strcat(file, pEntry->d_name);
  6643. strcat(file, ".tar");
  6644. strncat(file, " ", 1);
  6645. strcat(file, logPath);
  6646. strncat(file, "/", 1);
  6647. strcat(file, pEntry->d_name);
  6648. LOG_INFO("zip = %s \n", file);
  6649. system(file);
  6650. }
  6651. }
  6652. }
  6653. }
  6654. }
  6655. // Close folder
  6656. closedir(pDir);
  6657. }
  6658. void StopProcessingLoop()
  6659. {
  6660. for (;;)
  6661. {
  6662. CheckFactoryConfigFunction();
  6663. CheckFwUpdateFunction();
  6664. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  6665. {
  6666. ChkPrimaryStatus();
  6667. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0)
  6668. {
  6669. LOG_INFO("Soft reboot for retry self-tets (Primary).");
  6670. KillAllTask();
  6671. sleep(3);
  6672. TryCloseWatchdog();
  6673. system("/usr/bin/run_evse_restart.sh");
  6674. return;
  6675. }
  6676. }
  6677. if(Is_Ocpp_ResetReq())
  6678. {
  6679. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  6680. {
  6681. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = true;
  6682. }
  6683. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  6684. {
  6685. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  6686. }
  6687. }
  6688. CheckSoftHardReset();
  6689. sleep(1);
  6690. }
  6691. }
  6692. void CreateWatchdog(void)
  6693. {
  6694. wtdFd = InitWatchDog();
  6695. if(wtdFd < 0)
  6696. {
  6697. LOG_INFO("Watchdog Initial Fail");
  6698. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  6699. }
  6700. else
  6701. {
  6702. LOG_INFO("Watchdog Initial Success");
  6703. }
  6704. }
  6705. void TryCloseWatchdog(void)
  6706. {
  6707. if(wtdFd > 0)
  6708. {
  6709. write(wtdFd, "V", 1);
  6710. close(wtdFd);
  6711. }
  6712. }
  6713. void TryFeedWatchdog(void)
  6714. {
  6715. if(wtdFd > 0)
  6716. {
  6717. write(wtdFd, "a", 1);
  6718. }
  6719. }
  6720. bool IsParallelCabinetIdle(void)
  6721. {
  6722. bool result = true;
  6723. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  6724. {
  6725. result = false;
  6726. }
  6727. return result;
  6728. }
  6729. bool IsStandbyEnable(void)
  6730. {
  6731. bool result = true;
  6732. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6733. {
  6734. time_t t = time(NULL);
  6735. struct tm *now = localtime(&t);
  6736. int timeHour = now->tm_hour + (ShmChargerInfo->CabinetMiscValue.TimeOffset / 60);
  6737. if(timeHour >= 24)
  6738. {
  6739. timeHour -= 24;
  6740. }
  6741. else if(timeHour < 0)
  6742. {
  6743. timeHour += 24;
  6744. }
  6745. if(timeHour >= 0 && timeHour < 8)
  6746. {
  6747. if(!_isStandbyEnable)
  6748. {
  6749. LOG_INFO("[TCC]Customer Enable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  6750. now->tm_hour, now->tm_min, now->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  6751. }
  6752. _isStandbyEnable = true;
  6753. result = true;
  6754. }
  6755. else
  6756. {
  6757. if(_isStandbyEnable)
  6758. {
  6759. LOG_INFO("[TCC]Customer Disable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  6760. now->tm_hour, now->tm_min, now->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  6761. }
  6762. _isStandbyEnable = false;
  6763. result = false;
  6764. }
  6765. }
  6766. return result;
  6767. }
  6768. bool IsConnectorWholeIdle(void)
  6769. {
  6770. bool result = true;
  6771. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  6772. {
  6773. if (chargingInfo[count]->SystemStatus != S_IDLE &&
  6774. chargingInfo[count]->SystemStatus != S_RESERVATION &&
  6775. chargingInfo[count]->SystemStatus != S_FAULT &&
  6776. chargingInfo[count]->SystemStatus != S_MAINTAIN)
  6777. {
  6778. result = false;
  6779. break;
  6780. }
  6781. }
  6782. return result;
  6783. }
  6784. void ClearAlarmCodeWhenAcOff()
  6785. {
  6786. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  6787. {
  6788. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  6789. }
  6790. }
  6791. //==========================================
  6792. // Check task processing
  6793. //==========================================
  6794. void CheckTask()
  6795. {
  6796. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  6797. {
  6798. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  6799. {
  6800. if(system("pidof -s Module_4g > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  6801. {
  6802. LOG_ERROR("Module_4g not running, restart it.");
  6803. system("/root/Module_4g &");
  6804. }
  6805. }
  6806. }
  6807. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  6808. {
  6809. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  6810. {
  6811. if(system("pidof -s Module_Wifi > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  6812. {
  6813. LOG_ERROR("Module_Wifi not running, restart it.");
  6814. system("/root/Module_Wifi &");
  6815. }
  6816. }
  6817. }
  6818. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  6819. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  6820. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  6821. {
  6822. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = true;
  6823. if((time((time_t*)NULL) - Get_Ocpp_procDogTime()) > 180)
  6824. {
  6825. LOG_ERROR("OcppBackend watch dog timeout task restart.");
  6826. Clean_Ocpp_procDogTime();
  6827. Kill_Ocpp_Service();
  6828. }
  6829. if(!Is_Ocpp_Task_Alive())
  6830. {
  6831. LOG_ERROR("OcppBackend not running, restart it.");
  6832. Run_Ocpp_Service();
  6833. }
  6834. }
  6835. else
  6836. {
  6837. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = false;
  6838. }
  6839. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  6840. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  6841. {
  6842. if(!Is_MaintainOcpp_Task_Alive())
  6843. {
  6844. LOG_ERROR("Maintain Backend not running, restart it.");
  6845. Run_MaintainOcpp_Service();
  6846. }
  6847. }
  6848. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  6849. {
  6850. LOG_ERROR("Module_ProduceUtils not running, restart it.");
  6851. system ("/root/Module_ProduceUtils &");
  6852. }
  6853. if(system("pidof -s Module_EvComm > /dev/null") != 0)
  6854. {
  6855. LOG_ERROR("Module_EvComm not running, restart it.");
  6856. system ("/root/Module_EvComm &");
  6857. }
  6858. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  6859. {
  6860. LOG_ERROR("Module_InternalComm not running, restart it.");
  6861. system ("/root/Module_InternalComm &");
  6862. //ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  6863. }
  6864. }
  6865. void InitialDHCP()
  6866. {
  6867. char tmpbuf[256];
  6868. memset(tmpbuf,0,256);
  6869. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  6870. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  6871. system(tmpbuf);
  6872. }
  6873. void InitialDispenserDhcpServerConfig(void)
  6874. {
  6875. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  6876. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  6877. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  6878. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  6879. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  6880. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  6881. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  6882. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  6883. usleep(1000);
  6884. }
  6885. void StartDispenserDhcpServer(void)
  6886. {
  6887. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  6888. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  6889. }
  6890. //==========================================
  6891. // Check Smart Charging Profile
  6892. //==========================================
  6893. void Ocpp_ProfileReq_Cmd(byte gunIndex)
  6894. {
  6895. if(!Is_Ocpp_ChargingProfileConf(gunIndex))
  6896. {
  6897. if(!Is_Ocpp_ChargingProfileReq(gunIndex))
  6898. {
  6899. Set_Ocpp_ChargingProfileReq(gunIndex);
  6900. }
  6901. }
  6902. }
  6903. int GetStartScheduleTime(unsigned char *time)
  6904. {
  6905. int result = -1;
  6906. struct tm tmScheduleStart;
  6907. struct timeb tbScheduleStart;
  6908. 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))
  6909. {
  6910. tmScheduleStart.tm_year -= 1900;
  6911. tmScheduleStart.tm_mon -= 1;
  6912. tbScheduleStart.time = mktime(&tmScheduleStart);
  6913. tbScheduleStart.millitm = 0;
  6914. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  6915. }
  6916. return result;
  6917. }
  6918. void Ocpp_ChargingProfile_Process(byte _index)
  6919. {
  6920. int _startCount = NO_DEFINE;
  6921. int _maxCount = 0;
  6922. float _limit = 0;
  6923. int _numberPhases = 0;
  6924. if(Is_Ocpp_ChargingProfileKind(_index, "Absolute") && Is_Ocpp_ChargingProfileId(_index))
  6925. {
  6926. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(_index);
  6927. _startCount = Get_Ocpp_ChargingProfileStartIndex(_index);
  6928. if(_startCount < _maxCount)
  6929. {
  6930. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  6931. _numberPhases = Get_Ocpp_ChargingProfileScheduleNumberPhases(_index, _startCount);
  6932. chargingInfo[_index]->ChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  6933. if(chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  6934. (int)chargingInfo[_index]->PresentChargingVoltage > 0)
  6935. {
  6936. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  6937. }
  6938. else
  6939. {
  6940. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6941. }
  6942. }
  6943. else
  6944. {
  6945. chargingInfo[_index]->ChargingProfilePower = -1;
  6946. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6947. }
  6948. }
  6949. else
  6950. {
  6951. chargingInfo[_index]->ChargingProfilePower = -1;
  6952. chargingInfo[_index]->ChargingProfileCurrent = -1;
  6953. }
  6954. if((int)_lastProfilePower[_index] != (int)chargingInfo[_index]->ChargingProfilePower ||
  6955. (int)_lastProfileCurrent[_index] != (int)chargingInfo[_index]->ChargingProfileCurrent)
  6956. {
  6957. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  6958. LOG_INFO("Gun %d Get Profile - Index = %d, Limit = %f",
  6959. _index + 1, _startCount, _limit);
  6960. LOG_INFO("Max: %d kW, Gun %d ChargingProfile Power = %d, Current = %d",
  6961. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower == -1 ? (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower : ((int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000),
  6962. _index + 1, (int)chargingInfo[_index]->ChargingProfilePower, (int)chargingInfo[_index]->ChargingProfileCurrent);
  6963. }
  6964. _lastProfilePower[_index] = chargingInfo[_index]->ChargingProfilePower;
  6965. _lastProfileCurrent[_index] = chargingInfo[_index]->ChargingProfileCurrent;
  6966. }
  6967. void Ocpp_MaxChargingProfile_Process(void)
  6968. {
  6969. int _startCount = NO_DEFINE;
  6970. int _maxCount = 0;
  6971. float _limit = 0;
  6972. int _numberPhases = 0;
  6973. if(Is_Ocpp_MaxChargingProfileKind("Absolute") && Is_Ocpp_MaxChargingProfileId())
  6974. {
  6975. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  6976. _startCount = Get_Ocpp_MaxProfileStartIndex();
  6977. if(_startCount < _maxCount)
  6978. {
  6979. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  6980. _numberPhases = Get_Ocpp_MaxProfileScheduleNumberPhases(_startCount);
  6981. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  6982. }
  6983. else
  6984. {
  6985. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6986. }
  6987. }
  6988. else
  6989. {
  6990. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  6991. }
  6992. if((int)_lastMaxProfilePower != (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower)
  6993. {
  6994. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  6995. LOG_INFO("Charger Get Max Profile - Index = %d, Limit = %f",
  6996. _startCount, _limit);
  6997. LOG_INFO("Charger Max Profile Power = %d", (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower);
  6998. }
  6999. _lastMaxProfilePower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower;
  7000. }
  7001. void CheckSmartChargeProfile(byte _index)
  7002. {
  7003. if(Is_Ocpp_ChargingProfileConf(_index))
  7004. {
  7005. Clean_Ocpp_ChargingProfileConf(_index);
  7006. Ocpp_MaxChargingProfile_Process();
  7007. Ocpp_ChargingProfile_Process(_index);
  7008. }
  7009. }
  7010. void TheEndCharging(byte gun_index)
  7011. {
  7012. chargingInfo[gun_index]->isRemoteStart = NO;
  7013. //StopGunInfoTimeoutDet(gun_index);
  7014. //StartGunInfoTimeoutDet(gun_index, Timeout_EvseCompleteDet);
  7015. ChangeStartOrStopDateTime(NO, gun_index);
  7016. DB_Insert_Record(localDb, gun_index);
  7017. }
  7018. void UpdateErrorCodeToOcpp(byte index)
  7019. {
  7020. //printf("ConnectorAlarmCode = %s \n", chargingInfo[index]->ConnectorAlarmCode);
  7021. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL)
  7022. {
  7023. SetOcppErrorCode(index, "InternalError");
  7024. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->ConnectorAlarmCode);
  7025. }
  7026. else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL)
  7027. {
  7028. SetOcppErrorCode(index, "OtherError");
  7029. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->EvConnAlarmCode);
  7030. }
  7031. }
  7032. void CheckMiscCommandRequirement(void)
  7033. {
  7034. BOOL NeedAnnouncement = FALSE;
  7035. unsigned char connector = 0;
  7036. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate != YES)
  7037. {
  7038. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7039. {
  7040. NeedAnnouncement = FALSE;
  7041. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  7042. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  7043. {
  7044. if(ShmChargerInfo->DispenserMiscReq[i].CtrlValue != 0)
  7045. {
  7046. NeedAnnouncement = TRUE;
  7047. }
  7048. for(int j = 0; j < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity; j++)
  7049. {
  7050. connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorID[j] - 1;
  7051. if(ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue != 0)
  7052. {
  7053. NeedAnnouncement = TRUE;
  7054. }
  7055. }
  7056. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement != NeedAnnouncement)
  7057. {
  7058. if(NeedAnnouncement)
  7059. {
  7060. //LOG_INFO("********** Dispenser %d Misc Command Need Announcement **********", i + 1);
  7061. }
  7062. else
  7063. {
  7064. //LOG_INFO("********** Dispenser %d Misc Command Announced **********", i + 1);
  7065. }
  7066. }
  7067. }
  7068. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  7069. }
  7070. }
  7071. }
  7072. void CheckDispenserVersionUpdateRequirement(void)
  7073. {
  7074. }
  7075. bool IsReadyParalleling(void)
  7076. {
  7077. bool ready = true;
  7078. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  7079. {
  7080. if(chargingInfo[i]->SystemStatus != S_IDLE && chargingInfo[i]->SystemStatus != S_MAINTAIN)
  7081. {
  7082. ready = false;
  7083. break;
  7084. }
  7085. }
  7086. return ready;
  7087. }
  7088. void SetParallelingStart(void)
  7089. {
  7090. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7091. {
  7092. if(chargingInfo[i]->SystemStatus == S_IDLE)
  7093. {
  7094. setChargerMode(i, MODE_MAINTAIN);
  7095. }
  7096. }
  7097. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Wait;
  7098. }
  7099. void SetParallelingDone(void)
  7100. {
  7101. unsigned short totalPow = 0;
  7102. totalPow = GetTotalParallelCabinetPower();
  7103. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  7104. {
  7105. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  7106. }
  7107. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  7108. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_None;
  7109. }
  7110. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff)
  7111. {
  7112. unsigned char relay = 0;
  7113. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  7114. {
  7115. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7116. {
  7117. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  7118. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  7119. {
  7120. relay = OnOff == YES ? YES : NO;
  7121. if(ShmChargerInfo->ParallelCabinet.PCabinet[index].OutputRelaySetting[index] != relay)
  7122. {
  7123. LOG_INFO("Set Parallel Cabinet Gun %d Output Relay %s", index + 1, relay == YES ? "ON" : "OFF");
  7124. }
  7125. ShmChargerInfo->ParallelCabinet.PCabinet[i].OutputRelaySetting[index] = relay;
  7126. }
  7127. }
  7128. }
  7129. }
  7130. void SetParallelCabinetAcContactor(unsigned char OnOff)
  7131. {
  7132. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7133. {
  7134. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  7135. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  7136. {
  7137. ShmChargerInfo->ParallelCabinet.PCabinet[i].AcContactorSetting = OnOff == YES ? YES : NO;
  7138. }
  7139. }
  7140. }
  7141. unsigned short GetTotalParallelCabinetPower(void)
  7142. {
  7143. unsigned short power = 0, slavePow = 0;
  7144. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  7145. {
  7146. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  7147. {
  7148. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  7149. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  7150. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  7151. {
  7152. slavePow = ParsingRatingPower((char *)ShmChargerInfo->ParallelCabinet.PCabinet[i].ModelName);
  7153. power += slavePow;
  7154. }
  7155. }
  7156. }
  7157. return power;
  7158. }
  7159. unsigned char preParallelStatus = 0;
  7160. void MCabinetParallelingProcess(void)
  7161. {
  7162. bool PsuReInitial = false;
  7163. unsigned char status = 0;
  7164. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  7165. {
  7166. if(IsReadyParalleling())
  7167. {
  7168. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7169. {
  7170. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest)
  7171. {
  7172. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest = false;
  7173. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm = true;
  7174. PsuReInitial = true;
  7175. }
  7176. }
  7177. if(PsuReInitial)
  7178. {
  7179. SetParallelingStart();
  7180. }
  7181. }
  7182. status = ShmChargerInfo->ParallelCabinet.ParallelStatus;
  7183. switch(status)
  7184. {
  7185. case _Parallel_None:
  7186. if(preParallelStatus != status)
  7187. {
  7188. LOG_INFO("MCabinet Parallel Status [None]");
  7189. GetClockTime(&_Parallel_Time);
  7190. }
  7191. unsigned short totalPow = 0;
  7192. totalPow = GetTotalParallelCabinetPower();
  7193. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  7194. {
  7195. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  7196. }
  7197. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  7198. break;
  7199. case _Parallel_Wait:
  7200. if(preParallelStatus != status)
  7201. {
  7202. LOG_INFO("MCabinet Parallel Status [Wait]");
  7203. SetAcContactor(OFF);
  7204. SetParallelCabinetAcContactor(OFF);
  7205. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7206. {
  7207. SetPCabinetOutputRelay(i, OFF);
  7208. }
  7209. GetClockTime(&_Parallel_Time);
  7210. }
  7211. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  7212. {
  7213. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Working;
  7214. }
  7215. break;
  7216. case _Parallel_Working:
  7217. if(preParallelStatus != status)
  7218. {
  7219. LOG_INFO("MCabinet Parallel Status [Working]");
  7220. SetAcContactor(ON);
  7221. SetParallelCabinetAcContactor(ON);
  7222. GetClockTime(&_Parallel_Time);
  7223. }
  7224. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  7225. {
  7226. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Done;
  7227. }
  7228. break;
  7229. case _Parallel_Done:
  7230. if(preParallelStatus != status)
  7231. {
  7232. LOG_INFO("MCabinet Parallel Status [Done]");
  7233. GetClockTime(&_Parallel_Time);
  7234. }
  7235. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  7236. {
  7237. SetParallelingDone();
  7238. }
  7239. break;
  7240. }
  7241. preParallelStatus = status;
  7242. }
  7243. }
  7244. void SCabinetControllProcess(void)
  7245. {
  7246. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  7247. {
  7248. if(ShmChargerInfo->SCabinetControl.SAcContactor)
  7249. {
  7250. SetAcContactor(ON);
  7251. }
  7252. else
  7253. {
  7254. SetAcContactor(OFF);
  7255. }
  7256. unsigned short _parallelRelay = 0;
  7257. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  7258. {
  7259. _parallelRelay = ShmChargerInfo->SCabinetControl.SParallelRelay[i] > 0 ? (1 < i) : 0;
  7260. }
  7261. if(_parallelRelay != ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue)
  7262. {
  7263. LOG_INFO("SCabinet Set Parallel Relay %X -> %X", ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue, _parallelRelay);
  7264. }
  7265. ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue = _parallelRelay;
  7266. }
  7267. }
  7268. void CheckConnectorDisconnectionRecovery(unsigned char connector)
  7269. {
  7270. unsigned char dispenser = 0;
  7271. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  7272. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  7273. {
  7274. LOG_INFO("Connector %d Disconnection Recovery", connector + 1);
  7275. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.Disconnection = false;
  7276. }
  7277. }
  7278. bool ConnectorRequestToCharging(unsigned char connector)
  7279. {
  7280. unsigned char role = 0;
  7281. bool done = false;
  7282. role = ShmPsuGrouping->GroupCollection[connector].Role;
  7283. switch(role)
  7284. {
  7285. case _GROLE_IDLE:
  7286. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest)
  7287. {
  7288. LOG_INFO("Request Gun %d To Start Charging", connector + 1);
  7289. }
  7290. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest = true;
  7291. break;
  7292. case _GROLE_MASTER:
  7293. done = true;
  7294. break;
  7295. case _GROLE_SLAVE:
  7296. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest)
  7297. {
  7298. LOG_INFO("Gun %d Wait For Re-Grouping", connector + 1);
  7299. }
  7300. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest = true;
  7301. break;
  7302. case _GROLE_REQUEST_TO_CHARGING:
  7303. done = true;
  7304. break;
  7305. default:
  7306. break;
  7307. }
  7308. return done;
  7309. }
  7310. bool IsConnectorGroupingCompleted(unsigned char connector)
  7311. {
  7312. unsigned char role = 0;
  7313. bool completed = false;
  7314. role = ShmPsuGrouping->GroupCollection[connector].Role;
  7315. switch(role)
  7316. {
  7317. case _GROLE_MASTER:
  7318. completed = true;
  7319. break;
  7320. default:
  7321. break;
  7322. }
  7323. return completed;
  7324. }
  7325. void SelfTestFailReason(void)
  7326. {
  7327. char reason[50];
  7328. memset(reason, 0x00, sizeof(reason));
  7329. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  7330. {
  7331. StatusCodeCompose(reason, "042251");
  7332. }
  7333. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail ||
  7334. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail ||
  7335. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  7336. {
  7337. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail)
  7338. {
  7339. StatusCodeCompose(reason, "042280");
  7340. }
  7341. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  7342. {
  7343. StatusCodeCompose(reason, "042281");
  7344. }
  7345. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail)
  7346. {
  7347. StatusCodeCompose(reason, "042282");
  7348. }
  7349. }
  7350. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail ||
  7351. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  7352. {
  7353. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail)
  7354. {
  7355. StatusCodeCompose(reason, "042285");
  7356. }
  7357. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  7358. {
  7359. StatusCodeCompose(reason, "042286");
  7360. }
  7361. }
  7362. else
  7363. {
  7364. StatusCodeCompose(reason, "Self Test Fail");
  7365. }
  7366. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  7367. {
  7368. SetOcppErrorCode(gun_index, "InternalError");
  7369. SetOcppVendorErrorCode(gun_index, reason);
  7370. setChargerMode(gun_index, MODE_MAINTAIN);
  7371. }
  7372. }
  7373. bool IsAvailableBackToIdle(int gun_index)
  7374. {
  7375. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle ||
  7376. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false ||
  7377. (!chargingInfo[gun_index]->ConnectorPlugIn && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm) ||
  7378. isResetStopChargeFlag(gun_index) == YES)
  7379. {
  7380. return true;
  7381. }
  7382. return false;
  7383. }
  7384. void CheckGunAvailable(void)
  7385. {
  7386. if(!ShmChargerInfo->Control.SysCtrl.bits.PsuInit)
  7387. {
  7388. if(ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  7389. {
  7390. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  7391. {
  7392. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  7393. {
  7394. ShmChargerInfo->Control.GunAvailable[i] = YES;
  7395. }
  7396. }
  7397. ShmChargerInfo->Control.SysCtrl.bits.PsuInit = true;
  7398. }
  7399. }
  7400. }
  7401. int presentChargedEnergyClear(unsigned char gun_index)
  7402. {
  7403. int result = FAIL;
  7404. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  7405. memset(chargingInfo[gun_index]->presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod) * sizeof(float));
  7406. result = PASS;
  7407. return result;
  7408. }
  7409. float presentChargedEnergyTotal(unsigned char gun_index)
  7410. {
  7411. float result = 0.0f;
  7412. for(int idx = 0; idx < ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod); idx++)
  7413. {
  7414. result += chargingInfo[gun_index]->presentChargedEnergyPeriod[idx];
  7415. }
  7416. return result;
  7417. }
  7418. int presentChargedEnergyUpdate(unsigned char gun_index, float kwh)
  7419. {
  7420. int result = FAIL;
  7421. time_t CurrentTime;
  7422. struct tm *tm;
  7423. CurrentTime = time(NULL);
  7424. tm=localtime(&CurrentTime);
  7425. chargingInfo[gun_index]->presentChargedEnergyPeriod[tm->tm_hour] += kwh;
  7426. return result;
  7427. }
  7428. void ChkConnectorAction(void)
  7429. {
  7430. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7431. {
  7432. if(ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel)
  7433. {
  7434. Clean_Ocpp_TcciSerialNo(i);
  7435. LOG_INFO("Gun %d Charging Canceled");
  7436. ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel = false;
  7437. }
  7438. }
  7439. }
  7440. int main(void)
  7441. {
  7442. unsigned long time = 0;
  7443. int _dispenser = 0;
  7444. _isStandbyEnable = true;
  7445. if(CreateShareMemory() == 0)
  7446. {
  7447. #ifdef SystemLogMessage
  7448. LOG_ERROR("CreatShareMemory NG");
  7449. #endif
  7450. if(ShmStatusCodeData!=NULL)
  7451. {
  7452. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  7453. }
  7454. sleep(5);
  7455. system("reboot -f");
  7456. sleep(5);
  7457. system("reboot -f");
  7458. }
  7459. LOG_INFO("\n");
  7460. LOG_INFO("===== Boot and Initialization start =====");
  7461. LOG_INFO("Main Fork PID = %d", getpid());
  7462. if (!InitialSystemDefaultConfig())
  7463. {
  7464. LOG_INFO("InitialSystemDefaultConfig NG");
  7465. StopProcessingLoop();
  7466. }
  7467. LOG_INFO("ModelName: [%s], SN: [%s], SW Version: [%s][%s]",
  7468. ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber, fwVersion, subVersion);
  7469. LOG_INFO("InitialShareMemoryInfo");
  7470. InitialShareMemoryInfo();
  7471. LOG_INFO("ChargerType (IEC or UL) = %d", ShmSysConfigAndInfo->SysInfo.ChargerType);
  7472. if(!InitialChargerSetting())
  7473. {
  7474. isModelNameMatch = false;
  7475. }
  7476. Initialization();
  7477. LOG_INFO("Spawn all Task.");
  7478. SpawnTask();
  7479. if (!isModelNameMatch)
  7480. {
  7481. LOG_INFO("Module Name & HW info none match.");
  7482. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  7483. // Module Name 與硬體對應不正確
  7484. LOG_ERROR("Module Name & HW info none match.");
  7485. sleep(3);
  7486. KillAllTask();
  7487. StopProcessingLoop();
  7488. }
  7489. LOG_INFO("Module Name & HW info correct. Initialize.......");
  7490. CreateTimeoutFork();
  7491. SelfTestRun();
  7492. while(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  7493. {
  7494. sleep(1);
  7495. }
  7496. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  7497. {
  7498. sleep(1);
  7499. SelfTestFailReason();
  7500. }
  7501. else
  7502. {
  7503. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  7504. {
  7505. setChargerMode(gun_index, MODE_IDLE);
  7506. if(ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 ||
  7507. !ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  7508. {
  7509. ShmChargerInfo->Control.GunAvailable[gun_index] = YES;
  7510. }
  7511. }
  7512. }
  7513. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE)
  7514. {
  7515. SetAcContactor(ON);
  7516. SpawnOcppService();
  7517. SpawnMaintainOcppService();
  7518. }
  7519. else
  7520. {
  7521. InitSlaveCabinetEthernet();
  7522. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  7523. {
  7524. setChargerMode(gun_index, MODE_MAINTAIN);
  7525. }
  7526. }
  7527. // Local DB
  7528. if(DB_Open(localDb) != PASS)
  7529. {
  7530. LOG_INFO("DB_Open fail.");
  7531. isDb_ready = false;
  7532. }
  7533. else
  7534. {
  7535. isDb_ready = true;
  7536. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  7537. {
  7538. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  7539. LOG_INFO("Gun %d IsAvailable Initial: [%s]", _index + 1, chargingInfo[_index]->IsAvailable == YES ? "Enable" : "Disable");
  7540. }
  7541. for(int _index=0; _index< ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;_index++)
  7542. {
  7543. chargingInfo[_index]->PowerConsumption = DB_Get_PowerConsumption(localDb, _index);
  7544. LOG_INFO("Gun %d Consumption Initial: [%.1f kWh]", _index + 1, chargingInfo[_index]->PowerConsumption);
  7545. }
  7546. DB_Reboot_Record(localDb);
  7547. }
  7548. if (NetworkDB_Open(networkDb) != PASS)
  7549. {
  7550. LOG_ERROR("NetworkDB_Open fail.");
  7551. }
  7552. sleep(1);
  7553. CreateWatchdog();
  7554. // Main loop
  7555. LOG_INFO("%s \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_COMPLETE ?
  7556. "****************************Main Loop**********************************" :
  7557. "********************** SelfTest Fail **********************");
  7558. GetClockTime(&_cmdMainPriority_time);
  7559. for (;;)
  7560. {
  7561. CheckGunAvailable();
  7562. CheckOcppStatus();
  7563. ChkPrimaryStatus();
  7564. ChkPsuStatus();
  7565. PsuFailureResume();
  7566. ChkConnectorAction();
  7567. if (IsConnectorWholeIdle())
  7568. {
  7569. CheckFactoryConfigFunction();
  7570. CheckFwUpdateFunction();
  7571. }
  7572. if(CheckWiringInfoUpdate() != YES)
  7573. {
  7574. // return fork process
  7575. return 0;
  7576. }
  7577. CheckSoftHardReset();
  7578. StandbyCheck();
  7579. // 讀卡邏輯
  7580. //ScannerCardProcess();
  7581. AuthorizeTimeoutProcess();
  7582. // 當 AC 沒有搭上時,清除一些錯誤碼
  7583. ClearAlarmCodeWhenAcOff();
  7584. if ((GetTimeoutValue(_cmdMainPriority_time) / uSEC_VAL) > MAIN_PRIORITY_INTERVAL)
  7585. {
  7586. CheckTask();
  7587. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7588. {
  7589. if((chargingInfo[_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[_index]->SystemStatus <= S_CHARGING) ||
  7590. (chargingInfo[_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  7591. {
  7592. if (chargingInfo[_index]->SystemStatus == S_CHARGING && _ocppProfileChkFlag[_index] == 12)
  7593. {
  7594. Ocpp_ProfileReq_Cmd(_index);
  7595. gunOutputVol[_index] = chargingInfo[_index]->PresentChargingVoltage;
  7596. _ocppProfileChkFlag[_index] = 0;
  7597. }
  7598. else if (chargingInfo[_index]->SystemStatus != S_CHARGING)
  7599. {
  7600. Ocpp_ProfileReq_Cmd(_index);
  7601. _ocppProfileChkFlag[_index] = 0;
  7602. }
  7603. else
  7604. {
  7605. _ocppProfileChkFlag[_index]++;
  7606. }
  7607. }
  7608. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  7609. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  7610. {
  7611. DB_Update_PowerConsumption(localDb, _index, chargingInfo[_index]->PowerConsumption);
  7612. }
  7613. }
  7614. GetClockTime(&_cmdMainPriority_time);
  7615. }
  7616. CheckMiscCommandRequirement();
  7617. CheckDispenserVersionUpdateRequirement();
  7618. MCabinetParallelingProcess();
  7619. SCabinetControllProcess();
  7620. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  7621. {
  7622. //CheckGpioInStatus();
  7623. CheckErrorOccurStatus(gun_index);
  7624. ChkOcppStatus(gun_index);
  7625. if((chargingInfo[gun_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= S_CHARGING) ||
  7626. (chargingInfo[gun_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  7627. {
  7628. if(chargingInfo[gun_index]->SystemStatus == S_CHARGING)
  7629. {
  7630. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  7631. if (chargingInfo[gun_index]->PresentChargingVoltage >= gunOutputVol[gun_index] + 10 ||
  7632. chargingInfo[gun_index]->PresentChargingVoltage <= gunOutputVol[gun_index] - 10)
  7633. {
  7634. _ocppProfileChkFlag[gun_index] = 12;
  7635. gunOutputVol[gun_index] = chargingInfo[gun_index]->PresentChargingVoltage;
  7636. }
  7637. }
  7638. }
  7639. CheckSmartChargeProfile(gun_index);
  7640. //LOG_INFO("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  7641. switch(chargingInfo[gun_index]->SystemStatus)
  7642. {
  7643. case S_MAINTAIN:
  7644. if(isModeChange(gun_index))
  7645. {
  7646. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  7647. {
  7648. LOG_INFO("================== Slave Cabinet %d ==================", ShmChargerInfo->Control.CabinetSwitch - 1);
  7649. }
  7650. LOG_INFO("================== S_MAINTAIN (%x) ================== \n", gun_index + 1);
  7651. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  7652. GetClockTime(&_SystemStatus_Time[gun_index]);
  7653. }
  7654. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  7655. if(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE ||
  7656. ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  7657. ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  7658. {
  7659. break;
  7660. }
  7661. if(chargingInfo[gun_index]->IsAvailable == YES &&
  7662. ShmChargerInfo->Control.GunAvailable[gun_index] == YES &&
  7663. ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  7664. {
  7665. if(time >= MAINTAIN_RECOVERY_TIME)
  7666. {
  7667. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  7668. setChargerMode(gun_index, MODE_IDLE);
  7669. }
  7670. }
  7671. break;
  7672. case S_RESERVATION:
  7673. if(isModeChange(gun_index))
  7674. {
  7675. LOG_INFO("================== S_RESERVATION (%x) ================== \n", gun_index + 1);
  7676. Get_Ocpp_ReserveNowIdTag(gun_index, &ShmChargerInfo->Control.ResevedIdTag[gun_index][0]);
  7677. Set_Connector_MiscCommand(gun_index, MISC_CONN_RESERVATION);
  7678. LOG_INFO("****** Reservation IdTag: [%s] ******", &ShmChargerInfo->Control.ResevedIdTag[gun_index][0]);
  7679. GetClockTime(&_SystemStatus_Time[gun_index]);
  7680. }
  7681. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  7682. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  7683. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  7684. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  7685. {
  7686. UpdateErrorCodeToOcpp(gun_index);
  7687. setChargerMode(gun_index, MODE_FAULT);
  7688. continue;
  7689. }
  7690. if(Is_Ocpp_ReserveNowExpired(gun_index))
  7691. {
  7692. chargingInfo[gun_index]->ReservationId = -1;
  7693. LOG_INFO("Gun %d reservation time's up...", gun_index + 1);
  7694. setChargerMode(gun_index, MODE_IDLE);
  7695. }
  7696. else
  7697. {
  7698. if(IsConnectorAuthorizeSuccess(gun_index))
  7699. {
  7700. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  7701. {
  7702. LOG_INFO("Gun %d remote start reserve charging", gun_index + 1);
  7703. }
  7704. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  7705. {
  7706. LOG_INFO("Gun %d swipe rfid start reserve charging", gun_index + 1);
  7707. }
  7708. setChargerMode(gun_index, MODE_AUTHORIZING);
  7709. }
  7710. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  7711. {
  7712. SetAcContactor(ON);
  7713. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  7714. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  7715. {
  7716. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  7717. if(Is_Ocpp_ReserveNowIdTag(gun_index, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId))
  7718. {
  7719. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  7720. LOG_INFO("Gun %d reserve Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  7721. DispenserAuthInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  7722. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  7723. }
  7724. }
  7725. }
  7726. }
  7727. break;
  7728. case S_IDLE:
  7729. case S_FAULT:
  7730. {
  7731. // clean PermissionRequest flag
  7732. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  7733. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  7734. if(isModeChange(gun_index))
  7735. {
  7736. if (chargingInfo[gun_index]->SystemStatus == S_IDLE)
  7737. {
  7738. LOG_INFO("================== S_IDLE (%x) ================== \n", gun_index + 1);
  7739. chargingInfo[gun_index]->PresentChargedDuration = 0;
  7740. chargingInfo[gun_index]->RemainChargingDuration = 0;
  7741. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "00:00:00");
  7742. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "00:00:00");
  7743. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  7744. Clean_Ocpp_StopReason(gun_index);
  7745. ReleaseAlarmCode(gun_index);
  7746. // clean connector information
  7747. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  7748. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  7749. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  7750. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  7751. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  7752. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  7753. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -65535;
  7754. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  7755. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.GetStartChargingSoc = false;
  7756. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  7757. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  7758. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  7759. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.OutputLimitEnable = false;
  7760. chargingInfo[gun_index]->EvBatterytargetVoltage = 0;
  7761. chargingInfo[gun_index]->EvBatterytargetCurrent = 0;
  7762. chargingInfo[gun_index]->EvBatteryMaxVoltage = 0;
  7763. chargingInfo[gun_index]->EvBatterySoc = 0;
  7764. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetVoltage = 0;
  7765. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetCurrent = 0;
  7766. // clean RemoteStart & RemoteStop flag
  7767. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_START);
  7768. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_STOP);
  7769. Clean_Connector_MiscCommand(gun_index, MISC_CONN_UNLOCK);
  7770. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  7771. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopConfirm = false;
  7772. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopConfirm = false;
  7773. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NormalStopRequest = false;
  7774. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AlarmStopRequest = false;
  7775. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  7776. memset(chargingInfo[gun_index]->ConnectorAlarmCode, 0x00, sizeof(chargingInfo[gun_index]->ConnectorAlarmCode));
  7777. Clean_Ocpp_RunningCost(gun_index);
  7778. Clean_Ocpp_FinalCost(gun_index);
  7779. Clean_DeductInfo(gun_index);
  7780. Clean_Ocpp_TcciSerialNo(gun_index);
  7781. Clean_UserTransaction(gun_index);
  7782. presentChargedEnergyClear(gun_index);
  7783. chargingInfo[gun_index]->PresentChargingPower = 0;
  7784. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower = 0;
  7785. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent = 0;
  7786. chargingInfo[gun_index]->ChargingProfilePower = -1;
  7787. chargingInfo[gun_index]->ChargingProfileCurrent = -1;
  7788. // clean force charging info
  7789. memset(&ShmChargerInfo->Control.FCharging[gun_index], 0x00, sizeof(ForceCharging));
  7790. if(chargingInfo[gun_index]->ReservationId != -1)
  7791. {
  7792. setChargerMode(gun_index, MODE_RESERVATION);
  7793. continue;
  7794. }
  7795. }
  7796. else if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  7797. {
  7798. LOG_INFO("================== S_FAULT (%x) ================== \n", gun_index + 1);
  7799. }
  7800. GetClockTime(&_SystemStatus_Time[gun_index]);
  7801. }
  7802. if (chargingInfo[gun_index]->IsAvailable == NO ||
  7803. ShmChargerInfo->Control.GunAvailable[gun_index] == NO)
  7804. {
  7805. setChargerMode(gun_index, MODE_MAINTAIN);
  7806. continue;
  7807. }
  7808. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  7809. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  7810. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  7811. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  7812. {
  7813. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest)
  7814. {
  7815. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  7816. {
  7817. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemotenAlarmCode, 6);
  7818. }
  7819. }
  7820. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  7821. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  7822. {
  7823. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  7824. {
  7825. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042304", 6);
  7826. LOG_INFO("Connector %d Disconnect Occur", gun_index + 1);
  7827. }
  7828. }
  7829. if(chargingInfo[gun_index]->SystemStatus != S_FAULT)
  7830. {
  7831. UpdateErrorCodeToOcpp(gun_index);
  7832. setChargerMode(gun_index, MODE_FAULT);
  7833. }
  7834. else
  7835. {
  7836. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  7837. {
  7838. CheckConnectorDisconnectionRecovery(gun_index);
  7839. }
  7840. }
  7841. }
  7842. else
  7843. {
  7844. if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  7845. {
  7846. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  7847. if(time >= FAULT_RELEASE_TIME)
  7848. {
  7849. ReleaseAlarmCode(gun_index);
  7850. setChargerMode(gun_index, MODE_IDLE);
  7851. }
  7852. break;
  7853. }
  7854. // clean stop charge flag
  7855. chargingInfo[gun_index]->StopChargeFlag = false;
  7856. chargingInfo[gun_index]->ChargingStopFlag.ChargingStopValue = 0;
  7857. { // Idle 正常程序起點
  7858. // 判斷是否有啟用檢查插槍
  7859. if(IsConnectorAuthorizeSuccess(gun_index))
  7860. {
  7861. if(chargingInfo[gun_index]->IsAvailable &&
  7862. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  7863. {
  7864. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  7865. {
  7866. LOG_INFO("Gun %d remote start charging", gun_index + 1);
  7867. }
  7868. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  7869. {
  7870. LOG_INFO("Gun %d swipe rfid start charging", gun_index + 1);
  7871. }
  7872. setChargerMode(gun_index, MODE_AUTHORIZING);
  7873. continue;
  7874. }
  7875. }
  7876. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  7877. {
  7878. SetAcContactor(ON);
  7879. if(chargingInfo[gun_index]->IsAvailable &&
  7880. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  7881. chargingInfo[gun_index]->ConnectorPlugIn &&
  7882. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  7883. {
  7884. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  7885. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  7886. LOG_INFO("Gun %d Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  7887. DispenserAuthInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  7888. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  7889. continue;
  7890. }
  7891. }
  7892. // only for test & debug purpose
  7893. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging &&
  7894. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging)
  7895. {
  7896. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging = false;
  7897. strcpy((char *)chargingInfo[gun_index]->StartUserId, "ChargingTest");
  7898. LOG_INFO("Gun %d Start Force Charging", gun_index + 1);
  7899. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  7900. continue;
  7901. }
  7902. else
  7903. {
  7904. }
  7905. } // Idle 正常程序終點
  7906. }
  7907. }
  7908. break;
  7909. case S_AUTHORIZING:
  7910. if(isModeChange(gun_index))
  7911. {
  7912. LOG_INFO("================== S_AUTHORIZING (%x) ================== \n", gun_index + 1);
  7913. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  7914. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  7915. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  7916. //StopSystemTimeoutDet();
  7917. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  7918. SetAcContactor(ON);
  7919. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  7920. }
  7921. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  7922. {
  7923. FouceAnnounceAccountBalance(gun_index);
  7924. }
  7925. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm)
  7926. {
  7927. // sync with dispenser
  7928. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  7929. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  7930. LOG_INFO("Sync with Gun %d through remote start", gun_index + 1);
  7931. FouceAnnounceAccountBalance(gun_index);
  7932. }
  7933. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm)
  7934. {
  7935. // sync with dispenser
  7936. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm = false;
  7937. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  7938. LOG_INFO("Sync with Connector %d through swipe rfid", gun_index + 1);
  7939. }
  7940. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  7941. {
  7942. if(!chargingInfo[gun_index]->ConnectorPlugIn)
  7943. {
  7944. LOG_INFO("*********** Gun %d Plug In Timeout ***********", gun_index + 1);
  7945. }
  7946. else
  7947. {
  7948. LOG_INFO("*********** Gun %d Preparing State Timeout ***********", gun_index + 1);
  7949. }
  7950. setChargerMode(gun_index, MODE_IDLE);
  7951. }
  7952. else
  7953. {
  7954. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  7955. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  7956. {
  7957. AddPlugInTimes(gun_index);
  7958. LOG_INFO("Gun %d CardNumber = [%s]", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  7959. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  7960. GunAuthInitial(gun_index);
  7961. }
  7962. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  7963. {
  7964. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  7965. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  7966. LOG_INFO("*********** Gun %d User Stop Plug In ***********", gun_index + 1);
  7967. setChargerMode(gun_index, MODE_IDLE);
  7968. }
  7969. else if(isEvBoardStopChargeFlag(gun_index) == YES)
  7970. {
  7971. LOG_INFO("********** Gun %d Alarm Stop (S_AUTHORIZING) **********", gun_index + 1);
  7972. setChargerMode(gun_index, MODE_IDLE);
  7973. }
  7974. }
  7975. break;
  7976. case S_REASSIGN_CHECK:
  7977. {
  7978. if (isModeChange(gun_index))
  7979. {
  7980. LOG_INFO("================== S_REASSIGN_CHECK (%x) ================== \n", gun_index + 1);
  7981. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  7982. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  7983. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  7984. //StopSystemTimeoutDet();
  7985. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  7986. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy = ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy;
  7987. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration = ShmSysConfigAndInfo->SysConfig.MaxChargingDuration;
  7988. LOG_INFO("Max Total Current %d A, Max Total Power %d kW, Total Energy %d kWh, Total Duration %d",
  7989. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent,
  7990. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  7991. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy,
  7992. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration);
  7993. SetAcContactor(ON);
  7994. SetPCabinetOutputRelay(gun_index, ON);
  7995. GetClockTime(&_SystemStatus_Time[gun_index]);
  7996. }
  7997. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  7998. if(ConnectorRequestToCharging(gun_index))
  7999. {
  8000. setChargerMode(gun_index, MODE_REASSIGN);
  8001. }
  8002. else if(time >= WAIT_REASSIGN_TIME)
  8003. {
  8004. if(ShmPsuData->Work_Step != _WORK_CHARGING)
  8005. {
  8006. LOG_INFO("Gun %d Wait PSU Work Step Timeout", gun_index + 1);
  8007. }
  8008. else
  8009. {
  8010. LOG_INFO("Gun %d Request Charging Timeout", gun_index + 1);
  8011. }
  8012. setChargerMode(gun_index, MODE_IDLE);
  8013. }
  8014. }
  8015. break;
  8016. case S_REASSIGN:
  8017. {
  8018. if (isModeChange(gun_index))
  8019. {
  8020. LOG_INFO("================== S_REASSIGN (%x) ================== \n", gun_index + 1);
  8021. //gettimeofday(&_toAverage_time, NULL);
  8022. GetClockTime(&_SystemStatus_Time[gun_index]);
  8023. }
  8024. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  8025. if(IsConnectorGroupingCompleted(gun_index))
  8026. {
  8027. setChargerMode(gun_index, MODE_PRECHARGE);
  8028. }
  8029. else if(time >= MAX_REQUEST_CHARGING_TIME)
  8030. {
  8031. LOG_INFO("Gun %d Grouping Timeout", gun_index + 1);
  8032. setChargerMode(gun_index, MODE_IDLE);
  8033. }
  8034. }
  8035. break;
  8036. case S_PREPARNING:
  8037. {
  8038. if (isModeChange(gun_index))
  8039. {
  8040. LOG_INFO("================== S_PREPARNING (%x) ================== \n", gun_index + 1);
  8041. //StopGunInfoTimeoutDet(gun_index);
  8042. //StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  8043. SetAcContactor(ON);
  8044. GetClockTime(&_SystemStatus_Time[gun_index]);
  8045. }
  8046. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  8047. if ((chargingInfo[gun_index]->AvailableChargingPower > 10) && time >= MIN_WAIT_PREPARNING_TIME)
  8048. {
  8049. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  8050. break;;
  8051. }
  8052. if (isEvBoardStopChargeFlag(gun_index) == YES)
  8053. {
  8054. // 板端或後臺要求停止
  8055. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARNING) **********", gun_index + 1);
  8056. Set_Ocpp_StopReason(gun_index, "Local");
  8057. ChargingAlarmProcess(gun_index);
  8058. }
  8059. else if(isNormalStopChargeFlag(gun_index) == YES)
  8060. {
  8061. LOG_INFO("********** Gun %d Normal Stop (S_PREPARNING) %s**********", gun_index + 1,
  8062. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  8063. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  8064. ChargingTerminalProcess(gun_index);
  8065. }
  8066. else if(isResetStopChargeFlag(gun_index) == YES)
  8067. {
  8068. LOG_INFO("********** Gun %d Reset Stop (S_PREPARNING) %s**********", gun_index + 1,
  8069. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  8070. char *reason[16];
  8071. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  8072. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  8073. ChargingTerminalProcess(gun_index);
  8074. }
  8075. if(time > GUN_PREPARE_TIMEOUT)
  8076. {
  8077. LOG_INFO("********** Gun %d PrepareTimeout **********", gun_index + 1);
  8078. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  8079. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  8080. {
  8081. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "012279", 6);
  8082. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  8083. }
  8084. Set_Ocpp_StopReason(gun_index, "Local");
  8085. ChargingAlarmProcess(gun_index);
  8086. }
  8087. }
  8088. break;
  8089. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  8090. {
  8091. if (isModeChange(gun_index))
  8092. {
  8093. LOG_INFO("================== S_PREPARING_FOR_EV (%x) ================== \n", gun_index + 1);
  8094. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  8095. //StopGunInfoTimeoutDet(gun_index);
  8096. //StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  8097. GetClockTime(&_SystemStatus_Time[gun_index]);
  8098. }
  8099. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  8100. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  8101. {
  8102. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  8103. }
  8104. // only for test & debug purpose
  8105. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  8106. {
  8107. LOG_INFO("Gun %d [Force Charging] S_PREPARING_FOR_EV OK", gun_index + 1);
  8108. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = true;
  8109. }
  8110. if (isEvBoardStopChargeFlag(gun_index) == YES)
  8111. {
  8112. // 板端或後臺要求停止
  8113. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  8114. Set_Ocpp_StopReason(gun_index, "Local");
  8115. ChargingAlarmProcess(gun_index);
  8116. }
  8117. else if(isNormalStopChargeFlag(gun_index) == YES)
  8118. {
  8119. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  8120. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  8121. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  8122. ChargingTerminalProcess(gun_index);
  8123. }
  8124. else if(isResetStopChargeFlag(gun_index) == YES)
  8125. {
  8126. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  8127. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  8128. char *reason[16];
  8129. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  8130. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  8131. ChargingTerminalProcess(gun_index);
  8132. }
  8133. if(time >= GUN_EV_WAIT_TIMEOUT)
  8134. {
  8135. LOG_INFO("********** Gun %d Wait Dispenser Timeout **********", gun_index + 1);
  8136. ChargingTerminalProcess(gun_index);
  8137. }
  8138. }
  8139. break;
  8140. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  8141. {
  8142. if (isModeChange(gun_index))
  8143. {
  8144. LOG_INFO("================== S_PREPARING_FOR_EVSE (%x) ================== \n", gun_index + 1);
  8145. GetClockTime(&_SystemStatus_Time[gun_index]);
  8146. #ifdef STARTTRANSATION_IMMEDIATELY
  8147. OcppStartTransaction(gun_index);
  8148. #endif
  8149. }
  8150. CheckoutStartChargingSoc(gun_index, NO);
  8151. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  8152. // only for test & debug purpose
  8153. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  8154. {
  8155. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  8156. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  8157. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  8158. {
  8159. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  8160. }
  8161. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  8162. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  8163. }
  8164. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Charging)
  8165. {
  8166. setChargerMode(gun_index, MODE_CHARGING);
  8167. }
  8168. // only for test & debug purpose
  8169. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  8170. {
  8171. if(time >= FORCE_CHARGING_WAIT_EVSE)
  8172. {
  8173. LOG_INFO("Gun %d [Charging Simulation] S_PREPARING_FOR_EVSE OK", gun_index + 1);
  8174. setChargerMode(gun_index, MODE_CHARGING);
  8175. }
  8176. }
  8177. if (isEvBoardStopChargeFlag(gun_index) == YES)
  8178. {
  8179. // 板端或後臺要求停止
  8180. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  8181. Set_Ocpp_StopReason(gun_index, "Local");
  8182. ChargingAlarmProcess(gun_index);
  8183. }
  8184. else if(isNormalStopChargeFlag(gun_index) == YES)
  8185. {
  8186. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  8187. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  8188. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  8189. ChargingTerminalProcess(gun_index);
  8190. }
  8191. else if(isResetStopChargeFlag(gun_index) == YES)
  8192. {
  8193. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  8194. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  8195. char *reason[16];
  8196. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  8197. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  8198. ChargingTerminalProcess(gun_index);
  8199. }
  8200. else if(isInvalidIdStopFlag(gun_index) == YES)
  8201. {
  8202. LOG_INFO("********** Gun %d InvalidId Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  8203. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  8204. ChargingTerminalProcess(gun_index);
  8205. }
  8206. if(time >= GUN_EV_WAIT_TIMEOUT)
  8207. {
  8208. LOG_INFO("********** Gun %d GFD timeout **********", gun_index + 1);
  8209. ChargingTerminalProcess(gun_index);
  8210. }
  8211. }
  8212. break;
  8213. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  8214. {
  8215. if (isModeChange(gun_index))
  8216. {
  8217. LOG_INFO("================== S_CHARGING (%x) ================== \n", gun_index + 1);
  8218. //StopGunInfoTimeoutDet(gun_index);
  8219. GetClockTime(&_SystemStatus_Time[gun_index]);
  8220. CheckoutStartChargingSoc(gun_index, YES);
  8221. chargingInfo[gun_index]->PresentChargedDuration = 0;
  8222. ChangeStartOrStopDateTime(YES, gun_index);
  8223. #ifndef STARTTRANSATION_IMMEDIATELY
  8224. OcppStartTransaction(gun_index);
  8225. #endif
  8226. }
  8227. // only for test & debug purpose
  8228. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  8229. {
  8230. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  8231. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  8232. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  8233. {
  8234. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  8235. }
  8236. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  8237. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  8238. }
  8239. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  8240. // 計算 Power
  8241. //chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  8242. chargingInfo[gun_index]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent))) / 1000 / 100);
  8243. if(time >= (chargingInfo[gun_index]->PresentChargedDuration + ENERAGY_INTERVAL))
  8244. {
  8245. chargingInfo[gun_index]->PresentChargedDuration++;
  8246. float changingPow = (chargingInfo[gun_index]->PresentChargingPower) / 3600;
  8247. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  8248. {
  8249. chargingInfo[gun_index]->ChargingFee += changingPow * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  8250. }
  8251. presentChargedEnergyUpdate(gun_index, changingPow);
  8252. chargingInfo[gun_index]->PresentChargedEnergy += changingPow;
  8253. chargingInfo[gun_index]->PowerConsumption += changingPow;
  8254. }
  8255. if (isEvBoardStopChargeFlag(gun_index))
  8256. {
  8257. LOG_INFO("********** Gun %d Alarm Stop (S_CHARGING) **********", gun_index + 1);
  8258. Set_Ocpp_StopReason(gun_index, "Local");
  8259. ChargingAlarmProcess(gun_index);
  8260. }
  8261. else if(isNormalStopChargeFlag(gun_index) == YES)
  8262. {
  8263. LOG_INFO("********** Gun %d Normal Stop (S_CHARGING) %s**********", gun_index + 1,
  8264. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  8265. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  8266. ChargingTerminalProcess(gun_index);
  8267. }
  8268. else if(isResetStopChargeFlag(gun_index) == YES)
  8269. {
  8270. LOG_INFO("********** Gun %d Reset Stop (S_CHARGING) %s**********", gun_index + 1,
  8271. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  8272. char *reason[16];
  8273. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  8274. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  8275. ChargingTerminalProcess(gun_index);
  8276. }
  8277. else if(isInvalidIdStopFlag(gun_index) == YES)
  8278. {
  8279. LOG_INFO("********** Gun %d InvalidId Stop (S_CHARGING) **********", gun_index + 1);
  8280. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  8281. ChargingTerminalProcess(gun_index);
  8282. }
  8283. else if(CheckBackendChargingTimeout(gun_index) ||
  8284. CheckBackendChargingEnergy(gun_index))
  8285. {
  8286. LOG_INFO("********** Gun %d Backend Stop (S_CHARGING) **********", gun_index + 1);
  8287. // 板端或後臺要求停止
  8288. ChargingTerminalProcess(gun_index);
  8289. }
  8290. }
  8291. break;
  8292. case S_ALARM:
  8293. case S_TERMINATING:
  8294. {
  8295. if (isModeChange(gun_index))
  8296. {
  8297. Set_Ocpp_StopReason(gun_index, "Other");
  8298. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  8299. {
  8300. LOG_INFO("================== S_ALARM (%x) ================== \n", gun_index + 1);
  8301. UpdateErrorCodeToOcpp(gun_index);
  8302. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  8303. {
  8304. OcppStopTransaction(gun_index);
  8305. }
  8306. TheEndCharging(gun_index);
  8307. }
  8308. else
  8309. {
  8310. LOG_INFO("================== S_TERMINATING (%x) ================== \n", gun_index + 1);
  8311. }
  8312. //StopGunInfoTimeoutDet(gun_index);
  8313. GetClockTime(&_SystemStatus_Time[gun_index]);
  8314. }
  8315. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  8316. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  8317. {
  8318. if(time >= GUN_COMP_WAIT_TIMEOUT)
  8319. {
  8320. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  8321. {
  8322. LOG_INFO ("********** Gun %d psu is releasable **********\n", gun_index + 1);
  8323. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  8324. }
  8325. }
  8326. if(IsAvailableBackToIdle(gun_index) == YES &&
  8327. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  8328. {
  8329. setChargerMode(gun_index, MODE_IDLE);
  8330. }
  8331. }
  8332. else
  8333. {
  8334. setChargerMode(gun_index, MODE_COMPLETE);
  8335. }
  8336. }
  8337. break;
  8338. case S_COMPLETE:
  8339. {
  8340. if (isModeChange(gun_index))
  8341. {
  8342. LOG_INFO("================== S_COMPLETE (%x) ================== \n", gun_index + 1);
  8343. if (strcmp((char *)chargingInfo[gun_index]->StartDateTime, "") != EQUAL)
  8344. {
  8345. OcppStopTransaction(gun_index);
  8346. }
  8347. TheEndCharging(gun_index);
  8348. GetClockTime(&_SystemStatus_Time[gun_index]);
  8349. }
  8350. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  8351. if(time >= GUN_COMP_WAIT_TIMEOUT)
  8352. {
  8353. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  8354. {
  8355. LOG_INFO ("********** Gun %d psu is releasable **********", gun_index + 1);
  8356. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  8357. }
  8358. }
  8359. if(IsAvailableBackToIdle(gun_index) == YES &&
  8360. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  8361. {
  8362. setChargerMode(gun_index, MODE_IDLE);
  8363. }
  8364. }
  8365. break;
  8366. case S_CCS_PRECHARGE_ST0:
  8367. if (isModeChange(gun_index))
  8368. {
  8369. LOG_INFO("CCS Precharge Processing 1....................%x \n", gun_index);
  8370. //StopGunInfoTimeoutDet(gun_index);
  8371. //StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  8372. }
  8373. break;
  8374. case S_CCS_PRECHARGE_ST1:
  8375. if (isModeChange(gun_index))
  8376. {
  8377. LOG_INFO("CCS Precharge Processing 2....................%x \n", gun_index);
  8378. }
  8379. break;
  8380. }
  8381. }
  8382. TryFeedWatchdog();
  8383. usleep(whileLoopTime);
  8384. }
  8385. return FAIL;
  8386. }