main.c 315 KB

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