main.c 334 KB

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