main.c 333 KB

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