main.c 264 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354
  1. #include "define.h"
  2. #include "main.h"
  3. //#define CDFA_CERTIFICATE // Only enable for CDFA certificate to send receipt by Phihong back end API
  4. //==========================
  5. // System basic sample constant
  6. //==========================
  7. #define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)
  8. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  9. #define PASS 1
  10. #define FAIL -1
  11. #define YES 1
  12. #define NO 0
  13. #define ON 1
  14. #define OFF 0
  15. #define BUFFER_SIZE 128
  16. //==========================
  17. // Timeout constant define
  18. //==========================
  19. #define TIMEOUT_SPEC_HANDSHAKING 180
  20. #define TIMEOUT_SPEC_AUTH 15
  21. #define TIMEOUT_SPEC_HANDSHAKING_LED 185
  22. #define TIMEOUT_SPEC_LOGPPRINTOUT 30
  23. #define TIMEOUT_SPEC_PROFILE_PREPARE 60
  24. #define TIMEOUT_SPEC_BS_HLC_HANDSHAKE 25
  25. #define TIMEOUT_SPEC_EV_READY 30
  26. #define TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET 10
  27. #define TIMEOUT_SPEC_CCS_HANDSHAKE 120
  28. #define TIMEOUT_SPEC_PWN_CHANGE 5
  29. #define TIMEOUT_SPEC_POWERSAVING_LCD 120
  30. #define TIMEOUT_SPEC_POWERSAVING_RFID 120
  31. #define TIMEOUT_SPEC_POWERSAVING_METER 120
  32. #define TIMEOUT_SPEC_POWERSAVING_LED_STATUS 120
  33. #define TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION 15
  34. #define TIMEOUT_SPEC_RESET_WIFI_MODULE 300
  35. #define TIMEOUT_SPEC_TASK_CHECK 10
  36. #define TIMEOUT_SPEC_SIMULATION 10
  37. //==========================
  38. // GPIO constant define
  39. //==========================
  40. #define GPIO_OUT_RST_RFID 62
  41. #define GPIO_AC_OUT_RST_4G 114
  42. #define GPIO_OUT_RST_QCA 115
  43. #define GPIO_OUT_RST_ETH 56
  44. #define GPIO_IN_WAKEUP 63
  45. #define GPIO_AC_OUT_RST_4G_WIFI 59
  46. #define GPIO_DC_OUT_RST_4G_WIFI 104 //lwtest
  47. #define MtdBlockSize 0x300000
  48. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  49. //==========================
  50. // Declare method
  51. //==========================
  52. void trim(char *s);
  53. int mystrcmp(char *p1, char *p2);
  54. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  55. void split(char **arr, char *str, const char *del);
  56. int isReachableInternet();
  57. int isRouteFail();
  58. int InitRfidPort(void);
  59. int GetCardSerialNumber();
  60. void setLedMotion(unsigned char gun_index, unsigned char led_mode);
  61. void setRequest(unsigned char gun_index, unsigned char isOn);
  62. void setSpeaker(unsigned char isOn, unsigned char speaker_mode);
  63. void gpio_set_value(unsigned int gpio, unsigned int value);
  64. //==========================
  65. // Declare RFID module type
  66. //==========================
  67. #define MODULE_EWT 0
  68. int wtdFd = -1;
  69. int rfidFd = -1;
  70. char* rfidPortName = "/dev/ttyS2";
  71. RFID rfid;
  72. char *valid_Internet[2] = { "8.8.8.8", "180.76.76.76" };
  73. //==========================
  74. // Declare share memory
  75. //==========================
  76. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  77. struct StatusCodeData *ShmStatusCodeData;
  78. struct PsuData *ShmPsuData;
  79. struct CHAdeMOData *ShmCHAdeMOData;
  80. struct CcsData *ShmCcsData;
  81. struct PrimaryMcuData *ShmPrimaryMcuData;
  82. struct FanModuleData *ShmFanModuleData;
  83. struct RelayModuleData *ShmRelayModuleData;
  84. struct OCPP16Data *ShmOCPP16Data;
  85. struct OCPP20Data *ShmOCPP20Data;
  86. struct OCPP16Data *ShmOCPP16DataPH;
  87. struct Charger *ShmCharger;
  88. struct timespec startTime[3][TMR_IDX_CNT];
  89. struct timespec startChargingTime[3];
  90. struct timespec endChargingTime[3];
  91. sqlite3 *localDb;
  92. struct SysConfigData SysConfigOrg;
  93. ParsingRatedCur modelnameInfo = { 0 };
  94. struct PreviousData
  95. {
  96. uint16_t targetCurrent;
  97. uint16_t current_limit;
  98. uint16_t primaryMcuCp_Pwn_Duty;
  99. float ChargingProfilePower;
  100. float ChargingProfileCurrent;
  101. } previousData[4];
  102. //=================================
  103. // Common routine
  104. //=================================
  105. long long current_timestamp()
  106. {
  107. struct timeval te;
  108. gettimeofday(&te, NULL); // get current time
  109. long long milliseconds = te.tv_sec * 1000LL + te.tv_usec / 1000; // calculate milliseconds
  110. return milliseconds;
  111. }
  112. void trim(char *s)
  113. {
  114. int i = 0, j, k, l = 0;
  115. while ((s[i] == ' ') || (s[i] == '\t') || (s[i] == '\n'))
  116. i++;
  117. j = strlen(s) - 1;
  118. while ((s[j] == ' ') || (s[j] == '\t') || (s[j] == '\n'))
  119. j--;
  120. if (i == 0 && j == strlen(s) - 1)
  121. {
  122. }
  123. else if (i == 0)
  124. s[j + 1] = '\0';
  125. else
  126. {
  127. for (k = i; k <= j; k++)
  128. s[l++] = s[k];
  129. s[l] = '\0';
  130. }
  131. }
  132. int mystrcmp(char *p1, char *p2)
  133. {
  134. while (*p1 == *p2)
  135. {
  136. if (*p1 == '\0' || *p2 == '\0')
  137. break;
  138. p1++;
  139. p2++;
  140. }
  141. if (*p1 == '\0' && *p2 == '\0')
  142. return (PASS);
  143. else
  144. return (FAIL);
  145. }
  146. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  147. {
  148. strncpy(dest, src + start, cnt);
  149. dest[cnt] = 0;
  150. }
  151. void split(char **arr, char *str, const char *del)
  152. {
  153. char *s = strtok(str, del);
  154. while (s != NULL)
  155. {
  156. *arr++ = s;
  157. s = strtok(NULL, del);
  158. }
  159. }
  160. int StoreLogMsg(const char *fmt, ...)
  161. {
  162. char Buf[4096 + 256];
  163. char buffer[4096];
  164. time_t CurrentTime;
  165. struct tm *tm;
  166. struct timeval tv;
  167. va_list args;
  168. va_start(args, fmt);
  169. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  170. va_end(args);
  171. memset(Buf, 0, sizeof(Buf));
  172. CurrentTime = time(NULL);
  173. tm = localtime(&CurrentTime);
  174. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  175. if ((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL)
  176. && (strlen((char*) ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  177. {
  178. sprintf(Buf, "echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]%s_%s_SystemLog",
  179. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec,
  180. buffer, tm->tm_year + 1900, tm->tm_mon + 1, ShmSysConfigAndInfo->SysConfig.ModelName,
  181. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  182. }
  183. else
  184. {
  185. sprintf(Buf, "echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  186. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec,
  187. buffer, tm->tm_year + 1900, tm->tm_mon + 1);
  188. }
  189. #ifdef SystemLogMessage
  190. system(Buf);
  191. #endif
  192. #ifdef ConsloePrintLog
  193. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  194. #endif
  195. return rc;
  196. }
  197. void refreshStartTimer(struct timespec *timer)
  198. {
  199. clock_gettime(CLOCK_MONOTONIC, timer);
  200. }
  201. int getDiffSecNow(struct timespec timer)
  202. {
  203. struct timespec timerNow;
  204. clock_gettime(CLOCK_MONOTONIC, &timerNow);
  205. return (int) ((((unsigned long) (timerNow.tv_sec - timer.tv_sec) * 1000)
  206. + ((unsigned long) ((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000)))) / 1000);
  207. }
  208. int getDiffSecBetween(struct timespec start, struct timespec end)
  209. {
  210. return (int) ((((unsigned long) (end.tv_sec - start.tv_sec) * 1000)
  211. + ((unsigned long) ((end.tv_nsec / 1000000) - (start.tv_nsec / 1000000)))) / 1000);
  212. }
  213. long long DiffTimebWithNow(struct timeb ST)
  214. {
  215. //return milli-second
  216. struct timeb ET;
  217. long long StartTime, StopTime;
  218. ftime(&ET);
  219. StartTime = (long long) ST.time;
  220. StopTime = (long long) ET.time;
  221. return ((StopTime - StartTime) * 1000) + (ET.millitm - ST.millitm);
  222. }
  223. long long DiffTimeb(struct timeb ST, struct timeb ET)
  224. {
  225. //return milli-second
  226. long long StartTime, StopTime;
  227. StartTime = (long long) ST.time;
  228. StopTime = (long long) ET.time;
  229. return ((StopTime - StartTime) * 1000) + (ET.millitm - ST.millitm);
  230. }
  231. int DiffTimebWithNowSec(struct timeb ST)
  232. {
  233. //return milli-second
  234. struct timeb ET;
  235. unsigned int StartTime, StopTime;
  236. ftime(&ET);
  237. StartTime = (unsigned int) ST.time;
  238. StopTime = (unsigned int) ET.time;
  239. return (StopTime - StartTime);
  240. }
  241. int isOvertNow(uint8_t *start)
  242. {
  243. int result = YES;
  244. struct ParsingResult
  245. {
  246. int result;
  247. int scanedElement;
  248. int year;
  249. int month;
  250. int mday;
  251. int hour;
  252. int min;
  253. int sec;
  254. int tz_hour;
  255. int tz_min;
  256. float minSec;
  257. } parsingResult;
  258. struct tm tmStart;
  259. struct timeb tbStart;
  260. memset(&parsingResult, 0x00, sizeof(struct ParsingResult));
  261. if (strstr((char*) start, ".") != NULL)
  262. {
  263. // Original data with mini second
  264. if (strstr((char*) start, "Z") != NULL)
  265. {
  266. // Original data with Z
  267. parsingResult.scanedElement = sscanf((char*) start, "%d-%d-%dT%d:%d:%d.%fZ", &parsingResult.year,
  268. &parsingResult.month, &parsingResult.mday, &parsingResult.hour, &parsingResult.min,
  269. &parsingResult.sec, &parsingResult.minSec);
  270. }
  271. else
  272. {
  273. // Original data without Z
  274. parsingResult.scanedElement = sscanf((char*) start, "%d-%d-%dT%d:%d:%d.%f%d:%d", &parsingResult.year,
  275. &parsingResult.month, &parsingResult.mday, &parsingResult.hour, &parsingResult.min,
  276. &parsingResult.sec, &parsingResult.minSec, &parsingResult.tz_hour, &parsingResult.tz_min);
  277. }
  278. }
  279. else
  280. {
  281. // Original data without mini second
  282. if (strstr((char*) start, "Z") != NULL)
  283. {
  284. // Original data with Z
  285. parsingResult.scanedElement = sscanf((char*) start, "%d-%d-%dT%d:%d:%dZ", &parsingResult.year,
  286. &parsingResult.month, &parsingResult.mday, &parsingResult.hour, &parsingResult.min,
  287. &parsingResult.sec);
  288. }
  289. else
  290. {
  291. // Original data without Z
  292. parsingResult.scanedElement = sscanf((char*) start, "%d-%d-%dT%d:%d:%d%d:%d", &parsingResult.year,
  293. &parsingResult.month, &parsingResult.mday, &parsingResult.hour, &parsingResult.min,
  294. &parsingResult.sec, &parsingResult.tz_hour, &parsingResult.tz_min);
  295. }
  296. }
  297. if (parsingResult.scanedElement >= 6)
  298. {
  299. tmStart.tm_year = parsingResult.year - 1900;
  300. tmStart.tm_mon = parsingResult.month - 1;
  301. tmStart.tm_mday = parsingResult.mday;
  302. tmStart.tm_hour = parsingResult.hour;
  303. tmStart.tm_min = parsingResult.min;
  304. tmStart.tm_sec = parsingResult.sec;
  305. tmStart.tm_gmtoff = 0;
  306. tbStart.time = mktime(&tmStart);
  307. tbStart.timezone = 0;
  308. tbStart.millitm = 0;
  309. tbStart.time -= (parsingResult.tz_hour * 3600)
  310. + (parsingResult.tz_min * 60 * (parsingResult.tz_hour >= 0 ? 1 : -1));
  311. if (DiffTimebWithNowSec(tbStart) <= 0)
  312. result = NO;
  313. else
  314. result = YES;
  315. }
  316. else
  317. {
  318. DEBUG_WARN("Start date parsing error.\n");
  319. }
  320. return result;
  321. }
  322. void getDateTimeString(char* result)
  323. {
  324. time_t CurrentTime;
  325. struct tm *tm;
  326. CurrentTime = time(NULL);
  327. tm = localtime(&CurrentTime);
  328. sprintf(result, "%04d.%02d.%02d %02d:%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
  329. tm->tm_min, tm->tm_sec);
  330. }
  331. int getCurrentYear()
  332. {
  333. int result = 0;
  334. time_t CurrentTime;
  335. struct tm *tm;
  336. CurrentTime = time(NULL);
  337. tm = localtime(&CurrentTime);
  338. result = (tm->tm_year + 1900);
  339. return result;
  340. }
  341. unsigned long long getAvailableMemory()
  342. {
  343. long pages = sysconf(_SC_AVPHYS_PAGES);
  344. long page_size = sysconf(_SC_PAGE_SIZE);
  345. return pages * page_size;
  346. }
  347. unsigned int isKernelSupportNAT()
  348. {
  349. unsigned int result = NO;
  350. unsigned int version = 0;
  351. FILE *fp;
  352. char cmd[256];
  353. char buf[512];
  354. // Get IP address & net mask
  355. strcpy(cmd, "uname -v");
  356. fp = popen(cmd, "r");
  357. if (fp != NULL)
  358. {
  359. if (fgets(buf, sizeof(buf), fp) != NULL)
  360. {
  361. sscanf(buf, "#%d", &version);
  362. if (version >= 30)
  363. result = YES;
  364. }
  365. }
  366. pclose(fp);
  367. return result;
  368. }
  369. int getEth0MacAddress()
  370. {
  371. int result = PASS;
  372. FILE *fp;
  373. char cmd[256];
  374. char buf[512];
  375. char tmp[512];
  376. strcpy(cmd, "ifconfig eth0");
  377. fp = popen(cmd, "r");
  378. if (fp != NULL)
  379. {
  380. while (fgets(buf, sizeof(buf), fp) != NULL)
  381. {
  382. if (strstr(buf, "eth0") > 0)
  383. {
  384. result = PASS;
  385. }
  386. if (strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
  387. {
  388. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  389. strcpy((char*) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
  390. }
  391. }
  392. }
  393. pclose(fp);
  394. return result;
  395. }
  396. int isUap0up(void)
  397. {
  398. int result = FAIL;
  399. FILE *fp;
  400. char cmd[256];
  401. char buf[512];
  402. strcpy(cmd, "ifconfig uap0");
  403. ;
  404. fp = popen(cmd, "r");
  405. if (fp != NULL)
  406. {
  407. while (fgets(buf, sizeof(buf), fp) != NULL)
  408. {
  409. if (strstr(buf, "uap0") > 0)
  410. {
  411. result = PASS;
  412. }
  413. }
  414. }
  415. pclose(fp);
  416. return result;
  417. }
  418. //======================================================
  419. // Get tartget charging info data
  420. //======================================================
  421. struct ChargingInfoData* getTargetChargingInfoData(uint8_t gun_index)
  422. {
  423. struct ChargingInfoData *targetChargingInfoData = NULL;
  424. // Get target gun data
  425. if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_Chademo)
  426. {
  427. for (int index = 0; index < CHAdeMO_QUANTITY; index++)
  428. {
  429. if ((ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == gun_index))
  430. {
  431. targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  432. break;
  433. }
  434. }
  435. }
  436. else if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_CCS_2)
  437. {
  438. for (int index = 0; index < CCS_QUANTITY; index++)
  439. {
  440. if ((ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == gun_index))
  441. {
  442. targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  443. break;
  444. }
  445. }
  446. }
  447. else if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_GB)
  448. {
  449. for (int index = 0; index < GB_QUANTITY; index++)
  450. {
  451. if ((ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == gun_index))
  452. {
  453. targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  454. break;
  455. }
  456. }
  457. }
  458. else if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_AC)
  459. {
  460. for (int index = 0; index < AC_QUANTITY; index++)
  461. {
  462. if ((ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index == gun_index))
  463. {
  464. targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.AcChargingData[index];
  465. break;
  466. }
  467. }
  468. }
  469. if (targetChargingInfoData == NULL)
  470. targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.AcChargingData[0];
  471. return targetChargingInfoData;
  472. }
  473. //======================================================
  474. // OCPP routine
  475. //======================================================
  476. void ocpp_process_start()
  477. {
  478. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  479. system("/root/OcppBackend &");
  480. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  481. system("/root/OcppBackend20 &");
  482. }
  483. uint8_t ocpp_get_isRemoteStartWait()
  484. {
  485. uint8_t result = OFF;
  486. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  487. {
  488. result = ShmOCPP16Data->MsMsg.bits.isRemoteStartWaitReq;
  489. }
  490. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  491. {
  492. result = ShmOCPP20Data->MsMsg.bits.isRemoteStartWaitReq;
  493. }
  494. return result;
  495. }
  496. uint8_t ocpp_get_connection_status()
  497. {
  498. uint8_t result = OFF;
  499. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  500. {
  501. result = ShmOCPP16Data->OcppConnStatus;
  502. }
  503. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  504. {
  505. result = ShmOCPP20Data->OcppConnStatus;
  506. }
  507. return result;
  508. }
  509. uint16_t ocpp_get_connection_timeout()
  510. {
  511. uint16_t result = TIMEOUT_SPEC_HANDSHAKING;
  512. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  513. {
  514. if (strcmp((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData, "") != 0)
  515. {
  516. result = atoi((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  517. }
  518. }
  519. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  520. {
  521. if (strcmp( (char *) ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value,"") != 0)
  522. {
  523. result = atoi( (char *) ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_EVConnectionTimeOut].variableAttribute[0].value);
  524. }
  525. }
  526. return result;
  527. }
  528. uint8_t ocpp_get_update_firmware_req()
  529. {
  530. uint8_t result = NO;
  531. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  532. {
  533. result = ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq;
  534. }
  535. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  536. {
  537. result = ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq;
  538. }
  539. return result;
  540. }
  541. uint8_t ocpp_get_reset_req()
  542. {
  543. uint8_t result = NO;
  544. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  545. {
  546. result = ShmOCPP16Data->MsMsg.bits.ResetReq;
  547. }
  548. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  549. {
  550. result = ShmOCPP20Data->MsMsg.bits.ResetReq;
  551. }
  552. return result;
  553. }
  554. void ocpp_boot_info_sync()
  555. {
  556. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  557. {
  558. memcpy((char*) ShmOCPP16Data->OcppServerURL, (char*) ShmSysConfigAndInfo->SysConfig.OcppServerURL,
  559. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  560. memcpy((char*) ShmOCPP16Data->ChargeBoxId, (char*) ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  561. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  562. sprintf((char*) ShmOCPP16Data->BootNotification.CpFwVersion,
  563. (char*) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  564. sprintf((char*) ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
  565. switch (ShmSysConfigAndInfo->SysConfig.ModelName[SAFETY_REGULATION])
  566. {
  567. case 'M':
  568. case 'Z':
  569. sprintf((char*) ShmOCPP16Data->BootNotification.CpMeterType, "MID");
  570. break;
  571. case 'P':
  572. sprintf((char*) ShmOCPP16Data->BootNotification.CpMeterType, "PTB");
  573. break;
  574. case 'I':
  575. sprintf((char*) ShmOCPP16Data->BootNotification.CpMeterType, "TIC");
  576. break;
  577. case 'U':
  578. sprintf((char*) ShmOCPP16Data->BootNotification.CpMeterType, "UL");
  579. break;
  580. default:
  581. sprintf((char*) ShmOCPP16Data->BootNotification.CpMeterType, "N/A");
  582. break;
  583. }
  584. }
  585. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  586. {
  587. memcpy((char*) ShmOCPP20Data->OcppServerURL, (char*) ShmSysConfigAndInfo->SysConfig.OcppServerURL,
  588. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  589. memcpy((char*) ShmOCPP20Data->ChargeBoxId, (char*) ShmSysConfigAndInfo->SysConfig.ChargeBoxId,
  590. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  591. sprintf((char*) ShmOCPP20Data->BootNotification.chargingStation.firmwareVersion,
  592. (char*) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  593. }
  594. }
  595. void ocpp_set_remotestart(uint8_t gun_index, uint8_t status)
  596. {
  597. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  598. {
  599. if (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq != status)
  600. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = status;
  601. }
  602. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  603. {
  604. if (ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq != status)
  605. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq = status;
  606. }
  607. }
  608. void ocpp_set_remotestop(uint8_t gun_index, uint8_t status)
  609. {
  610. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  611. {
  612. if (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != status)
  613. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = status;
  614. }
  615. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  616. {
  617. if (ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq != status)
  618. ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq = status;
  619. }
  620. }
  621. uint8_t ocpp_get_remotestart(uint8_t gun_index)
  622. {
  623. uint8_t result = OFF;
  624. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  625. {
  626. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq;
  627. }
  628. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  629. {
  630. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStartTransactionReq;
  631. }
  632. return result;
  633. }
  634. void ocpp_copy_userid_from_remotestart(uint8_t gun_index)
  635. {
  636. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  637. {
  638. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag,
  639. ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  640. }
  641. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  642. {
  643. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken,
  644. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  645. }
  646. }
  647. uint8_t ocpp_get_remotestop(uint8_t gun_index)
  648. {
  649. uint8_t result = OFF;
  650. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  651. {
  652. result = ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq;
  653. }
  654. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  655. {
  656. result = ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq;
  657. }
  658. return result;
  659. }
  660. void ocpp_set_auth_req(uint8_t status, ...)
  661. {
  662. va_list args;
  663. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  664. {
  665. if (ShmOCPP16Data->SpMsg.bits.AuthorizeReq != status)
  666. {
  667. if (status == ON)
  668. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, 0x00,
  669. ARRAY_SIZE(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  670. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = status;
  671. }
  672. }
  673. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  674. {
  675. if (ShmOCPP20Data->SpMsg.bits.AuthorizeReq != status)
  676. {
  677. if (status == ON)
  678. {
  679. memset(&ShmOCPP20Data->Authorize.Response_idTokenInfo, 0x00, sizeof(struct IdTokenInfoType));
  680. va_start(args, status);
  681. sprintf((char*) ShmOCPP20Data->Authorize.idToken.type, "%s", va_arg(args, char*));
  682. va_end(args);
  683. }
  684. ShmOCPP20Data->SpMsg.bits.AuthorizeReq = status;
  685. }
  686. }
  687. }
  688. uint8_t ocpp_get_auth_req()
  689. {
  690. uint8_t result = OFF;
  691. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  692. {
  693. result = ShmOCPP16Data->SpMsg.bits.AuthorizeReq;
  694. }
  695. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  696. {
  697. result = ShmOCPP20Data->SpMsg.bits.AuthorizeReq;
  698. }
  699. return result;
  700. }
  701. void ocpp_set_auth_conf(uint8_t status)
  702. {
  703. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  704. {
  705. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != status)
  706. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = status;
  707. }
  708. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  709. {
  710. if (ShmOCPP20Data->SpMsg.bits.AuthorizeConf != status)
  711. ShmOCPP20Data->SpMsg.bits.AuthorizeConf = status;
  712. }
  713. }
  714. uint8_t ocpp_get_auth_conf()
  715. {
  716. uint8_t result = OFF;
  717. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  718. {
  719. result = ShmOCPP16Data->SpMsg.bits.AuthorizeConf;
  720. }
  721. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  722. {
  723. result = ShmOCPP20Data->SpMsg.bits.AuthorizeConf;
  724. }
  725. return result;
  726. }
  727. uint8_t ocpp_get_auth_result(uint8_t isValidParent, ...)
  728. {
  729. uint8_t result = OFF;
  730. va_list args;
  731. uint8_t gun_index;
  732. if (isValidParent)
  733. {
  734. va_start(args, isValidParent);
  735. gun_index = va_arg(args, int);
  736. va_end(args);
  737. switch (getTargetChargingInfoData(gun_index)->SystemStatus)
  738. {
  739. case SYS_MODE_AUTHORIZING:
  740. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  741. {
  742. if ((strcmp((char*) ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted") == 0))
  743. result = PASS;
  744. }
  745. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  746. {
  747. if ((strcmp((char*) ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted") == 0))
  748. result = PASS;
  749. }
  750. break;
  751. case SYS_MODE_CHARGING:
  752. case SYS_MODE_TERMINATING:
  753. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  754. {
  755. if ((strcmp((char*) ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted") == 0)
  756. && (strcmp((char*) ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag,
  757. (char*) ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag) == 0))
  758. result = PASS;
  759. }
  760. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  761. {
  762. if ((strcmp((char*) ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted") == 0)
  763. && (strcmp((char*) ShmOCPP20Data->Authorize.Response_idTokenInfo.groupIdToken.idToken,
  764. (char*) ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.groupIdToken.idToken) == 0))
  765. result = PASS;
  766. }
  767. break;
  768. default:
  769. break;
  770. }
  771. }
  772. else
  773. {
  774. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  775. {
  776. if ((strcmp((char*) ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted") == 0))
  777. result = PASS;
  778. }
  779. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  780. {
  781. if ((strcmp((char*) ShmOCPP20Data->Authorize.Response_idTokenInfo.status, "Accepted") == 0))
  782. result = PASS;
  783. }
  784. }
  785. //DEBUG_INFO("Authorize result : %s \n", ((result == PASS)?"Pass":"Fail"));
  786. return result;
  787. }
  788. uint8_t ocpp_get_unlocker_req(uint8_t gun_index)
  789. {
  790. uint8_t result = OFF;
  791. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  792. {
  793. result = ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].isUnlockerConnetor;
  794. }
  795. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  796. {
  797. result = ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].isUnlockerConnetor;
  798. }
  799. return result;
  800. }
  801. void ocpp_set_unlocker_req(uint8_t gun_index, uint8_t status)
  802. {
  803. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  804. {
  805. if (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].isUnlockerConnetor != status)
  806. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].isUnlockerConnetor = status;
  807. }
  808. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  809. {
  810. if (ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].isUnlockerConnetor != status)
  811. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].isUnlockerConnetor = status;
  812. }
  813. }
  814. void ocpp_set_starttransaction_req(uint8_t gun_index, uint8_t status)
  815. {
  816. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  817. {
  818. if (ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq != status)
  819. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = status;
  820. }
  821. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  822. {
  823. if (ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
  824. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
  825. }
  826. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != 0)
  827. {
  828. if (ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq != status)
  829. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionReq = status;
  830. }
  831. }
  832. void ocpp_set_starttransaction_conf(uint8_t gun_index, uint8_t status)
  833. {
  834. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  835. {
  836. if (ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf != status)
  837. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = status;
  838. }
  839. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  840. {
  841. if (ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
  842. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
  843. }
  844. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != 0)
  845. {
  846. if (ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf != status)
  847. ShmOCPP16DataPH->CpMsg.bits[gun_index].StartTransactionConf = status;
  848. }
  849. }
  850. void ocpp_set_stoptransaction_req(uint8_t gun_index, uint8_t status)
  851. {
  852. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  853. {
  854. if (ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq != status)
  855. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = status;
  856. }
  857. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  858. {
  859. if (ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq != status)
  860. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = status;
  861. }
  862. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != 0)
  863. {
  864. if (ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq != status)
  865. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = status;
  866. }
  867. }
  868. void ocpp_set_stoptransaction_conf(uint8_t gun_index, uint8_t status)
  869. {
  870. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  871. {
  872. if (ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf != status)
  873. {
  874. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = status;
  875. }
  876. }
  877. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  878. {
  879. if (ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf != status)
  880. {
  881. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventConf = status;
  882. }
  883. }
  884. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != 0)
  885. {
  886. if (ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionConf != status)
  887. {
  888. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionConf = status;
  889. }
  890. }
  891. }
  892. void ocpp_copy_userid_to_starttransaction(uint8_t gun_index)
  893. {
  894. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  895. {
  896. memcpy((char*) ShmOCPP16Data->StartTransaction[gun_index].IdTag,
  897. (char*) getTargetChargingInfoData(gun_index)->StartUserId,
  898. ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  899. }
  900. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  901. {
  902. memcpy((char*) ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken,
  903. (char*) getTargetChargingInfoData(gun_index)->StartUserId,
  904. ARRAY_SIZE(getTargetChargingInfoData(gun_index)->StartUserId));
  905. }
  906. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != 0)
  907. {
  908. memcpy((char*) ShmOCPP16DataPH->StartTransaction[gun_index].IdTag,
  909. (char*) getTargetChargingInfoData(gun_index)->StartUserId,
  910. ARRAY_SIZE(ShmOCPP16DataPH->StartTransaction[gun_index].IdTag));
  911. }
  912. }
  913. uint8_t ocpp_get_starttransaction_result(uint8_t gun_index)
  914. {
  915. uint8_t result = PASS;
  916. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  917. {
  918. if (strstr((char*) ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  919. {
  920. if ((strcmp((char*) ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Blocked") == 0)
  921. || (strcmp((char*) ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Expired") == 0)
  922. || (strcmp((char*) ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.Status, "Invalid") == 0))
  923. result = NO;
  924. }
  925. }
  926. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  927. {
  928. if (strstr((char*) ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE"))
  929. {
  930. if ((strcmp((char*) ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Blocked") == 0)
  931. || (strcmp((char*) ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Expired") == 0)
  932. || (strcmp((char*) ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Invalid") == 0)
  933. || (strcmp((char*) ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NoCredit") == 0)
  934. || (strcmp((char*) ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAllowedTypeEVSE") == 0)
  935. || (strcmp((char*) ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisLocation") == 0)
  936. || (strcmp((char*) ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "NotAtThisTime") == 0)
  937. || (strcmp((char*) ShmOCPP20Data->TransactionEvent[gun_index].Response_idTokenInfo.status, "Unknown") == 0))
  938. result = NO;
  939. }
  940. }
  941. return result;
  942. }
  943. uint8_t ocpp_get_maxcharging_profileId()
  944. {
  945. uint8_t result = 0;
  946. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  947. {
  948. result = ShmOCPP16Data->MaxChargingProfile.ChargingProfileId;
  949. }
  950. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  951. {
  952. result = ShmOCPP20Data->MaxChargingProfile.id;
  953. }
  954. return result;
  955. }
  956. uint8_t ocpp_get_smartcharging_profileId(uint8_t gun_index)
  957. {
  958. uint8_t result = 0;
  959. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  960. {
  961. result = ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId;
  962. }
  963. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  964. {
  965. result = ShmOCPP20Data->SmartChargingProfile[gun_index].id;
  966. }
  967. return result;
  968. }
  969. void ocpp_reset_smartcharging_profileId(uint8_t gun_index)
  970. {
  971. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  972. {
  973. ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId = 0;
  974. }
  975. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  976. {
  977. ShmOCPP20Data->SmartChargingProfile[gun_index].id = 0;
  978. }
  979. }
  980. uint8_t ocpp_get_profile_req(uint8_t gun_index)
  981. {
  982. uint8_t result = OFF;
  983. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  984. {
  985. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq;
  986. }
  987. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  988. {
  989. result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq;
  990. }
  991. return result;
  992. }
  993. void ocpp_set_profile_req(uint8_t gun_index, uint8_t status)
  994. {
  995. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  996. {
  997. if (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
  998. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
  999. }
  1000. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1001. {
  1002. if (ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq != status)
  1003. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileReq = status;
  1004. }
  1005. }
  1006. uint8_t ocpp_get_profile_conf(uint8_t gun_index)
  1007. {
  1008. uint8_t result = OFF;
  1009. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1010. {
  1011. result = ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf;
  1012. }
  1013. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1014. {
  1015. result = ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf;
  1016. }
  1017. return result;
  1018. }
  1019. void ocpp_set_profile_conf(uint8_t gun_index, uint8_t status)
  1020. {
  1021. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1022. {
  1023. if (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
  1024. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
  1025. }
  1026. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1027. {
  1028. if (ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf != status)
  1029. ShmOCPP20Data->CSUMsg.bits[gun_index].ChargingProfileConf = status;
  1030. }
  1031. }
  1032. uint8_t ocpp_get_StopTransactionOnEVSideDisconnect()
  1033. {
  1034. uint8_t result = OFF;
  1035. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1036. {
  1037. if (strcmp((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)
  1038. result = ON;
  1039. }
  1040. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1041. {
  1042. if (strcmp((char *) ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnEVSideDisconnect].variableAttribute[0].value, "TRUE") == 0)
  1043. result = ON;
  1044. }
  1045. return result;
  1046. }
  1047. uint8_t ocpp_get_cancelreservation_req(uint8_t gun_index)
  1048. {
  1049. uint8_t result = OFF;
  1050. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1051. {
  1052. result = ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq;
  1053. }
  1054. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1055. {
  1056. result = ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq;
  1057. }
  1058. return result;
  1059. }
  1060. void ocpp_set_cancelreservation_req(uint8_t gun_index, uint8_t status)
  1061. {
  1062. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1063. {
  1064. if (ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq != status)
  1065. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = status;
  1066. if (ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
  1067. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  1068. }
  1069. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1070. {
  1071. if (ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq != status)
  1072. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq = status;
  1073. if (ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationReq == OFF)
  1074. ShmOCPP20Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  1075. }
  1076. }
  1077. uint8_t ocpp_compare_reserve_id_with_user(uint8_t gun_index)
  1078. {
  1079. uint8_t result = OFF;
  1080. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1081. {
  1082. if (strcmp((char*) ShmSysConfigAndInfo->SysConfig.UserId, (char*) ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  1083. result = ON;
  1084. }
  1085. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1086. {
  1087. if (strcmp((char*) ShmSysConfigAndInfo->SysConfig.UserId, (char*) ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
  1088. result = ON;
  1089. }
  1090. return result;
  1091. }
  1092. uint8_t ocpp_compare_reserve_id_with_remote_user(uint8_t gun_index)
  1093. {
  1094. uint8_t result = OFF;
  1095. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1096. {
  1097. if (strcmp((char*) ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag,
  1098. (char*) ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  1099. {
  1100. result = ON;
  1101. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag,
  1102. ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  1103. }
  1104. }
  1105. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1106. {
  1107. if (strcmp((char*) ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken,
  1108. (char*) ShmOCPP20Data->ReserveNow[gun_index].idToken.idToken) == 0)
  1109. {
  1110. result = ON;
  1111. memcpy(ShmSysConfigAndInfo->SysConfig.UserId,
  1112. ShmOCPP20Data->RequestStartTransaction[gun_index].idToken.idToken,
  1113. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  1114. }
  1115. }
  1116. return result;
  1117. }
  1118. uint8_t ocpp_isAuthorizeRemoteStart()
  1119. {
  1120. uint8_t result = NO;
  1121. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1122. {
  1123. if (strstr((char*) ShmOCPP16Data->ConfigurationTable.CoreProfile[AuthorizeRemoteTxRequests].ItemData, "TRUE"))
  1124. {
  1125. result = YES;
  1126. }
  1127. }
  1128. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1129. {
  1130. if (strstr(
  1131. (char*) ShmOCPP20Data->ControllerComponentVariable[AuthCtrlr_AuthorizeRemoteStart].variableAttribute[0].value, "TRUE"))
  1132. {
  1133. result = YES;
  1134. }
  1135. }
  1136. return result;
  1137. }
  1138. uint8_t ocpp_get_freevend_idtag(uint8_t * userId)
  1139. {
  1140. uint8_t result = NO;
  1141. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1142. {
  1143. strcpy((char*) userId, (char*) ShmOCPP16Data->ConfigurationTable.CoreProfile[FreeVendIdtag].ItemData);
  1144. result = YES;
  1145. }
  1146. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1147. {
  1148. strcpy((char*) userId,
  1149. (char*) ShmOCPP20Data->ControllerComponentVariable[ChargingStation_FreeVendIdtag].variableAttribute[0].value);
  1150. result = YES;
  1151. }
  1152. return result;
  1153. }
  1154. //======================================================
  1155. // Check interface status
  1156. //======================================================
  1157. int isInterfaceUp(const char *interface)
  1158. {
  1159. int result = FAIL;
  1160. FILE *fp;
  1161. char cmd[256];
  1162. char buf[512];
  1163. strcpy(cmd, "ifconfig");
  1164. ;
  1165. fp = popen(cmd, "r");
  1166. if (fp != NULL)
  1167. {
  1168. while (fgets(buf, sizeof(buf), fp) != NULL)
  1169. {
  1170. if (strstr(buf, interface) > 0)
  1171. {
  1172. result = PASS;
  1173. }
  1174. }
  1175. }
  1176. pclose(fp);
  1177. return result;
  1178. }
  1179. //======================================================
  1180. // Create all share memory
  1181. //======================================================
  1182. int CreatShareMemory()
  1183. {
  1184. int result = PASS;
  1185. int MeterSMId;
  1186. //creat ShmSysConfigAndInfo
  1187. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  1188. {
  1189. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1190. result = FAIL;
  1191. }
  1192. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1193. {
  1194. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  1195. result = FAIL;
  1196. }
  1197. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  1198. //creat ShmStatusCodeData
  1199. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  1200. {
  1201. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1202. result = FAIL;
  1203. }
  1204. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1205. {
  1206. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1207. result = FAIL;
  1208. }
  1209. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  1210. //creat ShmPsuData
  1211. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  1212. {
  1213. DEBUG_ERROR("shmget ShmPsuData NG\n");
  1214. result = FAIL;
  1215. }
  1216. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1217. {
  1218. DEBUG_ERROR("shmat ShmPsuData NG\n");
  1219. result = FAIL;
  1220. }
  1221. memset(ShmPsuData, 0, sizeof(struct PsuData));
  1222. //creat ShmCHAdeMOData
  1223. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  1224. {
  1225. DEBUG_ERROR("shmget ShmCHAdeMOData NG1\n");
  1226. result = FAIL;
  1227. }
  1228. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1229. {
  1230. DEBUG_ERROR("shmat ShmCHAdeMOData NG2\n");
  1231. result = FAIL;
  1232. }
  1233. memset(ShmCHAdeMOData, 0, sizeof(struct CHAdeMOData));
  1234. //creat ShmCcsData
  1235. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  1236. {
  1237. DEBUG_ERROR("shmget ShmCcsData NG\n");
  1238. result = FAIL;
  1239. }
  1240. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1241. {
  1242. DEBUG_ERROR("shmat ShmCcsData NG\n");
  1243. result = FAIL;
  1244. }
  1245. memset(ShmCcsData, 0, sizeof(struct CcsData));
  1246. //creat ShmPrimaryMcuData
  1247. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  1248. {
  1249. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1250. result = FAIL;
  1251. }
  1252. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1253. {
  1254. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1255. result = FAIL;
  1256. }
  1257. memset(ShmPrimaryMcuData, 0, sizeof(struct PrimaryMcuData));
  1258. //creat ShmOCPP16Data
  1259. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  1260. {
  1261. DEBUG_ERROR("shmget ShmOCPP16Data NG\n");
  1262. result = FAIL;
  1263. }
  1264. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1265. {
  1266. DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
  1267. result = FAIL;
  1268. }
  1269. memset(ShmOCPP16Data, 0, sizeof(struct OCPP16Data));
  1270. //creat ShmOCPP20Data
  1271. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  1272. {
  1273. DEBUG_ERROR("shmget OCPP20Data NG\n");
  1274. result = FAIL;
  1275. }
  1276. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1277. {
  1278. DEBUG_ERROR("shmat OCPP20Data NG\n");
  1279. result = FAIL;
  1280. }
  1281. memset(ShmOCPP20Data, 0, sizeof(struct OCPP20Data));
  1282. //creat ShmCharger
  1283. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
  1284. {
  1285. DEBUG_ERROR("shmget ShmCharger NG\n");
  1286. result = FAIL;
  1287. }
  1288. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1289. {
  1290. DEBUG_ERROR("shmat ShmCharger NG\n");
  1291. result = FAIL;
  1292. }
  1293. memset(ShmCharger, 0, sizeof(struct Charger));
  1294. //creat ShmOCPP16DataPH
  1295. if ((MeterSMId = shmget(ShmOcppPHModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  1296. {
  1297. DEBUG_ERROR("shmget ShmOCPP16DataPH NG\n");
  1298. result = FAIL;
  1299. }
  1300. else if ((ShmOCPP16DataPH = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1301. {
  1302. DEBUG_ERROR("shmat ShmOCPP16DataPH NG\n");
  1303. result = FAIL;
  1304. }
  1305. else
  1306. {
  1307. }
  1308. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  1309. return result;
  1310. }
  1311. //======================================================
  1312. // Call WEBAPI to mail receipt (Only for CDFA verify)
  1313. //======================================================
  1314. #ifdef CDFA_CERTIFICATE
  1315. int sendReceiptMail(char *startDateTime, char *stopDateTime, char *receiptInfo)
  1316. {
  1317. int result = FAIL;
  1318. char reciever[128];
  1319. char subject[128];
  1320. char content[8192];
  1321. char priceDetail[4098]={0};
  1322. char cmdBuf[8192];
  1323. double totalEnergy;
  1324. double totalPrice;
  1325. json_object *detailPrice = json_tokener_parse(receiptInfo);
  1326. if(!is_error(detailPrice))
  1327. {
  1328. if(json_object_object_get(detailPrice, "totalChargedEnergy") != NULL)
  1329. totalEnergy = json_object_get_double(json_object_object_get(detailPrice, "totalChargedEnergy"));
  1330. if(json_object_object_get(detailPrice, "totalPrice") != NULL)
  1331. totalPrice = json_object_get_double(json_object_object_get(detailPrice, "totalPrice"));
  1332. if(json_object_object_get(detailPrice, "receiptDetail") != NULL)
  1333. {
  1334. for(int idx=0;idx<json_object_array_length(json_object_object_get(detailPrice, "receiptDetail"));idx++)
  1335. {
  1336. sprintf(priceDetail, "%s---%02d:00, %.4f KWH @ $ %.2f = $ %.2f<br/>\n", priceDetail
  1337. , json_object_get_int(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "hour"))
  1338. , json_object_get_double(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "chargedEnergy"))
  1339. , json_object_get_double(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "unitPrice"))
  1340. , json_object_get_double(json_object_object_get(json_object_array_get_idx(json_object_object_get(detailPrice, "receiptDetail"), idx), "periodPrice")));
  1341. }
  1342. }
  1343. }
  1344. json_object_put(detailPrice);
  1345. // Generate mail content
  1346. sprintf(reciever, "folus_wen@phihong.com.tw");
  1347. sprintf(subject, "Charging Receipt From %s", ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  1348. sprintf(content, "<img src=\"https://www.phihong.com/images/logo.png\"/><br/>\n"
  1349. "<h1>Receipt of charging on %s</h1><br/>\n"
  1350. "<b>Location Name:</b> CDFA Lab<br/>\n"
  1351. "<b>Location Address:</b> xxxxxxxxxxx<br/>\n"
  1352. "<b>EVSE ID:</b> %s<br/>\n"
  1353. "<b>Maximum Rate of Energy Transfer:</b> 11.5 KW AC<br/>\n"
  1354. "<b>Charging start date time:</b> %s<br/>\n"
  1355. "<b>Charging stop date time:</b> %s<br/>\n"
  1356. "<b>Energy Delivered:</b> %.4f KWH<br/>\n"
  1357. "<b>Cost of Charging:</b><br/>\n"
  1358. "%s"
  1359. "***Total charged cost: $ %.2f<br/>\n"
  1360. "<br/>\n"
  1361. "<br/>\n"
  1362. "%s<br/>\n"
  1363. "47800 Fremont Blvd.<br/>\n"
  1364. "Fremont, CA 94538, USA<br/>\n", startDateTime
  1365. , ShmSysConfigAndInfo->SysConfig.ChargeBoxId
  1366. , startDateTime
  1367. , stopDateTime
  1368. , totalEnergy
  1369. , priceDetail
  1370. , totalPrice
  1371. , ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  1372. json_object *post = json_object_new_object();
  1373. json_object_object_add(post, "Receiver", json_object_new_string(reciever));
  1374. json_object_object_add(post, "Subject", json_object_new_string(subject));
  1375. json_object_object_add(post, "Body", json_object_new_string(content));
  1376. json_object_object_add(post, "UseHtml", json_object_new_boolean(1));
  1377. // Call WEBAPI
  1378. sprintf(cmdBuf, "curl -X POST -k -d '%s' -H \"Content-Type: application/json\" https://ocpp.phihong.com.tw:5014/api/Notification/ &", json_object_to_json_string_ext(post, JSON_C_TO_STRING_PLAIN));
  1379. system(cmdBuf);
  1380. //DEBUG_INFO("cmdBuf: %s\n", cmdBuf);
  1381. json_object_put(post);
  1382. return result;
  1383. }
  1384. #endif /* CDFA_CERTIFICATE */
  1385. //======================================================
  1386. // SQLite3 related routine
  1387. //======================================================
  1388. int getReceiptInfo(uint8_t gun_index, char *receiptInfo)
  1389. {
  1390. int result = PASS;
  1391. double totalEnergy = 0.0;
  1392. double totalPrice = 0.0;
  1393. json_object *receipt = json_object_new_object();
  1394. json_object *receiptAry = json_object_new_array();
  1395. for (uint8_t idxHour = 0; idxHour < ARRAY_SIZE(getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod); idxHour++)
  1396. {
  1397. json_object *receiptDetail = json_object_new_object();
  1398. json_object_object_add(receiptDetail, "hour", json_object_new_int(idxHour));
  1399. json_object_object_add(receiptDetail, "chargedEnergy",
  1400. json_object_new_double(getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod[idxHour]));
  1401. json_object_object_add(receiptDetail, "unitPrice",
  1402. json_object_new_double(ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour]));
  1403. json_object_object_add(receiptDetail, "periodPrice",
  1404. json_object_new_double(getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod[idxHour]
  1405. * ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour]));
  1406. totalEnergy += getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod[idxHour];
  1407. totalPrice += getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod[idxHour]
  1408. * ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour];
  1409. json_object_array_add(receiptAry, receiptDetail);
  1410. }
  1411. json_object_object_add(receipt, "receiptDetail", receiptAry);
  1412. json_object_object_add(receipt, "totalChargedEnergy", json_object_new_double(totalEnergy));
  1413. json_object_object_add(receipt, "totalPrice", json_object_new_double(totalPrice));
  1414. json_object_object_add(receipt, "serviceProvider",
  1415. json_object_new_string((char*) ShmSysConfigAndInfo->SysConfig.chargePointVendor));
  1416. json_object_object_add(receipt, "serviceProviderAddress", json_object_new_string("Address"));
  1417. json_object_object_add(receipt, "serviceProviderTel", json_object_new_string("TEL"));
  1418. json_object_object_add(receipt, "chargerBoxId",
  1419. json_object_new_string((char*) ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1420. sprintf(receiptInfo, "%s", json_object_to_json_string_ext(receipt, JSON_C_TO_STRING_PLAIN));
  1421. json_object_put(receipt);
  1422. //DEBUG_INFO("receiptInfo: %s\n", receiptInfo);
  1423. return result;
  1424. }
  1425. int DB_Open(sqlite3 *db)
  1426. {
  1427. int result = PASS;
  1428. char* errMsg = NULL;
  1429. char* createRecordSql = "CREATE TABLE IF NOT EXISTS charging_record("
  1430. "idx integer primary key AUTOINCREMENT, "
  1431. "reservationId text, "
  1432. "transactionId text, "
  1433. "startMethod text, "
  1434. "userId text, "
  1435. "dateTimeStart text, "
  1436. "dateTimeStop text,"
  1437. "socStart text, "
  1438. "socStop text, "
  1439. "chargeEnergy text, "
  1440. "stopReason text, "
  1441. "finalCost text"
  1442. ");";
  1443. char* createRecordBufSql = "CREATE TABLE IF NOT EXISTS charging_record_buffer("
  1444. "reservationId text, "
  1445. "transactionId text, "
  1446. "startMethod text, "
  1447. "userId text, "
  1448. "dateTimeStart text, "
  1449. "dateTimeStop text,"
  1450. "socStart text, "
  1451. "socStop text, "
  1452. "chargeEnergy text, "
  1453. "stopReason text, "
  1454. "finalCost text"
  1455. ");";
  1456. char* createCfgSql = "CREATE TABLE IF NOT EXISTS `config` ( "
  1457. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  1458. "`item` TEXT NOT NULL, "
  1459. "`connector` INTEGER NOT NULL, "
  1460. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  1461. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  1462. if (sqlite3_open(DB_FILE, &db))
  1463. {
  1464. result = FAIL;
  1465. DEBUG_ERROR("Can't open database: %s\n", sqlite3_errmsg(db));
  1466. sqlite3_close(db);
  1467. }
  1468. else
  1469. {
  1470. DEBUG_INFO("Local charging record database open successfully.\n");
  1471. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  1472. {
  1473. result = FAIL;
  1474. DEBUG_ERROR("Create local charging record table error message: %s\n", errMsg);
  1475. }
  1476. else
  1477. {
  1478. DEBUG_INFO("Opened local charging record table successfully\n");
  1479. }
  1480. if (sqlite3_exec(db, createRecordBufSql, 0, 0, &errMsg) != SQLITE_OK)
  1481. {
  1482. result = FAIL;
  1483. DEBUG_ERROR("Create local charging record buffer table error message: %s\n", errMsg);
  1484. }
  1485. else
  1486. {
  1487. DEBUG_INFO("Opened local charging record buffer table successfully\n");
  1488. }
  1489. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  1490. {
  1491. result = FAIL;
  1492. DEBUG_ERROR("Create local config table error message: %s\n", errMsg);
  1493. }
  1494. else
  1495. {
  1496. DEBUG_INFO("Opened local config table successfully\n");
  1497. }
  1498. sqlite3_close(db);
  1499. }
  1500. return result;
  1501. }
  1502. int DB_Check_Record_Buf(sqlite3 *db, int gun_index)
  1503. {
  1504. int result = PASS;
  1505. char* errMsg = NULL;
  1506. char sqlStr[4096];
  1507. char **rs;
  1508. int rows, cols;
  1509. if (sqlite3_open(DB_FILE, &db))
  1510. {
  1511. result = FAIL;
  1512. DEBUG_INFO("Can't open database: %s\n", sqlite3_errmsg(db));
  1513. sqlite3_close(db);
  1514. }
  1515. else
  1516. {
  1517. sqlite3_get_table(db, "select * from charging_record_buffer", &rs, &rows, &cols, &errMsg);
  1518. if (rows > 0)
  1519. {
  1520. // Copy record buffer
  1521. for (int idxRow = 1; idxRow <= rows; idxRow++)
  1522. {
  1523. if (strcmp(rs[(idxRow * cols) + 3], "0") == 0)
  1524. {
  1525. result = false;
  1526. }
  1527. sprintf(sqlStr,
  1528. "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1529. "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
  1530. rs[(idxRow * cols) + 0], rs[(idxRow * cols) + 1], rs[(idxRow * cols) + 2],
  1531. rs[(idxRow * cols) + 3], rs[(idxRow * cols) + 4], rs[(idxRow * cols) + 5],
  1532. rs[(idxRow * cols) + 6], rs[(idxRow * cols) + 7], rs[(idxRow * cols) + 8],
  1533. rs[(idxRow * cols) + 9], rs[(idxRow * cols) + 10]);
  1534. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1535. {
  1536. result = FAIL;
  1537. DEBUG_INFO("Copy local charging record buffer error message: %s\n", errMsg);
  1538. }
  1539. else
  1540. {
  1541. DEBUG_INFO("Copy local charging record buffer successfully\n");
  1542. }
  1543. #ifdef CDFA_CERTIFICATE
  1544. // Only for CDFA certificate
  1545. sendReceiptMail(rs[(idxRow*cols)+4],
  1546. rs[(idxRow*cols)+5],
  1547. rs[(idxRow*cols)+10]);
  1548. #endif /* CDFA_CERTIFICATE */
  1549. }
  1550. // Delete buffer
  1551. if (sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
  1552. {
  1553. result = FAIL;
  1554. DEBUG_INFO("Delete local charging record buffer error message: %s\n", errMsg);
  1555. }
  1556. else
  1557. {
  1558. DEBUG_INFO("Delete local charging record buffer successfully\n");
  1559. }
  1560. }
  1561. else
  1562. {
  1563. DEBUG_INFO("There is not any charging record buffer.\n", gun_index);
  1564. }
  1565. sqlite3_free_table(rs);
  1566. sqlite3_close(db);
  1567. }
  1568. return result;
  1569. }
  1570. int DB_Update_Record_Buf(sqlite3 *db, int gun_index)
  1571. {
  1572. int result = PASS;
  1573. char* errMsg = NULL;
  1574. char sqlStr[4096];
  1575. char receiptInfo[2048];
  1576. getReceiptInfo(gun_index, receiptInfo);
  1577. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1578. {
  1579. sprintf(sqlStr,
  1580. "insert into charging_record_buffer(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1581. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1582. getTargetChargingInfoData(gun_index)->ReservationId,
  1583. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  1584. getTargetChargingInfoData(gun_index)->StartMethod, getTargetChargingInfoData(gun_index)->StartUserId,
  1585. getTargetChargingInfoData(gun_index)->StartDateTime, getTargetChargingInfoData(gun_index)->StopDateTime,
  1586. getTargetChargingInfoData(gun_index)->EvBatterySoc, getTargetChargingInfoData(gun_index)->EvBatterySoc,
  1587. getTargetChargingInfoData(gun_index)->PresentChargedEnergy, "Power Loss",
  1588. #ifdef CDFA_CERTIFICATE
  1589. receiptInfo);// Only for CDFA certificate
  1590. #else
  1591. ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  1592. #endif /* CDFA_CERTIFICATE */
  1593. }
  1594. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1595. {
  1596. sprintf(sqlStr,
  1597. "insert into charging_record_buffer(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1598. #ifdef CDFA_CERTIFICATE
  1599. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1600. #else
  1601. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%f');",
  1602. #endif /* CDFA_CERTIFICATE */
  1603. getTargetChargingInfoData(gun_index)->ReservationId,
  1604. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  1605. getTargetChargingInfoData(gun_index)->StartMethod, getTargetChargingInfoData(gun_index)->StartUserId,
  1606. getTargetChargingInfoData(gun_index)->StartDateTime, getTargetChargingInfoData(gun_index)->StopDateTime,
  1607. getTargetChargingInfoData(gun_index)->EvBatterySoc, getTargetChargingInfoData(gun_index)->EvBatterySoc,
  1608. getTargetChargingInfoData(gun_index)->PresentChargedEnergy, "Power Loss",
  1609. #ifdef CDFA_CERTIFICATE
  1610. //receiptInfo);// Only for CDFA certificate
  1611. #else
  1612. ShmOCPP20Data->CostUpdated.totalCost);
  1613. #endif /* CDFA_CERTIFICATE */
  1614. }
  1615. if (sqlite3_open(DB_FILE, &db))
  1616. {
  1617. result = FAIL;
  1618. DEBUG_INFO("Can't open database: %s\n", sqlite3_errmsg(db));
  1619. sqlite3_close(db);
  1620. }
  1621. else
  1622. {
  1623. // Delete buffer
  1624. if (sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
  1625. {
  1626. result = FAIL;
  1627. DEBUG_INFO("Delete local charging record buffer error message: %s\n", errMsg);
  1628. }
  1629. else
  1630. {
  1631. //DEBUG_INFO( "Delete local charging record buffer successfully\n");
  1632. }
  1633. // Insert record buffer
  1634. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1635. {
  1636. result = FAIL;
  1637. DEBUG_INFO("Insert local charging record buffer error message: %s\n", errMsg);
  1638. }
  1639. else
  1640. {
  1641. //DEBUG_INFO( "Insert local charging record buffer successfully\n");
  1642. }
  1643. sqlite3_close(db);
  1644. }
  1645. return result;
  1646. }
  1647. int DB_Insert_Record(sqlite3 *db, int gun_index)
  1648. {
  1649. int result = PASS;
  1650. char* errMsg = NULL;
  1651. char sqlStr[4096];
  1652. char receiptInfo[2048];
  1653. getReceiptInfo(gun_index, receiptInfo);
  1654. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  1655. {
  1656. sprintf(sqlStr,
  1657. "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1658. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1659. getTargetChargingInfoData(gun_index)->ReservationId,
  1660. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  1661. getTargetChargingInfoData(gun_index)->StartMethod, getTargetChargingInfoData(gun_index)->StartUserId,
  1662. getTargetChargingInfoData(gun_index)->StartDateTime, getTargetChargingInfoData(gun_index)->StopDateTime,
  1663. getTargetChargingInfoData(gun_index)->EvBatterySoc, getTargetChargingInfoData(gun_index)->EvBatterySoc,
  1664. getTargetChargingInfoData(gun_index)->PresentChargedEnergy,
  1665. ShmOCPP16Data->StopTransaction[gun_index].StopReason,
  1666. #ifdef CDFA_CERTIFICATE
  1667. receiptInfo);// Only for CDFA certificate
  1668. #else
  1669. ShmOCPP16Data->Cost.FinalCost[gun_index].description);
  1670. #endif /* CDFA_CERTIFICATE */
  1671. }
  1672. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  1673. {
  1674. sprintf(sqlStr,
  1675. "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
  1676. #ifdef CDFA_CERTIFICATE
  1677. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%s');",
  1678. #else
  1679. "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%f');",
  1680. #endif /* CDFA_CERTIFICATE */
  1681. getTargetChargingInfoData(gun_index)->ReservationId,
  1682. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
  1683. getTargetChargingInfoData(gun_index)->StartMethod, getTargetChargingInfoData(gun_index)->StartUserId,
  1684. getTargetChargingInfoData(gun_index)->StartDateTime, getTargetChargingInfoData(gun_index)->StopDateTime,
  1685. getTargetChargingInfoData(gun_index)->EvBatterySoc, getTargetChargingInfoData(gun_index)->EvBatterySoc,
  1686. getTargetChargingInfoData(gun_index)->PresentChargedEnergy,
  1687. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason,
  1688. #ifdef CDFA_CERTIFICATE
  1689. receiptInfo);// Only for CDFA certificate
  1690. #else
  1691. ShmOCPP20Data->CostUpdated.totalCost);
  1692. #endif /* CDFA_CERTIFICATE */
  1693. }
  1694. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1695. if (sqlite3_open(DB_FILE, &db))
  1696. {
  1697. result = FAIL;
  1698. DEBUG_INFO("Can't open database: %s\n", sqlite3_errmsg(db));
  1699. sqlite3_close(db);
  1700. }
  1701. else
  1702. {
  1703. // Delete buffer
  1704. if (sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
  1705. {
  1706. result = FAIL;
  1707. DEBUG_INFO("Delete local charging record buffer error message: %s\n", errMsg);
  1708. }
  1709. else
  1710. {
  1711. DEBUG_INFO("Delete local charging record buffer successfully\n");
  1712. }
  1713. // Insert charging record
  1714. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1715. {
  1716. result = FAIL;
  1717. DEBUG_INFO("Insert local charging record error message: %s\n", errMsg);
  1718. }
  1719. else
  1720. {
  1721. DEBUG_INFO("Insert local charging record successfully\n");
  1722. }
  1723. sprintf(sqlStr,
  1724. "delete from charging_record where (idx < (select idx from charging_record order by idx desc limit 1)-2000) and (dateTimeStop < '%04d.01.01 00:00:00');",
  1725. (getCurrentYear() - 3));
  1726. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1727. {
  1728. result = FAIL;
  1729. DEBUG_INFO("delete local charging error message: %s\n", errMsg);
  1730. }
  1731. else
  1732. {
  1733. DEBUG_INFO("delete local charging record successfully\n");
  1734. }
  1735. sqlite3_close(db);
  1736. }
  1737. #ifdef CDFA_CERTIFICATE
  1738. // Only for CDFA certificate
  1739. sendReceiptMail((char*)getTargetChargingInfoData(gun_index)->StartDateTime,
  1740. (char*)getTargetChargingInfoData(gun_index)->StopDateTime,
  1741. receiptInfo);
  1742. #endif
  1743. return result;
  1744. }
  1745. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t isOperactive)
  1746. {
  1747. uint8_t result = false;
  1748. char* errMsg = NULL;
  1749. char sqlStr[1024];
  1750. srand(time(NULL));
  1751. if (sqlite3_open(DB_FILE, &db))
  1752. {
  1753. result = FAIL;
  1754. DEBUG_INFO("Can't open database: %s\n", sqlite3_errmsg(db));
  1755. sqlite3_close(db);
  1756. }
  1757. else
  1758. {
  1759. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('isOperactive', %d, %d);",
  1760. gun_index, isOperactive);
  1761. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1762. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1763. {
  1764. result = FAIL;
  1765. DEBUG_INFO("update config error message: %s\n", errMsg);
  1766. }
  1767. else
  1768. {
  1769. DEBUG_INFO("update connector-%d config item isOperactive to %d\n", gun_index, isOperactive);
  1770. }
  1771. sqlite3_close(db);
  1772. }
  1773. return result;
  1774. }
  1775. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  1776. {
  1777. uint8_t result = true;
  1778. char* errMsg = NULL;
  1779. char sqlStr[1024];
  1780. char **rs;
  1781. int rows, cols;
  1782. sprintf(sqlStr, "select * from config where item='isOperactive' and connector=%d;", gun_index);
  1783. //DEBUG_INFO("sqlStr= %s\n", sqlStr);
  1784. if (sqlite3_open(DB_FILE, &db))
  1785. {
  1786. result = FAIL;
  1787. DEBUG_INFO("Can't open database: %s\n", sqlite3_errmsg(db));
  1788. sqlite3_close(db);
  1789. }
  1790. else
  1791. {
  1792. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1793. if (rows > 0)
  1794. {
  1795. for (int idxRow = 1; idxRow <= rows; idxRow++)
  1796. {
  1797. if (strcmp(rs[(idxRow * cols) + 3], "0") == 0)
  1798. {
  1799. result = false;
  1800. }
  1801. DEBUG_INFO("Query connector-%d isOperactive: %s\n", gun_index, rs[(idxRow * cols) + 3]);
  1802. }
  1803. }
  1804. else
  1805. {
  1806. DEBUG_INFO("Query connector-%d fail, default value as operactive.\n", gun_index);
  1807. }
  1808. sqlite3_free_table(rs);
  1809. sqlite3_close(db);
  1810. }
  1811. return result;
  1812. }
  1813. //======================================================
  1814. // Peripheral initial
  1815. //======================================================
  1816. int InitWatchDog()
  1817. {
  1818. int fd;
  1819. int timeout = 180;
  1820. system("/usr/bin/fuser -k /dev/watchdog");
  1821. sleep(1);
  1822. system("echo V > /dev/watchdog");
  1823. sleep(1);
  1824. fd = open("/dev/watchdog", O_RDWR);
  1825. if (fd <= 0)
  1826. {
  1827. DEBUG_ERROR("System watch dog initial fail.\n");
  1828. }
  1829. ioctl(fd, _IOWR('W', 6, int), &timeout);
  1830. return fd;
  1831. }
  1832. void InitGPIO()
  1833. {
  1834. /*****************0~3, 4 bank, bank x 32+ num*********************/
  1835. /***************************************************************/
  1836. /*************** INPUT PIN ***************************************/
  1837. /***************************************************************/
  1838. /***************************************************************/
  1839. /*************** OUTPUT PIN ************************************/
  1840. /***************************************************************/
  1841. /*MCU request:GPIO3_20 => H:ON; L:OFF*/
  1842. system("echo 116 > /sys/class/gpio/export");
  1843. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  1844. system("echo 0 > /sys/class/gpio/gpio116/value");
  1845. /*Rfid:GPIO0_19 => Reset_PING H:ON; L:OFF*/
  1846. system("echo 19 > /sys/class/gpio/export");
  1847. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  1848. system("echo 1 > /sys/class/gpio/gpio19/value");
  1849. /*Speaker:GPIO2_1 => H:ON; L:OFF*/
  1850. system("echo 65 > /sys/class/gpio/export");
  1851. system("echo \"out\" > /sys/class/gpio/gpio65/direction");
  1852. system("echo 0 > /sys/class/gpio/gpio65/value");
  1853. /*Ethernet RST:GPIO1_24 => H:ON; L:OFF*/
  1854. system("echo 56 > /sys/class/gpio/export");
  1855. system("echo \"out\" > /sys/class/gpio/gpio56/direction");
  1856. system("echo 0 > /sys/class/gpio/gpio56/value");
  1857. sleep(3);
  1858. system("echo 1 > /sys/class/gpio/gpio56/value");
  1859. /*4G POWER RST:GPIO3_18 => H:ON; L:OFF*/
  1860. system("echo 114 > /sys/class/gpio/export");
  1861. system("echo \"out\" > /sys/class/gpio/gpio114/direction");
  1862. system("echo 1 > /sys/class/gpio/gpio114/value");
  1863. sleep(3);
  1864. system("echo 0 > /sys/class/gpio/gpio114/value");
  1865. /*4G/WIFI POWER RST:GPIO1_27 => H:OFF L:ON*/
  1866. system("echo 59 > /sys/class/gpio/export");
  1867. system("echo \"out\" > /sys/class/gpio/gpio59/direction");
  1868. system("echo 1 > /sys/class/gpio/gpio59/value");
  1869. sleep(3);
  1870. system("echo 0 > /sys/class/gpio/gpio59/value");
  1871. /*RFID ICC:GPIO0_20 => H:ON; L:OFF*/
  1872. system("echo 20 > /sys/class/gpio/export");
  1873. system("echo \"in\" > /sys/class/gpio/gpio20/direction");
  1874. /*QCA7000 interrupt:GPIO2_00 => H:ON; L:OFF*/
  1875. system("echo 64 > /sys/class/gpio/export");
  1876. system("echo \"in\" > /sys/class/gpio/gpio64/direction");
  1877. /*QCA7000 Reset:GPIO3_19 => H:ON; L:OFF*/
  1878. system("echo 115 > /sys/class/gpio/export");
  1879. system("echo \"out\" > /sys/class/gpio/gpio115/direction");
  1880. system("echo 0 > /sys/class/gpio/gpio115/value");
  1881. sleep(3);
  1882. system("echo 1 > /sys/class/gpio/gpio115/value");
  1883. /*RFID RST: GPIO1_30 => H:OFF; L:ON*/
  1884. system("echo 62 > /sys/class/gpio/export");
  1885. system("echo \"out\" > /sys/class/gpio/gpio62/direction");
  1886. system("echo 1 > /sys/class/gpio/gpio62/value");
  1887. /*Wake up button GPIO1_31 => H:OFF; L:ON*/
  1888. system("echo 63 > /sys/class/gpio/export");
  1889. system("echo \"in\" > /sys/class/gpio/gpio63/direction");
  1890. sleep(1);
  1891. DEBUG_INFO("Initial GPIO OK\n");
  1892. }
  1893. //lwtest
  1894. uint8_t getAcPhaseCount(void)
  1895. {
  1896. uint8_t cnt = 1;
  1897. if (ShmSysConfigAndInfo->SysConfig.ModelName[INPUT_TYPE] == 'Y'
  1898. || ShmSysConfigAndInfo->SysConfig.ModelName[INPUT_TYPE] == 'D'
  1899. || ShmSysConfigAndInfo->SysConfig.ModelName[INPUT_TYPE] == 'W')
  1900. cnt = 3;
  1901. else
  1902. cnt = 1;
  1903. DEBUG_INFO("Get Phase Count %d\n", cnt);
  1904. return cnt;
  1905. }
  1906. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  1907. {
  1908. int fd, wrd;
  1909. unsigned char *buf;
  1910. unsigned int ChkSum, ChkSumOrg;
  1911. if ((buf = malloc(MtdBlockSize)) == NULL)
  1912. {
  1913. DEBUG_ERROR("malloc buffer NG, rebooting..\n");
  1914. if (ShmStatusCodeData != NULL)
  1915. {
  1916. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1917. }
  1918. sleep(5);
  1919. system("reboot -f");
  1920. sleep(5);
  1921. system("reboot -f");
  1922. }
  1923. memset(buf, 0, MtdBlockSize);
  1924. //================================================
  1925. // Load configuration from mtdblock10
  1926. //================================================
  1927. system("nanddump /dev/mtd10 -f /mnt/EvseConfig.bin");
  1928. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1929. if (fd < 0)
  1930. {
  1931. free(buf);
  1932. DEBUG_ERROR("open mtdblock10 NG, rebooting..\n");
  1933. if (ShmStatusCodeData != NULL)
  1934. {
  1935. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1936. }
  1937. sleep(5);
  1938. system("reboot -f");
  1939. sleep(5);
  1940. system("reboot -f");
  1941. }
  1942. wrd = read(fd, buf, MtdBlockSize);
  1943. close(fd);
  1944. if (wrd < MtdBlockSize)
  1945. {
  1946. free(buf);
  1947. DEBUG_ERROR("read SysConfigData data NG, rebooting..\n");
  1948. if (ShmStatusCodeData != NULL)
  1949. {
  1950. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1951. }
  1952. sleep(5);
  1953. system("reboot -f");
  1954. sleep(5);
  1955. system("reboot -f");
  1956. }
  1957. ChkSum = 0;
  1958. for (wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  1959. {
  1960. ChkSum += buf[wrd];
  1961. }
  1962. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  1963. memcpy(&ptr->ModelName, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), ARRAY_SIZE(ptr->ModelName));
  1964. memcpy(&ptr->SerialNumber, buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName) + ARRAY_SIZE(ptr->AcModelName)),
  1965. ARRAY_SIZE(ptr->SerialNumber));
  1966. //================================================
  1967. // Load configuration from mtdblock11
  1968. //================================================
  1969. if (ChkSum != ChkSumOrg)
  1970. {
  1971. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\n");
  1972. system("nanddump /dev/mtd11 -f /mnt/EvseConfig.bin");
  1973. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  1974. if (fd < 0)
  1975. {
  1976. free(buf);
  1977. DEBUG_ERROR("open mtdblock11 (backup) NG, rebooting..\n");
  1978. if (ShmStatusCodeData != NULL)
  1979. {
  1980. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1981. }
  1982. sleep(5);
  1983. system("reboot -f");
  1984. sleep(5);
  1985. system("reboot -f");
  1986. }
  1987. memset(buf, 0, MtdBlockSize);
  1988. wrd = read(fd, buf, MtdBlockSize);
  1989. close(fd);
  1990. if (wrd < MtdBlockSize)
  1991. {
  1992. free(buf);
  1993. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\n");
  1994. if (ShmStatusCodeData != NULL)
  1995. {
  1996. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1997. }
  1998. sleep(5);
  1999. system("reboot -f");
  2000. sleep(5);
  2001. system("reboot -f");
  2002. }
  2003. ChkSum = 0;
  2004. for (wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  2005. {
  2006. ChkSum += buf[wrd];
  2007. }
  2008. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  2009. //================================================
  2010. // Load configuration from mtdblock12 (Factory default)
  2011. //================================================
  2012. if (ChkSum != ChkSumOrg)
  2013. {
  2014. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\n");
  2015. system("nanddump /dev/mtd12 -f /mnt/EvseConfig.bin");
  2016. fd = open("/mnt/EvseConfig.bin", O_RDWR);
  2017. if (fd < 0)
  2018. {
  2019. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\n");
  2020. free(buf);
  2021. if (ShmStatusCodeData != NULL)
  2022. {
  2023. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  2024. }
  2025. sleep(5);
  2026. system("reboot -f");
  2027. sleep(5);
  2028. system("reboot -f");
  2029. }
  2030. memset(buf, 0, MtdBlockSize);
  2031. wrd = read(fd, buf, MtdBlockSize);
  2032. close(fd);
  2033. if (wrd < MtdBlockSize)
  2034. {
  2035. DEBUG_ERROR("read factory default SysConfigData data NG, rebooting..\n");
  2036. free(buf);
  2037. if (ShmStatusCodeData != NULL)
  2038. {
  2039. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  2040. }
  2041. sleep(5);
  2042. system("reboot -f");
  2043. sleep(5);
  2044. system("reboot -f");
  2045. }
  2046. ChkSum = 0;
  2047. for (wrd = ARRAY_SIZE(ptr->CsuBootLoadFwRev); wrd < MtdBlockSize - 4; wrd++)
  2048. {
  2049. ChkSum += buf[wrd];
  2050. }
  2051. memcpy(&ChkSumOrg, buf + (MtdBlockSize - 4), sizeof(ChkSumOrg));
  2052. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev)), &ptr->ModelName, ARRAY_SIZE(ptr->ModelName));
  2053. memcpy(buf + (ARRAY_SIZE(ptr->CsuBootLoadFwRev) + ARRAY_SIZE(ptr->ModelName)
  2054. + ARRAY_SIZE(ptr->AcModelName)), &ptr->SerialNumber, ARRAY_SIZE(ptr->SerialNumber));
  2055. if (ChkSum != ChkSumOrg)
  2056. {
  2057. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\n");
  2058. free(buf);
  2059. system("cd /root;./Module_FactoryConfig -m");
  2060. system("rm -f /Storage/OCPP/OCPPConfiguration");
  2061. system("sync");
  2062. sleep(5);
  2063. system("reboot -f");
  2064. sleep(5);
  2065. system("reboot -f");
  2066. return FAIL;
  2067. }
  2068. }
  2069. }
  2070. //load OK
  2071. memcpy((struct SysConfigData *) ptr, buf, sizeof(struct SysConfigData));
  2072. free(buf);
  2073. system("rm -f /mnt/EvseConfig.bin");
  2074. // SysConfig in flash is empty (0xffffffff)
  2075. if ((strlen((char*) ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  2076. || (strlen((char*) ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  2077. || (strlen((char*) ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  2078. || (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  2079. {
  2080. if (strlen( (char*) ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  2081. {
  2082. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00,
  2083. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  2084. }
  2085. if (strlen((char*) ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  2086. {
  2087. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00,
  2088. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  2089. }
  2090. if (strlen((char*) ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  2091. {
  2092. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  2093. }
  2094. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  2095. {
  2096. DEBUG_INFO("Ethernet dhcp config is null.\n");
  2097. }
  2098. if (strlen((char*) ShmSysConfigAndInfo->SysConfig.ModelName) == 0x00)
  2099. {
  2100. DEBUG_INFO("Model name over length.\n");
  2101. }
  2102. if (strlen((char*) ShmSysConfigAndInfo->SysConfig.SerialNumber) == 0x00)
  2103. {
  2104. DEBUG_INFO("Model serial number over length.\n");
  2105. }
  2106. if (strlen((char*) ShmSysConfigAndInfo->SysConfig.SystemId) == 0x00)
  2107. {
  2108. DEBUG_INFO("SystemId over length.\n");
  2109. }
  2110. system("cd /root;./Module_FactoryConfig -m");
  2111. sleep(3);
  2112. system("/usr/bin/run_evse_restart.sh");
  2113. }
  2114. DEBUG_INFO("Load SysConfigData OK\n");
  2115. ShmCharger->isCcsEnable = OFF;
  2116. DEBUG_INFO("Is CCS Enable: %s \n", ((ShmCharger->isCcsEnable == ON)?"YES":"NO"));
  2117. RatedCurrentParsing((char*) ShmSysConfigAndInfo->SysConfig.ModelName, &modelnameInfo);
  2118. //lwtest
  2119. if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_OUTPUT_TYPE] == 'A')
  2120. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent = modelnameInfo.ratedPower / AC_OUTPUT_VOL;
  2121. else
  2122. ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent = 0;
  2123. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = getAcPhaseCount();
  2124. DEBUG_INFO("Gun quantity: %d\n", modelnameInfo.GetGunCount);
  2125. DEBUG_INFO("Model name rated power: %d\n", modelnameInfo.ratedPower);
  2126. return PASS;
  2127. }
  2128. int StoreUsrConfigData(struct SysConfigData *UsrData)
  2129. {
  2130. int result = PASS;
  2131. int fd, wrd;
  2132. unsigned int i, Chk;
  2133. unsigned char *ptr, *BufTmp;
  2134. Chk = 0;
  2135. ptr = (unsigned char *) UsrData;
  2136. if ((BufTmp = malloc(MtdBlockSize)) != NULL)
  2137. {
  2138. memset(BufTmp, 0, MtdBlockSize);
  2139. memcpy(BufTmp, ptr, sizeof(struct SysConfigData));
  2140. for (i = 0; i < MtdBlockSize - 4; i++)
  2141. Chk += *(BufTmp + i);
  2142. memcpy(BufTmp + MtdBlockSize - 4, &Chk, 4);
  2143. // Output configuration to file.
  2144. fd = open("/mnt/EvseConfig.bin", O_RDWR | O_CREAT);
  2145. if (fd < 0)
  2146. {
  2147. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  2148. free(BufTmp);
  2149. return 0;
  2150. }
  2151. wrd = write(fd, BufTmp, MtdBlockSize);
  2152. close(fd);
  2153. if (wrd < MtdBlockSize)
  2154. {
  2155. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  2156. free(BufTmp);
  2157. return 0;
  2158. }
  2159. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  2160. DEBUG_INFO("Erase /dev/mtd10.\n");
  2161. runShellCmd("flash_erase /dev/mtd10 0 0");
  2162. DEBUG_INFO("Write /dev/mtd10.\n");
  2163. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  2164. DEBUG_INFO("Erase /dev/mtd11.\n");
  2165. runShellCmd("flash_erase /dev/mtd11 0 0");
  2166. DEBUG_INFO("Write /dev/mtd11.\n");
  2167. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  2168. system("rm -f /mnt/EvseConfig.bin");
  2169. DEBUG_INFO("EvseConfig write to flash OK\n");
  2170. }
  2171. else
  2172. {
  2173. DEBUG_ERROR("alloc BlockSize NG\r\n");
  2174. result = FAIL;
  2175. }
  2176. if (BufTmp != NULL)
  2177. free(BufTmp);
  2178. return result;
  2179. }
  2180. void InitEthernet()
  2181. {
  2182. uint8_t cnt_pingDNS_Fail;
  2183. char tmpbuf[256];
  2184. unsigned int natInterface = 0;
  2185. // Detele bridge interface
  2186. system("/sbin/ifconfig uap0 down");
  2187. system("/sbin/ifconfig br0 down");
  2188. system("/usr/sbin/brctl delbr br0");
  2189. //Init Eth0 for internet
  2190. memset(tmpbuf, 0, 256);
  2191. sprintf(tmpbuf, "/sbin/ifconfig eth0 %s netmask %s up &", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2192. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2193. DEBUG_INFO("eth0 config as ip: %s, netmask: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2194. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2195. system(tmpbuf);
  2196. memset(tmpbuf, 0, 256);
  2197. sprintf(tmpbuf, "route add default gw %s eth0 &", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2198. system(tmpbuf);
  2199. system("ifconfig lo up &");
  2200. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  2201. if ((ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_OUTPUT_TYPE] == 'A')
  2202. && (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_TYPE] == 'W'))
  2203. system("/sbin/ethtool -s eth0 speed 10 duplex full autoneg off");
  2204. //Run DHCP client if enabled
  2205. system("killall udhcpc");
  2206. system("rm -rf /etc/resolv.conf");
  2207. system("echo nameserver 8.8.8.8 >> /etc/resolv.conf"); //Google DNS server
  2208. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  2209. if (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0)
  2210. {
  2211. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  2212. ShmSysConfigAndInfo->SysConfig.SystemId);
  2213. system(tmpbuf);
  2214. }
  2215. // Upgrade system id to /etc/hostname
  2216. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  2217. system(tmpbuf);
  2218. // Ethernet MAC address
  2219. getEth0MacAddress();
  2220. // Init for eth1 when model name is DC
  2221. if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_OUTPUT_TYPE] == 'D')
  2222. {
  2223. memset(tmpbuf, 0, 256);
  2224. sprintf(tmpbuf, "/sbin/ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up &");
  2225. DEBUG_INFO("eth1 config as ip: 192.168.0.10, netmask: 255.255.255.0\n");
  2226. system(tmpbuf);
  2227. }
  2228. //check internet status
  2229. if (fork() == 0)
  2230. {
  2231. for (;;)
  2232. {
  2233. if (isRouteFail())
  2234. {
  2235. //DEBUG_ERROR("eth0 not in route, restart eth0.\n");
  2236. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  2237. if ((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0))
  2238. {
  2239. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2240. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  2241. ShmSysConfigAndInfo->SysConfig.SystemId);
  2242. system(tmpbuf);
  2243. }
  2244. else
  2245. {
  2246. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2247. memset(tmpbuf, 0, 256);
  2248. sprintf(tmpbuf, "/sbin/ifconfig eth0 %s netmask %s up &",
  2249. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2250. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2251. system(tmpbuf);
  2252. memset(tmpbuf, 0, 256);
  2253. sprintf(tmpbuf, "route add default gw %s eth0 &",
  2254. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2255. system(tmpbuf);
  2256. }
  2257. }
  2258. if (isReachableInternet() == PASS)
  2259. {
  2260. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = OFF;
  2261. cnt_pingDNS_Fail = 0;
  2262. }
  2263. else
  2264. {
  2265. if (cnt_pingDNS_Fail >= 3)
  2266. {
  2267. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  2268. if (!ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  2269. {
  2270. if ((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0))
  2271. {
  2272. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2273. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  2274. ShmSysConfigAndInfo->SysConfig.SystemId);
  2275. system(tmpbuf);
  2276. }
  2277. else
  2278. {
  2279. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  2280. memset(tmpbuf, 0, 256);
  2281. sprintf(tmpbuf, "/sbin/ifconfig eth0 %s netmask %s up &",
  2282. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  2283. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  2284. system(tmpbuf);
  2285. memset(tmpbuf, 0, 256);
  2286. sprintf(tmpbuf, "route add default gw %s eth0 &",
  2287. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  2288. system(tmpbuf);
  2289. }
  2290. cnt_pingDNS_Fail = 0;
  2291. }
  2292. }
  2293. else
  2294. {
  2295. cnt_pingDNS_Fail++;
  2296. }
  2297. }
  2298. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet
  2299. && ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0)
  2300. || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  2301. && ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0)
  2302. || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  2303. && (ShmOCPP16Data->OcppConnStatus != PASS))
  2304. {
  2305. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  2306. }
  2307. else
  2308. {
  2309. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  2310. }
  2311. //============================================================
  2312. // Priority for internet 0 : First / 1 : Second / 2: Third
  2313. //============================================================
  2314. if (!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  2315. {
  2316. system("/sbin/ifmetric eth0 0");
  2317. if ((ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'W')
  2318. || (ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'D'))
  2319. {
  2320. system("/sbin/ifmetric mlan0 1");
  2321. }
  2322. if ((ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'T')
  2323. || (ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'D'))
  2324. {
  2325. system("/sbin/ifmetric ppp0 2");
  2326. }
  2327. if (isKernelSupportNAT() == YES)
  2328. {
  2329. if (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2330. {
  2331. if (natInterface != 1)
  2332. {
  2333. system("/sbin/iptables -t nat -F");
  2334. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o eth0 -j MASQUERADE");
  2335. natInterface = 1;
  2336. }
  2337. }
  2338. else
  2339. {
  2340. system("/sbin/iptables -t nat -F");
  2341. }
  2342. }
  2343. }
  2344. else if (!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  2345. {
  2346. if ((ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'W')
  2347. || (ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'D'))
  2348. {
  2349. system("/sbin/ifmetric eth0 1");
  2350. system("/sbin/ifmetric mlan0 0");
  2351. }
  2352. if ((ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'T')
  2353. || (ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'D'))
  2354. {
  2355. system("/sbin/ifmetric ppp0 2");
  2356. }
  2357. }
  2358. else if (!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  2359. {
  2360. if ((ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'W')
  2361. || (ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'D'))
  2362. {
  2363. system("/sbin/ifmetric mlan0 2");
  2364. }
  2365. if ((ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'T')
  2366. || (ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'D'))
  2367. {
  2368. system("/sbin/ifmetric eth0 1");
  2369. system("/sbin/ifmetric ppp0 0");
  2370. }
  2371. if (isKernelSupportNAT() == YES)
  2372. {
  2373. if (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2374. {
  2375. if (natInterface != 2)
  2376. {
  2377. system("/sbin/iptables -t nat -F");
  2378. system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o ppp0 -j MASQUERADE");
  2379. natInterface = 2;
  2380. }
  2381. }
  2382. else
  2383. {
  2384. system("/sbin/iptables -t nat -F");
  2385. }
  2386. }
  2387. }
  2388. else
  2389. {
  2390. if (isKernelSupportNAT() == YES)
  2391. {
  2392. if (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
  2393. {
  2394. system("/sbin/iptables -t nat -F");
  2395. natInterface = 0;
  2396. }
  2397. }
  2398. }
  2399. // Bridge ethernet to uap0 to get dynamic ip address
  2400. if (ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing
  2401. && (ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2) && (isUap0up() == PASS)
  2402. && (access("/sys/class/net/br0/address", F_OK) == -1))
  2403. {
  2404. sleep(10);
  2405. system("/usr/sbin/brctl addbr br0");
  2406. system("/usr/sbin/brctl addif br0 eth0");
  2407. system("/usr/sbin/brctl addif br0 uap0");
  2408. system("/sbin/ifconfig br0 192.168.10.200 up");
  2409. system("/bin/sed -i '/interface/d' /etc/udhcpd.conf");
  2410. system("/bin/echo 'interface br0' >> /etc/udhcpd.conf");
  2411. system("kill udhcpd");
  2412. system("/usr/sbin/udhcpd /etc/udhcpd.conf");
  2413. DEBUG_INFO("Bridge uap0 & eth0 for local power sharing by ethernet.\n");
  2414. }
  2415. sleep(5);
  2416. }
  2417. }
  2418. DEBUG_INFO("Initial Ethernet OK\n");
  2419. }
  2420. int SpawnTask(uint8_t gun_index)
  2421. {
  2422. if (gun_index == 0)
  2423. {
  2424. system("pkill Module_");
  2425. system("pkill OcppBackend");
  2426. system("/root/Module_EventLogging &");
  2427. system("/root/Module_Speaker &");
  2428. system("/root/Module_ProduceUtils &");
  2429. system("/root/Module_LcmControl &");
  2430. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != 0)
  2431. {
  2432. ocpp_process_start();
  2433. }
  2434. else
  2435. {
  2436. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  2437. ocpp_process_start();
  2438. }
  2439. }
  2440. return PASS;
  2441. }
  2442. int InitQca7000()
  2443. {
  2444. int result = PASS;
  2445. system("/sbin/rmmod qcaspi");
  2446. if (isKernelSupportNAT() == YES)
  2447. system("/sbin/insmod /lib/qcaspi_nat.ko");
  2448. else
  2449. system("/sbin/insmod /lib/qcaspi.ko");
  2450. sleep(2);
  2451. /*+++ vern, for CCS +++*/
  2452. //system("/sbin/ifconfig eth1 192.168.253.11 netmask 255.255.255.0 up");
  2453. system("/sbin/ifconfig eth1 192.168.0.11 netmask 255.255.255.0 up");
  2454. /*--- vern, for CCS ---*/
  2455. sleep(1);
  2456. return result;
  2457. }
  2458. int Initialization(uint8_t gun_index)
  2459. {
  2460. int result = PASS;
  2461. if (gun_index == 0)
  2462. {
  2463. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2464. InitGPIO();
  2465. InitEthernet();
  2466. // Only AX or DC model enable QCA7000
  2467. if ((ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_OUTPUT_TYPE] == 'A')
  2468. && (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_TYPE] == 'X'))
  2469. {
  2470. if (InitQca7000() != PASS)
  2471. {
  2472. DEBUG_ERROR("QCA7000 initial fail.\n");
  2473. result = FAIL;
  2474. }
  2475. }
  2476. if (DB_Open(localDb) != PASS)
  2477. {
  2478. DEBUG_ERROR("Local database initial fail.\n");
  2479. result = FAIL;
  2480. }
  2481. if ((rfidFd = InitRfidPort()) == FAIL)
  2482. {
  2483. DEBUG_ERROR("RFID port initial fail.\n");
  2484. result = FAIL;
  2485. }
  2486. if ((wtdFd = InitWatchDog()) == FAIL)
  2487. {
  2488. DEBUG_ERROR("Watchdog initial fail.\n");
  2489. result = FAIL;
  2490. }
  2491. if (result == PASS)
  2492. DEBUG_INFO("Initialization OK.\n");
  2493. else
  2494. DEBUG_INFO("Initialization Fail.\n");
  2495. }
  2496. return result;
  2497. }
  2498. //=====================================================
  2499. // Common routine
  2500. //=====================================================
  2501. char* getSystemModeName(unsigned char mode)
  2502. {
  2503. char* result;
  2504. switch (mode)
  2505. {
  2506. case SYS_MODE_BOOTING:
  2507. result = "booting";
  2508. break;
  2509. case SYS_MODE_IDLE:
  2510. result = "idle";
  2511. break;
  2512. case SYS_MODE_AUTHORIZING:
  2513. result = "authorizing";
  2514. break;
  2515. case SYS_MODE_PREPARING:
  2516. result = "preparing";
  2517. break;
  2518. case SYS_MODE_CHARGING:
  2519. result = "charging";
  2520. break;
  2521. case SYS_MODE_TERMINATING:
  2522. result = "terminating";
  2523. break;
  2524. case SYS_MODE_COMPLETE:
  2525. result = "complete";
  2526. break;
  2527. case SYS_MODE_ALARM:
  2528. result = "alarm";
  2529. break;
  2530. case SYS_MODE_FAULT:
  2531. result = "fault";
  2532. break;
  2533. case SYS_MODE_MAINTAIN:
  2534. result = "maintain";
  2535. break;
  2536. case SYS_MODE_RESERVATION:
  2537. result = "reservation";
  2538. break;
  2539. case SYS_MODE_BOOKING:
  2540. result = "booking";
  2541. break;
  2542. case SYS_MODE_DEBUG:
  2543. result = "debug";
  2544. break;
  2545. case SYS_MODE_UPDATE:
  2546. result = "upgrade";
  2547. break;
  2548. default:
  2549. result = "unknown";
  2550. break;
  2551. }
  2552. return result;
  2553. }
  2554. void setChargerMode(unsigned char gun_index, unsigned char mode)
  2555. {
  2556. getTargetChargingInfoData(gun_index)->PreviousSystemStatus = getTargetChargingInfoData(gun_index)->SystemStatus;
  2557. getTargetChargingInfoData(gun_index)->SystemStatus = mode;
  2558. DEBUG_INFO("Gun-%02d mode switch from %s to %s\n", gun_index,
  2559. getSystemModeName(getTargetChargingInfoData(gun_index)->PreviousSystemStatus),
  2560. getSystemModeName(getTargetChargingInfoData(gun_index)->SystemStatus));
  2561. }
  2562. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  2563. {
  2564. return ((getTargetChargingInfoData(gun_index)->SystemStatus == mode) ? YES : NO);
  2565. }
  2566. unsigned char isModeChange(unsigned char gun_index)
  2567. {
  2568. unsigned char result = NO;
  2569. if (!isMode(gun_index, getTargetChargingInfoData(gun_index)->PreviousSystemStatus))
  2570. {
  2571. result = YES;
  2572. getTargetChargingInfoData(gun_index)->PreviousSystemStatus = getTargetChargingInfoData(gun_index)->SystemStatus;
  2573. }
  2574. return result;
  2575. }
  2576. void gpio_set_value(unsigned int gpio, unsigned int value)
  2577. {
  2578. int fd;
  2579. char buf[256];
  2580. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2581. fd = open(buf, O_WRONLY);
  2582. if (fd < 0)
  2583. {
  2584. DEBUG_ERROR("GPIO-%d set %d fail.\n", gpio, value);
  2585. return;
  2586. }
  2587. if (value)
  2588. write(fd, "1", 2);
  2589. else
  2590. write(fd, "0", 2);
  2591. close(fd);
  2592. }
  2593. int gpio_get_value(unsigned int gpio)
  2594. {
  2595. int fd;
  2596. char buf[256];
  2597. char ch;
  2598. int8_t result = FAIL;
  2599. sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
  2600. fd = open(buf, O_RDONLY);
  2601. if (fd < 0)
  2602. {
  2603. DEBUG_ERROR("GPIO-%d get fail\n", gpio);
  2604. return result;
  2605. }
  2606. read(fd, &ch, 1);
  2607. if (ch != '0')
  2608. result = 1;
  2609. else
  2610. result = 0;
  2611. close(fd);
  2612. return result;
  2613. }
  2614. int presentChargedEnergyClear(unsigned char gun_index)
  2615. {
  2616. int result = FAIL;
  2617. getTargetChargingInfoData(gun_index)->PresentChargedEnergy = 0;
  2618. memset(getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod, 0x00,
  2619. ARRAY_SIZE(getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod) * sizeof(float));
  2620. result = PASS;
  2621. return result;
  2622. }
  2623. float presentChargedEnergyTotal(unsigned char gun_index)
  2624. {
  2625. float result = 0.0f;
  2626. for (int idx = 0; idx < ARRAY_SIZE(getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod); idx++)
  2627. {
  2628. //result += getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod[idx];
  2629. result += (((int) (getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod[idx] * 10000)) / (float) 10000);
  2630. }
  2631. return result;
  2632. }
  2633. int presentChargedEnergyUpdate(unsigned char gun_index)
  2634. {
  2635. int result = FAIL;
  2636. time_t CurrentTime;
  2637. struct tm *tm;
  2638. CurrentTime = time(NULL);
  2639. tm = localtime(&CurrentTime);
  2640. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  2641. {
  2642. // Resolution: 0.0001 kwh
  2643. getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod[tm->tm_hour] += (((float) (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption
  2644. - ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start)) / 10000.0)
  2645. - presentChargedEnergyTotal(gun_index);
  2646. }
  2647. else
  2648. {
  2649. // Resolution: 0.0001 kwh
  2650. getTargetChargingInfoData(gun_index)->presentChargedEnergyPeriod[tm->tm_hour] += ((((float) (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption
  2651. - ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start)) / 10000.0)
  2652. + (((float) (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption
  2653. - ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start)) / 10000.0)
  2654. + (((float) (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption
  2655. - ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start)) / 10000.0))
  2656. - presentChargedEnergyTotal(gun_index);
  2657. }
  2658. getTargetChargingInfoData(gun_index)->PresentChargedEnergy = presentChargedEnergyTotal(gun_index);
  2659. return result;
  2660. }
  2661. //===============================================
  2662. // Get firmware version
  2663. //===============================================
  2664. void get_firmware_version(unsigned char gun_index)
  2665. {
  2666. FILE *fp;
  2667. char cmd[512];
  2668. char buf[512];
  2669. // Get CSU hardware version
  2670. sprintf((char*) ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  2671. // Get CSU boot loader version
  2672. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev,
  2673. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  2674. // Get CSU kernel version
  2675. sprintf(cmd, "/bin/uname -r");
  2676. fp = popen(cmd, "r");
  2677. if (fp == NULL)
  2678. sprintf((char*) ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  2679. else
  2680. {
  2681. while (fgets(buf, sizeof(buf), fp) != NULL)
  2682. {
  2683. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf) - 1);
  2684. }
  2685. }
  2686. // Get MCU firmware version
  2687. //lwtest
  2688. if (modelnameInfo.GetGunCount > 1)
  2689. {
  2690. strcpy((char*) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[0].ver.Version_FW);
  2691. strcpy((char*) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[1].ver.Version_FW);
  2692. }
  2693. else
  2694. strcpy((char*) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[0].ver.Version_FW);
  2695. // Get CSU root file system version
  2696. sprintf((char*) ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "E0.02.00.0000.00");
  2697. // Get AC connector type from model name
  2698. for (uint8_t idx = 0; idx < 3; idx++)
  2699. {
  2700. switch (ShmSysConfigAndInfo->SysConfig.ModelName[CONNECTOR_TYPE1 + idx])
  2701. {
  2702. case '1':
  2703. // J1772 Plug
  2704. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  2705. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  2706. break;
  2707. case '2':
  2708. // J1772 Socket
  2709. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  2710. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  2711. break;
  2712. case '3':
  2713. // CE Plug
  2714. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  2715. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  2716. break;
  2717. case '4':
  2718. // CE Socket
  2719. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  2720. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  2721. break;
  2722. case '5':
  2723. // GB Plug
  2724. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  2725. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  2726. break;
  2727. case '6':
  2728. // GB Socket
  2729. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  2730. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  2731. break;
  2732. }
  2733. }
  2734. // Get network option from model name
  2735. switch (ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE])
  2736. {
  2737. case 'B':
  2738. case 'U':
  2739. //Blue tooth
  2740. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  2741. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  2742. break;
  2743. case 'W':
  2744. // WIFI
  2745. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  2746. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  2747. break;
  2748. case 'T':
  2749. // 3G/4G
  2750. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  2751. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  2752. break;
  2753. case 'D':
  2754. // WIFI + 4G
  2755. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  2756. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
  2757. break;
  2758. default:
  2759. // LAN
  2760. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  2761. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  2762. break;
  2763. }
  2764. // Get rating power from model name
  2765. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10],
  2766. &ShmSysConfigAndInfo->SysConfig.ModelName[RATING_POWER], 0x03);
  2767. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10],
  2768. &ShmSysConfigAndInfo->SysConfig.ModelName[RATING_POWER], 0x03);
  2769. // Get vender code from model name
  2770. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14],
  2771. &ShmSysConfigAndInfo->SysConfig.ModelName[VENDOR_CODE], 0x02);
  2772. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14],
  2773. &ShmSysConfigAndInfo->SysConfig.ModelName[VENDOR_CODE], 0x02);
  2774. //lwtest
  2775. if ((modelnameInfo.GetGunCount > 1 && gun_index == 0) || modelnameInfo.GetGunCount < 2)
  2776. {
  2777. DEBUG_INFO("========================================\n");
  2778. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  2779. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  2780. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  2781. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  2782. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  2783. DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[0].ver.Version_FW);
  2784. DEBUG_INFO("========================================\n");
  2785. }
  2786. }
  2787. //===============================================
  2788. // Upgrade firmware
  2789. //===============================================
  2790. int upgrade_check()
  2791. {
  2792. int result = PASS;
  2793. int fd;
  2794. DIR *dir;
  2795. struct dirent *file;
  2796. char cmd[512];
  2797. long int MaxLen = 48 * 1024 * 1024, ImageLen = 0, wrd;
  2798. if ((dir = opendir("/mnt")) != NULL)
  2799. {
  2800. /* print all the files and directories within directory */
  2801. while ((file = readdir(dir)) != NULL)
  2802. {
  2803. if ((strlen(file->d_name) > 2))
  2804. {
  2805. // Wait for MCU upgrade finish.
  2806. while (ShmCharger->gun_info[0].mcuFlag.isMcuUpgradeReq || (modelnameInfo.GetGunCount > 1
  2807. ? ShmCharger->gun_info[1].mcuFlag.isMcuUpgradeReq : FALSE))
  2808. sleep(1);
  2809. // Wait for LCM upgrade finish.
  2810. while (ShmCharger->isUpgradeLcmReq)
  2811. sleep(1);
  2812. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  2813. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  2814. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  2815. if ((fd = open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  2816. {
  2817. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  2818. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  2819. ImageLen = read(fd, ptr, MaxLen);
  2820. close(fd);
  2821. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13] << 0) | (ptr[0x12] << 8) | (ptr[0x11] << 16) | (ptr[0x10] << 24));
  2822. substr(ShmCharger->fwUpgradeInfo.modelName, (char *) ptr, 0, 0x10);
  2823. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  2824. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName,
  2825. ShmSysConfigAndInfo->SysConfig.ModelName);
  2826. if ((ShmCharger->fwUpgradeInfo.modelName[0] == ShmSysConfigAndInfo->SysConfig.ModelName[0])
  2827. && (ShmCharger->fwUpgradeInfo.modelName[1] == ShmSysConfigAndInfo->SysConfig.ModelName[1])
  2828. && (ShmCharger->fwUpgradeInfo.modelName[7] == ShmSysConfigAndInfo->SysConfig.ModelName[7])
  2829. && (ShmCharger->fwUpgradeInfo.modelName[8] == ShmSysConfigAndInfo->SysConfig.ModelName[8])
  2830. && (ShmCharger->fwUpgradeInfo.modelName[9] == ShmSysConfigAndInfo->SysConfig.ModelName[9])
  2831. && (ShmCharger->fwUpgradeInfo.modelName[11] == ShmSysConfigAndInfo->SysConfig.ModelName[11])
  2832. && (ShmCharger->fwUpgradeInfo.modelName[12] == ShmSysConfigAndInfo->SysConfig.ModelName[12])
  2833. && (ShmCharger->fwUpgradeInfo.modelName[13] == ShmSysConfigAndInfo->SysConfig.ModelName[13])
  2834. && (ShmCharger->fwUpgradeInfo.fwType > 0))
  2835. {
  2836. switch (ShmCharger->fwUpgradeInfo.fwType)
  2837. {
  2838. //case CSU_MLO:
  2839. case CSU_BOOTLOADER:
  2840. case CSU_KERNEL_CONFIGURATION:
  2841. case CSU_KERNEL_IMAGE:
  2842. case CSU_ROOT_FILE_SYSTEM:
  2843. case CSU_USER_CONFIGURATION:
  2844. case CSU_PRIMARY_CONTROLLER:
  2845. if (Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location,
  2846. ShmCharger->fwUpgradeInfo.modelName) != PASS)
  2847. {
  2848. result = FAIL;
  2849. }
  2850. else
  2851. {
  2852. if (ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  2853. {
  2854. DEBUG_INFO("Restore model name & serial number.\n");
  2855. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2856. if (LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2857. {
  2858. DEBUG_INFO("Re-load configuration fail.\n");
  2859. result = FAIL;
  2860. }
  2861. else
  2862. {
  2863. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName,
  2864. ARRAY_SIZE(SysConfigOrg.ModelName));
  2865. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber,
  2866. &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  2867. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId,
  2868. ARRAY_SIZE(SysConfigOrg.SystemId));
  2869. if (StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  2870. {
  2871. DEBUG_INFO("Re-write configuration fail.\n");
  2872. result = FAIL;
  2873. }
  2874. else
  2875. DEBUG_INFO("Re-write configuration OK.\n");
  2876. }
  2877. }
  2878. }
  2879. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2880. system(cmd);
  2881. break;
  2882. case AC_WALLMOUNT_CONTROLLER:
  2883. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  2884. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  2885. sleep(10);
  2886. break;
  2887. case LCM:
  2888. fd = open("/mnt/lcm.zip", O_RDWR | O_CREAT | O_EXCL);
  2889. if (fd < 0)
  2890. {
  2891. DEBUG_ERROR("Can not create lcm.zip file.\n");
  2892. result = FAIL;
  2893. }
  2894. else
  2895. {
  2896. // Write image to flash
  2897. DEBUG_INFO("Writing data to lcm.zip file...\n");
  2898. wrd = write(fd, ptr + 48, ImageLen - 48);
  2899. close(fd);
  2900. DEBUG_INFO(">> lcm.zip Written length: 0x%x\n", wrd);
  2901. if (wrd != (ImageLen - 48))
  2902. {
  2903. result = FAIL;
  2904. }
  2905. else
  2906. {
  2907. runShellCmd("mkdir -p /mnt/lcd");
  2908. runShellCmd("unzip /mnt/lcm.zip -d /mnt/lcd");
  2909. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2910. system(cmd);
  2911. system("rm -f /mnt/lcm.zip");
  2912. result = PASS;
  2913. }
  2914. }
  2915. ShmCharger->isUpgradeLcmReq = ON;
  2916. sleep(10);
  2917. break;
  2918. default:
  2919. result = FAIL;
  2920. DEBUG_WARN("Image file is unknown type.\n");
  2921. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2922. system(cmd);
  2923. break;
  2924. }
  2925. }
  2926. else
  2927. {
  2928. result = FAIL;
  2929. DEBUG_ERROR("Model name and Firmware type error.\n");
  2930. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2931. system(cmd);
  2932. }
  2933. free(ptr);
  2934. }
  2935. else
  2936. {
  2937. result = FAIL;
  2938. DEBUG_ERROR("New firmware open error.\n");
  2939. }
  2940. }
  2941. else
  2942. {
  2943. if (strlen(file->d_name) >= 3)
  2944. {
  2945. result = FAIL;
  2946. DEBUG_ERROR("File name error.\n");
  2947. }
  2948. else
  2949. {
  2950. DEBUG_ERROR("Searching file.\n");
  2951. }
  2952. }
  2953. }
  2954. closedir(dir);
  2955. }
  2956. else
  2957. {
  2958. result = FAIL;
  2959. DEBUG_ERROR("/mnt does not valid.\n");
  2960. }
  2961. return result;
  2962. }
  2963. //===============================================
  2964. // Check RFID is match with start user
  2965. //===============================================
  2966. bool isMatchStartUser(void)
  2967. {
  2968. uint8_t tmpUser[32];
  2969. uint8_t isPrintLogOut = OFF;
  2970. bool _matchFlag = FALSE;
  2971. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2972. {
  2973. // Big endian
  2974. switch (rfid.snType)
  2975. {
  2976. case RFID_SN_TYPE_6BYTE:
  2977. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1],
  2978. rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  2979. break;
  2980. case RFID_SN_TYPE_7BYTE:
  2981. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1],
  2982. rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5],
  2983. rfid.currentCard[6]);
  2984. break;
  2985. case RFID_SN_TYPE_8BYTE:
  2986. sprintf((char *) tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1],
  2987. rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5],
  2988. rfid.currentCard[6], rfid.currentCard[7]);
  2989. break;
  2990. case RFID_SN_TYPE_10BYTE:
  2991. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0],
  2992. rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4],
  2993. rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8],
  2994. rfid.currentCard[9]);
  2995. break;
  2996. case RFID_SN_TYPE_4BYTE:
  2997. default:
  2998. sprintf((char*) tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1],
  2999. rfid.currentCard[2], rfid.currentCard[3]);
  3000. break;
  3001. }
  3002. }
  3003. else
  3004. {
  3005. // Little endian
  3006. switch (rfid.snType)
  3007. {
  3008. case RFID_SN_TYPE_6BYTE:
  3009. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4],
  3010. rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3011. break;
  3012. case RFID_SN_TYPE_7BYTE:
  3013. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5],
  3014. rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1],
  3015. rfid.currentCard[0]);
  3016. break;
  3017. case RFID_SN_TYPE_8BYTE:
  3018. sprintf((char *) tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X",
  3019. rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4],
  3020. rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3021. break;
  3022. case RFID_SN_TYPE_10BYTE:
  3023. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9],
  3024. rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5],
  3025. rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1],
  3026. rfid.currentCard[0]);
  3027. break;
  3028. case RFID_SN_TYPE_4BYTE:
  3029. default:
  3030. sprintf((char*) tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2],
  3031. rfid.currentCard[1], rfid.currentCard[0]);
  3032. break;
  3033. }
  3034. }
  3035. //lwtest
  3036. for (uint8_t index = 0; index < modelnameInfo.GetGunCount; index++)
  3037. {
  3038. DEBUG_INFO("Gun %d start SN compare [%s] \n", index, getTargetChargingInfoData(index)->StartUserId);
  3039. if (strcmp((char*) tmpUser, (char*) getTargetChargingInfoData(index)->StartUserId) == 0)
  3040. {
  3041. _matchFlag = YES;
  3042. ShmCharger->gun_selectd = index;
  3043. break;
  3044. }
  3045. }
  3046. if (isPrintLogOut == OFF)
  3047. {
  3048. if (_matchFlag == YES) //lwtest
  3049. {
  3050. DEBUG_INFO("==== isMatchStartUser(gun %d) ==== \n", ShmCharger->gun_selectd);
  3051. DEBUG_INFO("tmpUser : %s \n", tmpUser);
  3052. DEBUG_INFO("StartUserId : %s \n", getTargetChargingInfoData(ShmCharger->gun_selectd)->StartUserId);
  3053. DEBUG_INFO("========================== \n");
  3054. }
  3055. else
  3056. DEBUG_INFO("==== Start Card SN none match ==== \n");
  3057. isPrintLogOut = ON;
  3058. }
  3059. return _matchFlag;
  3060. }
  3061. bool isMatchPresentUser()
  3062. {
  3063. uint8_t tmpUser[32];
  3064. uint8_t isPrintLogOut = OFF;
  3065. bool _matchFlag = FALSE;
  3066. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3067. {
  3068. // Big endian
  3069. switch (rfid.snType)
  3070. {
  3071. case RFID_SN_TYPE_6BYTE:
  3072. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1],
  3073. rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  3074. break;
  3075. case RFID_SN_TYPE_7BYTE:
  3076. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1],
  3077. rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5],
  3078. rfid.currentCard[6]);
  3079. break;
  3080. case RFID_SN_TYPE_8BYTE:
  3081. sprintf((char *) tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1],
  3082. rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5],
  3083. rfid.currentCard[6], rfid.currentCard[7]);
  3084. break;
  3085. case RFID_SN_TYPE_10BYTE:
  3086. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0],
  3087. rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4],
  3088. rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8],
  3089. rfid.currentCard[9]);
  3090. break;
  3091. case RFID_SN_TYPE_4BYTE:
  3092. default:
  3093. sprintf((char*) tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1],
  3094. rfid.currentCard[2], rfid.currentCard[3]);
  3095. break;
  3096. }
  3097. }
  3098. else
  3099. {
  3100. // Little endian
  3101. switch (rfid.snType)
  3102. {
  3103. case RFID_SN_TYPE_6BYTE:
  3104. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4],
  3105. rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3106. break;
  3107. case RFID_SN_TYPE_7BYTE:
  3108. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5],
  3109. rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1],
  3110. rfid.currentCard[0]);
  3111. break;
  3112. case RFID_SN_TYPE_8BYTE:
  3113. sprintf((char *) tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X",
  3114. rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4],
  3115. rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3116. break;
  3117. case RFID_SN_TYPE_10BYTE:
  3118. sprintf((char*) tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9],
  3119. rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5],
  3120. rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1],
  3121. rfid.currentCard[0]);
  3122. break;
  3123. case RFID_SN_TYPE_4BYTE:
  3124. default:
  3125. sprintf((char*) tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2],
  3126. rfid.currentCard[1], rfid.currentCard[0]);
  3127. break;
  3128. }
  3129. }
  3130. //lwtest
  3131. //compare with SN card if already authoraized but not entry the charging session.
  3132. for (uint8_t index = 0; index < modelnameInfo.GetGunCount; index++)
  3133. {
  3134. DEBUG_INFO("Gun %d present SN compare [%s] \n", index, ShmCharger->gun_info[index].AuthAcceptUserId);
  3135. if (strcmp((char*) tmpUser, (char*) ShmCharger->gun_info[index].AuthAcceptUserId) == 0)
  3136. {
  3137. _matchFlag = TRUE;
  3138. ShmCharger->gun_selectd = index;
  3139. break;
  3140. }
  3141. }
  3142. if (isPrintLogOut == OFF)
  3143. {
  3144. if (_matchFlag == YES)
  3145. {
  3146. DEBUG_INFO("==== isMatchPresentUser (%d)==== \n", ShmCharger->gun_selectd);
  3147. DEBUG_INFO("tmpUser : %s \n", tmpUser);
  3148. DEBUG_INFO("UserId : %s \n", ShmCharger->gun_info[ShmCharger->gun_selectd].AuthAcceptUserId);
  3149. DEBUG_INFO("============================ \n");
  3150. isPrintLogOut = ON;
  3151. }
  3152. else
  3153. DEBUG_INFO("==== Present Card SN none match ==== \n");
  3154. }
  3155. return _matchFlag;
  3156. }
  3157. //===============================================
  3158. // Check RFID is need to authorize .. lwtest
  3159. //===============================================
  3160. bool isNeedToAuth(void)
  3161. {
  3162. bool result = FALSE;
  3163. for (uint8_t index = 0; index < modelnameInfo.GetGunCount; index++)
  3164. {
  3165. if (getTargetChargingInfoData(index)->SystemStatus == SYS_MODE_IDLE)
  3166. {
  3167. result = TRUE;
  3168. DEBUG_INFO("Need to Authorize (gun %d)\n", index);
  3169. break;
  3170. }
  3171. }
  3172. return result;
  3173. }
  3174. //===============================================
  3175. // Read RFID Serial Number
  3176. //===============================================
  3177. int GetCardSerialNumber()
  3178. {
  3179. int isSuccess = FAIL;
  3180. int module_type = MODULE_EWT;
  3181. if (getRequestCardSN(rfidFd, module_type, &rfid))
  3182. {
  3183. if (rfid.cardType == ISO14443A)
  3184. {
  3185. if (rfid.snType == RFID_SN_TYPE_4BYTE)
  3186. {
  3187. isSuccess = PASS;
  3188. }
  3189. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  3190. {
  3191. isSuccess = PASS;
  3192. }
  3193. sethaltCard(rfidFd, module_type);
  3194. }
  3195. else if (rfid.cardType == IS014443B)
  3196. {
  3197. isSuccess = PASS;
  3198. }
  3199. else if (rfid.cardType == FELICA)
  3200. {
  3201. isSuccess = PASS;
  3202. }
  3203. else
  3204. {
  3205. }
  3206. }
  3207. else
  3208. {
  3209. }
  3210. return isSuccess;
  3211. }
  3212. //===============================================
  3213. // Set led motion
  3214. //===============================================
  3215. void setLedMotion(unsigned char gun_index, unsigned char led_mode)
  3216. {
  3217. //lwtest
  3218. if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_OUTPUT_TYPE] != 'A')
  3219. return;
  3220. switch (led_mode)
  3221. {
  3222. case LED_ACTION_INIT:
  3223. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  3224. break;
  3225. case LED_ACTION_IDLE:
  3226. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  3227. break;
  3228. case LED_ACTION_AUTHED:
  3229. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  3230. break;
  3231. case LED_ACTION_CONNECTED:
  3232. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  3233. break;
  3234. case LED_ACTION_CHARGING:
  3235. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  3236. break;
  3237. case LED_ACTION_STOP:
  3238. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  3239. break;
  3240. case LED_ACTION_ALARM:
  3241. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  3242. break;
  3243. case LED_ACTION_MAINTAIN:
  3244. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  3245. break;
  3246. case LED_ACTION_RFID_PASS:
  3247. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  3248. break;
  3249. case LED_ACTION_RFID_FAIL:
  3250. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  3251. break;
  3252. case LED_ACTION_BLE_CONNECT:
  3253. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  3254. break;
  3255. case LED_ACTION_BLE_DISABLE:
  3256. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  3257. break;
  3258. case LED_ACTION_DEBUG:
  3259. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  3260. break;
  3261. case LED_ACTION_ALL_OFF:
  3262. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  3263. break;
  3264. case LED_RELAY_ON:
  3265. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  3266. break;
  3267. case LED_RELAY_OFF:
  3268. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  3269. break;
  3270. case LED_ACTION_HANDSHAKE_FAIL:
  3271. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  3272. break;
  3273. case LED_ACTION_INTERNET_DISCONNECT:
  3274. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  3275. break;
  3276. case LED_ACTION_RESTORE_SETTING:
  3277. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RESTORE_SETTING;
  3278. break;
  3279. case LED_ACTION_IDLE_BACKEND_CONNECTED:
  3280. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_CONNECTED;
  3281. break;
  3282. case LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP:
  3283. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_CONNECTED_SLEEP;
  3284. break;
  3285. case LED_ACTION_IDLE_BACKEND_DISCONNECTED:
  3286. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_DISCONNECTED;
  3287. break;
  3288. case LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP:
  3289. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE_BACKEND_DISCONNECTED_SLEEP;
  3290. break;
  3291. case LED_ACTION_RESERVATION_MODE:
  3292. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RESERVATION_MODE;
  3293. break;
  3294. }
  3295. }
  3296. //===============================================
  3297. // Request on/off set
  3298. //===============================================
  3299. void setRequest(unsigned char gun_index, unsigned char isOn)
  3300. {
  3301. if (isOn == ON)
  3302. {
  3303. if (ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  3304. {
  3305. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  3306. DEBUG_INFO("Gun-%d permission request: ON \n", gun_index);
  3307. }
  3308. }
  3309. else
  3310. {
  3311. if (ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  3312. {
  3313. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  3314. DEBUG_INFO("Gun-%d permission request: OFF \n", gun_index);
  3315. }
  3316. }
  3317. }
  3318. //===============================================
  3319. // Request on/off get
  3320. //===============================================
  3321. int getRequest(unsigned char gun_index)
  3322. {
  3323. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  3324. }
  3325. //===============================================
  3326. // Relay on/off set
  3327. //===============================================
  3328. void setRelay(unsigned char gun_index, unsigned char isOn)
  3329. {
  3330. if (isOn == ON)
  3331. {
  3332. if (ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  3333. {
  3334. switch (ShmCharger->gun_info[gun_index].chargingMode)
  3335. {
  3336. case CHARGING_MODE_BS:
  3337. case CHARGING_MODE_HLC:
  3338. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3339. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  3340. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  3341. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  3342. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3343. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_BS] \n", gun_index);
  3344. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3345. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_HLC] \n", gun_index);
  3346. break;
  3347. case CHARGING_MODE_SOCKETE:
  3348. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3349. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] = 0x01;
  3350. DEBUG_INFO("Gun-%d Output relay status: ON. [CHARGING_MODE_SOCKETE] \n", gun_index);
  3351. break;
  3352. default:
  3353. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  3354. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  3355. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  3356. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  3357. DEBUG_INFO("Gun-%d Output relay status: ON. [DEFFAULT] \n", gun_index);
  3358. break;
  3359. }
  3360. }
  3361. }
  3362. else
  3363. {
  3364. if (ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  3365. {
  3366. switch (ShmCharger->gun_info[gun_index].chargingMode)
  3367. {
  3368. case CHARGING_MODE_BS:
  3369. case CHARGING_MODE_HLC:
  3370. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3371. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  3372. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  3373. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  3374. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  3375. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_BS] \n", gun_index);
  3376. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  3377. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_HLC] \n", gun_index);
  3378. break;
  3379. case CHARGING_MODE_SOCKETE:
  3380. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3381. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] = 0;
  3382. DEBUG_INFO("Gun-%d Output relay status: OFF. [CHARGING_MODE_SOCKETE] \n", gun_index);
  3383. break;
  3384. default:
  3385. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  3386. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  3387. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  3388. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  3389. DEBUG_INFO("Gun-%d Output relay status: OFF. [DEFFAULT] \n", gun_index);
  3390. break;
  3391. }
  3392. }
  3393. }
  3394. }
  3395. //===============================================
  3396. // Relay on/off get
  3397. //===============================================
  3398. int getRelay(unsigned char gun_index)
  3399. {
  3400. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  3401. }
  3402. //===============================================
  3403. // Set speaker on/off request
  3404. //===============================================
  3405. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  3406. {
  3407. //lwtest
  3408. if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_OUTPUT_TYPE] != 'A')
  3409. return;
  3410. if (isOn == ON)
  3411. {
  3412. ShmCharger->isSpeakerOn = ON;
  3413. ShmCharger->speaker_type = speaker_mode;
  3414. }
  3415. }
  3416. //===============================================
  3417. // Initialization RFID communication port
  3418. //===============================================
  3419. int InitRfidPort()
  3420. {
  3421. int uartO2 = open(rfidPortName, O_RDWR);
  3422. struct termios tios;
  3423. if (uartO2 != FAIL)
  3424. {
  3425. ioctl(uartO2, TCGETS, &tios);
  3426. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  3427. tios.c_lflag = 0;
  3428. tios.c_iflag = 0;
  3429. tios.c_oflag = 0;
  3430. tios.c_cc[VMIN] = 0;
  3431. tios.c_cc[VTIME] = (unsigned char) 1;
  3432. tios.c_lflag = 0;
  3433. tcflush(uartO2, TCIFLUSH);
  3434. ioctl(uartO2, TCSETS, &tios);
  3435. }
  3436. if (uartO2 < 0)
  3437. {
  3438. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  3439. }
  3440. return uartO2;
  3441. }
  3442. //===============================================
  3443. // Check internet access status
  3444. //===============================================
  3445. int isReachableInternet()
  3446. {
  3447. int result = FAIL;
  3448. FILE *fp;
  3449. char cmd[256];
  3450. char buf[512];
  3451. char tmp[512];
  3452. // Get ip address & net mask
  3453. strcpy(cmd, "ifconfig eth0");
  3454. fp = popen(cmd, "r");
  3455. if (fp != NULL)
  3456. {
  3457. while (fgets(buf, sizeof(buf), fp) != NULL)
  3458. {
  3459. if (strstr(buf, "inet addr:") > 0)
  3460. {
  3461. sscanf(buf, "%*s%s", tmp);
  3462. substr((char*) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp, strspn(tmp, "addr:"),
  3463. strlen(tmp) - strspn(tmp, "addr:"));
  3464. sscanf(buf, "%*s%*s%*s%s", tmp);
  3465. substr((char*) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp,
  3466. strspn(tmp, "Mask:"), strlen(tmp) - strspn(tmp, "Mask:"));
  3467. }
  3468. }
  3469. }
  3470. pclose(fp);
  3471. memset(buf, 0x00, sizeof(buf));
  3472. // Get gateway
  3473. fp = popen("ip route", "r");
  3474. if (fp == NULL)
  3475. result = FAIL;
  3476. else
  3477. {
  3478. while (fgets(buf, sizeof(buf), fp) != NULL)
  3479. {
  3480. if ((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  3481. break;
  3482. }
  3483. if (strstr(buf, "default") != NULL)
  3484. {
  3485. sscanf(buf, "%*s%*s%s", tmp);
  3486. substr((char*) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  3487. }
  3488. }
  3489. pclose(fp);
  3490. memset(buf, 0x00, sizeof(buf));
  3491. for (int idx = 0; idx < ARRAY_SIZE(valid_Internet); idx++)
  3492. {
  3493. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  3494. fp = popen(cmd, "r");
  3495. if (fp != NULL)
  3496. {
  3497. while (fgets(buf, sizeof(buf), fp) != NULL)
  3498. {
  3499. if (strstr(buf, "transmitted") > 0)
  3500. {
  3501. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  3502. if (strstr(buf, "100%") != NULL)
  3503. {
  3504. }
  3505. else
  3506. {
  3507. result = PASS;
  3508. }
  3509. //DEBUG_INFO("%s",buf);
  3510. //DEBUG_INFO("%s\n",tmp);
  3511. }
  3512. }
  3513. }
  3514. pclose(fp);
  3515. }
  3516. return result;
  3517. }
  3518. int isRouteFail()
  3519. {
  3520. int result = YES;
  3521. FILE *fp;
  3522. char buf[512];
  3523. fp = popen("route -n", "r");
  3524. if (fp != NULL)
  3525. {
  3526. while (fgets(buf, sizeof(buf), fp) != NULL)
  3527. {
  3528. if (strstr(buf, "eth0") != NULL)
  3529. result = NO;
  3530. }
  3531. }
  3532. pclose(fp);
  3533. return result;
  3534. }
  3535. //===============================================
  3536. // Check reservation date is expired
  3537. //===============================================
  3538. int isReservationExpired(unsigned char gun_index)
  3539. {
  3540. int result = NO;
  3541. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3542. {
  3543. if (isOvertNow(ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate))
  3544. result = YES;
  3545. }
  3546. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3547. {
  3548. if (isOvertNow(ShmOCPP20Data->ReserveNow[gun_index].expiryDateTime))
  3549. result = YES;
  3550. }
  3551. return result;
  3552. }
  3553. //===============================================
  3554. // Check charging profile related date routine
  3555. //===============================================
  3556. int isProfileValid(uint8_t gun_index)
  3557. {
  3558. int result = NO;
  3559. struct tm tmFrom, tmTo;
  3560. struct timeb tbFrom, tbTo;
  3561. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3562. {
  3563. if ((sscanf((char*) ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom, "%4d-%2d-%2dT%2d:%2d:%2d",
  3564. &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6)
  3565. && (sscanf((char*) ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo, "%4d-%2d-%2dT%2d:%2d:%2d",
  3566. &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
  3567. {
  3568. tmFrom.tm_year -= 1900;
  3569. tmFrom.tm_mon -= 1;
  3570. tbFrom.time = mktime(&tmFrom);
  3571. tmTo.tm_year -= 1900;
  3572. tmTo.tm_mon -= 1;
  3573. tbTo.time = mktime(&tmTo);
  3574. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3575. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3576. if ((DiffTimebWithNow(tbFrom) >= 0) && (DiffTimebWithNow(tbTo) <= 0))
  3577. {
  3578. result = YES;
  3579. }
  3580. }
  3581. else
  3582. {
  3583. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3584. }
  3585. }
  3586. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3587. {
  3588. if ((sscanf((char*) ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom, "%4d-%2d-%2dT%2d:%2d:%2d",
  3589. &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6)
  3590. && (sscanf((char*) ShmOCPP20Data->SmartChargingProfile[gun_index].validTo, "%4d-%2d-%2dT%2d:%2d:%2d",
  3591. &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
  3592. {
  3593. tmFrom.tm_year -= 1900;
  3594. tmFrom.tm_mon -= 1;
  3595. tbFrom.time = mktime(&tmFrom);
  3596. tmTo.tm_year -= 1900;
  3597. tmTo.tm_mon -= 1;
  3598. tbTo.time = mktime(&tmTo);
  3599. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  3600. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  3601. if ((DiffTimebWithNow(tbFrom) >= 0) && (DiffTimebWithNow(tbTo) <= 0))
  3602. {
  3603. result = YES;
  3604. }
  3605. }
  3606. else
  3607. {
  3608. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  3609. }
  3610. }
  3611. return result;
  3612. }
  3613. int getMaxScheduleStart()
  3614. {
  3615. int result = -1;
  3616. struct tm tmScheduleStart;
  3617. struct timeb tbScheduleStart;
  3618. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3619. {
  3620. if ((sscanf((char*) ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.StartSchedule, "%4d-%2d-%2dT%2d:%2d:%2d",
  3621. &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday, &tmScheduleStart.tm_hour,
  3622. &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6))
  3623. {
  3624. tmScheduleStart.tm_year -= 1900;
  3625. tmScheduleStart.tm_mon -= 1;
  3626. tbScheduleStart.time = mktime(&tmScheduleStart);
  3627. tbScheduleStart.millitm = 0;
  3628. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  3629. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  3630. }
  3631. else
  3632. {
  3633. DEBUG_WARN("Max schedule start date parsing error.\n");
  3634. }
  3635. }
  3636. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3637. {
  3638. if ((sscanf((char*) ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].startSchedule,
  3639. "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday,
  3640. &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6))
  3641. {
  3642. tmScheduleStart.tm_year -= 1900;
  3643. tmScheduleStart.tm_mon -= 1;
  3644. tbScheduleStart.time = mktime(&tmScheduleStart);
  3645. tbScheduleStart.millitm = 0;
  3646. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  3647. //DEBUG_INFO("Max schedule start compare Now(seconds): %d\n", result);
  3648. }
  3649. else
  3650. {
  3651. DEBUG_WARN("Max schedule start date parsing error.\n");
  3652. }
  3653. }
  3654. return result;
  3655. }
  3656. int getScheduleStart(int gun_index)
  3657. {
  3658. int result = -1;
  3659. struct tm tmScheduleStart;
  3660. struct timeb tbScheduleStart;
  3661. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3662. {
  3663. if ((sscanf((char*) ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule,
  3664. "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday,
  3665. &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6))
  3666. {
  3667. tmScheduleStart.tm_year -= 1900;
  3668. tmScheduleStart.tm_mon -= 1;
  3669. tbScheduleStart.time = mktime(&tmScheduleStart);
  3670. tbScheduleStart.millitm = 0;
  3671. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  3672. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3673. }
  3674. else
  3675. {
  3676. DEBUG_WARN("Schedule start date parsing error.\n");
  3677. }
  3678. }
  3679. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3680. {
  3681. if ((sscanf((char*) ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].startSchedule,
  3682. "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday,
  3683. &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6))
  3684. {
  3685. tmScheduleStart.tm_year -= 1900;
  3686. tmScheduleStart.tm_mon -= 1;
  3687. tbScheduleStart.time = mktime(&tmScheduleStart);
  3688. tbScheduleStart.millitm = 0;
  3689. result = DiffTimebWithNow(tbScheduleStart) / 1000;
  3690. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  3691. }
  3692. else
  3693. {
  3694. DEBUG_WARN("Schedule start date parsing error.\n");
  3695. }
  3696. }
  3697. return result;
  3698. }
  3699. int getStartSinceToday()
  3700. {
  3701. int result = -1;
  3702. time_t t;
  3703. struct tm *tmStartToday;
  3704. struct timeb tbStartToday;
  3705. t = time(NULL);
  3706. tmStartToday = localtime(&t);
  3707. tmStartToday->tm_hour = 0;
  3708. tmStartToday->tm_min = 0;
  3709. tmStartToday->tm_sec = 0;
  3710. tbStartToday.time = mktime(tmStartToday);
  3711. result = DiffTimebWithNow(tbStartToday) / 1000;
  3712. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  3713. return result;
  3714. }
  3715. int getStartSinceWeek()
  3716. {
  3717. int result = -1;
  3718. time_t t;
  3719. struct tm *tmStartWeek;
  3720. struct timeb tbStartWeek;
  3721. t = time(NULL);
  3722. tmStartWeek = localtime(&t);
  3723. t -= 86400 * tmStartWeek->tm_wday;
  3724. tmStartWeek = localtime(&t);
  3725. tmStartWeek->tm_hour = 0;
  3726. tmStartWeek->tm_min = 0;
  3727. tmStartWeek->tm_sec = 0;
  3728. tbStartWeek.time = mktime(tmStartWeek);
  3729. result = DiffTimebWithNow(tbStartWeek) / 1000;
  3730. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  3731. return result;
  3732. }
  3733. //===============================================
  3734. // Valid from local white list
  3735. //===============================================
  3736. int isValidLocalWhiteCard()
  3737. {
  3738. uint8_t result = FAIL;
  3739. for (uint8_t idx = 0; idx < ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard); idx++)
  3740. {
  3741. if (strcmp((char*) ShmSysConfigAndInfo->SysConfig.UserId,
  3742. (char*) ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  3743. {
  3744. result = PASS;
  3745. }
  3746. }
  3747. return result;
  3748. }
  3749. //==========================================
  3750. // Check routine
  3751. //==========================================
  3752. void checkTask()
  3753. {
  3754. //lwtest
  3755. if ((ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'D'))
  3756. {
  3757. if (system("pidof -s Module_4g > /dev/null") != 0)
  3758. {
  3759. DEBUG_INFO("Module_4g not running, restart it.\n");
  3760. system("/root/Module_4g &");
  3761. }
  3762. }
  3763. if ((ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE] == 'D'))
  3764. {
  3765. if (system("pidof -s Module_Wifi > /dev/null") != 0)
  3766. {
  3767. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  3768. system("/root/Module_Wifi &");
  3769. }
  3770. }
  3771. if (system("pidof -s Module_EventLogging > /dev/null") != 0)
  3772. {
  3773. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  3774. system("/root/Module_EventLogging &");
  3775. }
  3776. if ((strcmp((char *) &ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") != 0))
  3777. {
  3778. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3779. {
  3780. if ((time((time_t*) NULL) - ShmOCPP16Data->procDogTime) > 180)
  3781. {
  3782. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  3783. ShmOCPP16Data->procDogTime = time((time_t*) NULL);
  3784. system("pkill OcppBackend");
  3785. sleep(3);
  3786. ocpp_process_start();
  3787. }
  3788. if (system("pidof -s OcppBackend > /dev/null") != 0)
  3789. {
  3790. DEBUG_INFO("OcppBackend not running, restart it.\n");
  3791. ocpp_process_start();
  3792. }
  3793. }
  3794. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3795. {
  3796. if ((time((time_t*) NULL) - ShmOCPP20Data->procDogTime) > 180)
  3797. {
  3798. DEBUG_WARN("OcppBackend20 watch dog timeout task restart.\n");
  3799. ShmOCPP20Data->procDogTime = time((time_t*) NULL);
  3800. system("pkill OcppBackend20");
  3801. sleep(3);
  3802. ocpp_process_start();
  3803. }
  3804. if (system("pidof -s OcppBackend20 > /dev/null") != 0)
  3805. {
  3806. DEBUG_INFO("OcppBackend20 not running, restart it.\n");
  3807. ocpp_process_start();
  3808. }
  3809. }
  3810. }
  3811. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != 0)
  3812. {
  3813. if (system("pidof -s OcppBackendPH > /dev/null") != 0)
  3814. {
  3815. DEBUG_INFO("OcppBackendPH not running, restart it.\n");
  3816. system("/root/OcppBackendPH &");
  3817. }
  3818. }
  3819. //lwtest
  3820. if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_OUTPUT_TYPE] == 'A')
  3821. {
  3822. if (system("pidof -s Module_Speaker > /dev/null") != 0)
  3823. {
  3824. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  3825. system("/root/Module_Speaker &");
  3826. }
  3827. }
  3828. if (system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  3829. {
  3830. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  3831. system("/root/Module_ProduceUtils &");
  3832. }
  3833. }
  3834. //===============================================
  3835. // Check simulator status ..lwtest
  3836. //===============================================
  3837. bool isTwoGunInChargingMode(void)
  3838. {
  3839. bool result = FALSE;
  3840. if (modelnameInfo.GetGunCount < 2)
  3841. result = FALSE;
  3842. else
  3843. {
  3844. if (getTargetChargingInfoData(0)->SystemStatus == SYS_MODE_CHARGING && getTargetChargingInfoData(1)->SystemStatus == SYS_MODE_CHARGING)
  3845. result = TRUE;
  3846. }
  3847. return result;
  3848. }
  3849. void checkConnectionTimeout()
  3850. {
  3851. if ((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
  3852. {
  3853. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  3854. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING);
  3855. }
  3856. else
  3857. {
  3858. ShmCharger->timeoutSpec.Setting_Timeout_Spec = ocpp_get_connection_timeout();
  3859. if ((ShmCharger->timeoutSpec.Setting_Timeout_Spec) < TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  3860. {
  3861. ShmCharger->timeoutSpec.Present_Timeout_Spec = (TIMEOUT_SPEC_BS_HLC_HANDSHAKE + 10);
  3862. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  3863. }
  3864. else
  3865. {
  3866. ShmCharger->timeoutSpec.Present_Timeout_Spec = ShmCharger->timeoutSpec.Setting_Timeout_Spec;
  3867. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  3868. }
  3869. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  3870. }
  3871. }
  3872. void checkReset()
  3873. {
  3874. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3875. {
  3876. if (ShmOCPP16Data->MsMsg.bits.ResetReq)
  3877. {
  3878. if ((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE))
  3879. && (modelnameInfo.GetGunCount > 1 ? (!isMode(1, SYS_MODE_CHARGING)
  3880. && !isMode(1, SYS_MODE_TERMINATING)
  3881. && !isMode(1, SYS_MODE_COMPLETE)) : TRUE))
  3882. {
  3883. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  3884. sprintf((char*) ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  3885. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  3886. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  3887. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  3888. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3889. if (strcmp((char*) ShmOCPP16Data->Reset.Type, "Hard") == 0)
  3890. {
  3891. system("sync");
  3892. sleep(10);
  3893. system("reboot -f");
  3894. sleep(10);
  3895. system("reboot -f");
  3896. }
  3897. else
  3898. {
  3899. sleep(10);
  3900. close(wtdFd);
  3901. system("/usr/bin/run_evse_restart.sh");
  3902. }
  3903. }
  3904. }
  3905. }
  3906. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3907. {
  3908. if (ShmOCPP20Data->MsMsg.bits.ResetReq)
  3909. {
  3910. if ((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE))
  3911. && (modelnameInfo.GetGunCount > 1 ? (!isMode(1, SYS_MODE_CHARGING)
  3912. && !isMode(1, SYS_MODE_TERMINATING)
  3913. && !isMode(0, SYS_MODE_COMPLETE)) : TRUE))
  3914. {
  3915. ShmOCPP20Data->MsMsg.bits.ResetReq = OFF;
  3916. sprintf((char*) ShmOCPP20Data->Reset.Response_status, "Accepted");
  3917. ShmOCPP20Data->MsMsg.bits.ResetConf = ON;
  3918. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP20Data->Reset.type);
  3919. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  3920. setChargerMode(gun_index, SYS_MODE_BOOTING);
  3921. if (strcmp((char*) ShmOCPP20Data->Reset.type, "Immediate") == 0)
  3922. {
  3923. system("sync");
  3924. sleep(10);
  3925. system("reboot -f");
  3926. sleep(10);
  3927. system("reboot -f");
  3928. }
  3929. else
  3930. {
  3931. sleep(10);
  3932. close(wtdFd);
  3933. system("/usr/bin/run_evse_restart.sh");
  3934. }
  3935. }
  3936. }
  3937. }
  3938. }
  3939. void checkReservation(uint8_t gun_index)
  3940. {
  3941. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3942. {
  3943. if (ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  3944. {
  3945. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3946. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3947. if (isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3948. {
  3949. sleep(5);
  3950. getTargetChargingInfoData(gun_index)->ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  3951. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3952. }
  3953. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3954. }
  3955. }
  3956. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3957. {
  3958. if (ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq)
  3959. {
  3960. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  3961. ShmOCPP20Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  3962. if (isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  3963. {
  3964. getTargetChargingInfoData(gun_index)->ReservationId = ShmOCPP20Data->ReserveNow[gun_index].id;
  3965. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  3966. }
  3967. DEBUG_INFO("Reservation request on gun-%d.\n", gun_index);
  3968. }
  3969. }
  3970. }
  3971. void checkUnlocker(uint8_t gun_index)
  3972. {
  3973. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3974. {
  3975. if (ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3976. {
  3977. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3978. sprintf( (char*) ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].ResponseStatus, "Unlocked");
  3979. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].UnlockConnectorConf = ON;
  3980. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].isUnlockerConnetor = ON;
  3981. }
  3982. }
  3983. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  3984. {
  3985. if (ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  3986. {
  3987. ShmOCPP20Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  3988. sprintf((char*) ShmOCPP20Data->UnlockConnector[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].Response_status, "UnlockFailed");
  3989. ShmOCPP20Data->CsMsg.bits[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].UnlockConnectorConf = ON;
  3990. ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].isUnlockerConnetor = ON;
  3991. }
  3992. }
  3993. }
  3994. void checkAvailability(uint8_t gun_index)
  3995. {
  3996. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  3997. {
  3998. if (ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  3999. {
  4000. if (strcmp((char*) ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  4001. {
  4002. DB_Update_Operactive(localDb, gun_index, true);
  4003. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  4004. }
  4005. else
  4006. {
  4007. DB_Update_Operactive(localDb, gun_index, false);
  4008. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  4009. }
  4010. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  4011. }
  4012. }
  4013. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4014. {
  4015. if (ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  4016. {
  4017. if (strcmp((char*) ShmOCPP20Data->ChangeAvailability[gun_index].operationalStatus, "Operative") == 0)
  4018. {
  4019. DB_Update_Operactive(localDb, gun_index, true);
  4020. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  4021. }
  4022. else
  4023. {
  4024. DB_Update_Operactive(localDb, gun_index, false);
  4025. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  4026. }
  4027. ShmOCPP20Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  4028. }
  4029. }
  4030. }
  4031. void checkChargingProfileLimit(uint8_t gun_index, uint8_t system_mode)
  4032. {
  4033. uint16_t MaxChargingProfileChargingCurrent = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  4034. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4035. {
  4036. // Get profile charging profile limit
  4037. if ((ocpp_get_smartcharging_profileId(gun_index) > 0))
  4038. {
  4039. // Checking profile kind
  4040. if ((mystrcmp((char*) ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  4041. {
  4042. // Checking limitation
  4043. for (uint8_t idx_period = 0; idx_period < ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod); idx_period++)
  4044. {
  4045. if ((getScheduleStart(gun_index) >= ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod)
  4046. && ((idx_period == 0) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0)))
  4047. {
  4048. //lwtest
  4049. ShmCharger->gun_info[gun_index].ChargingProfilePower = AC_OUTPUT_VOL
  4050. * ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit
  4051. * ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].NumberPhases;
  4052. ShmCharger->gun_info[gun_index].ChargingProfileCurrent = ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit;
  4053. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].ChargingProfileCurrent;
  4054. }
  4055. else
  4056. break;
  4057. }
  4058. }
  4059. }
  4060. else
  4061. {
  4062. //lwtest
  4063. if (isTwoGunInChargingMode())
  4064. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.5;
  4065. else
  4066. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  4067. //printf("Gun %d charging targetCurrent %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4068. }
  4069. // Get max charging profile limit
  4070. if ((ocpp_get_maxcharging_profileId() > 0))
  4071. {
  4072. // Absolute profile
  4073. if ((mystrcmp((char*) ShmOCPP16Data->MaxChargingProfile.ChargingProfileKind, "Absolute") == PASS))
  4074. {
  4075. for (uint8_t idx_period = 0; idx_period < ARRAY_SIZE(ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod); idx_period++)
  4076. {
  4077. if ((getMaxScheduleStart() >= ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod)
  4078. && ((idx_period == 0) || (ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0)))
  4079. {
  4080. //lwtest
  4081. MaxChargingProfileChargingCurrent = ShmOCPP16Data->MaxChargingProfile.ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit;
  4082. // printf("Gun %d Get Max Profile limit = %.2f\n", gun_index,
  4083. // ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
  4084. }
  4085. else
  4086. break;
  4087. }
  4088. }
  4089. }
  4090. // Compare target current is over max current for EVSE limit
  4091. //lwtest
  4092. if (isTwoGunInChargingMode() == FALSE)
  4093. {
  4094. //AC type or only one connector in charging
  4095. if (ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  4096. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  4097. }
  4098. else
  4099. {
  4100. uint8_t _otherIndex = 0;
  4101. _otherIndex = (gun_index > 0) ? 0 : 1;
  4102. if ((ShmCharger->gun_info[_otherIndex].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent)
  4103. > MaxChargingProfileChargingCurrent)
  4104. {
  4105. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent
  4106. - ShmCharger->gun_info[_otherIndex].targetCurrent);
  4107. }
  4108. }
  4109. }
  4110. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4111. {
  4112. // Get profile charging profile limit
  4113. if ((ocpp_get_smartcharging_profileId(gun_index) > 0))
  4114. {
  4115. // Checking profile kind
  4116. if ((mystrcmp((char*) ShmOCPP20Data->SmartChargingProfile[gun_index].chargingProfileKind, "Absolute") == PASS))
  4117. {
  4118. // Checking limitation
  4119. for (uint8_t idx_period = 0; idx_period < ARRAY_SIZE(ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod); idx_period++)
  4120. {
  4121. if ((getScheduleStart(gun_index) >= ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod)
  4122. && ((idx_period == 0) || (ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0)))
  4123. {
  4124. ShmCharger->gun_info[gun_index].ChargingProfilePower = AC_OUTPUT_VOL
  4125. * ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit
  4126. * ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].numberPhases;
  4127. ShmCharger->gun_info[gun_index].ChargingProfileCurrent = ShmOCPP20Data->SmartChargingProfile[gun_index].chargingSchedule[0].chargingSchedulePeriod[idx_period].limit;
  4128. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].ChargingProfileCurrent;
  4129. }
  4130. else
  4131. break;
  4132. }
  4133. }
  4134. }
  4135. else
  4136. {
  4137. //lwtest
  4138. if (isTwoGunInChargingMode())
  4139. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.5;
  4140. else
  4141. ShmCharger->gun_info[gun_index].targetCurrent = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  4142. }
  4143. // Get max charging profile limit
  4144. if ((ocpp_get_maxcharging_profileId() > 0))
  4145. {
  4146. // Absolute profile
  4147. if ((mystrcmp((char*) ShmOCPP20Data->MaxChargingProfile.chargingProfileKind, "Absolute") == PASS))
  4148. {
  4149. // Checking limitation
  4150. for (uint8_t idx_period = 0; idx_period < ARRAY_SIZE(ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod); idx_period++)
  4151. {
  4152. if ((getMaxScheduleStart() >= ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod)
  4153. && ((idx_period == 0) || (ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].startPeriod > 0)))
  4154. {
  4155. //lwtest
  4156. MaxChargingProfileChargingCurrent = ShmOCPP20Data->MaxChargingProfile.chargingSchedule[0].chargingSchedulePeriod[idx_period].limit;
  4157. }
  4158. else
  4159. break;
  4160. }
  4161. }
  4162. }
  4163. // Compare target current is over max current for EVSE limit
  4164. //lwtest
  4165. if (isTwoGunInChargingMode() == FALSE)
  4166. {
  4167. //AC type or only one connector in charging
  4168. if (ShmCharger->gun_info[gun_index].targetCurrent > MaxChargingProfileChargingCurrent)
  4169. ShmCharger->gun_info[gun_index].targetCurrent = MaxChargingProfileChargingCurrent;
  4170. }
  4171. else
  4172. {
  4173. uint8_t _otherIndex = 0;
  4174. _otherIndex = (gun_index > 0) ? 0 : 1;
  4175. if ((ShmCharger->gun_info[_otherIndex].targetCurrent + ShmCharger->gun_info[gun_index].targetCurrent)
  4176. > MaxChargingProfileChargingCurrent)
  4177. {
  4178. ShmCharger->gun_info[gun_index].targetCurrent = (MaxChargingProfileChargingCurrent
  4179. - ShmCharger->gun_info[_otherIndex].targetCurrent);
  4180. }
  4181. }
  4182. }
  4183. // Charging session target current check if OCPP disconnect and power sharing server connected
  4184. if (ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing > 0)
  4185. {
  4186. if (ShmSysConfigAndInfo->SysInfo.localSharingInfo.isConnectedSharingServer)
  4187. ShmCharger->gun_info[gun_index].targetCurrent =
  4188. ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index] > ShmSysConfigAndInfo->SysConfig.RatingCurrent
  4189. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmSysConfigAndInfo->SysInfo.localSharingInfo.AvailableShargingCurrent[gun_index];
  4190. else
  4191. {
  4192. if (ShmCharger->gun_info[gun_index].targetCurrent != 0)
  4193. DEBUG_WARN("Disconnect from power sharing server, target current set to 0.\n");
  4194. ShmCharger->gun_info[gun_index].targetCurrent = 0;
  4195. }
  4196. }
  4197. switch (system_mode)
  4198. {
  4199. case SYS_MODE_IDLE:
  4200. if (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4201. {
  4202. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4203. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4204. if (ShmCharger->isCcsEnable)
  4205. {
  4206. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  4207. }
  4208. else
  4209. {
  4210. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4211. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4212. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4213. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4214. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4215. ? 6 : ((ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4216. }
  4217. }
  4218. else
  4219. {
  4220. ShmCharger->gun_info[gun_index].targetCurrent =
  4221. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4222. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4223. if (ShmCharger->isCcsEnable)
  4224. {
  4225. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  4226. }
  4227. else
  4228. {
  4229. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4230. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4231. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4232. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4233. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4234. ? 6 : ((ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4235. }
  4236. }
  4237. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4238. break;
  4239. case SYS_MODE_PREPARING:
  4240. if (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4241. {
  4242. ShmCharger->gun_info[gun_index].targetCurrent =
  4243. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4244. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4245. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4246. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4247. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4248. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4249. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4250. ? 6 : ( (ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4251. }
  4252. else
  4253. {
  4254. ShmCharger->gun_info[gun_index].targetCurrent =
  4255. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4256. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4257. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4258. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4259. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4260. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4261. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4262. ? 6 : ( (ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4263. }
  4264. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4265. break;
  4266. case SYS_MODE_CHARGING:
  4267. case SYS_MODE_TERMINATING:
  4268. if (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  4269. {
  4270. ShmCharger->gun_info[gun_index].targetCurrent =
  4271. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4272. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4273. if ((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  4274. || (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4275. {
  4276. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4277. {
  4278. if (getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4279. {
  4280. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4281. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4282. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4283. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4284. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4285. ? 6 : ((ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4286. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4287. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4288. }
  4289. }
  4290. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4291. {
  4292. ShmCharger->gun_info[gun_index].targetCurrent =
  4293. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.RatingCurrent)
  4294. ? ShmSysConfigAndInfo->SysConfig.RatingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4295. ShmCharger->gun_info[gun_index].targetCurrent =
  4296. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4297. ? 6 : ShmCharger->gun_info[gun_index].targetCurrent);
  4298. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float) ShmCharger->gun_info[gun_index].targetCurrent;
  4299. }
  4300. }
  4301. }
  4302. else
  4303. {
  4304. ShmCharger->gun_info[gun_index].targetCurrent =
  4305. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4306. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4307. if ((ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  4308. || (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current))
  4309. {
  4310. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  4311. {
  4312. if (getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  4313. {
  4314. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4315. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4316. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4317. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current =
  4318. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4319. ? 6 : ((ShmCharger->gun_info[gun_index].targetCurrent == 0) ? 100 : ShmCharger->gun_info[gun_index].targetCurrent));
  4320. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  4321. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  4322. }
  4323. }
  4324. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  4325. {
  4326. ShmCharger->gun_info[gun_index].targetCurrent =
  4327. ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)
  4328. ? ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent : ShmCharger->gun_info[gun_index].targetCurrent);
  4329. ShmCharger->gun_info[gun_index].targetCurrent =
  4330. ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)
  4331. ? 6 : ShmCharger->gun_info[gun_index].targetCurrent);
  4332. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float) ShmCharger->gun_info[gun_index].targetCurrent;
  4333. }
  4334. }
  4335. }
  4336. if (ShmCharger->gun_info[gun_index].targetCurrent != previousData[gun_index].targetCurrent)
  4337. {
  4338. DEBUG_INFO("==================================================\n");
  4339. DEBUG_INFO("Gun-%02d targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  4340. DEBUG_INFO("MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  4341. DEBUG_INFO("==================================================\n");
  4342. previousData[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].targetCurrent;
  4343. }
  4344. if (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit != previousData[gun_index].current_limit)
  4345. {
  4346. DEBUG_INFO("==================================================\n");
  4347. DEBUG_INFO("Gun-%02d mcu current_limit: %d\n", gun_index,
  4348. ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  4349. DEBUG_INFO("==================================================\n");
  4350. previousData[gun_index].current_limit = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
  4351. }
  4352. //lwtest
  4353. if (ShmCharger->gun_info[gun_index].ChargingProfileCurrent != previousData[gun_index].ChargingProfileCurrent)
  4354. {
  4355. DEBUG_INFO("==================================================\n");
  4356. DEBUG_INFO("Gun-%02d Profile limit : %.2f\n", gun_index,
  4357. ShmCharger->gun_info[gun_index].ChargingProfileCurrent);
  4358. DEBUG_INFO("Gun-%02d Profile power : %.2f\n", gun_index,
  4359. ShmCharger->gun_info[gun_index].ChargingProfilePower);
  4360. DEBUG_INFO("==================================================\n");
  4361. previousData[gun_index].ChargingProfileCurrent = ShmCharger->gun_info[gun_index].ChargingProfileCurrent;
  4362. previousData[gun_index].ChargingProfilePower = ShmCharger->gun_info[gun_index].ChargingProfilePower;
  4363. }
  4364. break;
  4365. }
  4366. }
  4367. void checkStopReason(uint8_t gun_index)
  4368. {
  4369. sleep(2);
  4370. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4371. {
  4372. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00,
  4373. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4374. if (!ocpp_get_starttransaction_result(gun_index))
  4375. {
  4376. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "DeAuthorized");
  4377. }
  4378. else if (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  4379. {
  4380. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  4381. }
  4382. else if ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A)
  4383. || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  4384. {
  4385. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  4386. memcpy((char*) ShmOCPP16Data->StopTransaction[gun_index].IdTag,
  4387. (char*) ShmOCPP16Data->StartTransaction[gun_index].IdTag,
  4388. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4389. }
  4390. else if (ShmOCPP16Data->MsMsg.bits.ResetReq)
  4391. {
  4392. if (strcmp((char*) ShmOCPP16Data->Reset.Type, "Hard") == 0)
  4393. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  4394. else
  4395. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  4396. }
  4397. else if (ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  4398. {
  4399. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  4400. if (!isMatchStartUser())
  4401. {
  4402. memcpy((char*) ShmOCPP16Data->StopTransaction[gun_index].IdTag,
  4403. (char*) ShmSysConfigAndInfo->SysConfig.UserId,
  4404. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4405. }
  4406. else
  4407. {
  4408. memcpy((char*) ShmOCPP16Data->StopTransaction[gun_index].IdTag,
  4409. (char*) getTargetChargingInfoData(gun_index)->StartUserId,
  4410. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4411. }
  4412. DEBUG_INFO("Gun-[%d] : IdTag [ %s ].\n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  4413. }
  4414. else if (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  4415. {
  4416. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  4417. }
  4418. else if (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].isUnlockerConnetor == ON)
  4419. {
  4420. sprintf((char*) ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId - 1].StopReason, "UnlockCommand");
  4421. }
  4422. else
  4423. {
  4424. sprintf((char*) ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  4425. }
  4426. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  4427. getTargetChargingInfoData(gun_index)->PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption / 10000.0);
  4428. presentChargedEnergyUpdate(gun_index);
  4429. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n", gun_index,getTargetChargingInfoData(gun_index)->PresentChargedEnergy);
  4430. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  4431. }
  4432. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4433. {
  4434. memset(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken, 0x00,
  4435. ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  4436. if (!ocpp_get_starttransaction_result(gun_index))
  4437. {
  4438. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "DeAuthorized");
  4439. }
  4440. else if (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  4441. {
  4442. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EmergencyStop");
  4443. }
  4444. else if ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A)
  4445. || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  4446. {
  4447. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "EVDisconnected");
  4448. }
  4449. else if (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_GROUND_FAIL)
  4450. {
  4451. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "GroundFault");
  4452. }
  4453. else if (ShmOCPP20Data->MsMsg.bits.ResetReq)
  4454. {
  4455. if (strcmp((char*) ShmOCPP20Data->Reset.type, "Immediate") == 0)
  4456. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "ImmediateReset");
  4457. else
  4458. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OnIdle");
  4459. }
  4460. else if (ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  4461. {
  4462. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Local");
  4463. if (!isMatchStartUser())
  4464. {
  4465. memcpy((char*) ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken,
  4466. (char*) ShmSysConfigAndInfo->SysConfig.UserId,
  4467. ARRAY_SIZE(ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken));
  4468. }
  4469. else
  4470. {
  4471. memcpy((char*) ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken,
  4472. (char*) getTargetChargingInfoData(gun_index)->StartUserId,
  4473. ARRAY_SIZE(getTargetChargingInfoData(gun_index)->StartUserId));
  4474. }
  4475. DEBUG_INFO("Gun-[%d] : idToken [ %s ].\n", gun_index,
  4476. ShmOCPP20Data->TransactionEvent[gun_index].idToken.idToken);
  4477. }
  4478. else if ((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L1_OVER_CURRENT)
  4479. || (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L2_OVER_CURRENT)
  4480. || (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_L3_OVER_CURRENT))
  4481. {
  4482. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "OvercurrentFault");
  4483. }
  4484. else if (ShmOCPP20Data->CsMsg.bits[gun_index].RequestStopTransactionReq)
  4485. {
  4486. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Remote");
  4487. }
  4488. else if (ShmCharger->gun_info[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].isUnlockerConnetor == ON)
  4489. {
  4490. sprintf( (char*) ShmOCPP20Data->TransactionEvent[ShmOCPP20Data->UnlockConnector[gun_index].connectorId - 1].transactionInfo.stoppedReason,
  4491. "UnlockCommand");
  4492. }
  4493. else
  4494. {
  4495. sprintf((char*) ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason, "Other");
  4496. }
  4497. DEBUG_INFO("Gun-[%d] : StopReason [ %s ].\n", gun_index,
  4498. ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason);
  4499. getTargetChargingInfoData(gun_index)->PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/ 10000.0);
  4500. presentChargedEnergyUpdate(gun_index);
  4501. DEBUG_INFO("Gun-[%d] : PresentChargedEnergy [ %.4f ].\n", gun_index,
  4502. getTargetChargingInfoData(gun_index)->PresentChargedEnergy);
  4503. ShmOCPP20Data->CpMsg.bits[gun_index].TransactionEventReq = ON;
  4504. }
  4505. // Maintain server
  4506. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.MaintainServerURL, "") != 0)
  4507. {
  4508. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].StopReason,
  4509. ShmOCPP16Data->StopTransaction[gun_index].StopReason,
  4510. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].StopReason));
  4511. memcpy(ShmOCPP16DataPH->StopTransaction[gun_index].IdTag, ShmOCPP16Data->StopTransaction[gun_index].IdTag,
  4512. ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  4513. ShmOCPP16DataPH->CpMsg.bits[gun_index].StopTransactionReq = ON;
  4514. }
  4515. }
  4516. void checkRemoteUpgradeStatus()
  4517. {
  4518. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  4519. {
  4520. if (strcmp((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed") == 0)
  4521. {
  4522. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4523. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4524. ShmCharger->isUpdateSuccess = NO;
  4525. }
  4526. else if (strcmp((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded") == 0)
  4527. {
  4528. DEBUG_INFO("Firmware remote upgrading...\n");
  4529. sprintf((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4530. sleep(1);
  4531. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4532. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4533. int result = upgrade_check();
  4534. DEBUG_INFO("Remote update Result: %s... \n", ((result == PASS)?"Pass": "Fail"));
  4535. if (result == PASS)
  4536. {
  4537. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4538. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4539. ShmCharger->isUpdateSuccess = YES;
  4540. DEBUG_INFO("Remote update success...\n");
  4541. }
  4542. else
  4543. {
  4544. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4545. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4546. ShmCharger->isUpdateSuccess = NO;
  4547. DEBUG_INFO("Remote update unsuccess...\n");
  4548. }
  4549. }
  4550. else
  4551. {
  4552. }
  4553. }
  4554. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  4555. {
  4556. if (strcmp((char*) ShmOCPP20Data->FirmwareStatusNotification.status, "DownloadFailed") == 0)
  4557. {
  4558. DEBUG_INFO("Firmware remote upgraded fail...\n");
  4559. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4560. ShmCharger->isUpdateSuccess = NO;
  4561. }
  4562. else if (strcmp((char*) ShmOCPP20Data->FirmwareStatusNotification.status, "Downloaded") == 0)
  4563. {
  4564. DEBUG_INFO("Firmware remote upgrading...\n");
  4565. sprintf((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  4566. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  4567. ShmOCPP20Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  4568. int result = upgrade_check();
  4569. DEBUG_INFO("Remote update Result: %s... \n", ((result == PASS)?"Pass": "Fail"));
  4570. if (result == PASS)
  4571. {
  4572. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  4573. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  4574. ShmCharger->isUpdateSuccess = YES;
  4575. DEBUG_INFO("Remote update success...\n");
  4576. }
  4577. else
  4578. {
  4579. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  4580. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  4581. ShmCharger->isUpdateSuccess = NO;
  4582. DEBUG_INFO("Remote update unsuccess...\n");
  4583. }
  4584. }
  4585. else
  4586. {
  4587. }
  4588. }
  4589. }
  4590. void checkHandshakeCountdown(uint8_t gun_index)
  4591. {
  4592. switch (getTargetChargingInfoData(gun_index)->SystemStatus)
  4593. {
  4594. case SYS_MODE_IDLE:
  4595. if (ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
  4596. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4597. else
  4598. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout())
  4599. - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4600. if (!ShmCharger->isAuthrizing)
  4601. {
  4602. if (ocpp_get_isRemoteStartWait())
  4603. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout())
  4604. - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4605. else
  4606. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4607. }
  4608. break;
  4609. case SYS_MODE_PREPARING:
  4610. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout())
  4611. - getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]));
  4612. break;
  4613. case SYS_MODE_RESERVATION:
  4614. if (ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
  4615. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4616. else
  4617. ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout())
  4618. - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
  4619. break;
  4620. }
  4621. }
  4622. void checkRfidAuthrize()
  4623. {
  4624. static uint8_t isCheckdResult = FALSE;
  4625. uint8_t isStartByWrongReservation = NO;
  4626. if (!ShmCharger->isAuthrizing)
  4627. {
  4628. // Read RFID
  4629. if (GetCardSerialNumber() != FAIL)
  4630. {
  4631. uint8_t isSnStart = FALSE;
  4632. uint8_t bufferRFID[ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId)];
  4633. if (ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  4634. {
  4635. // Big endian
  4636. switch (rfid.snType)
  4637. {
  4638. case RFID_SN_TYPE_6BYTE:
  4639. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0],
  4640. rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4],
  4641. rfid.currentCard[5]);
  4642. break;
  4643. case RFID_SN_TYPE_7BYTE:
  4644. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0],
  4645. rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4],
  4646. rfid.currentCard[5], rfid.currentCard[6]);
  4647. break;
  4648. case RFID_SN_TYPE_8BYTE:
  4649. sprintf((char *) bufferRFID, "%02X%02X%02X%02X%02X%02X%02X%02X",
  4650. rfid.currentCard[0], rfid.currentCard[1],
  4651. rfid.currentCard[2], rfid.currentCard[3],
  4652. rfid.currentCard[4], rfid.currentCard[5],
  4653. rfid.currentCard[6], rfid.currentCard[7]);
  4654. break;
  4655. case RFID_SN_TYPE_10BYTE:
  4656. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0],
  4657. rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4],
  4658. rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8],
  4659. rfid.currentCard[9]);
  4660. break;
  4661. case RFID_SN_TYPE_4BYTE:
  4662. default:
  4663. sprintf((char*) bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1],
  4664. rfid.currentCard[2], rfid.currentCard[3]);
  4665. break;
  4666. }
  4667. }
  4668. else
  4669. {
  4670. // Little endian
  4671. switch (rfid.snType)
  4672. {
  4673. case RFID_SN_TYPE_6BYTE:
  4674. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5],
  4675. rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1],
  4676. rfid.currentCard[0]);
  4677. break;
  4678. case RFID_SN_TYPE_7BYTE:
  4679. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6],
  4680. rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2],
  4681. rfid.currentCard[1], rfid.currentCard[0]);
  4682. break;
  4683. case RFID_SN_TYPE_8BYTE:
  4684. sprintf((char *) bufferRFID,
  4685. "%02X%02X%02X%02X%02X%02X%02X%02X",
  4686. rfid.currentCard[7], rfid.currentCard[6],
  4687. rfid.currentCard[5], rfid.currentCard[4],
  4688. rfid.currentCard[3], rfid.currentCard[2],
  4689. rfid.currentCard[1], rfid.currentCard[0]);
  4690. break;
  4691. case RFID_SN_TYPE_10BYTE:
  4692. sprintf((char*) bufferRFID, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9],
  4693. rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5],
  4694. rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1],
  4695. rfid.currentCard[0]);
  4696. break;
  4697. case RFID_SN_TYPE_4BYTE:
  4698. default:
  4699. sprintf((char*) bufferRFID, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2],
  4700. rfid.currentCard[1], rfid.currentCard[0]);
  4701. break;
  4702. }
  4703. }
  4704. DEBUG_INFO("Authorize request User Id : %s\n", bufferRFID);
  4705. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4706. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  4707. // Check SN already start
  4708. if (isMatchStartUser() || isMatchPresentUser()) //lwtest
  4709. {
  4710. //Already entry charging
  4711. if (strcmp((char*) getTargetChargingInfoData(ShmCharger->gun_selectd)->StartUserId, "") != 0)
  4712. DEBUG_INFO("%s running on connector-%02d.\n",
  4713. getTargetChargingInfoData(ShmCharger->gun_selectd)->StartUserId, ShmCharger->gun_selectd);
  4714. //Already entry preparing
  4715. else if (strcmp((char*) ShmCharger->gun_info[ShmCharger->gun_selectd].AuthAcceptUserId, "") != 0)
  4716. DEBUG_INFO("%s ready running on connector-%02d.\n",
  4717. ShmCharger->gun_info[ShmCharger->gun_selectd].AuthAcceptUserId, ShmCharger->gun_selectd);
  4718. isSnStart = TRUE;
  4719. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  4720. ocpp_set_auth_conf(ON);
  4721. setLedMotion(ShmCharger->gun_selectd, LED_ACTION_RFID_PASS);
  4722. setSpeaker(ON, SPEAKER_SHORT);
  4723. sleep(3);
  4724. //break;
  4725. }
  4726. else if (isNeedToAuth() == FALSE)
  4727. {
  4728. //lwtest
  4729. isSnStart = TRUE;
  4730. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = OFF;
  4731. ocpp_set_auth_conf(ON);
  4732. }
  4733. else
  4734. {
  4735. //lwtest .. for GL testing control.
  4736. for (uint8_t index = 0; index < modelnameInfo.GetGunCount; index++)
  4737. {
  4738. if (ShmCharger->gun_info[index].isCmdToPrepareMode == ON)
  4739. {
  4740. ShmCharger->gun_info[index].rfidReq = ON;
  4741. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00,
  4742. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4743. return;
  4744. }
  4745. }
  4746. }
  4747. // Request authorize if isSnStart is false
  4748. if (!isSnStart)
  4749. {
  4750. DEBUG_INFO("RFID start. [ %s ]\n", bufferRFID);
  4751. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, bufferRFID,
  4752. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4753. refreshStartTimer(&startTime[ShmCharger->gun_selectd][TMR_IDX_AUTH]); //lwtest
  4754. ocpp_set_auth_conf(OFF);
  4755. ocpp_set_auth_req(ON, "ISO14443");
  4756. setLedMotion(0, LED_ACTION_AUTHED);
  4757. ShmCharger->isAuthrizing = TRUE;
  4758. ShmCharger->isGetAuthResult = FALSE;
  4759. isCheckdResult = FALSE;
  4760. }
  4761. }
  4762. }
  4763. else //isAuthrizing = true
  4764. {
  4765. // Wait authorize result
  4766. if (!ocpp_get_auth_conf()
  4767. && (getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH))
  4768. {
  4769. // Authorization timeout process.
  4770. DEBUG_WARN("Authorize timeout [ %s ]!!!\n", ShmSysConfigAndInfo->SysConfig.UserId);
  4771. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4772. {
  4773. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4774. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  4775. }
  4776. //lwtest
  4777. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4778. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  4779. sleep(3);
  4780. ShmCharger->isAuthrizing = FALSE;
  4781. }
  4782. else
  4783. {
  4784. if (ocpp_get_auth_conf()
  4785. || (!ocpp_get_connection_status()
  4786. && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)
  4787. || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE)))
  4788. || (!ocpp_get_connection_status()
  4789. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)
  4790. && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > 2)))
  4791. {
  4792. if (ocpp_get_auth_result(NO)
  4793. || (!ocpp_get_connection_status()
  4794. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE))
  4795. || (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS)
  4796. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  4797. {
  4798. if (!isCheckdResult)
  4799. {
  4800. refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
  4801. ShmCharger->isGetAuthResult = TRUE;
  4802. isCheckdResult = TRUE;
  4803. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") == 0)
  4804. {
  4805. ocpp_set_auth_conf(ON);
  4806. }
  4807. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4808. {
  4809. if ((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_RESERVATION)
  4810. && (strcmp((char*) ShmSysConfigAndInfo->SysConfig.UserId,
  4811. (char*) ShmOCPP16Data->ReserveNow[gun_index].IdTag) != 0))
  4812. {
  4813. sleep(1);
  4814. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4815. isStartByWrongReservation = YES;
  4816. DEBUG_INFO("Not match: UserId = [%s] / Reserve IdTag = [%s] \n",
  4817. ShmSysConfigAndInfo->SysConfig.UserId,
  4818. ShmOCPP16Data->ReserveNow[ShmCharger->gun_selectd].IdTag);
  4819. DEBUG_INFO("Authorize fail.\n");
  4820. //lwtest
  4821. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00,
  4822. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4823. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  4824. }
  4825. else
  4826. {
  4827. //lwtest
  4828. if (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_IDLE)
  4829. {
  4830. DEBUG_INFO("Authorize pass (gun %d) [ %s ].\n", gun_index,
  4831. ShmSysConfigAndInfo->SysConfig.UserId);
  4832. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  4833. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_B;
  4834. ShmCharger->gun_selectd = gun_index;
  4835. memcpy(ShmCharger->gun_info[gun_index].AuthAcceptUserId,
  4836. ShmSysConfigAndInfo->SysConfig.UserId,
  4837. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4838. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00,
  4839. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4840. break;
  4841. }
  4842. setSpeaker(ON, SPEAKER_SHORT);
  4843. }
  4844. }
  4845. }
  4846. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4847. checkHandshakeCountdown(gun_index);
  4848. if ((getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < (ocpp_get_connection_timeout()))
  4849. && (isStartByWrongReservation != YES))
  4850. {
  4851. if (GetCardSerialNumber() != FAIL)
  4852. {
  4853. if (isMatchPresentUser())
  4854. {
  4855. DEBUG_INFO("Cancel present user.\n");
  4856. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4857. {
  4858. setLedMotion(gun_index, LED_ACTION_IDLE);
  4859. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4860. }
  4861. setSpeaker(ON, SPEAKER_SHORT);
  4862. ShmCharger->isAuthrizing = FALSE;
  4863. }
  4864. }
  4865. else
  4866. {
  4867. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4868. {
  4869. if (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < 3)
  4870. setLedMotion(gun_index, LED_ACTION_RFID_PASS);
  4871. else
  4872. setLedMotion(gun_index, LED_ACTION_AUTHED);
  4873. if ((((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_IDLE)
  4874. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_RESERVATION))
  4875. && (((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_B)
  4876. || (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C))
  4877. || (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)))
  4878. || ((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_PREPARING)
  4879. && (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_B)
  4880. && (ShmCharger->gun_selectd == gun_index))
  4881. || (((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING)
  4882. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_TERMINATING))
  4883. && (ShmCharger->gun_selectd == gun_index)))
  4884. {
  4885. DEBUG_INFO("Connector-%02d action detect.\n", gun_index);
  4886. ShmCharger->isAuthrizing = FALSE;
  4887. ShmCharger->gun_info[gun_index].rfidReq = ON;
  4888. break;
  4889. }
  4890. }
  4891. }
  4892. }
  4893. else
  4894. {
  4895. if (isStartByWrongReservation != YES)
  4896. DEBUG_INFO("Connector action detect timeout. \n");
  4897. else
  4898. DEBUG_INFO("User Id and reservation IdTag is not match. \n");
  4899. ShmCharger->isAuthrizing = FALSE;
  4900. ShmCharger->isGetAuthResult = TRUE;
  4901. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4902. {
  4903. if (isStartByWrongReservation != YES)
  4904. {
  4905. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  4906. sleep(6);
  4907. }
  4908. else
  4909. {
  4910. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  4911. sleep(3);
  4912. }
  4913. setLedMotion(gun_index, LED_ACTION_IDLE);
  4914. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  4915. }
  4916. }
  4917. }
  4918. else
  4919. {
  4920. DEBUG_INFO("Authorize fail.\n");
  4921. ShmCharger->isAuthrizing = FALSE;
  4922. ShmCharger->isGetAuthResult = TRUE;
  4923. //lwtest
  4924. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00,
  4925. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  4926. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4927. {
  4928. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  4929. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  4930. }
  4931. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  4932. sleep(3);
  4933. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  4934. {
  4935. if ((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_PREPARING))
  4936. setLedMotion(gun_index, LED_ACTION_AUTHED);
  4937. else
  4938. setLedMotion(gun_index, LED_ACTION_IDLE);
  4939. }
  4940. }
  4941. }
  4942. }
  4943. }
  4944. }
  4945. //======================================================
  4946. // Main process
  4947. //======================================================
  4948. int main(void)
  4949. {
  4950. //==================================================
  4951. // Create all share memory
  4952. //==================================================
  4953. if (CreatShareMemory() == 0)
  4954. {
  4955. DEBUG_ERROR("CreatShareMemory NG\n");
  4956. if (ShmStatusCodeData != NULL)
  4957. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = TRUE;
  4958. sleep(5);
  4959. system("reboot -f");
  4960. sleep(5);
  4961. system("reboot -f");
  4962. }
  4963. else
  4964. {
  4965. modelnameInfo.GetGunCount = 1;
  4966. DEBUG_INFO("CreatShareMemory OK\n");
  4967. }
  4968. for (int gun_index = 0; gun_index < MAX_GUN_NUM; gun_index++)
  4969. {
  4970. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[gun_index].SystemStatus = 0x00;
  4971. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[gun_index].PreviousSystemStatus = 0xff;
  4972. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[gun_index].Index = 0xff;
  4973. ShmSysConfigAndInfo->SysInfo.CcsChargingData[gun_index].SystemStatus = 0x00;
  4974. ShmSysConfigAndInfo->SysInfo.CcsChargingData[gun_index].PreviousSystemStatus = 0xff;
  4975. ShmSysConfigAndInfo->SysInfo.CcsChargingData[gun_index].Index = 0xff;
  4976. ShmSysConfigAndInfo->SysInfo.GbChargingData[gun_index].SystemStatus = 0x00;
  4977. ShmSysConfigAndInfo->SysInfo.GbChargingData[gun_index].PreviousSystemStatus = 0xff;
  4978. ShmSysConfigAndInfo->SysInfo.GbChargingData[gun_index].Index = 0xff;
  4979. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = 0x00;
  4980. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  4981. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = 0xff;
  4982. }
  4983. //==================================================
  4984. // Main loop
  4985. //==================================================
  4986. for (;;)
  4987. {
  4988. //==============================================
  4989. // Synchronize share memory from OCPP struct
  4990. //==============================================
  4991. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ocpp_get_connection_status();
  4992. //==============================================
  4993. // Period check for 10 seconds
  4994. //==============================================
  4995. if ((getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) > TIMEOUT_SPEC_TASK_CHECK)
  4996. || (getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) < 0))
  4997. {
  4998. //==============================================
  4999. // Check task processing
  5000. //==============================================
  5001. if (getTargetChargingInfoData(0)->SystemStatus != SYS_MODE_BOOTING
  5002. && getTargetChargingInfoData(1)->SystemStatus != SYS_MODE_BOOTING) //lwtest
  5003. checkTask();
  5004. //==============================================
  5005. // Check connection timeout specification
  5006. //==============================================
  5007. checkConnectionTimeout();
  5008. refreshStartTimer(&startTime[0][TMR_IDX_CHECK_TASK]);
  5009. }
  5010. //==============================================
  5011. // Something need run in Idle mode
  5012. //==============================================
  5013. if (((getTargetChargingInfoData(0)->SystemStatus == SYS_MODE_IDLE)
  5014. || (getTargetChargingInfoData(0)->SystemStatus == SYS_MODE_ALARM)
  5015. || (getTargetChargingInfoData(0)->SystemStatus == SYS_MODE_DEBUG))
  5016. && (modelnameInfo.GetGunCount > 1 ? ((getTargetChargingInfoData(1)->SystemStatus == SYS_MODE_IDLE)
  5017. || (getTargetChargingInfoData(1)->SystemStatus == SYS_MODE_ALARM)
  5018. || (getTargetChargingInfoData(1)->SystemStatus == SYS_MODE_DEBUG)) : TRUE))
  5019. {
  5020. // Check restore factory setting request
  5021. if (ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  5022. {
  5023. sleep(5);
  5024. system("cd /root;./Module_FactoryConfig -m");
  5025. system("rm -f /Storage/OCPP/OCPPConfiguration");
  5026. system("sync");
  5027. sleep(5);
  5028. system("reboot -f");
  5029. sleep(5);
  5030. system("reboot -f");
  5031. }
  5032. // Check upgrade firmware request
  5033. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate || ocpp_get_update_firmware_req())
  5034. {
  5035. // If available memory too small, free memory cache first
  5036. if (getAvailableMemory() < (200 * 1024 * 1024))
  5037. {
  5038. DEBUG_INFO("Available memory (%.2f Bytes) less than 200 MBytes, free cache first.\n",
  5039. getAvailableMemory() / (1024 * 1024.0));
  5040. system("echo 3 > /proc/sys/vm/drop_caches");
  5041. }
  5042. ShmCharger->isUpdateSuccess = NO;
  5043. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  5044. {
  5045. setChargerMode(gun_index, SYS_MODE_UPDATE);
  5046. }
  5047. }
  5048. }
  5049. //==============================================
  5050. // Check remote reset request
  5051. //==============================================
  5052. checkReset();
  5053. //==============================================
  5054. // Check RFID authorization
  5055. //==============================================
  5056. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  5057. {
  5058. // The user is not allowed to use RFID when the system change to under those modes.
  5059. if ((getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_ALARM)
  5060. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_FAULT)
  5061. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_DEBUG)
  5062. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_UPDATE)
  5063. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_BOOTING)
  5064. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_COMPLETE)
  5065. || (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_MAINTAIN)
  5066. || (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE))
  5067. {
  5068. }
  5069. else
  5070. {
  5071. checkRfidAuthrize();
  5072. }
  5073. }
  5074. //==============================================
  5075. // Connector loop
  5076. //==============================================
  5077. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  5078. {
  5079. /*
  5080. * TODO:
  5081. * 1. Simulate voltage, current, power consumption
  5082. */
  5083. if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_OUTPUT_TYPE] == 'A')
  5084. {
  5085. static uint64_t tmpPowerConsumption[3];
  5086. static long long tsLast[3], tsNow[3];
  5087. // Voltage
  5088. ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12 =
  5089. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5090. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5091. {
  5092. ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23 =
  5093. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5094. ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31 =
  5095. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5096. }
  5097. getTargetChargingInfoData(gun_index)->PresentChargingVoltage = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  5098. ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  5099. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5100. {
  5101. getTargetChargingInfoData(gun_index)->PresentChargingVoltageL2 = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  5102. getTargetChargingInfoData(gun_index)->PresentChargingVoltageL3 = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  5103. ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  5104. ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  5105. }
  5106. else
  5107. {
  5108. ShmSysConfigAndInfo->SysInfo.InputVoltageS = 0;
  5109. ShmSysConfigAndInfo->SysInfo.InputVoltageT = 0;
  5110. }
  5111. //lwtest
  5112. // if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5113. // printf("=== AC 3 phase R=%.2f v | S=%.2f v | T%.2f v\n", ShmSysConfigAndInfo->SysInfo.InputVoltageR,
  5114. // ShmSysConfigAndInfo->SysInfo.InputVoltageS, ShmSysConfigAndInfo->SysInfo.InputVoltageT);
  5115. // else
  5116. // printf("=== AC 1 phase R=%.2f v\n", ShmSysConfigAndInfo->SysInfo.InputVoltageR);
  5117. //------------------------------------------------------------------------------------
  5118. // Current
  5119. uint16_t _targetCurrent = ShmCharger->gun_info[gun_index].targetCurrent/ShmSysConfigAndInfo->SysConfig.AcPhaseCount;
  5120. getTargetChargingInfoData(gun_index)->PresentChargingCurrent =
  5121. (float) (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5122. ? (((rand() % 10) + ((_targetCurrent * 10) - SIM_DEVIATION)) / 10.0) : 0);
  5123. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5124. {
  5125. getTargetChargingInfoData(gun_index)->PresentChargingCurrentL2 =
  5126. (float) (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5127. ? (((rand() % 10) + ((_targetCurrent * 10) - SIM_DEVIATION)) / 10.0) : 0);
  5128. getTargetChargingInfoData(gun_index)->PresentChargingCurrentL3 =
  5129. (float) (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5130. ? (((rand() % 10) + ((_targetCurrent * 10) - SIM_DEVIATION)) / 10.0) : 0);
  5131. }
  5132. //lwtest
  5133. if (getTargetChargingInfoData(gun_index)->PresentChargingCurrent < 0.0)
  5134. {
  5135. //lwtest for debug
  5136. DEBUG_INFO("LW debug message : PresentChargingCurrent < 0, value = %.2f A\n",
  5137. getTargetChargingInfoData(gun_index)->PresentChargingCurrent);
  5138. getTargetChargingInfoData(gun_index)->PresentChargingCurrent = 0;
  5139. }
  5140. //lwtest
  5141. // if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5142. // printf("=== AC 3 phase Gun %d Current I1=%.2f A| I2=%.2f A| I3=%.2f A\n", gun_index,
  5143. // getTargetChargingInfoData(gun_index)->PresentChargingCurrent,
  5144. // getTargetChargingInfoData(gun_index)->PresentChargingCurrentL2,
  5145. // getTargetChargingInfoData(gun_index)->PresentChargingCurrentL3);
  5146. // else
  5147. // printf("=== AC 1 phase Gun %d Current=%.2f A\n", gun_index,
  5148. // getTargetChargingInfoData(gun_index)->PresentChargingCurrent);
  5149. //------------------------------------------------------------------------------------
  5150. // Energy
  5151. tsNow[gun_index] = current_timestamp();
  5152. tmpPowerConsumption[gun_index] += (uint64_t) ((getTargetChargingInfoData(gun_index)->PresentChargingVoltage
  5153. * getTargetChargingInfoData(gun_index)->PresentChargingCurrent)
  5154. * ((tsNow[gun_index] - tsLast[gun_index]) / 360000.0));
  5155. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  5156. {
  5157. tmpPowerConsumption[gun_index] += (uint64_t) ((getTargetChargingInfoData(gun_index)->PresentChargingVoltageL2
  5158. * getTargetChargingInfoData(gun_index)->PresentChargingCurrentL2)
  5159. * ((tsNow[gun_index] - tsLast[gun_index]) / 360000.0));
  5160. tmpPowerConsumption[gun_index] += (uint64_t) ((getTargetChargingInfoData(gun_index)->PresentChargingVoltageL3
  5161. * getTargetChargingInfoData(gun_index)->PresentChargingCurrentL3)
  5162. * ((tsNow[gun_index] - tsLast[gun_index]) / 360000.0));
  5163. }
  5164. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption = tmpPowerConsumption[gun_index];
  5165. tsLast[gun_index] = tsNow[gun_index];
  5166. // Connector temperature
  5167. getTargetChargingInfoData(gun_index)->ConnectorTemp =
  5168. (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5169. ? ((rand() % 3) + ((50) - 3)) : ((rand() % 3) + ((25) - 3)));
  5170. }
  5171. else
  5172. {
  5173. //DC type
  5174. static uint64_t tmpPowerConsumption[3];
  5175. static long long tsLast[3], tsNow[3];
  5176. // Input Voltage
  5177. ShmSysConfigAndInfo->SysInfo.InputVoltageR =
  5178. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5179. ShmSysConfigAndInfo->SysInfo.InputVoltageS =
  5180. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5181. ShmSysConfigAndInfo->SysInfo.InputVoltageT =
  5182. (float) (((rand() % 10) + ((AC_OUTPUT_VOL * 10) - SIM_DEVIATION)) / 10.0);
  5183. //lwtest
  5184. // printf("=== DC input Voltage R=%.2f v| S=%.2f v| T=%.2f v\n",
  5185. // ShmSysConfigAndInfo->SysInfo.InputVoltageR, ShmSysConfigAndInfo->SysInfo.InputVoltageS,
  5186. // ShmSysConfigAndInfo->SysInfo.InputVoltageT);
  5187. //------------------------------------------------------------------------------------
  5188. // Output Voltage
  5189. getTargetChargingInfoData(gun_index)->PresentChargingVoltage =
  5190. (float) (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5191. ? (((rand() % 10) + ((DC_SIMULATE_OUTPUT_VOL * 10) - 100)) / 10.0) : 0);
  5192. //lwtest
  5193. // printf("=== DC Gun %d Present Charging Voltage %.2f v\n", gun_index,
  5194. // getTargetChargingInfoData(gun_index)->PresentChargingVoltage);
  5195. //------------------------------------------------------------------------------------
  5196. // Current
  5197. getTargetChargingInfoData(gun_index)->PresentChargingCurrent =
  5198. (float) (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5199. ? (((rand() % 10) + ((ShmCharger->gun_info[gun_index].targetCurrent * 10) - SIM_DEVIATION)) / 10.0) : 0);
  5200. //lwtest
  5201. if (getTargetChargingInfoData(gun_index)->PresentChargingCurrent < 0.0)
  5202. {
  5203. //lwtest for debug
  5204. DEBUG_INFO("LW debug message : PresentChargingCurrent < 0, value = %.2f A\n",
  5205. getTargetChargingInfoData(gun_index)->PresentChargingCurrent);
  5206. getTargetChargingInfoData(gun_index)->PresentChargingCurrent = 0;
  5207. }
  5208. // //lwtest
  5209. // printf("=== DC Gun %d Present Charging Current %.2f A\n", gun_index,
  5210. // getTargetChargingInfoData(gun_index)->PresentChargingCurrent);
  5211. //------------------------------------------------------------------------------------
  5212. // Energy
  5213. tsNow[gun_index] = current_timestamp();
  5214. tmpPowerConsumption[gun_index] += (uint64_t) ((getTargetChargingInfoData(gun_index)->PresentChargingVoltage
  5215. * getTargetChargingInfoData(gun_index)->PresentChargingCurrent)
  5216. * ((tsNow[gun_index] - tsLast[gun_index]) / 360000.0));
  5217. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption = tmpPowerConsumption[gun_index];
  5218. tsLast[gun_index] = tsNow[gun_index];
  5219. //lwtest
  5220. // printf("=== DC Gun %d power_consumption %.2lld v\n", gun_index, ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption);
  5221. //------------------------------------------------------------------------------------
  5222. //Power Offered , Current Offered
  5223. //lwtest
  5224. if (isTwoGunInChargingMode())
  5225. {
  5226. getTargetChargingInfoData(gun_index)->CurrentOffered = 120 * 10 * 0.5;
  5227. getTargetChargingInfoData(gun_index)->PowerOffered = (modelnameInfo.ratedPower / 100.0) * 0.5;
  5228. }
  5229. else
  5230. {
  5231. getTargetChargingInfoData(gun_index)->CurrentOffered = 120 * 10;
  5232. getTargetChargingInfoData(gun_index)->PowerOffered = (modelnameInfo.ratedPower / 100.0);
  5233. }
  5234. //------------------------------------------------------------------------------------
  5235. // SOC
  5236. if (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_IDLE)
  5237. getTargetChargingInfoData(gun_index)->EvBatterySoc = 0;
  5238. else
  5239. {
  5240. if (getDiffSecNow(startTime[gun_index][TMR_IDX_SIMULATION]) > TIMEOUT_SPEC_SIMULATION)
  5241. {
  5242. if (getTargetChargingInfoData(gun_index)->EvBatterySoc < 100)
  5243. getTargetChargingInfoData(gun_index)->EvBatterySoc += 1;
  5244. refreshStartTimer(&startTime[gun_index][TMR_IDX_SIMULATION]);
  5245. }
  5246. }
  5247. //------------------------------------------------------------------------------------
  5248. // Connector temperature
  5249. getTargetChargingInfoData(gun_index)->ConnectorTemp =
  5250. (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5251. ? ((rand() % 3) + ((80) - 3)) : ((rand() % 3) + ((25) - 3)));
  5252. //------------------------------------------------------------------------------------
  5253. // System fan RPM
  5254. getTargetChargingInfoData(gun_index)->ConnectorTemp =
  5255. (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_CHARGING
  5256. ? ((rand() % 3) + ((50) - 3)) : ((rand() % 3) + ((25) - 3))) + 60;
  5257. //------------------------------------------------------------------------------------
  5258. // Remain charging time
  5259. getTargetChargingInfoData(gun_index)->RemainChargingDuration =
  5260. (100 - getTargetChargingInfoData(gun_index)->EvBatterySoc) * 10;
  5261. //------------------------------------------------------------------------------------
  5262. // EV battery max voltage
  5263. getTargetChargingInfoData(gun_index)->EvBatteryMaxVoltage = DC_SIMULATE_OUTPUT_VOL;
  5264. // EV battery target voltage
  5265. getTargetChargingInfoData(gun_index)->EvBatterytargetVoltage = DC_SIMULATE_OUTPUT_VOL - SIM_DEVIATION;
  5266. }
  5267. /*
  5268. * TODO:
  5269. * 1. Synchronize current rating value from MCU
  5270. */
  5271. if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_OUTPUT_TYPE] == 'A')
  5272. {
  5273. if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_TYPE] == 'X'
  5274. && strncmp((char*) &ShmSysConfigAndInfo->SysConfig.ModelName[RATING_POWER], "111", 3) == 0)
  5275. ShmSysConfigAndInfo->SysConfig.RatingCurrent = 48;
  5276. else if (ShmSysConfigAndInfo->SysConfig.ModelName[EVSE_TYPE] == 'X'
  5277. && strncmp((char*) &ShmSysConfigAndInfo->SysConfig.ModelName[RATING_POWER], "221", 3) == 0)
  5278. ShmSysConfigAndInfo->SysConfig.RatingCurrent = 32;
  5279. else
  5280. ShmSysConfigAndInfo->SysConfig.RatingCurrent = modelnameInfo.ratedPower / AC_OUTPUT_VOL;
  5281. }
  5282. else
  5283. ShmSysConfigAndInfo->SysConfig.RatingCurrent = modelnameInfo.ratedPower / DC_SIMULATE_OUTPUT_VOL;
  5284. //lwtest
  5285. // printf("=== Rating Current %d, ratedPower = %d\n", ShmSysConfigAndInfo->SysConfig.RatingCurrent,
  5286. // modelnameInfo.ratedPower);
  5287. //==========================================
  5288. // Synchronize present charging power
  5289. //==========================================
  5290. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  5291. {
  5292. if (getRelay(gun_index) == ON)
  5293. {
  5294. getTargetChargingInfoData(gun_index)->PresentChargingPower =
  5295. (getTargetChargingInfoData(gun_index)->PresentChargingVoltage
  5296. * getTargetChargingInfoData(gun_index)->PresentChargingCurrent) / 1000;
  5297. //min power
  5298. //lwtest
  5299. if (getTargetChargingInfoData(gun_index)->PresentChargingPower <= (ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.01))
  5300. getTargetChargingInfoData(gun_index)->PresentChargingPower = ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.01;
  5301. // printf("===Gun %d 1 Phase relay ON Rating Pow %.2f w\n", gun_index,
  5302. // getTargetChargingInfoData(gun_index)->PresentChargingPower);
  5303. }
  5304. else
  5305. getTargetChargingInfoData(gun_index)->PresentChargingPower = 0;
  5306. }
  5307. else
  5308. {
  5309. if (getRelay(gun_index) == ON)
  5310. {
  5311. getTargetChargingInfoData(gun_index)->PresentChargingPower = (((getTargetChargingInfoData(gun_index)->PresentChargingVoltage
  5312. * getTargetChargingInfoData(gun_index)->PresentChargingCurrent) / 1000)
  5313. + ((getTargetChargingInfoData(gun_index)->PresentChargingVoltageL2
  5314. * getTargetChargingInfoData(gun_index)->PresentChargingCurrentL2) / 1000)
  5315. + ((getTargetChargingInfoData(gun_index)->PresentChargingVoltageL3
  5316. * getTargetChargingInfoData(gun_index)->PresentChargingCurrentL3) / 1000));
  5317. //lwtest
  5318. //min power
  5319. if (getTargetChargingInfoData(gun_index)->PresentChargingPower <= (ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.01))
  5320. getTargetChargingInfoData(gun_index)->PresentChargingPower = ShmSysConfigAndInfo->SysConfig.RatingCurrent * 0.01;
  5321. //lwtest
  5322. // printf("===Gun %d 3 Phase relay ON Rating Pow %.2f\n", gun_index,
  5323. // getTargetChargingInfoData(gun_index)->PresentChargingPower);
  5324. }
  5325. else
  5326. {
  5327. getTargetChargingInfoData(gun_index)->PresentChargingPower = 0;
  5328. }
  5329. }
  5330. //==========================================
  5331. // Check initialization "PASS" or "FAIL"
  5332. //==========================================
  5333. if (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
  5334. {
  5335. // Alarm event check
  5336. if ((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode > 0)
  5337. || (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode > 0)
  5338. || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
  5339. {
  5340. if (getTargetChargingInfoData(gun_index)->SystemStatus != SYS_MODE_ALARM)
  5341. {
  5342. if (getTargetChargingInfoData(gun_index)->SystemStatus != SYS_MODE_UPDATE)
  5343. {
  5344. setChargerMode(gun_index, SYS_MODE_ALARM);
  5345. }
  5346. }
  5347. }
  5348. }
  5349. //==========================================
  5350. // Reservation request check
  5351. //==========================================
  5352. checkReservation(gun_index);
  5353. //==========================================
  5354. // Change availability check
  5355. //==========================================
  5356. //lwtest
  5357. if (getTargetChargingInfoData(gun_index)->SystemStatus == SYS_MODE_BOOTING)
  5358. {
  5359. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  5360. DEBUG_INFO("connector-0 %d operative state %d\n", gun_index,
  5361. ShmCharger->gun_info[gun_index].isOperactive);
  5362. }
  5363. checkAvailability(gun_index);
  5364. if (ShmCharger->gun_info[gun_index].isOperactive)
  5365. {
  5366. if (isMode(gun_index, SYS_MODE_MAINTAIN))
  5367. setChargerMode(gun_index, SYS_MODE_IDLE);
  5368. }
  5369. else
  5370. {
  5371. if (isMode(gun_index, SYS_MODE_IDLE))
  5372. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  5373. }
  5374. //==========================================
  5375. // Unlock Connector signal check
  5376. //==========================================
  5377. checkUnlocker(gun_index);
  5378. //==========================================
  5379. // Clean isRemoteStartWait flag
  5380. //==========================================
  5381. if (ocpp_get_isRemoteStartWait())
  5382. {
  5383. if (ShmCharger->gun_info[gun_index].isRemoteStartWait == OFF)
  5384. {
  5385. ShmCharger->gun_info[gun_index].isRemoteStartWait = ON;
  5386. DEBUG_INFO("Remote start without connector id... \n");
  5387. }
  5388. refreshStartTimer(&startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]);
  5389. }
  5390. else
  5391. {
  5392. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]) > 5))
  5393. {
  5394. if (ShmCharger->gun_info[gun_index].isRemoteStartWait == ON)
  5395. {
  5396. ShmCharger->gun_info[gun_index].isRemoteStartWait = OFF;
  5397. DEBUG_INFO("Clean isRemoteStartWait flag... \n");
  5398. }
  5399. }
  5400. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]) > 10))
  5401. refreshStartTimer(&startTime[gun_index][TMR_IDX_CLEAN_REMOTE_START_WAIT]);
  5402. }
  5403. //==========================================
  5404. // Connector process
  5405. //==========================================
  5406. switch (getTargetChargingInfoData(gun_index)->SystemStatus)
  5407. {
  5408. case SYS_MODE_BOOTING:
  5409. if (isModeChange(gun_index))
  5410. {
  5411. uint8_t idxCHAdeMO = 0;
  5412. uint8_t idxCCS = 0;
  5413. uint8_t idxGB = 0;
  5414. uint8_t idxAC = 0;
  5415. DEBUG_INFO("========== SYS_MODE_BOOTING (%d) ========== \n", gun_index);
  5416. //CSU Initialization & task spawn
  5417. if ((Initialization(gun_index) != PASS) || (SpawnTask(gun_index) != PASS))
  5418. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  5419. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
  5420. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
  5421. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
  5422. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
  5423. refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
  5424. refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
  5425. for (int gun_index = 0; gun_index < modelnameInfo.GetGunCount; gun_index++)
  5426. {
  5427. if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_Chademo)
  5428. {
  5429. ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].Index = gun_index;
  5430. idxCHAdeMO ++;
  5431. }
  5432. else if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_CCS_2)
  5433. {
  5434. ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].Index = gun_index;
  5435. idxCCS ++;
  5436. }
  5437. else if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_GB)
  5438. {
  5439. ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGB].Index = gun_index;
  5440. idxGB ++;
  5441. }
  5442. else if (modelnameInfo.ParsingInfo[gun_index].GunType == Gun_Type_AC)
  5443. {
  5444. ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].Index = gun_index;
  5445. idxAC ++;
  5446. }
  5447. }
  5448. }
  5449. if (gun_index == 0)//lwtest
  5450. {
  5451. ShmCharger->gun_selectd = 0;
  5452. // Firmware version
  5453. get_firmware_version(gun_index);
  5454. // OCPP BootNotification info set
  5455. DEBUG_INFO("==========================================\n");
  5456. DEBUG_INFO("System ID: %s\n", ShmSysConfigAndInfo->SysConfig.SystemId);
  5457. DEBUG_INFO("==========================================\n");
  5458. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  5459. ocpp_boot_info_sync();
  5460. DEBUG_INFO("OcppServerURL: %s\n", ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  5461. DEBUG_INFO("ChargeBoxId: %s\n", ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  5462. DEBUG_INFO("ChargePointVendor: %s\n", ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  5463. DEBUG_INFO("==========================================\n");
  5464. // Set max current to rating current
  5465. if (modelnameInfo.GetGunCount > 1)
  5466. {
  5467. ShmCharger->gun_info[0].primaryMcuCp_Pwn_Duty.max_current = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  5468. ShmCharger->gun_info[1].primaryMcuCp_Pwn_Duty.max_current = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  5469. }
  5470. else
  5471. {
  5472. ShmCharger->gun_info[0].primaryMcuCp_Pwn_Duty.max_current = ShmSysConfigAndInfo->SysConfig.RatingCurrent;
  5473. }
  5474. // Default Ethernet / Wifi / 4G to 1:disconnected
  5475. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5476. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  5477. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  5478. //lwtest
  5479. switch (ShmSysConfigAndInfo->SysConfig.ModelName[INTERNET_TYPE])
  5480. {
  5481. case 'E':
  5482. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5483. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  5484. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  5485. break;
  5486. case 'W':
  5487. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5488. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  5489. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = OFF;
  5490. break;
  5491. case 'T':
  5492. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5493. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = OFF;
  5494. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  5495. break;
  5496. case 'D':
  5497. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  5498. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  5499. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  5500. break;
  5501. }
  5502. // If Web Server OPCC URL is empty kill Module_OcppBackend
  5503. if ((strcmp((char *) &ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") == 0))
  5504. {
  5505. if (strcmp((char *) &ShmSysConfigAndInfo->SysConfig.OcppServerURL, "") == 0)
  5506. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  5507. system("pkill OcppBackend");
  5508. }
  5509. // The system identifies 1 phase or 3 phases depending on the model name
  5510. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = getAcPhaseCount();
  5511. }
  5512. setChargerMode(gun_index, SYS_MODE_IDLE);
  5513. //lwtest .. for GL testing control.
  5514. ShmCharger->gun_info[gun_index].isCmdToPrepareMode = OFF;
  5515. break;
  5516. case SYS_MODE_IDLE:
  5517. if (isModeChange(gun_index))
  5518. {
  5519. DEBUG_INFO("========== SYS_MODE_IDLE (%d) ========== \n", gun_index);
  5520. setLedMotion(gun_index, LED_ACTION_IDLE);
  5521. setRelay(gun_index, OFF);
  5522. setRequest(gun_index, OFF);
  5523. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  5524. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5525. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5526. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  5527. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  5528. ShmCharger->gun_info[gun_index].isGetEvCCID = OFF;
  5529. ocpp_set_remotestart(gun_index, OFF);
  5530. ocpp_set_remotestop(gun_index, OFF);
  5531. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  5532. getTargetChargingInfoData(gun_index)->PresentChargedDuration = 0;
  5533. presentChargedEnergyClear(gun_index);
  5534. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  5535. ocpp_set_unlocker_req(gun_index, OFF);
  5536. getTargetChargingInfoData(gun_index)->ReservationId = -1;
  5537. // Response StopTransactionConf
  5538. ocpp_set_stoptransaction_conf(gun_index, OFF);
  5539. memset(getTargetChargingInfoData(gun_index)->StartUserId, 0x00,
  5540. ARRAY_SIZE(getTargetChargingInfoData(gun_index)->StartUserId));
  5541. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00,
  5542. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5543. //lwtest
  5544. memset(ShmCharger->gun_info[gun_index].AuthAcceptUserId, 0x00,
  5545. ARRAY_SIZE(ShmCharger->gun_info[gun_index].AuthAcceptUserId));
  5546. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_IDLE;
  5547. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5548. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  5549. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  5550. ShmCharger->gun_info[gun_index].isDoEvReadyOnce = OFF;
  5551. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5552. //if(ShmCharger->isCcsEnable)system("pkill Module_CCS");
  5553. DB_Check_Record_Buf(localDb, gun_index);
  5554. ShmCharger->gun_info[gun_index].isEmergencyStopReport = OFF;
  5555. // Default previousData every time
  5556. previousData[gun_index].primaryMcuCp_Pwn_Duty = 0;
  5557. previousData[gun_index].targetCurrent = 0;
  5558. previousData[gun_index].current_limit = 0;
  5559. previousData[gun_index].current_limit = 0;
  5560. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_A;
  5561. getTargetChargingInfoData(gun_index)->ConnectorPlugIn = 0;
  5562. sleep(5);
  5563. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_B;
  5564. getTargetChargingInfoData(gun_index)->ConnectorPlugIn = 1;
  5565. }
  5566. if (((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_B))
  5567. || ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C))
  5568. || ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON))
  5569. || (ShmCharger->gun_info[gun_index].rfidReq == ON)
  5570. || (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  5571. || (ocpp_get_remotestart(gun_index) == ON)
  5572. || (getTargetChargingInfoData(gun_index)->schedule.isTriggerStart == ON)
  5573. || (ShmCharger->gun_info[gun_index].isGetEvCCID == ON)
  5574. || (ShmCharger->gun_info[gun_index].isCmdToPrepareMode == ON)) //lwtest
  5575. {
  5576. if ((ShmCharger->gun_info[gun_index].rfidReq == ON))
  5577. {
  5578. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_RFID;
  5579. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_ISO14443;
  5580. DEBUG_INFO("Start Method : RFID...\n");
  5581. }
  5582. else if (ocpp_get_remotestart(gun_index))
  5583. {
  5584. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_BACKEND;
  5585. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_Central;
  5586. ocpp_copy_userid_from_remotestart(gun_index);
  5587. setSpeaker(ON, SPEAKER_SHORT);
  5588. DEBUG_INFO("Start Method : BACKEND...\n");
  5589. }
  5590. else if (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  5591. {
  5592. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_BLE;
  5593. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_Local;
  5594. memcpy(ShmSysConfigAndInfo->SysConfig.UserId,
  5595. ShmCharger->gun_info[gun_index].bleLoginCentralId.id,
  5596. ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  5597. setSpeaker(ON, SPEAKER_SHORT);
  5598. DEBUG_INFO("Start Method : BLE...\n");
  5599. }
  5600. else if (ShmCharger->gun_info[gun_index].isGetEvCCID == ON)
  5601. {
  5602. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_EVCCID;
  5603. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_MacAddress;
  5604. setSpeaker(ON, SPEAKER_SHORT);
  5605. DEBUG_INFO("Start Method : EVCCID...\n");
  5606. }
  5607. else if (ShmCharger->gun_info[gun_index].isCmdToPrepareMode == ON)//lwtest .. for GL testing control
  5608. {
  5609. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_CMD;
  5610. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_B;
  5611. DEBUG_INFO("Start Method : COMMAND...\n");
  5612. }
  5613. else
  5614. {
  5615. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_FREE;
  5616. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_NoAuthorization;
  5617. ocpp_get_freevend_idtag(ShmSysConfigAndInfo->SysConfig.UserId);
  5618. setSpeaker(ON, SPEAKER_SHORT);
  5619. DEBUG_INFO("Start Method : FREE...\n");
  5620. }
  5621. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5622. ocpp_set_remotestart(gun_index, OFF);
  5623. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  5624. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  5625. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5626. getTargetChargingInfoData(gun_index)->schedule.isTriggerStart = OFF;
  5627. ShmCharger->gun_info[gun_index].isGetEvCCID = OFF;
  5628. // Get target current
  5629. ocpp_set_profile_req(gun_index, ON);
  5630. sleep(1);
  5631. checkChargingProfileLimit(gun_index, getTargetChargingInfoData(gun_index)->SystemStatus);
  5632. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  5633. }
  5634. else
  5635. {
  5636. }
  5637. break;
  5638. case SYS_MODE_AUTHORIZING:
  5639. if (isModeChange(gun_index))
  5640. {
  5641. DEBUG_INFO("========== SYS_MODE_AUTHORIZING (%d)========== \n", gun_index);
  5642. refreshStartTimer(&startTime[gun_index][TMR_IDX_AUTH]);
  5643. if (ocpp_isAuthorizeRemoteStart()
  5644. && (getTargetChargingInfoData(gun_index)->StartMethod == START_METHOD_BACKEND))
  5645. {
  5646. DEBUG_INFO("Remote start request authorize.\n");
  5647. ocpp_set_auth_req(ON, "ISO14443");
  5648. }
  5649. else if ((getTargetChargingInfoData(gun_index)->StartMethod == START_METHOD_EVCCID))
  5650. {
  5651. DEBUG_INFO("EVCCID request authorize.\n");
  5652. ocpp_set_auth_req(ON, "MacAddress");
  5653. }
  5654. }
  5655. if (getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  5656. {
  5657. // Authorization timeout process.
  5658. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  5659. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  5660. sleep(3);
  5661. setChargerMode(gun_index, SYS_MODE_IDLE);
  5662. DEBUG_WARN("Authorize timeout !!!\n");
  5663. }
  5664. else
  5665. {
  5666. switch (getTargetChargingInfoData(gun_index)->StartMethod)
  5667. {
  5668. case START_METHOD_BACKEND:
  5669. if (ocpp_isAuthorizeRemoteStart())
  5670. {
  5671. if (ocpp_get_auth_conf())
  5672. {
  5673. if (ocpp_get_auth_result(gun_index))
  5674. {
  5675. memcpy((char*) getTargetChargingInfoData(gun_index)->StartUserId,
  5676. ShmSysConfigAndInfo->SysConfig.UserId,
  5677. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5678. DEBUG_INFO("Authorize pass.\n");
  5679. setSpeaker(ON, SPEAKER_SHORT);
  5680. setLedMotion(gun_index, LED_ACTION_RFID_PASS);
  5681. sleep(4);
  5682. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5683. }
  5684. else
  5685. {
  5686. DEBUG_INFO("Authorize fail.\n");
  5687. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  5688. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  5689. sleep(3);
  5690. setChargerMode(gun_index, SYS_MODE_IDLE);
  5691. }
  5692. ocpp_set_auth_conf(OFF);
  5693. }
  5694. }
  5695. else
  5696. {
  5697. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5698. }
  5699. break;
  5700. case START_METHOD_EVCCID:
  5701. ShmCharger->gun_info[gun_index].resultAuthorization = UNKNOW_RFID;
  5702. if (ocpp_get_auth_conf()
  5703. || (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)
  5704. || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE)))
  5705. || (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)
  5706. && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > 2)))
  5707. {
  5708. if (ocpp_get_auth_result(gun_index) || (!ocpp_get_connection_status()
  5709. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE))
  5710. || (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS)
  5711. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  5712. {
  5713. memcpy((char*) getTargetChargingInfoData(gun_index)->StartUserId,
  5714. ShmSysConfigAndInfo->SysConfig.UserId,
  5715. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5716. ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
  5717. DEBUG_INFO("Authorize pass [EVCCID].\n");
  5718. setSpeaker(ON, SPEAKER_SHORT);
  5719. setLedMotion(gun_index, LED_ACTION_RFID_PASS);
  5720. sleep(1);
  5721. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5722. ShmCharger->gun_info[gun_index].isEvCCIDAuthorizeFail = NO;
  5723. }
  5724. else
  5725. {
  5726. ShmCharger->gun_info[gun_index].resultAuthorization = UNVALIDATED_RFID;
  5727. DEBUG_INFO("Authorize fail [EVCCID].\n");
  5728. setSpeaker(ON, SPEAKER_INTERVAL_3COUNT);
  5729. setLedMotion(gun_index, LED_ACTION_RFID_FAIL);
  5730. sleep(3);
  5731. setChargerMode(gun_index, SYS_MODE_IDLE);
  5732. ShmCharger->gun_info[gun_index].isEvCCIDAuthorizeFail = YES;
  5733. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5734. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5735. }
  5736. ocpp_set_auth_conf(OFF);
  5737. }
  5738. break;
  5739. case START_METHOD_RFID:
  5740. case START_METHOD_BLE:
  5741. case START_METHOD_FREE:
  5742. case START_METHOD_CMD:
  5743. default:
  5744. setChargerMode(gun_index, SYS_MODE_PREPARING);
  5745. break;
  5746. }
  5747. }
  5748. break;
  5749. case SYS_MODE_PREPARING:
  5750. if (isModeChange(gun_index))
  5751. {
  5752. DEBUG_INFO("========== SYS_MODE_PREPARING (%d)========== \n", gun_index);
  5753. //lwtest .. for GL testing control
  5754. if (ShmCharger->gun_info[gun_index].isCmdToPrepareMode != ON)
  5755. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_C;
  5756. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5757. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5758. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  5759. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  5760. setRequest(gun_index, ON);
  5761. sleep(3);
  5762. }
  5763. // If control pilot detect Bx, skip watch dog time out.
  5764. if ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_B)
  5765. || (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C))
  5766. {
  5767. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  5768. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  5769. switch (ShmCharger->gun_info[gun_index].ccsHandshakeState)
  5770. {
  5771. case HANDSHAKE_CP_STATE_E:
  5772. DEBUG_INFO("Determine max charging current to MCU.\n");
  5773. ocpp_set_profile_req(gun_index, ON);
  5774. sleep(1);
  5775. checkChargingProfileLimit(gun_index, getTargetChargingInfoData(gun_index)->SystemStatus);
  5776. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
  5777. refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  5778. sleep(1);
  5779. break;
  5780. case HANDSHAKE_SET_MAX_CURRENT:
  5781. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_BS_MODE;
  5782. DEBUG_INFO("Enter BS Mode charging.\n");
  5783. //for EV READY 30 secs didn't start charging to STATE E
  5784. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5785. sleep(1);
  5786. break;
  5787. case HANDSHAKE_BS_MODE:
  5788. refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  5789. checkChargingProfileLimit(gun_index, getTargetChargingInfoData(gun_index)->SystemStatus);
  5790. if (ShmCharger->gun_info[gun_index].targetCurrent != 0
  5791. && ShmCharger->gun_info[gun_index].isCmdToPrepareMode == OFF)
  5792. setRelay(gun_index, ON);
  5793. //---------------LW test for GL test platform start-----------------
  5794. else if (ShmCharger->gun_info[gun_index].isCmdToPrepareMode == ON)
  5795. {
  5796. //Plugin state and wait trigger start.
  5797. if (ShmCharger->gun_info[gun_index].isCmdToPrepareMode == ON
  5798. && (ocpp_get_remotestart(gun_index) == ON || ShmCharger->gun_info[gun_index].rfidReq == ON))
  5799. {
  5800. if (ocpp_get_remotestart(gun_index))
  5801. {
  5802. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_Central;
  5803. ocpp_copy_userid_from_remotestart(gun_index);
  5804. DEBUG_INFO("LW debug Message : GL Remote Start.\n");
  5805. ///////////////
  5806. if (ocpp_isAuthorizeRemoteStart())
  5807. {
  5808. DEBUG_INFO("LW debug Message : GL Remote Start request authorize.\n");
  5809. ocpp_set_auth_req(ON, "ISO14443");
  5810. if (ocpp_get_auth_conf())
  5811. {
  5812. if (ocpp_get_auth_result(gun_index))
  5813. {
  5814. memcpy( (char*) getTargetChargingInfoData(gun_index)->StartUserId,
  5815. ShmSysConfigAndInfo->SysConfig.UserId,
  5816. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5817. DEBUG_INFO("LW debug Message : GL Authorize pass.\n");
  5818. }
  5819. else
  5820. {
  5821. DEBUG_INFO("LW debug Message : GL Authorize fail.\n");
  5822. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5823. }
  5824. ocpp_set_auth_conf(OFF);
  5825. }
  5826. }
  5827. }
  5828. setRelay(gun_index, ON);
  5829. ShmCharger->gun_info[gun_index].isCmdToPrepareMode = OFF;
  5830. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5831. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_C;
  5832. }
  5833. //---------------LW test for GL test platform end-----------------
  5834. }
  5835. if ((getRelay(gun_index) == ON) || (ShmCharger->gun_info[gun_index].targetCurrent == 0))
  5836. {
  5837. ocpp_set_unlocker_req(gun_index, OFF);
  5838. presentChargedEnergyClear(gun_index);
  5839. getDateTimeString((char*) getTargetChargingInfoData(gun_index)->StartDateTime);
  5840. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  5841. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  5842. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  5843. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  5844. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  5845. getTargetChargingInfoData(gun_index)->PowerConsumption =
  5846. (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption / 10000.0);
  5847. else
  5848. {
  5849. getTargetChargingInfoData(gun_index)->PowerConsumption =
  5850. (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption / 10000.0);
  5851. }
  5852. //lwtest
  5853. if (ocpp_get_remotestart(gun_index))
  5854. {
  5855. memcpy((char*) getTargetChargingInfoData(gun_index)->StartUserId,
  5856. ShmSysConfigAndInfo->SysConfig.UserId,
  5857. ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  5858. }
  5859. else
  5860. {
  5861. memcpy((char*) getTargetChargingInfoData(gun_index)->StartUserId,
  5862. ShmCharger->gun_info[gun_index].AuthAcceptUserId,
  5863. ARRAY_SIZE(ShmCharger->gun_info[gun_index].AuthAcceptUserId));
  5864. }
  5865. ocpp_copy_userid_to_starttransaction(gun_index);
  5866. ocpp_set_starttransaction_req(gun_index, ON);
  5867. refreshStartTimer(&startChargingTime[gun_index]);
  5868. setChargerMode(gun_index, SYS_MODE_CHARGING);
  5869. }
  5870. sleep(1);
  5871. break;
  5872. default:
  5873. break;
  5874. }
  5875. }
  5876. // Unplug charging gun to Idle mode
  5877. if ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A)
  5878. && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  5879. {
  5880. DEBUG_INFO("The connector was connected to the EV before.\n");
  5881. //Cancel CCS task negotiating
  5882. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5883. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5884. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5885. }
  5886. // Use RFID card to stop handshaking
  5887. if ((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchPresentUser())
  5888. {
  5889. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  5890. //Cancel CCS task negotiating
  5891. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5892. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5893. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5894. //lwtest
  5895. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5896. }
  5897. else
  5898. {
  5899. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5900. }
  5901. //lwtest
  5902. if (getTargetChargingInfoData(gun_index)->schedule.isTriggerStop == ON)
  5903. {
  5904. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5905. }
  5906. checkHandshakeCountdown(gun_index);
  5907. if (getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  5908. {
  5909. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  5910. if (getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec + 5))
  5911. {
  5912. DEBUG_INFO("Handshaking timeout...\n");
  5913. //Cancel CCS task negotiating
  5914. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  5915. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  5916. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  5917. }
  5918. }
  5919. break;
  5920. case SYS_MODE_CHARGING:
  5921. if (isModeChange(gun_index))
  5922. {
  5923. DEBUG_INFO("========== SYS_MODE_CHARGING (%d) ========== \n", gun_index);
  5924. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  5925. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  5926. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5927. refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  5928. ocpp_set_auth_req(OFF);
  5929. ocpp_reset_smartcharging_profileId(gun_index);
  5930. ocpp_set_profile_req(gun_index, ON);
  5931. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  5932. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  5933. }
  5934. if ((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)
  5935. || (ShmCharger->gun_info[gun_index].rfidReq == ON)
  5936. || (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON)
  5937. || ocpp_get_remotestop(gun_index)
  5938. || (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index))
  5939. || ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE)
  5940. && getTargetChargingInfoData(gun_index)->PilotState != CP_STATE_C)
  5941. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  5942. && !ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn)
  5943. || ocpp_get_reset_req() || ocpp_get_unlocker_req(gun_index)
  5944. || (getTargetChargingInfoData(gun_index)->schedule.isTriggerStop == ON)
  5945. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5946. && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE))
  5947. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5948. && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress
  5949. == HLC_RENEGOTIATE_MODE))
  5950. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5951. && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE))
  5952. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  5953. && ((49 <= ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus)
  5954. && (ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus <= 255))))
  5955. {
  5956. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  5957. }
  5958. else
  5959. {
  5960. // Charging session info calculation
  5961. if (ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 1)
  5962. getTargetChargingInfoData(gun_index)->PowerConsumption =
  5963. (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption / 10000.0);
  5964. else
  5965. {
  5966. getTargetChargingInfoData(gun_index)->PowerConsumption =
  5967. (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption / 10000.0);
  5968. }
  5969. refreshStartTimer(&endChargingTime[gun_index]);
  5970. getTargetChargingInfoData(gun_index)->PresentChargedDuration =
  5971. getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
  5972. presentChargedEnergyUpdate(gun_index);
  5973. // Response StartTransactionConf
  5974. ocpp_set_starttransaction_conf(gun_index, OFF);
  5975. // Charging profile preparation
  5976. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  5977. || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  5978. {
  5979. if (!ocpp_get_profile_conf(gun_index))
  5980. {
  5981. ocpp_set_profile_req(gun_index, ON);
  5982. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  5983. }
  5984. else
  5985. {
  5986. ocpp_set_profile_conf(gun_index, OFF);
  5987. }
  5988. }
  5989. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  5990. checkChargingProfileLimit(gun_index, getTargetChargingInfoData(gun_index)->SystemStatus);
  5991. // Charging session local limit condition check
  5992. if (ocpp_get_connection_status())
  5993. {
  5994. // On-line max condition check
  5995. if ((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  5996. && (getTargetChargingInfoData(gun_index)->PresentChargedDuration
  5997. >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60)))
  5998. {
  5999. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6000. {
  6001. setRelay(gun_index, OFF);
  6002. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  6003. }
  6004. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6005. {
  6006. setRelay(gun_index, OFF);
  6007. }
  6008. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6009. {
  6010. setRelay(gun_index, OFF);
  6011. }
  6012. else
  6013. {
  6014. }
  6015. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  6016. DEBUG_INFO("Connector-%d charging duration(%d) already over max duration(%d) in second.\n",
  6017. gun_index, getTargetChargingInfoData(gun_index)->PresentChargedDuration,
  6018. (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60));
  6019. }
  6020. else if ((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  6021. && (getTargetChargingInfoData(gun_index)->PresentChargedEnergy
  6022. >= ((float) ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  6023. {
  6024. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6025. {
  6026. setRelay(gun_index, OFF);
  6027. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  6028. }
  6029. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6030. {
  6031. setRelay(gun_index, OFF);
  6032. }
  6033. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6034. {
  6035. setRelay(gun_index, OFF);
  6036. }
  6037. else
  6038. {
  6039. }
  6040. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  6041. DEBUG_INFO("Connector-%d charging energy(%.2f) already over max energy(%.2f) in KWH.\n",
  6042. gun_index, getTargetChargingInfoData(gun_index)->PresentChargedEnergy,
  6043. ((float )ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy));
  6044. }
  6045. else
  6046. {
  6047. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6048. {
  6049. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  6050. if (ShmCharger->gun_info[gun_index].targetCurrent > 0)
  6051. setRelay(gun_index, ON);
  6052. else
  6053. setRelay(gun_index, OFF);
  6054. }
  6055. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6056. {
  6057. if (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C)
  6058. setRelay(gun_index, ON);
  6059. }
  6060. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6061. {
  6062. if (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  6063. setRelay(gun_index, ON);
  6064. else
  6065. setRelay(gun_index, OFF);
  6066. }
  6067. else
  6068. {
  6069. }
  6070. }
  6071. }
  6072. else
  6073. {
  6074. // Off-line max condition check
  6075. if ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  6076. || ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE)
  6077. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  6078. {
  6079. if (((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  6080. ? (getTargetChargingInfoData(gun_index)->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  6081. : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  6082. && (getTargetChargingInfoData(gun_index)->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))))
  6083. {
  6084. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6085. {
  6086. setRelay(gun_index, OFF);
  6087. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  6088. }
  6089. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6090. {
  6091. setRelay(gun_index, OFF);
  6092. }
  6093. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6094. {
  6095. setRelay(gun_index, OFF);
  6096. }
  6097. else
  6098. {
  6099. }
  6100. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  6101. DEBUG_INFO( "Connector-%d charging duration(%d) already over off-line max duration(%d) in second.\n",
  6102. gun_index, getTargetChargingInfoData(gun_index)->PresentChargedDuration,
  6103. (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60));
  6104. }
  6105. else if (((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  6106. ? (getTargetChargingInfoData(gun_index)->PresentChargedEnergy >= ((float) ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  6107. : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  6108. && (getTargetChargingInfoData(gun_index)->PresentChargedEnergy >= ((float) ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
  6109. {
  6110. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6111. {
  6112. setRelay(gun_index, OFF);
  6113. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  6114. }
  6115. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6116. {
  6117. setRelay(gun_index, OFF);
  6118. }
  6119. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6120. {
  6121. setRelay(gun_index, OFF);
  6122. }
  6123. else
  6124. {
  6125. }
  6126. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  6127. DEBUG_INFO( "Connector-%d charging energy(%.2f) already over off-line max energy(%.2f) in KWH.\n",
  6128. gun_index, getTargetChargingInfoData(gun_index)->PresentChargedEnergy,
  6129. ((float )ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy));
  6130. }
  6131. else
  6132. {
  6133. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6134. {
  6135. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  6136. if (ShmCharger->gun_info[gun_index].targetCurrent > 0)
  6137. setRelay(gun_index, ON);
  6138. else
  6139. setRelay(gun_index, OFF);
  6140. }
  6141. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6142. {
  6143. if (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C)
  6144. setRelay(gun_index, ON);
  6145. }
  6146. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6147. {
  6148. if (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  6149. setRelay(gun_index, ON);
  6150. else
  6151. setRelay(gun_index, OFF);
  6152. }
  6153. else
  6154. {
  6155. }
  6156. }
  6157. }
  6158. else
  6159. {
  6160. if (ShmCharger->gun_info[gun_index].primaryMcuState.relay_state > 0)
  6161. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  6162. setRelay(gun_index, OFF);
  6163. }
  6164. }
  6165. // Debug information
  6166. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  6167. || (getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
  6168. {
  6169. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  6170. getDateTimeString((char*) getTargetChargingInfoData(gun_index)->StopDateTime);
  6171. DB_Update_Record_Buf(localDb, gun_index);
  6172. }
  6173. }
  6174. break;
  6175. case SYS_MODE_TERMINATING:
  6176. if (isModeChange(gun_index))
  6177. {
  6178. DEBUG_INFO("========== SYS_MODE_TERMINATING (%d)========== \n", gun_index);
  6179. if (!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  6180. {
  6181. setLedMotion(gun_index, LED_ACTION_STOP);
  6182. }
  6183. else
  6184. {
  6185. }
  6186. getDateTimeString((char*) getTargetChargingInfoData(gun_index)->StopDateTime);
  6187. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  6188. if (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)
  6189. setRelay(gun_index, OFF);
  6190. }
  6191. refreshStartTimer(&endChargingTime[gun_index]);
  6192. getTargetChargingInfoData(gun_index)->PresentChargedDuration = getDiffSecBetween(
  6193. startChargingTime[gun_index], endChargingTime[gun_index]);
  6194. // End authorize pass
  6195. if (((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser())
  6196. || ((ShmCharger->gun_info[gun_index].rfidReq == ON) && ocpp_get_auth_result(YES, gun_index))
  6197. || (!ocpp_get_connection_status()
  6198. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)
  6199. && (ShmCharger->gun_info[gun_index].rfidReq == ON))
  6200. || (!ocpp_get_connection_status() && (isValidLocalWhiteCard() == PASS)
  6201. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)
  6202. && (ShmCharger->gun_info[gun_index].rfidReq == ON))
  6203. || (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON)
  6204. || ocpp_get_remotestop(gun_index)
  6205. || (ocpp_get_connection_status() && !ocpp_get_starttransaction_result(gun_index))
  6206. || ((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE)
  6207. && ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A)
  6208. || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES))
  6209. && ocpp_get_StopTransactionOnEVSideDisconnect())
  6210. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6211. && ((!ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn)
  6212. || (ShmCharger->gun_info[gun_index].isGunUnpluggedBefore == YES)))
  6213. || ocpp_get_reset_req() || ocpp_get_unlocker_req(gun_index)
  6214. || (getTargetChargingInfoData(gun_index)->schedule.isTriggerStop == ON)
  6215. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6216. && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE)))
  6217. {
  6218. setLedMotion(gun_index, LED_ACTION_STOP);
  6219. if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6220. {
  6221. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  6222. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  6223. sleep(1);
  6224. checkStopReason(gun_index);
  6225. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6226. }
  6227. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6228. {
  6229. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6230. //Cancel CCS task negotiating
  6231. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  6232. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
  6233. setRelay(gun_index, OFF);
  6234. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  6235. }
  6236. else
  6237. {
  6238. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6239. }
  6240. }
  6241. else
  6242. {
  6243. // If the charger stops charging, the led should be stoped blink
  6244. if (!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  6245. {
  6246. setLedMotion(gun_index, LED_ACTION_STOP);
  6247. }
  6248. // Charging profile preparation
  6249. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  6250. || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
  6251. {
  6252. if (!ocpp_get_profile_conf(gun_index))
  6253. {
  6254. ocpp_set_profile_req(gun_index, ON);
  6255. refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  6256. }
  6257. else
  6258. {
  6259. ocpp_set_profile_conf(gun_index, OFF);
  6260. }
  6261. }
  6262. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  6263. checkChargingProfileLimit(gun_index, getTargetChargingInfoData(gun_index)->SystemStatus);
  6264. setRelay(gun_index, OFF);
  6265. // Debug information
  6266. if ((getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO])
  6267. > TIMEOUT_SPEC_LOGPPRINTOUT)
  6268. || (getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
  6269. {
  6270. refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
  6271. }
  6272. if (!ocpp_get_auth_result(YES, gun_index))
  6273. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  6274. if ((ShmCharger->gun_info[gun_index].isGunUnpluggedBefore != YES)
  6275. && (((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE)
  6276. && (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_C))
  6277. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6278. && ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn))
  6279. && (ShmCharger->gun_info[gun_index].rfidReq != ON)
  6280. && (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON)
  6281. && !ocpp_get_remotestop(gun_index)
  6282. && (getTargetChargingInfoData(gun_index)->schedule.isTriggerStop != ON)
  6283. && !ocpp_get_reset_req() && ocpp_get_starttransaction_result(gun_index)
  6284. && !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  6285. && (getTargetChargingInfoData(gun_index)->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60)))
  6286. && !(ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  6287. && (getTargetChargingInfoData(gun_index)->PresentChargedEnergy >= ((float) ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  6288. && !(!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)
  6289. && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0)
  6290. ? (getTargetChargingInfoData(gun_index)->PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration * 60))
  6291. : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0)
  6292. && (getTargetChargingInfoData(gun_index)->PresentChargedDuration
  6293. >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration * 60))))
  6294. && !(!ocpp_get_connection_status()
  6295. && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)
  6296. && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0)
  6297. ? (getTargetChargingInfoData(gun_index)->PresentChargedEnergy >= ((float) ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy))
  6298. : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0)
  6299. && (getTargetChargingInfoData(gun_index)->PresentChargedEnergy >= ((float) ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
  6300. && (((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6301. && (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE))
  6302. || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6303. || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)))
  6304. {
  6305. sleep(1);
  6306. setChargerMode(gun_index, SYS_MODE_CHARGING);
  6307. }
  6308. }
  6309. if ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
  6310. && (ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON)
  6311. && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100))
  6312. {
  6313. DEBUG_INFO("Set PWM duty 100%% go to SYS_MODE_TERMINATING.\n");
  6314. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  6315. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  6316. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  6317. sleep(1);
  6318. checkStopReason(gun_index);
  6319. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  6320. }
  6321. break;
  6322. case SYS_MODE_COMPLETE:
  6323. if (isModeChange(gun_index))
  6324. {
  6325. DEBUG_INFO("========== SYS_MODE_COMPLETE (%d)========== \n", gun_index);
  6326. setLedMotion(gun_index, LED_ACTION_STOP);
  6327. sleep(6);
  6328. setRelay(gun_index, OFF);
  6329. setRequest(gun_index, OFF);
  6330. getTargetChargingInfoData(gun_index)->PilotState = CP_STATE_A;
  6331. }
  6332. if (((ShmCharger->gun_info[gun_index].chargingMode != CHARGING_MODE_SOCKETE)
  6333. && (getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A))
  6334. || ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6335. && (!ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn))
  6336. || ocpp_get_reset_req())
  6337. {
  6338. sleep(2);
  6339. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  6340. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  6341. ocpp_set_remotestop(gun_index, OFF);
  6342. getTargetChargingInfoData(gun_index)->schedule.isTriggerStop = OFF;
  6343. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = NO;
  6344. DB_Insert_Record(localDb, gun_index);
  6345. setChargerMode(gun_index, SYS_MODE_IDLE);
  6346. }
  6347. break;
  6348. case SYS_MODE_ALARM:
  6349. setLedMotion(gun_index, LED_ACTION_ALARM);
  6350. setRelay(gun_index, OFF);
  6351. DEBUG_INFO("========== SYS_MODE_ALARM ========== \n");
  6352. if ((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
  6353. || (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC))
  6354. {
  6355. if ((getTargetChargingInfoData(gun_index)->PilotState == CP_STATE_A))
  6356. {
  6357. setRequest(gun_index, OFF);
  6358. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  6359. }
  6360. }
  6361. else if (ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_SOCKETE)
  6362. {
  6363. if (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn != ON)
  6364. {
  6365. setRequest(gun_index, OFF);
  6366. ShmCharger->gun_info[gun_index].isGunUnpluggedBefore = YES;
  6367. }
  6368. }
  6369. else
  6370. {
  6371. }
  6372. /*
  6373. * 1. Customize emergency stop and report stop transaction then return to idle mode if error recover.
  6374. * 2. Customer: Bambang
  6375. */
  6376. if (((ShmSysConfigAndInfo->SysConfig.ModelName[VENDOR_CODE] == 'B')
  6377. && (ShmSysConfigAndInfo->SysConfig.ModelName[VENDOR_CODE + 1] == 'B'))
  6378. && (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == ALARM_EMERGENCY_STOP))
  6379. {
  6380. if (((getTargetChargingInfoData(gun_index)->PreviousSystemStatus == SYS_MODE_CHARGING)
  6381. || (getTargetChargingInfoData(gun_index)->PreviousSystemStatus == SYS_MODE_TERMINATING))
  6382. && !ShmCharger->gun_info[gun_index].isEmergencyStopReport)
  6383. {
  6384. checkStopReason(gun_index);
  6385. ShmCharger->gun_info[gun_index].isEmergencyStopReport = ON;
  6386. }
  6387. }
  6388. if ((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0)
  6389. && !ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
  6390. {
  6391. if (((getTargetChargingInfoData(gun_index)->PreviousSystemStatus == SYS_MODE_CHARGING)
  6392. || (getTargetChargingInfoData(gun_index)->PreviousSystemStatus == SYS_MODE_TERMINATING))
  6393. && (!ShmCharger->gun_info[gun_index].isEmergencyStopReport))
  6394. {
  6395. setChargerMode(gun_index, getTargetChargingInfoData(gun_index)->PreviousSystemStatus);
  6396. }
  6397. else
  6398. {
  6399. setChargerMode(gun_index, SYS_MODE_IDLE);
  6400. }
  6401. }
  6402. break;
  6403. case SYS_MODE_FAULT:
  6404. if (isModeChange(gun_index))
  6405. {
  6406. DEBUG_INFO("========== SYS_MODE_FAULT (%d) ========== \n", gun_index);
  6407. }
  6408. break;
  6409. case SYS_MODE_MAINTAIN:
  6410. if (isModeChange(gun_index))
  6411. {
  6412. setLedMotion(gun_index, LED_ACTION_MAINTAIN);
  6413. DEBUG_INFO("========== SYS_MODE_MAINTAIN (%d) ========== \n", gun_index);
  6414. }
  6415. break;
  6416. case SYS_MODE_UPDATE:
  6417. if (isModeChange(gun_index))
  6418. {
  6419. DEBUG_INFO("========== SYS_MODE_UPDATE (%d) ========== \n", gun_index);
  6420. setLedMotion(gun_index, LED_ACTION_MAINTAIN);
  6421. }
  6422. //======================================
  6423. // Check local upgrade firmware request
  6424. //======================================
  6425. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  6426. {
  6427. DEBUG_INFO("Firmware local upgrading...\n");
  6428. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  6429. int value = upgrade_check();
  6430. DEBUG_INFO("Local update Value: %s... \n", ((value == PASS)?"Pass": "Fail"));
  6431. if (value == PASS)
  6432. {
  6433. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  6434. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  6435. ShmCharger->isUpdateSuccess = YES;
  6436. DEBUG_INFO("Local update success...\n");
  6437. }
  6438. else
  6439. {
  6440. if (ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  6441. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  6442. ShmCharger->isUpdateSuccess = NO;
  6443. DEBUG_INFO("Local update unsuccess...\n");
  6444. }
  6445. }
  6446. else if (ocpp_get_update_firmware_req())
  6447. {
  6448. //======================================
  6449. // Check remote upgrade firmware request
  6450. //======================================
  6451. checkRemoteUpgradeStatus();
  6452. }
  6453. else
  6454. {
  6455. //======================================
  6456. // Upgrade complete reboot system
  6457. //======================================
  6458. if (!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq
  6459. && ((modelnameInfo.GetGunCount > 1) ? !ShmCharger->gun_info[gun_index ^ 1].mcuFlag.isMcuUpgradeReq : YES)
  6460. && !ShmCharger->isUpgradeLcmReq)
  6461. {
  6462. if (ShmCharger->isUpdateSuccess == YES)
  6463. {
  6464. sprintf((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  6465. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6466. sprintf((char*) ShmOCPP20Data->FirmwareStatusNotification.status, "Installed");
  6467. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6468. DEBUG_WARN("Firmware upgrade success.\n");
  6469. sleep(5);
  6470. DEBUG_INFO("Firmware upgraded, reboot...\n");
  6471. system("reboot -f");
  6472. sleep(5);
  6473. system("reboot -f");
  6474. }
  6475. else
  6476. {
  6477. sprintf((char*) ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  6478. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6479. sprintf((char*) ShmOCPP20Data->FirmwareStatusNotification.status, "InstallationFailed");
  6480. ShmOCPP20Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  6481. DEBUG_WARN("Firmware upgrade fail.\n");
  6482. sleep(5);
  6483. system("rm -rvf /mnt/* ");
  6484. close(wtdFd);
  6485. system("/usr/bin/run_evse_restart.sh");
  6486. }
  6487. }
  6488. }
  6489. break;
  6490. case SYS_MODE_RESERVATION:
  6491. if (isModeChange(gun_index))
  6492. {
  6493. DEBUG_INFO("========== SYS_MODE_RESERVATION (%d)========== \n", gun_index);
  6494. }
  6495. setLedMotion(gun_index, LED_ACTION_RESERVATION_MODE);
  6496. if (isReservationExpired(gun_index))
  6497. {
  6498. DEBUG_INFO("Reservation: Time's up...\n");
  6499. setChargerMode(gun_index, SYS_MODE_IDLE);
  6500. }
  6501. else if (ocpp_get_cancelreservation_req(gun_index))
  6502. {
  6503. DEBUG_INFO("Reservation: Cancel reservation...\n");
  6504. setChargerMode(gun_index, SYS_MODE_IDLE);
  6505. ocpp_set_cancelreservation_req(gun_index, OFF);
  6506. }
  6507. else
  6508. {
  6509. // Check is there RFID or back end request start
  6510. if ((ShmCharger->gun_info[gun_index].rfidReq == ON) || ocpp_get_remotestart(gun_index))
  6511. {
  6512. if ((ShmCharger->gun_info[gun_index].rfidReq == ON))
  6513. {
  6514. if (ocpp_compare_reserve_id_with_user(gun_index))
  6515. {
  6516. DEBUG_INFO("Start Method in reservation : RFID...\n");
  6517. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  6518. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_RFID;
  6519. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_ISO14443;
  6520. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  6521. }
  6522. else
  6523. {
  6524. DEBUG_INFO("It's not reserve user id.\n");
  6525. ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
  6526. }
  6527. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  6528. }
  6529. else if (ocpp_get_remotestart(gun_index))
  6530. {
  6531. if (ocpp_compare_reserve_id_with_remote_user(gun_index))
  6532. {
  6533. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  6534. getTargetChargingInfoData(gun_index)->StartMethod = START_METHOD_BACKEND;
  6535. getTargetChargingInfoData(gun_index)->StartIdType = IdTokenType_Central;
  6536. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  6537. }
  6538. ocpp_set_remotestop(gun_index, OFF);
  6539. }
  6540. }
  6541. }
  6542. break;
  6543. case SYS_MODE_BOOKING:
  6544. if (isModeChange(gun_index))
  6545. {
  6546. DEBUG_INFO("========== SYS_MODE_BOOKING (%d)========== \n", gun_index);
  6547. }
  6548. break;
  6549. case SYS_MODE_DEBUG:
  6550. if (isModeChange(gun_index))
  6551. {
  6552. setLedMotion(gun_index, LED_ACTION_DEBUG);
  6553. DEBUG_INFO("========== SYS_MODE_DEBUG (%d)========== \n", gun_index);
  6554. }
  6555. break;
  6556. }
  6557. }
  6558. // System watch dog reset
  6559. write(wtdFd, "a", 1);
  6560. usleep(50000);
  6561. }
  6562. return FAIL;
  6563. }