main.c 441 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428
  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 40 // unit: second
  83. #define MAX_REQUEST_CHARGING_TIME 15 // unit: second
  84. #define MIN_WAIT_PREPARNING_TIME 1 // unit: second
  85. #define FORCE_CHARGING_CMD_DELAY 3 // unit: second
  86. #define FORCE_CHARGING_WAIT_EVSE 10 // unit: second
  87. #define TRY_RESET_4G_WIFI_TIME 300 // unit: second
  88. #define FAST_RESET_4G_WIFI_TIME 60 // unit: second
  89. #define ENERAGY_INTERVAL 1 // unit: second
  90. #define FAULT_RELEASE_TIME 2 // unit: second
  91. #define MAINTAIN_RECOVERY_TIME 1 // unit: second
  92. #define PARALLEL_WAIT_TIME 3 // unit: second
  93. #define START_UPGRADE_TIME 60 // unit: second
  94. #define PCABINET_UPGRADE_TIME 300 // unit: second
  95. #define MCABINET_UPGRADE_TIME 60 // unit: second
  96. #define CONNECTION_TIME_OFFSET 5 // unit: second
  97. #define PLUG_IN_HOLD_TIME 1 // unit: second
  98. #define FINAL_COST_TIMEOUT 3 // unit: second
  99. #define FINAL_COST_UPDATE_TIME 10 // unit: second
  100. #define MAX_FINAL_COST_MISC_COUNT 3
  101. #define PARKING_STATUS_TIMEOUT 3 // unit: second
  102. #define OCCUPANCY_FEE_TIMEOUT 10 // unit: second
  103. #define MIN_AVAILABLE_MEMORY 200 // unit: MB
  104. #define AUTO_START_CHARGING "AutoStartCharging"
  105. #define CONTACTOR_AUTO_ON_OFF 0
  106. #define CONTACTOR_AUTO_ON_TIME 60
  107. #define CONTACTOR_AUTO_OFF_TIME 300
  108. #define WAIT_DISPENSER_STOP_TIMEOUT 3 // unit: second
  109. #define STARTTRANSATION_TIME 1
  110. #define STOP_TARTTRANSATION_DELAY 2
  111. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  112. unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  113. int whileLoopTime = 10000; // 10 ms
  114. int wtdFd = -1;
  115. byte _authorizeIndex = NO_DEFINE;
  116. BOOL _UpgradeNeedReboot = FALSE;
  117. bool _isBridgeModeConfig = false;
  118. #define GUN_DERATING_COUNT 2
  119. const int GunDeratingTemp[GUN_DERATING_COUNT] = {80, 85};
  120. const byte Group_to_Location_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  121. const byte Location_to_Group_2_Gun[GENERAL_GUN_QUANTITY] = {0, 1, 0, 0};
  122. const byte GroupParallelRelay_2_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  123. {
  124. {0, 1, 0, 0},
  125. {1, 0, 0, 0},
  126. {0, 0, 0, 0},
  127. {0, 0, 0, 0}
  128. };
  129. //Location: 1 2 3 4
  130. // Gun: 1 3 4 2
  131. //Parallel: 1 2 3
  132. //const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 1, 2};
  133. //const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 2, 3, 1};
  134. //const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  135. //{
  136. // {0, 3, 1, 2}, // group 0
  137. // {1, 0, 2, 3}, // group 1
  138. // {1, 3, 0, 2}, // group 2
  139. // {1, 3, 2, 0} // group 3
  140. //};
  141. //Location: 1 2 3 4
  142. // Gun: 1 4 3 2
  143. //Parallel: 1 2 3
  144. const byte Group_to_Location_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  145. const byte Location_to_Group_4_Gun[GENERAL_GUN_QUANTITY] = {0, 3, 2, 1};
  146. const byte GroupParallelRelay_4_Gun[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  147. {
  148. {0, 3, 2, 1}, // group 0
  149. {1, 0, 3, 2}, // group 1
  150. {1, 3, 0, 2}, // group 2
  151. {1, 3, 2, 0} // group 3
  152. };
  153. const byte GroupParallelRelay_4_Gun_6Parallel[GENERAL_GUN_QUANTITY][GENERAL_GUN_QUANTITY] =
  154. {
  155. {0, 4, 6, 1}, // group 0
  156. {4, 0, 3, 5}, // group 1
  157. {6, 3, 0, 2}, // group 2
  158. {1, 5, 2, 0} // group 3
  159. };
  160. long long DiffTimebWithNow(struct timeb ST);
  161. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit);
  162. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value);
  163. void ChargingTerminalProcess(byte gunIndex);
  164. void ChkPrimaryStatus();
  165. void ChargingAlarmProcess(byte gunIndex);
  166. void InformOcppErrOccur(byte codeType);
  167. void SetOcppErrorCode(unsigned char _index, char *errCode);
  168. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode);
  169. void ReleaseAlarmCode(byte gunIndex);
  170. int DB_Open(sqlite3 *db);
  171. int DB_Insert_Record(sqlite3 *db, int gun_index);
  172. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable);
  173. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index);
  174. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value);
  175. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value);
  176. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index);
  177. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index);
  178. float Get_PowerConsumption(uint8_t gun_index);
  179. void Update_PowerConsumption(uint8_t gun_index);
  180. int DB_Insert_FinalCost(sqlite3 *db, uint8_t gun_index, int txId, char *description);
  181. int DB_Update_FinalCost_Status(sqlite3 *db, int txId, int status);
  182. int DB_Get_Undisposed_FinalCost(sqlite3 *db, int gunIndex, char *costString);
  183. int DB_Reboot_Record(sqlite3 *db);
  184. void InitialDHCP();
  185. void InitialDispenserDhcpServerConfig(void);
  186. void StartDispenserDhcpServer(void);
  187. int GetStartScheduleTime(unsigned char *time);
  188. void CheckConnectionTimeout(void);
  189. bool IsParallelCabinetIdle(void);
  190. bool IsStandbyEnable(void);
  191. bool IsGunIdle(int gunIndex, int idleType);
  192. bool IsDispenserIdle(int dispenserIndex);
  193. bool IsConnectorWholeIdle(void);
  194. void SetAcContactor(unsigned char OnOff);
  195. void UpdateErrorCodeToOcpp(byte index);
  196. bool CheckConnectorTypeStatus(void);
  197. void CreateWatchdog(void);
  198. void TryCloseWatchdog(void);
  199. void TryFeedWatchdog(void);
  200. bool IsReadyParalleling(void);
  201. void SetParallelingStart(void);
  202. void SetParallelingDone(void);
  203. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff);
  204. void SetParallelCabinetAcContactor(unsigned char OnOff);
  205. unsigned short GetTotalParallelCabinetPower(void);
  206. void MCabinetParallelingProcess(void);
  207. void SCabinetControllProcess(void);
  208. bool Is_WebServer_Alive(void);
  209. void Run_WebServer_Service(void);
  210. bool Get_Ocpp_ConnectionStatus(void);
  211. void Run_Ocpp_Service(void);
  212. bool Is_Ocpp_Task_Alive(void);
  213. void Kill_Ocpp_Service(void);
  214. void Run_MaintainOcpp_Service(void);
  215. bool Is_MaintainOcpp_Task_Alive(void);
  216. void Kill_MaintainOcpp_Service(void);
  217. long int Get_Ocpp_procDogTime(void);
  218. void Clean_Ocpp_procDogTime(void);
  219. bool Is_Ocpp_BootNotification(void);
  220. void Clean_Ocpp_BootNotification(void);
  221. bool Is_Ocpp_ResetReq(void);
  222. bool Chk_Ocpp_Reset_Type(char *type);
  223. void Clean_Ocpp_ResetReq(void);
  224. void Set_Ocpp_AuthorizeReq(void);
  225. bool Is_Ocpp_AuthorizeConf(void);
  226. bool Is_Ocpp_Authorize_Status(char *status);
  227. bool Is_Ocpp_UpdateFirmwareReq(void);
  228. void Clean_Ocpp_UpdateFirmwareReq(void);
  229. bool Is_Ocpp_UpdateFirmwareStatus(char *status);
  230. void Set_Ocpp_UpdateFirmwareStatus(char *status);
  231. void Clean_Ocpp_UpdateFirmwareStatus(char *status);
  232. void Set_Ocpp_FirmwareStatusNotificationReq(void);
  233. bool Is_Ocpp_StopOnInvalidId_Enable(void);
  234. bool Is_Ocpp_AuthorizeRemoteTxRequests(void);
  235. int Get_Ocpp_ConnectionTimeOut(void);
  236. void Get_Ocpp_DefaultPrice(char *defaultPrice);
  237. void Get_Ocpp_UserPrice(char * userId, char *userPrice);
  238. void Clean_Ocpp_UserPrice(void);
  239. void Get_Ocpp_RunningCost(int gun_index, char *cost);
  240. void Clean_Ocpp_RunningCost(int gun_index);
  241. void Get_Ocpp_FinalCost(int gun_index, char *cost);
  242. void Clean_Ocpp_FinalCost(int gun_index);
  243. int Get_Ocpp_FinalCostTxId(int gun_index);
  244. void Get_Ocpp_TimeOffset(char *timeOffset);
  245. void Get_Ocpp_PreAuthAmount(char *amount);
  246. void Get_Ocpp_PowerSharingServerIP(char *ip);
  247. void Get_Ocpp_Refunding(char *Refunding);
  248. void Clean_Ocpp_Refunding(void);
  249. bool Is_Ocpp_LcdOverideReq(int *gun, int *page);
  250. void Clean_Ocpp_LcdOverideReq(void);
  251. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name);
  252. bool Is_Ocpp_ChargerStationReq(void);
  253. void Clean_Ocpp_ChargerStationReq(void);
  254. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature);
  255. bool Is_Ocpp_WeatherReq(void);
  256. void Clean_Ocpp_WeatherReq(void);
  257. void Set_Ocpp_DeductTransaction(int transaction);
  258. void Set_Ocpp_DeductCreditNo(char *userId);
  259. void Set_Ocpp_DeductVemData(char *vemData);
  260. void Set_Ocpp_DeductAmount(double value);
  261. void Set_Ocpp_DeductResult(unsigned char result);
  262. void Set_Ocpp_DonateInvoice(unsigned char donate);
  263. bool Is_Ocpp_ReportCreditDeductReq(void);
  264. void Set_Ocpp_ReportCreditDeductReq(void);
  265. void Clean_Ocpp_TcciSerialNo(int gun_index);
  266. bool Set_Ocpp_TcciReaderStatus(int gun_index, DeductResultInfoData *ReaderStatus);
  267. void Clean_Ocpp_TcciCreditInfo(int gun_index);
  268. void Set_Ocpp_TcciReaderStatusReq(int gun_index);
  269. bool Is_Ocpp_TcciReaderStatusReq(int gun_index);
  270. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id);
  271. void Set_Ocpp_StartTransactionReq(int gun_index);
  272. bool Is_Ocpp_StartTransactionConf(int gun_index);
  273. void Clean_Ocpp_StartTransactionConf(int gun_index);
  274. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status);
  275. bool Is_Ocpp_StartTransaction_Accepted(int gun_index);
  276. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status);
  277. void Get_Ocpp_TransactionId(int gun_index, char *transactionId);
  278. bool Is_Invalid_StartTransactionId(int gun_index);
  279. void Set_Ocpp_StopTransactionId(int gun_index, char *id);
  280. void Set_Ocpp_StopTransactionReq(int gun_index);
  281. void Chk_Ocpp_Backup_ErrorCode(int gun_index);
  282. bool Is_Ocpp_ReserveNowReq(int gun_index);
  283. void Clean_Ocpp_ReserveNowReq(int gun_index);
  284. int Get_Ocpp_ReserveNowId(int gun_index);
  285. void Set_Ocpp_ReserveNowConf(int gun_index);
  286. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date);
  287. bool Is_Ocpp_ReserveNowExpired(int gun_index);
  288. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag);
  289. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag);
  290. bool Is_Ocpp_CancelReservationReq(int gun_index);
  291. void Clean_Ocpp_CancelReservationReq(int gun_index);
  292. void Set_Ocpp_CancelReservationConf(int gun_index);
  293. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index);
  294. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index);
  295. bool Chk_Ocpp_Availability_Type(int gun_index, char *type);
  296. bool Is_Ocpp_UnlockConnectorReq(int gun_index);
  297. void Clean_Ocpp_UnlockConnectorReq(int gun_index);
  298. void Set_Ocpp_UnlockConnectorConf(int gun_index);
  299. bool Is_Ocpp_RemoteStartReq(int gun_index);
  300. void Clean_Ocpp_RemoteStartReq(int gun_index);
  301. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag);
  302. bool Is_Ocpp_RemoteStopReq(int gun_index);
  303. void Clean_Ocpp_RemoteStopReq(int gun_index);
  304. void Set_Ocpp_StopReason(int gun_index, char *reason);
  305. void Get_Ocpp_StopReason(int gun_index, char *reason);
  306. void Clean_Ocpp_StopReason(int gun_index);
  307. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  308. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode);
  309. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  310. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode);
  311. bool Is_Ocpp_ChargingProfileConf(int gun_index);
  312. void Clean_Ocpp_ChargingProfileConf(int gun_index);
  313. bool Is_Ocpp_ChargingProfileReq(int gun_index);
  314. void Set_Ocpp_ChargingProfileReq(int gun_index);
  315. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind);
  316. bool Is_Ocpp_ChargingProfileId(int gun_index);
  317. int Get_Ocpp_ChargingProfileStartTime(int gun_index);
  318. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index);
  319. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index);
  320. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index);
  321. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index);
  322. int Get_Ocpp_ChargingProfileStartIndex(int gun_index);
  323. bool Is_Ocpp_MaxChargingProfileKind(char *kind);
  324. bool Is_Ocpp_MaxChargingProfileId(void);
  325. int Get_Ocpp_MaxProfileStartTime(void);
  326. int Get_Ocpp_MaxProfileScheduleCount(void);
  327. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index);
  328. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index);
  329. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index);
  330. int Get_Ocpp_MaxProfileStartIndex(void);
  331. int Is_Ocpp_RemoteStartWaitReq(void);
  332. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget);
  333. int Get_Ocpp_OffLineMaxChargingPower(void);
  334. void Set_Deduct_UserId(int gunIndex, char *userId);
  335. void Set_Deduct_TransactionId(int gunIndex, int transaction);
  336. void Clean_DeductInfo(int gunIndex);
  337. bool Is_DeductResultReq(int gunIndex);
  338. bool Is_ReaderStatusReq(int gunIndex);
  339. void Clean_ReaderStatusInfo(int gunIndex);
  340. void Set_UserTransactionId(int gunIndex, int transaction);
  341. void Set_UserPrice(int gunIndex, float price);
  342. void Set_UserTotalCost(int gunIndex, float cost);
  343. void Set_UserAccountBalance(int gunIndex, float balance);
  344. void Set_UserDiscount(int gunIndex, float discount);
  345. void Clean_UserTransaction(int gunIndex);
  346. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  347. struct StatusCodeData *ShmStatusCodeData;
  348. struct PsuData *ShmPsuData;
  349. struct CHAdeMOData *ShmCHAdeMOData;
  350. struct GBTData *ShmGBTData;
  351. struct CcsData *ShmCcsData;
  352. struct PrimaryMcuData *ShmPrimaryMcuData;
  353. struct FanModuleData *ShmFanModuleData;
  354. struct RelayModuleData *ShmRelayModuleData[2];
  355. struct LedModuleData *ShmLedModuleData;
  356. struct OCPP16Data *ShmOCPP16Data;
  357. struct OCPP20Data *ShmOCPP20Data;
  358. struct OCPP16Data *ShmOCPP16DataPH;
  359. struct MeterInformation *ShmCsuMeterData;
  360. ChargerInfoData *ShmChargerInfo;
  361. PsuPositionInfoData *ShmPsuPosition;
  362. PsuGroupingInfoData *ShmPsuGrouping;
  363. struct ChargingInfoData *chargingInfo[CONNECTOR_QUANTITY];
  364. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  365. struct timespec _cmdMainPriority_time;
  366. struct timespec _cmdSubPriority_time;
  367. struct timespec _ConnectorAuthorizing_Time[CONNECTOR_QUANTITY];
  368. struct timespec _ConnectorAuthorizeFail_Time[CONNECTOR_QUANTITY];
  369. struct timespec _DispenserAuthorizing_Time[CONNECTOR_QUANTITY];
  370. struct timespec _DispenserUpgrade_time;
  371. struct timespec _Standby_time;
  372. struct timespec _SystemStatus_Time[CONNECTOR_QUANTITY];
  373. struct timespec _ChargedEnergy_Time[CONNECTOR_QUANTITY];
  374. struct timespec _Parallel_Time;
  375. struct timespec _RemoteStartNoId_Time;
  376. float gunOutputVol[CONNECTOR_QUANTITY];
  377. unsigned char _ocppProfileChkFlag[CONNECTOR_QUANTITY];
  378. float _lastProfilePower[CONNECTOR_QUANTITY];
  379. float _lastProfileCurrent[CONNECTOR_QUANTITY];
  380. float _lastMaxProfilePower;
  381. float _lsatGunMeterValue[CONNECTOR_QUANTITY];
  382. struct StructStatusNotification _BackupStatusNotification[CONNECTOR_QUANTITY];
  383. bool _useGunMeterValue[MAX_GUN_QUANTITY];
  384. bool _triggerTransaction[MAX_GUN_QUANTITY];
  385. bool _plugInHold[MAX_GUN_QUANTITY];
  386. struct timespec _PlugInHold_Time[MAX_GUN_QUANTITY];
  387. struct timespec _DispenserBill_Time[MAX_DISPENSER_QUANTITY];
  388. struct timespec _ConnectorBill_Time[MAX_GUN_QUANTITY];
  389. struct timespec _ParkingStatus_Time[MAX_GUN_QUANTITY];
  390. struct timespec _GetOccupancyFee_Time[MAX_GUN_QUANTITY];
  391. bool _canUpdateFinalCost[MAX_DISPENSER_QUANTITY];
  392. bool _isUndisposedFinalCost[MAX_DISPENSER_QUANTITY];
  393. bool _isGetOccupancyFeeReq[MAX_GUN_QUANTITY];
  394. bool _NeedReset4gWifi;
  395. struct timespec _4gWifiReset_time;
  396. int _psuLostQuantity = 0;
  397. bool _isStandbyEnable = true;
  398. bool isModelNameMatch = true;
  399. //int rfidFd = -1;
  400. //char* rfidPortName = "/dev/ttyS2";
  401. #if ENABLE_PCBA_TEST == 0
  402. char* fwVersion = "V2.05.00.0000.00";
  403. char* subVersion = "00";
  404. #else
  405. char* fwVersion = "PCBA.00.04";
  406. char* subVersion = "00";
  407. #endif
  408. sqlite3 *localDb;
  409. bool isDb_ready;
  410. sqlite3 *networkDb;
  411. //================================================
  412. // initial can-bus
  413. //================================================
  414. int InitCanBus()
  415. {
  416. int s0,nbytes;
  417. struct timeval tv;
  418. struct ifreq ifr0;
  419. struct sockaddr_can addr0;
  420. system("/sbin/ip link set can0 down");
  421. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  422. system("/sbin/ip link set can0 up");
  423. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  424. tv.tv_sec = 0;
  425. tv.tv_usec = 10000;
  426. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  427. {
  428. #ifdef SystemLogMessage
  429. LOG_ERROR("Set SO_RCVTIMEO NG");
  430. #endif
  431. }
  432. nbytes=40960;
  433. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  434. {
  435. #ifdef SystemLogMessage
  436. LOG_ERROR("Set SO_RCVBUF NG");
  437. #endif
  438. }
  439. nbytes=40960;
  440. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  441. {
  442. #ifdef SystemLogMessage
  443. LOG_ERROR("Set SO_SNDBUF NG");
  444. #endif
  445. }
  446. strcpy(ifr0.ifr_name, "can0" );
  447. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  448. addr0.can_family = AF_CAN;
  449. addr0.can_ifindex = ifr0.ifr_ifindex;
  450. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  451. return s0;
  452. }
  453. //================================================
  454. // initial uart port
  455. //================================================
  456. char *_priPortName = "/dev/ttyS1";
  457. char *_485PortName = "/dev/ttyS5";
  458. int InitComPort(byte target)
  459. {
  460. int fd;
  461. struct termios tios;
  462. if(target == UPGRADE_PRI)
  463. fd = open(_priPortName, O_RDWR);
  464. else if (target == UPGRADE_FAN || target == UPGRADE_RB || target == UPGRADE_AC || target == UPGRADE_LED)
  465. fd = open(_485PortName, O_RDWR);
  466. if(fd<=0)
  467. {
  468. #ifdef SystemLogMessage
  469. LOG_ERROR("open 407 Communication port NG");
  470. #endif
  471. return -1;
  472. }
  473. ioctl (fd, TCGETS, &tios);
  474. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  475. tios.c_lflag = 0;
  476. tios.c_iflag = 0;
  477. tios.c_oflag = 0;
  478. tios.c_cc[VMIN]=0;
  479. tios.c_cc[VTIME]=(unsigned char)1;
  480. tios.c_lflag=0;
  481. tcflush(fd, TCIFLUSH);
  482. ioctl (fd, TCSETS, &tios);
  483. return fd;
  484. }
  485. //=================================
  486. // Common routine
  487. //=================================
  488. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  489. {
  490. strncpy(dest, src + start, cnt);
  491. dest[cnt] = 0;
  492. }
  493. int InitWatchDog()
  494. {
  495. int fd;
  496. int timeout = 180;
  497. system("/usr/bin/fuser -k /dev/watchdog");
  498. sleep(1);
  499. system("echo V > /dev/watchdog");
  500. sleep(1);
  501. fd=open("/dev/watchdog", O_RDWR);
  502. if(fd<=0)
  503. {
  504. LOG_ERROR("System watch dog initial fail.\r\n");
  505. }
  506. ioctl(fd, _IOWR('W', 6, int), &timeout);
  507. return fd;
  508. }
  509. void setChargerMode(byte gun_index, byte mode)
  510. {
  511. chargingInfo[gun_index]->SystemStatus = mode;
  512. }
  513. long long DiffTimebWithNow(struct timeb ST)
  514. {
  515. //return milli-second
  516. struct timeb ET;
  517. long long StartTime,StopTime;
  518. ftime(&ET);
  519. StartTime=(long long)ST.time;
  520. StopTime=(long long)ET.time;
  521. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  522. }
  523. //=================================
  524. // Create all share memory
  525. //=================================
  526. int CreateShareMemory()
  527. {
  528. int MeterSMId;
  529. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  530. {
  531. #ifdef SystemLogMessage
  532. LOG_ERROR("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG");
  533. #endif
  534. return 0;
  535. }
  536. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  537. {
  538. #ifdef SystemLogMessage
  539. LOG_ERROR("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG");
  540. #endif
  541. return 0;
  542. }
  543. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  544. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  545. {
  546. #ifdef SystemLogMessage
  547. LOG_ERROR("[main]CreatShareMemory:shmget ShmStatusCodeData NG");
  548. #endif
  549. return 0;
  550. }
  551. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  552. {
  553. #ifdef SystemLogMessage
  554. LOG_ERROR("[main]CreatShareMemory:shmat ShmStatusCodeData NG");
  555. #endif
  556. return 0;
  557. }
  558. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  559. //creat ShmPsuData
  560. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  561. {
  562. #ifdef SystemLogMessage
  563. LOG_ERROR("[main]CreatShareMemory:shmget ShmPsuData NG");
  564. #endif
  565. return 0;
  566. }
  567. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  568. {
  569. #ifdef SystemLogMessage
  570. LOG_ERROR("[main]CreatShareMemory:shmat ShmPsuData NG");
  571. #endif
  572. return 0;
  573. }
  574. memset(ShmPsuData, 0, sizeof(struct PsuData));
  575. /*
  576. if(CHAdeMO_QUANTITY > 0)
  577. {
  578. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  579. {
  580. #ifdef SystemLogMessage
  581. LOG_ERROR("[main]CreatShareMemory:shmget ShmCHAdeMOData NG");
  582. #endif
  583. return 0;
  584. }
  585. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  586. {
  587. #ifdef SystemLogMessage
  588. LOG_ERROR("[main]CreatShareMemory:shmat ShmCHAdeMOData NG");
  589. #endif
  590. return 0;
  591. }
  592. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  593. }
  594. if(GB_QUANTITY > 0)
  595. {
  596. if ((MeterSMId = shmget(ShmGBTCommKey, sizeof(struct GBTData), IPC_CREAT | 0777)) < 0)
  597. {
  598. #ifdef SystemLogMessage
  599. LOG_ERROR("[main]CreatShareMemory:shmget ShmGBTData NG");
  600. #endif
  601. return 0;
  602. }
  603. else if ((ShmGBTData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  604. {
  605. #ifdef SystemLogMessage
  606. LOG_ERROR("[main]CreatShareMemory:shmat ShmGBTData NG");
  607. #endif
  608. return 0;
  609. }
  610. memset(ShmGBTData, 0, sizeof(struct GBTData));
  611. }
  612. //creat ShmCcsData
  613. if(CCS_QUANTITY > 0)
  614. {
  615. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  616. {
  617. #ifdef SystemLogMessage
  618. LOG_ERROR("[main]CreatShareMemory:shmget ShmCcsData NG");
  619. #endif
  620. return 0;
  621. }
  622. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  623. {
  624. #ifdef SystemLogMessage
  625. LOG_ERROR("[main]CreatShareMemory:shmat ShmCcsData NG");
  626. #endif
  627. return 0;
  628. }
  629. memset(ShmCcsData, 0, sizeof(struct CcsData));
  630. }
  631. */
  632. //creat ShmPrimaryMcuData
  633. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  634. {
  635. #ifdef SystemLogMessage
  636. LOG_ERROR("[main]CreatShareMemory:shmget ShmPrimaryMcuData NGn");
  637. #endif
  638. return 0;
  639. }
  640. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  641. {
  642. #ifdef SystemLogMessage
  643. LOG_ERROR("[main]CreatShareMemory:shmat ShmPrimaryMcuData NG");
  644. #endif
  645. return 0;
  646. }
  647. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  648. //creat ShmFanModuleData
  649. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  650. {
  651. #ifdef SystemLogMessage
  652. LOG_ERROR("[main]CreatShareMemory:shmget ShmFanModuleData NG");
  653. #endif
  654. return 0;
  655. }
  656. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  657. {
  658. #ifdef SystemLogMessage
  659. LOG_ERROR("[main]CreatShareMemory:shmat ShmFanModuleData NG");
  660. #endif
  661. return 0;
  662. }
  663. memset(ShmFanModuleData, 0, sizeof(struct FanModuleData));
  664. //creat ShmRelayModuleData
  665. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  666. {
  667. #ifdef SystemLogMessage
  668. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[0] NG");
  669. #endif
  670. return 0;
  671. }
  672. else if ((ShmRelayModuleData[0] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  673. {
  674. #ifdef SystemLogMessage
  675. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[0] NG");
  676. #endif
  677. return 0;
  678. }
  679. memset(ShmRelayModuleData[0], 0, sizeof(struct RelayModuleData));
  680. // DO360 RC2
  681. if ((MeterSMId = shmget(ShmRelay2BdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  682. {
  683. #ifdef SystemLogMessage
  684. LOG_ERROR("[main]CreatShareMemory:shmget ShmRelayModuleData[1] NG");
  685. #endif
  686. return 0;
  687. }
  688. else if ((ShmRelayModuleData[1] = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  689. {
  690. #ifdef SystemLogMessage
  691. LOG_ERROR("[main]CreatShareMemory:shmat ShmRelayModuleData[1] NG");
  692. #endif
  693. return 0;
  694. }
  695. memset(ShmRelayModuleData[1], 0, sizeof(struct RelayModuleData));
  696. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), IPC_CREAT | 0777)) < 0)
  697. {
  698. #ifdef SystemLogMessage
  699. LOG_ERROR("[main]CreatShareMemory:shmget ShmLedModuleData NG");
  700. #endif
  701. return 0;
  702. }
  703. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  704. {
  705. #ifdef SystemLogMessage
  706. LOG_ERROR("[main]CreatShareMemory:shmat ShmLedModuleData NG");
  707. #endif
  708. return 0;
  709. }
  710. memset(ShmLedModuleData, 0, sizeof(struct LedModuleData));
  711. //creat ShmOCPP16Data
  712. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  713. {
  714. #ifdef SystemLogMessage
  715. LOG_ERROR("[main]CreatShareMemory:shmget ShmOCPP16Data NG");
  716. #endif
  717. return 0;
  718. }
  719. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  720. {
  721. #ifdef SystemLogMessage
  722. LOG_ERROR("[main]CreatShareMemory:shmat ShmOCPP16Data NG");
  723. #endif
  724. return 0;
  725. }
  726. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  727. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  728. {
  729. LOG_ERROR("[main]CreatShareMemory:shmget OCPP20Data NG");
  730. return 0;
  731. }
  732. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  733. {
  734. LOG_ERROR("[main]CreatShareMemory:shmat OCPP20Data NG");
  735. return 0;
  736. }
  737. memset(ShmOCPP20Data,0,sizeof(struct OCPP20Data));
  738. //creat ShmOCPP16DataPH
  739. if ((MeterSMId = shmget(ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  740. {
  741. LOG_ERROR("shmget ShmOCPP16DataPH NG");
  742. return 0;
  743. }
  744. else if ((ShmOCPP16DataPH = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  745. {
  746. LOG_ERROR("shmat ShmOCPP16DataPH NG");
  747. return 0;
  748. }
  749. memset(ShmOCPP16DataPH,0,sizeof(struct OCPP16Data));
  750. if ((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), IPC_CREAT | 0777)) < 0)
  751. {
  752. #ifdef SystemLogMessage
  753. LOG_ERROR("[main]CreatShareMemory:shmget ChargerInfoData NG");
  754. #endif
  755. return 0;
  756. }
  757. else if ((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  758. {
  759. #ifdef SystemLogMessage
  760. LOG_ERROR("[main]CreatShareMemory:shmat ChargerInfoData NG");
  761. #endif
  762. return 0;
  763. }
  764. memset(ShmChargerInfo, 0, sizeof(ChargerInfoData));
  765. ShmPsuPosition = &ShmChargerInfo->PsuPosition;
  766. ShmPsuGrouping = &ShmChargerInfo->PsuGrouping;
  767. return 1;
  768. }
  769. //======================================================
  770. // Peripheral initial
  771. //======================================================
  772. void InitGPIO()
  773. {
  774. /*****************0~3, 4 bank, bank x 32+ num*********************/
  775. /***************************************************************/
  776. /*************** GPIO 0 ***************************************/
  777. /***************************************************************/
  778. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/ // no use
  779. system("echo 22 > /sys/class/gpio/export");
  780. system("echo \"in\" > /sys/class/gpio/gpio22/direction");
  781. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/ // no use
  782. system("echo 23 > /sys/class/gpio/export");
  783. system("echo \"in\" > /sys/class/gpio/gpio23/direction");
  784. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/ // no use
  785. system("echo 26 > /sys/class/gpio/export");
  786. system("echo \"out\" > /sys/class/gpio/gpio26/direction");
  787. system("echo 1 > /sys/class/gpio/gpio26/value");
  788. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/ // no use
  789. system("echo 27 > /sys/class/gpio/export");
  790. system("echo \"in\" > /sys/class/gpio/gpio27/direction");
  791. /* RMII1_REF_CLK => GPIO0_29 *//*USB 0 OCP detection*/
  792. system("echo 29 > /sys/class/gpio/export");
  793. system("echo \"in\" > /sys/class/gpio/gpio29/direction");
  794. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/ // no use
  795. system("echo 19 > /sys/class/gpio/export");
  796. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  797. system("echo 1 > /sys/class/gpio/gpio19/value");
  798. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/ // no use
  799. system("echo 20 > /sys/class/gpio/export");
  800. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  801. /***************************************************************/
  802. /*************** GPIO 1 ***************************************/
  803. /***************************************************************/
  804. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/ // no use
  805. system("echo 44 > /sys/class/gpio/export");
  806. system("echo \"in\" > /sys/class/gpio/gpio44/direction");
  807. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/ // no use
  808. system("echo 45 > /sys/class/gpio/export");
  809. system("echo \"in\" > /sys/class/gpio/gpio45/direction");
  810. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/ // no use
  811. system("echo 46 > /sys/class/gpio/export");
  812. system("echo \"out\" > /sys/class/gpio/gpio46/direction");
  813. system("echo 0 > /sys/class/gpio/gpio46/value");
  814. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/ // no use
  815. system("echo 47 > /sys/class/gpio/export");
  816. system("echo \"in\" > /sys/class/gpio/gpio47/direction");
  817. /***************************************************************/
  818. /*************** GPIO 2 ***************************************/
  819. /***************************************************************/
  820. /*LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control*/
  821. system("echo 89 > /sys/class/gpio/export");
  822. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  823. system("echo 1 > /sys/class/gpio/gpio89/value");
  824. /*LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control*/
  825. system("echo 87 > /sys/class/gpio/export");
  826. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  827. system("echo 0 > /sys/class/gpio/gpio87/value");
  828. /*LCD_PCLK => GPIO2_24*//*CCS communication board 1 proximity*/ // no use
  829. system("echo 88 > /sys/class/gpio/export");
  830. system("echo \"in\" > /sys/class/gpio/gpio88/direction");
  831. /*LCD_VSYNC => GPIO2_22*//*CCS communication board 2 proximity*/ // no use
  832. system("echo 86 > /sys/class/gpio/export");
  833. system("echo \"in\" > /sys/class/gpio/gpio86/direction");
  834. /***************************************************************/
  835. /*************** GPIO 3 ***************************************/
  836. /***************************************************************/
  837. /*MCASP0_FSX => GPIO3_15*//*Emergency Stop button detect*/
  838. system("echo 111 > /sys/class/gpio/export");
  839. system("echo \"in\" > /sys/class/gpio/gpio111/direction");
  840. /*MCASP0_ACLKR => GPIO3_18*//*USB1 OCP detect*/
  841. system("echo 114 > /sys/class/gpio/export");
  842. system("echo \"in\" > /sys/class/gpio/gpio114/direction");
  843. /*MCASP0_AHCLKR => GPIO3_17*//*Emergency IO for AM3352 and STM32F407*/
  844. system("echo 113 > /sys/class/gpio/export");
  845. system("echo \"in\" > /sys/class/gpio/gpio113/direction");
  846. /*MCASP0_ACLKX => GPIO3_14*//*Ethernet PHY reset*/
  847. system("echo 110 > /sys/class/gpio/export");
  848. system("echo \"out\" > /sys/class/gpio/gpio110/direction");
  849. system("echo 0 > /sys/class/gpio/gpio110/value");
  850. /* MCASP0_FSR => GPIO3_19 *//*SMR Enable control_1 for Pskill_1*/ // no use
  851. system("echo 115 > /sys/class/gpio/export");
  852. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  853. system("echo 0 > /sys/class/gpio/gpio115/value");
  854. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  855. system("echo 112 > /sys/class/gpio/export");
  856. system("echo \"out\" > /sys/class/gpio/gpio112/direction");
  857. system("echo 1 > /sys/class/gpio/gpio112/value");
  858. /* MCASP0_AXR1 => GPIO3_20 *//*SMR Enable control_2 for Pskill_2*/ // no use
  859. system("echo 116 > /sys/class/gpio/export");
  860. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  861. system("echo 0 > /sys/class/gpio/gpio116/value");
  862. /* (C14) EMU0.gpio3[7] */ /*CP open/short feature enable/disable, pull low for default enable*/
  863. system("echo 103 > /sys/class/gpio/export");
  864. system("echo \"out\" > /sys/class/gpio/gpio103/direction");
  865. system("echo 0 > /sys/class/gpio/gpio103/value");
  866. /* (B14) EMU1.gpio3[8] */ /*4G module reset, pull high to reset when entry kernel, after Application start, it should be pull low.*/
  867. system("echo 104 > /sys/class/gpio/export");
  868. system("echo \"out\" > /sys/class/gpio/gpio104/direction");
  869. system("echo 0 > /sys/class/gpio/gpio104/value");
  870. LOG_INFO("[main]InitGPIO: Initial GPIO OK");
  871. }
  872. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  873. {
  874. int fd, wrd;
  875. unsigned char *buf;
  876. unsigned int ChkSum, ChkSumOrg;
  877. if((buf = malloc(MtdBlockSize)) == NULL)
  878. {
  879. LOG_ERROR("malloc buffer NG, rebooting..\n");
  880. if(ShmStatusCodeData!=NULL)
  881. {
  882. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  883. }
  884. sleep(5);
  885. system("reboot -f");
  886. sleep(5);
  887. system("reboot -f");
  888. }
  889. memset(buf, 0, MtdBlockSize);
  890. //================================================
  891. // Load configuration from mtdblock10
  892. //================================================
  893. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  894. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  895. if(fd < 0)
  896. {
  897. free(buf);
  898. LOG_ERROR("open mtdblock10 NG, rebooting..\n");
  899. if(ShmStatusCodeData != NULL)
  900. {
  901. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  902. }
  903. sleep(5);
  904. system("reboot -f");
  905. sleep(5);
  906. system("reboot -f");
  907. }
  908. wrd = read(fd, buf, MtdBlockSize);
  909. close(fd);
  910. if(wrd < MtdBlockSize)
  911. {
  912. free(buf);
  913. LOG_ERROR("read SysConfigData data NG, rebooting..\n");
  914. if(ShmStatusCodeData != NULL)
  915. {
  916. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  917. }
  918. sleep(5);
  919. system("reboot -f");
  920. sleep(5);
  921. system("reboot -f");
  922. }
  923. ChkSum = 0;
  924. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  925. {
  926. ChkSum += buf[wrd];
  927. }
  928. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  929. memcpy(&ptr->ModelName, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), ARRAY_SIZE(ptr->ModelName));
  930. memcpy(&ptr->SerialNumber, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), ARRAY_SIZE(ptr->SerialNumber));
  931. //================================================
  932. // Load configuration from mtdblock11
  933. //================================================
  934. if(ChkSum != ChkSumOrg)
  935. {
  936. LOG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  937. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  938. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  939. if(fd < 0)
  940. {
  941. free(buf);
  942. LOG_ERROR("open mtdblock11 (backup) NG, rebooting..\n");
  943. if(ShmStatusCodeData != NULL)
  944. {
  945. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  946. }
  947. sleep(5);
  948. system("reboot -f");
  949. sleep(5);
  950. system("reboot -f");
  951. }
  952. memset(buf, 0, MtdBlockSize);
  953. wrd = read(fd, buf, MtdBlockSize);
  954. close(fd);
  955. if(wrd < MtdBlockSize)
  956. {
  957. free(buf);
  958. LOG_ERROR("read backup SysConfigData data NG, rebooting..\n");
  959. if(ShmStatusCodeData != NULL)
  960. {
  961. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  962. }
  963. sleep(5);
  964. system("reboot -f");
  965. sleep(5);
  966. system("reboot -f");
  967. }
  968. ChkSum = 0;
  969. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  970. {
  971. ChkSum += buf[wrd];
  972. }
  973. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  974. //================================================
  975. // Load configuration from mtdblock12 (Factory default)
  976. //================================================
  977. if(ChkSum != ChkSumOrg)
  978. {
  979. LOG_ERROR("backup SysConfigData checksum NG, read Factory default\n");
  980. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  981. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  982. if(fd < 0)
  983. {
  984. free(buf);
  985. LOG_ERROR("open mtdblock12 (Factory default) NG, rebooting..\n");
  986. if(ShmStatusCodeData != NULL)
  987. {
  988. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  989. }
  990. sleep(5);
  991. system("reboot -f");
  992. sleep(5);
  993. system("reboot -f");
  994. }
  995. memset(buf, 0, MtdBlockSize);
  996. wrd = read(fd, buf, MtdBlockSize);
  997. close(fd);
  998. if(wrd < MtdBlockSize)
  999. {
  1000. free(buf);
  1001. LOG_ERROR("read factory default SysConfigData data NG, rebooting..\n");
  1002. if(ShmStatusCodeData != NULL)
  1003. {
  1004. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1005. }
  1006. sleep(5);
  1007. system("reboot -f");
  1008. sleep(5);
  1009. system("reboot -f");
  1010. }
  1011. ChkSum = 0;
  1012. for(wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  1013. {
  1014. ChkSum += buf[wrd];
  1015. }
  1016. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  1017. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  1018. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  1019. if(ChkSum != ChkSumOrg)
  1020. {
  1021. LOG_ERROR("factory default SysConfigData checksum NG, restore factory default\n");
  1022. free(buf);
  1023. system("cd /root;./FactoryConfig -m");
  1024. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1025. system("sync");
  1026. sleep(5);
  1027. system("reboot -f");
  1028. sleep(5);
  1029. system("reboot -f");
  1030. return FAIL;
  1031. }
  1032. }
  1033. }
  1034. //load OK
  1035. memcpy((struct SysConfigData *)ptr, buf, sizeof(struct SysConfigData));
  1036. free(buf);
  1037. system("rm -f /mnt/EvseConfig.bin");
  1038. // SysConfig in flash is empty (0xffffffff)
  1039. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  1040. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  1041. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  1042. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  1043. {
  1044. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  1045. {
  1046. LOG_INFO("Model name over length.");
  1047. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  1048. }
  1049. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  1050. {
  1051. LOG_INFO("Model serial number over length.");
  1052. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  1053. }
  1054. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  1055. {
  1056. LOG_INFO("System id over length.");
  1057. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  1058. }
  1059. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  1060. {
  1061. LOG_INFO("Ethernet dhcp config is null.");
  1062. }
  1063. system("cd /root;./Module_FactoryConfig -m");
  1064. sleep(3);
  1065. system("/usr/bin/run_evse_restart.sh");
  1066. }
  1067. LOG_INFO("Load SysConfigData OK");
  1068. return PASS;
  1069. }
  1070. int isEthernetCablePlugIn(int ethernet)
  1071. {
  1072. int fd, PlugInStatus = 0;
  1073. char ch[2];
  1074. char buffer[64];
  1075. snprintf(buffer, sizeof(buffer), "/sys/class/net/eth%d/carrier", ethernet);
  1076. fd = open(buffer, O_RDONLY);
  1077. if(fd < 0)
  1078. {
  1079. return -1;
  1080. }
  1081. if(read(fd, ch, 1) < 0)
  1082. {
  1083. return -1;
  1084. }
  1085. PlugInStatus = atoi(ch);
  1086. close(fd);
  1087. return PlugInStatus;
  1088. }
  1089. int isRouteFail()
  1090. {
  1091. int result = YES;
  1092. FILE *fp;
  1093. char buf[512];
  1094. fp = popen("route -n", "r");
  1095. if(fp != NULL)
  1096. {
  1097. while(fgets(buf, sizeof(buf), fp) != NULL)
  1098. {
  1099. if(strstr(buf, "eth0") != NULL)
  1100. result = NO;
  1101. }
  1102. }
  1103. pclose(fp);
  1104. return result;
  1105. }
  1106. int isReachableInternet()
  1107. {
  1108. int result = FAIL;
  1109. FILE *fp;
  1110. char cmd[256];
  1111. char buf[512];
  1112. char tmp[512];
  1113. // if (ShmOCPP16Data->OcppConnStatus == PASS)
  1114. // {
  1115. // result = PASS;
  1116. // }
  1117. // else
  1118. {
  1119. strcpy(cmd, "ifconfig eth0");
  1120. fp = popen(cmd, "r");
  1121. if (fp != NULL)
  1122. {
  1123. while(fgets(buf, sizeof(buf), fp) != NULL)
  1124. {
  1125. if (strstr(buf, "inet addr:") > 0)
  1126. {
  1127. sscanf(buf, "%*s%s", tmp);
  1128. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(tmp)-strspn(tmp, "addr:"));
  1129. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != EQUAL)
  1130. {
  1131. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  1132. }
  1133. }
  1134. }
  1135. }
  1136. pclose(fp);
  1137. memset(buf, 0x00, sizeof(buf));
  1138. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1139. {
  1140. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1141. fp = popen(cmd, "r");
  1142. if(fp != NULL)
  1143. {
  1144. while(fgets(buf, sizeof(buf), fp) != NULL)
  1145. {
  1146. if(strstr(buf, "transmitted") > 0)
  1147. {
  1148. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1149. if(strstr(buf,"100%") != NULL)
  1150. {
  1151. }
  1152. else
  1153. {
  1154. result = PASS;
  1155. }
  1156. //DEBUG_INFO("%s",buf);
  1157. //DEBUG_INFO("%s\n",tmp);
  1158. }
  1159. }
  1160. }
  1161. pclose(fp);
  1162. }
  1163. }
  1164. return result;
  1165. }
  1166. int isUap0up(void)
  1167. {
  1168. int result = FAIL;
  1169. FILE *fp;
  1170. char cmd[256];
  1171. char buf[512];
  1172. strcpy(cmd, "ifconfig uap0");;
  1173. fp = popen(cmd, "r");
  1174. if(fp != NULL)
  1175. {
  1176. while(fgets(buf, sizeof(buf), fp) != NULL)
  1177. {
  1178. if(strstr(buf, "uap0") > 0)
  1179. {
  1180. result = PASS;
  1181. }
  1182. }
  1183. }
  1184. pclose(fp);
  1185. return result;
  1186. }
  1187. void InitialApModeDhcpServerConfig(void)
  1188. {
  1189. system("echo 'start 192.168.10.100' > /etc/udhcpd.conf");
  1190. system("echo 'end 192.168.10.150' >> /etc/udhcpd.conf");
  1191. system("echo 'interface uap0' >> /etc/udhcpd.conf");
  1192. system("echo 'lease_file /var/log/udhcpd.leases'>> /etc/udhcpd.conf");
  1193. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd.conf");
  1194. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd.conf");
  1195. system("echo 'opt router 192.168.10.10' >> /etc/udhcpd.conf");
  1196. system("echo 'option domain local' >> /etc/udhcpd.conf");
  1197. system("echo 'option lease 86400' >> /etc/udhcpd.conf");
  1198. usleep(1000);
  1199. }
  1200. void InitialBridgeModeDhcpServerConfig(void)
  1201. {
  1202. system("echo 'start 192.168.10.100' > /etc/udhcpd.conf");
  1203. system("echo 'end 192.168.10.150' >> /etc/udhcpd.conf");
  1204. system("echo 'interface br0' >> /etc/udhcpd.conf");
  1205. system("echo 'lease_file /var/log/udhcpd.leases'>> /etc/udhcpd.conf");
  1206. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd.conf");
  1207. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd.conf");
  1208. system("echo 'opt router 192.168.10.10' >> /etc/udhcpd.conf");
  1209. system("echo 'option domain local' >> /etc/udhcpd.conf");
  1210. system("echo 'option lease 86400' >> /etc/udhcpd.conf");
  1211. usleep(1000);
  1212. }
  1213. bool Is_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1214. {
  1215. return (ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue & misc_cmd) > 0 ? true : false;
  1216. }
  1217. void Set_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1218. {
  1219. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue |= misc_cmd;
  1220. }
  1221. void Clean_Dispenser_MiscCommand(int dispenser, int misc_cmd)
  1222. {
  1223. ShmChargerInfo->DispenserMiscReq[dispenser].CtrlValue &= ~misc_cmd;
  1224. }
  1225. bool Is_Connector_MiscCommand(int connector, int misc_cmd)
  1226. {
  1227. return (ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue & misc_cmd) > 0 ? true : false;
  1228. }
  1229. void Set_Connector_MiscCommand(int connector, int misc_cmd)
  1230. {
  1231. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue |= misc_cmd;
  1232. }
  1233. void Clean_Connector_MiscCommand(int connector, int misc_cmd)
  1234. {
  1235. ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue &= ~misc_cmd;
  1236. }
  1237. void AnnounceEthernetStatus(void)
  1238. {
  1239. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1240. {
  1241. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1242. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1243. {
  1244. Set_Dispenser_MiscCommand(index, MISC_DISP_ETHERNET_STATUS);
  1245. }
  1246. }
  1247. }
  1248. void AnnounceWiFiStatus(void)
  1249. {
  1250. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1251. {
  1252. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1253. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1254. {
  1255. Set_Dispenser_MiscCommand(index, MISC_DISP_WIFI_STATUS);
  1256. }
  1257. }
  1258. }
  1259. void AnnounceTelcomModemStatus(void)
  1260. {
  1261. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1262. {
  1263. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1264. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1265. {
  1266. Set_Dispenser_MiscCommand(index, MISC_DISP_TELCOM_MODEM_STATUS);
  1267. }
  1268. }
  1269. }
  1270. void AnnounceBackendStatus(void)
  1271. {
  1272. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1273. {
  1274. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1275. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1276. {
  1277. Set_Dispenser_MiscCommand(index, MISC_DISP_BACKEND_STATUS);
  1278. }
  1279. }
  1280. }
  1281. void AnnounceBillingStatus(void)
  1282. {
  1283. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1284. {
  1285. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1286. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1287. {
  1288. Set_Dispenser_MiscCommand(index, MISC_DISP_BILLING_STATUS);
  1289. }
  1290. }
  1291. }
  1292. void AnnounceStopChargingButtonStatus(void)
  1293. {
  1294. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1295. {
  1296. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1297. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1298. {
  1299. Set_Dispenser_MiscCommand(index, MISC_DISP_STOP_BUTTON_STATUS);
  1300. }
  1301. }
  1302. }
  1303. void AnnounceAuthModeConfig(void)
  1304. {
  1305. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1306. {
  1307. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1308. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1309. {
  1310. Set_Dispenser_MiscCommand(index, MISC_DISP_AUTH_MODE_CONFIG);
  1311. }
  1312. }
  1313. }
  1314. void AnnounceEVCCIDConfig(void)
  1315. {
  1316. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1317. {
  1318. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1319. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1320. {
  1321. Set_Dispenser_MiscCommand(index, MISC_DISP_EVCCID_CONFIG);
  1322. }
  1323. }
  1324. }
  1325. void AnnounceLEDIntensity(void)
  1326. {
  1327. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1328. {
  1329. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1330. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1331. {
  1332. Set_Dispenser_MiscCommand(index, MISC_DISP_LED_INTENSITY);
  1333. }
  1334. }
  1335. }
  1336. void AnnounceCurrency(void)
  1337. {
  1338. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1339. {
  1340. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1341. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1342. {
  1343. Set_Dispenser_MiscCommand(index, MISC_DISP_CURRENCY);
  1344. }
  1345. }
  1346. }
  1347. void AnnounceRfidEndian(void)
  1348. {
  1349. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1350. {
  1351. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1352. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1353. {
  1354. Set_Dispenser_MiscCommand(index, MISC_DISP_RFID_CARD_ENDIAN);
  1355. }
  1356. }
  1357. }
  1358. void AnnounceRfidReader(void)
  1359. {
  1360. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1361. {
  1362. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1363. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1364. {
  1365. Set_Dispenser_MiscCommand(index, MISC_DISP_RFID_ENABLE);
  1366. }
  1367. }
  1368. }
  1369. void AnnounceRemoteStartNoID(void)
  1370. {
  1371. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  1372. {
  1373. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1374. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1375. {
  1376. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  1377. {
  1378. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  1379. if(chargingInfo[gun]->SystemStatus == S_IDLE)
  1380. {
  1381. ShmChargerInfo->ConnectorPageReq[gun].bits.RemoteStartNoID = true;
  1382. break;
  1383. }
  1384. }
  1385. }
  1386. }
  1387. }
  1388. void AnnounceLineStatusCode(int gun, int status)
  1389. {
  1390. int dispenser = 0;
  1391. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun].ParentDispensetIndex;
  1392. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_None &&
  1393. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  1394. {
  1395. ShmChargerInfo->LineApi.StatusCode[gun] = status;
  1396. Set_Connector_MiscCommand(gun, MISC_CONN_LINE_STATUS);
  1397. }
  1398. }
  1399. void AnnounceStationInfo(void)
  1400. {
  1401. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1402. {
  1403. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1404. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1405. {
  1406. Set_Dispenser_MiscCommand(index, MISC_DISP_STATION_INFO);
  1407. }
  1408. }
  1409. }
  1410. void AnnounceOccupancyPrice(void)
  1411. {
  1412. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  1413. {
  1414. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  1415. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  1416. {
  1417. Set_Dispenser_MiscCommand(index, MISC_DISP_PARKING_PRICE);
  1418. }
  1419. }
  1420. }
  1421. void UpdateConfigAndIcon(void)
  1422. {
  1423. unsigned char status = _Connnection_Disable;
  1424. // update ethernet connection status
  1425. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1426. if(ShmChargerInfo->CabinetMiscValue.EthernetStatus != status)
  1427. {
  1428. ShmChargerInfo->CabinetMiscValue.EthernetStatus = status;
  1429. AnnounceEthernetStatus();
  1430. }
  1431. // update wifi connection status
  1432. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1433. {
  1434. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1435. }
  1436. else
  1437. {
  1438. status = _Connnection_Disable;
  1439. }
  1440. if(ShmChargerInfo->CabinetMiscValue.WiFiStatus != status)
  1441. {
  1442. ShmChargerInfo->CabinetMiscValue.WiFiStatus = status;
  1443. AnnounceWiFiStatus();
  1444. }
  1445. // update 4g connection status
  1446. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1447. {
  1448. status = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1449. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? _Connnection_Connected : _Connnection_Disconnected;
  1450. }
  1451. else
  1452. {
  1453. status = _Connnection_Disable;
  1454. }
  1455. if(ShmChargerInfo->CabinetMiscValue.TelcomModemStatus != status)
  1456. {
  1457. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = status;
  1458. AnnounceTelcomModemStatus();
  1459. }
  1460. // update backend connection status
  1461. status = ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES ? _Connnection_Disconnected : _Connnection_Connected;
  1462. if(ShmChargerInfo->CabinetMiscValue.BackendStatus != status)
  1463. {
  1464. ShmChargerInfo->CabinetMiscValue.BackendStatus = status;
  1465. AnnounceBackendStatus();
  1466. }
  1467. // update billing status
  1468. status = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  1469. if(ShmChargerInfo->CabinetMiscValue.BillingStatus != status)
  1470. {
  1471. ShmChargerInfo->CabinetMiscValue.BillingStatus = status;
  1472. AnnounceBillingStatus();
  1473. }
  1474. // update stop charging button
  1475. status = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  1476. if(ShmChargerInfo->CabinetMiscValue.StopChargingButton != status)
  1477. {
  1478. ShmChargerInfo->CabinetMiscValue.StopChargingButton = status;
  1479. AnnounceStopChargingButtonStatus();
  1480. }
  1481. // update AuthMode config
  1482. status = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  1483. if(ShmChargerInfo->CabinetMiscValue.AuthModeConfig != status)
  1484. {
  1485. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = status;
  1486. AnnounceAuthModeConfig();
  1487. AnnounceEVCCIDConfig();
  1488. }
  1489. // update EVCCID config
  1490. status = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  1491. if(ShmChargerInfo->CabinetMiscValue.EVCCIDConfig != status)
  1492. {
  1493. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = status;
  1494. AnnounceEVCCIDConfig();
  1495. }
  1496. // update LED Intensity
  1497. status = ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity;
  1498. if(ShmChargerInfo->CabinetMiscValue.LEDIntensity != status)
  1499. {
  1500. ShmChargerInfo->CabinetMiscValue.LEDIntensity = status;
  1501. AnnounceLEDIntensity();
  1502. }
  1503. // update currency
  1504. status = ShmSysConfigAndInfo->SysConfig.BillingData.Currency;
  1505. if(ShmChargerInfo->CabinetMiscValue.Currency != status)
  1506. {
  1507. ShmChargerInfo->CabinetMiscValue.Currency = status;
  1508. AnnounceCurrency();
  1509. }
  1510. // update RfidCardNumEndian
  1511. status = ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian;
  1512. if(ShmChargerInfo->CabinetMiscValue.RfidEndian != status)
  1513. {
  1514. ShmChargerInfo->CabinetMiscValue.RfidEndian = status;
  1515. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Phihong ||
  1516. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  1517. {
  1518. AnnounceRfidEndian();
  1519. }
  1520. }
  1521. // update AuthMode config
  1522. status = ShmSysConfigAndInfo->SysConfig.isRFID;
  1523. if(ShmChargerInfo->AuthInfo.AuthMode.bits.RFIDEnable != status)
  1524. {
  1525. ShmChargerInfo->AuthInfo.AuthMode.bits.RFIDEnable = status;
  1526. ShmChargerInfo->CabinetMiscValue.RfidReaderEnable = status;
  1527. AnnounceRfidReader();
  1528. }
  1529. }
  1530. void Check4gWifiReset(void)
  1531. {
  1532. int timeDelay = 0;
  1533. if(ShmChargerInfo->Control.SysCtrl.bits.BackendEnable)
  1534. {
  1535. if((!ShmSysConfigAndInfo->SysInfo.InternetConn && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus &&
  1536. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE ||
  1537. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)) ||
  1538. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES)
  1539. {
  1540. if(!_NeedReset4gWifi)
  1541. {
  1542. GetClockTime(&_4gWifiReset_time);
  1543. LOG_INFO("Backend Enable But Internet Not Available!");
  1544. }
  1545. _NeedReset4gWifi = true;
  1546. }
  1547. else
  1548. {
  1549. _NeedReset4gWifi = false;
  1550. }
  1551. if(_NeedReset4gWifi)
  1552. {
  1553. timeDelay = ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken == YES ?
  1554. FAST_RESET_4G_WIFI_TIME : TRY_RESET_4G_WIFI_TIME;
  1555. if(GetTimeoutValue(_4gWifiReset_time) / uSEC_VAL >= timeDelay)
  1556. {
  1557. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = true;
  1558. usleep(100000);
  1559. // before reset 4g io, kill Module_4g & Module_Wifi task
  1560. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  1561. {
  1562. system("killall Module_4g &");
  1563. }
  1564. usleep(100000);
  1565. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  1566. {
  1567. system("killall Module_Wifi &");
  1568. }
  1569. LOG_INFO("Reset 4g_Wifi Module");
  1570. system("echo 1 > /sys/class/gpio/gpio104/value");
  1571. sleep(1);
  1572. system("echo 0 > /sys/class/gpio/gpio104/value");
  1573. _NeedReset4gWifi = false;
  1574. ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable = false;
  1575. }
  1576. }
  1577. }
  1578. else
  1579. {
  1580. _NeedReset4gWifi = false;
  1581. }
  1582. }
  1583. void MasterCabinetDhcpClientConfig(void)
  1584. {
  1585. char tmpbuf[256];
  1586. //Run DHCP client if enabled
  1587. system("killall udhcpc");
  1588. system("rm -rf /etc/resolv.conf");
  1589. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  1590. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  1591. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1592. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1593. {
  1594. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  1595. system(tmpbuf);
  1596. }
  1597. //Upgrade system id to /etc/hostname
  1598. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  1599. system(tmpbuf);
  1600. }
  1601. void InitMasterCabinetEthernet(void)
  1602. {
  1603. char tmpbuf[256];
  1604. //Init Eth0 for internet
  1605. memset(tmpbuf,0,256);
  1606. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  1607. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1608. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1609. system(tmpbuf);
  1610. memset(tmpbuf,0,256);
  1611. sprintf(tmpbuf,"route add default gw %s eth0 ",
  1612. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1613. system(tmpbuf);
  1614. //system("ifconfig lo up");
  1615. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  1616. //Init Eth1 for administrator tool
  1617. memset(tmpbuf,0,256);
  1618. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", "192.168.100.1", "255.255.255.0");
  1619. system(tmpbuf);
  1620. }
  1621. void InitSlaveCabinetEthernet(void)
  1622. {
  1623. char tmpbuf[256];
  1624. system("/sbin/ifconfig eth0 down");
  1625. system("/sbin/ifconfig eth1 down");
  1626. system("killall udhcpc");
  1627. system("rm -rf /etc/resolv.conf");
  1628. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  1629. //Init Eth0 for internet
  1630. memset(tmpbuf,0,256);
  1631. sprintf(tmpbuf,"/sbin/ifconfig eth0 192.168.100.3%d netmask %s up",
  1632. (ShmChargerInfo->Control.CabinetSwitch - 1),
  1633. "255.255.255.0");
  1634. system(tmpbuf);
  1635. system("/sbin/route add default gw 192.168.100.1 eth0");
  1636. //Init Eth1 for internet
  1637. memset(tmpbuf,0,256);
  1638. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up", SLAVE_ETH1_IP, SLAVE_ETH1_SUBMASK);
  1639. system(tmpbuf);
  1640. }
  1641. void InitEthernet(void)
  1642. {
  1643. system("ifconfig eth0 down");
  1644. system("ifconfig eth1 down");
  1645. sleep(2);
  1646. char tmpbuf[256];
  1647. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  1648. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  1649. sleep(2);
  1650. InitMasterCabinetEthernet();
  1651. MasterCabinetDhcpClientConfig();
  1652. // DO360 DHCP Server
  1653. InitialDispenserDhcpServerConfig();
  1654. StartDispenserDhcpServer();
  1655. pid_t pid = fork();
  1656. uint8_t cnt_pingDNS_Fail;
  1657. if(pid == 0)
  1658. {
  1659. ShmChargerInfo->CabinetMiscValue.BackendStatus = _Connnection_Disconnected;
  1660. ShmChargerInfo->CabinetMiscValue.EthernetStatus = _Connnection_Disconnected;
  1661. ShmChargerInfo->CabinetMiscValue.WiFiStatus = _Connnection_Disable;
  1662. ShmChargerInfo->CabinetMiscValue.TelcomModemStatus = _Connnection_Disable;
  1663. LOG_INFO("Internet Fork PID = %d", getpid());
  1664. for(;;)
  1665. {
  1666. if(isEthernetCablePlugIn(0) == YES)
  1667. {
  1668. if(isRouteFail())
  1669. {
  1670. LOG_INFO("eth0 not in route, restart eht0.");
  1671. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  1672. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  1673. {
  1674. InitialDHCP();
  1675. }
  1676. else
  1677. {
  1678. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  1679. memset(tmpbuf,0,256);
  1680. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  1681. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1682. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  1683. system(tmpbuf);
  1684. memset(tmpbuf,0,256);
  1685. sprintf(tmpbuf,"route add default gw %s eth0 &",
  1686. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1687. system(tmpbuf);
  1688. }
  1689. }
  1690. }
  1691. if(isReachableInternet() == PASS)
  1692. {
  1693. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != NO)
  1694. {
  1695. LOG_INFO("Internet is Reachable");
  1696. }
  1697. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = NO;
  1698. ShmSysConfigAndInfo->SysInfo.ethInternetConn = YES;
  1699. cnt_pingDNS_Fail = 0;
  1700. }
  1701. else
  1702. {
  1703. if(++cnt_pingDNS_Fail > 3)
  1704. {
  1705. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet != YES)
  1706. {
  1707. LOG_INFO("Internet is Disconnect Via Ethernet");
  1708. }
  1709. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = YES;
  1710. ShmSysConfigAndInfo->SysInfo.ethInternetConn = NO;
  1711. }
  1712. }
  1713. bool ethResult = ShmSysConfigAndInfo->SysInfo.ethInternetConn;
  1714. if(ethResult == YES)
  1715. {
  1716. system("/sbin/ifmetric eth0 0");
  1717. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1718. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1719. {
  1720. system("/sbin/ifmetric mlan0 1");
  1721. }
  1722. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1723. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1724. {
  1725. system("/sbin/ifmetric ppp0 2");
  1726. }
  1727. }
  1728. if (!ethResult &&
  1729. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1730. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1731. {
  1732. ethResult = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  1733. if (ethResult)
  1734. {
  1735. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1736. {
  1737. system("/sbin/ifmetric eth0 1");
  1738. system("/sbin/ifmetric mlan0 0");
  1739. }
  1740. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1741. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1742. {
  1743. system("/sbin/ifmetric ppp0 2");
  1744. }
  1745. }
  1746. }
  1747. if (!ethResult &&
  1748. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES &&
  1749. (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1750. {
  1751. ethResult = (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi == NO &&
  1752. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn == YES) ? YES : NO;
  1753. if (ethResult)
  1754. {
  1755. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  1756. ((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')))
  1757. {
  1758. system("/sbin/ifmetric mlan0 2");
  1759. }
  1760. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1761. {
  1762. system("/sbin/ifmetric eth0 1");
  1763. system("/sbin/ifmetric ppp0 0");
  1764. }
  1765. }
  1766. }
  1767. // Bridge ethernet to uap0 to get dynamic ip address
  1768. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE &&
  1769. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP))
  1770. {
  1771. if((isUap0up() == PASS) && access("/sys/class/net/br0/address", F_OK) == -1)
  1772. {
  1773. system("/usr/sbin/brctl addbr br0");
  1774. system("/usr/sbin/brctl addif br0 eth0");
  1775. system("/usr/sbin/brctl addif br0 uap0");
  1776. system("/sbin/ifconfig br0 192.168.10.200 up");
  1777. InitialBridgeModeDhcpServerConfig();
  1778. //system("/bin/sed -i '/interface/d' /etc/udhcpd.conf");
  1779. //system("/bin/echo 'interface br0' >> /etc/udhcpd.conf");
  1780. system("pgrep -f \"udhcpd /etc/udhcpd.conf\" | xargs kill");
  1781. system("/usr/sbin/udhcpd /etc/udhcpd.conf");
  1782. LOG_INFO("Bridge uap0 & eth0 for local power sharing by ethernet.");
  1783. _isBridgeModeConfig = true;
  1784. }
  1785. }
  1786. else
  1787. {
  1788. if(access("/sys/class/net/br0/address", F_OK) != -1)
  1789. {
  1790. system("/sbin/ifconfig br0 down");
  1791. system("/usr/sbin/brctl delbr br0");
  1792. LOG_INFO("Disable uap0 & eth0 Bridge.");
  1793. }
  1794. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP && _isBridgeModeConfig)
  1795. {
  1796. InitialApModeDhcpServerConfig();
  1797. system("pgrep -f \"udhcpd /etc/udhcpd.conf\" | xargs kill");
  1798. system("/usr/sbin/udhcpd /etc/udhcpd.conf");
  1799. LOG_INFO("Restart uap0 udhcpd.");
  1800. _isBridgeModeConfig = false;
  1801. }
  1802. }
  1803. // Change uap0 to low priority
  1804. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE &&
  1805. (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_AP) &&
  1806. (isUap0up() == PASS))
  1807. {
  1808. system("/sbin/ifmetric uap0 1");
  1809. }
  1810. //==============================================
  1811. // Check Local Power Sharing Communication
  1812. //==============================================
  1813. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing == _SYS_POWER_SHARING_MODE_SLAVE)
  1814. {
  1815. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  1816. {
  1817. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == ON)
  1818. {
  1819. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
  1820. }
  1821. }
  1822. else
  1823. {
  1824. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError == OFF)
  1825. {
  1826. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = ON;
  1827. }
  1828. }
  1829. }
  1830. else
  1831. {
  1832. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LocalPowerSharingCommunicationError = OFF;
  1833. }
  1834. if(!Is_Ocpp_Task_Alive())
  1835. {
  1836. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = false;
  1837. }
  1838. ShmSysConfigAndInfo->SysInfo.InternetConn = ethResult;
  1839. ShmStatusCodeData->InfoCode.InfoEvents.bits.WiFiDisable = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == _SYS_WIFI_MODE_DISABLE ? true : false;
  1840. ShmStatusCodeData->InfoCode.InfoEvents.bits.Telocom4GModuleDisable = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == false ? true : false;
  1841. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet = ShmSysConfigAndInfo->SysInfo.OcppConnStatus == false ? true : false;
  1842. UpdateConfigAndIcon();
  1843. Check4gWifiReset();
  1844. sleep(5);
  1845. }
  1846. }
  1847. #ifdef SystemLogMessage
  1848. LOG_INFO("[main]InitEthernet: Initial Ethernet OK.");
  1849. #endif
  1850. }
  1851. void GetMacAddress()
  1852. {
  1853. for (byte index = 0; index < 2; index++)
  1854. {
  1855. int fd;
  1856. struct ifreq ifr;
  1857. char tarEth[5];
  1858. char Mac[18];
  1859. sprintf(tarEth,"eth%d",index);
  1860. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1861. ifr.ifr_addr.sa_family = AF_INET;
  1862. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  1863. ioctl(fd, SIOCGIFHWADDR, &ifr);
  1864. close(fd);
  1865. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  1866. ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2],
  1867. ifr.ifr_hwaddr.sa_data[3], ifr.ifr_hwaddr.sa_data[4], ifr.ifr_hwaddr.sa_data[5]);
  1868. if (index == 0)
  1869. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, Mac);
  1870. else
  1871. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthMacAddress, Mac);
  1872. }
  1873. }
  1874. void GetFirmwareVersion()
  1875. {
  1876. // Get CSU root file system version
  1877. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1878. sprintf(ShmChargerInfo->SysMisc.MainVersion, fwVersion);
  1879. sprintf(ShmChargerInfo->SysMisc.SubVersion, subVersion);
  1880. #if ENABLE_PCBA_TEST == 0
  1881. byte count = 0, chademo = 0, ccs = 0, gb = 0;
  1882. for(uint8_t idx=0;idx<3;idx++)
  1883. {
  1884. if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'J')
  1885. {
  1886. chademo++;
  1887. count++;
  1888. }
  1889. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'G')
  1890. {
  1891. gb++;
  1892. count++;
  1893. }
  1894. else if (ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'U' ||
  1895. ShmSysConfigAndInfo->SysConfig.ModelName[7+idx] == 'E')
  1896. {
  1897. ccs++;
  1898. count++;
  1899. }
  1900. }
  1901. if (count == 1)
  1902. {
  1903. if (chademo > 0)
  1904. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '1';
  1905. else if (ccs > 0)
  1906. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '2';
  1907. else if (gb > 0)
  1908. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '3';
  1909. }
  1910. else
  1911. {
  1912. if (chademo > 0 && ccs > 0)
  1913. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '4';
  1914. else if (chademo > 0 && gb > 0)
  1915. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '5';
  1916. else if (ccs > 0 && gb > 0)
  1917. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[7] = '6';
  1918. }
  1919. // Get network option from model name
  1920. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1921. {
  1922. case 'B':
  1923. case 'U':
  1924. //Blue tooth
  1925. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1926. break;
  1927. case 'W':
  1928. // WIFI
  1929. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1930. break;
  1931. case 'T':
  1932. // 3G/4G
  1933. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1934. break;
  1935. case 'D':
  1936. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1937. break;
  1938. default:
  1939. // LAN
  1940. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1941. break;
  1942. }
  1943. // Get rating power from model name
  1944. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1945. // Get IEC or UL
  1946. char _buf[3] = {0};
  1947. memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[2], 2);
  1948. if (strcmp(_buf, "YE") == EQUAL || strcmp(_buf, "YC") == EQUAL)
  1949. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_IEC;
  1950. else if (strcmp(_buf, "WU") == EQUAL)
  1951. ShmSysConfigAndInfo->SysInfo.ChargerType = _CHARGER_TYPE_UL;
  1952. #endif
  1953. }
  1954. void InitialShareMemoryInfo()
  1955. {
  1956. FILE *fp;
  1957. char cmd[512];
  1958. char buf[512];
  1959. _UpgradeNeedReboot = FALSE;
  1960. _psuLostQuantity = 0;
  1961. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, "Internet");
  1962. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, " ");
  1963. //sprintf((char *)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  1964. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = 0;
  1965. ShmSysConfigAndInfo->SysConfig.AcConnectorCount = 0;
  1966. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = 0;
  1967. ShmSysConfigAndInfo->SysInfo.InputVoltageR = 0;
  1968. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  1969. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  1970. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = 0;
  1971. ShmSysConfigAndInfo->SysInfo.PsuFanRotaSpeed = 0;
  1972. ShmSysConfigAndInfo->SysInfo.AuxPower5V = 0;
  1973. ShmSysConfigAndInfo->SysInfo.AuxPower12V = 0;
  1974. ShmSysConfigAndInfo->SysInfo.AuxPower24V = 0;
  1975. ShmSysConfigAndInfo->SysInfo.AuxPower48V = 0;
  1976. sprintf((char *)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV:5.0");
  1977. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1978. sprintf(cmd, "/bin/uname -r");
  1979. fp = popen(cmd, "r");
  1980. if(fp == NULL)
  1981. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1982. else
  1983. {
  1984. while(fgets(buf, sizeof(buf), fp) != NULL)
  1985. {
  1986. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1987. }
  1988. }
  1989. // 雙槍 CCS + Chademo
  1990. GetFirmwareVersion();
  1991. //sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, fwVersion);
  1992. sprintf((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, " ");
  1993. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ");
  1994. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LcmFwRev, " ");
  1995. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuHwRev, " ");
  1996. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuPrimFwRev, " ");
  1997. sprintf((char *) ShmSysConfigAndInfo->SysInfo.PsuSecFwRev, " ");
  1998. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrHwRev, " ");
  1999. sprintf((char *) ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, " ");
  2000. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, " ");
  2001. sprintf((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, " ");
  2002. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, " ");
  2003. sprintf((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, " ");
  2004. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleHwRev, " "); //DO360 RC2
  2005. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, " "); //DO360 RC2
  2006. sprintf((char *) ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, " ");
  2007. sprintf((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, " ");
  2008. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector1FwRev, " ");
  2009. sprintf((char *) ShmSysConfigAndInfo->SysInfo.Connector2FwRev, " ");
  2010. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = 0;
  2011. ShmSysConfigAndInfo->SysInfo.SystemCriticalTemp = 0;
  2012. ShmSysConfigAndInfo->SysInfo.PsuAmbientTemp = 0;
  2013. ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp = 0;
  2014. ShmSysConfigAndInfo->SysInfo.InternetConn = 0;
  2015. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = 0;
  2016. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2017. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  2018. strcpy((char *) ShmSysConfigAndInfo->SysConfig.UserId, "");
  2019. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail = NO;
  2020. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail = NO;
  2021. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail = NO;
  2022. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ChademoboardStestFail = NO;
  2023. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail = NO;
  2024. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
  2025. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail = NO;
  2026. memset(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  2027. memset(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  2028. memset(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev, 0, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.Relay2ModuleFwRev)); //DO360 RC2
  2029. ShmPrimaryMcuData->SelfTest_Comp = NO;
  2030. ShmRelayModuleData[0]->SelfTest_Comp = NO;
  2031. ShmRelayModuleData[1]->SelfTest_Comp = NO;
  2032. ShmFanModuleData->SelfTest_Comp = NO;
  2033. ShmLedModuleData->SelfTest_Comp = NO;
  2034. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2035. ShmSysConfigAndInfo->SysInfo.MainChargingMode = _MAIN_CHARGING_MODE_MAX;
  2036. ShmSysConfigAndInfo->SysInfo.ReAssignedFlag = _REASSIGNED_NONE;
  2037. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  2038. ShmFanModuleData->TestFanSpeed = 0;
  2039. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = NO;
  2040. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  2041. ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag = NO;
  2042. ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag = NO;
  2043. LOG_INFO("DispenserSequence = %d, MaxConnectorQuantity = %d",
  2044. ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence,
  2045. ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity);
  2046. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.WiringInfo.DispenserSequence; i++)
  2047. {
  2048. LOG_INFO("Dispenser[%d] <=> %d", i, ShmSysConfigAndInfo->SysConfig.WiringInfo.WiringSetting[i]);
  2049. }
  2050. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2051. {
  2052. memset((char *)&_BackupStatusNotification[i], 0, sizeof(struct StructStatusNotification));
  2053. _useGunMeterValue[i] = false;
  2054. _isGetOccupancyFeeReq[i] = false;
  2055. }
  2056. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  2057. {
  2058. _canUpdateFinalCost[i] = false;
  2059. _isUndisposedFinalCost[i] = true;
  2060. }
  2061. }
  2062. int Initialization(void)
  2063. {
  2064. // int pinOut[2] = { 116, 115 };
  2065. for (byte count = 0; count < CONNECTOR_QUANTITY; count++)
  2066. {
  2067. chargingInfo[count]->RemoteStartFlag = NO;
  2068. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStartConfirm = false;
  2069. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[count].Parameter.bits.RemoteStopConfirm = false;
  2070. SetOcppErrorCode(count, "NoError");
  2071. }
  2072. LOG_INFO("Initialization OK");
  2073. return PASS;
  2074. }
  2075. void InitialPsuGroupingAndLocation(void)
  2076. {
  2077. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  2078. {
  2079. memcpy(ShmPsuGrouping->Location, Group_to_Location_4_Gun, sizeof(ShmPsuGrouping->Location));
  2080. memcpy(ShmPsuGrouping->Layout, Location_to_Group_4_Gun, sizeof(ShmPsuGrouping->Layout));
  2081. }
  2082. else
  2083. {
  2084. memcpy(ShmPsuGrouping->Location, Group_to_Location_2_Gun, sizeof(ShmPsuGrouping->Location));
  2085. memcpy(ShmPsuGrouping->Layout, Location_to_Group_2_Gun, sizeof(ShmPsuGrouping->Layout));
  2086. }
  2087. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2088. {
  2089. ShmPsuGrouping->GroupCollection[i].Index = i;
  2090. ShmPsuGrouping->GroupCollection[i].Location = ShmPsuGrouping->Location[i];
  2091. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  2092. {
  2093. if(ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay == 0)
  2094. {
  2095. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun[i][0], GENERAL_GUN_QUANTITY);
  2096. }
  2097. else
  2098. {
  2099. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_4_Gun_6Parallel[i][0], GENERAL_GUN_QUANTITY);
  2100. }
  2101. }
  2102. else
  2103. {
  2104. memcpy(&ShmPsuGrouping->GroupCollection[i].ParallelConfig[0], &GroupParallelRelay_2_Gun[i][0], GENERAL_GUN_QUANTITY);
  2105. }
  2106. ShmChargerInfo->Control.GunAvailable[i] = NO;
  2107. }
  2108. }
  2109. void InitialCustomerCode(void)
  2110. {
  2111. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  2112. {
  2113. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Audi;
  2114. }
  2115. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'E' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == '4')
  2116. {
  2117. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_E4U;
  2118. }
  2119. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'U' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'U')
  2120. {
  2121. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_UCar;
  2122. }
  2123. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'T' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'C')
  2124. {
  2125. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_TCC;
  2126. }
  2127. else if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'O' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'L')
  2128. {
  2129. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Shell;
  2130. }
  2131. else
  2132. {
  2133. ShmChargerInfo->Control.CustomerCode = _CUSTOMER_CODE_Phihong;
  2134. }
  2135. }
  2136. void InitialMaxDispenserConnectorQuantity(void)
  2137. {
  2138. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable == false)
  2139. {
  2140. ShmChargerInfo->Control.MaxDispenser = 2;
  2141. ShmChargerInfo->Control.MaxConnector = 2;
  2142. }
  2143. else
  2144. {
  2145. ShmChargerInfo->Control.MaxDispenser = GENERAL_GUN_QUANTITY;
  2146. ShmChargerInfo->Control.MaxConnector = GENERAL_GUN_QUANTITY;
  2147. }
  2148. }
  2149. void InitialAuthMode(void)
  2150. {
  2151. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  2152. {
  2153. }
  2154. }
  2155. void InitialQRCodeMode(void)
  2156. {
  2157. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi ||
  2158. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  2159. {
  2160. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_ChargeBoxId;
  2161. }
  2162. else
  2163. {
  2164. // customized
  2165. if(ShmSysConfigAndInfo->SysConfig.QRCodeMadeMode)
  2166. {
  2167. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Customized;
  2168. }
  2169. else
  2170. {
  2171. ShmChargerInfo->AuthInfo.QRCodeMode = _QR_MODE_Default;
  2172. }
  2173. }
  2174. }
  2175. void InitialPowerCabinetSetting(void)
  2176. {
  2177. if(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'A' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'D')
  2178. {
  2179. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = true;
  2180. }
  2181. ShmChargerInfo->CabinetMiscValue.BillingStatus = ShmSysConfigAndInfo->SysConfig.BillingData.isBilling;
  2182. ShmChargerInfo->CabinetMiscValue.StopChargingButton = ShmSysConfigAndInfo->SysConfig.StopChargingByButton;
  2183. ShmChargerInfo->CabinetMiscValue.AuthModeConfig = ShmSysConfigAndInfo->SysConfig.AuthorisationMode;
  2184. ShmChargerInfo->CabinetMiscValue.EVCCIDConfig = ShmSysConfigAndInfo->SysConfig.isAuthrizeByEVCCID;
  2185. }
  2186. void Initial6ParallelSetting(void)
  2187. {
  2188. if(ShmSysConfigAndInfo->SysConfig.ModelName[11] == '3')
  2189. {
  2190. ShmChargerInfo->Control.SysCtrl.bits.Enable6ParallelRelay = 1;
  2191. }
  2192. }
  2193. void PowerModuleLibSelection(void)
  2194. {
  2195. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'L')
  2196. {
  2197. ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib = true;
  2198. }
  2199. else
  2200. {
  2201. ShmChargerInfo->Control.LibCtrl.bits.InfyPwrLib = true;
  2202. }
  2203. }
  2204. bool InitialChargerSetting(void)
  2205. {
  2206. bool result = true;
  2207. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' &&
  2208. (ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'O' || ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'L'))
  2209. {
  2210. ShmChargerInfo->Control.PrimaryCtrl.bits.AcContactorReverse = true;
  2211. ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse = true;
  2212. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2213. ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable = true;
  2214. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  2215. }
  2216. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'K')
  2217. {
  2218. if(!(ShmSysConfigAndInfo->SysConfig.ModelName[12] == 'P' && ShmSysConfigAndInfo->SysConfig.ModelName[13] == 'K'))
  2219. {
  2220. ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  2221. }
  2222. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2223. ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  2224. }
  2225. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D' && ShmSysConfigAndInfo->SysConfig.ModelName[1] == 'B')
  2226. {
  2227. //ShmChargerInfo->Control.SysCtrl.bits.FanBoardDisable = true;
  2228. ShmChargerInfo->Control.SysCtrl.bits.LedBoardDisable = true;
  2229. ShmChargerInfo->Control.SysCtrl.bits.DOLedIndication = true;
  2230. //ShmChargerInfo->Control.SysCtrl.bits.DKLedIndication = true;
  2231. ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable = true;
  2232. ShmChargerInfo->Control.RelayCtrl.bits.DcInputEnable = true;
  2233. }
  2234. else
  2235. {
  2236. result = false;
  2237. }
  2238. InitialCustomerCode();
  2239. InitialMaxDispenserConnectorQuantity();
  2240. InitialAuthMode();
  2241. InitialQRCodeMode();
  2242. Initial6ParallelSetting();
  2243. InitialPsuGroupingAndLocation();
  2244. InitialPowerCabinetSetting();
  2245. PowerModuleLibSelection();
  2246. if(!CheckConnectorTypeStatus())
  2247. {
  2248. result = false;
  2249. }
  2250. return result;
  2251. }
  2252. bool InitialSystemDefaultConfig()
  2253. {
  2254. bool result = true;
  2255. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2256. InitGPIO();
  2257. InitEthernet();
  2258. GetMacAddress();
  2259. return result;
  2260. }
  2261. void SelfTestRun(void)
  2262. {
  2263. LOG_INFO("Self test.");
  2264. system("/root/Module_ChargerSelfTest &");
  2265. }
  2266. void SpawnPowerModuleTask(void)
  2267. {
  2268. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  2269. {
  2270. system("/root/Module_PsuComm_PH &");
  2271. }
  2272. else
  2273. {
  2274. system("/root/Module_PsuComm &");
  2275. }
  2276. }
  2277. void KillPowerModuleTask(void)
  2278. {
  2279. if(ShmChargerInfo->Control.LibCtrl.bits.PhPwrLib)
  2280. {
  2281. system("killall Module_PsuComm_PH");
  2282. }
  2283. else
  2284. {
  2285. system("killall Module_PsuComm");
  2286. }
  2287. }
  2288. void SpawnNormalService(void)
  2289. {
  2290. system("/root/Module_EventLogging &");
  2291. system("/root/Module_PrimaryComm &");
  2292. system("/root/Module_EvComm &");
  2293. //system("/root/Module_LcmControl &");
  2294. system("/root/Module_InternalComm &");
  2295. SpawnPowerModuleTask();
  2296. system("/root/Module_ProduceUtils &");
  2297. system("/root/Module_CabinetParallel &");
  2298. system("/root/Module_LedIndication &");
  2299. system("/root/Module_Authorize &");
  2300. }
  2301. void SpawnOcppService(void)
  2302. {
  2303. if(strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  2304. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  2305. {
  2306. Run_Ocpp_Service();
  2307. }
  2308. }
  2309. void SpawnMaintainOcppService(void)
  2310. {
  2311. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  2312. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  2313. {
  2314. Run_MaintainOcpp_Service();
  2315. }
  2316. }
  2317. void SpawnInternetService(void)
  2318. {
  2319. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  2320. {
  2321. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2322. {
  2323. system("/root/Module_4g &");
  2324. }
  2325. }
  2326. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  2327. {
  2328. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2329. {
  2330. system("/root/Module_Wifi &");
  2331. }
  2332. }
  2333. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  2334. {
  2335. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  2336. {
  2337. system("/root/Module_4g &");
  2338. }
  2339. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  2340. {
  2341. system("/root/Module_Wifi &");
  2342. }
  2343. }
  2344. }
  2345. int SpawnTask(void)
  2346. {
  2347. sleep(2);
  2348. SpawnNormalService();
  2349. SpawnInternetService();
  2350. return PASS;
  2351. }
  2352. int runFlashShellCmd(const char *cmd)
  2353. {
  2354. int result = FAIL;
  2355. char buf[256];
  2356. FILE *fp;
  2357. fp = popen(cmd, "r");
  2358. if(fp != NULL)
  2359. {
  2360. while(fgets(buf, sizeof(buf), fp) != NULL)
  2361. {
  2362. LOG_INFO("%s\n", buf);
  2363. }
  2364. result = PASS;
  2365. }
  2366. pclose(fp);
  2367. return result;
  2368. }
  2369. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2370. {
  2371. int result = PASS;
  2372. int fd, wrd;
  2373. unsigned int i, Chk;
  2374. unsigned char *ptr, *BufTmp;
  2375. Chk = 0;
  2376. ptr = (unsigned char *)UsrData;
  2377. if((BufTmp = malloc(MtdBlockSize)) != NULL)
  2378. {
  2379. memset(BufTmp, 0, MtdBlockSize);
  2380. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2381. for(i = ARRAY_SIZE(UsrData->CsuBootLoadFwRev); i < MtdBlockSize - 4; i++)
  2382. {
  2383. Chk += *(BufTmp+i);
  2384. }
  2385. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  2386. // Output configuration to file.
  2387. fd = open("/mnt/EvseTempConfig.bin", O_RDWR | O_CREAT | O_TRUNC);
  2388. if(fd < 0)
  2389. {
  2390. LOG_ERROR("open /mnt/EvseTempConfig.bin NG");
  2391. free(BufTmp);
  2392. return 0;
  2393. }
  2394. wrd = write(fd, BufTmp, MtdBlockSize);
  2395. close(fd);
  2396. if(wrd < MtdBlockSize)
  2397. {
  2398. LOG_ERROR("write /mnt/EvseTempConfig.bin NG");
  2399. free(BufTmp);
  2400. return 0;
  2401. }
  2402. LOG_INFO("EvseTempConfig write to file in /mnt OK.");
  2403. LOG_INFO("Erase /dev/mtd10.");
  2404. runFlashShellCmd("flash_erase /dev/mtd10 0 0");
  2405. LOG_INFO("Write /dev/mtd10.");
  2406. runFlashShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseTempConfig.bin");
  2407. LOG_INFO("Erase /dev/mtd11.");
  2408. runFlashShellCmd("flash_erase /dev/mtd11 0 0");
  2409. LOG_INFO("Write /dev/mtd11.");
  2410. runFlashShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseTempConfig.bin");
  2411. system("rm -f /mnt/EvseTempConfig.bin");
  2412. LOG_INFO("EvseTempConfig write to flash OK");
  2413. }
  2414. else
  2415. {
  2416. LOG_ERROR("alloc BlockSize NG");
  2417. result = FAIL;
  2418. }
  2419. if(BufTmp != NULL)
  2420. {
  2421. free(BufTmp);
  2422. }
  2423. return result;
  2424. }
  2425. //===============================================
  2426. // Common Detect Chk - Stop Charging ?
  2427. //===============================================
  2428. bool isEvBoardStopChargeFlag(byte gunIndex)
  2429. {
  2430. return chargingInfo[gunIndex]->ChargingStopFlag.bits.AlarmStop;
  2431. }
  2432. bool isNormalStopChargeFlag(byte gunIndex)
  2433. {
  2434. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.NormalStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.ManualStop);
  2435. }
  2436. bool isResetStopChargeFlag(byte gunIndex)
  2437. {
  2438. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.HardResetStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.SoftResetStop);
  2439. }
  2440. bool isInvalidIdStopFlag(byte gunIndex)
  2441. {
  2442. return chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop;
  2443. }
  2444. bool isSessionTargetStopFlag(byte gunIndex)
  2445. {
  2446. return chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop;
  2447. }
  2448. bool isBackendStopFlag(byte gunIndex)
  2449. {
  2450. return (chargingInfo[gunIndex]->ChargingStopFlag.bits.BackendStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop || chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop);
  2451. }
  2452. void ReleaseAlarmCode(byte gunIndex)
  2453. {
  2454. if(!Is_Ocpp_StatusNotificationErrorCode(gunIndex, "NoError"))
  2455. {
  2456. SetOcppErrorCode(gunIndex, "NoError");
  2457. }
  2458. if(!Is_Ocpp_StatusNotificationVendorCode(gunIndex, ""))
  2459. {
  2460. SetOcppVendorErrorCode(gunIndex, "");
  2461. }
  2462. if (strcmp((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "") != EQUAL)
  2463. {
  2464. strcpy((char *)chargingInfo[gunIndex]->ConnectorAlarmCode, "");
  2465. }
  2466. if (strcmp((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "") != EQUAL)
  2467. {
  2468. strcpy((char *)chargingInfo[gunIndex]->EvConnAlarmCode, "");
  2469. }
  2470. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "") != EQUAL)
  2471. {
  2472. strcpy((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemotenAlarmCode, "");
  2473. }
  2474. if (strcmp((char *)ShmChargerInfo->GunError[gunIndex].GunAlarmCode, "") != EQUAL)
  2475. {
  2476. strcpy((char *)ShmChargerInfo->GunError[gunIndex].GunAlarmCode, "");
  2477. }
  2478. }
  2479. //===============================================
  2480. // EmergencyStop and Charging Stop
  2481. //===============================================
  2482. void ChargingTerminalProcess(byte gunIndex)
  2483. {
  2484. setChargerMode(gunIndex, MODE_TERMINATING);
  2485. }
  2486. void ChargingAlarmProcess(byte gunIndex)
  2487. {
  2488. UpdateErrorCodeToOcpp(gunIndex);
  2489. setChargerMode(gunIndex, MODE_ALARM);
  2490. }
  2491. void AcChargingTerminalProcess()
  2492. {
  2493. ac_chargingInfo[0]->SystemStatus = MODE_TERMINATING;
  2494. }
  2495. void StopChargingProcessByString(byte level)
  2496. {
  2497. if (level > ShmSysConfigAndInfo->SysWarningInfo.Level)
  2498. {
  2499. ShmSysConfigAndInfo->SysWarningInfo.Level = level;
  2500. }
  2501. }
  2502. void ReleaseChargingProcessByString(byte level)
  2503. {
  2504. if (level >= ShmSysConfigAndInfo->SysWarningInfo.Level)
  2505. ShmSysConfigAndInfo->SysWarningInfo.Level = 0;
  2506. }
  2507. // 一般錯誤停止充電處理函式
  2508. void BoardErrOccurByString(byte index, char *code)
  2509. {
  2510. byte level = 1;
  2511. if ((chargingInfo[index]->SystemStatus > S_IDLE && chargingInfo[index]->SystemStatus < S_TERMINATING) ||
  2512. (chargingInfo[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2513. {
  2514. if (strncmp(code, "023730", 6) == EQUAL && ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop == NO)
  2515. {
  2516. ShmStatusCodeData->InfoCode.InfoEvents.bits.ChademoChargerGetEmergencyStop = YES;
  2517. }
  2518. ChargingTerminalProcess(index);
  2519. }
  2520. StopChargingProcessByString(level);
  2521. }
  2522. // 急停狀況的停止充電處理函式
  2523. void EmcOccureByString(char *code)
  2524. {
  2525. byte level = 2;
  2526. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  2527. {
  2528. return;
  2529. }
  2530. // 嚴重的急停有以下幾種 : EMC 按鈕、Mainbreak、Dooropen
  2531. // 其錯誤等級為 2
  2532. if (strncmp(code, "042251", 6) == EQUAL || strncmp(code, "042252", 6) == EQUAL ||
  2533. strncmp(code, "042238", 6) == EQUAL || strncmp(code, "042267", 6) == EQUAL ||
  2534. strncmp(code, "042328", 6) == EQUAL)
  2535. {
  2536. for (byte gun = 0; gun < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun++)
  2537. {
  2538. if ((chargingInfo[gun]->SystemStatus > S_IDLE && chargingInfo[gun]->SystemStatus < S_TERMINATING) ||
  2539. (chargingInfo[gun]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  2540. {
  2541. if (strncmp((char *)chargingInfo[gun]->ConnectorAlarmCode, "", 6) == EQUAL)
  2542. {
  2543. memcpy(chargingInfo[gun]->ConnectorAlarmCode, code, 6);
  2544. }
  2545. InformOcppErrOccur(4);
  2546. ChargingAlarmProcess(gun);
  2547. }
  2548. }
  2549. StopChargingProcessByString(level);
  2550. }
  2551. }
  2552. void ReleaseEmsOccureByString(byte index, char *code)
  2553. {
  2554. bool isTrigger = false;
  2555. byte level = 2;
  2556. if (strncmp(code, "042251", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2557. {
  2558. isTrigger = true;
  2559. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2560. }
  2561. else if (strncmp(code, "042252", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2562. {
  2563. isTrigger = true;
  2564. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2565. }
  2566. else if (strncmp(code, "042237", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip == YES)
  2567. {
  2568. isTrigger = true;
  2569. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2570. }
  2571. else if (strncmp(code, "042238", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip == YES)
  2572. {
  2573. isTrigger = true;
  2574. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2575. }
  2576. else if (strncmp(code, "042267", 6) == 0 && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm == YES)
  2577. {
  2578. isTrigger = true;
  2579. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NO;
  2580. }
  2581. if (isTrigger)
  2582. {
  2583. ReleaseChargingProcessByString(level);
  2584. }
  2585. }
  2586. //===============================================
  2587. // 確認硬體 (按鈕) 狀態
  2588. //===============================================
  2589. void ChkPrimaryStatus()
  2590. {
  2591. if(ShmChargerInfo->Control.TestCtrl.bits.ChargingSimulation)
  2592. {
  2593. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  2594. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = NO;
  2595. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2596. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2597. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2598. return;
  2599. }
  2600. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL)
  2601. {
  2602. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES;
  2603. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = YES;
  2604. EmcOccureByString("042251");
  2605. }
  2606. else
  2607. {
  2608. ReleaseEmsOccureByString(0, "042251");
  2609. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByEmergency = NO;
  2610. }
  2611. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL)
  2612. {
  2613. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = YES;
  2614. //EmcOccureByString("042238");
  2615. }
  2616. else
  2617. {
  2618. //ReleaseEmsOccureByString(0, "042238");
  2619. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MainPowerBreakerTrip = NO;
  2620. }
  2621. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL)
  2622. {
  2623. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = YES;
  2624. }
  2625. else
  2626. {
  2627. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SpdTrip = NO;
  2628. }
  2629. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL)
  2630. {
  2631. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen = YES;
  2632. EmcOccureByString("042252");
  2633. }
  2634. else
  2635. {
  2636. ReleaseEmsOccureByString(0, "042252");
  2637. }
  2638. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2639. {
  2640. EmcOccureByString("042328");
  2641. SetAcContactor(OFF);
  2642. }
  2643. }
  2644. struct timespec _AcContactReOpen_Time;
  2645. void ChkPsuStatus(void)
  2646. {
  2647. if(!ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2648. {
  2649. for(int psuGroup = 0; psuGroup < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; psuGroup++)
  2650. {
  2651. if(ShmPsuData->PsuGroup[psuGroup].GroupErrorFlag.PsuGroupErrorValue != 0)
  2652. {
  2653. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = YES;
  2654. }
  2655. }
  2656. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm)
  2657. {
  2658. EmcOccureByString("042267");
  2659. }
  2660. }
  2661. else
  2662. {
  2663. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor &&
  2664. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume == NO)
  2665. {
  2666. bool allFaultStatus = true;
  2667. for(int gunIndex = 0; gunIndex < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gunIndex++)
  2668. {
  2669. if(chargingInfo[gunIndex]->SystemStatus != S_FAULT && chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  2670. {
  2671. allFaultStatus = false;
  2672. }
  2673. }
  2674. if(allFaultStatus)
  2675. {
  2676. LOG_INFO("PSU Failure, Need Resume");
  2677. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = YES;
  2678. }
  2679. }
  2680. }
  2681. }
  2682. int AcContactorReOpenCount = 0;
  2683. void PsuFailureResume(void)
  2684. {
  2685. if(ShmChargerInfo->Control.PsuCtrl.bits.FailureResume)
  2686. {
  2687. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu == NO)
  2688. {
  2689. LOG_INFO("Ac Contactor Off By Psu Failure");
  2690. // update _AcContactReOpen_Time
  2691. GetClockTime(&_AcContactReOpen_Time);
  2692. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = YES;
  2693. AcContactorReOpenCount++;
  2694. }
  2695. else
  2696. {
  2697. if(AcContactorReOpenCount < MAX_AC_CONTACTOR_COUNT)
  2698. {
  2699. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= AC_CONTACTOR_REOPEN_TIME)
  2700. {
  2701. ReleaseEmsOccureByString(0, "042267");
  2702. // update _AcContactReOpen_Time
  2703. GetClockTime(&_AcContactReOpen_Time);
  2704. ShmChargerInfo->Control.RelayCtrl.bits.AcContactorOffByPsu = NO;
  2705. ShmChargerInfo->Control.PsuCtrl.bits.FailureResume = NO;
  2706. LOG_INFO("Psu Failure Resume");
  2707. }
  2708. }
  2709. }
  2710. }
  2711. else
  2712. {
  2713. if(AcContactorReOpenCount > 0)
  2714. {
  2715. if(GetTimeoutValue(_AcContactReOpen_Time) / uSEC_VAL >= PSU_FAILURE_RESUME_TIME)
  2716. {
  2717. AcContactorReOpenCount = 0;
  2718. }
  2719. }
  2720. }
  2721. }
  2722. void SetAcContactor(unsigned char OnOff)
  2723. {
  2724. OnOff = OnOff ? ON : OFF;
  2725. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor != OnOff)
  2726. {
  2727. if(OnOff)
  2728. {
  2729. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2730. }
  2731. LOG_INFO("Set Ac Contactor %s", OnOff == ON ? "On" : "Off");
  2732. }
  2733. ShmChargerInfo->Control.RelayCtrl.bits.AcContactor = OnOff == ON ? ON : OFF;
  2734. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER && ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  2735. {
  2736. SetParallelCabinetAcContactor(OnOff);
  2737. }
  2738. }
  2739. int GetStandbyTime(void)
  2740. {
  2741. int time = STANDBY_TIME;
  2742. if(ShmChargerInfo->Control.TestCtrl.bits.FastStandbyTime)
  2743. {
  2744. time = FAST_STANDBY_TIME;
  2745. }
  2746. return time;
  2747. }
  2748. void StandbyCheck(void)
  2749. {
  2750. bool enable = IsStandbyEnable();
  2751. if(IsConnectorWholeIdle() && IsParallelCabinetIdle())
  2752. {
  2753. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2754. {
  2755. int time = GetStandbyTime();
  2756. #if CONTACTOR_AUTO_ON_OFF == 1
  2757. time = CONTACTOR_AUTO_ON_TIME;
  2758. #endif
  2759. if(ShmChargerInfo->Control.RelayCtrl.bits.AcContactor)
  2760. {
  2761. if(GetSecTimeoutValue(_Standby_time) >= time && enable)
  2762. {
  2763. SetAcContactor(OFF);
  2764. }
  2765. }
  2766. #if CONTACTOR_AUTO_ON_OFF == 1
  2767. time = CONTACTOR_AUTO_ON_TIME + CONTACTOR_AUTO_OFF_TIME;
  2768. if((GetTimeoutValue(_Standby_time) / uSEC_VAL) >= time)
  2769. {
  2770. SetAcContactor(ON);
  2771. }
  2772. #endif
  2773. }
  2774. else
  2775. {
  2776. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = YES;
  2777. GetClockTime(&_Standby_time);
  2778. }
  2779. }
  2780. else
  2781. {
  2782. if(ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown)
  2783. {
  2784. ShmChargerInfo->Control.RelayCtrl.bits.StandbyCountdown = NO;
  2785. }
  2786. }
  2787. }
  2788. void CheckDisconnectionStatusRecovery(void)
  2789. {
  2790. bool recovery = true;
  2791. // check all connector
  2792. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  2793. {
  2794. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].Parameter.bits.Disconnection)
  2795. {
  2796. recovery = false;
  2797. }
  2798. }
  2799. if(recovery)
  2800. {
  2801. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = false;
  2802. }
  2803. }
  2804. //===============================================
  2805. // 確認各小板偵測的錯誤狀況
  2806. //===============================================
  2807. void CheckErrorOccurStatus(byte index)
  2808. {
  2809. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.NormalStopRequest)
  2810. {
  2811. chargingInfo[index]->ChargingStopFlag.bits.NormalStop = true;
  2812. }
  2813. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AlarmStopRequest)
  2814. {
  2815. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2816. {
  2817. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].RemotenAlarmCode, 6);
  2818. }
  2819. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2820. {
  2821. LOG_INFO("Connector %d Error Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2822. }
  2823. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2824. }
  2825. if(ShmChargerInfo->GunError[index].ErrFlag.ErrorValue != 0)
  2826. {
  2827. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2828. {
  2829. memcpy(chargingInfo[index]->ConnectorAlarmCode, ShmChargerInfo->GunError[index].GunAlarmCode, 6);
  2830. }
  2831. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2832. {
  2833. LOG_INFO("Connector %d GunError Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2834. }
  2835. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2836. }
  2837. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest)
  2838. {
  2839. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.TimeoutStopRequest = false;
  2840. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.Disconnection = true;
  2841. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = true;
  2842. if(chargingInfo[index]->SystemStatus == S_AUTHORIZING ||
  2843. chargingInfo[index]->SystemStatus == S_PREPARNING ||
  2844. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EV ||
  2845. chargingInfo[index]->SystemStatus == S_PREPARING_FOR_EVSE ||
  2846. chargingInfo[index]->SystemStatus == S_CHARGING)
  2847. {
  2848. if(strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2849. {
  2850. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042304", 6);
  2851. }
  2852. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2853. {
  2854. LOG_INFO("Connector %d Disconnect Alarm Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2855. }
  2856. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2857. }
  2858. else if(chargingInfo[index]->SystemStatus == S_REASSIGN_CHECK ||
  2859. chargingInfo[index]->SystemStatus == S_REASSIGN)
  2860. {
  2861. setChargerMode(index, MODE_IDLE);
  2862. }
  2863. }
  2864. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo)
  2865. {
  2866. CheckDisconnectionStatusRecovery();
  2867. }
  2868. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES ||
  2869. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen)
  2870. {
  2871. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2872. {
  2873. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_PRIMARY;
  2874. StopChargingProcessByString(2);
  2875. }
  2876. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2877. {
  2878. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip == YES)
  2879. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042251", 6);
  2880. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DoorOpen == YES)
  2881. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042252", 6);
  2882. }
  2883. }
  2884. else
  2885. {
  2886. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_PRIMARY)
  2887. {
  2888. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2889. ReleaseChargingProcessByString(2);
  2890. }
  2891. }
  2892. // RB
  2893. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  2894. {
  2895. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  2896. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  2897. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2898. {
  2899. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2900. {
  2901. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INUVP;
  2902. StopChargingProcessByString(2);
  2903. }
  2904. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2905. {
  2906. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES)
  2907. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042203", 6);
  2908. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES)
  2909. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042204", 6);
  2910. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  2911. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042205", 6);
  2912. }
  2913. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2914. {
  2915. LOG_INFO("Connector %d RC Board Input UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2916. }
  2917. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2918. }
  2919. else
  2920. {
  2921. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2922. {
  2923. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2924. ReleaseChargingProcessByString(2);
  2925. }
  2926. }
  2927. }
  2928. else
  2929. {
  2930. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INUVP)
  2931. {
  2932. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2933. ReleaseChargingProcessByString(2);
  2934. }
  2935. }
  2936. if(ShmChargerInfo->Control.RelayCtrl.bits.AcInputDisable == NO)
  2937. {
  2938. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES ||
  2939. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES ||
  2940. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2941. {
  2942. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2943. {
  2944. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2945. StopChargingProcessByString(2);
  2946. }
  2947. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2948. {
  2949. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == YES)
  2950. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042200", 6);
  2951. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == YES)
  2952. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042201", 6);
  2953. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == YES)
  2954. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042202", 6);
  2955. }
  2956. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2957. {
  2958. LOG_INFO("Connector %d RC Board Input OVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2959. }
  2960. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2961. }
  2962. else
  2963. {
  2964. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2965. {
  2966. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2967. ReleaseChargingProcessByString(2);
  2968. }
  2969. }
  2970. }
  2971. else
  2972. {
  2973. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES ||
  2974. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2975. {
  2976. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_NONE)
  2977. {
  2978. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_INOVP;
  2979. StopChargingProcessByString(2);
  2980. }
  2981. if (strncmp((char *)chargingInfo[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2982. {
  2983. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputOVP == YES)
  2984. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042327", 6);
  2985. else if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DcInputUVP == YES)
  2986. memcpy(chargingInfo[index]->ConnectorAlarmCode, "042328", 6);
  2987. }
  2988. if(!chargingInfo[index]->ChargingStopFlag.bits.AlarmStop)
  2989. {
  2990. LOG_INFO("Gun %d DC Input OVP/UVP Occur %s", index + 1, chargingInfo[index]->ConnectorAlarmCode);
  2991. }
  2992. chargingInfo[index]->ChargingStopFlag.bits.AlarmStop = true;
  2993. }
  2994. else
  2995. {
  2996. if (ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess == _EXTRA_ERR_PROCESS_INOVP)
  2997. {
  2998. ShmSysConfigAndInfo->SysWarningInfo.ExtraErrProcess = _EXTRA_ERR_PROCESS_NONE;
  2999. ReleaseChargingProcessByString(2);
  3000. }
  3001. }
  3002. }
  3003. }
  3004. int DiffTimebWithNowSec(struct timeb ST)
  3005. {
  3006. //return milli-second
  3007. struct timeb ET;
  3008. unsigned int StartTime,StopTime;
  3009. ftime(&ET);
  3010. StartTime=(unsigned int)ST.time;
  3011. StopTime=(unsigned int)ET.time;
  3012. return (StopTime-StartTime);
  3013. }
  3014. int CheckTimeOut(char *start)
  3015. {
  3016. int result = YES;
  3017. struct ParsingResult
  3018. {
  3019. int result;
  3020. int scanedElement;
  3021. int year;
  3022. int month;
  3023. int mday;
  3024. int hour;
  3025. int min;
  3026. int sec;
  3027. int tz_hour;
  3028. int tz_min;
  3029. float minSec;
  3030. }parsingResult;
  3031. struct tm tmStart;
  3032. struct timeb tbStart;
  3033. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  3034. if(strstr((char*)start, ".") != NULL)
  3035. {
  3036. // Original data with mini second
  3037. if(strstr((char*)start, "Z") != NULL)
  3038. {
  3039. // Original data with Z
  3040. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%fZ",
  3041. &parsingResult.year,
  3042. &parsingResult.month,
  3043. &parsingResult.mday,
  3044. &parsingResult.hour,
  3045. &parsingResult.min,
  3046. &parsingResult.sec,
  3047. &parsingResult.minSec);
  3048. }
  3049. else
  3050. {
  3051. // Original data without Z
  3052. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d.%f%d:%d",
  3053. &parsingResult.year,
  3054. &parsingResult.month,
  3055. &parsingResult.mday,
  3056. &parsingResult.hour,
  3057. &parsingResult.min,
  3058. &parsingResult.sec,
  3059. &parsingResult.minSec,
  3060. &parsingResult.tz_hour,
  3061. &parsingResult.tz_min);
  3062. }
  3063. }
  3064. else
  3065. {
  3066. // Original data without mini second
  3067. if(strstr((char*)start, "Z") != NULL)
  3068. {
  3069. // Original data with Z
  3070. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%dZ",
  3071. &parsingResult.year,
  3072. &parsingResult.month,
  3073. &parsingResult.mday,
  3074. &parsingResult.hour,
  3075. &parsingResult.min,
  3076. &parsingResult.sec);
  3077. }
  3078. else
  3079. {
  3080. // Original data without Z
  3081. parsingResult.scanedElement = sscanf((char*)start, "%d-%d-%dT%d:%d:%d%d:%d",
  3082. &parsingResult.year,
  3083. &parsingResult.month,
  3084. &parsingResult.mday,
  3085. &parsingResult.hour,
  3086. &parsingResult.min,
  3087. &parsingResult.sec,
  3088. &parsingResult.tz_hour,
  3089. &parsingResult.tz_min);
  3090. }
  3091. }
  3092. if(parsingResult.scanedElement >= 6)
  3093. {
  3094. tmStart.tm_year = parsingResult.year - 1900;
  3095. tmStart.tm_mon = parsingResult.month - 1;
  3096. tmStart.tm_mday = parsingResult.mday;
  3097. tmStart.tm_hour = parsingResult.hour;
  3098. tmStart.tm_min = parsingResult.min;
  3099. tmStart.tm_sec = parsingResult.sec;
  3100. tmStart.tm_gmtoff = 0;
  3101. tbStart.time = mktime(&tmStart);
  3102. tbStart.timezone = 0;
  3103. tbStart.millitm = 0;
  3104. tbStart.time -= (parsingResult.tz_hour*3600) + (parsingResult.tz_min*60*(parsingResult.tz_hour>=0?1:-1));
  3105. if(DiffTimebWithNowSec(tbStart) <= 0)
  3106. result = NO;
  3107. else
  3108. result = YES;
  3109. }
  3110. else
  3111. {
  3112. LOG_INFO("Start date parsing error");
  3113. }
  3114. return result;
  3115. }
  3116. bool Is_WebServer_Alive(void)
  3117. {
  3118. if(system("pidof -s lighttpd > /dev/null") != 0)
  3119. {
  3120. return false;
  3121. }
  3122. return true;
  3123. }
  3124. void Run_WebServer_Service(void)
  3125. {
  3126. system("/root/web.sh");
  3127. }
  3128. bool Is_LocalPowerSharing_Alive(void)
  3129. {
  3130. if(system("pidof -s Module_PowerSharing > /dev/null") != 0)
  3131. {
  3132. return false;
  3133. }
  3134. return true;
  3135. }
  3136. void Run_LocalPowerSharing_Service(void)
  3137. {
  3138. system ("/root/Module_PowerSharing &");
  3139. }
  3140. //===============================================
  3141. // Ocpp_Service
  3142. //===============================================
  3143. void Run_Ocpp_Service(void)
  3144. {
  3145. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3146. {
  3147. system("/root/OcppBackend &");
  3148. return;
  3149. }
  3150. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3151. {
  3152. system("/root/OcppBackend20 &");
  3153. return;
  3154. }
  3155. }
  3156. bool Is_Ocpp_Task_Alive(void)
  3157. {
  3158. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3159. {
  3160. if(system("pidof -s OcppBackend > /dev/null") != 0)
  3161. {
  3162. return false;
  3163. }
  3164. return true;
  3165. }
  3166. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3167. {
  3168. if(system("pidof -s OcppBackend20 > /dev/null") != 0)
  3169. {
  3170. return false;
  3171. }
  3172. return true;
  3173. }
  3174. return false;
  3175. }
  3176. void Kill_Ocpp_Service(void)
  3177. {
  3178. system("killall OcppBackend");
  3179. system("killall OcppBackend20");
  3180. }
  3181. //===============================================
  3182. // Maintain Ocpp_Service
  3183. //===============================================
  3184. void Run_MaintainOcpp_Service(void)
  3185. {
  3186. system("/root/OcppBackendPH &");
  3187. }
  3188. bool Is_MaintainOcpp_Task_Alive(void)
  3189. {
  3190. if(system("pidof -s OcppBackendPH > /dev/null") != 0)
  3191. {
  3192. return false;
  3193. }
  3194. return true;
  3195. }
  3196. void Kill_MaintainOcpp_Service(void)
  3197. {
  3198. system("killall OcppBackendPH");
  3199. }
  3200. //===============================================
  3201. // Ocpp OcppConnStatus
  3202. //===============================================
  3203. bool Get_Ocpp_ConnectionStatus(void)
  3204. {
  3205. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3206. {
  3207. return ShmOCPP16Data->OcppConnStatus > 0 ? true : false;
  3208. }
  3209. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3210. {
  3211. return ShmOCPP20Data->OcppConnStatus > 0 ? true : false;
  3212. }
  3213. return false;
  3214. }
  3215. //===============================================
  3216. // Ocpp procDogTime
  3217. //===============================================
  3218. long int Get_Ocpp_procDogTime(void)
  3219. {
  3220. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3221. {
  3222. return ShmOCPP16Data->procDogTime;
  3223. }
  3224. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3225. {
  3226. return ShmOCPP20Data->procDogTime;
  3227. }
  3228. return 0;
  3229. }
  3230. void Clean_Ocpp_procDogTime(void)
  3231. {
  3232. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3233. {
  3234. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  3235. return;
  3236. }
  3237. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3238. {
  3239. ShmOCPP20Data->procDogTime = time((time_t*)NULL);
  3240. return;
  3241. }
  3242. }
  3243. //===============================================
  3244. // Ocpp Boot Notification
  3245. //===============================================
  3246. bool Is_Ocpp_BootNotification(void)
  3247. {
  3248. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3249. {
  3250. return ShmOCPP16Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  3251. }
  3252. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3253. {
  3254. return ShmOCPP20Data->SpMsg.bits.BootNotificationConf == YES ? true : false;
  3255. }
  3256. return false;
  3257. }
  3258. void Clean_Ocpp_BootNotification(void)
  3259. {
  3260. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3261. {
  3262. ShmOCPP16Data->SpMsg.bits.BootNotificationConf = false;
  3263. return;
  3264. }
  3265. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3266. {
  3267. ShmOCPP20Data->SpMsg.bits.BootNotificationConf = false;
  3268. return;
  3269. }
  3270. }
  3271. //===============================================
  3272. // Ocpp Rest
  3273. //===============================================
  3274. bool Is_Ocpp_ResetReq(void)
  3275. {
  3276. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3277. {
  3278. return ShmOCPP16Data->MsMsg.bits.ResetReq == YES ? true : false;
  3279. }
  3280. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3281. {
  3282. return ShmOCPP20Data->MsMsg.bits.ResetReq == YES ? true : false;
  3283. }
  3284. return false;
  3285. }
  3286. bool Chk_Ocpp_Reset_Type(char *type)
  3287. {
  3288. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3289. {
  3290. return strcmp((char *)ShmOCPP16Data->Reset.Type, type) == EQUAL ? true : false;
  3291. }
  3292. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3293. {
  3294. return strcmp((char *)ShmOCPP20Data->Reset.type, type) == EQUAL ? true : false;
  3295. }
  3296. return false;
  3297. }
  3298. void Clean_Ocpp_ResetReq(void)
  3299. {
  3300. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3301. {
  3302. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3303. ShmOCPP16Data->MsMsg.bits.ResetReq = false;
  3304. ShmOCPP16Data->MsMsg.bits.ResetConf = true;
  3305. return;
  3306. }
  3307. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3308. {
  3309. sprintf ( (char*) ShmOCPP20Data->Reset.Response_status, "Accepted" );
  3310. ShmOCPP20Data->MsMsg.bits.ResetReq = false;
  3311. ShmOCPP20Data->MsMsg.bits.ResetConf = true;
  3312. return;
  3313. }
  3314. }
  3315. //===============================================
  3316. // Ocpp AuthorizeReq
  3317. //===============================================
  3318. void Set_Ocpp_AuthorizeReq(void)
  3319. {
  3320. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3321. {
  3322. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = false;
  3323. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = true;
  3324. return;
  3325. }
  3326. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3327. {
  3328. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = false;
  3329. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = true;
  3330. return;
  3331. }
  3332. }
  3333. bool Is_Ocpp_AuthorizeConf(void)
  3334. {
  3335. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3336. {
  3337. return ShmOCPP16Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3338. }
  3339. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3340. {
  3341. return ShmOCPP20Data->SpMsg.bits.AuthorizeConf == YES ? true : false;
  3342. }
  3343. return false;
  3344. }
  3345. bool Is_Ocpp_Authorize_Status(char *status)
  3346. {
  3347. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3348. {
  3349. return strcmp((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  3350. }
  3351. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3352. {
  3353. return strcmp((char *)ShmOCPP20Data->Authorize.Response_idTokenInfo.status, status) == EQUAL ? true : false;
  3354. }
  3355. return false;
  3356. }
  3357. //===============================================
  3358. // Ocpp UpdateFirmwareReq
  3359. //===============================================
  3360. bool Is_Ocpp_UpdateFirmwareReq(void)
  3361. {
  3362. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3363. {
  3364. return ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3365. }
  3366. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3367. {
  3368. return ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq == YES ? true : false;
  3369. }
  3370. return false;
  3371. }
  3372. void Clean_Ocpp_UpdateFirmwareReq(void)
  3373. {
  3374. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3375. {
  3376. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = false;
  3377. return;
  3378. }
  3379. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3380. {
  3381. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = false;
  3382. return;
  3383. }
  3384. }
  3385. bool Is_Ocpp_UpdateFirmwareStatus(char *status)
  3386. {
  3387. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3388. {
  3389. return strcmp((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status) == EQUAL ? true : false;
  3390. }
  3391. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3392. {
  3393. return strcmp((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status) == EQUAL ? true : false;
  3394. }
  3395. return false;
  3396. }
  3397. void Set_Ocpp_UpdateFirmwareStatus(char *status)
  3398. {
  3399. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3400. {
  3401. strcpy((char *)ShmOCPP16Data->FirmwareStatusNotification.Status, status);
  3402. return;
  3403. }
  3404. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3405. {
  3406. strcpy((char *)ShmOCPP20Data->FirmwareStatusNotification.status, status);
  3407. return;
  3408. }
  3409. }
  3410. void Clean_Ocpp_UpdateFirmwareStatus(char *status)
  3411. {
  3412. Set_Ocpp_UpdateFirmwareStatus("");
  3413. }
  3414. void Set_Ocpp_FirmwareStatusNotificationReq(void)
  3415. {
  3416. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3417. {
  3418. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3419. return;
  3420. }
  3421. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3422. {
  3423. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = YES;
  3424. return;
  3425. }
  3426. }
  3427. //===============================================
  3428. // Ocpp StopTransactionOnInvalidId
  3429. //===============================================
  3430. bool Is_Ocpp_StopOnInvalidId_Enable(void)
  3431. {
  3432. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3433. {
  3434. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE") > 0 ? true : false;
  3435. }
  3436. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3437. {
  3438. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3439. }
  3440. return false;
  3441. }
  3442. //===============================================
  3443. // Ocpp AuthorizeRemoteTxRequests
  3444. //===============================================
  3445. bool Is_Ocpp_AuthorizeRemoteTxRequests(void)
  3446. {
  3447. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3448. {
  3449. return strstr((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE") > 0 ? true : false;
  3450. }
  3451. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3452. {
  3453. return strstr((char *)ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE") > 0 ? true : false;
  3454. }
  3455. return false;
  3456. }
  3457. //===============================================
  3458. // Ocpp ConnectionTimeOut
  3459. //===============================================
  3460. int Get_Ocpp_ConnectionTimeOut(void)
  3461. {
  3462. int timeout = 0;
  3463. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3464. {
  3465. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData, "") != 0)
  3466. {
  3467. timeout = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3468. }
  3469. }
  3470. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3471. {
  3472. if(strcmp((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value, "" ) != 0)
  3473. {
  3474. timeout = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  3475. }
  3476. }
  3477. return timeout > 0 ? timeout : 0;
  3478. }
  3479. //===============================================
  3480. // Ocpp DefaultPrice
  3481. //===============================================
  3482. void Get_Ocpp_DefaultPrice(char *defaultPrice)
  3483. {
  3484. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3485. {
  3486. strcpy(defaultPrice, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
  3487. return;
  3488. }
  3489. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3490. {
  3491. strcpy(defaultPrice, "");
  3492. return;
  3493. }
  3494. strcpy(defaultPrice, "");
  3495. }
  3496. //===============================================
  3497. // Ocpp UserPrice
  3498. //===============================================
  3499. void Get_Ocpp_UserPrice(char *userId, char *userPrice)
  3500. {
  3501. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3502. {
  3503. strcpy(userId, (char *)ShmOCPP16Data->Cost.SetUserPrice.idToken);
  3504. strcpy(userPrice, (char *)ShmOCPP16Data->Cost.SetUserPrice.price);
  3505. return;
  3506. }
  3507. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3508. {
  3509. strcpy(userId, "");
  3510. strcpy(userPrice, "");
  3511. return;
  3512. }
  3513. strcpy(userId, "");
  3514. strcpy(userPrice, "");
  3515. }
  3516. void Clean_Ocpp_UserPrice(void)
  3517. {
  3518. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3519. {
  3520. memset(&ShmOCPP16Data->Cost.SetUserPrice, 0x00, sizeof(struct StrcutSetUserPrice));
  3521. return;
  3522. }
  3523. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3524. {
  3525. return;
  3526. }
  3527. }
  3528. //===============================================
  3529. // Ocpp RunningCost
  3530. //===============================================
  3531. void Get_Ocpp_RunningCost(int gun_index, char *cost)
  3532. {
  3533. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3534. {
  3535. strcpy(cost, (char *)ShmOCPP16Data->Cost.RunningCost[gun_index].description);
  3536. return;
  3537. }
  3538. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3539. {
  3540. strcpy(cost, (char *)"");
  3541. return;
  3542. }
  3543. }
  3544. void Clean_Ocpp_RunningCost(int gun_index)
  3545. {
  3546. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3547. {
  3548. memset(&ShmOCPP16Data->Cost.RunningCost[gun_index], 0x00, sizeof(struct StrcutRunningCost));
  3549. return;
  3550. }
  3551. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3552. {
  3553. return;
  3554. }
  3555. }
  3556. //===============================================
  3557. // Ocpp FinalCost
  3558. //===============================================
  3559. void Get_Ocpp_FinalCost(int gun_index, char *cost)
  3560. {
  3561. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3562. {
  3563. strcpy(cost, (char *)ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  3564. return;
  3565. }
  3566. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3567. {
  3568. strcpy(cost, (char *)"");
  3569. return;
  3570. }
  3571. }
  3572. void Clean_Ocpp_FinalCost(int gun_index)
  3573. {
  3574. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3575. {
  3576. memset(&ShmOCPP16Data->Cost.FinalCost[gun_index], 0x00, sizeof(struct StrcutFinalCost));
  3577. return;
  3578. }
  3579. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3580. {
  3581. return;
  3582. }
  3583. }
  3584. int Get_Ocpp_FinalCostTxId(int gun_index)
  3585. {
  3586. int txId = 0;
  3587. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3588. {
  3589. txId = ShmOCPP16Data->Cost.FinalCost[gun_index].txId;
  3590. }
  3591. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3592. {
  3593. }
  3594. return txId;
  3595. }
  3596. //===============================================
  3597. // Ocpp TimeOffset
  3598. //===============================================
  3599. void Get_Ocpp_TimeOffset(char *timeOffset)
  3600. {
  3601. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3602. {
  3603. strcpy(timeOffset, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[TimeOffset].ItemData);
  3604. return;
  3605. }
  3606. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3607. {
  3608. strcpy(timeOffset, "");
  3609. return;
  3610. }
  3611. strcpy(timeOffset, "");
  3612. }
  3613. //===============================================
  3614. // Ocpp PreAuthAmount
  3615. //===============================================
  3616. void Get_Ocpp_PreAuthAmount(char *amount)
  3617. {
  3618. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3619. {
  3620. strcpy(amount, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PreAuthAmount].ItemData);
  3621. return;
  3622. }
  3623. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3624. {
  3625. strcpy(amount, "");
  3626. return;
  3627. }
  3628. strcpy(amount, "");
  3629. }
  3630. //===============================================
  3631. // Ocpp PowerSharingServerIP
  3632. //===============================================
  3633. void Get_Ocpp_PowerSharingServerIP(char *ip)
  3634. {
  3635. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3636. {
  3637. strcpy(ip, (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[PowerSharingServerIP].ItemData);
  3638. return;
  3639. }
  3640. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3641. {
  3642. strcpy(ip, "");
  3643. return;
  3644. }
  3645. strcpy(ip, "");
  3646. }
  3647. //===============================================
  3648. // Ocpp Refunding
  3649. //===============================================
  3650. void Get_Ocpp_Refunding(char *Refunding)
  3651. {
  3652. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3653. {
  3654. return;
  3655. }
  3656. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3657. {
  3658. return;
  3659. }
  3660. }
  3661. void Clean_Ocpp_Refunding(void)
  3662. {
  3663. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3664. {
  3665. return;
  3666. }
  3667. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3668. {
  3669. return;
  3670. }
  3671. }
  3672. //===============================================
  3673. // Ocpp LcdOveride
  3674. //===============================================
  3675. bool Is_Ocpp_LcdOverideReq(int *gun, int *page)
  3676. {
  3677. *gun = ShmSysConfigAndInfo->SysInfo.LcdOveride.connectorId;
  3678. *page = ShmSysConfigAndInfo->SysInfo.LcdOveride.page_index;
  3679. return ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq ? true : false;
  3680. }
  3681. void Clean_Ocpp_LcdOverideReq(void)
  3682. {
  3683. ShmSysConfigAndInfo->SysInfo.LcdOveride.isOverideReq = false;
  3684. }
  3685. //===============================================
  3686. // Ocpp StationInfo
  3687. //===============================================
  3688. void Get_Ocpp_ChargerStationInfo(int *station_id, char *name)
  3689. {
  3690. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3691. {
  3692. strcpy(name, (char *)ShmOCPP16Data->TcciCustomData.ChargerInfo.station_name);
  3693. }
  3694. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3695. {
  3696. }
  3697. }
  3698. bool Is_Ocpp_ChargerStationReq(void)
  3699. {
  3700. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3701. {
  3702. return ShmOCPP16Data->TcciCustomData.ChargerInfoReq ? true : false;
  3703. }
  3704. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3705. {
  3706. return false;
  3707. }
  3708. return false;
  3709. }
  3710. void Clean_Ocpp_ChargerStationReq(void)
  3711. {
  3712. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3713. {
  3714. ShmOCPP16Data->TcciCustomData.ChargerInfoReq = false;
  3715. }
  3716. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3717. {
  3718. }
  3719. }
  3720. //===============================================
  3721. // Ocpp WeatherInfo
  3722. //===============================================
  3723. void Get_Ocpp_WeatherInfo(int *weatherID, float *temperature)
  3724. {
  3725. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3726. {
  3727. *weatherID = ShmOCPP16Data->TcciCustomData.WeatherInfo.weatherId;
  3728. *temperature = ShmOCPP16Data->TcciCustomData.WeatherInfo.temperature;
  3729. }
  3730. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3731. {
  3732. }
  3733. }
  3734. bool Is_Ocpp_WeatherReq(void)
  3735. {
  3736. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3737. {
  3738. return ShmOCPP16Data->TcciCustomData.WeatherInfoReq ? true : false;
  3739. }
  3740. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3741. {
  3742. return false;
  3743. }
  3744. return false;
  3745. }
  3746. void Clean_Ocpp_WeatherReq(void)
  3747. {
  3748. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3749. {
  3750. ShmOCPP16Data->TcciCustomData.WeatherInfoReq = false;
  3751. }
  3752. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3753. {
  3754. }
  3755. }
  3756. //===============================================
  3757. // Ocpp DeductInfo
  3758. //===============================================
  3759. void Set_Ocpp_DeductTransaction(int transaction)
  3760. {
  3761. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3762. {
  3763. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = transaction;
  3764. }
  3765. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3766. {
  3767. }
  3768. }
  3769. void Set_Ocpp_DeductCreditNo(char *userId)
  3770. {
  3771. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3772. {
  3773. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, userId, strlen(userId));
  3774. }
  3775. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3776. {
  3777. }
  3778. }
  3779. void Set_Ocpp_DeductVemData(char *vemData)
  3780. {
  3781. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3782. {
  3783. strcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, vemData);
  3784. }
  3785. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3786. {
  3787. }
  3788. }
  3789. void Set_Ocpp_DeductAmount(double value)
  3790. {
  3791. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3792. {
  3793. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = value;
  3794. }
  3795. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3796. {
  3797. }
  3798. }
  3799. void Set_Ocpp_DeductResult(unsigned char result)
  3800. {
  3801. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3802. {
  3803. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = result;
  3804. }
  3805. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3806. {
  3807. }
  3808. }
  3809. void Set_Ocpp_DonateInvoice(unsigned char donate)
  3810. {
  3811. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3812. {
  3813. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = donate ? YES : NO;
  3814. }
  3815. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3816. {
  3817. }
  3818. }
  3819. bool Set_Ocpp_DeductInfo(DeductResultInfoData *deductInfo)
  3820. {
  3821. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3822. {
  3823. ShmOCPP16Data->TcciCustomData.DeductInfo.txId = deductInfo->TransactionId;
  3824. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.creditNo, (char *)deductInfo->creditNo, sizeof(deductInfo->creditNo));
  3825. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.vemData, deductInfo->VemData, sizeof(deductInfo->VemData));
  3826. ShmOCPP16Data->TcciCustomData.DeductInfo.amount = ((double)deductInfo->DeductAmount / 100);
  3827. memcpy((char *)&ShmOCPP16Data->TcciCustomData.DeductInfo.approvalNo, deductInfo->ApprovalNumber, sizeof(deductInfo->ApprovalNumber));
  3828. ShmOCPP16Data->TcciCustomData.DeductInfo.deductResult = deductInfo->DeductResult;
  3829. ShmOCPP16Data->TcciCustomData.DeductInfo.isDonateInvoice = deductInfo->IsDonateInvoice ? YES : NO;
  3830. return true;
  3831. }
  3832. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3833. {
  3834. }
  3835. return false;
  3836. }
  3837. bool Is_Ocpp_ReportCreditDeductReq(void)
  3838. {
  3839. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3840. {
  3841. return ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq ? true : false;
  3842. }
  3843. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3844. {
  3845. return false;
  3846. }
  3847. return false;
  3848. }
  3849. void Set_Ocpp_ReportCreditDeductReq(void)
  3850. {
  3851. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3852. {
  3853. ShmOCPP16Data->TcciCustomData.ReportCreditDeductReq = YES;
  3854. }
  3855. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3856. {
  3857. }
  3858. }
  3859. //===============================================
  3860. // Ocpp Tcci SerialNo
  3861. //===============================================
  3862. void Clean_Ocpp_TcciSerialNo(int gun_index)
  3863. {
  3864. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3865. {
  3866. memset((char *)&ShmOCPP16Data->TcciCustomData.SerialNo[gun_index], 0x00, sizeof(37));
  3867. }
  3868. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3869. {
  3870. }
  3871. }
  3872. //===============================================
  3873. // Ocpp Tcci ReaderStatus
  3874. //===============================================
  3875. bool Set_Ocpp_TcciReaderStatus(int gun_index, DeductResultInfoData *ReaderStatus)
  3876. {
  3877. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3878. {
  3879. if(ReaderStatus->DeductResult == _Deduct_PreAuth_Cancel || ReaderStatus->DeductResult == _Deduct_PreAuth || ReaderStatus->DeductResult == _Deduct_PreAuth_Pass)
  3880. {
  3881. switch(ReaderStatus->DeductResult)
  3882. {
  3883. case _Deduct_PreAuth_Pass:
  3884. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 1;
  3885. break;
  3886. case _Deduct_PreAuth_Cancel:
  3887. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 2;
  3888. break;
  3889. case _Deduct_PreAuth:
  3890. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].readerStatus = 0;
  3891. break;
  3892. }
  3893. memcpy((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, ReaderStatus->creditNo, sizeof(ReaderStatus->creditNo));
  3894. memcpy((char *)&ShmOCPP16Data->TcciCustomData.VEMData[gun_index], ReaderStatus->VemData, sizeof(ReaderStatus->VemData));
  3895. return true;
  3896. }
  3897. }
  3898. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3899. {
  3900. }
  3901. return false;
  3902. }
  3903. void Clean_Ocpp_TcciCreditInfo(int gun_index)
  3904. {
  3905. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3906. {
  3907. memset((char *)&ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].creditNo));
  3908. memset((char *)&ShmOCPP16Data->TcciCustomData.VEMData[gun_index], 0x00, sizeof(ShmOCPP16Data->TcciCustomData.VEMData[gun_index]));
  3909. }
  3910. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3911. {
  3912. }
  3913. }
  3914. void Set_Ocpp_TcciReaderStatusReq(int gun_index)
  3915. {
  3916. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3917. {
  3918. ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq = true;
  3919. }
  3920. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3921. {
  3922. }
  3923. }
  3924. bool Is_Ocpp_TcciReaderStatusReq(int gun_index)
  3925. {
  3926. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3927. {
  3928. return ShmOCPP16Data->TcciCustomData.ReaderStatus[gun_index].ReportReaderStatusReq ? true : false;
  3929. }
  3930. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3931. {
  3932. return false;
  3933. }
  3934. return false;
  3935. }
  3936. //===============================================
  3937. // Ocpp Tcci OccupancyFeeDisplay
  3938. //===============================================
  3939. bool Is_Ocpp_TcciOccupancyDisplayReq(int gun_index)
  3940. {
  3941. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3942. {
  3943. return ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].OccupancyFeeDisplayReq ? true : false;
  3944. }
  3945. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3946. {
  3947. return false;
  3948. }
  3949. return false;
  3950. }
  3951. void Clean_Ocpp_TcciOccupancyDisplayReq(int gun_index)
  3952. {
  3953. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3954. {
  3955. ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].OccupancyFeeDisplayReq = false;
  3956. }
  3957. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3958. {
  3959. }
  3960. }
  3961. // input: gun_index, sn_len
  3962. // output: sn
  3963. // return: action
  3964. int Get_Ocpp_TcciOccupancyAction(int gun_index, int sn_len, char *sn)
  3965. {
  3966. int action = 0;
  3967. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3968. {
  3969. action = ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].actionId;
  3970. if(action == _ParkingAction_DisplayButton)
  3971. {
  3972. memcpy(sn, ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN, sn_len);
  3973. }
  3974. }
  3975. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3976. {
  3977. }
  3978. return action;
  3979. }
  3980. void Clean_Ocpp_TcciOccupancyActionAndSn(int gun_index)
  3981. {
  3982. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3983. {
  3984. ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].actionId = 0;
  3985. memset(ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.OccupancyFeeDisplay[gun_index].occupancySN));
  3986. }
  3987. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3988. {
  3989. }
  3990. }
  3991. //===============================================
  3992. // Ocpp Tcci SetOccupancyPrice
  3993. //===============================================
  3994. bool Is_Ocpp_TcciSetOccupancyPriceReq(void)
  3995. {
  3996. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3997. {
  3998. return ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.SetOccupancyPriceReq ? true : false;
  3999. }
  4000. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4001. {
  4002. return false;
  4003. }
  4004. return false;
  4005. }
  4006. void Clean_Ocpp_TcciSetOccupancyPriceReq(void)
  4007. {
  4008. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4009. {
  4010. ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.SetOccupancyPriceReq = false;
  4011. }
  4012. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4013. {
  4014. }
  4015. }
  4016. float Get_Ocpp_TcciOccupancyPrice(void)
  4017. {
  4018. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4019. {
  4020. return ShmOCPP16Data->TcciCustomData.SetOccupancyPrice.price;
  4021. }
  4022. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4023. {
  4024. }
  4025. return 0;
  4026. }
  4027. //===============================================
  4028. // Ocpp Tcci GetOccupancyFee
  4029. //===============================================
  4030. bool Is_Ocpp_TcciGetOccupancyFeeReq(int gun_index)
  4031. {
  4032. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4033. {
  4034. return ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].GetOccupancyFeeReq ? true : false;
  4035. }
  4036. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4037. {
  4038. return false;
  4039. }
  4040. return false;
  4041. }
  4042. void Set_Ocpp_TcciGetOccupancyFeeReq(int gun_index, OccupancyBillInfo *bill)
  4043. {
  4044. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4045. {
  4046. ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].GetOccupancyFeeReq = true;
  4047. memset(ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].occupancySN, 0x00, sizeof(ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].occupancySN));
  4048. memcpy(ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].occupancySN, bill->Sn, sizeof(bill->Sn));
  4049. }
  4050. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4051. {
  4052. }
  4053. }
  4054. void Clean_Ocpp_TcciGetOccupancyFeeReq(int gun_index)
  4055. {
  4056. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4057. {
  4058. ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].GetOccupancyFeeReq = false;
  4059. }
  4060. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4061. {
  4062. }
  4063. }
  4064. void Get_Ocpp_TcciGetOccupancyFeeResponse(int gun_index, OccupancyBillInfo *bill)
  4065. {
  4066. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4067. {
  4068. bill->Duration = ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].response_duration;
  4069. bill->Fee = ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].response_occupancyFee;
  4070. memcpy(bill->StartTime, ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].response_startTime, sizeof(bill->StartTime));
  4071. }
  4072. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4073. {
  4074. }
  4075. }
  4076. bool Is_Ocpp_TcciGetOccupancyFeeStatus(int gun_index, char *status)
  4077. {
  4078. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4079. {
  4080. return strcmp((char *)ShmOCPP16Data->TcciCustomData.GetOccupancyFee[gun_index].response_status, status) == EQUAL ? true : false;
  4081. }
  4082. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4083. {
  4084. }
  4085. return false;
  4086. }
  4087. //===============================================
  4088. // Ocpp Tcci ReportOccupancyDeductReq
  4089. //===============================================
  4090. bool Is_Ocpp_TcciReportOccupancyDeductReq(void)
  4091. {
  4092. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4093. {
  4094. return ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq ? true : false;
  4095. }
  4096. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4097. {
  4098. return false;
  4099. }
  4100. return false;
  4101. }
  4102. void Set_Ocpp_TcciReportOccupancyDeductReq(void)
  4103. {
  4104. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4105. {
  4106. ShmOCPP16Data->TcciCustomData.ReportOccupancyDeductReq = true;
  4107. }
  4108. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4109. {
  4110. }
  4111. }
  4112. //===============================================
  4113. // Ocpp Tcci OccupancyDeductResult
  4114. //===============================================
  4115. bool Set_Ocpp_OccupancyDeductResult(OccupancyDeductInfo *deduct, bool pass_fail)
  4116. {
  4117. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4118. {
  4119. memset(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult, 0x00, sizeof(struct StructOccupancyDeductResult));
  4120. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.deductResult = pass_fail;
  4121. ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.amount = deduct->Fee;
  4122. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.occupancySN[0], &deduct->Sn[0], sizeof(deduct->Sn));
  4123. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.creditNo[0], &deduct->creditNo[0], sizeof(deduct->creditNo));
  4124. memcpy(&ShmOCPP16Data->TcciCustomData.OccupancyDeductResult.approvalNo[0], &deduct->ApprovalNumber[0], sizeof(deduct->ApprovalNumber));
  4125. return true;
  4126. }
  4127. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4128. {
  4129. }
  4130. return false;
  4131. }
  4132. //===============================================
  4133. // Ocpp Start Transaction
  4134. //===============================================
  4135. void Set_Ocpp_StartTransactionIdTag(int gun_index, char *id)
  4136. {
  4137. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4138. {
  4139. strcpy((char *)ShmOCPP16Data->StartTransaction[gun_index].IdTag, id);
  4140. }
  4141. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4142. {
  4143. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4144. }
  4145. // MaintainServer
  4146. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4147. {
  4148. strcpy((char *)ShmOCPP16DataPH->StartTransaction[gun_index].IdTag, id);
  4149. }
  4150. }
  4151. void Set_Ocpp_StartTransactionReq(int gun_index)
  4152. {
  4153. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4154. {
  4155. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = true;
  4156. }
  4157. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4158. {
  4159. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4160. }
  4161. // MaintainServer
  4162. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4163. {
  4164. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq = true;
  4165. }
  4166. }
  4167. bool Is_Ocpp_StartTransactionConf(int gun_index)
  4168. {
  4169. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4170. {
  4171. return ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == YES ? true : false;
  4172. }
  4173. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4174. {
  4175. return ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf == YES ? true : false;
  4176. }
  4177. return false;
  4178. }
  4179. void Clean_Ocpp_StartTransactionConf(int gun_index)
  4180. {
  4181. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4182. {
  4183. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = false;
  4184. }
  4185. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4186. {
  4187. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = false;
  4188. }
  4189. // MaintainServer
  4190. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4191. {
  4192. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf = false;
  4193. }
  4194. }
  4195. bool Is_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4196. {
  4197. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4198. {
  4199. return strcmp((char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, status) == EQUAL ? true : false;
  4200. }
  4201. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4202. {
  4203. return strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, status) == EQUAL ? true : false;
  4204. }
  4205. return false;
  4206. }
  4207. bool Is_Ocpp_StartTransaction_Accepted(int gun_index)
  4208. {
  4209. return Is_Ocpp_StartTransaction_Status(gun_index, "Accepted");
  4210. }
  4211. void Get_Ocpp_StartTransaction_Status(int gun_index, char *status)
  4212. {
  4213. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4214. {
  4215. strcpy(status, (char *)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status);
  4216. }
  4217. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4218. {
  4219. strcpy(status, (char *)ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status);
  4220. }
  4221. }
  4222. void Get_Ocpp_TransactionId(int gun_index, char *transactionId)
  4223. {
  4224. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4225. {
  4226. sprintf(transactionId, "%d", ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId);
  4227. return;
  4228. }
  4229. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4230. {
  4231. strcpy(transactionId, (char *)&ShmOCPP20Data->TransactionEvent [gun_index].transactionInfo.transactionId[0]);
  4232. return;
  4233. }
  4234. sprintf(transactionId, "%d", 0);
  4235. }
  4236. bool Is_Invalid_StartTransactionId(int gun_index)
  4237. {
  4238. if(Is_Ocpp_StartTransaction_Status(gun_index, "Blocked") ||
  4239. Is_Ocpp_StartTransaction_Status(gun_index, "Expired") ||
  4240. Is_Ocpp_StartTransaction_Status(gun_index, "Invalid") ||
  4241. Is_Ocpp_StartTransaction_Status(gun_index, "NoCredit") ||
  4242. Is_Ocpp_StartTransaction_Status(gun_index, "NotAllowedTypeEVSE") ||
  4243. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisLocation") ||
  4244. Is_Ocpp_StartTransaction_Status(gun_index, "NotAtThisTime") ||
  4245. Is_Ocpp_StartTransaction_Status(gun_index, "Unknown"))
  4246. {
  4247. return true;
  4248. }
  4249. return false;
  4250. }
  4251. //===============================================
  4252. // Ocpp Stop Transaction
  4253. //===============================================
  4254. void Set_Ocpp_StopTransactionId(int gun_index, char *id)
  4255. {
  4256. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4257. {
  4258. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].IdTag, id);
  4259. return;
  4260. }
  4261. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4262. {
  4263. strcpy((char *)ShmOCPP20Data->TransactionEvent [gun_index].idToken.idToken, id);
  4264. return;
  4265. }
  4266. }
  4267. void Set_Ocpp_StopTransactionReq(int gun_index)
  4268. {
  4269. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4270. {
  4271. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = true;
  4272. }
  4273. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4274. {
  4275. ShmOCPP20Data->CpMsg.bits [gun_index].TransactionEventReq = true;
  4276. }
  4277. // MaintainServer
  4278. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4279. {
  4280. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = true;
  4281. }
  4282. }
  4283. //===============================================
  4284. // Ocpp Check Backup Error Code
  4285. //===============================================
  4286. void Chk_Ocpp_Backup_ErrorCode(int gun_index)
  4287. {
  4288. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4289. {
  4290. if(strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, "") == EQUAL)
  4291. {
  4292. if(strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, "") != EQUAL)
  4293. {
  4294. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, (char *)_BackupStatusNotification[gun_index].ErrorCode);
  4295. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, (char *)_BackupStatusNotification[gun_index].VendorErrorCode);
  4296. }
  4297. }
  4298. }
  4299. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4300. {
  4301. }
  4302. }
  4303. //===============================================
  4304. // Ocpp Reserve Now
  4305. //===============================================
  4306. bool Is_Ocpp_ReserveNowReq(int gun_index)
  4307. {
  4308. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4309. {
  4310. return ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4311. }
  4312. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4313. {
  4314. return ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq == YES ? true : false;
  4315. }
  4316. return false;
  4317. }
  4318. void Clean_Ocpp_ReserveNowReq(int gun_index)
  4319. {
  4320. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4321. {
  4322. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4323. return;
  4324. }
  4325. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4326. {
  4327. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = false;
  4328. return;
  4329. }
  4330. }
  4331. int Get_Ocpp_ReserveNowId(int gun_index)
  4332. {
  4333. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4334. {
  4335. return ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  4336. }
  4337. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4338. {
  4339. return ShmOCPP20Data->ReserveNow[gun_index].id;
  4340. }
  4341. return 0;
  4342. }
  4343. void Set_Ocpp_ReserveNowConf(int gun_index)
  4344. {
  4345. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4346. {
  4347. ShmOCPP16Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4348. return;
  4349. }
  4350. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4351. {
  4352. ShmOCPP20Data->CsMsg.bits [gun_index].ReserveNowConf = true;
  4353. return;
  4354. }
  4355. }
  4356. void Get_Ocpp_ReserveNowExpiryDate(int gun_index, char *date)
  4357. {
  4358. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4359. {
  4360. strcpy(date, (char *)ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate);
  4361. return;
  4362. }
  4363. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4364. {
  4365. strcpy(date, (char *)ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime);
  4366. return;
  4367. }
  4368. strcpy(date, "");
  4369. return;
  4370. }
  4371. bool Is_Ocpp_ReserveNowExpired(int gun_index)
  4372. {
  4373. bool _expired = true;
  4374. char _expiryDate[128];
  4375. Get_Ocpp_ReserveNowExpiryDate(gun_index, _expiryDate);
  4376. _expired = CheckTimeOut(_expiryDate);
  4377. return _expired;
  4378. }
  4379. void Get_Ocpp_ReserveNowIdTag(int gun_index, char *idTag)
  4380. {
  4381. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4382. {
  4383. strcpy(idTag, (char *)ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  4384. return;
  4385. }
  4386. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4387. {
  4388. strcpy(idTag, (char *)ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken);
  4389. return;
  4390. }
  4391. strcpy(idTag, "");
  4392. return;
  4393. }
  4394. bool Is_Ocpp_ReserveNowIdTag(uint8_t gun_index, char *authIdTag)
  4395. {
  4396. char ReserveIdTag[32];
  4397. Get_Ocpp_ReserveNowIdTag(gun_index, ReserveIdTag);
  4398. return strcmp(ReserveIdTag, authIdTag) == EQUAL ? true : false;
  4399. }
  4400. //===============================================
  4401. // Ocpp Cancel Reservation
  4402. //===============================================
  4403. bool Is_Ocpp_CancelReservationReq(int gun_index)
  4404. {
  4405. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4406. {
  4407. return ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4408. }
  4409. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4410. {
  4411. return ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == YES ? true : false;
  4412. }
  4413. return false;
  4414. }
  4415. void Clean_Ocpp_CancelReservationReq(int gun_index)
  4416. {
  4417. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4418. {
  4419. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4420. return;
  4421. }
  4422. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4423. {
  4424. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = false;
  4425. return;
  4426. }
  4427. }
  4428. void Set_Ocpp_CancelReservationConf(int gun_index)
  4429. {
  4430. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4431. {
  4432. ShmOCPP16Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4433. return;
  4434. }
  4435. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4436. {
  4437. ShmOCPP20Data->CsMsg.bits [gun_index].CancelReservationConf = true;
  4438. return;
  4439. }
  4440. }
  4441. //===============================================
  4442. // Ocpp Change Availability
  4443. //===============================================
  4444. bool Is_Ocpp_ChangeAvailabilityReq(int gun_index)
  4445. {
  4446. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4447. {
  4448. return ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4449. }
  4450. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4451. {
  4452. return ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq == YES ? true : false;
  4453. }
  4454. return false;
  4455. }
  4456. void Clean_Ocpp_ChangeAvailabilityReq(int gun_index)
  4457. {
  4458. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4459. {
  4460. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4461. return;
  4462. }
  4463. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4464. {
  4465. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = false;
  4466. return;
  4467. }
  4468. }
  4469. bool Chk_Ocpp_Availability_Type(int gun_index, char *type)
  4470. {
  4471. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4472. {
  4473. return strcmp((char *)ShmOCPP16Data->ChangeAvailability[gun_index].Type, type) == EQUAL ? true : false;
  4474. }
  4475. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4476. {
  4477. return strcmp((char *)ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, type) == EQUAL ? true : false;
  4478. }
  4479. return false;
  4480. }
  4481. //===============================================
  4482. // Ocpp Unlock Connector
  4483. //===============================================
  4484. bool Is_Ocpp_UnlockConnectorReq(int gun_index)
  4485. {
  4486. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4487. {
  4488. return ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4489. }
  4490. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4491. {
  4492. return ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == YES ? true : false;
  4493. }
  4494. return false;
  4495. }
  4496. void Clean_Ocpp_UnlockConnectorReq(int gun_index)
  4497. {
  4498. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4499. {
  4500. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4501. return;
  4502. }
  4503. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4504. {
  4505. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = false;
  4506. return;
  4507. }
  4508. }
  4509. void Set_Ocpp_UnlockConnectorConf(int gun_index)
  4510. {
  4511. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4512. {
  4513. strcpy((char *)ShmOCPP16Data->UnlockConnector[gun_index].ResponseStatus, "Unlocked");
  4514. ShmOCPP16Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4515. return;
  4516. }
  4517. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4518. {
  4519. strcpy((char *)ShmOCPP20Data->UnlockConnector[gun_index].Response_status, "Unlocked");
  4520. ShmOCPP20Data->CsMsg.bits [gun_index].UnlockConnectorConf = true;
  4521. return;
  4522. }
  4523. }
  4524. //===============================================
  4525. // Ocpp Remote Start
  4526. //===============================================
  4527. bool Is_Ocpp_RemoteStartReq(int gun_index)
  4528. {
  4529. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4530. {
  4531. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == YES ? true : false;
  4532. }
  4533. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4534. {
  4535. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq == YES ? true : false;
  4536. }
  4537. return false;
  4538. }
  4539. void Clean_Ocpp_RemoteStartReq(int gun_index)
  4540. {
  4541. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4542. {
  4543. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = false;
  4544. return;
  4545. }
  4546. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4547. {
  4548. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = false;
  4549. return;
  4550. }
  4551. }
  4552. void Get_Ocpp_RemoteStartIdTag(int gun_index, char *idTag)
  4553. {
  4554. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4555. {
  4556. strcpy(idTag, (char *)&ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag[0]);
  4557. return;
  4558. }
  4559. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4560. {
  4561. strcpy(idTag, (char *)&ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken[0]);
  4562. return;
  4563. }
  4564. strcpy(idTag, "");
  4565. }
  4566. //===============================================
  4567. // Ocpp Remote Stop
  4568. //===============================================
  4569. bool Is_Ocpp_RemoteStopReq(int gun_index)
  4570. {
  4571. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4572. {
  4573. return ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == YES ? true : false;
  4574. }
  4575. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4576. {
  4577. return ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq == YES ? true : false;
  4578. }
  4579. return false;
  4580. }
  4581. void Clean_Ocpp_RemoteStopReq(int gun_index)
  4582. {
  4583. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4584. {
  4585. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = false;
  4586. return;
  4587. }
  4588. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4589. {
  4590. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = false;
  4591. return;
  4592. }
  4593. }
  4594. //===============================================
  4595. // Ocpp StopReason
  4596. //===============================================
  4597. void Set_Ocpp_StopReason(int gun_index, char *reason)
  4598. {
  4599. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4600. {
  4601. if(strcmp((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "") == EQUAL)
  4602. {
  4603. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, reason);
  4604. }
  4605. }
  4606. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4607. {
  4608. if(strcmp((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "" ) == EQUAL)
  4609. {
  4610. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, reason);
  4611. }
  4612. }
  4613. // Maintain server
  4614. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4615. {
  4616. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, ShmOCPP16Data->StopTransaction[gun_index].StopReason, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  4617. }
  4618. }
  4619. void Get_Ocpp_StopReason(int gun_index, char *reason)
  4620. {
  4621. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4622. {
  4623. strcpy(reason, (char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4624. return;
  4625. }
  4626. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4627. {
  4628. strcpy(reason, (char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4629. return;
  4630. }
  4631. strcpy(reason, "");
  4632. }
  4633. void Clean_Ocpp_StopReason(int gun_index)
  4634. {
  4635. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4636. {
  4637. strcpy((char *)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "");
  4638. }
  4639. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4640. {
  4641. strcpy((char *)ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "");
  4642. }
  4643. // Maintain server
  4644. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  4645. {
  4646. strcpy((char *)ShmOCPP16DataPH->StopTransaction[gun_index].StopReason, "");
  4647. }
  4648. }
  4649. //===============================================
  4650. // Ocpp StatusNotification ErrorCode
  4651. //===============================================
  4652. void Set_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4653. {
  4654. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4655. {
  4656. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode);
  4657. return;
  4658. }
  4659. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4660. {
  4661. return;
  4662. }
  4663. }
  4664. bool Is_Ocpp_StatusNotificationErrorCode(int gun_index, char *errCode)
  4665. {
  4666. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4667. {
  4668. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4669. }
  4670. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4671. {
  4672. return strcmp((char *)_BackupStatusNotification[gun_index].ErrorCode, errCode) == EQUAL ? true : false;
  4673. }
  4674. return false;
  4675. }
  4676. //===============================================
  4677. // Ocpp StatusNotification VendorErrorCode
  4678. //===============================================
  4679. void Set_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4680. {
  4681. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4682. {
  4683. strcpy((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode);
  4684. return;
  4685. }
  4686. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4687. {
  4688. return;
  4689. }
  4690. }
  4691. bool Is_Ocpp_StatusNotificationVendorCode(int gun_index, char *vendorCode)
  4692. {
  4693. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4694. {
  4695. return strcmp((char *)ShmOCPP16Data->StatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4696. }
  4697. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4698. {
  4699. return strcmp((char *)_BackupStatusNotification[gun_index].VendorErrorCode, vendorCode) == EQUAL ? true : false;
  4700. }
  4701. return false;
  4702. }
  4703. //===============================================
  4704. // Ocpp ChargingProfileConf
  4705. //===============================================
  4706. bool Is_Ocpp_ChargingProfileConf(int gun_index)
  4707. {
  4708. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4709. {
  4710. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4711. }
  4712. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4713. {
  4714. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf == YES ? true : false;
  4715. }
  4716. return false;
  4717. }
  4718. void Clean_Ocpp_ChargingProfileConf(int gun_index)
  4719. {
  4720. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4721. {
  4722. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4723. return;
  4724. }
  4725. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4726. {
  4727. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = NO;
  4728. return;
  4729. }
  4730. }
  4731. //===============================================
  4732. // Ocpp ChargingProfileReq
  4733. //===============================================
  4734. bool Is_Ocpp_ChargingProfileReq(int gun_index)
  4735. {
  4736. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4737. {
  4738. return ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4739. }
  4740. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4741. {
  4742. return ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq == YES ? true : false;
  4743. }
  4744. return false;
  4745. }
  4746. void Set_Ocpp_ChargingProfileReq(int gun_index)
  4747. {
  4748. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4749. {
  4750. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4751. return;
  4752. }
  4753. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4754. {
  4755. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = YES;
  4756. return;
  4757. }
  4758. }
  4759. //===============================================
  4760. // Ocpp SmartChargingProfile ChargingProfileKind
  4761. //===============================================
  4762. bool Is_Ocpp_ChargingProfileKind(int gun_index, char *kind)
  4763. {
  4764. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4765. {
  4766. return strcmp((char *)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, kind) == EQUAL ? true : false;
  4767. }
  4768. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4769. {
  4770. return strcmp((char *)ShmOCPP20Data->SmartChargingProfile [gun_index].chargingProfileKind, kind) == EQUAL ? true : false;
  4771. }
  4772. return false;
  4773. }
  4774. bool Is_Ocpp_ChargingProfileId(int gun_index)
  4775. {
  4776. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4777. {
  4778. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == YES ? true : false;
  4779. }
  4780. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4781. {
  4782. return ShmOCPP20Data->SmartChargingProfile[gun_index].id == YES ? true : false;
  4783. }
  4784. return false;
  4785. }
  4786. int Get_Ocpp_ChargingProfileStartTime(int gun_index)
  4787. {
  4788. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4789. {
  4790. return GetStartScheduleTime(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  4791. }
  4792. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4793. {
  4794. return GetStartScheduleTime(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule);
  4795. }
  4796. return 0;
  4797. }
  4798. int Get_Ocpp_ChargingProfileScheduleCount(int gun_index)
  4799. {
  4800. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4801. {
  4802. return ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);
  4803. }
  4804. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4805. {
  4806. return ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod);
  4807. }
  4808. return 0;
  4809. }
  4810. int Get_Ocpp_ChargingProfileScheduleTime(int gun_index, int schedule_index)
  4811. {
  4812. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4813. {
  4814. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4815. }
  4816. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4817. {
  4818. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4819. }
  4820. return 0;
  4821. }
  4822. float Get_Ocpp_ChargingProfileScheduleLimit(int gun_index, int schedule_index)
  4823. {
  4824. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4825. {
  4826. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4827. }
  4828. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4829. {
  4830. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4831. }
  4832. return 0;
  4833. }
  4834. int Get_Ocpp_ChargingProfileScheduleNumberPhases(int gun_index, int schedule_index)
  4835. {
  4836. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4837. {
  4838. return ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4839. }
  4840. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4841. {
  4842. return ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[schedule_index].numberPhases;
  4843. }
  4844. return 0;
  4845. }
  4846. int Get_Ocpp_ChargingProfileStartIndex(int gun_index)
  4847. {
  4848. int _time = 0, _scheduleTime = 0;
  4849. int _maxCount = 0;
  4850. float _limit = 0;
  4851. int _startCount = NO_DEFINE;
  4852. _time = Get_Ocpp_ChargingProfileStartTime(gun_index);
  4853. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(gun_index);
  4854. for(int i = 0; i < _maxCount; i++)
  4855. {
  4856. _scheduleTime = Get_Ocpp_ChargingProfileScheduleTime(gun_index, i);
  4857. if(_time >= _scheduleTime)
  4858. {
  4859. _limit = Get_Ocpp_ChargingProfileScheduleLimit(gun_index, i);
  4860. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4861. {
  4862. _startCount = i;
  4863. }
  4864. }
  4865. }
  4866. return _startCount;
  4867. }
  4868. //===============================================
  4869. // Ocpp MaxChargingProfile ChargingProfileKind
  4870. //===============================================
  4871. bool Is_Ocpp_MaxChargingProfileKind(char *kind)
  4872. {
  4873. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4874. {
  4875. return strcmp((char *)ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, kind) == EQUAL ? true : false;
  4876. }
  4877. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4878. {
  4879. return strcmp((char *)ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, kind) == EQUAL ? true : false;
  4880. }
  4881. return false;
  4882. }
  4883. bool Is_Ocpp_MaxChargingProfileId(void)
  4884. {
  4885. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4886. {
  4887. return ShmOCPP16Data->MaxChargingProfile.ChargingProfileId == YES ? true : false;
  4888. }
  4889. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4890. {
  4891. return ShmOCPP20Data->MaxChargingProfile.id == YES ? true : false;
  4892. }
  4893. return false;
  4894. }
  4895. int Get_Ocpp_MaxProfileStartTime(void)
  4896. {
  4897. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4898. {
  4899. return GetStartScheduleTime(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule);
  4900. }
  4901. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4902. {
  4903. return GetStartScheduleTime(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule);
  4904. }
  4905. return 0;
  4906. }
  4907. int Get_Ocpp_MaxProfileScheduleCount(void)
  4908. {
  4909. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4910. {
  4911. return ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod);
  4912. }
  4913. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4914. {
  4915. return ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod);
  4916. }
  4917. return 0;
  4918. }
  4919. int Get_Ocpp_MaxProfileScheduleTime(int schedule_index)
  4920. {
  4921. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4922. {
  4923. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].StartPeriod;
  4924. }
  4925. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4926. {
  4927. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].startPeriod;
  4928. }
  4929. return 0;
  4930. }
  4931. float Get_Ocpp_MaxProfileScheduleLimit(int schedule_index)
  4932. {
  4933. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4934. {
  4935. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].Limit;
  4936. }
  4937. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4938. {
  4939. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4940. }
  4941. return 0;
  4942. }
  4943. int Get_Ocpp_MaxProfileScheduleNumberPhases(int schedule_index)
  4944. {
  4945. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4946. {
  4947. return ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[schedule_index].NumberPhases;
  4948. }
  4949. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4950. {
  4951. return ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[schedule_index].limit;
  4952. }
  4953. return 0;
  4954. }
  4955. int Get_Ocpp_MaxProfileStartIndex(void)
  4956. {
  4957. int _time = 0, _scheduleTime = 0;
  4958. int _maxCount = 0;
  4959. float _limit = 0;
  4960. int _startCount = NO_DEFINE;
  4961. _time = Get_Ocpp_MaxProfileStartTime();
  4962. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  4963. for(int i = 0; i < _maxCount; i++)
  4964. {
  4965. _scheduleTime = Get_Ocpp_MaxProfileScheduleTime(i);
  4966. if(_time >= _scheduleTime)
  4967. {
  4968. _limit = Get_Ocpp_MaxProfileScheduleLimit(i);
  4969. if((i == 0 && _limit >= MIN_OUTPUT_CUR) || _limit > MIN_OUTPUT_CUR)
  4970. {
  4971. _startCount = i;
  4972. }
  4973. }
  4974. }
  4975. return _startCount;
  4976. }
  4977. //===============================================
  4978. // Ocpp isRemoteStartWaitReq
  4979. //===============================================
  4980. int Is_Ocpp_RemoteStartWaitReq(void)
  4981. {
  4982. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4983. {
  4984. return ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  4985. }
  4986. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4987. {
  4988. return ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  4989. }
  4990. return 0;
  4991. }
  4992. //===============================================
  4993. // Ocpp SessionTarget
  4994. //===============================================
  4995. void Get_Ocpp_SessionTarget(int gunIndex, struct StructSessionTarget *sessionTarget)
  4996. {
  4997. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4998. {
  4999. memcpy(sessionTarget, &ShmOCPP16Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5000. }
  5001. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5002. {
  5003. memcpy(sessionTarget, &ShmOCPP20Data->SessionTarget[gunIndex], sizeof(struct StructSessionTarget));
  5004. }
  5005. }
  5006. //===============================================
  5007. // Ocpp OffLineMaxChargingPower
  5008. //===============================================
  5009. int Get_Ocpp_OffLineMaxChargingPower(void)
  5010. {
  5011. // unit: W
  5012. int maxChargingPower = 0;
  5013. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  5014. {
  5015. maxChargingPower = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[OffLineMaxChargingPower].ItemData);
  5016. }
  5017. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  5018. {
  5019. maxChargingPower = atoi((char *)ShmOCPP20Data->ControllerComponentVariable[OffLineMaxChargingPower].variableAttribute[0].value);
  5020. }
  5021. return maxChargingPower > 0 ? maxChargingPower : 0;
  5022. }
  5023. //===============================================
  5024. // DeductResult
  5025. //===============================================
  5026. void Set_Deduct_UserId(int gunIndex, char *userId)
  5027. {
  5028. memcpy((char *)&ShmChargerInfo->DeductResultReq[gunIndex].creditNo, userId, strlen(userId));
  5029. }
  5030. void Set_Deduct_TransactionId(int gunIndex, int transaction)
  5031. {
  5032. ShmChargerInfo->DeductResultReq[gunIndex].TransactionId = transaction;
  5033. }
  5034. void Clean_DeductInfo(int gunIndex)
  5035. {
  5036. memset(&ShmChargerInfo->DeductResultReq[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5037. }
  5038. bool Is_DeductResultReq(int gunIndex)
  5039. {
  5040. return ShmChargerInfo->DeductResultReq[gunIndex].DeductReq ? true : false;
  5041. }
  5042. //===============================================
  5043. // ReaderStatus
  5044. //===============================================
  5045. bool Is_ReaderStatusReq(int gunIndex)
  5046. {
  5047. return ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq ? true : false;
  5048. }
  5049. void Clean_ReaderStatusInfo(int gunIndex)
  5050. {
  5051. memset(&ShmChargerInfo->ReaderStatus[gunIndex], 0x00, sizeof(DeductResultInfoData));
  5052. }
  5053. //===============================================
  5054. // TransactionInfoData
  5055. //===============================================
  5056. void Set_UserTransactionId(int gunIndex, int transaction)
  5057. {
  5058. ShmChargerInfo->UserTransaction[gunIndex].TransactionId = transaction;
  5059. }
  5060. void Set_UserPrice(int gunIndex, float price)
  5061. {
  5062. ShmChargerInfo->UserTransaction[gunIndex].UserPrice = price;
  5063. }
  5064. void Set_UserTotalCost(int gunIndex, float cost)
  5065. {
  5066. ShmChargerInfo->UserTransaction[gunIndex].TotalCost = cost;
  5067. }
  5068. void Set_UserAccountBalance(int gunIndex, float balance)
  5069. {
  5070. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = balance;
  5071. }
  5072. void Set_UserDiscount(int gunIndex, float discount)
  5073. {
  5074. ShmChargerInfo->UserTransaction[gunIndex].CostDiscount = discount;
  5075. }
  5076. void Clean_UserTransaction(int gunIndex)
  5077. {
  5078. memset(&ShmChargerInfo->UserTransaction[gunIndex], 0x00, sizeof(TransactionInfoData));
  5079. ShmChargerInfo->UserTransaction[gunIndex].AccountBalance = -65535;
  5080. }
  5081. void Clean_UserPriceAndReceipt(int gunIndex)
  5082. {
  5083. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gunIndex]));
  5084. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex]));
  5085. }
  5086. //===============================================
  5087. // OccupancyDeductReq
  5088. //===============================================
  5089. bool Is_OccupancyDeductReq(int gunIndex)
  5090. {
  5091. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq ? true : false;
  5092. }
  5093. void Clean_OccupancyDeductReq(int gunIndex)
  5094. {
  5095. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.DeductReq = false;
  5096. }
  5097. bool Is_OccupancyKeepCountingReq(int gunIndex)
  5098. {
  5099. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting ? true : false;
  5100. }
  5101. void Set_OccupancyKeepCountingReq(int gunIndex)
  5102. {
  5103. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = true;
  5104. }
  5105. void Clean_OccupancyKeepCountingReq(int gunIndex)
  5106. {
  5107. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.KeepCounting = false;
  5108. }
  5109. //===============================================
  5110. // Main process
  5111. //===============================================
  5112. // 檢查 Byte 中某個 Bit 的值
  5113. // _byte : 欲改變的 byte
  5114. // _bit : 該 byte 的第幾個 bit
  5115. unsigned char DetectBitValue(unsigned char _byte, unsigned char _bit)
  5116. {
  5117. return ( _byte & mask_table[_bit] ) != 0x00;
  5118. }
  5119. // 設定 Byte 中某個 Bit的值
  5120. // _byte : 欲改變的 byte
  5121. // _bit : 該 byte 的第幾個 bit
  5122. // value : 修改的值為 0 or 1
  5123. void SetBitValue(unsigned char *_byte, unsigned char _bit, unsigned char value)
  5124. {
  5125. if(value == 1)
  5126. *_byte |= (1 << _bit);
  5127. else if (value == 0)
  5128. *_byte ^= (1 << _bit);
  5129. }
  5130. void UserScanFunction()
  5131. {
  5132. }
  5133. unsigned char isModeChange(unsigned char gun_index)
  5134. {
  5135. unsigned char result = NO;
  5136. if(chargingInfo[gun_index]->SystemStatus != chargingInfo[gun_index]->PreviousSystemStatus)
  5137. {
  5138. result = YES;
  5139. chargingInfo[gun_index]->PreviousSystemStatus = chargingInfo[gun_index]->SystemStatus;
  5140. }
  5141. return result;
  5142. }
  5143. void AnnounceAccountBalance(unsigned char index, float balance)
  5144. {
  5145. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = true;
  5146. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = balance;
  5147. if(!ShmChargerInfo->Control.TestCtrl.bits.DisableBalance)
  5148. {
  5149. Set_Connector_MiscCommand(index, MISC_CONN_ACCOUNT_BALANCE);
  5150. }
  5151. LOG_INFO("%sAnnounce Account Balance %4.2f To Connector %d",
  5152. ShmChargerInfo->Control.TestCtrl.bits.DisableBalance ? "Do Not " : "", balance, index + 1);
  5153. }
  5154. void FouceAnnounceAccountBalance(unsigned char index)
  5155. {
  5156. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance)
  5157. {
  5158. AnnounceAccountBalance(index, 0);
  5159. }
  5160. }
  5161. void CleanConnectorUserIdAndPrice(unsigned char index)
  5162. {
  5163. if(chargingInfo[index]->SystemStatus == S_IDLE)
  5164. {
  5165. LOG_INFO("*********** Connector %d Clean UserId & UserPrice ***********", index + 1);
  5166. memset(chargingInfo[index]->StartUserId, 0x00, 32);
  5167. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].UserPrice = 0;
  5168. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AccountBalance = -65535;
  5169. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].Parameter.bits.AnnounceBalance = false;
  5170. }
  5171. }
  5172. void DispenserAuthInitial(unsigned char dispenser)
  5173. {
  5174. LOG_INFO("*********** Dispenser %d Authorizing Initial ***********", dispenser + 1);
  5175. memset(&ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5176. // clean dispenser price
  5177. memset((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser][0], 0x00, sizeof(ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[dispenser]));
  5178. }
  5179. void GunAuthInitial(unsigned char connector)
  5180. {
  5181. LOG_INFO("*********** Gun %d Authorizing Initial ***********", connector + 1);
  5182. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5183. }
  5184. unsigned char _preGunAuthResult[GENERAL_GUN_QUANTITY];
  5185. unsigned char _preDispenserAuthResult[GENERAL_GUN_QUANTITY];
  5186. struct timespec _GunAuth_Time[GENERAL_GUN_QUANTITY];
  5187. struct timespec _DispenserAuth_Time[GENERAL_GUN_QUANTITY];
  5188. void AuthorizeTimeoutProcess(void)
  5189. {
  5190. int CompletedTime = 0;
  5191. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5192. {
  5193. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult != _AuthResult_None)
  5194. {
  5195. if(_preGunAuthResult[index] != ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult)
  5196. {
  5197. GetClockTime(&_GunAuth_Time[index]);
  5198. }
  5199. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5200. {
  5201. FouceAnnounceAccountBalance(index);
  5202. }
  5203. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult == _AuthResult_Invalid)
  5204. {
  5205. if(GetTimeoutValue(_GunAuth_Time[index]) / uSEC_VAL >= AUTHORIZE_COMPLETED_TIME)
  5206. {
  5207. LOG_INFO("*********** Gun %d Authorizing Initial ***********", index + 1);
  5208. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  5209. CleanConnectorUserIdAndPrice(index);
  5210. }
  5211. }
  5212. }
  5213. _preGunAuthResult[index] = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].AuthInfo.AuthResult;
  5214. }
  5215. for(int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  5216. {
  5217. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult != _AuthResult_None)
  5218. {
  5219. if(_preDispenserAuthResult[index] != ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult)
  5220. {
  5221. GetClockTime(&_DispenserAuth_Time[index]);
  5222. }
  5223. CompletedTime = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult == _AuthResult_Valid ?
  5224. (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + AUTHORIZE_COMPLETED_TIME) : AUTHORIZE_COMPLETED_TIME;
  5225. if(GetTimeoutValue(_DispenserAuth_Time[index]) / uSEC_VAL >= CompletedTime)
  5226. {
  5227. DispenserAuthInitial(index);
  5228. }
  5229. }
  5230. _preDispenserAuthResult[index] = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].AuthInfo.AuthResult;
  5231. }
  5232. }
  5233. BOOL IsConnectorAuthorizeSuccess(unsigned char connector)
  5234. {
  5235. BOOL success = false;
  5236. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].AuthInfo.AuthResult == _AuthResult_Valid)
  5237. {
  5238. success = true;
  5239. }
  5240. return success;
  5241. }
  5242. BOOL IsAutoGunSelectionAuthorizeSuccess(unsigned char connector)
  5243. {
  5244. BOOL success = false;
  5245. unsigned char dispenser = 0;
  5246. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  5247. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].AuthInfo.AuthResult == _AuthResult_Valid)
  5248. {
  5249. success = true;
  5250. }
  5251. return success;
  5252. }
  5253. bool CheckConnectorTypeStatus(void)
  5254. {
  5255. bool result = true;
  5256. if (strlen((char *) ShmSysConfigAndInfo->SysConfig.ModelName) >= 14)
  5257. {
  5258. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount = ShmChargerInfo->Control.MaxConnector;
  5259. ShmChargerInfo->Control.ChargerRatingPower = ParsingRatingPower((char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5260. ShmChargerInfo->Control.PsuQuantityByModel = ShmChargerInfo->Control.ChargerRatingPower / (INFY_SINGLE_PSU_POWER / 100);
  5261. LOG_INFO("DC Gun Count = %d, RatingPower = %d.%d kW",
  5262. ShmSysConfigAndInfo->SysConfig.TotalConnectorCount,
  5263. (ShmChargerInfo->Control.ChargerRatingPower / 10),
  5264. (ShmChargerInfo->Control.ChargerRatingPower % 10));
  5265. if(ShmSysConfigAndInfo->SysConfig.TotalConnectorCount == 0)
  5266. {
  5267. result = false;
  5268. }
  5269. //DO360 charging info default setting
  5270. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5271. {
  5272. chargingInfo[i] = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData;
  5273. chargingInfo[i]->Index = (i < ShmSysConfigAndInfo->SysConfig.WiringInfo.MaxConnectorQuantity || i == 0) ? i : 0xFF;
  5274. chargingInfo[i]->Evboard_id = 0x00;
  5275. chargingInfo[i]->ReservationId = -1;
  5276. chargingInfo[i]->slotsIndex = 0;
  5277. chargingInfo[i]->SystemStatus = S_BOOTING;
  5278. chargingInfo[i]->Type = _Type_Unknown;
  5279. chargingInfo[i]->type_index = 0;
  5280. chargingInfo[i]->IsAvailable = YES;
  5281. }
  5282. }
  5283. else
  5284. {
  5285. // Module Name 不正確 - 告警
  5286. result = false;
  5287. }
  5288. return result;
  5289. }
  5290. void KillTaskExceptPrimary()
  5291. {
  5292. system("killall Module_Authorize");
  5293. system("killall Module_CabinetParallel");
  5294. system("killall Module_ChargerSelfTest");
  5295. system("killall Module_LedIndication");
  5296. system("killall Module_EvComm");
  5297. system("killall Module_InternalComm");
  5298. KillPowerModuleTask();
  5299. system("killall Module_4g &");
  5300. system("killall Module_Wifi &");
  5301. }
  5302. void KillNtpd(void)
  5303. {
  5304. system("killall ntpd");
  5305. }
  5306. void KillAllTask()
  5307. {
  5308. system("killall Module_Authorize");
  5309. system("killall Module_CabinetParallel");
  5310. system("killall Module_ChargerSelfTest");
  5311. system("killall Module_LedIndication");
  5312. system("killall Module_EventLogging");
  5313. system("killall Module_PrimaryComm");
  5314. system("killall Module_EvComm");
  5315. system("killall Module_LcmControl");
  5316. system("killall Module_InternalComm");
  5317. KillPowerModuleTask();
  5318. Kill_Ocpp_Service();
  5319. Kill_MaintainOcpp_Service();
  5320. system("killall Module_4g &");
  5321. system("killall Module_Wifi &");
  5322. }
  5323. int TransferImageViaFTP(char *IpAddress, char *SourcePath)
  5324. {
  5325. int isPass = PASS;
  5326. unsigned char ftpcmdbuf[256];
  5327. //Using ftpput command to transfer upgrade image,
  5328. //User name : root
  5329. //User password : y42j/4cj84
  5330. //Destination : as same as source path
  5331. sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s %s %s", IpAddress, SourcePath, SourcePath);
  5332. if(system((char*)ftpcmdbuf) != 0)
  5333. {
  5334. isPass = FAIL;
  5335. }
  5336. return isPass;
  5337. }
  5338. void TriggerDispenserUpgrade(unsigned char index, char *FileName)
  5339. {
  5340. memset(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, 0x00, 128);
  5341. memcpy(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].FwFileName, FileName, strlen(FileName));
  5342. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].Setting.bits.FirmwareUpdateRequest = true;
  5343. LOG_INFO("Trigger dispenser %d to update image [%s]", index + 1, FileName);
  5344. }
  5345. BOOL PutImageToAllDispenser(char *SourcePath, char *FileName, char *model_name)
  5346. {
  5347. int isPass = FAIL;
  5348. int isImageOK[GENERAL_GUN_QUANTITY];
  5349. int dispenser_index = 0;
  5350. BOOL transferred = FALSE;
  5351. memset(isImageOK, 0x00, sizeof(isImageOK));
  5352. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5353. {
  5354. isPass = FAIL;
  5355. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].Status == _CNS_DispenserMatched)
  5356. {
  5357. dispenser_index = ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].DispenserIndex;
  5358. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName, (char *)model_name) != EQUAL)
  5359. {
  5360. LOG_INFO("Dispenser %d model name [%s] update ignore",
  5361. dispenser_index + 1,
  5362. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser_index].ModelName);
  5363. continue;
  5364. }
  5365. char DispenserIpAddress[16];
  5366. memset((char*)DispenserIpAddress, 0x00, 16);
  5367. sprintf((char*)DispenserIpAddress, "%d.%d.%d.%d",
  5368. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 0) & 0xFF),
  5369. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 8) & 0xFF),
  5370. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 16) & 0xFF),
  5371. ((ShmSysConfigAndInfo->SysInfo.DispenserInfo.ConnectionInfo[i].IpAddress >> 24) & 0xFF));
  5372. LOG_INFO("Transfer Image(%s) to Dispenser %d (IP: %s)", SourcePath, dispenser_index + 1, DispenserIpAddress);
  5373. isPass = TransferImageViaFTP(DispenserIpAddress, SourcePath);
  5374. isImageOK[dispenser_index] = isPass;
  5375. LOG_INFO("Dispenser %d Put Image %s", dispenser_index + 1, isPass == PASS ? "OK" : "Fail");
  5376. }
  5377. }
  5378. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5379. {
  5380. if(isImageOK[i] > 0)
  5381. {
  5382. TriggerDispenserUpgrade(i, FileName);
  5383. transferred = true;
  5384. }
  5385. }
  5386. return transferred;
  5387. }
  5388. BOOL WaitAllDispenserUpgradeCompleted(void)
  5389. {
  5390. BOOL completed = TRUE;
  5391. GetClockTime(&_DispenserUpgrade_time);
  5392. do
  5393. {
  5394. completed = TRUE;
  5395. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  5396. {
  5397. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest ||
  5398. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm)
  5399. {
  5400. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted)
  5401. {
  5402. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateRequest = false;
  5403. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateConfirm = false;
  5404. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.FirmwareUpdateCompleted = false;
  5405. LOG_INFO("Dispenser %d Upgrade Success", i + 1);
  5406. }
  5407. else
  5408. {
  5409. completed = FALSE;
  5410. }
  5411. }
  5412. }
  5413. if(GetTimeoutValue(_DispenserUpgrade_time) / uSEC_VAL >= DISPENSER_UPGRADE_TIME)
  5414. {
  5415. LOG_INFO("Wait Upgrade Timeout");
  5416. completed = FALSE;
  5417. break;
  5418. }
  5419. if(completed == FALSE)
  5420. {
  5421. sleep(1);
  5422. }
  5423. }while(!completed);
  5424. return completed;
  5425. }
  5426. void UpgradePrepare(void)
  5427. {
  5428. int count = 0;
  5429. SetAcContactor(OFF);
  5430. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5431. {
  5432. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5433. {
  5434. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5435. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5436. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5437. {
  5438. count++;
  5439. }
  5440. }
  5441. }
  5442. if(count > 0)
  5443. {
  5444. sleep(3);
  5445. }
  5446. else
  5447. {
  5448. sleep(1);
  5449. }
  5450. }
  5451. void TriggerPCabinetUpgrade(unsigned char index, char *FileName)
  5452. {
  5453. memset(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, 0x00, 128);
  5454. memcpy(ShmChargerInfo->ParallelCabinet.PCabinet[index].FwFileName, FileName, strlen(FileName));
  5455. ShmChargerInfo->ParallelCabinet.PCabinet[index].UpdateRequest = YES;
  5456. LOG_INFO("Trigger PCabinet %d to update image [%s]", index + 1, FileName);
  5457. }
  5458. BOOL PutImageToAllParallelCabinet(char *SourcePath, char *FileName)
  5459. {
  5460. int isPass = FAIL;
  5461. BOOL transferred = FALSE;
  5462. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5463. {
  5464. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5465. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5466. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5467. {
  5468. int channel = ShmChargerInfo->ParallelCabinet.PCabinet[i].SocketChannel;
  5469. if(ShmChargerInfo->CabinetConnInfo[channel].Status == _SCONN_MATCHED &&
  5470. ShmChargerInfo->ParallelCabinet.PCUpdateState[i] == Not_Update)
  5471. {
  5472. char PCabinetIpAddress[16];
  5473. memset((char*)PCabinetIpAddress, 0x00, 16);
  5474. sprintf((char*)PCabinetIpAddress, "%d.%d.%d.%d",
  5475. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 0) & 0xFF),
  5476. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 8) & 0xFF),
  5477. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 16) & 0xFF),
  5478. ((ShmChargerInfo->CabinetConnInfo[channel].IpAddress >> 24) & 0xFF));
  5479. isPass = TransferImageViaFTP(PCabinetIpAddress, SourcePath);
  5480. if(isPass == PASS)
  5481. {
  5482. TriggerPCabinetUpgrade(i, FileName);
  5483. }
  5484. transferred = true;
  5485. }
  5486. }
  5487. }
  5488. return transferred;
  5489. }
  5490. BOOL WaitAllPCabinetStartUpgrade(void)
  5491. {
  5492. BOOL isStart = true;
  5493. int count = 0;
  5494. struct timespec _waitStart;
  5495. GetClockTime(&_waitStart);
  5496. do
  5497. {
  5498. isStart = true;
  5499. count = 0;
  5500. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5501. {
  5502. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5503. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5504. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5505. {
  5506. count++;
  5507. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updating)
  5508. {
  5509. isStart = false;
  5510. }
  5511. }
  5512. }
  5513. if((GetTimeoutValue(_waitStart) / uSEC_VAL) >= START_UPGRADE_TIME)
  5514. {
  5515. isStart = false;
  5516. break;
  5517. }
  5518. usleep(10000);
  5519. }while(!isStart);
  5520. if(isStart && count > 0)
  5521. {
  5522. LOG_INFO("Total %d PCabinet upgrade start...", count);
  5523. }
  5524. else
  5525. {
  5526. if(count > 0)
  5527. {
  5528. LOG_INFO("Wait PCabinet upgrade timeout");
  5529. }
  5530. else
  5531. {
  5532. LOG_INFO("No PCabinet need to upgrade");
  5533. }
  5534. }
  5535. return isStart;
  5536. }
  5537. BOOL WaitAllPCabinetUpgradeCompleted(void)
  5538. {
  5539. BOOL completed = true;
  5540. int count = 0;
  5541. struct timespec _waitCompleted;
  5542. GetClockTime(&_waitCompleted);
  5543. do
  5544. {
  5545. completed = true;
  5546. count = 0;
  5547. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  5548. {
  5549. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  5550. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  5551. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  5552. {
  5553. count++;
  5554. if(ShmChargerInfo->ParallelCabinet.PCUpdateState[i] != Updated)
  5555. {
  5556. completed = false;
  5557. }
  5558. }
  5559. }
  5560. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= PCABINET_UPGRADE_TIME)
  5561. {
  5562. completed = false;
  5563. break;
  5564. }
  5565. usleep(10000);
  5566. }while(!completed);
  5567. if(completed && count > 0)
  5568. {
  5569. LOG_INFO("Total %d PCabinet upgrade completed!", count);
  5570. }
  5571. else
  5572. {
  5573. if(count > 0)
  5574. {
  5575. LOG_INFO("Wait PCabinet upgrade completed timeout");
  5576. }
  5577. else
  5578. {
  5579. LOG_INFO("Need not to wait PCabinet completed");
  5580. }
  5581. }
  5582. return completed;
  5583. }
  5584. BOOL WaitMCabinetUpgradeCompleted(void)
  5585. {
  5586. BOOL completed = true;
  5587. struct timespec _waitCompleted;
  5588. GetClockTime(&_waitCompleted);
  5589. do
  5590. {
  5591. completed = false;
  5592. if(ShmChargerInfo->SCabinetControl.NeedUpgrade == _R_NoUpgrade)
  5593. {
  5594. completed = true;
  5595. }
  5596. if(ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Idle &&
  5597. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Alarm &&
  5598. ShmChargerInfo->SCabinetControl.SCabinetStatus != _DeviceStatus_Charging)
  5599. {
  5600. completed = false;
  5601. break;
  5602. }
  5603. if((GetTimeoutValue(_waitCompleted) / uSEC_VAL) >= MCABINET_UPGRADE_TIME)
  5604. {
  5605. completed = false;
  5606. break;
  5607. }
  5608. usleep(10000);
  5609. }while(!completed);
  5610. if(completed)
  5611. {
  5612. LOG_INFO("MCabinet upgrade completed!");
  5613. }
  5614. else
  5615. {
  5616. LOG_INFO("Wait MCabinet upgrade timeout or disconnected...");
  5617. }
  5618. return completed;
  5619. }
  5620. bool IsModelNameMatch(char *model)
  5621. {
  5622. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == model[0] &&
  5623. ShmSysConfigAndInfo->SysConfig.ModelName[1] == model[1] &&
  5624. ShmSysConfigAndInfo->SysConfig.ModelName[7] == model[7] &&
  5625. ShmSysConfigAndInfo->SysConfig.ModelName[8] == model[8] &&
  5626. ShmSysConfigAndInfo->SysConfig.ModelName[9] == model[9] &&
  5627. ShmSysConfigAndInfo->SysConfig.ModelName[11] == model[11] &&
  5628. ShmSysConfigAndInfo->SysConfig.ModelName[12] == model[12] &&
  5629. ShmSysConfigAndInfo->SysConfig.ModelName[13] == model[13])
  5630. {
  5631. return true;
  5632. }
  5633. return false;
  5634. }
  5635. unsigned long long getAvailableMemory(void)
  5636. {
  5637. long pages = sysconf(_SC_AVPHYS_PAGES);
  5638. long page_size = sysconf(_SC_PAGE_SIZE);
  5639. return pages * page_size;
  5640. }
  5641. void ImageTypeParsing(unsigned int type, char *type_str)
  5642. {
  5643. switch(type)
  5644. {
  5645. case 0x10000001:
  5646. sprintf(type_str, "Image_uboot");
  5647. break;
  5648. case 0x10000002:
  5649. sprintf(type_str, "Image_dtb");
  5650. break;
  5651. case 0x10000003:
  5652. sprintf(type_str, "Image_zImage");
  5653. break;
  5654. case 0x10000004:
  5655. sprintf(type_str, "Image_ramdisk");
  5656. break;
  5657. case 0x10000005:
  5658. sprintf(type_str, "Image_Factory");
  5659. break;
  5660. case 0x10000006:
  5661. sprintf(type_str, "Image_Primary");
  5662. break;
  5663. case 0x10000007:
  5664. sprintf(type_str, "Image_CCS_uboot");
  5665. break;
  5666. case 0x10000008:
  5667. sprintf(type_str, "Image_CCS_dtb");
  5668. break;
  5669. case 0x10000009:
  5670. sprintf(type_str, "Image_CCS_zImage");
  5671. break;
  5672. case 0x1000000A:
  5673. sprintf(type_str, "Image_CCS_ramdisk");
  5674. break;
  5675. case 0x1000000B:
  5676. sprintf(type_str, "Image_CAHdeMO");
  5677. break;
  5678. case 0x1000000C:
  5679. sprintf(type_str, "Image_GBT");
  5680. break;
  5681. case 0x1000000D:
  5682. sprintf(type_str, "Image_Relay");
  5683. break;
  5684. case 0x1000000E:
  5685. sprintf(type_str, "Image_Fan");
  5686. break;
  5687. case 0x1000000F:
  5688. sprintf(type_str, "Image_LCM");
  5689. break;
  5690. case 0x10000010:
  5691. sprintf(type_str, "Image_Infy_PFC");
  5692. break;
  5693. case 0x10000011:
  5694. sprintf(type_str, "Image_Infy_DD");
  5695. break;
  5696. case 0x10000014:
  5697. sprintf(type_str, "Image_LED");
  5698. break;
  5699. case 0x10000015:
  5700. sprintf(type_str, "Image_LCM_APK");
  5701. break;
  5702. default:
  5703. sprintf(type_str, "Image_Unknown");
  5704. break;
  5705. }
  5706. }
  5707. char CheckUpdateProcess()
  5708. {
  5709. char _model[32];
  5710. char _type[32];
  5711. bool support = false;
  5712. bool OneImageOnly = false;
  5713. int isPass = true;
  5714. uint8_t retSucc = 0;
  5715. uint8_t retFail = 0;
  5716. byte target = 0;
  5717. char *new_str = NULL;
  5718. unsigned char *ptr = NULL;
  5719. int fd = 0;
  5720. int uartFd = 0;
  5721. unsigned int Type = 0;
  5722. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0;
  5723. DIR *d;
  5724. struct dirent *dir;
  5725. char cmdBuf[128];
  5726. int availableMem = 0;
  5727. memset(_model, 0x00, sizeof(_model));
  5728. memset(_type, 0x00, sizeof(_type));
  5729. availableMem = getAvailableMemory() / (1024 * 1024);
  5730. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5731. // If available memory too small, free memory cache first
  5732. if(availableMem < MIN_AVAILABLE_MEMORY)
  5733. {
  5734. LOG_INFO("Available memory less than %d MBytes, free cache & kill ntpd.", MIN_AVAILABLE_MEMORY);
  5735. system("echo 3 > /proc/sys/vm/drop_caches");
  5736. KillNtpd();
  5737. availableMem = getAvailableMemory() / (1024 * 1024);
  5738. LOG_INFO("AvailableMemory: %d MBytes", availableMem);
  5739. }
  5740. d = opendir("/mnt/");
  5741. if(d)
  5742. {
  5743. while((dir = readdir(d)) != NULL && !OneImageOnly)
  5744. {
  5745. if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
  5746. continue;
  5747. }
  5748. new_str = calloc(strlen("/mnt/") + strlen(dir->d_name) + 1, sizeof(char));
  5749. strcat(new_str, "/mnt/");
  5750. strcat(new_str, dir->d_name);
  5751. fd = open(new_str, O_RDONLY);
  5752. if(fd < 0)
  5753. {
  5754. close(fd);
  5755. free(new_str);
  5756. continue;
  5757. }
  5758. LOG_INFO("%s\n", new_str);
  5759. ptr = calloc(MaxLen, sizeof(char)); //-48 is take out the header
  5760. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  5761. //get the image length
  5762. ImageLen = read(fd, ptr, MaxLen);
  5763. if(ImageLen > 20)
  5764. {
  5765. memcpy(_model, ptr, 16);
  5766. Type = (((unsigned int)ptr[16]) << 24 | ((unsigned int)ptr[17]) << 16 | ((unsigned int)ptr[18]) << 8 | ((unsigned int)ptr[19]));
  5767. ImageTypeParsing(Type, _type);
  5768. LOG_INFO("Model: %s, Type: %s", _model, _type);
  5769. }
  5770. free(ptr);
  5771. if (ImageLen > 20)
  5772. {
  5773. support = false;
  5774. availableMem = getAvailableMemory() / (1024 * 1024);
  5775. LOG_INFO("AvailableMemory: %dMByte", availableMem);
  5776. //if(strcmp(model, (char *)ShmSysConfigAndInfo->SysConfig.ModelName) == EQUAL)
  5777. if(IsModelNameMatch(_model))
  5778. {
  5779. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5780. {
  5781. PutImageToAllParallelCabinet(new_str, dir->d_name);
  5782. WaitAllPCabinetStartUpgrade();
  5783. }
  5784. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.NeedUpgrade)
  5785. {
  5786. ShmChargerInfo->SCabinetControl.UpgradeState = Updating;
  5787. sleep(1);
  5788. }
  5789. switch (Type)
  5790. {
  5791. case 0x10000001:
  5792. case 0x10000002:
  5793. case 0x10000003:
  5794. case 0x10000004:
  5795. case 0x10000005:
  5796. support = true;
  5797. _UpgradeNeedReboot = TRUE;
  5798. LOG_INFO("Upgrade Flash Start...", Type);
  5799. isPass = Upgrade_Flash(Type, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5800. LOG_INFO("Upgrade Flash End, %s", isPass ? "Pass" : "Fail");
  5801. break;
  5802. case 0x10000006: // primary mcu
  5803. case 0x1000000D: // relay board
  5804. case 0x1000000E: // fan board
  5805. case 0x10000014: // led board
  5806. support = true;
  5807. target = 0x00;
  5808. if (Type == 0x10000006)
  5809. {
  5810. target = UPGRADE_PRI;
  5811. }
  5812. else if (Type == 0x1000000D)
  5813. {
  5814. target = UPGRADE_RB;
  5815. }
  5816. else if (Type == 0x1000000E)
  5817. {
  5818. target = UPGRADE_FAN;
  5819. }
  5820. else if (Type == 0x10000014)
  5821. {
  5822. target = UPGRADE_LED;
  5823. }
  5824. uartFd = InitComPort(target);
  5825. if(target == UPGRADE_RB)
  5826. {
  5827. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC1, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5828. if(isPass == PASS)
  5829. {
  5830. retSucc++;
  5831. }
  5832. else
  5833. {
  5834. retFail++;
  5835. }
  5836. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5837. if(ShmChargerInfo->Control.SysCtrl.bits.SecondRelayBoardEnable)
  5838. {
  5839. sleep(3);
  5840. isPass = Upgrade_UART(uartFd, Type, UPGRADE_DORC2, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5841. }
  5842. }
  5843. else
  5844. {
  5845. isPass = Upgrade_UART(uartFd, Type, target, new_str, (char *)ShmSysConfigAndInfo->SysConfig.ModelName);
  5846. }
  5847. close(uartFd);
  5848. break;
  5849. }
  5850. if(support)
  5851. {
  5852. if(isPass == PASS)
  5853. {
  5854. retSucc++;
  5855. }
  5856. else
  5857. {
  5858. retFail++;
  5859. }
  5860. LOG_INFO("Upgrade %s", isPass == PASS ? "Success" : "Fail");
  5861. }
  5862. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  5863. {
  5864. LOG_INFO("MCabinet upgrade completed");
  5865. WaitAllPCabinetUpgradeCompleted();
  5866. }
  5867. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE && ShmChargerInfo->SCabinetControl.UpgradeState == Updating)
  5868. {
  5869. ShmChargerInfo->SCabinetControl.UpgradeState = Updated;
  5870. LOG_INFO("SCabinet upgrade completed");
  5871. WaitMCabinetUpgradeCompleted();
  5872. }
  5873. }
  5874. else
  5875. {
  5876. if(_model[0] == 'D' && (_model[1] == 'D' || _model[1] == 'X'))
  5877. {
  5878. OneImageOnly = true;
  5879. _UpgradeNeedReboot = TRUE;
  5880. LOG_INFO("Start Put Image To Dispenser");
  5881. PutImageToAllDispenser(new_str, dir->d_name, _model);
  5882. WaitAllDispenserUpgradeCompleted();
  5883. LOG_INFO("All Dispenser Upgrade Completed");
  5884. }
  5885. }
  5886. }
  5887. else
  5888. {
  5889. retFail++;
  5890. LOG_INFO("Model Name Is Not Match: %s", _model);
  5891. }
  5892. sprintf(cmdBuf, "rm -f %s", new_str);
  5893. system(cmdBuf);
  5894. close(fd);
  5895. free(new_str);
  5896. }
  5897. }
  5898. free(dir);
  5899. closedir(d);
  5900. if(retFail != 0)
  5901. {
  5902. return FAIL;
  5903. }
  5904. return PASS;
  5905. }
  5906. void TriggerDispenserConnectionTimeoutSetting(void)
  5907. {
  5908. BOOL trigger = FALSE;
  5909. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  5910. {
  5911. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0 &&
  5912. !Is_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT))
  5913. {
  5914. trigger = TRUE;
  5915. Set_Dispenser_MiscCommand(i, MISC_DISP_CONNECTION_TIMEOUT);
  5916. }
  5917. }
  5918. if(trigger)
  5919. {
  5920. LOG_INFO("********** Trigger Dispenser Connector Timeout Value: %d **********", ShmChargerInfo->CabinetMiscValue.ConnectionTimeout);
  5921. }
  5922. }
  5923. void CheckConnectionTimeout(void)
  5924. {
  5925. if(!Is_Ocpp_Task_Alive())
  5926. {
  5927. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5928. }
  5929. else
  5930. {
  5931. if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  5932. {
  5933. _connectionTimeout = Get_Ocpp_ConnectionTimeOut();
  5934. if(_connectionTimeout <= 0)
  5935. {
  5936. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5937. }
  5938. }
  5939. else
  5940. {
  5941. _connectionTimeout = CONN_PLUG_TIME_OUT;
  5942. }
  5943. }
  5944. if(_connectionTimeout != ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  5945. {
  5946. ShmChargerInfo->CabinetMiscValue.ConnectionTimeout = _connectionTimeout;
  5947. TriggerDispenserConnectionTimeoutSetting();
  5948. }
  5949. }
  5950. void CreateTimeoutFork()
  5951. {
  5952. pid_t timeoutPid;
  5953. timeoutPid = fork();
  5954. if (timeoutPid == 0)
  5955. {
  5956. GetClockTime(&_cmdSubPriority_time);
  5957. CheckConnectionTimeout();
  5958. LOG_INFO("Timeout Fork PID = %d", getpid());
  5959. while(true)
  5960. {
  5961. if ((GetTimeoutValue(_cmdSubPriority_time) / 1000) > 5000)
  5962. {
  5963. CheckConnectionTimeout();
  5964. GetClockTime(&_cmdSubPriority_time);
  5965. }
  5966. sleep(1);
  5967. }
  5968. }
  5969. }
  5970. void CheckFactoryConfigFunction(void)
  5971. {
  5972. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  5973. {
  5974. char Buf[256];
  5975. sprintf(Buf, "cd /root;./FactoryConfig -m");
  5976. system(Buf);
  5977. system("rm -f /Storage/OCPP/OCPPConfiguration");
  5978. system("sync");
  5979. sleep(5);
  5980. system("reboot -f");
  5981. sleep(5);
  5982. system("reboot -f");
  5983. }
  5984. }
  5985. void CheckFwUpdateFunction(void)
  5986. {
  5987. char UpdateResult = PASS;
  5988. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES)
  5989. {
  5990. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  5991. UpgradePrepare();
  5992. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  5993. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  5994. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  5995. LOG_INFO("ftp : update start.");
  5996. TryCloseWatchdog();
  5997. //KillTask();
  5998. system("killall Module_PrimaryComm");
  5999. system("killall Module_InternalComm");
  6000. UpdateResult = CheckUpdateProcess();
  6001. if (UpdateResult == PASS)
  6002. {
  6003. LOG_INFO("ftp : update complete.");
  6004. }
  6005. else
  6006. {
  6007. LOG_INFO("ftp : update fail.");
  6008. }
  6009. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = NO;
  6010. sleep(10);
  6011. if(_UpgradeNeedReboot)
  6012. {
  6013. system("reboot -f");
  6014. }
  6015. else
  6016. {
  6017. KillAllTask();
  6018. system("/usr/bin/run_evse_restart.sh");
  6019. }
  6020. }
  6021. else if(Is_Ocpp_UpdateFirmwareReq())
  6022. {
  6023. Clean_Ocpp_UpdateFirmwareReq();
  6024. ShmChargerInfo->Control.SysCtrl.bits.NeedSelfTest = false;
  6025. UpgradePrepare();
  6026. ShmChargerInfo->Control.PrimaryCtrl.bits.Paused = true;
  6027. ShmChargerInfo->Control.RelayCtrl.bits.Paused = true;
  6028. ShmChargerInfo->Control.FanCtrl.bits.Paused = true;
  6029. if(Is_Ocpp_UpdateFirmwareStatus("Downloaded"))
  6030. {
  6031. LOG_INFO("Backend : update start.");
  6032. TryCloseWatchdog();
  6033. Set_Ocpp_UpdateFirmwareStatus("Installing");
  6034. Set_Ocpp_FirmwareStatusNotificationReq();
  6035. //KillTask();
  6036. system("killall Module_PrimaryComm");
  6037. system("killall Module_InternalComm");
  6038. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  6039. {
  6040. setChargerMode(_index, MODE_UPDATE);
  6041. }
  6042. UpdateResult = CheckUpdateProcess();
  6043. if (UpdateResult == PASS)
  6044. {
  6045. LOG_INFO("Backend : update complete.");
  6046. Set_Ocpp_UpdateFirmwareStatus("Installed");
  6047. }
  6048. else
  6049. {
  6050. LOG_INFO("Backend : update fail.");
  6051. Set_Ocpp_UpdateFirmwareStatus("InstallationFailed");
  6052. }
  6053. Set_Ocpp_FirmwareStatusNotificationReq();
  6054. sleep(10);
  6055. if(_UpgradeNeedReboot)
  6056. {
  6057. system("reboot -f");
  6058. }
  6059. else
  6060. {
  6061. KillAllTask();
  6062. system("/usr/bin/run_evse_restart.sh");
  6063. }
  6064. }
  6065. }
  6066. }
  6067. void ExecuteWriteFlash(void)
  6068. {
  6069. struct SysConfigData config;
  6070. memcpy(&config, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  6071. config.SwitchDebugFlag = NO;
  6072. strcpy((char *) config.UserId, "");
  6073. int result = StoreUsrConfigData(&config);
  6074. if(result != 1)
  6075. {
  6076. LOG_INFO("Store User Config NG");
  6077. }
  6078. else
  6079. {
  6080. LOG_INFO("Store User Config OK");
  6081. }
  6082. }
  6083. struct timespec _WiringInfo_time;
  6084. int CheckFlashUpdate(void)
  6085. {
  6086. if(ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged)
  6087. {
  6088. ShmChargerInfo->Control.SysCtrl.bits.FlashConfigChanged = false;
  6089. LOG_INFO("Flash Config Changed");
  6090. GetClockTime(&_WiringInfo_time);
  6091. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = true;
  6092. }
  6093. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable)
  6094. {
  6095. // wait for 30s to write flash or write flash immediately
  6096. if((GetTimeoutValue(_WiringInfo_time) / uSEC_VAL) >= WIRING_INFO_DELAY ||
  6097. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashImmediately)
  6098. {
  6099. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashEnable = false;
  6100. LOG_INFO("Start Writing Flash");
  6101. pid_t writePid = fork();
  6102. if(writePid == 0)
  6103. {
  6104. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = true;
  6105. ExecuteWriteFlash();
  6106. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashBusy = false;
  6107. if(ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset)
  6108. {
  6109. ShmChargerInfo->Control.SysCtrl.bits.WriteFlashAndReset = false;
  6110. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  6111. LOG_INFO("Write Flash OK And Need Soft Reset");
  6112. }
  6113. return NO;
  6114. }
  6115. LOG_INFO("Write Flash Conpleted");
  6116. }
  6117. }
  6118. return YES;
  6119. }
  6120. void CheckSoftHardReset(void)
  6121. {
  6122. if(ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset)
  6123. {
  6124. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = false;
  6125. LOG_INFO("********** Need Soft Reset **********");
  6126. sleep(5);
  6127. Kill_Ocpp_Service();
  6128. KillAllTask();
  6129. TryCloseWatchdog();
  6130. system("/usr/bin/run_evse_restart.sh");
  6131. }
  6132. if(ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset)
  6133. {
  6134. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = false;
  6135. LOG_INFO("********** Need Hard Reset **********");
  6136. sleep(5);
  6137. system("reboot -f");
  6138. }
  6139. }
  6140. struct timespec _Reboot_time;
  6141. void TriggerDispenserHardwareReboot(void)
  6142. {
  6143. BOOL trigger = FALSE;
  6144. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6145. {
  6146. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6147. {
  6148. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm == 0 &&
  6149. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  6150. {
  6151. trigger = TRUE;
  6152. Set_Dispenser_MiscCommand(i, MISC_DISP_HARDWARE_REBOOT);
  6153. }
  6154. }
  6155. }
  6156. if(trigger)
  6157. {
  6158. GetClockTime(&_Reboot_time);
  6159. LOG_INFO("********** Trigger Dispenser Hardware Reboot **********");
  6160. }
  6161. }
  6162. BOOL IsDispenserHardwareRebootResponsed(void)
  6163. {
  6164. BOOL response_ok = TRUE;
  6165. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6166. {
  6167. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6168. {
  6169. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.HardwareRebootConfirm)
  6170. {
  6171. response_ok = FALSE;
  6172. // 10s for reboot request timeout
  6173. if((GetTimeoutValue(_Reboot_time) / uSEC_VAL) >= REBOOT_TIMEOUT)
  6174. {
  6175. response_ok = TRUE;
  6176. LOG_INFO("Reboot request timeout");
  6177. }
  6178. }
  6179. }
  6180. }
  6181. return response_ok;
  6182. }
  6183. struct timespec _Reset_time;
  6184. void TriggerDispenserSoftwareReset(void)
  6185. {
  6186. BOOL trigger = FALSE;
  6187. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6188. {
  6189. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6190. {
  6191. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm == 0 &&
  6192. !Is_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET))
  6193. {
  6194. trigger = TRUE;
  6195. Set_Dispenser_MiscCommand(i, MISC_DISP_SOFTWARE_RESET);
  6196. }
  6197. }
  6198. }
  6199. if(trigger)
  6200. {
  6201. GetClockTime(&_Reset_time);
  6202. LOG_INFO("********** Trigger Dispenser Software Reset **********");
  6203. }
  6204. }
  6205. BOOL IsDispenserSoftwareResetResponsed(void)
  6206. {
  6207. BOOL response_ok = TRUE;
  6208. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6209. {
  6210. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6211. {
  6212. if(!ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.SoftwareResetConfirm)
  6213. {
  6214. response_ok = FALSE;
  6215. // 10s for reset request timeout
  6216. if((GetTimeoutValue(_Reset_time) / uSEC_VAL) >= RESET_TIMEOUT)
  6217. {
  6218. response_ok = TRUE;
  6219. LOG_INFO("Reset request timeout");
  6220. }
  6221. }
  6222. }
  6223. }
  6224. return response_ok;
  6225. }
  6226. // return time offset
  6227. int TimeOffsetKeyParsing(char *StringItem)
  6228. {
  6229. int offset = 0;
  6230. int hour = 0, minute = 0, sign = 1;
  6231. char *ptrSave, *ptrToken;
  6232. char strSource[501];
  6233. memset(strSource, 0x00, sizeof(strSource));
  6234. strcpy(strSource, StringItem);
  6235. if(strSource[0] != '+' && strSource[0] != '-')
  6236. {
  6237. return offset;
  6238. }
  6239. sign = strSource[0] == '+' ? (1) : (-1);
  6240. ptrToken = strtok_r(&strSource[1], ":", &ptrSave);
  6241. if(ptrToken != NULL)
  6242. {
  6243. hour = atoi(ptrToken);
  6244. ptrToken = strtok_r(NULL, ":", &ptrSave);
  6245. if(ptrToken != NULL)
  6246. {
  6247. minute = atoi(ptrToken);
  6248. offset = ((hour * 60) + minute) * sign;
  6249. }
  6250. }
  6251. return offset;
  6252. }
  6253. char MyDefaultPriceString[501];
  6254. void DefaultPriceHandler(void)
  6255. {
  6256. char price[501];
  6257. char defaultPrice[512];
  6258. memset(defaultPrice, 0x00, sizeof(defaultPrice));
  6259. Get_Ocpp_DefaultPrice((char *)price);
  6260. if(strcmp(MyDefaultPriceString, (char *)price) != EQUAL)
  6261. {
  6262. float ConnectionFee = 0, OccupancyFee = 0;
  6263. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6264. {
  6265. ConnectionFee = PriceParsing(price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6266. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = PriceParsing(price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6267. OccupancyFee = PriceParsing(price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6268. LOG_INFO("******** Customer Audi Default Price *******");
  6269. LOG_INFO("********** Connection Fee: %4.2f **********", ConnectionFee);
  6270. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6271. LOG_INFO("********** Occupancy Fee: %4.2f **********", OccupancyFee);
  6272. }
  6273. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6274. {
  6275. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6276. ShellPriceParsing(price, defaultPrice);
  6277. LOG_INFO("******* Customer Shell Default Price *******");
  6278. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6279. }
  6280. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6281. {
  6282. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = TccDefaultPriceParsing(price, &ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6283. LOG_INFO("******* Customer Tcc Default Price *******");
  6284. LOG_INFO("*** TimeOfDayPricing: %4.2f", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6285. }
  6286. else
  6287. {
  6288. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = DefaultPriceParsing(price);
  6289. strcpy(defaultPrice, price);
  6290. LOG_INFO("********** Standard Default Price **********");
  6291. LOG_INFO("********** Current Rate: %4.2f **********", ShmChargerInfo->PriceAndReceiptInfo.CurrentRate);
  6292. LOG_INFO("*** Default Price: [%s]", defaultPrice);
  6293. }
  6294. strcpy(MyDefaultPriceString, price);
  6295. }
  6296. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  6297. {
  6298. ShmChargerInfo->PriceAndReceiptInfo.CurrentRate = GetNowTimePricing(&ShmChargerInfo->PriceAndReceiptInfo.TimeOfDayPricing[0]);
  6299. }
  6300. if(ShmChargerInfo->CabinetMiscValue.DefaultPrice != ShmChargerInfo->PriceAndReceiptInfo.CurrentRate)
  6301. {
  6302. ShmChargerInfo->CabinetMiscValue.DefaultPrice = ShmChargerInfo->PriceAndReceiptInfo.CurrentRate;
  6303. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6304. {
  6305. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6306. {
  6307. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE))
  6308. {
  6309. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE);
  6310. }
  6311. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY))
  6312. {
  6313. Set_Dispenser_MiscCommand(i, MISC_DISP_CURRENCY);
  6314. }
  6315. }
  6316. }
  6317. LOG_INFO("********** Trigger Dispenser Default Price Value **********");
  6318. }
  6319. if(strlen(defaultPrice) > 0)
  6320. {
  6321. strcpy(ShmChargerInfo->PriceAndReceiptInfo.DefaultPriceString, defaultPrice);
  6322. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6323. {
  6324. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6325. {
  6326. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING))
  6327. {
  6328. Set_Dispenser_MiscCommand(i, MISC_DISP_DEFAULT_PRICE_STRING);
  6329. }
  6330. }
  6331. }
  6332. LOG_INFO("********** Trigger Dispenser Default Price Value String **********");
  6333. }
  6334. }
  6335. void UserPriceHandler(void)
  6336. {
  6337. char UserId[32];
  6338. char price[500];
  6339. char userPrice[512];
  6340. memset(userPrice, 0x00, sizeof(userPrice));
  6341. Get_Ocpp_UserPrice(UserId, price);
  6342. if(strlen((char *)UserId) > 0 && strlen((char *)price) > 0)
  6343. {
  6344. float ConnectionFee = 0, CurrentRate = 0, OccupancyFee = 0;
  6345. float AccountBalance = 0;
  6346. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6347. {
  6348. ConnectionFee = PriceParsing((char *)price, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6349. CurrentRate = PriceParsing((char *)price, "Current Rate", _AUDI_PRICE_SPLIT_KEY);
  6350. OccupancyFee = PriceParsing((char *)price, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6351. AccountBalance = PriceParsing((char *)price, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6352. LOG_INFO("********* Customer Audi User Price *********");
  6353. LOG_INFO("********* User Id: [%s]", UserId);
  6354. LOG_INFO("********* Connection Fee: [%7.2f]", ConnectionFee);
  6355. LOG_INFO("********* Current Rate: [%7.2f]", CurrentRate);
  6356. LOG_INFO("********* Occupancy Fee: [%7.2f]", OccupancyFee);
  6357. LOG_INFO("********* Account Balance: [%7.2f]", AccountBalance);
  6358. }
  6359. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6360. {
  6361. ShellPriceParsing(price, userPrice);
  6362. LOG_INFO("******** Customer Shell User Price *********");
  6363. LOG_INFO("******** User Id: [%s]", UserId);
  6364. LOG_INFO("******** User Price: [%s]", userPrice);
  6365. }
  6366. else
  6367. {
  6368. CurrentRate = PriceParsing((char *)price, "chargineFee", _PHIHONG_PRICE_SPLIT_KEY);
  6369. OccupancyFee = PriceParsing((char *)price, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6370. strcpy(userPrice, price);
  6371. LOG_INFO("*********** Standard User Price ************");
  6372. LOG_INFO("*********** User Id: [%s]", UserId);
  6373. LOG_INFO("*********** Current Rate: [%7.2f]", CurrentRate);
  6374. LOG_INFO("*********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6375. LOG_INFO("*********** User Price: [%s]", userPrice);
  6376. }
  6377. #if 0
  6378. LOG_INFO("********** User Id: %s **********", UserId);
  6379. LOG_INFO("********** User Price: %s", price);
  6380. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6381. LOG_INFO("********** Current Rate: %7.2f **********", CurrentRate);
  6382. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6383. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6384. #endif
  6385. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6386. {
  6387. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.StartUserId, UserId) == EQUAL)
  6388. {
  6389. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice != CurrentRate ||
  6390. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance != AccountBalance)
  6391. {
  6392. LOG_INFO("********** Connector Id %d User Price %4.2f, Account Balance %7.2f **********",
  6393. (i + 1), CurrentRate, AccountBalance);
  6394. }
  6395. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].UserPrice = CurrentRate;
  6396. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].AccountBalance = AccountBalance;
  6397. if(strlen(userPrice) > 0)
  6398. {
  6399. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[i][0], userPrice);
  6400. Set_Connector_MiscCommand(i, MISC_CONN_USER_PRICE_STRING);
  6401. }
  6402. if(chargingInfo[i]->SystemStatus == S_AUTHORIZING)
  6403. {
  6404. AnnounceAccountBalance(i, AccountBalance);
  6405. }
  6406. }
  6407. }
  6408. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6409. {
  6410. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthResult == _AuthResult_Valid)
  6411. {
  6412. if(strcmp((char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].AuthInfo.AuthId, UserId) == EQUAL)
  6413. {
  6414. if(strlen(userPrice) > 0)
  6415. {
  6416. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[i][0], userPrice);
  6417. }
  6418. }
  6419. }
  6420. }
  6421. Clean_Ocpp_UserPrice();
  6422. }
  6423. }
  6424. void RunningCostParsing(int gunIndex, char *strCost)
  6425. {
  6426. float ConnectionFee = 0, SessionFee = 0, OccupancyFee = -1, TotalCost = 0, EnergyCost = -1;
  6427. float AccountBalance = 0;
  6428. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6429. {
  6430. ConnectionFee = PriceParsing((char *)strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6431. SessionFee = PriceParsing((char *)strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6432. OccupancyFee = PriceParsing((char *)strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6433. TotalCost = PriceParsing((char *)strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6434. AccountBalance = PriceParsing((char *)strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6435. LOG_INFO("******** Customer Audi Gun %d Running Cost ********", (gunIndex + 1));
  6436. LOG_INFO("******** Connection Fee: [%7.2f]", ConnectionFee);
  6437. LOG_INFO("******** Session Fee: [%7.2f]", SessionFee);
  6438. LOG_INFO("******** Occupancy Fee: [%7.2f]", OccupancyFee);
  6439. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6440. LOG_INFO("******** Account Balance: [%7.2f]", AccountBalance);
  6441. }
  6442. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6443. {
  6444. TotalCost = ShellRunningCostParsing(strCost);
  6445. LOG_INFO("******** Customer Shell Gun %d Running Cost *******", (gunIndex + 1));
  6446. LOG_INFO("******** Gun %d Running Cost", (gunIndex + 1));
  6447. LOG_INFO("******** Total Cost: [%7.2f]", TotalCost);
  6448. }
  6449. else
  6450. {
  6451. EnergyCost = PriceParsing((char *)strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6452. TotalCost = EnergyCost;
  6453. OccupancyFee = PriceParsing((char *)strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6454. LOG_INFO("********** Standard Gun %d Running Cost ***********", (gunIndex + 1));
  6455. LOG_INFO("********** Energy Cost: [%7.2f]", EnergyCost);
  6456. LOG_INFO("********** Occupancy Fee: [%7.2f]", OccupancyFee);
  6457. LOG_INFO("********** Total Cost: [%7.2f]", TotalCost);
  6458. }
  6459. #if 0
  6460. LOG_INFO("********** Connector Id: %d Running Cost **********", (gunIndex + 1));
  6461. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6462. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6463. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6464. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6465. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6466. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6467. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance)
  6468. {
  6469. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance %7.2f **********",
  6470. (gunIndex + 1), TotalCost, AccountBalance);
  6471. }
  6472. #endif
  6473. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = TotalCost;
  6474. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = AccountBalance;
  6475. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = EnergyCost;
  6476. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = OccupancyFee;
  6477. }
  6478. void RunningCostHandler(void)
  6479. {
  6480. char strCost[256];
  6481. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6482. {
  6483. Get_Ocpp_RunningCost(i, strCost);
  6484. if(strlen((char *)strCost) > 0)
  6485. {
  6486. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus >= S_REASSIGN_CHECK &&
  6487. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.SystemStatus <= S_CHARGING)
  6488. {
  6489. RunningCostParsing(i, strCost);
  6490. }
  6491. Clean_Ocpp_RunningCost(i);
  6492. }
  6493. }
  6494. }
  6495. void EnableConnectorBillRequest(int gunIndex, ChargingBillInfoData *bill)
  6496. {
  6497. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6498. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6499. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = bill->TxId;
  6500. memcpy(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, &bill->FeeInfo, sizeof(FeeInfoData));
  6501. }
  6502. void DisableConnectorBill(int gunIndex)
  6503. {
  6504. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Idle;
  6505. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt = 0;
  6506. ShmChargerInfo->AllBill.GunBill[gunIndex].TxId = 0;
  6507. memset(&ShmChargerInfo->AllBill.GunBill[gunIndex].FeeInfo, 0x00, sizeof(FeeInfoData));
  6508. }
  6509. void FinalCostParsing(int txId, char *strCost, ChargingBillInfoData *_bill)
  6510. {
  6511. memset(_bill, 0x00, sizeof(ChargingBillInfoData));
  6512. _bill->TxId = txId;
  6513. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6514. {
  6515. _bill->FeeInfo.ConnectionFee = PriceParsing(strCost, "Connection Fee", _AUDI_PRICE_SPLIT_KEY);
  6516. _bill->FeeInfo.SessionFee = PriceParsing(strCost, "Session Fee", _AUDI_PRICE_SPLIT_KEY);
  6517. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "Occupancy Fee", _AUDI_PRICE_SPLIT_KEY);
  6518. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "Total Cost", _AUDI_PRICE_SPLIT_KEY);
  6519. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "Account Balance", _AUDI_PRICE_SPLIT_KEY);
  6520. }
  6521. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6522. {
  6523. _bill->FeeInfo.TotalCost = ShellFinalCostParsing(strCost);
  6524. GetShellReceiptInfo(strCost, (char *)_bill->Receipt);
  6525. }
  6526. else
  6527. {
  6528. _bill->FeeInfo.TotalCost = PriceParsing(strCost, "chargingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6529. _bill->FeeInfo.ParkingFee = PriceParsing(strCost, "parkingFee", _PHIHONG_PRICE_SPLIT_KEY);
  6530. _bill->FeeInfo.AccountBalance = PriceParsing(strCost, "accountBalance", _PHIHONG_PRICE_SPLIT_KEY);
  6531. _bill->FeeInfo.CostDiscount = PriceParsing(strCost, "couponPoint", _PHIHONG_PRICE_SPLIT_KEY);
  6532. _bill->FeeInfo.RemainAmount = PriceParsing(strCost, "farewellMessage", _PHIHONG_PRICE_SPLIT_KEY);
  6533. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0)
  6534. {
  6535. sprintf(_bill->Receipt, "%s%d",
  6536. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0],
  6537. txId);
  6538. }
  6539. }
  6540. }
  6541. void UpdateCurrentFinalCost(int gunIndex, ChargingBillInfoData *_bill)
  6542. {
  6543. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Audi)
  6544. {
  6545. LOG_INFO("********* Customer Audi Gun %d Final Cost *********", (gunIndex + 1));
  6546. LOG_INFO("********* Connection Fee: [%7.2f]", _bill->FeeInfo.ConnectionFee);
  6547. LOG_INFO("********* Session Fee: [%7.2f]", _bill->FeeInfo.SessionFee);
  6548. LOG_INFO("********* Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6549. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6550. LOG_INFO("********* Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6551. }
  6552. else if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_Shell)
  6553. {
  6554. LOG_INFO("********* Customer Shell Gun %d Final Cost ********", (gunIndex + 1));
  6555. LOG_INFO("********* Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6556. LOG_INFO("********* Receipt: [%s]", _bill->Receipt);
  6557. }
  6558. else
  6559. {
  6560. LOG_INFO("*********** Standard Gun %d Final Cost ************", (gunIndex + 1));
  6561. LOG_INFO("*********** Occupancy Fee: [%7.2f]", _bill->FeeInfo.ParkingFee);
  6562. LOG_INFO("*********** Total Cost: [%7.2f]", _bill->FeeInfo.TotalCost);
  6563. LOG_INFO("*********** Account Balance: [%7.2f]", _bill->FeeInfo.AccountBalance);
  6564. LOG_INFO("*********** Coupon Point: [%7.2f]", _bill->FeeInfo.CostDiscount);
  6565. LOG_INFO("*********** Remain Amount: [%7.2f]", _bill->FeeInfo.RemainAmount);
  6566. LOG_INFO("*********** Receipt: [%s]", _bill->Receipt);
  6567. }
  6568. #if 0
  6569. LOG_INFO("********** Connector Id: %d Final Cost **********", (gunIndex + 1));
  6570. LOG_INFO("********** Connection Fee: %7.2f **********", ConnectionFee);
  6571. LOG_INFO("********** Session Fee: %7.2f **********", SessionFee);
  6572. LOG_INFO("********** Occupancy Fee: %7.2f **********", OccupancyFee);
  6573. LOG_INFO("********** Total Cost: %7.2f **********", TotalCost);
  6574. LOG_INFO("********** Account Balance: %7.2f **********", AccountBalance);
  6575. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost != TotalCost ||
  6576. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance != AccountBalance ||
  6577. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount != CostDiscount)
  6578. {
  6579. LOG_INFO("********** Connector Id %d Total Cost %.2f, Account Balance: %7.2f, Discount: %d.%02d **********",
  6580. (gunIndex + 1), TotalCost, AccountBalance, (CostDiscount / 100), (CostDiscount % 100));
  6581. }
  6582. #endif
  6583. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].TotalCost = _bill->FeeInfo.TotalCost;
  6584. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].AccountBalance = _bill->FeeInfo.AccountBalance;
  6585. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].CostDiscount = _bill->FeeInfo.CostDiscount;
  6586. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].EnergyCost = _bill->FeeInfo.EnergyCost;
  6587. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParkingFee = _bill->FeeInfo.ParkingFee;
  6588. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemainAmount = _bill->FeeInfo.RemainAmount;
  6589. Set_Connector_MiscCommand(gunIndex, MISC_CONN_FINAL_COST);
  6590. if(strlen(_bill->Receipt) > 0)
  6591. {
  6592. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gunIndex][0], _bill->Receipt);
  6593. Set_Connector_MiscCommand(gunIndex, MISC_CONN_RECEIPT);
  6594. }
  6595. DB_Update_FinalCost_Status(localDb, _bill->TxId, BILL_SATAUS_Bypass_OK);
  6596. }
  6597. bool IsBillStatusChanged(int gunIndex)
  6598. {
  6599. return ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus == ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus ?
  6600. false : true;
  6601. }
  6602. void SyncBillStatus(int gunIndex)
  6603. {
  6604. ShmChargerInfo->AllBill.GunBill[gunIndex].PreStatus = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6605. }
  6606. void EnableUndisposedBill(int dispenserIndex)
  6607. {
  6608. LOG_INFO("Enable Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6609. _isUndisposedFinalCost[dispenserIndex] = true;
  6610. }
  6611. void FindUndisposedAndTrigger(int dispenserIndex)
  6612. {
  6613. int txId = 0;
  6614. char strCost[256];
  6615. ChargingBillInfoData _bill;
  6616. bool find = false;
  6617. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6618. {
  6619. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6620. txId = DB_Get_Undisposed_FinalCost(localDb, gun, strCost);
  6621. if(txId >= 0)
  6622. {
  6623. LOG_INFO("Find Gun %d Undisposed ChargingBill, txId: [%d]", gun + 1, txId);
  6624. FinalCostParsing(txId, (char *)strCost, &_bill);
  6625. EnableConnectorBillRequest(gun, &_bill);
  6626. find = true;
  6627. break;
  6628. }
  6629. }
  6630. if(!find)
  6631. {
  6632. LOG_INFO("Dispenser %d Undisposed ChargingBill Not Found", dispenserIndex + 1);
  6633. _isUndisposedFinalCost[dispenserIndex] = false;
  6634. }
  6635. }
  6636. bool IsDispenserBillStatusBusy(int dispenserIndex)
  6637. {
  6638. bool busy = false;
  6639. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  6640. {
  6641. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  6642. if(ShmChargerInfo->AllBill.GunBill[gun].BillStatus != _BillStatus_Idle)
  6643. {
  6644. busy = true;
  6645. break;
  6646. }
  6647. }
  6648. return busy;
  6649. }
  6650. void DispenserBillRequestProcess(int dispenserIndex)
  6651. {
  6652. if(IsDispenserIdle(dispenserIndex) && !IsDispenserBillStatusBusy(dispenserIndex))
  6653. {
  6654. if(!_canUpdateFinalCost[dispenserIndex])
  6655. {
  6656. _canUpdateFinalCost[dispenserIndex] = true;
  6657. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6658. }
  6659. if(_canUpdateFinalCost[dispenserIndex] && _isUndisposedFinalCost[dispenserIndex])
  6660. {
  6661. if(GetSecTimeoutValue(_DispenserBill_Time[dispenserIndex]) >= FINAL_COST_UPDATE_TIME)
  6662. {
  6663. //LOG_INFO("Try Find Dispenser %d Undisposed ChargingBill", dispenserIndex + 1);
  6664. FindUndisposedAndTrigger(dispenserIndex);
  6665. GetClockTime(&_DispenserBill_Time[dispenserIndex]);
  6666. }
  6667. }
  6668. }
  6669. else
  6670. {
  6671. _canUpdateFinalCost[dispenserIndex] = false;
  6672. }
  6673. }
  6674. void GunBillRequestProcess(int gunIndex)
  6675. {
  6676. unsigned char _status = ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus;
  6677. switch(_status)
  6678. {
  6679. case _BillStatus_Idle:
  6680. if(IsBillStatusChanged(gunIndex))
  6681. {
  6682. SyncBillStatus(gunIndex);
  6683. }
  6684. break;
  6685. case _BillStatus_MiscReq:
  6686. if(IsBillStatusChanged(gunIndex))
  6687. {
  6688. Database_INFO("Gun %d ChargingBill MiscRequest", gunIndex + 1);
  6689. SyncBillStatus(gunIndex);
  6690. }
  6691. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_WaitMiscClean;
  6692. Set_Connector_MiscCommand(gunIndex, MISC_CONN_CHARGING_BILL);
  6693. break;
  6694. case _BillStatus_WaitMiscClean:
  6695. if(IsBillStatusChanged(gunIndex))
  6696. {
  6697. SyncBillStatus(gunIndex);
  6698. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6699. }
  6700. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6701. {
  6702. Database_INFO("Gun %d ChargingBill WaitMisc Timeout", gunIndex + 1);
  6703. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6704. }
  6705. break;
  6706. case _BillStatus_MiscClean:
  6707. if(IsBillStatusChanged(gunIndex))
  6708. {
  6709. Database_INFO("Gun %d ChargingBill MiscClean", gunIndex + 1);
  6710. SyncBillStatus(gunIndex);
  6711. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6712. }
  6713. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6714. {
  6715. Database_INFO("Gun %d ChargingBill Wait Dispenser Timeout", gunIndex + 1);
  6716. ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt++;
  6717. if(ShmChargerInfo->AllBill.GunBill[gunIndex].TryCnt >= MAX_FINAL_COST_MISC_COUNT)
  6718. {
  6719. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Fail;
  6720. }
  6721. else
  6722. {
  6723. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_MiscReq;
  6724. }
  6725. }
  6726. break;
  6727. case _BillStatus_UpdateDone:
  6728. if(IsBillStatusChanged(gunIndex))
  6729. {
  6730. Database_INFO("Gun %d ChargingBill UpdateDone", gunIndex + 1);
  6731. SyncBillStatus(gunIndex);
  6732. }
  6733. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_Completed;
  6734. break;
  6735. case _BillStatus_Completed:
  6736. if(IsBillStatusChanged(gunIndex))
  6737. {
  6738. SyncBillStatus(gunIndex);
  6739. GetClockTime(&_ConnectorBill_Time[gunIndex]);
  6740. }
  6741. if((GetTimeoutValue(_ConnectorBill_Time[gunIndex]) / uSEC_VAL) >= FINAL_COST_TIMEOUT)
  6742. {
  6743. ShmChargerInfo->AllBill.GunBill[gunIndex].BillStatus = _BillStatus_End;
  6744. }
  6745. break;
  6746. case _BillStatus_Fail:
  6747. if(IsBillStatusChanged(gunIndex))
  6748. {
  6749. SyncBillStatus(gunIndex);
  6750. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_FAIL);
  6751. }
  6752. DisableConnectorBill(gunIndex);
  6753. break;
  6754. case _BillStatus_End:
  6755. if(IsBillStatusChanged(gunIndex))
  6756. {
  6757. SyncBillStatus(gunIndex);
  6758. DB_Update_FinalCost_Status(localDb, ShmChargerInfo->AllBill.GunBill[gunIndex].TxId, BILL_SATAUS_UPDATED_OK);
  6759. }
  6760. DisableConnectorBill(gunIndex);
  6761. break;
  6762. default:
  6763. DisableConnectorBill(gunIndex);
  6764. break;
  6765. }
  6766. }
  6767. void FinalCostHandler(void)
  6768. {
  6769. int txId = 0;
  6770. char strCost[256];
  6771. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6772. {
  6773. txId = Get_Ocpp_FinalCostTxId(i);
  6774. Get_Ocpp_FinalCost(i, strCost);
  6775. if(strlen((char *)strCost) > 0)
  6776. {
  6777. DB_Insert_FinalCost(localDb, i, txId, strCost);
  6778. Database_INFO("Insert FinalCost: Gun %d TxId[%d]", i + 1, txId);
  6779. if(ShmChargerInfo->UserTransaction[i].TransactionId == txId)
  6780. {
  6781. ChargingBillInfoData _bill;
  6782. FinalCostParsing(txId, (char *)strCost, &_bill);
  6783. UpdateCurrentFinalCost(i, &_bill);
  6784. }
  6785. else
  6786. {
  6787. EnableUndisposedBill(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].ParentDispensetIndex);
  6788. }
  6789. Clean_Ocpp_FinalCost(i);
  6790. }
  6791. }
  6792. if(ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill)
  6793. {
  6794. ShmChargerInfo->Control.TestCtrl.bits.TryUndisposedBill = false;
  6795. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6796. {
  6797. _isUndisposedFinalCost[i] = true;
  6798. }
  6799. }
  6800. for(int i = 0; i < MAX_DISPENSER_QUANTITY; i++)
  6801. {
  6802. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  6803. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  6804. {
  6805. DispenserBillRequestProcess(i);
  6806. }
  6807. else
  6808. {
  6809. _canUpdateFinalCost[i] = false;
  6810. }
  6811. }
  6812. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6813. {
  6814. GunBillRequestProcess(i);
  6815. }
  6816. }
  6817. char MyTimeOffsetString[501];
  6818. void TimeOffsetHandler(void)
  6819. {
  6820. char timeOffset[501];
  6821. Get_Ocpp_TimeOffset((char *)timeOffset);
  6822. if(strcmp(MyTimeOffsetString, (char *)timeOffset) != EQUAL)
  6823. {
  6824. BOOL trigger = FALSE;
  6825. int offset = 0;
  6826. offset = TimeOffsetKeyParsing(timeOffset);
  6827. LOG_INFO("********** Time Offset String [%s] -> Value [%d] -> Time [%s%02d:%02d]",
  6828. timeOffset, offset, offset >= 0 ? "+" : "-", (abs(offset) / 60), (abs(offset) % 60));
  6829. ShmChargerInfo->CabinetMiscValue.TimeOffset = offset;
  6830. #if 0
  6831. time_t timep;
  6832. struct tm *tm;
  6833. time(&timep);
  6834. tm = localtime(&timep);
  6835. LOG_INFO(" NowTime: %d/%02d/%02d %02d:%02d:%02d",
  6836. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  6837. timep += (offset * 60);
  6838. tm = localtime(&timep);
  6839. LOG_INFO("OffsetTime: %d/%02d/%02d %02d:%02d:%02d",
  6840. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  6841. #endif
  6842. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6843. {
  6844. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity > 0)
  6845. {
  6846. if(!Is_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET))
  6847. {
  6848. trigger = TRUE;
  6849. Set_Dispenser_MiscCommand(i, MISC_DISP_TIME_OFFSET);
  6850. }
  6851. }
  6852. }
  6853. if(trigger)
  6854. {
  6855. LOG_INFO("********** Trigger Dispenser Time Offset **********");
  6856. }
  6857. strcpy(MyTimeOffsetString, timeOffset);
  6858. }
  6859. }
  6860. struct timespec _Cost_Price_time;
  6861. void CheckOcppCostAndPrice(void)
  6862. {
  6863. // 5s for reboot request timeout
  6864. if((GetTimeoutValue(_Cost_Price_time) / uSEC_VAL) >= OCPP_COST_REQ_INTERVAL)
  6865. {
  6866. GetClockTime(&_Cost_Price_time);
  6867. DefaultPriceHandler();
  6868. UserPriceHandler();
  6869. RunningCostHandler();
  6870. TimeOffsetHandler();
  6871. }
  6872. FinalCostHandler();
  6873. }
  6874. void CheckRemoteStartNoIdRequest(void)
  6875. {
  6876. if(Is_Ocpp_RemoteStartWaitReq())
  6877. {
  6878. if(!ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6879. {
  6880. LOG_INFO("Ocpp remote start without connector id is trigger");
  6881. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = true;
  6882. AnnounceRemoteStartNoID();
  6883. GetClockTime(&_RemoteStartNoId_Time);
  6884. }
  6885. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6886. {
  6887. // to avoid keep RemoteStartNoId state when ocpp is disconnected
  6888. if((GetTimeoutValue(_RemoteStartNoId_Time) / uSEC_VAL) >= (ShmChargerInfo->CabinetMiscValue.ConnectionTimeout + CONNECTION_TIME_OFFSET))
  6889. {
  6890. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  6891. }
  6892. }
  6893. }
  6894. else
  6895. {
  6896. if(ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId)
  6897. {
  6898. LOG_INFO("Ocpp remote start without connector id is expired");
  6899. }
  6900. ShmChargerInfo->Control.OcppCtrl.bits.RemoteStartNoId = false;
  6901. }
  6902. }
  6903. void CheckOcppPageChangeRequest(void)
  6904. {
  6905. int gunId = 0, pageIndex = 0;
  6906. if(Is_Ocpp_LcdOverideReq(&gunId, &pageIndex))
  6907. {
  6908. Clean_Ocpp_LcdOverideReq();
  6909. LOG_INFO("Gun %d Ocpp Line Status [%d] is trigger", gunId, pageIndex);
  6910. AnnounceLineStatusCode(gunId - 1, pageIndex);
  6911. }
  6912. }
  6913. void CheckLcmPageChangeRequest(void)
  6914. {
  6915. bool isTrigger = false;
  6916. CheckRemoteStartNoIdRequest();
  6917. CheckOcppPageChangeRequest();
  6918. for(int index = 0; index < CONNECTOR_QUANTITY; index++)
  6919. {
  6920. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_None &&
  6921. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].LocalStatus != _DS_Timeout)
  6922. {
  6923. isTrigger = false;
  6924. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorQuantity; i++)
  6925. {
  6926. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[index].ConnectorID[i] - 1;
  6927. if(ShmChargerInfo->ConnectorPageReq[gun].CtrlValue != 0)
  6928. {
  6929. isTrigger = true;
  6930. break;
  6931. }
  6932. }
  6933. if(isTrigger)
  6934. {
  6935. Set_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  6936. }
  6937. else
  6938. {
  6939. Clean_Dispenser_MiscCommand(index, MISC_DISP_CHANGE_LCM_INFO);
  6940. }
  6941. }
  6942. }
  6943. }
  6944. void CheckStationInfo(void)
  6945. {
  6946. int station_id = 0;
  6947. char station_name[64];
  6948. if(Is_Ocpp_ChargerStationReq())
  6949. {
  6950. memset(station_name, 0x00, sizeof(station_name));
  6951. Get_Ocpp_ChargerStationInfo(&station_id, station_name);
  6952. if(strcmp(station_name, ShmChargerInfo->StationInfo.StationName) != EQUAL ||
  6953. ShmChargerInfo->StationInfo.StationID != station_id)
  6954. {
  6955. strcpy(ShmChargerInfo->StationInfo.StationName, station_name);
  6956. ShmChargerInfo->StationInfo.StationID = station_id;
  6957. LOG_INFO("Station ID: %d, Name: %s", ShmChargerInfo->StationInfo.StationID, ShmChargerInfo->StationInfo.StationName);
  6958. AnnounceStationInfo();
  6959. }
  6960. Clean_Ocpp_ChargerStationReq();
  6961. }
  6962. }
  6963. void CheckWeatherInfo(void)
  6964. {
  6965. int weatherID = 0;
  6966. float temperature = 0;
  6967. char strWeather[16];
  6968. 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};
  6969. if(Is_Ocpp_WeatherReq())
  6970. {
  6971. Get_Ocpp_WeatherInfo(&weatherID, &temperature);
  6972. if(weatherID != ShmChargerInfo->StationInfo.WeatherID ||
  6973. temperature != ShmChargerInfo->StationInfo.Temperature)
  6974. {
  6975. ShmChargerInfo->StationInfo.WeatherID = weatherID;
  6976. ShmChargerInfo->StationInfo.Temperature = temperature;
  6977. if(ShmChargerInfo->StationInfo.WeatherID <= _WEATHER_Mist)
  6978. {
  6979. strcpy(strWeather, str_weather_type[ShmChargerInfo->StationInfo.WeatherID]);
  6980. }
  6981. else
  6982. {
  6983. sprintf(strWeather, "%d", ShmChargerInfo->StationInfo.WeatherID);
  6984. }
  6985. LOG_INFO("Weather: [%s][%d], Temperature: %4.1f", strWeather, ShmChargerInfo->StationInfo.WeatherID, ShmChargerInfo->StationInfo.Temperature);
  6986. AnnounceStationInfo();
  6987. }
  6988. Clean_Ocpp_WeatherReq();
  6989. }
  6990. }
  6991. bool _WriteDeductBusy[CONNECTOR_QUANTITY] = {0};
  6992. void CheckDeductResultRequest(void)
  6993. {
  6994. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  6995. char cardNo[21];
  6996. char appNo[10];
  6997. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  6998. {
  6999. if(Is_DeductResultReq(i))
  7000. {
  7001. if(!_WriteDeductBusy[i])
  7002. {
  7003. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7004. {
  7005. _WriteDeductBusy[i] = true;
  7006. //Set_Ocpp_DeductTransaction(_BackupDeductResult[i].TransactionId);
  7007. //Set_Ocpp_DeductCreditNo((char *)&_BackupDeductResult[i].creditNo);
  7008. //Set_Ocpp_DeductVemData("");
  7009. //Set_Ocpp_DeductAmount(((double)_BackupDeductResult[i].DeductAmount / 100));
  7010. //Set_Ocpp_DeductResult(_BackupDeductResult[i].DeductResult);
  7011. //Set_Ocpp_DonateInvoice(_BackupDeductResult[i].IsDonateInvoice);
  7012. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->DeductResultReq[i]))
  7013. {
  7014. memset(cardNo, 0x00, sizeof(cardNo));
  7015. memcpy(cardNo, (char *)&ShmChargerInfo->DeductResultReq[i].creditNo[0], sizeof(ShmChargerInfo->DeductResultReq[i].creditNo));
  7016. memset(appNo, 0x00, sizeof(appNo));
  7017. memcpy(appNo, (char *)&ShmChargerInfo->DeductResultReq[i].ApprovalNumber[0], sizeof(ShmChargerInfo->DeductResultReq[i].ApprovalNumber));
  7018. Set_Ocpp_ReportCreditDeductReq();
  7019. LOG_INFO("DeductReport Gun %d TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7020. i + 1,
  7021. ShmChargerInfo->DeductResultReq[i].TransactionId,
  7022. cardNo,
  7023. appNo,
  7024. (ShmChargerInfo->DeductResultReq[i].DeductAmount / 100),
  7025. (ShmChargerInfo->DeductResultReq[i].DeductAmount % 100),
  7026. str_ReaderStatus[ShmChargerInfo->DeductResultReq[i].DeductResult],
  7027. ShmChargerInfo->DeductResultReq[i].IsDonateInvoice ? "Yes" : "No");
  7028. if(ShmChargerInfo->DeductResultReq[i].DeductResult == _Deduct_PreAuth_Pass)
  7029. {
  7030. memcpy(&ShmChargerInfo->ReaderStatus[i], &ShmChargerInfo->DeductResultReq[i], sizeof(DeductResultInfoData));
  7031. ShmChargerInfo->ReaderStatus[i].ReaderStatusReq = YES;
  7032. }
  7033. }
  7034. }
  7035. }
  7036. else
  7037. {
  7038. if(!Is_Ocpp_ReportCreditDeductReq())
  7039. {
  7040. Clean_DeductInfo(i);
  7041. _WriteDeductBusy[i] = false;
  7042. }
  7043. }
  7044. }
  7045. else
  7046. {
  7047. _WriteDeductBusy[i] = false;
  7048. }
  7049. }
  7050. }
  7051. bool _WriteReDeductBusy = false;
  7052. void CheckReDeductRequest(void)
  7053. {
  7054. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7055. char cardNo[21];
  7056. char appNo[10];
  7057. if(ShmChargerInfo->ReDeductReq.DeductReq)
  7058. {
  7059. if(!_WriteReDeductBusy)
  7060. {
  7061. if(!Is_Ocpp_ReportCreditDeductReq() && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7062. {
  7063. _WriteReDeductBusy = true;
  7064. if(Set_Ocpp_DeductInfo(&ShmChargerInfo->ReDeductReq))
  7065. {
  7066. memset(cardNo, 0x00, sizeof(cardNo));
  7067. memcpy(cardNo, (char *)&ShmChargerInfo->ReDeductReq.creditNo[0], sizeof(ShmChargerInfo->ReDeductReq.creditNo));
  7068. memset(appNo, 0x00, sizeof(appNo));
  7069. memcpy(appNo, (char *)&ShmChargerInfo->ReDeductReq.ApprovalNumber[0], sizeof(ShmChargerInfo->ReDeductReq.ApprovalNumber));
  7070. Set_Ocpp_ReportCreditDeductReq();
  7071. LOG_INFO("ReDeductReport TxId: %d, UserId: %s, ApprovalNo: [%s], Amount: %d.%02d, [%s], Donate: %s",
  7072. ShmChargerInfo->ReDeductReq.TransactionId,
  7073. cardNo,
  7074. appNo,
  7075. (ShmChargerInfo->ReDeductReq.DeductAmount / 100),
  7076. (ShmChargerInfo->ReDeductReq.DeductAmount % 100),
  7077. str_ReaderStatus[ShmChargerInfo->ReDeductReq.DeductResult],
  7078. ShmChargerInfo->ReDeductReq.IsDonateInvoice ? "Yes" : "No");
  7079. if(ShmChargerInfo->ReDeductReq.DeductResult == _Deduct_PreAuth_Pass && ShmChargerInfo->ReDeductReq.ConnectorId != 0xFF)
  7080. {
  7081. int gunIndex = ShmChargerInfo->ReDeductReq.ConnectorId - 1;
  7082. memcpy(&ShmChargerInfo->ReaderStatus[gunIndex], &ShmChargerInfo->ReDeductReq, sizeof(DeductResultInfoData));
  7083. ShmChargerInfo->ReaderStatus[gunIndex].ReaderStatusReq = YES;
  7084. }
  7085. }
  7086. }
  7087. }
  7088. else
  7089. {
  7090. if(!Is_Ocpp_ReportCreditDeductReq())
  7091. {
  7092. memset(&ShmChargerInfo->ReDeductReq, 0x00, sizeof(DeductResultInfoData));
  7093. _WriteReDeductBusy = false;
  7094. }
  7095. }
  7096. }
  7097. else
  7098. {
  7099. _WriteReDeductBusy = false;
  7100. }
  7101. }
  7102. bool _WriteReaderStatusBusy[CONNECTOR_QUANTITY] = {0};
  7103. void CheckReaderStatusRequest(void)
  7104. {
  7105. char *str_ReaderStatus[] = {STR_DEDUCT_FAIL, STR_DEDUCT_PASS, STR_DEDUCT_CANCEL, STR_DEDUCT_PREAUTH};
  7106. char cardNo[21];
  7107. char appNo[10];
  7108. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7109. {
  7110. if(Is_ReaderStatusReq(i))
  7111. {
  7112. if(!_WriteReaderStatusBusy[i])
  7113. {
  7114. if(!Is_Ocpp_TcciReaderStatusReq(i) && ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  7115. {
  7116. _WriteReaderStatusBusy[i] = true;
  7117. if(Set_Ocpp_TcciReaderStatus(i, &ShmChargerInfo->ReaderStatus[i]))
  7118. {
  7119. memset(cardNo, 0x00, sizeof(cardNo));
  7120. memcpy(cardNo, (char *)&ShmChargerInfo->ReaderStatus[i].creditNo[0], sizeof(ShmChargerInfo->ReaderStatus[i].creditNo));
  7121. memset(appNo, 0x00, sizeof(appNo));
  7122. memcpy(appNo, (char *)&ShmChargerInfo->ReaderStatus[i].ApprovalNumber[0], sizeof(ShmChargerInfo->ReaderStatus[i].ApprovalNumber));
  7123. Set_Ocpp_TcciReaderStatusReq(i);
  7124. LOG_INFO("StatusReport Gun %d: [%s], CreditNo: [%s], ApprovalNo: [%s]",
  7125. i + 1, str_ReaderStatus[ShmChargerInfo->ReaderStatus[i].DeductResult], cardNo, appNo);
  7126. }
  7127. }
  7128. }
  7129. else
  7130. {
  7131. if(!Is_Ocpp_TcciReaderStatusReq(i))
  7132. {
  7133. Clean_ReaderStatusInfo(i);
  7134. _WriteReaderStatusBusy[i] = false;
  7135. }
  7136. }
  7137. }
  7138. else
  7139. {
  7140. _WriteReaderStatusBusy[i] = false;
  7141. }
  7142. }
  7143. }
  7144. void CheckOccupancyPrice(void)
  7145. {
  7146. float _price = 0;
  7147. if(Is_Ocpp_TcciSetOccupancyPriceReq())
  7148. {
  7149. _price = Get_Ocpp_TcciOccupancyPrice();
  7150. if(ShmChargerInfo->CabinetMiscValue.ParkingPrice != _price)
  7151. {
  7152. ShmChargerInfo->CabinetMiscValue.ParkingPrice = _price;
  7153. LOG_INFO("Set OccupancyPrice: [%6.2f]", ShmChargerInfo->CabinetMiscValue.ParkingPrice);
  7154. AnnounceOccupancyPrice();
  7155. }
  7156. Clean_Ocpp_TcciSetOccupancyPriceReq();
  7157. }
  7158. }
  7159. bool IsParkingButtonStatusChanged(int gunIndex)
  7160. {
  7161. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus ?
  7162. false : true;
  7163. }
  7164. void SyncParkingButtonStatus(int gunIndex)
  7165. {
  7166. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7167. }
  7168. int OccupancyButtonSync(int gunIndex)
  7169. {
  7170. int _pass = FAIL;
  7171. unsigned char _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus;
  7172. switch(_status)
  7173. {
  7174. case _BillStatus_Idle:
  7175. if(IsParkingButtonStatusChanged(gunIndex))
  7176. {
  7177. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7178. SyncParkingButtonStatus(gunIndex);
  7179. }
  7180. break;
  7181. case _BillStatus_MiscReq:
  7182. if(IsParkingButtonStatusChanged(gunIndex))
  7183. {
  7184. Occupancy_INFO("Gun %d OccupancyButton [MiscRequest]", gunIndex + 1);
  7185. SyncParkingButtonStatus(gunIndex);
  7186. }
  7187. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_WaitMiscClean;
  7188. Set_Connector_MiscCommand(gunIndex, MISC_CONN_PARKING_STATUS);
  7189. break;
  7190. case _BillStatus_WaitMiscClean:
  7191. if(IsParkingButtonStatusChanged(gunIndex))
  7192. {
  7193. SyncParkingButtonStatus(gunIndex);
  7194. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7195. }
  7196. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7197. {
  7198. Occupancy_INFO("Gun %d OccupancyButton [WaitMisc Timeout]", gunIndex + 1);
  7199. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7200. }
  7201. break;
  7202. case _BillStatus_MiscClean:
  7203. if(IsParkingButtonStatusChanged(gunIndex))
  7204. {
  7205. Occupancy_INFO("Gun %d OccupancyButton [MiscClean]", gunIndex + 1);
  7206. SyncParkingButtonStatus(gunIndex);
  7207. GetClockTime(&_ParkingStatus_Time[gunIndex]);
  7208. }
  7209. if((GetTimeoutValue(_ParkingStatus_Time[gunIndex]) / uSEC_VAL) >= PARKING_STATUS_TIMEOUT)
  7210. {
  7211. LOG_INFO("Gun %d OccupancyButton [Wait Timeout]", gunIndex + 1);
  7212. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7213. }
  7214. break;
  7215. case _BillStatus_UpdateDone:
  7216. if(IsParkingButtonStatusChanged(gunIndex))
  7217. {
  7218. Occupancy_INFO("Gun %d OccupancyButton [UpdateDone]", gunIndex + 1);
  7219. SyncParkingButtonStatus(gunIndex);
  7220. }
  7221. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_Idle;
  7222. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7223. _pass = PASS;
  7224. break;
  7225. default:
  7226. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7227. break;
  7228. }
  7229. return _pass;
  7230. }
  7231. int OccupancyFeeRequest(int gunIndex)
  7232. {
  7233. if(!_isGetOccupancyFeeReq[gunIndex])
  7234. {
  7235. if(!Is_Ocpp_TcciGetOccupancyFeeReq(gunIndex))
  7236. {
  7237. Set_Ocpp_TcciGetOccupancyFeeReq(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill);
  7238. _isGetOccupancyFeeReq[gunIndex] = true;
  7239. LOG_INFO("Gun %d Set OccupancyFee Request", gunIndex + 1);
  7240. }
  7241. }
  7242. else
  7243. {
  7244. if(!Is_Ocpp_TcciGetOccupancyFeeReq(gunIndex))
  7245. {
  7246. if(Is_Ocpp_TcciGetOccupancyFeeStatus(gunIndex, "Accepted"))
  7247. {
  7248. _isGetOccupancyFeeReq[gunIndex] = false;
  7249. Get_Ocpp_TcciGetOccupancyFeeResponse(gunIndex, &ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill);
  7250. LOG_INFO("Gun %d Get Occupancy Fee: [%.2f], Duration: [%d], StartTime: [%s]",
  7251. gunIndex + 1,
  7252. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Fee,
  7253. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Duration,
  7254. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.StartTime);
  7255. return PASS;
  7256. }
  7257. else
  7258. {
  7259. LOG_INFO("Gun %d Get OccupancyFee Rejected", gunIndex + 1);
  7260. return FAIL;
  7261. }
  7262. }
  7263. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7264. {
  7265. // shall not reach here
  7266. Clean_Ocpp_TcciGetOccupancyFeeReq(gunIndex);
  7267. _isGetOccupancyFeeReq[gunIndex] = false;
  7268. LOG_INFO("Gun %d GetOccupancyFee Request Timeout", gunIndex + 1);
  7269. return FAIL;
  7270. }
  7271. }
  7272. return WAIT;
  7273. }
  7274. bool IsParkingStatusChanged(int gunIndex)
  7275. {
  7276. return ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status == ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus ?
  7277. false : true;
  7278. }
  7279. void SyncParkingStatus(int gunIndex)
  7280. {
  7281. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].PreStatus = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7282. }
  7283. bool Is_Occupancy_Cancel(int gunIndex)
  7284. {
  7285. unsigned char dispenser = 0;
  7286. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].ParentDispensetIndex;
  7287. if((!ShmSysConfigAndInfo->SysInfo.OcppConnStatus && !ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq) ||
  7288. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_None ||
  7289. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus == _DS_Timeout)
  7290. {
  7291. return true;
  7292. }
  7293. return false;
  7294. }
  7295. void OccupancyRequest(int gunIndex)
  7296. {
  7297. int _action = 0;
  7298. bool feeRequest = false;
  7299. char _sn[37];
  7300. unsigned char _status = 0;
  7301. char *str_action[] = {STR_OCCUPANCY_ACT_NONE, STR_OCCUPANCY_ACT_DISPLAY, STR_OCCUPANCY_ACT_CANCEL, STR_OCCUPANCY_ACT_PAID_OK, STR_OCCUPANCY_ACT_PAID_NG};
  7302. if(Is_Ocpp_TcciOccupancyDisplayReq(gunIndex))
  7303. {
  7304. memset(_sn, 0x00, sizeof(_sn));
  7305. _action = Get_Ocpp_TcciOccupancyAction(gunIndex, sizeof(_sn) - 1, _sn);
  7306. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ActionId = _action;
  7307. LOG_INFO("Gun %d OccupancyAction is [%s]", gunIndex + 1, str_action[_action]);
  7308. Clean_Ocpp_TcciOccupancyDisplayReq(gunIndex);
  7309. }
  7310. if(ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest)
  7311. {
  7312. feeRequest = true;
  7313. ShmChargerInfo->ConnectorActReq[gunIndex].Flag.bits.ParkingFeeRequest =false;
  7314. }
  7315. _status = ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status;
  7316. if(_status != _Parking_NoOccupancy && chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7317. {
  7318. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7319. Occupancy_INFO("Gun %d OccupancyStatus [ForceClean]", gunIndex + 1);
  7320. return;
  7321. }
  7322. switch(_status)
  7323. {
  7324. case _Parking_NoOccupancy:
  7325. if(IsParkingStatusChanged(gunIndex))
  7326. {
  7327. Occupancy_INFO("Gun %d OccupancyStatus [Clean]", gunIndex + 1);
  7328. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex], 0x00, sizeof(OccupancyInfoData));
  7329. SyncParkingStatus(gunIndex);
  7330. }
  7331. if(_action == _ParkingAction_DisplayButton)
  7332. {
  7333. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7334. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_SyncOccupancy;
  7335. }
  7336. break;
  7337. case _Parking_SyncOccupancy:
  7338. if(IsParkingStatusChanged(gunIndex))
  7339. {
  7340. Occupancy_INFO("Gun %d OccupancyStatus [Sync]", gunIndex + 1);
  7341. SyncParkingStatus(gunIndex);
  7342. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7343. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7344. }
  7345. if(_action == _ParkingAction_DisplayButton)
  7346. {
  7347. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7348. }
  7349. else if(_action == _ParkingAction_CancelOccupancy ||
  7350. Is_Occupancy_Cancel(gunIndex))
  7351. {
  7352. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7353. }
  7354. else if(_action == _ParkingAction_PaidOnlineOK)
  7355. {
  7356. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7357. }
  7358. else
  7359. {
  7360. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  7361. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  7362. {
  7363. if(OccupancyButtonSync(gunIndex) == PASS)
  7364. {
  7365. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7366. }
  7367. }
  7368. }
  7369. break;
  7370. case _Parking_Occupied:
  7371. if(IsParkingStatusChanged(gunIndex))
  7372. {
  7373. Occupancy_INFO("Gun %d OccupancyStatus [Start]", gunIndex + 1);
  7374. SyncParkingStatus(gunIndex);
  7375. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus, 0x00, sizeof(MiscRequestStatus));
  7376. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].ButtonReqStatus.MiscStatus = _BillStatus_MiscReq;
  7377. }
  7378. if(_action == _ParkingAction_DisplayButton)
  7379. {
  7380. memcpy(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyBill.Sn, _sn, sizeof(_sn));
  7381. }
  7382. else if(_action == _ParkingAction_CancelOccupancy ||
  7383. Is_Occupancy_Cancel(gunIndex))
  7384. {
  7385. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7386. }
  7387. else if(_action == _ParkingAction_PaidOnlineOK)
  7388. {
  7389. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7390. }
  7391. else if(_action == _ParkingAction_PaidOnlineFail)
  7392. {
  7393. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7394. }
  7395. else
  7396. {
  7397. if(feeRequest)
  7398. {
  7399. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_RequestFee;
  7400. }
  7401. }
  7402. break;
  7403. case _Parking_RequestFee:
  7404. if(IsParkingStatusChanged(gunIndex))
  7405. {
  7406. Occupancy_INFO("Gun %d OccupancyStatus [RequestFee]", gunIndex + 1);
  7407. SyncParkingStatus(gunIndex);
  7408. _isGetOccupancyFeeReq[gunIndex] = false;
  7409. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7410. }
  7411. if(_action == _ParkingAction_CancelOccupancy ||
  7412. Is_Occupancy_Cancel(gunIndex))
  7413. {
  7414. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7415. }
  7416. else if(_action == _ParkingAction_PaidOnlineOK)
  7417. {
  7418. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7419. }
  7420. else if(_action == _ParkingAction_PaidOnlineFail)
  7421. {
  7422. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7423. }
  7424. else
  7425. {
  7426. if(ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].OccupancyReq.bits.SelfReq)
  7427. {
  7428. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7429. }
  7430. else
  7431. {
  7432. int res = OccupancyFeeRequest(gunIndex);
  7433. if(res == PASS)
  7434. {
  7435. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_WaitToPay;
  7436. }
  7437. else if(res == FAIL)
  7438. {
  7439. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7440. }
  7441. else
  7442. {
  7443. // wait response
  7444. }
  7445. }
  7446. }
  7447. break;
  7448. case _Parking_WaitToPay:
  7449. if(IsParkingStatusChanged(gunIndex))
  7450. {
  7451. Occupancy_INFO("Gun %d OccupancyStatus [WaitToPay]", gunIndex + 1);
  7452. SyncParkingStatus(gunIndex);
  7453. memset(&ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].BillReqStatus, 0x00, sizeof(MiscRequestStatus));
  7454. }
  7455. if(_action == _ParkingAction_CancelOccupancy ||
  7456. Is_Occupancy_Cancel(gunIndex))
  7457. {
  7458. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7459. }
  7460. else if(_action == _ParkingAction_PaidOnlineOK)
  7461. {
  7462. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineOK;
  7463. }
  7464. else if(_action == _ParkingAction_PaidOnlineFail)
  7465. {
  7466. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_PaidOnlineFail;
  7467. }
  7468. break;
  7469. case _Parking_PaidOnlineOK:
  7470. if(IsParkingStatusChanged(gunIndex))
  7471. {
  7472. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineOK]", gunIndex + 1);
  7473. SyncParkingStatus(gunIndex);
  7474. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7475. }
  7476. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7477. {
  7478. LOG_INFO("Gun %d Reset OccupancyStatus via PaidOnlineOK", gunIndex + 1);
  7479. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_NoOccupancy;
  7480. }
  7481. break;
  7482. case _Parking_PaidOnlineFail:
  7483. if(IsParkingStatusChanged(gunIndex))
  7484. {
  7485. Occupancy_INFO("Gun %d OccupancyStatus [PaidOnlineFail]", gunIndex + 1);
  7486. SyncParkingStatus(gunIndex);
  7487. GetClockTime(&_GetOccupancyFee_Time[gunIndex]);
  7488. }
  7489. if((GetTimeoutValue(_GetOccupancyFee_Time[gunIndex]) / uSEC_VAL) >= OCCUPANCY_FEE_TIMEOUT)
  7490. {
  7491. LOG_INFO("Gun %d Rerutn to Occupied via PaidOnlineFail", gunIndex + 1);
  7492. ShmChargerInfo->AllBill.OccupancyInfo[gunIndex].Status = _Parking_Occupied;
  7493. }
  7494. break;
  7495. default:
  7496. DisableConnectorBill(gunIndex);
  7497. break;
  7498. }
  7499. }
  7500. void CheckOccupancyFee(void)
  7501. {
  7502. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7503. {
  7504. OccupancyRequest(i);
  7505. }
  7506. }
  7507. #define OCCUPANCY_DEDUCT_IDLE 0
  7508. #define OCCUPANCY_DEDUCT_REQUEST 1
  7509. #define OCCUPANCY_DEDUCT_CANCEL 2
  7510. unsigned char _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7511. int _OccupancyReportGunId = 0;
  7512. void CheckOccupancyDeduct(void)
  7513. {
  7514. char cardNo[21];
  7515. char appNo[10];
  7516. if(_OccupancyDeductStatus == OCCUPANCY_DEDUCT_IDLE)
  7517. {
  7518. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7519. {
  7520. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  7521. {
  7522. if(Is_OccupancyDeductReq(i))
  7523. {
  7524. if(ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyReq.bits.SelfReq &&
  7525. ShmChargerInfo->AllBill.OccupancyInfo[i].Status == _Parking_WaitToPay)
  7526. {
  7527. ShmChargerInfo->AllBill.OccupancyInfo[i].Status = _Parking_NoOccupancy;
  7528. LOG_INFO("Gun %d Occupancy Concluded!", i + 1);
  7529. break;
  7530. }
  7531. Set_Ocpp_OccupancyDeductResult(&ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyDeduct, YES);
  7532. Set_Ocpp_TcciReportOccupancyDeductReq();
  7533. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_REQUEST;
  7534. _OccupancyReportGunId = i + 1;
  7535. Clean_OccupancyDeductReq(i);
  7536. break;
  7537. }
  7538. if(Is_OccupancyKeepCountingReq(i))
  7539. {
  7540. if(ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyReq.bits.SelfReq)
  7541. {
  7542. LOG_INFO("Gun %d Occupancy Resume!", i + 1);
  7543. Clean_OccupancyKeepCountingReq(i);
  7544. break;
  7545. }
  7546. OccupancyDeductInfo _cancel_deduct;
  7547. memset(&_cancel_deduct, 0x00, sizeof(OccupancyDeductInfo));
  7548. memcpy(&_cancel_deduct.Sn, ShmChargerInfo->AllBill.OccupancyInfo[i].OccupancyBill.Sn, sizeof(_cancel_deduct.Sn));
  7549. Set_Ocpp_OccupancyDeductResult(&_cancel_deduct, NO);
  7550. Set_Ocpp_TcciReportOccupancyDeductReq();
  7551. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_CANCEL;
  7552. _OccupancyReportGunId = i + 1;
  7553. Clean_OccupancyKeepCountingReq(i);
  7554. break;
  7555. }
  7556. }
  7557. if(_OccupancyDeductStatus != OCCUPANCY_DEDUCT_IDLE)
  7558. {
  7559. memset(cardNo, 0x00, sizeof(cardNo));
  7560. memset(appNo, 0x00, sizeof(appNo));
  7561. memcpy(cardNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.creditNo));
  7562. memcpy(appNo, ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber, sizeof(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.ApprovalNumber));
  7563. if(_OccupancyDeductStatus == OCCUPANCY_DEDUCT_REQUEST)
  7564. {
  7565. LOG_INFO("Gun %d Occupancy Deduct [%s] Sn: [%s], Fee: [%.2f], CreditNo: [%s], ApprovalNo: [%s]",
  7566. _OccupancyReportGunId,
  7567. _OccupancyDeductStatus == OCCUPANCY_DEDUCT_REQUEST ? "Request" : "Cancel",
  7568. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Sn,
  7569. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyDeduct.Fee,
  7570. cardNo,
  7571. appNo);
  7572. }
  7573. else
  7574. {
  7575. LOG_INFO("Gun %d Occupancy Deduct [%s] Sn: [%s]",
  7576. _OccupancyReportGunId,
  7577. _OccupancyDeductStatus == OCCUPANCY_DEDUCT_REQUEST ? "Request" : "Cancel",
  7578. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].OccupancyBill.Sn);
  7579. }
  7580. }
  7581. }
  7582. }
  7583. else
  7584. {
  7585. if(!Is_Ocpp_TcciReportOccupancyDeductReq())
  7586. {
  7587. if(_OccupancyDeductStatus == OCCUPANCY_DEDUCT_REQUEST)
  7588. {
  7589. if(ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status == _Parking_WaitToPay)
  7590. {
  7591. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status = _Parking_NoOccupancy;
  7592. LOG_INFO("Gun %d OccupancyDeduct Request Completed!", _OccupancyReportGunId);
  7593. }
  7594. }
  7595. else
  7596. {
  7597. ShmChargerInfo->AllBill.OccupancyInfo[_OccupancyReportGunId - 1].Status = _Parking_Occupied;
  7598. LOG_INFO("Gun %d Occupancy Continued!", _OccupancyReportGunId);
  7599. }
  7600. _OccupancyDeductStatus = OCCUPANCY_DEDUCT_IDLE;
  7601. }
  7602. }
  7603. }
  7604. //===============================================
  7605. // OCPP
  7606. //===============================================
  7607. void CheckOcppStatus()
  7608. {
  7609. if (Is_Ocpp_BootNotification())
  7610. {
  7611. Clean_Ocpp_BootNotification();
  7612. LOG_INFO("*********************OCPP Boot Notification****************************");
  7613. }
  7614. if (Is_Ocpp_ResetReq())
  7615. {
  7616. bool canReset = true;
  7617. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2)
  7618. {
  7619. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  7620. {
  7621. if (chargingInfo[_index]->SystemStatus != S_IDLE &&
  7622. chargingInfo[_index]->SystemStatus != S_RESERVATION &&
  7623. chargingInfo[_index]->SystemStatus != S_MAINTAIN)
  7624. {
  7625. canReset = false;
  7626. //if (chargingInfo[_index]->SystemStatus >= S_REASSIGN && chargingInfo[_index]->SystemStatus < S_TERMINATING)
  7627. //{
  7628. // ChargingTerminalProcess(_index);
  7629. //}
  7630. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7631. {
  7632. chargingInfo[_index]->ChargingStopFlag.bits.HardResetStop = true;
  7633. }
  7634. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7635. {
  7636. chargingInfo[_index]->ChargingStopFlag.bits.SoftResetStop = true;
  7637. }
  7638. }
  7639. }
  7640. }
  7641. if(canReset)
  7642. {
  7643. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  7644. {
  7645. //trigger dispenser to hardware reboot
  7646. TriggerDispenserHardwareReboot();
  7647. if(IsDispenserHardwareRebootResponsed())
  7648. {
  7649. Clean_Ocpp_ResetReq();
  7650. LOG_INFO("****** Hard Reboot ******");
  7651. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7652. {
  7653. setChargerMode(i, MODE_MAINTAIN);
  7654. }
  7655. sleep(3);
  7656. system("reboot -f");
  7657. }
  7658. }
  7659. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  7660. {
  7661. //trigger dispenser to software reset
  7662. TriggerDispenserSoftwareReset();
  7663. if(IsDispenserSoftwareResetResponsed())
  7664. {
  7665. Clean_Ocpp_ResetReq();
  7666. LOG_INFO("****** Soft Reboot ******");
  7667. for(int i = 0; i < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; i++)
  7668. {
  7669. setChargerMode(i, MODE_MAINTAIN);
  7670. }
  7671. sleep(3);
  7672. Kill_Ocpp_Service();
  7673. KillAllTask();
  7674. TryCloseWatchdog();
  7675. system("/usr/bin/run_evse_restart.sh");
  7676. }
  7677. }
  7678. }
  7679. }
  7680. CheckOcppCostAndPrice();
  7681. CheckLcmPageChangeRequest();
  7682. CheckStationInfo();
  7683. CheckWeatherInfo();
  7684. CheckDeductResultRequest();
  7685. CheckReDeductRequest();
  7686. CheckReaderStatusRequest();
  7687. CheckOccupancyPrice();
  7688. CheckOccupancyFee();
  7689. CheckOccupancyDeduct();
  7690. }
  7691. void CheckoutStartChargingSoc(byte gunIndex, bool force)
  7692. {
  7693. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc)
  7694. {
  7695. if(chargingInfo[gunIndex]->EvBatterySoc > 0 || force)
  7696. {
  7697. chargingInfo[gunIndex]->EvBatteryStartSoc = chargingInfo[gunIndex]->EvBatterySoc;
  7698. LOG_INFO("Gun %d StartSoc = %d", chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->EvBatteryStartSoc);
  7699. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Parameter.bits.GetStartChargingSoc = true;
  7700. }
  7701. }
  7702. }
  7703. void UpdateChargingTimestamp(char *timestamp)
  7704. {
  7705. struct timeb SeqEndTime;
  7706. struct tm *tm;
  7707. ftime(&SeqEndTime);
  7708. SeqEndTime.time = time(NULL);
  7709. tm = localtime(&SeqEndTime.time);
  7710. sprintf(timestamp, "%04d-%02d-%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  7711. }
  7712. void OcppStartTransaction(byte gunIndex)
  7713. {
  7714. if(_triggerTransaction[gunIndex])
  7715. {
  7716. return;
  7717. }
  7718. // clean deduct info
  7719. Clean_DeductInfo(gunIndex);
  7720. // set deduct user id
  7721. //Set_Deduct_UserId(gunIndex, (char *)&chargingInfo[gunIndex]->StartUserId);
  7722. // initial start & stop datetime
  7723. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, "00:00:00");
  7724. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, "00:00:00");
  7725. Update_PowerConsumption(gunIndex);
  7726. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  7727. Set_Ocpp_StartTransactionIdTag(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  7728. LOG_INFO("Gun %d Start Transaction IdTag = %s, MeterValue: %.4f",
  7729. chargingInfo[gunIndex]->Index + 1, chargingInfo[gunIndex]->StartUserId, chargingInfo[gunIndex]->PowerConsumption);
  7730. Clean_Ocpp_StartTransactionConf(gunIndex);
  7731. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StartCharging);
  7732. Set_Ocpp_StartTransactionReq(gunIndex);
  7733. // set _triggerTransaction
  7734. _triggerTransaction[gunIndex] = true;
  7735. }
  7736. void OcppStopTransaction(byte gunIndex)
  7737. {
  7738. if(!_triggerTransaction[gunIndex])
  7739. {
  7740. return;
  7741. }
  7742. Update_PowerConsumption(gunIndex);
  7743. _lsatGunMeterValue[gunIndex] = chargingInfo[gunIndex]->PowerConsumption;
  7744. Set_Ocpp_StopTransactionId(gunIndex, (char *)chargingInfo[gunIndex]->StartUserId);
  7745. LOG_INFO("Gun %d Stop Transaction IdTag = %s, StopSoc = %d, MeterValue: %.4f",
  7746. chargingInfo[gunIndex]->Index + 1,
  7747. chargingInfo[gunIndex]->StartUserId,
  7748. chargingInfo[gunIndex]->EvBatterySoc,
  7749. chargingInfo[gunIndex]->PowerConsumption);
  7750. UpdateChargingTimestamp(ShmChargerInfo->Timestamp[gunIndex].StopCharging);
  7751. Set_Ocpp_StopTransactionReq(gunIndex);
  7752. // clean _triggerTransaction
  7753. _triggerTransaction[gunIndex] = false;
  7754. }
  7755. bool Is_DispenserStop(byte gunIndex)
  7756. {
  7757. bool _isStop = false;
  7758. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Preparing &&
  7759. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteStatus != _CRS_Charging)
  7760. {
  7761. _isStop = true;
  7762. }
  7763. return _isStop;
  7764. }
  7765. void ChkOcppStatus(byte gunIndex)
  7766. {
  7767. Chk_Ocpp_Backup_ErrorCode(gunIndex);
  7768. if(Is_Ocpp_ReserveNowReq(gunIndex))
  7769. {
  7770. Clean_Ocpp_ReserveNowReq(gunIndex);
  7771. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE && !Is_Ocpp_ReserveNowExpired(gunIndex))
  7772. {
  7773. LOG_INFO("***************ChkOcppStatus: Gun %d OcppReservedStatus********************", gunIndex + 1);
  7774. chargingInfo[gunIndex]->ReservationId = Get_Ocpp_ReserveNowId(gunIndex);
  7775. setChargerMode(gunIndex, MODE_RESERVATION);
  7776. }
  7777. Set_Ocpp_ReserveNowConf(gunIndex);
  7778. }
  7779. if(Is_Ocpp_CancelReservationReq(gunIndex))
  7780. {
  7781. Clean_Ocpp_CancelReservationReq(gunIndex);
  7782. if(chargingInfo[gunIndex]->SystemStatus == S_RESERVATION)
  7783. {
  7784. LOG_INFO("***************ChkOcppStatus: Gun %d Cancel OcppReservedStatus********************", gunIndex + 1);
  7785. chargingInfo[gunIndex]->ReservationId = -1;
  7786. setChargerMode(gunIndex, MODE_IDLE);
  7787. }
  7788. Set_Ocpp_CancelReservationConf(gunIndex);
  7789. }
  7790. if (Is_Ocpp_ChangeAvailabilityReq(gunIndex) == YES)
  7791. {
  7792. LOG_INFO("***************ChkOcppStatus: Gun %d OcppChangeAvailability********************", gunIndex + 1);
  7793. Clean_Ocpp_ChangeAvailabilityReq(gunIndex);
  7794. if(Chk_Ocpp_Availability_Type(gunIndex, "Operative"))
  7795. {
  7796. if (isDb_ready)
  7797. DB_Update_Operactive(localDb, gunIndex, true);
  7798. chargingInfo[gunIndex]->IsAvailable = YES;
  7799. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  7800. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  7801. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  7802. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  7803. {
  7804. setChargerMode(gunIndex, MODE_IDLE);
  7805. }
  7806. }
  7807. else if(Chk_Ocpp_Availability_Type(gunIndex, "Inoperative"))
  7808. {
  7809. if (isDb_ready)
  7810. DB_Update_Operactive(localDb, gunIndex, false);
  7811. chargingInfo[gunIndex]->IsAvailable = NO;
  7812. Set_Connector_MiscCommand(gunIndex, MISC_CONN_AVAILABILITY);
  7813. if (chargingInfo[gunIndex]->SystemStatus == S_IDLE ||
  7814. chargingInfo[gunIndex]->SystemStatus == S_RESERVATION ||
  7815. chargingInfo[gunIndex]->SystemStatus == S_MAINTAIN)
  7816. {
  7817. setChargerMode(gunIndex, MODE_MAINTAIN);
  7818. }
  7819. }
  7820. }
  7821. if (Is_Ocpp_UnlockConnectorReq(gunIndex))
  7822. {
  7823. Clean_Ocpp_UnlockConnectorReq(gunIndex);
  7824. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  7825. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  7826. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  7827. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7828. {
  7829. // 充電中,需停止充電
  7830. Set_Ocpp_StopReason(gunIndex, "UnlockCommand");
  7831. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_UNLOCK);
  7832. chargingInfo[gunIndex]->ChargingStopFlag.bits.UnlockStop = true;
  7833. LOG_INFO("********** OcppUnlockStop Gun %d **********", gunIndex + 1);
  7834. }
  7835. Set_Ocpp_UnlockConnectorConf(gunIndex);
  7836. }
  7837. if (Is_Ocpp_RemoteStopReq(gunIndex))
  7838. {
  7839. Clean_Ocpp_RemoteStopReq(gunIndex);
  7840. if(chargingInfo[gunIndex]->SystemStatus == S_PREPARNING ||
  7841. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EV ||
  7842. chargingInfo[gunIndex]->SystemStatus == S_PREPARING_FOR_EVSE ||
  7843. chargingInfo[gunIndex]->SystemStatus == S_CHARGING)
  7844. {
  7845. Set_Ocpp_StopReason(gunIndex, "Remote");
  7846. //Set_Connector_MiscCommand(gunIndex, MISC_CONN_REMOTE_STOP);
  7847. chargingInfo[gunIndex]->ChargingStopFlag.bits.RemoteStop = true;
  7848. LOG_INFO("********** OcppRemoteStop Gun %d **********", gunIndex + 1);
  7849. }
  7850. }
  7851. if(Is_Ocpp_StartTransactionConf(gunIndex))
  7852. {
  7853. char acceptId[128] = {0};
  7854. char status[32] = {0};
  7855. Get_Ocpp_StartTransaction_Status(gunIndex, status);
  7856. Get_Ocpp_TransactionId(gunIndex, acceptId);
  7857. Set_Deduct_TransactionId(gunIndex, atoi(acceptId));
  7858. Set_UserTransactionId(gunIndex, atoi(acceptId));
  7859. LOG_INFO("Gun %d OCPP Start Transaction %s, TransactionId [%s]", gunIndex + 1, status, acceptId);
  7860. if(!Is_Ocpp_StartTransaction_Accepted(gunIndex))
  7861. {
  7862. if(Is_Ocpp_StopOnInvalidId_Enable())
  7863. {
  7864. if(Is_Invalid_StartTransactionId(gunIndex))
  7865. {
  7866. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop = true;
  7867. LOG_INFO("Gun %d OCPP StopOnInvalidId", gunIndex + 1);
  7868. }
  7869. }
  7870. LOG_INFO("Gun %d Transaction [%s] Not Accepted, %s",
  7871. gunIndex + 1, acceptId,
  7872. chargingInfo[gunIndex]->ChargingStopFlag.bits.InvalidIdStop > 0 ? "And Stop Charging" : "But StopOnInvalidId is Disable");
  7873. }
  7874. Clean_Ocpp_StartTransactionConf(gunIndex);
  7875. }
  7876. }
  7877. bool CheckBackendChargingTimeout(byte gunIndex)
  7878. {
  7879. bool result = false;
  7880. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  7881. {
  7882. if (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  7883. {
  7884. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))
  7885. result = true;
  7886. }
  7887. }
  7888. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  7889. {
  7890. // 隨插即充電的要看 offline
  7891. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  7892. {
  7893. if (chargingInfo[gunIndex]->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  7894. result = true;
  7895. }
  7896. }
  7897. return result;
  7898. }
  7899. bool CheckBackendChargingEnergy(byte gunIndex)
  7900. {
  7901. bool result = false;
  7902. if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  7903. {
  7904. if (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  7905. {
  7906. if (chargingInfo[gunIndex]->PresentChargedEnergy >= ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)
  7907. result = true;
  7908. }
  7909. }
  7910. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  7911. {
  7912. // 隨插即充電的要看 offline
  7913. if (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  7914. {
  7915. if (chargingInfo[gunIndex]->PresentChargedEnergy >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  7916. result = true;
  7917. }
  7918. }
  7919. return result;
  7920. }
  7921. bool IsReachConfigMaxChargingSoc(byte gunIndex)
  7922. {
  7923. bool result = false;
  7924. if(ShmSysConfigAndInfo->SysConfig.MaxChargingSoc > 0)
  7925. {
  7926. if(chargingInfo[gunIndex]->EvBatterySoc >= ShmSysConfigAndInfo->SysConfig.MaxChargingSoc)
  7927. {
  7928. LOG_INFO("Gun %d MaxChargingSoc: [%d%%] Stop", gunIndex + 1, ShmSysConfigAndInfo->SysConfig.MaxChargingSoc);
  7929. result = true;
  7930. }
  7931. }
  7932. return result;
  7933. }
  7934. bool CheckSessionTargetStop(byte gunIndex)
  7935. {
  7936. bool stop = false;
  7937. struct StructSessionTarget target;
  7938. Get_Ocpp_SessionTarget(gunIndex, &target);
  7939. if(target.targetDuration > 0)
  7940. {
  7941. if(chargingInfo[gunIndex]->PresentChargedDuration >= (target.targetDuration * 60))
  7942. {
  7943. LOG_INFO("Gun %d SessionTarget(Duration) Stop, Present: %d (sec), Target: %d (min)",
  7944. gunIndex + 1,
  7945. chargingInfo[gunIndex]->PresentChargedDuration,
  7946. target.targetDuration);
  7947. stop = true;
  7948. }
  7949. }
  7950. if(target.targetSoc > 0)
  7951. {
  7952. if(chargingInfo[gunIndex]->EvBatterySoc >= target.targetSoc)
  7953. {
  7954. LOG_INFO("Gun %d SessionTarget(SOC) Stop, Present: %d (%%), Target: %d (%%)",
  7955. gunIndex + 1,
  7956. chargingInfo[gunIndex]->EvBatterySoc,
  7957. target.targetSoc);
  7958. stop = true;
  7959. }
  7960. }
  7961. if(target.targetEnergy > 0)
  7962. {
  7963. if(chargingInfo[gunIndex]->PresentChargedEnergy >= target.targetEnergy)
  7964. {
  7965. LOG_INFO("Gun %d SessionTarget(Energy) Stop, Present: %8.3f (kWh), Target: %4d (kWh)",
  7966. gunIndex + 1,
  7967. chargingInfo[gunIndex]->PresentChargedEnergy,
  7968. target.targetEnergy);
  7969. stop = true;
  7970. }
  7971. }
  7972. if(stop)
  7973. {
  7974. chargingInfo[gunIndex]->ChargingStopFlag.bits.SessionTargetStop = true;
  7975. }
  7976. return stop;
  7977. }
  7978. void InformOcppErrOccur(byte codeType)
  7979. {
  7980. char _error[25];
  7981. switch(codeType)
  7982. {
  7983. case 4: strcpy(_error, "InternalError"); break;
  7984. case 6: strcpy(_error, "NoError"); break;
  7985. case 7: strcpy(_error, "OtherError"); break;
  7986. case 13: strcpy(_error, "UnderVoltage"); break;
  7987. case 14: strcpy(_error, "OverVoltage"); break;
  7988. }
  7989. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  7990. {
  7991. SetOcppErrorCode(gun_index, _error);
  7992. }
  7993. }
  7994. void SetOcppErrorCode(unsigned char _index, char *errCode)
  7995. {
  7996. Set_Ocpp_StatusNotificationErrorCode(_index, errCode);
  7997. strcpy((char *)_BackupStatusNotification[_index].ErrorCode, errCode);
  7998. }
  7999. void SetOcppVendorErrorCode(unsigned char _index, char *vendorCode)
  8000. {
  8001. Set_Ocpp_StatusNotificationVendorCode(_index, vendorCode);
  8002. strcpy((char *)_BackupStatusNotification[_index].VendorErrorCode, vendorCode);
  8003. }
  8004. //===============================================
  8005. // SQLite3 related routine
  8006. //===============================================
  8007. int NetworkDB_Open(sqlite3 *db)
  8008. {
  8009. int result = PASS;
  8010. char* errMsg = NULL;
  8011. char* createNerRecordSql="CREATE TABLE IF NOT EXISTS network_record("
  8012. "idx integer primary key AUTOINCREMENT, "
  8013. "occurDatetime text NOT NULL, "
  8014. "isInternet text NOT NULL, "
  8015. "isOcppConnected text NOT NULL, "
  8016. "isEth0Internet text NOT NULL, "
  8017. "isMlan0Internet text NOT NULL, "
  8018. "isPpp0Internet text NOT NULL, "
  8019. "rssiMlan0 text NOT NULL, "
  8020. "rssiPpp0 text NOT NULL"
  8021. ");";
  8022. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  8023. if(sqlite3_open(NETWORK_DB_FILE, &db))
  8024. {
  8025. result = FAIL;
  8026. LOG_ERROR("Can't open database (Net) : %s", sqlite3_errmsg(db));
  8027. sqlite3_close(db);
  8028. }
  8029. else
  8030. {
  8031. LOG_INFO("Local network status record database open successfully.");
  8032. if (sqlite3_exec(db, createNerRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8033. {
  8034. result = FAIL;
  8035. LOG_ERROR("Create local network status record table error message: %s", errMsg);
  8036. }
  8037. else
  8038. {
  8039. LOG_INFO("Opened local network status record table successfully");
  8040. }
  8041. sqlite3_close(db);
  8042. }
  8043. return result;
  8044. }
  8045. int DB_Open(sqlite3 *db)
  8046. {
  8047. int result = PASS;
  8048. char* errMsg = NULL;
  8049. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  8050. "idx integer primary key AUTOINCREMENT, "
  8051. "reservationId text, "
  8052. "transactionId text, "
  8053. "startMethod text, "
  8054. "userId text, "
  8055. "dateTimeStart text, "
  8056. "dateTimeStop text,"
  8057. "socStart text, "
  8058. "socStop text, "
  8059. "chargeEnergy text, "
  8060. "stopReason text"
  8061. ");";
  8062. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  8063. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8064. "`IsAvailable` TEXT NOT NULL, "
  8065. "`connector` INTEGER NOT NULL, "
  8066. "`val` TEXT NOT NULL, unique(IsAvailable,connector) on conflict replace);";
  8067. char* creatererrecordSql="CREATE TABLE IF NOT EXISTS `event_record` ( "
  8068. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8069. "`occurDatetime` TEXT NOT NULL, "
  8070. "`statusCode` TEXT NOT NULL, unique(occurDatetime,statusCode) on conflict replace);";
  8071. char* createrebootSql="CREATE TABLE IF NOT EXISTS `reboot_record` ( "
  8072. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8073. "`rebootDatetime` TEXT NOT NULL, unique(rebootDatetime) on conflict replace);";
  8074. char * createconsumptionSql = "CREATE TABLE IF NOT EXISTS `power_consumption` ( "
  8075. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8076. "`connector` INTEGER NOT NULL, "
  8077. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8078. char *createGunConsumptionSql = "CREATE TABLE IF NOT EXISTS `gun_consumption` ( "
  8079. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  8080. "`connector` INTEGER NOT NULL, "
  8081. "`val` TEXT NOT NULL, unique(connector) on conflict replace);";
  8082. char *createFinalCostSql = "CREATE TABLE IF NOT EXISTS `final_cost` ( "
  8083. "idx integer primary key AUTOINCREMENT, "
  8084. "gun text, "
  8085. "transactionId text, "
  8086. "occurDatetime text, "
  8087. "updateDatetime text, "
  8088. "description text, "
  8089. "updateStatus integer"
  8090. ");";
  8091. if(sqlite3_open(DB_FILE, &db))
  8092. {
  8093. result = FAIL;
  8094. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8095. sqlite3_close(db);
  8096. }
  8097. else
  8098. {
  8099. LOG_INFO( "Local charging record database open successfully.");
  8100. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8101. {
  8102. result = FAIL;
  8103. LOG_ERROR( "Create local charging record table error message: %s", errMsg);
  8104. }
  8105. else
  8106. {
  8107. LOG_INFO( "Opened local charging record table successfully");
  8108. }
  8109. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  8110. {
  8111. result = FAIL;
  8112. LOG_ERROR( "Create local config table error message: %s", errMsg);
  8113. }
  8114. else
  8115. {
  8116. LOG_INFO( "Opened local config table successfully");
  8117. }
  8118. if (sqlite3_exec(db, creatererrecordSql, 0, 0, &errMsg) != SQLITE_OK)
  8119. {
  8120. result = FAIL;
  8121. LOG_ERROR( "Create local record table error message: %s", errMsg);
  8122. }
  8123. else
  8124. {
  8125. LOG_INFO( "Opened local record table successfully");
  8126. }
  8127. if (sqlite3_exec(db, createrebootSql, 0, 0, &errMsg) != SQLITE_OK)
  8128. {
  8129. result = FAIL;
  8130. LOG_ERROR( "Create reboot record table error message: %s", errMsg);
  8131. }
  8132. else
  8133. {
  8134. LOG_INFO( "Opened reboot record table successfully");
  8135. }
  8136. if (sqlite3_exec(db, createconsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8137. {
  8138. result = FAIL;
  8139. LOG_ERROR( "Create power consumption record table error message: %s", errMsg);
  8140. }
  8141. else
  8142. {
  8143. LOG_INFO( "Opened power consumption record table successfully");
  8144. }
  8145. if (sqlite3_exec(db, createGunConsumptionSql, 0, 0, &errMsg) != SQLITE_OK)
  8146. {
  8147. result = FAIL;
  8148. LOG_ERROR( "Create gun consumption record table error message: %s", errMsg);
  8149. }
  8150. else
  8151. {
  8152. LOG_INFO( "Opened gun consumption record table successfully");
  8153. }
  8154. if (sqlite3_exec(db, createFinalCostSql, 0, 0, &errMsg) != SQLITE_OK)
  8155. {
  8156. result = FAIL;
  8157. LOG_ERROR( "Create final cost table error message: %s", errMsg);
  8158. }
  8159. else
  8160. {
  8161. LOG_INFO( "Opened final cost table successfully");
  8162. }
  8163. sqlite3_close(db);
  8164. }
  8165. return result;
  8166. }
  8167. int DB_Insert_Record(sqlite3 *db, int gun_index)
  8168. {
  8169. int result = PASS;
  8170. char* errMsg = NULL;
  8171. char insertSql[1024];
  8172. char transactionId[128];
  8173. char reason[32];
  8174. Get_Ocpp_TransactionId(gun_index, transactionId);
  8175. Get_Ocpp_StopReason(gun_index, reason);
  8176. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  8177. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  8178. chargingInfo[gun_index]->ReservationId,
  8179. transactionId,
  8180. chargingInfo[gun_index]->StartMethod,
  8181. chargingInfo[gun_index]->StartUserId,
  8182. chargingInfo[gun_index]->StartDateTime,
  8183. chargingInfo[gun_index]->StopDateTime,
  8184. chargingInfo[gun_index]->EvBatteryStartSoc,
  8185. chargingInfo[gun_index]->EvBatterySoc,
  8186. chargingInfo[gun_index]->PresentChargedEnergy,
  8187. reason);
  8188. if(sqlite3_open(DB_FILE, &db))
  8189. {
  8190. result = FAIL;
  8191. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8192. sqlite3_close(db);
  8193. }
  8194. else
  8195. {
  8196. //LOG_INFO( "Local charging record database open successfully.");
  8197. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8198. {
  8199. result = FAIL;
  8200. LOG_ERROR("Insert local charging record error message: %s", errMsg);
  8201. }
  8202. else
  8203. {
  8204. //LOG_INFO("Insert local charging record successfully");
  8205. }
  8206. sprintf(insertSql, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
  8207. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8208. {
  8209. result = FAIL;
  8210. LOG_ERROR("delete local charging error message: %s", errMsg);
  8211. }
  8212. else
  8213. {
  8214. //LOG_INFO("delete local charging record successfully");
  8215. }
  8216. sqlite3_close(db);
  8217. }
  8218. return result;
  8219. }
  8220. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t IsAvailable)
  8221. {
  8222. uint8_t result = false;
  8223. char* errMsg = NULL;
  8224. char sqlStr[1024];
  8225. srand(time(NULL));
  8226. if(sqlite3_open(DB_FILE, &db))
  8227. {
  8228. result = FAIL;
  8229. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8230. sqlite3_close(db);
  8231. }
  8232. else
  8233. {
  8234. LOG_INFO( "Local charging record database open successfully (%d).", IsAvailable);
  8235. sprintf(sqlStr, "insert or replace into config (IsAvailable, connector, val) values('IsAvailable', %d, %d);", gun_index, IsAvailable);
  8236. LOG_INFO("sqlStr= %s", sqlStr);
  8237. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8238. {
  8239. result = FAIL;
  8240. LOG_INFO( "update config error message: %s", errMsg);
  8241. }
  8242. else
  8243. {
  8244. LOG_INFO("update connector-%d config item isOperactive to %d", gun_index, IsAvailable);
  8245. }
  8246. sqlite3_close(db);
  8247. }
  8248. return result;
  8249. }
  8250. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  8251. {
  8252. uint8_t result = true;
  8253. char* errMsg = NULL;
  8254. char sqlStr[1024];
  8255. char **rs;
  8256. int rows, cols;
  8257. sprintf(sqlStr, "select * from config where IsAvailable='IsAvailable' and connector=%d;", gun_index);
  8258. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  8259. if(sqlite3_open(DB_FILE, &db))
  8260. {
  8261. result = FAIL;
  8262. LOG_ERROR( "Can't open database: %s", sqlite3_errmsg(db));
  8263. sqlite3_close(db);
  8264. }
  8265. else
  8266. {
  8267. LOG_INFO( "Local config query database open successfully.");
  8268. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8269. if(rows>0)
  8270. {
  8271. for(int idxRow=1;idxRow<=rows;idxRow++)
  8272. {
  8273. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  8274. {
  8275. result = false;
  8276. }
  8277. LOG_INFO("Query connector-%d isOperactive: %s", gun_index, rs[(idxRow*cols)+3]);
  8278. }
  8279. }
  8280. else
  8281. {
  8282. LOG_INFO("Query connector-%d fail, set default value to operactive.", gun_index);
  8283. }
  8284. sqlite3_free_table(rs);
  8285. sqlite3_close(db);
  8286. }
  8287. return result;
  8288. }
  8289. void DB_Update_LocalConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8290. {
  8291. char* errMsg = NULL;
  8292. char sqlStr[1024];
  8293. srand(time(NULL));
  8294. if(sqlite3_open(DB_FILE, &db))
  8295. {
  8296. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8297. sqlite3_close(db);
  8298. }
  8299. else
  8300. {
  8301. sprintf(sqlStr, "insert or replace into power_consumption (connector, val) values(%d, %f);", gun_index, value);
  8302. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8303. {
  8304. LOG_ERROR("update power_consumption error message: %s", errMsg);
  8305. }
  8306. else
  8307. {
  8308. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8309. }
  8310. sqlite3_close(db);
  8311. }
  8312. }
  8313. void DB_Update_GunConsumption(sqlite3 *db, uint8_t gun_index, float value)
  8314. {
  8315. char* errMsg = NULL;
  8316. char sqlStr[1024];
  8317. srand(time(NULL));
  8318. if(sqlite3_open(DB_FILE, &db))
  8319. {
  8320. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8321. sqlite3_close(db);
  8322. }
  8323. else
  8324. {
  8325. sprintf(sqlStr, "insert or replace into gun_consumption (connector, val) values(%d, %f);", gun_index, value);
  8326. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  8327. {
  8328. LOG_ERROR("update gun_consumption error message: %s", errMsg);
  8329. }
  8330. else
  8331. {
  8332. //LOG_INFO("update connector-%d power_consumption item consumption to %f \r\n", gun_index, value);
  8333. }
  8334. sqlite3_close(db);
  8335. }
  8336. }
  8337. float DB_Get_LocalConsumption(sqlite3 *db, uint8_t gun_index)
  8338. {
  8339. float result = 0;
  8340. char* errMsg = NULL;
  8341. char sqlStr[1024];
  8342. char **rs;
  8343. int rows, cols;
  8344. sprintf(sqlStr, "select * from power_consumption where connector=%d;", gun_index);
  8345. if (sqlite3_open(DB_FILE, &db))
  8346. {
  8347. result = FAIL;
  8348. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8349. sqlite3_close(db);
  8350. }
  8351. else
  8352. {
  8353. //LOG_INFO("Local config query database open successfully.");
  8354. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8355. if (rows > 0)
  8356. {
  8357. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8358. {
  8359. result = atof(rs[(idxRow * cols) + 2]);
  8360. }
  8361. }
  8362. else
  8363. {
  8364. LOG_INFO("Query connector-%d fail, set default value to consumption.", gun_index);
  8365. }
  8366. sqlite3_free_table(rs);
  8367. sqlite3_close(db);
  8368. }
  8369. return result;
  8370. }
  8371. float DB_Get_GunConsumption(sqlite3 *db, uint8_t gun_index)
  8372. {
  8373. float result = 0;
  8374. char* errMsg = NULL;
  8375. char sqlStr[1024];
  8376. char **rs;
  8377. int rows, cols;
  8378. sprintf(sqlStr, "select * from gun_consumption where connector=%d;", gun_index);
  8379. if (sqlite3_open(DB_FILE, &db))
  8380. {
  8381. result = FAIL;
  8382. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8383. sqlite3_close(db);
  8384. }
  8385. else
  8386. {
  8387. //LOG_INFO("Local config query database open successfully.");
  8388. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  8389. if (rows > 0)
  8390. {
  8391. for (int idxRow = 1; idxRow <= rows; idxRow++)
  8392. {
  8393. result = atof(rs[(idxRow * cols) + 2]);
  8394. }
  8395. }
  8396. else
  8397. {
  8398. LOG_INFO("Query gun %d fail, set -1 to gun consumption.", gun_index + 1);
  8399. result = -1;
  8400. }
  8401. sqlite3_free_table(rs);
  8402. sqlite3_close(db);
  8403. }
  8404. return result;
  8405. }
  8406. float Get_PowerConsumption(uint8_t gun_index)
  8407. {
  8408. float value = 0;
  8409. //value = ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0 ?
  8410. // ShmChargerInfo->MeterValue[gun_index].GunConsumption : ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8411. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  8412. {
  8413. if(!_useGunMeterValue[gun_index])
  8414. {
  8415. LOG_INFO("***** Gun %d Use GunMeterValue *****", gun_index + 1);
  8416. }
  8417. _useGunMeterValue[gun_index] = true;
  8418. value = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  8419. }
  8420. else
  8421. {
  8422. value = ShmChargerInfo->MeterValue[gun_index].LocalConsumption;
  8423. }
  8424. return value;
  8425. }
  8426. void Update_PowerConsumption(uint8_t gun_index)
  8427. {
  8428. chargingInfo[gun_index]->PowerConsumption = Get_PowerConsumption(gun_index);
  8429. }
  8430. int DB_Insert_FinalCost(sqlite3 *db, uint8_t gun_index, int txId, char *description)
  8431. {
  8432. char timeBuf[32];
  8433. int result = PASS;
  8434. char* errMsg = NULL;
  8435. char insertSql[1024];
  8436. getNowDatetime((unsigned char *)timeBuf);
  8437. sprintf(insertSql, "insert into final_cost(gun, transactionId, occurDatetime, updateDatetime, description, updateStatus) "
  8438. "values('%d', '%d', '%s', '%s', '%s', '%d');",
  8439. gun_index + 1,
  8440. txId,
  8441. timeBuf,
  8442. "",
  8443. description,
  8444. BILL_SATAUS_UNDISPOSED);
  8445. if(sqlite3_open(DB_FILE, &db))
  8446. {
  8447. result = FAIL;
  8448. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8449. sqlite3_close(db);
  8450. }
  8451. else
  8452. {
  8453. //LOG_INFO("FinalCost database open successfully.");
  8454. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8455. {
  8456. result = FAIL;
  8457. LOG_ERROR("Insert final_cost error message: %s", errMsg);
  8458. }
  8459. else
  8460. {
  8461. //LOG_INFO("Insert final_cost successfully");
  8462. }
  8463. sprintf(insertSql, "delete from final_cost where idx < (select idx from final_cost order by idx desc limit 1)-2000;");
  8464. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8465. {
  8466. result = FAIL;
  8467. LOG_ERROR("delete final_cost error message: %s", errMsg);
  8468. }
  8469. else
  8470. {
  8471. //LOG_INFO("delete final_cost successfully");
  8472. }
  8473. sqlite3_close(db);
  8474. }
  8475. return result;
  8476. }
  8477. int DB_Update_FinalCost_Status(sqlite3 *db, int txId, int status)
  8478. {
  8479. char timeBuf[32];
  8480. int result = PASS;
  8481. char* errMsg = NULL;
  8482. char updateSql[1024];
  8483. getNowDatetime((unsigned char *)timeBuf);
  8484. sprintf(updateSql, "update final_cost set updateStatus = %d, updateDatetime = '%s' where transactionId = '%d';",
  8485. status,
  8486. timeBuf,
  8487. txId);
  8488. if(sqlite3_open(DB_FILE, &db))
  8489. {
  8490. result = FAIL;
  8491. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8492. sqlite3_close(db);
  8493. }
  8494. else
  8495. {
  8496. if(sqlite3_exec(db, updateSql, 0, 0, &errMsg) != SQLITE_OK)
  8497. {
  8498. LOG_ERROR("update final_cost status error message: %s", errMsg);
  8499. result = FAIL;
  8500. }
  8501. else
  8502. {
  8503. //LOG_INFO("update final_cost txId[%d] status %d ok", txId, status);
  8504. }
  8505. sqlite3_close(db);
  8506. }
  8507. return result;
  8508. }
  8509. int DB_Get_Undisposed_FinalCost(sqlite3 *db, int gunIndex, char *costString)
  8510. {
  8511. int txId = -1;
  8512. char **rs;
  8513. int rows, cols;
  8514. char* errMsg = NULL;
  8515. char sqlString[1024];
  8516. sprintf(sqlString, "select * from final_cost where updateStatus = -1 and gun = '%d' order by idx asc limit 1;",
  8517. gunIndex + 1);
  8518. if(sqlite3_open(DB_FILE, &db))
  8519. {
  8520. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8521. sqlite3_close(db);
  8522. }
  8523. else
  8524. {
  8525. sqlite3_get_table(db, sqlString, &rs, &rows, &cols, &errMsg);
  8526. if(rows > 0)
  8527. {
  8528. for(int idxRow = 1; idxRow <= rows; idxRow++)
  8529. {
  8530. txId = atoi(rs[(idxRow * cols) + 2]);
  8531. strcpy(costString, rs[(idxRow * cols) + 5]);
  8532. }
  8533. }
  8534. sqlite3_free_table(rs);
  8535. sqlite3_close(db);
  8536. }
  8537. return txId;
  8538. }
  8539. void CheckInProgress(uint8_t gun_index)
  8540. {
  8541. if((ShmChargerInfo->Connector[gun_index].Status > S_IDLE && ShmChargerInfo->Connector[gun_index].Status <= S_FAULT) ||
  8542. (ShmChargerInfo->Connector[gun_index].Status >= S_CCS_PRECHARGE_ST0 && ShmChargerInfo->Connector[gun_index].Status <= S_CCS_PRECHARGE_ST1))
  8543. {
  8544. if(chargingInfo[gun_index]->InProgress == NO)
  8545. {
  8546. LOG_INFO("Gun %d In Progress", gun_index + 1);
  8547. }
  8548. chargingInfo[gun_index]->InProgress = YES;
  8549. }
  8550. else
  8551. {
  8552. if(chargingInfo[gun_index]->InProgress == YES)
  8553. {
  8554. LOG_INFO("Gun %d Real Idle", gun_index + 1);
  8555. }
  8556. chargingInfo[gun_index]->InProgress = NO;
  8557. }
  8558. }
  8559. void CheckDebugMode(uint8_t gun_index)
  8560. {
  8561. if(ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay)
  8562. {
  8563. if(chargingInfo[gun_index]->SystemStatus != S_DEBUG)
  8564. {
  8565. setChargerMode(gun_index, MODE_DEBUG);
  8566. }
  8567. }
  8568. }
  8569. int DB_Reboot_Record(sqlite3 *db)
  8570. {
  8571. int result = PASS;
  8572. char* errMsg = NULL;
  8573. char insertSql[256];
  8574. sprintf(insertSql, "insert into reboot_record(rebootDatetime) values(CURRENT_TIMESTAMP);");
  8575. if(sqlite3_open(DB_FILE, &db))
  8576. {
  8577. result = FAIL;
  8578. LOG_ERROR("Can't open database: %s", sqlite3_errmsg(db));
  8579. sqlite3_close(db);
  8580. }
  8581. else
  8582. {
  8583. LOG_INFO("Local charging record database open successfully.");
  8584. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8585. {
  8586. result = FAIL;
  8587. LOG_ERROR("Insert reboot record error message: %s", errMsg);
  8588. }
  8589. else
  8590. {
  8591. //LOG_INFO("Insert reboot record successfully");
  8592. }
  8593. sprintf(insertSql, "delete from reboot_record where idx < (select idx from reboot_record order by idx desc limit 1)-2000;");
  8594. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  8595. {
  8596. result = FAIL;
  8597. LOG_ERROR("delete reboot record error message: %s", errMsg);
  8598. }
  8599. else
  8600. {
  8601. //LOG_INFO("delete reboot record successfully");
  8602. }
  8603. sqlite3_close(db);
  8604. }
  8605. return result;
  8606. }
  8607. //===============================================
  8608. // Config process
  8609. //===============================================
  8610. void AddPlugInTimes(byte gunIndex)
  8611. {
  8612. if (chargingInfo[gunIndex]->Type == _Type_Chademo)
  8613. ShmSysConfigAndInfo->SysConfig.ChademoPlugInTimes += 1;
  8614. else if(chargingInfo[gunIndex]->Type == _Type_CCS_2)
  8615. ShmSysConfigAndInfo->SysConfig.Ccs2PlugInTimes += 1;
  8616. else if(chargingInfo[gunIndex]->Type == _Type_GB)
  8617. ShmSysConfigAndInfo->SysConfig.GbPlugInTimes += 1;
  8618. }
  8619. void ChangeStartOrStopDateTime(byte isStart, byte gunIndex)
  8620. {
  8621. char cmdBuf[32];
  8622. struct timeb csuTime;
  8623. struct tm *tmCSU;
  8624. ftime(&csuTime);
  8625. tmCSU = localtime(&csuTime.time);
  8626. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  8627. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  8628. tmCSU->tm_sec);
  8629. if (isStart)
  8630. strcpy((char *)chargingInfo[gunIndex]->StartDateTime, cmdBuf);
  8631. else
  8632. strcpy((char *)chargingInfo[gunIndex]->StopDateTime, cmdBuf);
  8633. }
  8634. void zipLogFiles()
  8635. {
  8636. const char* logPath = "/Storage/SystemLog";
  8637. // 獲取目錄
  8638. DIR* pDir = opendir(logPath);
  8639. if (pDir != NULL)
  8640. {
  8641. struct timeb csuTime;
  8642. struct tm *tmCSU;
  8643. ftime(&csuTime);
  8644. tmCSU = localtime(&csuTime.time);
  8645. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  8646. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  8647. // tmCSU->tm_sec);
  8648. // Read items inside the folder
  8649. struct dirent* pEntry = NULL;
  8650. while ((pEntry = readdir(pDir)) != NULL)
  8651. {
  8652. if (strcmp(pEntry->d_name, ".") != 0 &&
  8653. strcmp(pEntry->d_name, "..") != 0 &&
  8654. strncmp(pEntry->d_name, "[", 1) == 0 &&
  8655. strstr(pEntry->d_name, "tar") < 0)
  8656. {
  8657. char yearC[5];
  8658. unsigned short year = 0;
  8659. char monthC[3];
  8660. unsigned short month = 0;
  8661. yearC[4] = '\0';
  8662. strncpy(yearC, pEntry->d_name + 1, 4);
  8663. monthC[2] = '\0';
  8664. strncpy(monthC, pEntry->d_name + 6, 2);
  8665. year = atoi(yearC);
  8666. month = atoi(monthC);
  8667. if (year != 0)
  8668. {
  8669. if (year < tmCSU->tm_year + 1900 ||
  8670. (year >= tmCSU->tm_year + 1900 && month < tmCSU->tm_mon + 1))
  8671. {
  8672. LOG_INFO("tar file name : %s", pEntry->d_name);
  8673. char file[256];
  8674. memset(file, 0x00, sizeof(file));
  8675. strcat(file, "tar zcvf ");
  8676. strcat(file, logPath);
  8677. strncat(file, "/", 1);
  8678. strcat(file, pEntry->d_name);
  8679. strcat(file, ".tar");
  8680. strncat(file, " ", 1);
  8681. strcat(file, logPath);
  8682. strncat(file, "/", 1);
  8683. strcat(file, pEntry->d_name);
  8684. LOG_INFO("zip = %s \n", file);
  8685. system(file);
  8686. }
  8687. }
  8688. }
  8689. }
  8690. }
  8691. // Close folder
  8692. closedir(pDir);
  8693. }
  8694. void StopProcessingLoop()
  8695. {
  8696. for (;;)
  8697. {
  8698. CheckFactoryConfigFunction();
  8699. CheckFwUpdateFunction();
  8700. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  8701. {
  8702. ChkPrimaryStatus();
  8703. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 0)
  8704. {
  8705. LOG_INFO("Soft reboot for retry self-tets (Primary).");
  8706. KillAllTask();
  8707. sleep(3);
  8708. TryCloseWatchdog();
  8709. system("/usr/bin/run_evse_restart.sh");
  8710. return;
  8711. }
  8712. }
  8713. if(Is_Ocpp_ResetReq())
  8714. {
  8715. if(Chk_Ocpp_Reset_Type("Hard") || Chk_Ocpp_Reset_Type("Immediate"))
  8716. {
  8717. ShmChargerInfo->Control.SysCtrl.bits.NeedHardReset = true;
  8718. }
  8719. if(Chk_Ocpp_Reset_Type("Soft") || Chk_Ocpp_Reset_Type("OnIdle"))
  8720. {
  8721. ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  8722. }
  8723. }
  8724. CheckSoftHardReset();
  8725. sleep(1);
  8726. }
  8727. }
  8728. void CreateWatchdog(void)
  8729. {
  8730. wtdFd = InitWatchDog();
  8731. if(wtdFd < 0)
  8732. {
  8733. LOG_INFO("Watchdog Initial Fail");
  8734. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  8735. }
  8736. else
  8737. {
  8738. LOG_INFO("Watchdog Initial Success");
  8739. }
  8740. }
  8741. void TryCloseWatchdog(void)
  8742. {
  8743. if(wtdFd > 0)
  8744. {
  8745. write(wtdFd, "V", 1);
  8746. close(wtdFd);
  8747. }
  8748. }
  8749. void TryFeedWatchdog(void)
  8750. {
  8751. if(wtdFd > 0)
  8752. {
  8753. write(wtdFd, "a", 1);
  8754. }
  8755. }
  8756. bool IsParallelCabinetIdle(void)
  8757. {
  8758. bool result = true;
  8759. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  8760. {
  8761. result = false;
  8762. }
  8763. return result;
  8764. }
  8765. bool IsStandbyEnable(void)
  8766. {
  8767. bool result = true;
  8768. if(ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC)
  8769. {
  8770. time_t timep;
  8771. struct tm *tm;
  8772. time(&timep);
  8773. timep += (ShmChargerInfo->CabinetMiscValue.TimeOffset * 60);
  8774. tm = localtime(&timep);
  8775. if(tm->tm_hour >= 0 && tm->tm_hour < 8)
  8776. {
  8777. if(!_isStandbyEnable)
  8778. {
  8779. LOG_INFO("[TCC]Customer Enable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  8780. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  8781. }
  8782. _isStandbyEnable = true;
  8783. result = true;
  8784. }
  8785. else
  8786. {
  8787. if(_isStandbyEnable)
  8788. {
  8789. LOG_INFO("[TCC]Customer Disable Standby Function This Time[%02d:%02d:%02d], Offset[%d]",
  8790. tm->tm_hour, tm->tm_min, tm->tm_sec, ShmChargerInfo->CabinetMiscValue.TimeOffset);
  8791. }
  8792. _isStandbyEnable = false;
  8793. result = false;
  8794. }
  8795. }
  8796. return result;
  8797. }
  8798. bool IsGunIdle(int gunIndex, int idleType)
  8799. {
  8800. bool idle = true;
  8801. if(chargingInfo[gunIndex]->SystemStatus == S_IDLE &&
  8802. chargingInfo[gunIndex]->ConnectorPlugIn &&
  8803. idleType == GUN_IDLE_TYPE_WHOLE)
  8804. {
  8805. idle = false;
  8806. }
  8807. else if (chargingInfo[gunIndex]->SystemStatus != S_IDLE &&
  8808. chargingInfo[gunIndex]->SystemStatus != S_RESERVATION &&
  8809. chargingInfo[gunIndex]->SystemStatus != S_FAULT &&
  8810. chargingInfo[gunIndex]->SystemStatus != S_MAINTAIN)
  8811. {
  8812. idle = false;
  8813. }
  8814. return idle;
  8815. }
  8816. bool IsDispenserIdle(int dispenserIndex)
  8817. {
  8818. bool dispenserIdle = true;
  8819. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  8820. {
  8821. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  8822. if(!IsGunIdle(gun, GUN_IDLE_TYPE_NORMAL))
  8823. {
  8824. dispenserIdle = false;
  8825. break;
  8826. }
  8827. }
  8828. return dispenserIdle;
  8829. }
  8830. bool IsConnectorWholeIdle(void)
  8831. {
  8832. bool result = true;
  8833. for (byte count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  8834. {
  8835. if(!IsGunIdle(count, GUN_IDLE_TYPE_WHOLE))
  8836. {
  8837. result = false;
  8838. break;
  8839. }
  8840. }
  8841. return result;
  8842. }
  8843. bool IsUpgradeAvailable(void)
  8844. {
  8845. bool result = true;
  8846. for(int count = 0; count < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; count++)
  8847. {
  8848. if(!IsGunIdle(count, GUN_IDLE_TYPE_NORMAL))
  8849. {
  8850. result = false;
  8851. break;
  8852. }
  8853. }
  8854. return result;
  8855. }
  8856. void ClearAlarmCodeWhenAcOff()
  8857. {
  8858. if (!ShmSysConfigAndInfo->SysInfo.AcContactorStatus)
  8859. {
  8860. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = NO;
  8861. }
  8862. }
  8863. //==========================================
  8864. // Check task processing
  8865. //==========================================
  8866. void CheckTask()
  8867. {
  8868. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  8869. {
  8870. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == YES)
  8871. {
  8872. if(system("pidof -s Module_4g > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  8873. {
  8874. LOG_ERROR("Module_4g not running, restart it.");
  8875. system("/root/Module_4g &");
  8876. }
  8877. }
  8878. }
  8879. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W' || ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
  8880. {
  8881. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE)
  8882. {
  8883. if(system("pidof -s Module_Wifi > /dev/null") != 0 && ShmChargerInfo->Control.SysCtrl.bits.Wifi4gResetEnable == false)
  8884. {
  8885. LOG_ERROR("Module_Wifi not running, restart it.");
  8886. system("/root/Module_Wifi &");
  8887. }
  8888. }
  8889. }
  8890. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  8891. strcmp((char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != EQUAL &&
  8892. strcmp((char *)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, "") != EQUAL)
  8893. {
  8894. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = true;
  8895. if((time((time_t*)NULL) - Get_Ocpp_procDogTime()) > 180)
  8896. {
  8897. LOG_ERROR("OcppBackend watch dog timeout task restart.");
  8898. Clean_Ocpp_procDogTime();
  8899. Kill_Ocpp_Service();
  8900. }
  8901. if(!Is_Ocpp_Task_Alive())
  8902. {
  8903. LOG_ERROR("OcppBackend not running, restart it.");
  8904. Run_Ocpp_Service();
  8905. }
  8906. }
  8907. else
  8908. {
  8909. ShmChargerInfo->Control.SysCtrl.bits.BackendEnable = false;
  8910. }
  8911. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE &&
  8912. strcmp((char *)ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != EQUAL)
  8913. {
  8914. if(!Is_MaintainOcpp_Task_Alive())
  8915. {
  8916. LOG_ERROR("Maintain Backend not running, restart it.");
  8917. Run_MaintainOcpp_Service();
  8918. }
  8919. }
  8920. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  8921. {
  8922. LOG_ERROR("Module_ProduceUtils not running, restart it.");
  8923. system ("/root/Module_ProduceUtils &");
  8924. }
  8925. if(system("pidof -s Module_EvComm > /dev/null") != 0)
  8926. {
  8927. LOG_ERROR("Module_EvComm not running, restart it.");
  8928. system ("/root/Module_EvComm &");
  8929. }
  8930. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  8931. {
  8932. LOG_ERROR("Module_InternalComm not running, restart it.");
  8933. system ("/root/Module_InternalComm &");
  8934. //ShmChargerInfo->Control.SysCtrl.bits.NeedSoftReset = true;
  8935. }
  8936. if(!Is_WebServer_Alive())
  8937. {
  8938. LOG_ERROR("lighttpd not running, restart it.");
  8939. Run_WebServer_Service();
  8940. }
  8941. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  8942. {
  8943. if(!Is_LocalPowerSharing_Alive())
  8944. {
  8945. LOG_ERROR("Module_PowerSharing not running, restart it.");
  8946. Run_LocalPowerSharing_Service();
  8947. }
  8948. }
  8949. }
  8950. void InitialDHCP()
  8951. {
  8952. char tmpbuf[256];
  8953. memset(tmpbuf,0,256);
  8954. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  8955. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  8956. system(tmpbuf);
  8957. }
  8958. void InitialDispenserDhcpServerConfig(void)
  8959. {
  8960. system("echo 'start 192.168.100.10' > /etc/udhcpd_eth1.conf");
  8961. system("echo 'end 192.168.100.20' >> /etc/udhcpd_eth1.conf");
  8962. system("echo 'interface eth1' >> /etc/udhcpd_eth1.conf");
  8963. system("echo 'opt dns 8.8.8.8' >> /etc/udhcpd_eth1.conf");
  8964. system("echo 'option subnet 255.255.255.0' >> /etc/udhcpd_eth1.conf");
  8965. system("echo 'opt router 192.168.100.1' >> /etc/udhcpd_eth1.conf");
  8966. system("echo 'option domain local' >> /etc/udhcpd_eth1.conf");
  8967. system("echo 'option lease 86400' >> /etc/udhcpd_eth1.conf");
  8968. usleep(1000);
  8969. }
  8970. void StartDispenserDhcpServer(void)
  8971. {
  8972. system("pgrep -f \"udhcpd /etc/udhcpd_eth1.conf\" | xargs kill");
  8973. system("/usr/sbin/udhcpd /etc/udhcpd_eth1.conf > /dev/null &");
  8974. }
  8975. //==========================================
  8976. // Check Smart Charging Profile
  8977. //==========================================
  8978. void Ocpp_ProfileReq_Cmd(byte gunIndex)
  8979. {
  8980. if(!Is_Ocpp_ChargingProfileConf(gunIndex))
  8981. {
  8982. if(!Is_Ocpp_ChargingProfileReq(gunIndex))
  8983. {
  8984. Set_Ocpp_ChargingProfileReq(gunIndex);
  8985. }
  8986. }
  8987. }
  8988. int GetStartScheduleTime(unsigned char *time)
  8989. {
  8990. int result = -1;
  8991. struct tm tmScheduleStart;
  8992. struct timeb tbScheduleStart;
  8993. 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))
  8994. {
  8995. tmScheduleStart.tm_year -= 1900;
  8996. tmScheduleStart.tm_mon -= 1;
  8997. tbScheduleStart.time = mktime(&tmScheduleStart);
  8998. tbScheduleStart.millitm = 0;
  8999. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  9000. }
  9001. return result;
  9002. }
  9003. void Ocpp_ChargingProfile_Process(byte _index)
  9004. {
  9005. int _startCount = NO_DEFINE;
  9006. int _maxCount = 0;
  9007. float _limit = 0;
  9008. int _numberPhases = 0;
  9009. if(Is_Ocpp_ChargingProfileKind(_index, "Absolute") && Is_Ocpp_ChargingProfileId(_index))
  9010. {
  9011. _maxCount = Get_Ocpp_ChargingProfileScheduleCount(_index);
  9012. _startCount = Get_Ocpp_ChargingProfileStartIndex(_index);
  9013. if(_startCount < _maxCount)
  9014. {
  9015. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9016. _numberPhases = Get_Ocpp_ChargingProfileScheduleNumberPhases(_index, _startCount);
  9017. chargingInfo[_index]->ChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9018. if(chargingInfo[_index]->EvBatterytargetVoltage > 0 &&
  9019. (int)chargingInfo[_index]->PresentChargingVoltage > 0)
  9020. {
  9021. chargingInfo[_index]->ChargingProfileCurrent = (chargingInfo[_index]->ChargingProfilePower / chargingInfo[_index]->PresentChargingVoltage) * 10;
  9022. }
  9023. else
  9024. {
  9025. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9026. }
  9027. }
  9028. else
  9029. {
  9030. chargingInfo[_index]->ChargingProfilePower = -1;
  9031. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9032. }
  9033. }
  9034. else
  9035. {
  9036. chargingInfo[_index]->ChargingProfilePower = -1;
  9037. chargingInfo[_index]->ChargingProfileCurrent = -1;
  9038. }
  9039. if((int)_lastProfilePower[_index] != (int)chargingInfo[_index]->ChargingProfilePower ||
  9040. (int)_lastProfileCurrent[_index] != (int)chargingInfo[_index]->ChargingProfileCurrent)
  9041. {
  9042. _limit = Get_Ocpp_ChargingProfileScheduleLimit(_index, _startCount);
  9043. LOG_INFO("Gun %d Get Profile - Index = %d, Limit = %f",
  9044. _index + 1, _startCount, _limit);
  9045. LOG_INFO("Max: %d kW, Gun %d ChargingProfile Power = %d, Current = %d",
  9046. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower == -1 ? (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower : ((int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000),
  9047. _index + 1, (int)chargingInfo[_index]->ChargingProfilePower, (int)chargingInfo[_index]->ChargingProfileCurrent);
  9048. }
  9049. _lastProfilePower[_index] = chargingInfo[_index]->ChargingProfilePower;
  9050. _lastProfileCurrent[_index] = chargingInfo[_index]->ChargingProfileCurrent;
  9051. }
  9052. void Ocpp_MaxChargingProfile_Process(void)
  9053. {
  9054. int _startCount = NO_DEFINE;
  9055. int _maxCount = 0;
  9056. float _limit = 0;
  9057. int _numberPhases = 0;
  9058. if(Is_Ocpp_MaxChargingProfileKind("Absolute") && Is_Ocpp_MaxChargingProfileId())
  9059. {
  9060. _maxCount = Get_Ocpp_MaxProfileScheduleCount();
  9061. _startCount = Get_Ocpp_MaxProfileStartIndex();
  9062. if(_startCount < _maxCount)
  9063. {
  9064. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9065. _numberPhases = Get_Ocpp_MaxProfileScheduleNumberPhases(_startCount);
  9066. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = _limit * AC_OUTPUT_VOL * _numberPhases;
  9067. }
  9068. else
  9069. {
  9070. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9071. }
  9072. }
  9073. else
  9074. {
  9075. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower = -1;
  9076. }
  9077. if((int)_lastMaxProfilePower != (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower)
  9078. {
  9079. _limit = Get_Ocpp_MaxProfileScheduleLimit(_startCount);
  9080. LOG_INFO("Charger Get Max Profile - Index = %d, Limit = %f",
  9081. _startCount, _limit);
  9082. LOG_INFO("Charger Max Profile Power = %d", (int)ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower);
  9083. }
  9084. _lastMaxProfilePower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower;
  9085. }
  9086. void CheckSmartChargeProfile(byte _index)
  9087. {
  9088. if(Is_Ocpp_ChargingProfileConf(_index))
  9089. {
  9090. Clean_Ocpp_ChargingProfileConf(_index);
  9091. Ocpp_MaxChargingProfile_Process();
  9092. Ocpp_ChargingProfile_Process(_index);
  9093. }
  9094. }
  9095. void GetLocalPowerLimit(byte gunIndex)
  9096. {
  9097. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9098. {
  9099. if(ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  9100. {
  9101. float _localLimitPower = ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gunIndex] * AC_OUTPUT_VOL;
  9102. if(chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 && (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  9103. {
  9104. chargingInfo[gunIndex]->LocalPowerLimitCurrent = _localLimitPower / chargingInfo[gunIndex]->PresentChargingVoltage;
  9105. }
  9106. else
  9107. {
  9108. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9109. }
  9110. }
  9111. else
  9112. {
  9113. chargingInfo[gunIndex]->LocalPowerLimitCurrent = 0;
  9114. }
  9115. }
  9116. else
  9117. {
  9118. chargingInfo[gunIndex]->LocalPowerLimitCurrent = -1;
  9119. }
  9120. }
  9121. void TheEndCharging(byte gun_index)
  9122. {
  9123. chargingInfo[gun_index]->isRemoteStart = NO;
  9124. if(strlen((char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL) > 0 && ShmChargerInfo->UserTransaction[gun_index].TransactionId > 0)
  9125. {
  9126. char receiptString[512];
  9127. sprintf(receiptString, "%s%d",
  9128. (char *)&ShmSysConfigAndInfo->SysConfig.OcppReceiptrURL[0], ShmChargerInfo->UserTransaction[gun_index].TransactionId);
  9129. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.ReceiptUrl[gun_index][0], receiptString);
  9130. Set_Connector_MiscCommand(gun_index, MISC_CONN_RECEIPT);
  9131. LOG_INFO("Gun %d ReceiptrURL: [%s]", gun_index + 1, receiptString);
  9132. }
  9133. ChangeStartOrStopDateTime(NO, gun_index);
  9134. DB_Insert_Record(localDb, gun_index);
  9135. }
  9136. void UpdateErrorCodeToOcpp(byte index)
  9137. {
  9138. //printf("ConnectorAlarmCode = %s \n", chargingInfo[index]->ConnectorAlarmCode);
  9139. if (strcmp((char *)chargingInfo[index]->ConnectorAlarmCode, "") != EQUAL)
  9140. {
  9141. SetOcppErrorCode(index, "InternalError");
  9142. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->ConnectorAlarmCode);
  9143. }
  9144. else if (strcmp((char *)chargingInfo[index]->EvConnAlarmCode, "") != EQUAL)
  9145. {
  9146. SetOcppErrorCode(index, "OtherError");
  9147. SetOcppVendorErrorCode(index, (char *)chargingInfo[index]->EvConnAlarmCode);
  9148. }
  9149. }
  9150. void CheckMiscCommandRequirement(void)
  9151. {
  9152. BOOL NeedAnnouncement = FALSE;
  9153. unsigned char connector = 0;
  9154. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate != YES)
  9155. {
  9156. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9157. {
  9158. NeedAnnouncement = FALSE;
  9159. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_None &&
  9160. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].LocalStatus != _DS_Timeout)
  9161. {
  9162. if(ShmChargerInfo->DispenserMiscReq[i].CtrlValue != 0)
  9163. {
  9164. NeedAnnouncement = TRUE;
  9165. }
  9166. for(int j = 0; j < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorQuantity; j++)
  9167. {
  9168. connector = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].ConnectorID[j] - 1;
  9169. if(ShmChargerInfo->ConnectorMiscReq[connector].CtrlValue != 0)
  9170. {
  9171. NeedAnnouncement = TRUE;
  9172. }
  9173. }
  9174. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement != NeedAnnouncement)
  9175. {
  9176. if(NeedAnnouncement)
  9177. {
  9178. //LOG_INFO("********** Dispenser %d Misc Command Need Announcement **********", i + 1);
  9179. }
  9180. else
  9181. {
  9182. //LOG_INFO("********** Dispenser %d Misc Command Announced **********", i + 1);
  9183. }
  9184. }
  9185. }
  9186. ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[i].Setting.bits.MiscNeedAnnouncement = NeedAnnouncement;
  9187. }
  9188. }
  9189. }
  9190. void CheckDispenserVersionUpdateRequirement(void)
  9191. {
  9192. }
  9193. bool IsReadyParalleling(void)
  9194. {
  9195. bool ready = true;
  9196. for(int i = 0; i < ShmChargerInfo->Control.MaxConnector; i++)
  9197. {
  9198. if(chargingInfo[i]->SystemStatus != S_IDLE && chargingInfo[i]->SystemStatus != S_MAINTAIN)
  9199. {
  9200. ready = false;
  9201. break;
  9202. }
  9203. }
  9204. return ready;
  9205. }
  9206. void SetParallelingStart(void)
  9207. {
  9208. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9209. {
  9210. if(chargingInfo[i]->SystemStatus == S_IDLE)
  9211. {
  9212. setChargerMode(i, MODE_MAINTAIN);
  9213. }
  9214. }
  9215. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Wait;
  9216. }
  9217. void SetParallelingDone(void)
  9218. {
  9219. unsigned short totalPow = 0;
  9220. totalPow = GetTotalParallelCabinetPower();
  9221. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9222. {
  9223. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9224. }
  9225. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9226. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_None;
  9227. }
  9228. void SetPCabinetOutputRelay(unsigned char index, unsigned char OnOff)
  9229. {
  9230. unsigned char relay = 0;
  9231. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9232. {
  9233. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9234. {
  9235. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9236. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9237. {
  9238. relay = OnOff == YES ? YES : NO;
  9239. if(ShmChargerInfo->ParallelCabinet.PCabinet[index].OutputRelaySetting[index] != relay)
  9240. {
  9241. LOG_INFO("Set Parallel Cabinet Gun %d Output Relay %s", index + 1, relay == YES ? "ON" : "OFF");
  9242. }
  9243. ShmChargerInfo->ParallelCabinet.PCabinet[i].OutputRelaySetting[index] = relay;
  9244. }
  9245. }
  9246. }
  9247. }
  9248. void SetParallelCabinetAcContactor(unsigned char OnOff)
  9249. {
  9250. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9251. {
  9252. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle &&
  9253. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm)
  9254. {
  9255. ShmChargerInfo->ParallelCabinet.PCabinet[i].AcContactorSetting = OnOff == YES ? YES : NO;
  9256. }
  9257. }
  9258. }
  9259. unsigned short GetTotalParallelCabinetPower(void)
  9260. {
  9261. unsigned short power = 0, slavePow = 0;
  9262. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9263. {
  9264. for(int i = 0; i < MAX_SLAVE_CABINET_QUANTITY; i++)
  9265. {
  9266. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Idle ||
  9267. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Alarm ||
  9268. ShmChargerInfo->ParallelCabinet.PCabinet[i].LocalStatus == _DS_Charging)
  9269. {
  9270. slavePow = ParsingRatingPower((char *)ShmChargerInfo->ParallelCabinet.PCabinet[i].ModelName);
  9271. power += slavePow;
  9272. }
  9273. }
  9274. }
  9275. return power;
  9276. }
  9277. unsigned char preParallelStatus = 0;
  9278. void MCabinetParallelingProcess(void)
  9279. {
  9280. bool PsuReInitial = false;
  9281. unsigned char status = 0;
  9282. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9283. {
  9284. if(IsReadyParalleling())
  9285. {
  9286. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9287. {
  9288. if(ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest)
  9289. {
  9290. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelRequest = false;
  9291. ShmChargerInfo->ParallelCabinet.PCabinet[i].ParallelConfirm = true;
  9292. PsuReInitial = true;
  9293. }
  9294. }
  9295. if(PsuReInitial)
  9296. {
  9297. SetParallelingStart();
  9298. }
  9299. }
  9300. status = ShmChargerInfo->ParallelCabinet.ParallelStatus;
  9301. switch(status)
  9302. {
  9303. case _Parallel_None:
  9304. if(preParallelStatus != status)
  9305. {
  9306. LOG_INFO("MCabinet Parallel Status [None]");
  9307. GetClockTime(&_Parallel_Time);
  9308. }
  9309. unsigned short totalPow = 0;
  9310. totalPow = GetTotalParallelCabinetPower();
  9311. if(totalPow != ShmChargerInfo->ParallelCabinet.TotalParallelPower)
  9312. {
  9313. LOG_INFO("Total Parallel Cabinet Power: %d.%d kW", (totalPow / 10), (totalPow % 10));
  9314. }
  9315. ShmChargerInfo->ParallelCabinet.TotalParallelPower = totalPow;
  9316. break;
  9317. case _Parallel_Wait:
  9318. if(preParallelStatus != status)
  9319. {
  9320. LOG_INFO("MCabinet Parallel Status [Wait]");
  9321. SetAcContactor(OFF);
  9322. SetParallelCabinetAcContactor(OFF);
  9323. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9324. {
  9325. SetPCabinetOutputRelay(i, OFF);
  9326. }
  9327. GetClockTime(&_Parallel_Time);
  9328. }
  9329. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9330. {
  9331. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Working;
  9332. }
  9333. break;
  9334. case _Parallel_Working:
  9335. if(preParallelStatus != status)
  9336. {
  9337. LOG_INFO("MCabinet Parallel Status [Working]");
  9338. SetAcContactor(ON);
  9339. SetParallelCabinetAcContactor(ON);
  9340. GetClockTime(&_Parallel_Time);
  9341. }
  9342. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  9343. {
  9344. ShmChargerInfo->ParallelCabinet.ParallelStatus = _Parallel_Done;
  9345. }
  9346. break;
  9347. case _Parallel_Done:
  9348. if(preParallelStatus != status)
  9349. {
  9350. LOG_INFO("MCabinet Parallel Status [Done]");
  9351. GetClockTime(&_Parallel_Time);
  9352. }
  9353. if((GetTimeoutValue(_Parallel_Time) / uSEC_VAL) >= PARALLEL_WAIT_TIME)
  9354. {
  9355. SetParallelingDone();
  9356. }
  9357. break;
  9358. }
  9359. preParallelStatus = status;
  9360. }
  9361. }
  9362. void SCabinetControllProcess(void)
  9363. {
  9364. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  9365. {
  9366. if(ShmChargerInfo->SCabinetControl.SAcContactor)
  9367. {
  9368. SetAcContactor(ON);
  9369. }
  9370. else
  9371. {
  9372. SetAcContactor(OFF);
  9373. }
  9374. unsigned short _parallelRelay = 0;
  9375. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9376. {
  9377. _parallelRelay = ShmChargerInfo->SCabinetControl.SParallelRelay[i] > 0 ? (1 < i) : 0;
  9378. }
  9379. if(_parallelRelay != ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue)
  9380. {
  9381. LOG_INFO("SCabinet Set Parallel Relay %X -> %X", ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue, _parallelRelay);
  9382. }
  9383. ShmChargerInfo->PsuGrouping.ParallelRelayConfig.CtrlValue = _parallelRelay;
  9384. }
  9385. }
  9386. void CheckConnectorDisconnectionRecovery(unsigned char connector)
  9387. {
  9388. unsigned char dispenser = 0;
  9389. dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].ParentDispensetIndex;
  9390. if(ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenser].LocalStatus != _DS_Timeout)
  9391. {
  9392. LOG_INFO("Connector %d Disconnection Recovery", connector + 1);
  9393. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[connector].Parameter.bits.Disconnection = false;
  9394. }
  9395. }
  9396. bool ConnectorRequestToCharging(unsigned char connector)
  9397. {
  9398. unsigned char role = 0;
  9399. bool done = false;
  9400. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9401. switch(role)
  9402. {
  9403. case _GROLE_IDLE:
  9404. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest)
  9405. {
  9406. LOG_INFO("Request Gun %d To Start Charging", connector + 1);
  9407. }
  9408. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.ChargingRequest = true;
  9409. break;
  9410. case _GROLE_MASTER:
  9411. done = true;
  9412. break;
  9413. case _GROLE_SLAVE:
  9414. if(!ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest)
  9415. {
  9416. LOG_INFO("Gun %d Wait For Re-Grouping", connector + 1);
  9417. }
  9418. ShmPsuGrouping->GroupCollection[connector].GroupCtrl.bits.SlaveChargingRequest = true;
  9419. break;
  9420. case _GROLE_REQUEST_TO_CHARGING:
  9421. done = true;
  9422. break;
  9423. default:
  9424. break;
  9425. }
  9426. return done;
  9427. }
  9428. bool IsConnectorGroupingCompleted(unsigned char connector)
  9429. {
  9430. unsigned char role = 0;
  9431. bool completed = false;
  9432. role = ShmPsuGrouping->GroupCollection[connector].Role;
  9433. switch(role)
  9434. {
  9435. case _GROLE_MASTER:
  9436. completed = true;
  9437. break;
  9438. default:
  9439. break;
  9440. }
  9441. return completed;
  9442. }
  9443. void SelfTestFailReason(void)
  9444. {
  9445. char reason[50];
  9446. memset(reason, 0x00, sizeof(reason));
  9447. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip)
  9448. {
  9449. StatusCodeCompose(reason, "042251");
  9450. }
  9451. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail ||
  9452. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail ||
  9453. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9454. {
  9455. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RelayboardStestFail)
  9456. {
  9457. StatusCodeCompose(reason, "042280");
  9458. }
  9459. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FanboardStestFail)
  9460. {
  9461. StatusCodeCompose(reason, "042281");
  9462. }
  9463. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PrimaryStestFail)
  9464. {
  9465. StatusCodeCompose(reason, "042282");
  9466. }
  9467. }
  9468. else if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail ||
  9469. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  9470. {
  9471. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail)
  9472. {
  9473. StatusCodeCompose(reason, "042285");
  9474. }
  9475. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuModuleStestFail)
  9476. {
  9477. StatusCodeCompose(reason, "042286");
  9478. }
  9479. }
  9480. else
  9481. {
  9482. StatusCodeCompose(reason, "Self Test Fail");
  9483. }
  9484. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  9485. {
  9486. SetOcppErrorCode(gun_index, "InternalError");
  9487. SetOcppVendorErrorCode(gun_index, reason);
  9488. setChargerMode(gun_index, MODE_MAINTAIN);
  9489. }
  9490. }
  9491. bool IsAvailableBackToIdle(int gun_index)
  9492. {
  9493. if(_plugInHold[gun_index] == false &&
  9494. (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false || chargingInfo[gun_index]->ConnectorPlugIn == false))
  9495. {
  9496. _plugInHold[gun_index] = true;
  9497. GetClockTime(&_PlugInHold_Time[gun_index]);
  9498. LOG_INFO ("Gun %d Plug In Hold Delay", gun_index + 1);
  9499. }
  9500. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Idle ||
  9501. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Enable == false ||
  9502. (!chargingInfo[gun_index]->ConnectorPlugIn && ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm) ||
  9503. isResetStopChargeFlag(gun_index) == YES)
  9504. {
  9505. if(!_plugInHold[gun_index])
  9506. {
  9507. _plugInHold[gun_index] = true;
  9508. GetClockTime(&_PlugInHold_Time[gun_index]);
  9509. LOG_INFO ("Force Gun %d Plug In Hold Delay", gun_index + 1);
  9510. }
  9511. if((GetTimeoutValue(_PlugInHold_Time[gun_index]) / uSEC_VAL) >= PLUG_IN_HOLD_TIME)
  9512. {
  9513. return true;
  9514. }
  9515. }
  9516. return false;
  9517. }
  9518. void CheckGunAvailable(void)
  9519. {
  9520. if(!ShmChargerInfo->Control.SysCtrl.bits.PsuInit)
  9521. {
  9522. if(ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  9523. {
  9524. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9525. {
  9526. if(ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity > 0)
  9527. {
  9528. ShmChargerInfo->Control.GunAvailable[i] = YES;
  9529. }
  9530. }
  9531. ShmChargerInfo->Control.SysCtrl.bits.PsuInit = true;
  9532. }
  9533. }
  9534. }
  9535. int presentChargedEnergyClear(unsigned char gun_index)
  9536. {
  9537. int result = FAIL;
  9538. chargingInfo[gun_index]->PresentChargedEnergy = 0;
  9539. memset(chargingInfo[gun_index]->presentChargedEnergyPeriod, 0x00, ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod) * sizeof(float));
  9540. result = PASS;
  9541. return result;
  9542. }
  9543. float presentChargedEnergyTotal(unsigned char gun_index)
  9544. {
  9545. float result = 0.0f;
  9546. for(int idx = 0; idx < ARRAY_SIZE(chargingInfo[gun_index]->presentChargedEnergyPeriod); idx++)
  9547. {
  9548. result += chargingInfo[gun_index]->presentChargedEnergyPeriod[idx];
  9549. }
  9550. return result;
  9551. }
  9552. int presentChargedEnergyUpdate(unsigned char gun_index, float kwh)
  9553. {
  9554. int result = FAIL;
  9555. time_t CurrentTime;
  9556. struct tm *tm;
  9557. CurrentTime = time(NULL);
  9558. tm=localtime(&CurrentTime);
  9559. chargingInfo[gun_index]->presentChargedEnergyPeriod[tm->tm_hour] += kwh;
  9560. return result;
  9561. }
  9562. void ChargedEnergySummation(unsigned char gun_index)
  9563. {
  9564. unsigned long time = 0;
  9565. time = GetSecTimeoutValue(_ChargedEnergy_Time[gun_index]);
  9566. if(time >= (chargingInfo[gun_index]->PresentChargedDuration + ENERAGY_INTERVAL))
  9567. {
  9568. chargingInfo[gun_index]->PresentChargedDuration++;
  9569. float changedEnergy = 0;
  9570. float _meterEnergy = 0;
  9571. float _calChangingEnergy = (chargingInfo[gun_index]->PresentChargingPower) / 3600;
  9572. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= 0)
  9573. {
  9574. if(ShmChargerInfo->MeterValue[gun_index].GunConsumption >= _lsatGunMeterValue[gun_index])
  9575. {
  9576. _meterEnergy = ShmChargerInfo->MeterValue[gun_index].GunConsumption - _lsatGunMeterValue[gun_index];
  9577. }
  9578. else
  9579. {
  9580. _meterEnergy = 0;
  9581. }
  9582. changedEnergy = _meterEnergy;
  9583. _lsatGunMeterValue[gun_index] = ShmChargerInfo->MeterValue[gun_index].GunConsumption;
  9584. }
  9585. else
  9586. {
  9587. changedEnergy = _calChangingEnergy;
  9588. }
  9589. if(ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  9590. {
  9591. chargingInfo[gun_index]->ChargingFee += changedEnergy * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  9592. }
  9593. presentChargedEnergyUpdate(gun_index, changedEnergy);
  9594. chargingInfo[gun_index]->PresentChargedEnergy += changedEnergy;
  9595. ShmChargerInfo->MeterValue[gun_index].LocalConsumption += changedEnergy;
  9596. }
  9597. }
  9598. void ChkConnectorAction(void)
  9599. {
  9600. for(int i = 0; i < CONNECTOR_QUANTITY; i++)
  9601. {
  9602. if(ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel)
  9603. {
  9604. Clean_Ocpp_TcciSerialNo(i);
  9605. if(ShmChargerInfo->AllBill.OccupancyInfo[i].Status == _Parking_NoOccupancy ||
  9606. ShmChargerInfo->AllBill.OccupancyInfo[i].Status == _Parking_SyncOccupancy)
  9607. {
  9608. LOG_INFO("Gun %d Charging Canceled", i + 1);
  9609. }
  9610. else
  9611. {
  9612. LOG_INFO("Gun %d Occupancy Keep Counting", i + 1);
  9613. Set_OccupancyKeepCountingReq(i);
  9614. }
  9615. ShmChargerInfo->ConnectorActReq[i].Flag.bits.ChargingCancel = false;
  9616. }
  9617. }
  9618. }
  9619. void Update_DispenserAvailability(int dispenserIndex)
  9620. {
  9621. for(int i = 0; i < ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorQuantity; i++)
  9622. {
  9623. int gun = ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[dispenserIndex].ConnectorID[i] - 1;
  9624. Set_Connector_MiscCommand(gun, MISC_CONN_AVAILABILITY);
  9625. }
  9626. }
  9627. void InitialDataBaseValue(void)
  9628. {
  9629. // Local DB
  9630. if(DB_Open(localDb) != PASS)
  9631. {
  9632. LOG_INFO("DB_Open fail.");
  9633. isDb_ready = false;
  9634. }
  9635. else
  9636. {
  9637. isDb_ready = true;
  9638. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9639. {
  9640. chargingInfo[_index]->IsAvailable = DB_Get_Operactive(localDb, _index);
  9641. LOG_INFO("Gun %d IsAvailable Initial: [%s]", _index + 1, chargingInfo[_index]->IsAvailable == YES ? "Enable" : "Disable");
  9642. }
  9643. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9644. {
  9645. ShmChargerInfo->MeterValue[_index].LocalConsumption = DB_Get_LocalConsumption(localDb, _index);
  9646. LOG_INFO("Local Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  9647. }
  9648. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9649. {
  9650. ShmChargerInfo->MeterValue[_index].GunConsumption = DB_Get_GunConsumption(localDb, _index);
  9651. LOG_INFO("Remote Gun %d Consumption Initial: [%.4f kWh]", _index + 1, ShmChargerInfo->MeterValue[_index].GunConsumption);
  9652. }
  9653. for(int _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  9654. {
  9655. Update_PowerConsumption(_index);
  9656. LOG_INFO("Gun %d Power Consumption Initial: [%.4f kWh]", _index + 1, chargingInfo[_index]->PowerConsumption);
  9657. }
  9658. DB_Reboot_Record(localDb);
  9659. }
  9660. if (NetworkDB_Open(networkDb) != PASS)
  9661. {
  9662. LOG_ERROR("NetworkDB_Open fail.");
  9663. }
  9664. }
  9665. float CalculateMaxOutputRatio(int gunIndex)
  9666. {
  9667. float ratio = 0;
  9668. int inUsingCnt = 0;
  9669. for(int i = 0; i < GENERAL_GUN_QUANTITY; i++)
  9670. {
  9671. if(ShmChargerInfo->PsuGrouping.GroupCollection[i].Role == _GROLE_MASTER)
  9672. {
  9673. inUsingCnt += ShmChargerInfo->PsuGrouping.GroupCollection[i].GunPsuQuantity;
  9674. }
  9675. }
  9676. if(inUsingCnt != 0)
  9677. {
  9678. ratio = ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].Role == _GROLE_MASTER ?
  9679. ((float)ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].GunPsuQuantity / inUsingCnt) : 0;
  9680. }
  9681. ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio = ratio;
  9682. return ratio;
  9683. }
  9684. // return unit: 0.1kW
  9685. unsigned short GetModelNameLimitPower(int gunIndex)
  9686. {
  9687. unsigned short limitPower = 0;
  9688. float fPower = 0;
  9689. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9690. ShmChargerInfo->Control.ChargerRatingPower != 0)
  9691. {
  9692. fPower = (float)ShmChargerInfo->Control.ChargerRatingPower / 10;
  9693. if(ShmChargerInfo->Control.CabinetRole == _CROLE_MASTER)
  9694. {
  9695. fPower += (float)ShmChargerInfo->ParallelCabinet.TotalParallelPower / 10;
  9696. }
  9697. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9698. limitPower = (unsigned short)(fPower * 10);
  9699. }
  9700. return limitPower;
  9701. }
  9702. void GetWebPageConfigPowerLimit(int gunIndex)
  9703. {
  9704. unsigned short limitPower = 0;
  9705. float fPower = 0;
  9706. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9707. ShmSysConfigAndInfo->SysConfig.MaxChargingPower != 0)
  9708. {
  9709. fPower = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingPower;
  9710. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9711. limitPower = (unsigned short)(fPower * 10);
  9712. }
  9713. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower = limitPower;
  9714. }
  9715. void GetWebPageConfigCurrentLimit(int gunIndex)
  9716. {
  9717. unsigned short limitCurrent = 0;
  9718. float fCurrent = 0;
  9719. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9720. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent != 0)
  9721. {
  9722. fCurrent = (float)ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent;
  9723. fCurrent = fCurrent * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9724. limitCurrent = (unsigned short)(fCurrent * 10);
  9725. }
  9726. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent = limitCurrent;
  9727. }
  9728. void GetOffLineMaxChargingPowerLimit(int gunIndex)
  9729. {
  9730. unsigned short limitPower = 0;
  9731. float fPower = 0;
  9732. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9733. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower != 0 &&
  9734. ShmStatusCodeData->InfoCode.InfoEvents.bits.BackendDisconnectedViaEthernet == YES)
  9735. {
  9736. fPower = ShmChargerInfo->OutputLimit.Offline_MaxChargingPower / 1000;
  9737. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9738. limitPower = (unsigned short)(fPower * 10);
  9739. }
  9740. ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] = limitPower * 100;
  9741. }
  9742. void GetMaxChargingProfileLimit(int gunIndex)
  9743. {
  9744. unsigned short limitPower = 0, limitCurrent = 0;
  9745. float fPower = 0;
  9746. // max charging profile
  9747. if(ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio != 0 &&
  9748. ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower >= 0)
  9749. {
  9750. fPower = ShmSysConfigAndInfo->SysInfo.MaxChargingProfilePower / 1000;
  9751. fPower = fPower * ShmChargerInfo->PsuGrouping.GroupCollection[gunIndex].MaxOutputRatio;
  9752. limitPower = (unsigned short)(fPower * 10);
  9753. if (chargingInfo[gunIndex]->EvBatterytargetVoltage > 0 &&
  9754. (int)chargingInfo[gunIndex]->PresentChargingVoltage > 0)
  9755. {
  9756. limitCurrent = (limitPower * 100 / chargingInfo[gunIndex]->PresentChargingVoltage) * 10;
  9757. }
  9758. }
  9759. ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] = limitPower * 100;
  9760. ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] = limitCurrent;
  9761. }
  9762. void GetMaxChargingLimit(int gunIndex)
  9763. {
  9764. GetWebPageConfigPowerLimit(gunIndex);
  9765. GetWebPageConfigCurrentLimit(gunIndex);
  9766. GetOffLineMaxChargingPowerLimit(gunIndex);
  9767. GetMaxChargingProfileLimit(gunIndex);
  9768. }
  9769. void GetGunOtpDerating(int gunIndex)
  9770. {
  9771. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp.isNeedDerating)
  9772. {
  9773. struct DERATING_BY_OTP *_derating;
  9774. _derating = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].GeneralChargingData.deratingByConnOtp;
  9775. if(_derating->deratingIndex < GUN_DERATING_COUNT)
  9776. {
  9777. int _connectorTemp = (int)chargingInfo[gunIndex]->ConnectorTemp - 60;
  9778. int _chillerTemp = (int)chargingInfo[gunIndex]->ChillerTemp - 60;
  9779. if(_connectorTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  9780. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  9781. {
  9782. LOG_INFO("Gun %d GunTemp %d is Derating(%d) to %d A",
  9783. gunIndex + 1,
  9784. _connectorTemp,
  9785. GunDeratingTemp[_derating->deratingIndex],
  9786. (_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  9787. _derating->deratingIndex++;
  9788. }
  9789. else if(_chillerTemp >= GunDeratingTemp[_derating->deratingIndex] &&
  9790. _derating->deratingTargetCurrent[_derating->deratingIndex + 1] > 0)
  9791. {
  9792. LOG_INFO("Gun %d ChillerTemp %d is Derating(%d) to %d A",
  9793. gunIndex + 1,
  9794. _chillerTemp,
  9795. GunDeratingTemp[_derating->deratingIndex],
  9796. (_derating->deratingTargetCurrent[_derating->deratingIndex + 1] / 10));
  9797. _derating->deratingIndex++;
  9798. }
  9799. }
  9800. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = _derating->deratingTargetCurrent[_derating->deratingIndex];
  9801. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  9802. }
  9803. else
  9804. {
  9805. ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] = 0;
  9806. ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] = 0;
  9807. }
  9808. }
  9809. void CalculateGunAbsMaxPower(int gunIndex)
  9810. {
  9811. int maxValue = 0;
  9812. maxValue = (int)GetModelNameLimitPower(gunIndex);
  9813. ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] = maxValue != 0 ? (maxValue * 100) : 0;
  9814. }
  9815. void CalculateGunAbsMaxVoltage(int gunIndex)
  9816. {
  9817. int maxValue = 0;
  9818. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalVoltage;
  9819. ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] = maxValue;
  9820. }
  9821. void CalculateGunAbsMaxCurrent(int gunIndex)
  9822. {
  9823. int maxValue = 0;
  9824. maxValue = (int)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteMaxPhysicalCurrent;
  9825. ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] = maxValue;
  9826. }
  9827. void RestrictChargingCapability(int gunIndex, unsigned short *power, unsigned short *voltage, unsigned short *current)
  9828. {
  9829. unsigned short limitPower = 0;
  9830. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] != 0)
  9831. {
  9832. limitPower = ShmChargerInfo->OutputLimit.GunAbsoluteMaxPower[gunIndex] / 100;
  9833. if(limitPower != 0 && *power > limitPower)
  9834. {
  9835. *power = limitPower;
  9836. }
  9837. }
  9838. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex] != 0 &&
  9839. *voltage > ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex])
  9840. {
  9841. *voltage = ShmChargerInfo->OutputLimit.GunAbsoluteMaxVoltage[gunIndex];
  9842. }
  9843. if(ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex] != 0 &&
  9844. *current > ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex])
  9845. {
  9846. *current = ShmChargerInfo->OutputLimit.GunAbsoluteMaxCurrent[gunIndex];
  9847. }
  9848. // restrict from WebPageConfig power
  9849. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower != 0)
  9850. {
  9851. if(*power > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower)
  9852. {
  9853. *power = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingPower;
  9854. }
  9855. }
  9856. // restrict from WebPageConfig current
  9857. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent != 0)
  9858. {
  9859. if(*current > ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent)
  9860. {
  9861. *current = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].MaxTotalChargingCurrent;
  9862. }
  9863. }
  9864. // restrict from Offline_MaxChargingPower
  9865. if(ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] != 0)
  9866. {
  9867. limitPower = ShmChargerInfo->OutputLimit.GunOfflineMaxPower[gunIndex] / 100;
  9868. if(limitPower != 0 && *power > limitPower)
  9869. {
  9870. *power = limitPower;
  9871. }
  9872. }
  9873. // restrict from MaxChargingProfile
  9874. if(ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] != 0)
  9875. {
  9876. limitPower = ShmChargerInfo->OutputLimit.GunMaxProfilePower[gunIndex] / 100;
  9877. if(limitPower != 0 && *power > limitPower)
  9878. {
  9879. *power = limitPower;
  9880. }
  9881. }
  9882. // restrict from MaxChargingProfile
  9883. if(ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex] != 0)
  9884. {
  9885. if(*current > ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex])
  9886. {
  9887. *current = ShmChargerInfo->OutputLimit.GunMaxProfileCurrent[gunIndex];
  9888. }
  9889. }
  9890. // restrict from ChargingProfile
  9891. if((chargingInfo[gunIndex]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gunIndex]->SystemStatus <= S_CHARGING) ||
  9892. (chargingInfo[gunIndex]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gunIndex]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  9893. {
  9894. if(chargingInfo[gunIndex]->ChargingProfilePower >= 0 && *power > ((int)chargingInfo[gunIndex]->ChargingProfilePower / 100))
  9895. {
  9896. *power = (int)chargingInfo[gunIndex]->ChargingProfilePower / 100;
  9897. }
  9898. if(chargingInfo[gunIndex]->ChargingProfileCurrent >= 0 && *current > (int)chargingInfo[gunIndex]->ChargingProfileCurrent)
  9899. {
  9900. *current = (int)chargingInfo[gunIndex]->ChargingProfileCurrent;
  9901. }
  9902. }
  9903. // restrict from local power limit
  9904. if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > _SYS_POWER_SHARING_MODE_DISABLE)
  9905. {
  9906. if(chargingInfo[gunIndex]->LocalPowerLimitCurrent >= 0 && *current > (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10))
  9907. {
  9908. *current = (int)(chargingInfo[gunIndex]->LocalPowerLimitCurrent * 10);
  9909. }
  9910. }
  9911. // restrict after otp
  9912. if(ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] != 0)
  9913. {
  9914. limitPower = ShmChargerInfo->OutputLimit.GunOtpMaxPower[gunIndex] / 100;
  9915. if(limitPower != 0 && *power > limitPower)
  9916. {
  9917. *power = limitPower;
  9918. }
  9919. }
  9920. // restrict after otp
  9921. if(ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex] != 0 &&
  9922. *current > ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex])
  9923. {
  9924. *current = ShmChargerInfo->OutputLimit.GunOtpMaxCurrent[gunIndex];
  9925. }
  9926. }
  9927. void UpdateChargingCapability(void)
  9928. {
  9929. unsigned short voltage = 0, current = 0, power = 0;
  9930. ShmChargerInfo->OutputLimit.Offline_MaxChargingPower = Get_Ocpp_OffLineMaxChargingPower();
  9931. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  9932. {
  9933. voltage = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.MaximumChargingVoltage;
  9934. current = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingCurrent;
  9935. power = (unsigned short)ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].GeneralChargingData.AvailableChargingPower;
  9936. CalculateGunAbsMaxPower(i);
  9937. CalculateGunAbsMaxVoltage(i);
  9938. CalculateGunAbsMaxCurrent(i);
  9939. CalculateMaxOutputRatio(i);
  9940. GetMaxChargingLimit(i);
  9941. GetLocalPowerLimit(i);
  9942. GetGunOtpDerating(i);
  9943. RestrictChargingCapability(i, &power, &voltage, &current);
  9944. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityVoltage = voltage;
  9945. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityCurrent = current;
  9946. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[i].CapabilityPower = power;
  9947. }
  9948. }
  9949. void GunErrorCollection(void)
  9950. {
  9951. bool _PsuProtectionAlarm = false;
  9952. for(int i = 0; i < MAX_GUN_QUANTITY; i++)
  9953. {
  9954. _PsuProtectionAlarm |= ShmChargerInfo->GunError[i].ErrFlag.bits.PsuGroupAllAlarmFault;
  9955. }
  9956. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuProtectionAlarm = _PsuProtectionAlarm;
  9957. }
  9958. void UpdateGunPresentChargingPower(int gunIndex)
  9959. {
  9960. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].Enable)
  9961. {
  9962. chargingInfo[gunIndex]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gunIndex].RemoteChargingCurrent))) / 1000 / 100);
  9963. }
  9964. else
  9965. {
  9966. chargingInfo[gunIndex]->PresentChargingPower = ((float)((chargingInfo[gunIndex]->PresentChargingVoltage) * (chargingInfo[gunIndex]->PresentChargingCurrent)) / 1000);
  9967. }
  9968. }
  9969. void CheckPsuLostQuantity(void)
  9970. {
  9971. if(ShmPsuData->Work_Step == _WORK_CHARGING)
  9972. {
  9973. if(ShmChargerInfo->Control.PsuQuantityByModel == ShmPsuData->SystemPresentPsuQuantity)
  9974. {
  9975. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = false;
  9976. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = 0;
  9977. }
  9978. else
  9979. {
  9980. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity = ShmChargerInfo->Control.PsuQuantityByModel > ShmPsuData->SystemPresentPsuQuantity ?
  9981. (ShmChargerInfo->Control.PsuQuantityByModel - ShmPsuData->SystemPresentPsuQuantity) : 0;
  9982. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch ||
  9983. _psuLostQuantity != ShmSysConfigAndInfo->SysInfo.PsuLostQuantity)
  9984. {
  9985. LOG_INFO("Psu Quantity is %d, it should be %d, Psu Lost: %d",
  9986. ShmPsuData->SystemPresentPsuQuantity, ShmChargerInfo->Control.PsuQuantityByModel,
  9987. ShmSysConfigAndInfo->SysInfo.PsuLostQuantity);
  9988. }
  9989. ShmStatusCodeData->InfoCode.InfoEvents.bits.PsuQuantityNotMatch = true;
  9990. _psuLostQuantity = ShmSysConfigAndInfo->SysInfo.PsuLostQuantity;
  9991. }
  9992. }
  9993. }
  9994. int main(void)
  9995. {
  9996. unsigned long time = 0;
  9997. int _dispenser = 0;
  9998. _isStandbyEnable = true;
  9999. if(CreateShareMemory() == 0)
  10000. {
  10001. #ifdef SystemLogMessage
  10002. LOG_ERROR("CreatShareMemory NG");
  10003. #endif
  10004. if(ShmStatusCodeData!=NULL)
  10005. {
  10006. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  10007. }
  10008. sleep(5);
  10009. system("reboot -f");
  10010. sleep(5);
  10011. system("reboot -f");
  10012. }
  10013. LOG_INFO("\n");
  10014. LOG_INFO("===== Boot and Initialization start =====");
  10015. LOG_INFO("Main Fork PID = %d", getpid());
  10016. if (!InitialSystemDefaultConfig())
  10017. {
  10018. LOG_INFO("InitialSystemDefaultConfig NG");
  10019. StopProcessingLoop();
  10020. }
  10021. LOG_INFO("ModelName: [%s], SN: [%s], SW Version: [%s][%s]",
  10022. ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber, fwVersion, subVersion);
  10023. LOG_INFO("InitialShareMemoryInfo");
  10024. InitialShareMemoryInfo();
  10025. LOG_INFO("ChargerType (IEC or UL) = %d", ShmSysConfigAndInfo->SysInfo.ChargerType);
  10026. if(!InitialChargerSetting())
  10027. {
  10028. isModelNameMatch = false;
  10029. }
  10030. Initialization();
  10031. InitialDataBaseValue();
  10032. LOG_INFO("Spawn all Task.");
  10033. SpawnTask();
  10034. if (!isModelNameMatch)
  10035. {
  10036. LOG_INFO("Module Name & HW info none match.");
  10037. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.ModelNameNoneMatchStestFail = YES;
  10038. // Module Name 與硬體對應不正確
  10039. LOG_ERROR("Module Name & HW info none match.");
  10040. sleep(3);
  10041. KillAllTask();
  10042. StopProcessingLoop();
  10043. }
  10044. LOG_INFO("Module Name & HW info correct. Initialize.......");
  10045. CreateTimeoutFork();
  10046. SelfTestRun();
  10047. while(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE)
  10048. {
  10049. sleep(1);
  10050. }
  10051. if(ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE)
  10052. {
  10053. sleep(1);
  10054. SelfTestFailReason();
  10055. }
  10056. else
  10057. {
  10058. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10059. {
  10060. setChargerMode(gun_index, MODE_IDLE);
  10061. if(ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity > 0 ||
  10062. !ShmChargerInfo->Control.PsuCtrl.bits.SelfTestOK)
  10063. {
  10064. ShmChargerInfo->Control.GunAvailable[gun_index] = YES;
  10065. }
  10066. }
  10067. }
  10068. if(ShmChargerInfo->Control.CabinetRole != _CROLE_SLAVE)
  10069. {
  10070. SetAcContactor(ON);
  10071. SpawnOcppService();
  10072. SpawnMaintainOcppService();
  10073. }
  10074. else
  10075. {
  10076. InitSlaveCabinetEthernet();
  10077. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10078. {
  10079. setChargerMode(gun_index, MODE_MAINTAIN);
  10080. }
  10081. }
  10082. sleep(1);
  10083. CreateWatchdog();
  10084. // Main loop
  10085. LOG_INFO("%s \n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq == _STEST_COMPLETE ?
  10086. "****************************Main Loop**********************************" :
  10087. "********************** SelfTest Fail **********************");
  10088. GetClockTime(&_cmdMainPriority_time);
  10089. for (;;)
  10090. {
  10091. CheckGunAvailable();
  10092. CheckOcppStatus();
  10093. ChkPrimaryStatus();
  10094. ChkPsuStatus();
  10095. PsuFailureResume();
  10096. ChkConnectorAction();
  10097. if(IsUpgradeAvailable())
  10098. {
  10099. CheckFactoryConfigFunction();
  10100. CheckFwUpdateFunction();
  10101. }
  10102. if(CheckFlashUpdate() != YES)
  10103. {
  10104. // return fork process
  10105. return 0;
  10106. }
  10107. CheckSoftHardReset();
  10108. StandbyCheck();
  10109. // 讀卡邏輯
  10110. //ScannerCardProcess();
  10111. AuthorizeTimeoutProcess();
  10112. // 當 AC 沒有搭上時,清除一些錯誤碼
  10113. ClearAlarmCodeWhenAcOff();
  10114. if ((GetTimeoutValue(_cmdMainPriority_time) / uSEC_VAL) > MAIN_PRIORITY_INTERVAL)
  10115. {
  10116. CheckTask();
  10117. for (byte _index = 0; _index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; _index++)
  10118. {
  10119. if((chargingInfo[_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[_index]->SystemStatus <= S_CHARGING) ||
  10120. (chargingInfo[_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10121. {
  10122. if (chargingInfo[_index]->SystemStatus == S_CHARGING && _ocppProfileChkFlag[_index] == 12)
  10123. {
  10124. Ocpp_ProfileReq_Cmd(_index);
  10125. gunOutputVol[_index] = chargingInfo[_index]->PresentChargingVoltage;
  10126. _ocppProfileChkFlag[_index] = 0;
  10127. }
  10128. else if (chargingInfo[_index]->SystemStatus != S_CHARGING)
  10129. {
  10130. Ocpp_ProfileReq_Cmd(_index);
  10131. _ocppProfileChkFlag[_index] = 0;
  10132. }
  10133. else
  10134. {
  10135. _ocppProfileChkFlag[_index]++;
  10136. }
  10137. }
  10138. if (chargingInfo[_index]->SystemStatus >= SYS_MODE_CHARGING &&
  10139. chargingInfo[_index]->SystemStatus <= SYS_MODE_ALARM)
  10140. {
  10141. DB_Update_LocalConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].LocalConsumption);
  10142. if(ShmChargerInfo->MeterValue[_index].GunConsumption >= 0)
  10143. {
  10144. DB_Update_GunConsumption(localDb, _index, ShmChargerInfo->MeterValue[_index].GunConsumption);
  10145. }
  10146. }
  10147. }
  10148. CheckPsuLostQuantity();
  10149. GetClockTime(&_cmdMainPriority_time);
  10150. }
  10151. CheckMiscCommandRequirement();
  10152. CheckDispenserVersionUpdateRequirement();
  10153. MCabinetParallelingProcess();
  10154. SCabinetControllProcess();
  10155. UpdateChargingCapability();
  10156. GunErrorCollection();
  10157. for (byte gun_index = 0; gun_index < ShmSysConfigAndInfo->SysConfig.TotalConnectorCount; gun_index++)
  10158. {
  10159. //CheckGpioInStatus();
  10160. CheckErrorOccurStatus(gun_index);
  10161. ChkOcppStatus(gun_index);
  10162. if((chargingInfo[gun_index]->SystemStatus >= S_PREPARING_FOR_EVSE && chargingInfo[gun_index]->SystemStatus <= S_CHARGING) ||
  10163. (chargingInfo[gun_index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingInfo[gun_index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  10164. {
  10165. if(chargingInfo[gun_index]->SystemStatus == S_CHARGING)
  10166. {
  10167. // 進充電後,如果輸出電壓落差大於 10V 則直接繼續看 ChargingProfile
  10168. if (chargingInfo[gun_index]->PresentChargingVoltage >= gunOutputVol[gun_index] + 10 ||
  10169. chargingInfo[gun_index]->PresentChargingVoltage <= gunOutputVol[gun_index] - 10)
  10170. {
  10171. _ocppProfileChkFlag[gun_index] = 12;
  10172. gunOutputVol[gun_index] = chargingInfo[gun_index]->PresentChargingVoltage;
  10173. }
  10174. }
  10175. }
  10176. CheckSmartChargeProfile(gun_index);
  10177. Update_PowerConsumption(gun_index);
  10178. CheckInProgress(gun_index);
  10179. CheckDebugMode(gun_index);
  10180. //LOG_INFO("index = %d, ErrorCode = %s \n", gun_index, ShmOCPP16Data->StatusNotification[gun_index].ErrorCode);
  10181. switch(chargingInfo[gun_index]->SystemStatus)
  10182. {
  10183. case S_MAINTAIN:
  10184. if(isModeChange(gun_index))
  10185. {
  10186. if(ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10187. {
  10188. LOG_INFO("================== Slave Cabinet %d ==================", ShmChargerInfo->Control.CabinetSwitch - 1);
  10189. }
  10190. LOG_INFO("================== S_MAINTAIN (%x) ================== \n", gun_index + 1);
  10191. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10192. GetClockTime(&_SystemStatus_Time[gun_index]);
  10193. }
  10194. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  10195. if(ShmSysConfigAndInfo->SysInfo.BootingStatus != BOOT_COMPLETE ||
  10196. ShmSysConfigAndInfo->SysInfo.SelfTestSeq != _STEST_COMPLETE ||
  10197. ShmChargerInfo->Control.CabinetRole == _CROLE_SLAVE)
  10198. {
  10199. break;
  10200. }
  10201. if(chargingInfo[gun_index]->IsAvailable == YES &&
  10202. ShmChargerInfo->Control.GunAvailable[gun_index] == YES &&
  10203. ShmChargerInfo->ParallelCabinet.ParallelStatus == _Parallel_None)
  10204. {
  10205. if(time >= MAINTAIN_RECOVERY_TIME)
  10206. {
  10207. Set_Connector_MiscCommand(gun_index, MISC_CONN_AVAILABILITY);
  10208. setChargerMode(gun_index, MODE_IDLE);
  10209. }
  10210. }
  10211. break;
  10212. case S_RESERVATION:
  10213. if(isModeChange(gun_index))
  10214. {
  10215. LOG_INFO("================== S_RESERVATION (%x) ================== \n", gun_index + 1);
  10216. char expiryDate[128];
  10217. Get_Ocpp_ReserveNowExpiryDate(gun_index, expiryDate);
  10218. Get_Ocpp_ReserveNowIdTag(gun_index, &ShmChargerInfo->Control.ResevedIdTag[gun_index][0]);
  10219. Set_Connector_MiscCommand(gun_index, MISC_CONN_RESERVATION);
  10220. LOG_INFO("****** Reservation IdTag: [%s], Expiry: [%s]", &ShmChargerInfo->Control.ResevedIdTag[gun_index][0], expiryDate);
  10221. GetClockTime(&_SystemStatus_Time[gun_index]);
  10222. }
  10223. if(chargingInfo[gun_index]->ConnectorPlugIn)
  10224. {
  10225. SetAcContactor(ON);
  10226. }
  10227. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  10228. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  10229. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  10230. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10231. {
  10232. UpdateErrorCodeToOcpp(gun_index);
  10233. setChargerMode(gun_index, MODE_FAULT);
  10234. continue;
  10235. }
  10236. if(Is_Ocpp_ReserveNowExpired(gun_index))
  10237. {
  10238. chargingInfo[gun_index]->ReservationId = -1;
  10239. LOG_INFO("Gun %d reservation is expired...", gun_index + 1);
  10240. setChargerMode(gun_index, MODE_IDLE);
  10241. }
  10242. else
  10243. {
  10244. if(IsConnectorAuthorizeSuccess(gun_index))
  10245. {
  10246. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10247. {
  10248. LOG_INFO("Gun %d remote start reserve charging", gun_index + 1);
  10249. }
  10250. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  10251. {
  10252. LOG_INFO("Gun %d swipe rfid start reserve charging", gun_index + 1);
  10253. }
  10254. setChargerMode(gun_index, MODE_AUTHORIZING);
  10255. }
  10256. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  10257. {
  10258. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  10259. if(Is_Ocpp_ReserveNowIdTag(gun_index, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId))
  10260. {
  10261. SetAcContactor(ON);
  10262. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  10263. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10264. {
  10265. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  10266. LOG_INFO("Gun %d reserve Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  10267. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  10268. {
  10269. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  10270. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  10271. }
  10272. DispenserAuthInitial(_dispenser);
  10273. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10274. }
  10275. }
  10276. }
  10277. else
  10278. {
  10279. if(chargingInfo[gun_index]->ReservationId == -1)
  10280. {
  10281. LOG_INFO("Gun %d reservation is clean", gun_index + 1);
  10282. setChargerMode(gun_index, MODE_IDLE);
  10283. }
  10284. }
  10285. }
  10286. break;
  10287. case S_IDLE:
  10288. case S_FAULT:
  10289. {
  10290. // clean PermissionRequest flag
  10291. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = false;
  10292. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  10293. if(isModeChange(gun_index))
  10294. {
  10295. if (chargingInfo[gun_index]->SystemStatus == S_IDLE)
  10296. {
  10297. LOG_INFO("================== S_IDLE (%x) ================== \n", gun_index + 1);
  10298. chargingInfo[gun_index]->ReservationId = -1;
  10299. _triggerTransaction[gun_index] = false;
  10300. _plugInHold[gun_index] = false;
  10301. chargingInfo[gun_index]->PresentChargedDuration = 0;
  10302. chargingInfo[gun_index]->RemainChargingDuration = 0;
  10303. strcpy((char *)chargingInfo[gun_index]->StartDateTime, "");
  10304. strcpy((char *)chargingInfo[gun_index]->StopDateTime, "");
  10305. strcpy((char *)chargingInfo[gun_index]->StartUserId, "");
  10306. memset(ShmChargerInfo->Timestamp[gun_index].StartCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StartCharging));
  10307. memset(ShmChargerInfo->Timestamp[gun_index].StopCharging, 0x00, sizeof(ShmChargerInfo->Timestamp[gun_index].StopCharging));
  10308. Clean_Ocpp_StopReason(gun_index);
  10309. ReleaseAlarmCode(gun_index);
  10310. // clean connector information
  10311. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage = 0;
  10312. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent = 0;
  10313. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteRemainChargingDuration = 0;
  10314. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteSoc = 0;
  10315. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].UserPrice = 0;
  10316. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].TotalCost = 0;
  10317. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AccountBalance = -65535;
  10318. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].EnergyCost = -1;
  10319. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParkingFee = -1;
  10320. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AnnounceBalance = false;
  10321. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.GetStartChargingSoc = false;
  10322. memset(&ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo, 0x00, sizeof(AuthorizingInfoData));
  10323. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  10324. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  10325. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.OutputLimitEnable = false;
  10326. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].GeneralChargingData.deratingByConnOtp.deratingIndex = 0;
  10327. chargingInfo[gun_index]->EvBatterytargetVoltage = 0;
  10328. chargingInfo[gun_index]->EvBatterytargetCurrent = 0;
  10329. chargingInfo[gun_index]->EvBatteryMaxVoltage = 0;
  10330. chargingInfo[gun_index]->EvBatterySoc = 0;
  10331. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetVoltage = 0;
  10332. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteTargetCurrent = 0;
  10333. // clean RemoteStart & RemoteStop flag
  10334. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_START);
  10335. Clean_Connector_MiscCommand(gun_index, MISC_CONN_REMOTE_STOP);
  10336. Clean_Connector_MiscCommand(gun_index, MISC_CONN_UNLOCK);
  10337. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  10338. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStopConfirm = false;
  10339. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.UnlockStopConfirm = false;
  10340. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NormalStopRequest = false;
  10341. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.AlarmStopRequest = false;
  10342. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  10343. ShmChargerInfo->GunError[gun_index].ErrFlag.ErrorValue = 0;
  10344. memset(chargingInfo[gun_index]->ConnectorAlarmCode, 0x00, sizeof(chargingInfo[gun_index]->ConnectorAlarmCode));
  10345. Clean_Ocpp_RunningCost(gun_index);
  10346. Clean_Ocpp_FinalCost(gun_index);
  10347. Clean_DeductInfo(gun_index);
  10348. Clean_Ocpp_TcciSerialNo(gun_index);
  10349. Clean_UserTransaction(gun_index);
  10350. Clean_UserPriceAndReceipt(gun_index);
  10351. presentChargedEnergyClear(gun_index);
  10352. chargingInfo[gun_index]->PresentChargingPower = 0;
  10353. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingPower = 0;
  10354. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxTotalChargingCurrent = 0;
  10355. chargingInfo[gun_index]->ChargingProfilePower = -1;
  10356. chargingInfo[gun_index]->ChargingProfileCurrent = -1;
  10357. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  10358. {
  10359. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging = false;
  10360. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger = false;
  10361. // reset dispenser availability
  10362. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  10363. }
  10364. // clean force charging info
  10365. memset(&ShmChargerInfo->Control.FCharging[gun_index], 0x00, sizeof(ForceCharging));
  10366. }
  10367. else if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  10368. {
  10369. LOG_INFO("================== S_FAULT (%x) ================== \n", gun_index + 1);
  10370. }
  10371. GetClockTime(&_SystemStatus_Time[gun_index]);
  10372. }
  10373. if (chargingInfo[gun_index]->IsAvailable == NO ||
  10374. ShmChargerInfo->Control.GunAvailable[gun_index] == NO)
  10375. {
  10376. setChargerMode(gun_index, MODE_MAINTAIN);
  10377. continue;
  10378. }
  10379. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2 ||
  10380. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Alarm ||
  10381. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest ||
  10382. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10383. {
  10384. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest)
  10385. {
  10386. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  10387. {
  10388. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemotenAlarmCode, 6);
  10389. }
  10390. }
  10391. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.FaultStatusRequest = false;
  10392. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10393. {
  10394. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  10395. {
  10396. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "042304", 6);
  10397. LOG_INFO("Connector %d Disconnect Occur", gun_index + 1);
  10398. }
  10399. }
  10400. if(chargingInfo[gun_index]->SystemStatus != S_FAULT)
  10401. {
  10402. UpdateErrorCodeToOcpp(gun_index);
  10403. setChargerMode(gun_index, MODE_FAULT);
  10404. }
  10405. else
  10406. {
  10407. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.Disconnection)
  10408. {
  10409. CheckConnectorDisconnectionRecovery(gun_index);
  10410. }
  10411. }
  10412. }
  10413. else
  10414. {
  10415. if (chargingInfo[gun_index]->SystemStatus == S_FAULT)
  10416. {
  10417. time = GetSecTimeoutValue(_SystemStatus_Time[gun_index]);
  10418. if(time >= FAULT_RELEASE_TIME)
  10419. {
  10420. ReleaseAlarmCode(gun_index);
  10421. setChargerMode(gun_index, MODE_IDLE);
  10422. }
  10423. break;
  10424. }
  10425. // clean stop charge flag
  10426. chargingInfo[gun_index]->StopChargeFlag = false;
  10427. chargingInfo[gun_index]->ChargingStopFlag.ChargingStopValue = 0;
  10428. if(chargingInfo[gun_index]->ConnectorPlugIn)
  10429. {
  10430. SetAcContactor(ON);
  10431. }
  10432. { // Idle 正常程序起點
  10433. // 判斷是否有啟用檢查插槍
  10434. if(IsConnectorAuthorizeSuccess(gun_index))
  10435. {
  10436. if(chargingInfo[gun_index]->IsAvailable &&
  10437. chargingInfo[gun_index]->SystemStatus == S_IDLE)
  10438. {
  10439. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10440. {
  10441. LOG_INFO("Gun %d remote start charging", gun_index + 1);
  10442. Clean_Ocpp_TcciCreditInfo(gun_index);
  10443. }
  10444. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RFID)
  10445. {
  10446. LOG_INFO("Gun %d swipe rfid start charging", gun_index + 1);
  10447. }
  10448. setChargerMode(gun_index, MODE_AUTHORIZING);
  10449. continue;
  10450. }
  10451. }
  10452. else if(IsAutoGunSelectionAuthorizeSuccess(gun_index))
  10453. {
  10454. SetAcContactor(ON);
  10455. if(chargingInfo[gun_index]->IsAvailable &&
  10456. chargingInfo[gun_index]->SystemStatus == S_IDLE &&
  10457. chargingInfo[gun_index]->ConnectorPlugIn &&
  10458. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10459. {
  10460. _dispenser = ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex;
  10461. strcpy((char *)chargingInfo[gun_index]->StartUserId, (char *)ShmSysConfigAndInfo->SysInfo.DispenserInfo.Dispenser[_dispenser].AuthInfo.AuthId);
  10462. LOG_INFO("Gun %d Auto Selection, CardNumber = %s", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  10463. if(strlen((char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]) > 0)
  10464. {
  10465. strcpy((char *)&ShmChargerInfo->PriceAndReceiptInfo.UserPriceString[gun_index], (char *)&ShmChargerInfo->PriceAndReceiptInfo.DispenserPriceString[_dispenser][0]);
  10466. Set_Connector_MiscCommand(gun_index, MISC_CONN_USER_PRICE_STRING);
  10467. }
  10468. DispenserAuthInitial(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  10469. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10470. continue;
  10471. }
  10472. }
  10473. // only for test & debug purpose
  10474. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging &&
  10475. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging)
  10476. {
  10477. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.StartForceCharging = false;
  10478. strcpy((char *)chargingInfo[gun_index]->StartUserId, "ChargingTest");
  10479. LOG_INFO("Gun %d Start Force Charging%s",
  10480. gun_index + 1,
  10481. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger > 0 ? ", Triggered by WebAPI" : "");
  10482. if(ShmChargerInfo->Control.TestCtrl.bits.FChargingReleaseExtend)
  10483. {
  10484. ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableReleaseAndExtend = true;
  10485. LOG_INFO("Gun %d Enable ReleaseAndExtend At Force Charging", gun_index + 1);
  10486. }
  10487. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.WebApiTrigger)
  10488. {
  10489. LOG_INFO("Set Dispenser %d Inoperative",
  10490. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex + 1);
  10491. Update_DispenserAvailability(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].ParentDispensetIndex);
  10492. }
  10493. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10494. continue;
  10495. }
  10496. else
  10497. {
  10498. }
  10499. } // Idle 正常程序終點
  10500. }
  10501. }
  10502. break;
  10503. case S_AUTHORIZING:
  10504. if(isModeChange(gun_index))
  10505. {
  10506. LOG_INFO("================== S_AUTHORIZING (%x) ================== \n", gun_index + 1);
  10507. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10508. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  10509. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  10510. //StopSystemTimeoutDet();
  10511. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  10512. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  10513. SetAcContactor(ON);
  10514. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  10515. }
  10516. if(!_triggerTransaction[gun_index] &&
  10517. ShmChargerInfo->Control.CustomerCode == _CUSTOMER_CODE_TCC &&
  10518. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].AuthInfo.AuthType == _AuthType_RemoteStart)
  10519. {
  10520. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= STARTTRANSATION_TIME)
  10521. {
  10522. OcppStartTransaction(gun_index);
  10523. }
  10524. }
  10525. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= FORCE_BALANCE_TIME)
  10526. {
  10527. FouceAnnounceAccountBalance(gun_index);
  10528. }
  10529. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm)
  10530. {
  10531. // sync with dispenser
  10532. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.RemoteStartConfirm = false;
  10533. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  10534. LOG_INFO("Sync with Gun %d through remote start", gun_index + 1);
  10535. FouceAnnounceAccountBalance(gun_index);
  10536. }
  10537. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm)
  10538. {
  10539. // sync with dispenser
  10540. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.SwipeRfidConfirm = false;
  10541. GetClockTime(&_ConnectorAuthorizing_Time[gun_index]);
  10542. LOG_INFO("Sync with Connector %d through swipe rfid", gun_index + 1);
  10543. }
  10544. if(GetTimeoutValue(_ConnectorAuthorizing_Time[gun_index]) / uSEC_VAL >= ShmChargerInfo->CabinetMiscValue.ConnectionTimeout)
  10545. {
  10546. if(!chargingInfo[gun_index]->ConnectorPlugIn)
  10547. {
  10548. LOG_INFO("*********** Gun %d Plug In Timeout ***********", gun_index + 1);
  10549. }
  10550. else
  10551. {
  10552. LOG_INFO("*********** Gun %d Preparing State Timeout ***********", gun_index + 1);
  10553. }
  10554. OcppStopTransaction(gun_index);
  10555. setChargerMode(gun_index, MODE_IDLE);
  10556. }
  10557. else
  10558. {
  10559. if(chargingInfo[gun_index]->ConnectorPlugIn &&
  10560. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Preparing)
  10561. {
  10562. AddPlugInTimes(gun_index);
  10563. LOG_INFO("Gun %d CardNumber = [%s]", gun_index + 1, chargingInfo[gun_index]->StartUserId);
  10564. setChargerMode(gun_index, MODE_REASSIGN_CHECK);
  10565. GunAuthInitial(gun_index);
  10566. }
  10567. else if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo)
  10568. {
  10569. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.StartWaitPlug = false;
  10570. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.NeedCleanAuthorizeInfo = false;
  10571. LOG_INFO("*********** Gun %d User Stop Plug In ***********", gun_index + 1);
  10572. OcppStopTransaction(gun_index);
  10573. setChargerMode(gun_index, MODE_IDLE);
  10574. }
  10575. else if(isEvBoardStopChargeFlag(gun_index) == YES)
  10576. {
  10577. LOG_INFO("********** Gun %d Alarm Stop (S_AUTHORIZING) **********", gun_index + 1);
  10578. OcppStopTransaction(gun_index);
  10579. setChargerMode(gun_index, MODE_IDLE);
  10580. }
  10581. }
  10582. break;
  10583. case S_REASSIGN_CHECK:
  10584. {
  10585. if (isModeChange(gun_index))
  10586. {
  10587. LOG_INFO("================== S_REASSIGN_CHECK (%x) ================== \n", gun_index + 1);
  10588. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  10589. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  10590. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  10591. //StopSystemTimeoutDet();
  10592. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = false;
  10593. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy = ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy;
  10594. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration = ShmSysConfigAndInfo->SysConfig.MaxChargingDuration;
  10595. LOG_INFO("Max Total Current %d A, Max Total Power %d kW, Total Energy %d kWh, Total Duration %d",
  10596. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent,
  10597. ShmSysConfigAndInfo->SysConfig.MaxChargingPower,
  10598. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputEnergy,
  10599. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].MaxOutputDuration);
  10600. SetAcContactor(ON);
  10601. SetPCabinetOutputRelay(gun_index, ON);
  10602. GetClockTime(&_SystemStatus_Time[gun_index]);
  10603. }
  10604. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10605. if(ConnectorRequestToCharging(gun_index))
  10606. {
  10607. setChargerMode(gun_index, MODE_REASSIGN);
  10608. }
  10609. else if(time >= WAIT_REASSIGN_TIME)
  10610. {
  10611. if(ShmPsuData->Work_Step != _WORK_CHARGING)
  10612. {
  10613. LOG_INFO("Gun %d Wait PSU Work Step Timeout", gun_index + 1);
  10614. }
  10615. else
  10616. {
  10617. LOG_INFO("Gun %d Request Charging Timeout", gun_index + 1);
  10618. }
  10619. setChargerMode(gun_index, MODE_IDLE);
  10620. }
  10621. }
  10622. break;
  10623. case S_REASSIGN:
  10624. {
  10625. if (isModeChange(gun_index))
  10626. {
  10627. LOG_INFO("================== S_REASSIGN (%x) ================== \n", gun_index + 1);
  10628. //gettimeofday(&_toAverage_time, NULL);
  10629. GetClockTime(&_SystemStatus_Time[gun_index]);
  10630. }
  10631. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10632. if(IsConnectorGroupingCompleted(gun_index))
  10633. {
  10634. setChargerMode(gun_index, MODE_PRECHARGE);
  10635. }
  10636. else if(time >= MAX_REQUEST_CHARGING_TIME)
  10637. {
  10638. LOG_INFO("Gun %d Grouping Timeout", gun_index + 1);
  10639. setChargerMode(gun_index, MODE_IDLE);
  10640. }
  10641. }
  10642. break;
  10643. case S_PREPARNING:
  10644. {
  10645. if (isModeChange(gun_index))
  10646. {
  10647. LOG_INFO("================== S_PREPARNING (%x) ================== \n", gun_index + 1);
  10648. //StopGunInfoTimeoutDet(gun_index);
  10649. //StartGunInfoTimeoutDet(gun_index, Timeout_Preparing);
  10650. SetAcContactor(ON);
  10651. GetClockTime(&_SystemStatus_Time[gun_index]);
  10652. }
  10653. UpdateGunPresentChargingPower(gun_index);
  10654. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10655. if ((chargingInfo[gun_index]->AvailableChargingPower > 10) && time >= MIN_WAIT_PREPARNING_TIME)
  10656. {
  10657. setChargerMode(gun_index, MODE_PREPARE_FOR_EV);
  10658. break;;
  10659. }
  10660. if (isEvBoardStopChargeFlag(gun_index) == YES)
  10661. {
  10662. // 板端或後臺要求停止
  10663. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARNING) **********", gun_index + 1);
  10664. Set_Ocpp_StopReason(gun_index, "Local");
  10665. ChargingAlarmProcess(gun_index);
  10666. }
  10667. else if(isNormalStopChargeFlag(gun_index) == YES)
  10668. {
  10669. LOG_INFO("********** Gun %d Normal Stop (S_PREPARNING) %s**********", gun_index + 1,
  10670. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10671. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10672. ChargingTerminalProcess(gun_index);
  10673. }
  10674. else if(isResetStopChargeFlag(gun_index) == YES)
  10675. {
  10676. LOG_INFO("********** Gun %d Reset Stop (S_PREPARNING) %s**********", gun_index + 1,
  10677. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10678. char *reason[16];
  10679. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10680. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10681. ChargingTerminalProcess(gun_index);
  10682. }
  10683. else if(isBackendStopFlag(gun_index))
  10684. {
  10685. LOG_INFO("********** Gun %d Backend Stop (S_PREPARNING) **********", gun_index + 1);
  10686. Set_Ocpp_StopReason(gun_index, "Other");
  10687. ChargingTerminalProcess(gun_index);
  10688. }
  10689. if(time > GUN_PREPARE_TIMEOUT)
  10690. {
  10691. LOG_INFO("********** Gun %d PrepareTimeout **********", gun_index + 1);
  10692. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuNoResource = YES;
  10693. if(strncmp((char *)chargingInfo[gun_index]->ConnectorAlarmCode, "", 6) == EQUAL)
  10694. {
  10695. memcpy(chargingInfo[gun_index]->ConnectorAlarmCode, "012279", 6);
  10696. LOG_INFO("Gun %d No Psu Resource", gun_index + 1);
  10697. }
  10698. Set_Ocpp_StopReason(gun_index, "Local");
  10699. ChargingAlarmProcess(gun_index);
  10700. }
  10701. }
  10702. break;
  10703. case S_PREPARING_FOR_EV: // 等待車端的通訊 (EV 小板),待車端回報後,開始樁端的測試
  10704. {
  10705. if (isModeChange(gun_index))
  10706. {
  10707. LOG_INFO("================== S_PREPARING_FOR_EV (%x) ================== \n", gun_index + 1);
  10708. //strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  10709. //StopGunInfoTimeoutDet(gun_index);
  10710. //StartGunInfoTimeoutDet(gun_index, Timeout_EvChargingDet);
  10711. GetClockTime(&_SystemStatus_Time[gun_index]);
  10712. }
  10713. UpdateGunPresentChargingPower(gun_index);
  10714. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10715. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest)
  10716. {
  10717. setChargerMode(gun_index, MODE_PREPARE_FOR_EVSE);
  10718. }
  10719. // only for test & debug purpose
  10720. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10721. {
  10722. LOG_INFO("Gun %d [Force Charging] S_PREPARING_FOR_EV OK", gun_index + 1);
  10723. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PermissionRequest = true;
  10724. }
  10725. if (isEvBoardStopChargeFlag(gun_index) == YES)
  10726. {
  10727. // 板端或後臺要求停止
  10728. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  10729. Set_Ocpp_StopReason(gun_index, "Local");
  10730. ChargingAlarmProcess(gun_index);
  10731. }
  10732. else if(isNormalStopChargeFlag(gun_index) == YES)
  10733. {
  10734. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  10735. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10736. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10737. ChargingTerminalProcess(gun_index);
  10738. }
  10739. else if(isResetStopChargeFlag(gun_index) == YES)
  10740. {
  10741. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EV) %s**********", gun_index + 1,
  10742. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10743. char *reason[16];
  10744. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10745. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10746. ChargingTerminalProcess(gun_index);
  10747. }
  10748. else if(isBackendStopFlag(gun_index))
  10749. {
  10750. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EV) **********", gun_index + 1);
  10751. Set_Ocpp_StopReason(gun_index, "Other");
  10752. ChargingTerminalProcess(gun_index);
  10753. }
  10754. if(time >= GUN_EV_WAIT_TIMEOUT)
  10755. {
  10756. LOG_INFO("********** Gun %d Wait Dispenser Timeout **********", gun_index + 1);
  10757. ChargingTerminalProcess(gun_index);
  10758. }
  10759. }
  10760. break;
  10761. case S_PREPARING_FOR_EVSE: // 等待 RB 通訊及測試,並將狀態回報, CSU 確認 Pass 後,開始進入充電
  10762. {
  10763. if (isModeChange(gun_index))
  10764. {
  10765. LOG_INFO("================== S_PREPARING_FOR_EVSE (%x) ================== \n", gun_index + 1);
  10766. GetClockTime(&_SystemStatus_Time[gun_index]);
  10767. #ifdef STARTTRANSATION_IMMEDIATELY
  10768. OcppStartTransaction(gun_index);
  10769. #endif
  10770. }
  10771. CheckoutStartChargingSoc(gun_index, NO);
  10772. UpdateGunPresentChargingPower(gun_index);
  10773. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10774. // only for test & debug purpose
  10775. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10776. {
  10777. if(time >= FORCE_CHARGING_CMD_DELAY)
  10778. {
  10779. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  10780. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  10781. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  10782. {
  10783. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  10784. }
  10785. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  10786. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  10787. }
  10788. }
  10789. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteStatus == _CRS_Charging)
  10790. {
  10791. setChargerMode(gun_index, MODE_CHARGING);
  10792. }
  10793. // only for test & debug purpose
  10794. else if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10795. {
  10796. if(time >= FORCE_CHARGING_WAIT_EVSE)
  10797. {
  10798. LOG_INFO("Gun %d [Charging Simulation] S_PREPARING_FOR_EVSE OK", gun_index + 1);
  10799. setChargerMode(gun_index, MODE_CHARGING);
  10800. }
  10801. }
  10802. if (isEvBoardStopChargeFlag(gun_index) == YES)
  10803. {
  10804. // 板端或後臺要求停止
  10805. LOG_INFO("********** Gun %d Alarm Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  10806. Set_Ocpp_StopReason(gun_index, "Local");
  10807. ChargingAlarmProcess(gun_index);
  10808. }
  10809. else if(isNormalStopChargeFlag(gun_index) == YES)
  10810. {
  10811. LOG_INFO("********** Gun %d Normal Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  10812. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10813. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10814. ChargingTerminalProcess(gun_index);
  10815. }
  10816. else if(isResetStopChargeFlag(gun_index) == YES)
  10817. {
  10818. LOG_INFO("********** Gun %d Reset Stop (S_PREPARING_FOR_EVSE) %s**********", gun_index + 1,
  10819. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10820. char *reason[16];
  10821. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10822. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10823. ChargingTerminalProcess(gun_index);
  10824. }
  10825. else if(isInvalidIdStopFlag(gun_index) == YES)
  10826. {
  10827. LOG_INFO("********** Gun %d InvalidId Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  10828. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  10829. ChargingTerminalProcess(gun_index);
  10830. }
  10831. else if(isBackendStopFlag(gun_index))
  10832. {
  10833. LOG_INFO("********** Gun %d Backend Stop (S_PREPARING_FOR_EVSE) **********", gun_index + 1);
  10834. Set_Ocpp_StopReason(gun_index, "Other");
  10835. ChargingTerminalProcess(gun_index);
  10836. }
  10837. if(time >= GUN_EV_WAIT_TIMEOUT)
  10838. {
  10839. LOG_INFO("********** Gun %d GFD timeout **********", gun_index + 1);
  10840. ChargingTerminalProcess(gun_index);
  10841. }
  10842. }
  10843. break;
  10844. case S_CHARGING: // 剛進入充電狀態,等待 EV 小板要求的輸出電流後開始輸出
  10845. {
  10846. if (isModeChange(gun_index))
  10847. {
  10848. LOG_INFO("================== S_CHARGING (%x) ================== \n", gun_index + 1);
  10849. //StopGunInfoTimeoutDet(gun_index);
  10850. GetClockTime(&_SystemStatus_Time[gun_index]);
  10851. GetClockTime(&_ChargedEnergy_Time[gun_index]);
  10852. CheckoutStartChargingSoc(gun_index, YES);
  10853. chargingInfo[gun_index]->PresentChargedDuration = 0;
  10854. ChangeStartOrStopDateTime(YES, gun_index);
  10855. #ifndef STARTTRANSATION_IMMEDIATELY
  10856. OcppStartTransaction(gun_index);
  10857. #endif
  10858. }
  10859. // only for test & debug purpose
  10860. if(ShmChargerInfo->Control.FCharging[gun_index].FCtrl.bits.EnableForceCharging)
  10861. {
  10862. float voltage = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetVoltage / 10;
  10863. float current = (float)ShmChargerInfo->Control.FCharging[gun_index].FTargetCurrent / 10;
  10864. if(chargingInfo[gun_index]->EvBatterytargetVoltage != voltage || chargingInfo[gun_index]->EvBatterytargetCurrent != current)
  10865. {
  10866. LOG_INFO("Gun %d Set Force Charging Voltage %4d V, Current: %4d A", gun_index + 1, (int)voltage, (int)current);
  10867. }
  10868. chargingInfo[gun_index]->EvBatterytargetVoltage = voltage;
  10869. chargingInfo[gun_index]->EvBatterytargetCurrent = current;
  10870. }
  10871. // 計算 Power
  10872. //chargingInfo[gun_index]->PresentChargingPower = ((float)((chargingInfo[gun_index]->PresentChargingVoltage) * (chargingInfo[gun_index]->PresentChargingCurrent)) / 1000);
  10873. //chargingInfo[gun_index]->PresentChargingPower = (((float)((ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingVoltage) * (ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].RemoteChargingCurrent))) / 1000 / 100);
  10874. UpdateGunPresentChargingPower(gun_index);
  10875. ChargedEnergySummation(gun_index);
  10876. CheckSessionTargetStop(gun_index);
  10877. if (isEvBoardStopChargeFlag(gun_index))
  10878. {
  10879. LOG_INFO("********** Gun %d Alarm Stop (S_CHARGING) **********", gun_index + 1);
  10880. Set_Ocpp_StopReason(gun_index, "Local");
  10881. ChargingAlarmProcess(gun_index);
  10882. }
  10883. else if(isNormalStopChargeFlag(gun_index) == YES)
  10884. {
  10885. LOG_INFO("********** Gun %d Normal Stop (S_CHARGING) %s**********", gun_index + 1,
  10886. chargingInfo[gun_index]->ChargingStopFlag.bits.ManualStop ? "(ManualStop)" : "");
  10887. Set_Ocpp_StopReason(gun_index, "EVDisconnected");
  10888. ChargingTerminalProcess(gun_index);
  10889. }
  10890. else if(isResetStopChargeFlag(gun_index) == YES)
  10891. {
  10892. LOG_INFO("********** Gun %d Reset Stop (S_CHARGING) %s**********", gun_index + 1,
  10893. chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "(Hard)" : "(Soft)");
  10894. char *reason[16];
  10895. strcpy((char *)&reason[0], chargingInfo[gun_index]->ChargingStopFlag.bits.HardResetStop ? "HardReset" : "SoftReset");
  10896. Set_Ocpp_StopReason(gun_index, (char *)&reason[0]);
  10897. ChargingTerminalProcess(gun_index);
  10898. }
  10899. else if(isInvalidIdStopFlag(gun_index) == YES)
  10900. {
  10901. LOG_INFO("********** Gun %d InvalidId Stop (S_CHARGING) **********", gun_index + 1);
  10902. Set_Ocpp_StopReason(gun_index, "DeAuthorized");
  10903. ChargingTerminalProcess(gun_index);
  10904. }
  10905. else if(CheckBackendChargingTimeout(gun_index) ||
  10906. CheckBackendChargingEnergy(gun_index) ||
  10907. IsReachConfigMaxChargingSoc(gun_index))
  10908. {
  10909. LOG_INFO("********** Gun %d Backend Condition Stop (S_CHARGING) **********", gun_index + 1);
  10910. ChargingTerminalProcess(gun_index);
  10911. }
  10912. else if(isSessionTargetStopFlag(gun_index) == YES)
  10913. {
  10914. LOG_INFO("********** Gun %d SessionTarget Stop (S_CHARGING) **********", gun_index + 1);
  10915. Set_Ocpp_StopReason(gun_index, "Other");
  10916. ChargingTerminalProcess(gun_index);
  10917. }
  10918. else if(isBackendStopFlag(gun_index))
  10919. {
  10920. LOG_INFO("********** Gun %d Backend Stop (S_CHARGING) **********", gun_index + 1);
  10921. Set_Ocpp_StopReason(gun_index, "Other");
  10922. ChargingTerminalProcess(gun_index);
  10923. }
  10924. }
  10925. break;
  10926. case S_ALARM:
  10927. case S_TERMINATING:
  10928. {
  10929. if (isModeChange(gun_index))
  10930. {
  10931. Set_Ocpp_StopReason(gun_index, "Other");
  10932. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  10933. {
  10934. LOG_INFO("================== S_ALARM (%x) ================== \n", gun_index + 1);
  10935. UpdateErrorCodeToOcpp(gun_index);
  10936. TheEndCharging(gun_index);
  10937. }
  10938. else
  10939. {
  10940. LOG_INFO("================== S_TERMINATING (%x) ================== \n", gun_index + 1);
  10941. }
  10942. //StopGunInfoTimeoutDet(gun_index);
  10943. GetClockTime(&_SystemStatus_Time[gun_index]);
  10944. }
  10945. UpdateGunPresentChargingPower(gun_index);
  10946. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10947. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  10948. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  10949. {
  10950. OcppStopTransaction(gun_index);
  10951. }
  10952. else
  10953. {
  10954. ChargedEnergySummation(gun_index);
  10955. }
  10956. if (chargingInfo[gun_index]->SystemStatus == S_ALARM)
  10957. {
  10958. if(time >= GUN_COMP_WAIT_TIMEOUT)
  10959. {
  10960. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  10961. {
  10962. LOG_INFO ("********** Gun %d psu is releasable **********\n", gun_index + 1);
  10963. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  10964. }
  10965. }
  10966. if(IsAvailableBackToIdle(gun_index) == YES &&
  10967. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  10968. {
  10969. ReleaseAlarmCode(gun_index);
  10970. setChargerMode(gun_index, MODE_IDLE);
  10971. }
  10972. }
  10973. else
  10974. {
  10975. setChargerMode(gun_index, MODE_COMPLETE);
  10976. }
  10977. }
  10978. break;
  10979. case S_COMPLETE:
  10980. {
  10981. if (isModeChange(gun_index))
  10982. {
  10983. LOG_INFO("================== S_COMPLETE (%x) ================== \n", gun_index + 1);
  10984. TheEndCharging(gun_index);
  10985. GetClockTime(&_SystemStatus_Time[gun_index]);
  10986. }
  10987. UpdateGunPresentChargingPower(gun_index);
  10988. time = GetTimeoutValue(_SystemStatus_Time[gun_index]) / uSEC_VAL;
  10989. if((Is_DispenserStop(gun_index) && time >= STOP_TARTTRANSATION_DELAY) ||
  10990. time >= WAIT_DISPENSER_STOP_TIMEOUT)
  10991. {
  10992. OcppStopTransaction(gun_index);
  10993. }
  10994. else
  10995. {
  10996. ChargedEnergySummation(gun_index);
  10997. }
  10998. if(time >= GUN_COMP_WAIT_TIMEOUT)
  10999. {
  11000. if(!ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11001. {
  11002. LOG_INFO ("********** Gun %d psu is releasable **********", gun_index + 1);
  11003. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable = true;
  11004. }
  11005. }
  11006. if(IsAvailableBackToIdle(gun_index) == YES &&
  11007. ShmSysConfigAndInfo->SysInfo.ConnectorInfo[gun_index].Parameter.bits.PsuReleasable)
  11008. {
  11009. setChargerMode(gun_index, MODE_IDLE);
  11010. }
  11011. }
  11012. break;
  11013. case S_CCS_PRECHARGE_ST0:
  11014. if (isModeChange(gun_index))
  11015. {
  11016. LOG_INFO("CCS Precharge Processing 1....................%x \n", gun_index);
  11017. //StopGunInfoTimeoutDet(gun_index);
  11018. //StartGunInfoTimeoutDet(gun_index, Timeout_ForCcsPrechargeDet);
  11019. }
  11020. break;
  11021. case S_CCS_PRECHARGE_ST1:
  11022. if (isModeChange(gun_index))
  11023. {
  11024. LOG_INFO("CCS Precharge Processing 2....................%x \n", gun_index);
  11025. }
  11026. break;
  11027. case S_DEBUG:
  11028. if (isModeChange(gun_index))
  11029. {
  11030. LOG_INFO("================== S_DEBUG (%x) ================== \n", gun_index + 1);
  11031. }
  11032. if(!ShmChargerInfo->Control.RelayCtrl.bits.AbnormalRelay &&
  11033. !ShmChargerInfo->Control.FanCtrl.bits.AbnormalFan)
  11034. {
  11035. setChargerMode(gun_index, MODE_IDLE);
  11036. }
  11037. break;
  11038. }
  11039. }
  11040. TryFeedWatchdog();
  11041. usleep(whileLoopTime);
  11042. }
  11043. return FAIL;
  11044. }