main.c 292 KB

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